* Helper function to optimize a FROM clause while tracking what was actually optimized. * * @param from - FROM clause to optimize * @param singleSourceClauses - Map of source aliases to their WHERE clauses * @param actuallyOptimized - Set to track which sources were actually optimized * @return
( from: From, singleSourceClauses: Map<string, BasicExpression<boolean>>, actuallyOptimized: Set<string>, )
| 923 | * @returns New FROM clause, potentially wrapped in a subquery |
| 924 | */ |
| 925 | function optimizeFromWithTracking( |
| 926 | from: From, |
| 927 | singleSourceClauses: Map<string, BasicExpression<boolean>>, |
| 928 | actuallyOptimized: Set<string>, |
| 929 | ): From { |
| 930 | if (from.type === `unionFrom`) { |
| 931 | return new UnionFromClass( |
| 932 | from.sources.map((source) => |
| 933 | optimizeJoinFromWithTracking( |
| 934 | source, |
| 935 | singleSourceClauses, |
| 936 | actuallyOptimized, |
| 937 | ), |
| 938 | ), |
| 939 | ) |
| 940 | } |
| 941 | |
| 942 | if (from.type === `unionAll`) { |
| 943 | return new UnionAllClass( |
| 944 | from.queries.map((branch) => deepCopyQuery(branch)), |
| 945 | ) |
| 946 | } |
| 947 | |
| 948 | const whereClause = singleSourceClauses.get(from.alias) |
| 949 | |
| 950 | if (!whereClause) { |
| 951 | // No optimization needed, but return a copy to maintain immutability |
| 952 | if (from.type === `collectionRef`) { |
| 953 | return new CollectionRefClass(from.collection, from.alias) |
| 954 | } |
| 955 | // Must be queryRef due to type system |
| 956 | return new QueryRefClass(deepCopyQuery(from.query), from.alias) |
| 957 | } |
| 958 | |
| 959 | if (from.type === `collectionRef`) { |
| 960 | // Create a new subquery with the WHERE clause for the collection |
| 961 | // This is always safe since we're creating a new subquery |
| 962 | const subQuery: QueryIR = { |
| 963 | from: new CollectionRefClass(from.collection, from.alias), |
| 964 | where: [whereClause], |
| 965 | } |
| 966 | actuallyOptimized.add(from.alias) // Mark as successfully optimized |
| 967 | return new QueryRefClass(subQuery, from.alias) |
| 968 | } |
| 969 | |
| 970 | // SAFETY CHECK: Only check safety when pushing WHERE clauses into existing subqueries |
| 971 | // We need to be careful about pushing WHERE clauses into subqueries that already have |
| 972 | // aggregates, HAVING, or ORDER BY + LIMIT since that could change their semantics |
| 973 | if (!isSafeToPushIntoExistingSubquery(from.query, whereClause, from.alias)) { |
| 974 | // Return a copy without optimization to maintain immutability |
| 975 | // Do NOT mark as optimized since we didn't actually optimize it |
| 976 | return new QueryRefClass(deepCopyQuery(from.query), from.alias) |
| 977 | } |
| 978 | |
| 979 | // Skip pushdown when a clause references a field that only exists via a renamed |
| 980 | // projection inside the subquery; leaving it outside preserves the alias mapping. |
| 981 | if (referencesAliasWithRemappedSelect(from.query, whereClause, from.alias)) { |
| 982 | return new QueryRefClass(deepCopyQuery(from.query), from.alias) |
no test coverage detected