* Determines whether the provided WHERE clause references fields that are * computed by a subquery SELECT rather than pass-through properties. * * If true, pushing the WHERE clause into the subquery could change semantics * (since computed fields do not necessarily exist at the subquery input le
( select: Select, whereClause: BasicExpression<boolean>, outerAlias: string, )
| 1118 | * @returns True if WHERE references computed fields, otherwise false |
| 1119 | */ |
| 1120 | function whereReferencesComputedSelectFields( |
| 1121 | select: Select, |
| 1122 | whereClause: BasicExpression<boolean>, |
| 1123 | outerAlias: string, |
| 1124 | ): boolean { |
| 1125 | // Build a set of computed field names at the top-level of the subquery output |
| 1126 | const computed = new Set<string>() |
| 1127 | for (const [key, value] of Object.entries(select)) { |
| 1128 | if (key.startsWith(`__SPREAD_SENTINEL__`)) continue |
| 1129 | if (value instanceof PropRef) continue |
| 1130 | // Nested object or non-PropRef expression counts as computed |
| 1131 | computed.add(key) |
| 1132 | } |
| 1133 | |
| 1134 | const refs = collectRefs(whereClause) |
| 1135 | |
| 1136 | for (const ref of refs) { |
| 1137 | const path = (ref as any).path as Array<string> |
| 1138 | if (!Array.isArray(path) || path.length < 2) continue |
| 1139 | const alias = path[0] |
| 1140 | const field = path[1] as string |
| 1141 | if (alias !== outerAlias) continue |
| 1142 | if (computed.has(field)) return true |
| 1143 | } |
| 1144 | return false |
| 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * Detects whether a WHERE clause references the subquery alias through fields that |
no test coverage detected