* Optimizes OR expressions
( expression: BasicExpression, collection: CollectionLike<T, TKey>, )
| 683 | * Optimizes OR expressions |
| 684 | */ |
| 685 | function optimizeOrExpression<T extends object, TKey extends string | number>( |
| 686 | expression: BasicExpression, |
| 687 | collection: CollectionLike<T, TKey>, |
| 688 | ): OptimizationResult<TKey> { |
| 689 | if (expression.type !== `func` || expression.args.length < 2) { |
| 690 | return { canOptimize: false, matchingKeys: new Set(), isExact: false } |
| 691 | } |
| 692 | |
| 693 | const results: Array<OptimizationResult<TKey>> = [] |
| 694 | |
| 695 | // Every disjunct must be optimizable: rows matched only by a disjunct |
| 696 | // that cannot use an index would be missing from the union, and no |
| 697 | // post-filtering can recover them. In that case fall back to a full scan. |
| 698 | for (const arg of expression.args) { |
| 699 | const result = optimizeQueryRecursive(arg, collection) |
| 700 | if (!result.canOptimize) { |
| 701 | return { canOptimize: false, matchingKeys: new Set(), isExact: false } |
| 702 | } |
| 703 | results.push(result) |
| 704 | } |
| 705 | |
| 706 | // Use unionSets utility for OR logic |
| 707 | const allMatchingSets = results.map((r) => r.matchingKeys) |
| 708 | const unionedKeys = unionSets(allMatchingSets) |
| 709 | return { |
| 710 | canOptimize: true, |
| 711 | matchingKeys: unionedKeys, |
| 712 | // An inexact (superset) disjunct makes the union a superset as well |
| 713 | isExact: results.every((r) => r.isExact), |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * Checks if an OR expression can be optimized |
no test coverage detected