( subset: BasicExpression<boolean>, superset: BasicExpression<boolean>, )
| 63 | } |
| 64 | |
| 65 | function isWhereSubsetInternal( |
| 66 | subset: BasicExpression<boolean>, |
| 67 | superset: BasicExpression<boolean>, |
| 68 | ): boolean { |
| 69 | // If subset is false it is requesting no data, |
| 70 | // thus the result set is empty |
| 71 | // and the empty set is a subset of any set |
| 72 | if (subset.type === `val` && subset.value === false) { |
| 73 | return true |
| 74 | } |
| 75 | |
| 76 | // If expressions are structurally equal, subset relationship holds |
| 77 | if (areExpressionsEqual(subset, superset)) { |
| 78 | return true |
| 79 | } |
| 80 | |
| 81 | // Handle superset being an AND: subset must imply ALL conjuncts |
| 82 | // If superset is (A AND B), then subset ⊆ (A AND B) only if subset ⊆ A AND subset ⊆ B |
| 83 | // Example: (age > 20) ⊆ (age > 10 AND status = 'active') is false (doesn't imply status condition) |
| 84 | if (superset.type === `func` && superset.name === `and`) { |
| 85 | return superset.args.every((arg) => |
| 86 | isWhereSubsetInternal(subset, arg as BasicExpression<boolean>), |
| 87 | ) |
| 88 | } |
| 89 | |
| 90 | // Handle OR in subset: (A OR B) ⊆ C only if both A ⊆ C and B ⊆ C. |
| 91 | // Must be checked before OR superset so that or(A, B) ⊆ or(C, D) |
| 92 | // decomposes the subset first: A ⊆ or(C, D) AND B ⊆ or(C, D). |
| 93 | if (subset.type === `func` && subset.name === `or`) { |
| 94 | return subset.args.every((arg) => |
| 95 | isWhereSubsetInternal(arg as BasicExpression<boolean>, superset), |
| 96 | ) |
| 97 | } |
| 98 | |
| 99 | // Handle OR in superset: subset ⊆ (A OR B) if subset ⊆ A or subset ⊆ B. |
| 100 | // Must be checked before decomposing AND subsets so that and(A, B) can |
| 101 | // match a structurally equal disjunct via areExpressionsEqual. |
| 102 | if (superset.type === `func` && superset.name === `or`) { |
| 103 | return superset.args.some((arg) => |
| 104 | isWhereSubsetInternal(subset, arg as BasicExpression<boolean>), |
| 105 | ) |
| 106 | } |
| 107 | |
| 108 | // Handle subset being an AND: (A AND B) implies both A and B |
| 109 | if (subset.type === `func` && subset.name === `and`) { |
| 110 | // For (A AND B) ⊆ C, since (A AND B) implies A, we check if any conjunct implies C |
| 111 | return subset.args.some((arg) => |
| 112 | isWhereSubsetInternal(arg as BasicExpression<boolean>, superset), |
| 113 | ) |
| 114 | } |
| 115 | |
| 116 | // Turn x IN [A, B, C] into x = A OR x = B OR x = C |
| 117 | // for unified handling of IN and OR |
| 118 | if (subset.type === `func` && subset.name === `in`) { |
| 119 | const inField = extractInField(subset) |
| 120 | if (inField) { |
| 121 | return isWhereSubsetInternal(convertInToOr(inField), superset) |
| 122 | } |
no test coverage detected