@@ -178,7 +178,7 @@ function combineWherePredicates(
178178 const flatPredicates : Array < BasicExpression < boolean > > = [ ]
179179 for ( const pred of predicates ) {
180180 if ( pred . type === `func` && pred . name === operation ) {
181- flatPredicates . push ( ...( pred . args as Array < BasicExpression < boolean > > ) )
181+ flatPredicates . push ( ...pred . args )
182182 } else {
183183 flatPredicates . push ( pred )
184184 }
@@ -231,7 +231,6 @@ function combineWherePredicates(
231231 } as BasicExpression < boolean >
232232}
233233
234-
235234/**
236235 * Combine multiple where predicates with OR logic (union).
237236 * Returns a predicate that is satisfied when any input predicate is satisfied.
@@ -321,15 +320,24 @@ export function minusWherePredicates(
321320 }
322321
323322 // Try to detect and handle common conditions
324- const commonConditions = findCommonConditions ( fromPredicate , subtractPredicate )
323+ const commonConditions = findCommonConditions (
324+ fromPredicate ,
325+ subtractPredicate
326+ )
325327 if ( commonConditions . length > 0 ) {
326328 // Extract predicates without common conditions
327329 const fromWithoutCommon = removeConditions ( fromPredicate , commonConditions )
328- const subtractWithoutCommon = removeConditions ( subtractPredicate , commonConditions )
329-
330+ const subtractWithoutCommon = removeConditions (
331+ subtractPredicate ,
332+ commonConditions
333+ )
334+
330335 // Recursively compute difference on simplified predicates
331- const simplifiedDifference = minusWherePredicates ( fromWithoutCommon , subtractWithoutCommon )
332-
336+ const simplifiedDifference = minusWherePredicates (
337+ fromWithoutCommon ,
338+ subtractWithoutCommon
339+ )
340+
333341 if ( simplifiedDifference !== null ) {
334342 // Combine the simplified difference with common conditions
335343 return combineConditions ( [ ...commonConditions , simplifiedDifference ] )
@@ -755,8 +763,6 @@ export function isPredicateSubset(
755763 )
756764}
757765
758-
759-
760766// ============================================================================
761767// Helper functions
762768// ============================================================================
@@ -771,36 +777,38 @@ function findCommonConditions(
771777) : Array < BasicExpression < boolean > > {
772778 const conditions1 = extractAllConditions ( predicate1 )
773779 const conditions2 = extractAllConditions ( predicate2 )
774-
780+
775781 const common : Array < BasicExpression < boolean > > = [ ]
776-
782+
777783 for ( const cond1 of conditions1 ) {
778784 for ( const cond2 of conditions2 ) {
779785 if ( areExpressionsEqual ( cond1 , cond2 ) ) {
780786 // Avoid duplicates
781- if ( ! common . some ( c => areExpressionsEqual ( c , cond1 ) ) ) {
787+ if ( ! common . some ( ( c ) => areExpressionsEqual ( c , cond1 ) ) ) {
782788 common . push ( cond1 )
783789 }
784790 break
785791 }
786792 }
787793 }
788-
794+
789795 return common
790796}
791797
792798/**
793799 * Extract all individual conditions from a predicate, flattening AND operations.
794800 */
795- function extractAllConditions ( predicate : BasicExpression < boolean > ) : Array < BasicExpression < boolean > > {
801+ function extractAllConditions (
802+ predicate : BasicExpression < boolean >
803+ ) : Array < BasicExpression < boolean > > {
796804 if ( predicate . type === `func` && predicate . name === `and` ) {
797805 const conditions : Array < BasicExpression < boolean > > = [ ]
798806 for ( const arg of predicate . args ) {
799807 conditions . push ( ...extractAllConditions ( arg as BasicExpression < boolean > ) )
800808 }
801809 return conditions
802810 }
803-
811+
804812 return [ predicate ]
805813}
806814
@@ -814,9 +822,12 @@ function removeConditions(
814822) : BasicExpression < boolean > | undefined {
815823 if ( predicate . type === `func` && predicate . name === `and` ) {
816824 const remainingArgs = predicate . args . filter (
817- ( arg ) => ! conditionsToRemove . some ( cond => areExpressionsEqual ( arg as BasicExpression < boolean > , cond ) )
818- ) as Array < BasicExpression < boolean > >
819-
825+ ( arg ) =>
826+ ! conditionsToRemove . some ( ( cond ) =>
827+ areExpressionsEqual ( arg as BasicExpression < boolean > , cond )
828+ )
829+ )
830+
820831 if ( remainingArgs . length === 0 ) {
821832 return undefined
822833 } else if ( remainingArgs . length === 1 ) {
@@ -829,7 +840,7 @@ function removeConditions(
829840 } as BasicExpression < boolean >
830841 }
831842 }
832-
843+
833844 // For non-AND predicates, don't remove anything
834845 return predicate
835846}
@@ -838,27 +849,26 @@ function removeConditions(
838849 * Combine multiple conditions into a single predicate using AND logic.
839850 * Flattens nested AND operations to avoid unnecessary nesting.
840851 */
841- function combineConditions ( conditions : Array < BasicExpression < boolean > > ) : BasicExpression < boolean > {
842- // Filter out undefined conditions
843- const validConditions = conditions . filter ( ( c ) : c is BasicExpression < boolean > => c !== undefined )
844-
845- if ( validConditions . length === 0 ) {
852+ function combineConditions (
853+ conditions : Array < BasicExpression < boolean > >
854+ ) : BasicExpression < boolean > {
855+ if ( conditions . length === 0 ) {
846856 return { type : `val` , value : true } as BasicExpression < boolean >
847- } else if ( validConditions . length === 1 ) {
848- return validConditions [ 0 ] !
857+ } else if ( conditions . length === 1 ) {
858+ return conditions [ 0 ] !
849859 } else {
850860 // Flatten all conditions, including those that are already AND operations
851861 const flattenedConditions : Array < BasicExpression < boolean > > = [ ]
852-
853- for ( const condition of validConditions ) {
862+
863+ for ( const condition of conditions ) {
854864 if ( condition . type === `func` && condition . name === `and` ) {
855865 // Flatten nested AND operations
856- flattenedConditions . push ( ...( condition . args as Array < BasicExpression < boolean > > ) )
866+ flattenedConditions . push ( ...condition . args )
857867 } else {
858868 flattenedConditions . push ( condition )
859869 }
860870 }
861-
871+
862872 if ( flattenedConditions . length === 1 ) {
863873 return flattenedConditions [ 0 ] !
864874 } else {
@@ -1002,7 +1012,6 @@ function arrayIncludesWithSet(
10021012 return array . some ( ( v ) => areValuesEqual ( v , value ) )
10031013}
10041014
1005-
10061015/**
10071016 * Get the maximum of two values, handling both numbers and Dates
10081017 */
@@ -1210,7 +1219,6 @@ function groupPredicatesByField(
12101219 return groups
12111220}
12121221
1213-
12141222function unionSameFieldPredicates (
12151223 predicates : Array < BasicExpression < boolean > >
12161224) : BasicExpression < boolean > | null {
0 commit comments