@quereus/quereus 4.6.0 → 4.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (535) hide show
  1. package/README.md +2 -2
  2. package/dist/src/common/errors.d.ts +17 -0
  3. package/dist/src/common/errors.d.ts.map +1 -1
  4. package/dist/src/common/errors.js +21 -0
  5. package/dist/src/common/errors.js.map +1 -1
  6. package/dist/src/core/database-assertions.d.ts +4 -3
  7. package/dist/src/core/database-assertions.d.ts.map +1 -1
  8. package/dist/src/core/database-assertions.js +44 -14
  9. package/dist/src/core/database-assertions.js.map +1 -1
  10. package/dist/src/core/database-events.d.ts +62 -0
  11. package/dist/src/core/database-events.d.ts.map +1 -1
  12. package/dist/src/core/database-events.js +88 -2
  13. package/dist/src/core/database-events.js.map +1 -1
  14. package/dist/src/core/database-internal.d.ts +5 -2
  15. package/dist/src/core/database-internal.d.ts.map +1 -1
  16. package/dist/src/core/database-materialized-views-plan-builders.d.ts.map +1 -1
  17. package/dist/src/core/database-materialized-views-plan-builders.js +4 -2
  18. package/dist/src/core/database-materialized-views-plan-builders.js.map +1 -1
  19. package/dist/src/core/database-materialized-views-plans.d.ts +12 -2
  20. package/dist/src/core/database-materialized-views-plans.d.ts.map +1 -1
  21. package/dist/src/core/database-materialized-views.d.ts +46 -0
  22. package/dist/src/core/database-materialized-views.d.ts.map +1 -1
  23. package/dist/src/core/database-materialized-views.js +75 -4
  24. package/dist/src/core/database-materialized-views.js.map +1 -1
  25. package/dist/src/core/database.d.ts +9 -1
  26. package/dist/src/core/database.d.ts.map +1 -1
  27. package/dist/src/core/database.js +36 -10
  28. package/dist/src/core/database.js.map +1 -1
  29. package/dist/src/core/derived-row-validator.d.ts.map +1 -1
  30. package/dist/src/core/derived-row-validator.js +7 -1
  31. package/dist/src/core/derived-row-validator.js.map +1 -1
  32. package/dist/src/core/statement.d.ts.map +1 -1
  33. package/dist/src/core/statement.js +15 -5
  34. package/dist/src/core/statement.js.map +1 -1
  35. package/dist/src/emit/ast-stringify.d.ts +18 -2
  36. package/dist/src/emit/ast-stringify.d.ts.map +1 -1
  37. package/dist/src/emit/ast-stringify.js +22 -4
  38. package/dist/src/emit/ast-stringify.js.map +1 -1
  39. package/dist/src/func/builtins/explain.d.ts.map +1 -1
  40. package/dist/src/func/builtins/explain.js +19 -5
  41. package/dist/src/func/builtins/explain.js.map +1 -1
  42. package/dist/src/func/builtins/scalar.d.ts.map +1 -1
  43. package/dist/src/func/builtins/scalar.js +24 -20
  44. package/dist/src/func/builtins/scalar.js.map +1 -1
  45. package/dist/src/func/builtins/schema.d.ts.map +1 -1
  46. package/dist/src/func/builtins/schema.js +32 -5
  47. package/dist/src/func/builtins/schema.js.map +1 -1
  48. package/dist/src/index.d.ts +4 -4
  49. package/dist/src/index.d.ts.map +1 -1
  50. package/dist/src/index.js +4 -4
  51. package/dist/src/index.js.map +1 -1
  52. package/dist/src/parser/ast.d.ts +58 -1
  53. package/dist/src/parser/ast.d.ts.map +1 -1
  54. package/dist/src/parser/parser.js +1 -1
  55. package/dist/src/parser/parser.js.map +1 -1
  56. package/dist/src/parser/utils.d.ts +13 -1
  57. package/dist/src/parser/utils.d.ts.map +1 -1
  58. package/dist/src/parser/utils.js +14 -0
  59. package/dist/src/parser/utils.js.map +1 -1
  60. package/dist/src/planner/analysis/assertion-classifier.d.ts.map +1 -1
  61. package/dist/src/planner/analysis/assertion-classifier.js +7 -2
  62. package/dist/src/planner/analysis/assertion-classifier.js.map +1 -1
  63. package/dist/src/planner/analysis/assertion-hoist-cache.d.ts.map +1 -1
  64. package/dist/src/planner/analysis/assertion-hoist-cache.js +5 -0
  65. package/dist/src/planner/analysis/assertion-hoist-cache.js.map +1 -1
  66. package/dist/src/planner/analysis/change-scope.d.ts +11 -3
  67. package/dist/src/planner/analysis/change-scope.d.ts.map +1 -1
  68. package/dist/src/planner/analysis/change-scope.js +5 -4
  69. package/dist/src/planner/analysis/change-scope.js.map +1 -1
  70. package/dist/src/planner/analysis/coverage-prover.d.ts.map +1 -1
  71. package/dist/src/planner/analysis/coverage-prover.js +13 -5
  72. package/dist/src/planner/analysis/coverage-prover.js.map +1 -1
  73. package/dist/src/planner/analysis/equi-correlation.d.ts +6 -1
  74. package/dist/src/planner/analysis/equi-correlation.d.ts.map +1 -1
  75. package/dist/src/planner/analysis/equi-correlation.js +8 -1
  76. package/dist/src/planner/analysis/equi-correlation.js.map +1 -1
  77. package/dist/src/planner/analysis/predicate-dependencies.d.ts +34 -0
  78. package/dist/src/planner/analysis/predicate-dependencies.d.ts.map +1 -0
  79. package/dist/src/planner/analysis/predicate-dependencies.js +60 -0
  80. package/dist/src/planner/analysis/predicate-dependencies.js.map +1 -0
  81. package/dist/src/planner/analysis/query-rewrite-matcher.d.ts +1 -1
  82. package/dist/src/planner/analysis/query-rewrite-matcher.d.ts.map +1 -1
  83. package/dist/src/planner/analysis/query-rewrite-matcher.js +0 -26
  84. package/dist/src/planner/analysis/query-rewrite-matcher.js.map +1 -1
  85. package/dist/src/planner/building/alter-table.d.ts.map +1 -1
  86. package/dist/src/planner/building/alter-table.js +53 -17
  87. package/dist/src/planner/building/alter-table.js.map +1 -1
  88. package/dist/src/planner/building/constraint-builder.d.ts.map +1 -1
  89. package/dist/src/planner/building/constraint-builder.js +9 -5
  90. package/dist/src/planner/building/constraint-builder.js.map +1 -1
  91. package/dist/src/planner/building/create-assertion.d.ts.map +1 -1
  92. package/dist/src/planner/building/create-assertion.js +72 -1
  93. package/dist/src/planner/building/create-assertion.js.map +1 -1
  94. package/dist/src/planner/building/create-view.d.ts +6 -1
  95. package/dist/src/planner/building/create-view.d.ts.map +1 -1
  96. package/dist/src/planner/building/create-view.js +12 -3
  97. package/dist/src/planner/building/create-view.js.map +1 -1
  98. package/dist/src/planner/building/ddl.d.ts.map +1 -1
  99. package/dist/src/planner/building/ddl.js +6 -2
  100. package/dist/src/planner/building/ddl.js.map +1 -1
  101. package/dist/src/planner/building/delete.d.ts.map +1 -1
  102. package/dist/src/planner/building/delete.js +23 -10
  103. package/dist/src/planner/building/delete.js.map +1 -1
  104. package/dist/src/planner/building/dml-target.d.ts.map +1 -1
  105. package/dist/src/planner/building/dml-target.js +4 -0
  106. package/dist/src/planner/building/dml-target.js.map +1 -1
  107. package/dist/src/planner/building/drop-assertion.d.ts.map +1 -1
  108. package/dist/src/planner/building/drop-assertion.js +3 -1
  109. package/dist/src/planner/building/drop-assertion.js.map +1 -1
  110. package/dist/src/planner/building/foreign-key-builder.d.ts.map +1 -1
  111. package/dist/src/planner/building/foreign-key-builder.js +7 -5
  112. package/dist/src/planner/building/foreign-key-builder.js.map +1 -1
  113. package/dist/src/planner/building/function-call.d.ts +35 -0
  114. package/dist/src/planner/building/function-call.d.ts.map +1 -1
  115. package/dist/src/planner/building/function-call.js +53 -40
  116. package/dist/src/planner/building/function-call.js.map +1 -1
  117. package/dist/src/planner/building/insert.d.ts.map +1 -1
  118. package/dist/src/planner/building/insert.js +185 -48
  119. package/dist/src/planner/building/insert.js.map +1 -1
  120. package/dist/src/planner/building/materialized-view.d.ts.map +1 -1
  121. package/dist/src/planner/building/materialized-view.js +3 -1
  122. package/dist/src/planner/building/materialized-view.js.map +1 -1
  123. package/dist/src/planner/building/schema-authored-context.d.ts +69 -0
  124. package/dist/src/planner/building/schema-authored-context.d.ts.map +1 -0
  125. package/dist/src/planner/building/schema-authored-context.js +75 -0
  126. package/dist/src/planner/building/schema-authored-context.js.map +1 -0
  127. package/dist/src/planner/building/schema-resolution.d.ts.map +1 -1
  128. package/dist/src/planner/building/schema-resolution.js +7 -4
  129. package/dist/src/planner/building/schema-resolution.js.map +1 -1
  130. package/dist/src/planner/building/select-aggregates.d.ts +187 -2
  131. package/dist/src/planner/building/select-aggregates.d.ts.map +1 -1
  132. package/dist/src/planner/building/select-aggregates.js +414 -81
  133. package/dist/src/planner/building/select-aggregates.js.map +1 -1
  134. package/dist/src/planner/building/select-compound.js +2 -2
  135. package/dist/src/planner/building/select-compound.js.map +1 -1
  136. package/dist/src/planner/building/select-context.d.ts +91 -0
  137. package/dist/src/planner/building/select-context.d.ts.map +1 -1
  138. package/dist/src/planner/building/select-context.js +113 -0
  139. package/dist/src/planner/building/select-context.js.map +1 -1
  140. package/dist/src/planner/building/select-modifiers.d.ts +10 -3
  141. package/dist/src/planner/building/select-modifiers.d.ts.map +1 -1
  142. package/dist/src/planner/building/select-modifiers.js +29 -8
  143. package/dist/src/planner/building/select-modifiers.js.map +1 -1
  144. package/dist/src/planner/building/select-ordinal.d.ts +64 -19
  145. package/dist/src/planner/building/select-ordinal.d.ts.map +1 -1
  146. package/dist/src/planner/building/select-ordinal.js +79 -30
  147. package/dist/src/planner/building/select-ordinal.js.map +1 -1
  148. package/dist/src/planner/building/select-projections.d.ts +1 -1
  149. package/dist/src/planner/building/select-projections.d.ts.map +1 -1
  150. package/dist/src/planner/building/select-projections.js +18 -11
  151. package/dist/src/planner/building/select-projections.js.map +1 -1
  152. package/dist/src/planner/building/select-window.d.ts +13 -3
  153. package/dist/src/planner/building/select-window.d.ts.map +1 -1
  154. package/dist/src/planner/building/select-window.js +142 -37
  155. package/dist/src/planner/building/select-window.js.map +1 -1
  156. package/dist/src/planner/building/select.d.ts.map +1 -1
  157. package/dist/src/planner/building/select.js +129 -59
  158. package/dist/src/planner/building/select.js.map +1 -1
  159. package/dist/src/planner/building/update.d.ts.map +1 -1
  160. package/dist/src/planner/building/update.js +27 -13
  161. package/dist/src/planner/building/update.js.map +1 -1
  162. package/dist/src/planner/building/view-mutation-builder.d.ts +1 -1
  163. package/dist/src/planner/building/view-mutation-builder.d.ts.map +1 -1
  164. package/dist/src/planner/building/view-mutation-builder.js +102 -8
  165. package/dist/src/planner/building/view-mutation-builder.js.map +1 -1
  166. package/dist/src/planner/building/with.d.ts.map +1 -1
  167. package/dist/src/planner/building/with.js +34 -4
  168. package/dist/src/planner/building/with.js.map +1 -1
  169. package/dist/src/planner/cache/correlation-detector.d.ts +15 -0
  170. package/dist/src/planner/cache/correlation-detector.d.ts.map +1 -1
  171. package/dist/src/planner/cache/correlation-detector.js +26 -0
  172. package/dist/src/planner/cache/correlation-detector.js.map +1 -1
  173. package/dist/src/planner/cache/materialization-advisory.d.ts +7 -0
  174. package/dist/src/planner/cache/materialization-advisory.d.ts.map +1 -1
  175. package/dist/src/planner/cache/materialization-advisory.js +12 -3
  176. package/dist/src/planner/cache/materialization-advisory.js.map +1 -1
  177. package/dist/src/planner/framework/characteristics.d.ts +9 -0
  178. package/dist/src/planner/framework/characteristics.d.ts.map +1 -1
  179. package/dist/src/planner/framework/characteristics.js.map +1 -1
  180. package/dist/src/planner/framework/pass.d.ts +2 -0
  181. package/dist/src/planner/framework/pass.d.ts.map +1 -1
  182. package/dist/src/planner/framework/pass.js +15 -0
  183. package/dist/src/planner/framework/pass.js.map +1 -1
  184. package/dist/src/planner/mutation/backward-body.d.ts.map +1 -1
  185. package/dist/src/planner/mutation/backward-body.js +4 -1
  186. package/dist/src/planner/mutation/backward-body.js.map +1 -1
  187. package/dist/src/planner/mutation/body-context.d.ts +23 -0
  188. package/dist/src/planner/mutation/body-context.d.ts.map +1 -0
  189. package/dist/src/planner/mutation/body-context.js +26 -0
  190. package/dist/src/planner/mutation/body-context.js.map +1 -0
  191. package/dist/src/planner/mutation/cte-flatten.js +15 -2
  192. package/dist/src/planner/mutation/cte-flatten.js.map +1 -1
  193. package/dist/src/planner/mutation/decomposition.d.ts +8 -0
  194. package/dist/src/planner/mutation/decomposition.d.ts.map +1 -1
  195. package/dist/src/planner/mutation/decomposition.js +8 -3
  196. package/dist/src/planner/mutation/decomposition.js.map +1 -1
  197. package/dist/src/planner/mutation/lens-enforcement.js +1 -1
  198. package/dist/src/planner/mutation/multi-source.d.ts +7 -0
  199. package/dist/src/planner/mutation/multi-source.d.ts.map +1 -1
  200. package/dist/src/planner/mutation/multi-source.js +5 -0
  201. package/dist/src/planner/mutation/multi-source.js.map +1 -1
  202. package/dist/src/planner/mutation/mutation-diagnostic.d.ts +1 -1
  203. package/dist/src/planner/mutation/mutation-diagnostic.d.ts.map +1 -1
  204. package/dist/src/planner/mutation/mutation-diagnostic.js.map +1 -1
  205. package/dist/src/planner/mutation/scope-transform.d.ts +32 -0
  206. package/dist/src/planner/mutation/scope-transform.d.ts.map +1 -1
  207. package/dist/src/planner/mutation/scope-transform.js +181 -33
  208. package/dist/src/planner/mutation/scope-transform.js.map +1 -1
  209. package/dist/src/planner/mutation/set-op.d.ts.map +1 -1
  210. package/dist/src/planner/mutation/set-op.js +66 -16
  211. package/dist/src/planner/mutation/set-op.js.map +1 -1
  212. package/dist/src/planner/mutation/single-source.d.ts.map +1 -1
  213. package/dist/src/planner/mutation/single-source.js +100 -24
  214. package/dist/src/planner/mutation/single-source.js.map +1 -1
  215. package/dist/src/planner/nodes/add-constraint-node.d.ts +15 -1
  216. package/dist/src/planner/nodes/add-constraint-node.d.ts.map +1 -1
  217. package/dist/src/planner/nodes/add-constraint-node.js +10 -1
  218. package/dist/src/planner/nodes/add-constraint-node.js.map +1 -1
  219. package/dist/src/planner/nodes/aggregate-node.d.ts +14 -5
  220. package/dist/src/planner/nodes/aggregate-node.d.ts.map +1 -1
  221. package/dist/src/planner/nodes/aggregate-node.js +28 -21
  222. package/dist/src/planner/nodes/aggregate-node.js.map +1 -1
  223. package/dist/src/planner/nodes/alter-table-node.d.ts +27 -2
  224. package/dist/src/planner/nodes/alter-table-node.d.ts.map +1 -1
  225. package/dist/src/planner/nodes/alter-table-node.js +49 -2
  226. package/dist/src/planner/nodes/alter-table-node.js.map +1 -1
  227. package/dist/src/planner/nodes/constraint-check-node.d.ts.map +1 -1
  228. package/dist/src/planner/nodes/constraint-check-node.js +17 -19
  229. package/dist/src/planner/nodes/constraint-check-node.js.map +1 -1
  230. package/dist/src/planner/nodes/create-assertion-node.d.ts +2 -1
  231. package/dist/src/planner/nodes/create-assertion-node.d.ts.map +1 -1
  232. package/dist/src/planner/nodes/create-assertion-node.js +5 -2
  233. package/dist/src/planner/nodes/create-assertion-node.js.map +1 -1
  234. package/dist/src/planner/nodes/cte-node.d.ts +19 -9
  235. package/dist/src/planner/nodes/cte-node.d.ts.map +1 -1
  236. package/dist/src/planner/nodes/cte-node.js +25 -9
  237. package/dist/src/planner/nodes/cte-node.js.map +1 -1
  238. package/dist/src/planner/nodes/delete-node.d.ts +20 -2
  239. package/dist/src/planner/nodes/delete-node.d.ts.map +1 -1
  240. package/dist/src/planner/nodes/delete-node.js +11 -2
  241. package/dist/src/planner/nodes/delete-node.js.map +1 -1
  242. package/dist/src/planner/nodes/dml-executor-node.d.ts +58 -1
  243. package/dist/src/planner/nodes/dml-executor-node.d.ts.map +1 -1
  244. package/dist/src/planner/nodes/dml-executor-node.js +107 -15
  245. package/dist/src/planner/nodes/dml-executor-node.js.map +1 -1
  246. package/dist/src/planner/nodes/drop-assertion-node.d.ts +2 -1
  247. package/dist/src/planner/nodes/drop-assertion-node.d.ts.map +1 -1
  248. package/dist/src/planner/nodes/drop-assertion-node.js +5 -2
  249. package/dist/src/planner/nodes/drop-assertion-node.js.map +1 -1
  250. package/dist/src/planner/nodes/filter.d.ts.map +1 -1
  251. package/dist/src/planner/nodes/filter.js +12 -6
  252. package/dist/src/planner/nodes/filter.js.map +1 -1
  253. package/dist/src/planner/nodes/hash-aggregate.d.ts +0 -5
  254. package/dist/src/planner/nodes/hash-aggregate.d.ts.map +1 -1
  255. package/dist/src/planner/nodes/hash-aggregate.js +17 -20
  256. package/dist/src/planner/nodes/hash-aggregate.js.map +1 -1
  257. package/dist/src/planner/nodes/insert-node.d.ts +19 -2
  258. package/dist/src/planner/nodes/insert-node.d.ts.map +1 -1
  259. package/dist/src/planner/nodes/insert-node.js +10 -2
  260. package/dist/src/planner/nodes/insert-node.js.map +1 -1
  261. package/dist/src/planner/nodes/key-set-semi-join-node.d.ts +42 -10
  262. package/dist/src/planner/nodes/key-set-semi-join-node.d.ts.map +1 -1
  263. package/dist/src/planner/nodes/key-set-semi-join-node.js +85 -16
  264. package/dist/src/planner/nodes/key-set-semi-join-node.js.map +1 -1
  265. package/dist/src/planner/nodes/lens-auxiliary-access-node.d.ts +5 -0
  266. package/dist/src/planner/nodes/lens-auxiliary-access-node.d.ts.map +1 -1
  267. package/dist/src/planner/nodes/lens-auxiliary-access-node.js.map +1 -1
  268. package/dist/src/planner/nodes/plan-node.d.ts +5 -0
  269. package/dist/src/planner/nodes/plan-node.d.ts.map +1 -1
  270. package/dist/src/planner/nodes/plan-node.js +12 -0
  271. package/dist/src/planner/nodes/plan-node.js.map +1 -1
  272. package/dist/src/planner/nodes/project-node.d.ts.map +1 -1
  273. package/dist/src/planner/nodes/project-node.js +16 -35
  274. package/dist/src/planner/nodes/project-node.js.map +1 -1
  275. package/dist/src/planner/nodes/retrieve-node.d.ts +16 -2
  276. package/dist/src/planner/nodes/retrieve-node.d.ts.map +1 -1
  277. package/dist/src/planner/nodes/retrieve-node.js +8 -1
  278. package/dist/src/planner/nodes/retrieve-node.js.map +1 -1
  279. package/dist/src/planner/nodes/returning-node.d.ts.map +1 -1
  280. package/dist/src/planner/nodes/returning-node.js +13 -32
  281. package/dist/src/planner/nodes/returning-node.js.map +1 -1
  282. package/dist/src/planner/nodes/stream-aggregate.d.ts +0 -5
  283. package/dist/src/planner/nodes/stream-aggregate.d.ts.map +1 -1
  284. package/dist/src/planner/nodes/stream-aggregate.js +18 -24
  285. package/dist/src/planner/nodes/stream-aggregate.js.map +1 -1
  286. package/dist/src/planner/nodes/table-access-nodes.d.ts +89 -1
  287. package/dist/src/planner/nodes/table-access-nodes.d.ts.map +1 -1
  288. package/dist/src/planner/nodes/table-access-nodes.js +64 -2
  289. package/dist/src/planner/nodes/table-access-nodes.js.map +1 -1
  290. package/dist/src/planner/nodes/update-node.d.ts +19 -2
  291. package/dist/src/planner/nodes/update-node.d.ts.map +1 -1
  292. package/dist/src/planner/nodes/update-node.js +10 -2
  293. package/dist/src/planner/nodes/update-node.js.map +1 -1
  294. package/dist/src/planner/optimizer.d.ts +18 -0
  295. package/dist/src/planner/optimizer.d.ts.map +1 -1
  296. package/dist/src/planner/optimizer.js +59 -10
  297. package/dist/src/planner/optimizer.js.map +1 -1
  298. package/dist/src/planner/planning-context.d.ts +24 -0
  299. package/dist/src/planner/planning-context.d.ts.map +1 -1
  300. package/dist/src/planner/planning-context.js.map +1 -1
  301. package/dist/src/planner/rules/access/rule-key-set-seek.d.ts +78 -20
  302. package/dist/src/planner/rules/access/rule-key-set-seek.d.ts.map +1 -1
  303. package/dist/src/planner/rules/access/rule-key-set-seek.js +297 -71
  304. package/dist/src/planner/rules/access/rule-key-set-seek.js.map +1 -1
  305. package/dist/src/planner/rules/access/rule-monotonic-range-access.d.ts.map +1 -1
  306. package/dist/src/planner/rules/access/rule-monotonic-range-access.js +2 -2
  307. package/dist/src/planner/rules/access/rule-monotonic-range-access.js.map +1 -1
  308. package/dist/src/planner/rules/access/rule-select-access-path.d.ts +11 -1
  309. package/dist/src/planner/rules/access/rule-select-access-path.d.ts.map +1 -1
  310. package/dist/src/planner/rules/access/rule-select-access-path.js +46 -2
  311. package/dist/src/planner/rules/access/rule-select-access-path.js.map +1 -1
  312. package/dist/src/planner/rules/aggregate/rule-groupby-fd-simplification.d.ts +7 -4
  313. package/dist/src/planner/rules/aggregate/rule-groupby-fd-simplification.d.ts.map +1 -1
  314. package/dist/src/planner/rules/aggregate/rule-groupby-fd-simplification.js +39 -5
  315. package/dist/src/planner/rules/aggregate/rule-groupby-fd-simplification.js.map +1 -1
  316. package/dist/src/planner/rules/cache/rule-cte-optimization.d.ts.map +1 -1
  317. package/dist/src/planner/rules/cache/rule-cte-optimization.js +6 -2
  318. package/dist/src/planner/rules/cache/rule-cte-optimization.js.map +1 -1
  319. package/dist/src/planner/rules/cache/rule-materialized-view-rewrite.js +10 -3
  320. package/dist/src/planner/rules/cache/rule-materialized-view-rewrite.js.map +1 -1
  321. package/dist/src/planner/rules/join/index-nested-loop.d.ts.map +1 -1
  322. package/dist/src/planner/rules/join/index-nested-loop.js +13 -7
  323. package/dist/src/planner/rules/join/index-nested-loop.js.map +1 -1
  324. package/dist/src/planner/rules/join/rule-fanout-lookup-join.d.ts +4 -0
  325. package/dist/src/planner/rules/join/rule-fanout-lookup-join.d.ts.map +1 -1
  326. package/dist/src/planner/rules/join/rule-fanout-lookup-join.js +50 -15
  327. package/dist/src/planner/rules/join/rule-fanout-lookup-join.js.map +1 -1
  328. package/dist/src/planner/rules/join/rule-join-elimination.d.ts +6 -0
  329. package/dist/src/planner/rules/join/rule-join-elimination.d.ts.map +1 -1
  330. package/dist/src/planner/rules/join/rule-join-elimination.js +25 -9
  331. package/dist/src/planner/rules/join/rule-join-elimination.js.map +1 -1
  332. package/dist/src/planner/rules/join/rule-join-key-inference.d.ts +7 -0
  333. package/dist/src/planner/rules/join/rule-join-key-inference.d.ts.map +1 -1
  334. package/dist/src/planner/rules/join/rule-join-key-inference.js +23 -11
  335. package/dist/src/planner/rules/join/rule-join-key-inference.js.map +1 -1
  336. package/dist/src/planner/rules/join/rule-join-physical-selection.d.ts +2 -2
  337. package/dist/src/planner/rules/join/rule-join-physical-selection.d.ts.map +1 -1
  338. package/dist/src/planner/rules/join/rule-join-physical-selection.js +46 -16
  339. package/dist/src/planner/rules/join/rule-join-physical-selection.js.map +1 -1
  340. package/dist/src/planner/rules/join/rule-monotonic-merge-join.d.ts +2 -0
  341. package/dist/src/planner/rules/join/rule-monotonic-merge-join.d.ts.map +1 -1
  342. package/dist/src/planner/rules/join/rule-monotonic-merge-join.js +14 -0
  343. package/dist/src/planner/rules/join/rule-monotonic-merge-join.js.map +1 -1
  344. package/dist/src/planner/rules/predicate/rule-aggregate-predicate-pushdown.d.ts +2 -2
  345. package/dist/src/planner/rules/predicate/rule-aggregate-predicate-pushdown.d.ts.map +1 -1
  346. package/dist/src/planner/rules/predicate/rule-aggregate-predicate-pushdown.js +19 -22
  347. package/dist/src/planner/rules/predicate/rule-aggregate-predicate-pushdown.js.map +1 -1
  348. package/dist/src/planner/rules/predicate/rule-filter-selectivity.d.ts +33 -19
  349. package/dist/src/planner/rules/predicate/rule-filter-selectivity.d.ts.map +1 -1
  350. package/dist/src/planner/rules/predicate/rule-filter-selectivity.js +37 -18
  351. package/dist/src/planner/rules/predicate/rule-filter-selectivity.js.map +1 -1
  352. package/dist/src/planner/rules/predicate/rule-join-predicate-pushdown.d.ts +6 -3
  353. package/dist/src/planner/rules/predicate/rule-join-predicate-pushdown.d.ts.map +1 -1
  354. package/dist/src/planner/rules/predicate/rule-join-predicate-pushdown.js +6 -3
  355. package/dist/src/planner/rules/predicate/rule-join-predicate-pushdown.js.map +1 -1
  356. package/dist/src/planner/rules/predicate/rule-predicate-pushdown.d.ts.map +1 -1
  357. package/dist/src/planner/rules/predicate/rule-predicate-pushdown.js +11 -21
  358. package/dist/src/planner/rules/predicate/rule-predicate-pushdown.js.map +1 -1
  359. package/dist/src/planner/rules/shared/access-leaf.d.ts +16 -3
  360. package/dist/src/planner/rules/shared/access-leaf.d.ts.map +1 -1
  361. package/dist/src/planner/rules/shared/access-leaf.js +21 -5
  362. package/dist/src/planner/rules/shared/access-leaf.js.map +1 -1
  363. package/dist/src/planner/rules/subquery/rule-anti-join-fk-empty.js +1 -1
  364. package/dist/src/planner/rules/subquery/rule-anti-join-fk-empty.js.map +1 -1
  365. package/dist/src/planner/rules/subquery/rule-semi-join-fk-trivial.js +1 -1
  366. package/dist/src/planner/rules/subquery/rule-semi-join-fk-trivial.js.map +1 -1
  367. package/dist/src/planner/stored-body-context.d.ts +57 -0
  368. package/dist/src/planner/stored-body-context.d.ts.map +1 -0
  369. package/dist/src/planner/stored-body-context.js +64 -0
  370. package/dist/src/planner/stored-body-context.js.map +1 -0
  371. package/dist/src/planner/util/ind-utils.d.ts +28 -20
  372. package/dist/src/planner/util/ind-utils.d.ts.map +1 -1
  373. package/dist/src/planner/util/ind-utils.js +36 -27
  374. package/dist/src/planner/util/ind-utils.js.map +1 -1
  375. package/dist/src/planner/util/key-utils.d.ts +19 -4
  376. package/dist/src/planner/util/key-utils.d.ts.map +1 -1
  377. package/dist/src/planner/util/key-utils.js +23 -7
  378. package/dist/src/planner/util/key-utils.js.map +1 -1
  379. package/dist/src/planner/util/output-names.d.ts +12 -0
  380. package/dist/src/planner/util/output-names.d.ts.map +1 -0
  381. package/dist/src/planner/util/output-names.js +20 -0
  382. package/dist/src/planner/util/output-names.js.map +1 -0
  383. package/dist/src/planner/util/row-estimates.d.ts +11 -0
  384. package/dist/src/planner/util/row-estimates.d.ts.map +1 -1
  385. package/dist/src/planner/util/row-estimates.js +28 -0
  386. package/dist/src/planner/util/row-estimates.js.map +1 -1
  387. package/dist/src/runtime/deferred-constraint-queue.d.ts.map +1 -1
  388. package/dist/src/runtime/deferred-constraint-queue.js +3 -2
  389. package/dist/src/runtime/deferred-constraint-queue.js.map +1 -1
  390. package/dist/src/runtime/emit/add-constraint.d.ts.map +1 -1
  391. package/dist/src/runtime/emit/add-constraint.js +22 -6
  392. package/dist/src/runtime/emit/add-constraint.js.map +1 -1
  393. package/dist/src/runtime/emit/alter-schema-event.d.ts +59 -4
  394. package/dist/src/runtime/emit/alter-schema-event.d.ts.map +1 -1
  395. package/dist/src/runtime/emit/alter-schema-event.js +63 -4
  396. package/dist/src/runtime/emit/alter-schema-event.js.map +1 -1
  397. package/dist/src/runtime/emit/alter-table.d.ts.map +1 -1
  398. package/dist/src/runtime/emit/alter-table.js +212 -92
  399. package/dist/src/runtime/emit/alter-table.js.map +1 -1
  400. package/dist/src/runtime/emit/analyze.d.ts.map +1 -1
  401. package/dist/src/runtime/emit/analyze.js +15 -4
  402. package/dist/src/runtime/emit/analyze.js.map +1 -1
  403. package/dist/src/runtime/emit/assertion-drop-guard.d.ts +37 -0
  404. package/dist/src/runtime/emit/assertion-drop-guard.d.ts.map +1 -0
  405. package/dist/src/runtime/emit/assertion-drop-guard.js +59 -0
  406. package/dist/src/runtime/emit/assertion-drop-guard.js.map +1 -0
  407. package/dist/src/runtime/emit/assertion-rename-helpers.d.ts +33 -0
  408. package/dist/src/runtime/emit/assertion-rename-helpers.d.ts.map +1 -0
  409. package/dist/src/runtime/emit/assertion-rename-helpers.js +124 -0
  410. package/dist/src/runtime/emit/assertion-rename-helpers.js.map +1 -0
  411. package/dist/src/runtime/emit/constraint-check.d.ts.map +1 -1
  412. package/dist/src/runtime/emit/constraint-check.js +10 -227
  413. package/dist/src/runtime/emit/constraint-check.js.map +1 -1
  414. package/dist/src/runtime/emit/create-assertion.d.ts.map +1 -1
  415. package/dist/src/runtime/emit/create-assertion.js +31 -14
  416. package/dist/src/runtime/emit/create-assertion.js.map +1 -1
  417. package/dist/src/runtime/emit/cte.d.ts.map +1 -1
  418. package/dist/src/runtime/emit/cte.js +11 -6
  419. package/dist/src/runtime/emit/cte.js.map +1 -1
  420. package/dist/src/runtime/emit/dml-executor.d.ts.map +1 -1
  421. package/dist/src/runtime/emit/dml-executor.js +169 -1
  422. package/dist/src/runtime/emit/dml-executor.js.map +1 -1
  423. package/dist/src/runtime/emit/drop-assertion.js +8 -8
  424. package/dist/src/runtime/emit/drop-assertion.js.map +1 -1
  425. package/dist/src/runtime/emit/drop-column-guards.d.ts +151 -0
  426. package/dist/src/runtime/emit/drop-column-guards.d.ts.map +1 -0
  427. package/dist/src/runtime/emit/drop-column-guards.js +230 -0
  428. package/dist/src/runtime/emit/drop-column-guards.js.map +1 -0
  429. package/dist/src/runtime/emit/drop-table.d.ts.map +1 -1
  430. package/dist/src/runtime/emit/drop-table.js +6 -0
  431. package/dist/src/runtime/emit/drop-table.js.map +1 -1
  432. package/dist/src/runtime/emit/drop-view.d.ts.map +1 -1
  433. package/dist/src/runtime/emit/drop-view.js +4 -0
  434. package/dist/src/runtime/emit/drop-view.js.map +1 -1
  435. package/dist/src/runtime/emit/key-set-semi-join.d.ts.map +1 -1
  436. package/dist/src/runtime/emit/key-set-semi-join.js +5 -3
  437. package/dist/src/runtime/emit/key-set-semi-join.js.map +1 -1
  438. package/dist/src/runtime/emit/materialized-view-helpers.d.ts +4 -4
  439. package/dist/src/runtime/emit/materialized-view-helpers.d.ts.map +1 -1
  440. package/dist/src/runtime/emit/materialized-view-helpers.js +34 -22
  441. package/dist/src/runtime/emit/materialized-view-helpers.js.map +1 -1
  442. package/dist/src/runtime/emit/materialized-view.d.ts.map +1 -1
  443. package/dist/src/runtime/emit/materialized-view.js +7 -2
  444. package/dist/src/runtime/emit/materialized-view.js.map +1 -1
  445. package/dist/src/runtime/emit/recursive-cte.d.ts.map +1 -1
  446. package/dist/src/runtime/emit/recursive-cte.js +4 -5
  447. package/dist/src/runtime/emit/recursive-cte.js.map +1 -1
  448. package/dist/src/runtime/emit/update.d.ts.map +1 -1
  449. package/dist/src/runtime/emit/update.js +18 -6
  450. package/dist/src/runtime/emit/update.js.map +1 -1
  451. package/dist/src/runtime/row-constraints.d.ts +85 -0
  452. package/dist/src/runtime/row-constraints.d.ts.map +1 -0
  453. package/dist/src/runtime/row-constraints.js +239 -0
  454. package/dist/src/runtime/row-constraints.js.map +1 -0
  455. package/dist/src/runtime/types.d.ts +8 -11
  456. package/dist/src/runtime/types.d.ts.map +1 -1
  457. package/dist/src/runtime/types.js.map +1 -1
  458. package/dist/src/schema/assertion.d.ts +14 -1
  459. package/dist/src/schema/assertion.d.ts.map +1 -1
  460. package/dist/src/schema/assertion.js +14 -1
  461. package/dist/src/schema/assertion.js.map +1 -1
  462. package/dist/src/schema/catalog-persistability.js +19 -3
  463. package/dist/src/schema/catalog-persistability.js.map +1 -1
  464. package/dist/src/schema/catalog.d.ts +6 -0
  465. package/dist/src/schema/catalog.d.ts.map +1 -1
  466. package/dist/src/schema/catalog.js +26 -36
  467. package/dist/src/schema/catalog.js.map +1 -1
  468. package/dist/src/schema/column-source-resolver.d.ts +18 -0
  469. package/dist/src/schema/column-source-resolver.d.ts.map +1 -0
  470. package/dist/src/schema/column-source-resolver.js +22 -0
  471. package/dist/src/schema/column-source-resolver.js.map +1 -0
  472. package/dist/src/schema/constraint-builder.d.ts.map +1 -1
  473. package/dist/src/schema/constraint-builder.js +7 -1
  474. package/dist/src/schema/constraint-builder.js.map +1 -1
  475. package/dist/src/schema/ddl-generator.js +8 -0
  476. package/dist/src/schema/ddl-generator.js.map +1 -1
  477. package/dist/src/schema/lens-compiler.d.ts.map +1 -1
  478. package/dist/src/schema/lens-compiler.js +203 -51
  479. package/dist/src/schema/lens-compiler.js.map +1 -1
  480. package/dist/src/schema/lens-prover.d.ts +2 -3
  481. package/dist/src/schema/lens-prover.d.ts.map +1 -1
  482. package/dist/src/schema/lens-prover.js +19 -11
  483. package/dist/src/schema/lens-prover.js.map +1 -1
  484. package/dist/src/schema/manager.d.ts +30 -12
  485. package/dist/src/schema/manager.d.ts.map +1 -1
  486. package/dist/src/schema/manager.js +49 -17
  487. package/dist/src/schema/manager.js.map +1 -1
  488. package/dist/src/schema/rename-rewriter.d.ts +142 -0
  489. package/dist/src/schema/rename-rewriter.d.ts.map +1 -1
  490. package/dist/src/schema/rename-rewriter.js +254 -9
  491. package/dist/src/schema/rename-rewriter.js.map +1 -1
  492. package/dist/src/schema/reserved-tags.d.ts +1 -1
  493. package/dist/src/schema/reserved-tags.d.ts.map +1 -1
  494. package/dist/src/schema/reserved-tags.js +4 -3
  495. package/dist/src/schema/reserved-tags.js.map +1 -1
  496. package/dist/src/schema/schema-differ.d.ts +16 -0
  497. package/dist/src/schema/schema-differ.d.ts.map +1 -1
  498. package/dist/src/schema/schema-differ.js +122 -11
  499. package/dist/src/schema/schema-differ.js.map +1 -1
  500. package/dist/src/schema/view.d.ts +11 -0
  501. package/dist/src/schema/view.d.ts.map +1 -1
  502. package/dist/src/schema/view.js +12 -0
  503. package/dist/src/schema/view.js.map +1 -1
  504. package/dist/src/types/builtin-types.d.ts +38 -0
  505. package/dist/src/types/builtin-types.d.ts.map +1 -1
  506. package/dist/src/types/builtin-types.js +77 -26
  507. package/dist/src/types/builtin-types.js.map +1 -1
  508. package/dist/src/types/index.d.ts +1 -1
  509. package/dist/src/types/index.d.ts.map +1 -1
  510. package/dist/src/types/index.js +1 -1
  511. package/dist/src/types/index.js.map +1 -1
  512. package/dist/src/util/{working-table-iterable.d.ts → array-row-iterable.d.ts} +6 -6
  513. package/dist/src/util/array-row-iterable.d.ts.map +1 -0
  514. package/dist/src/util/{working-table-iterable.js → array-row-iterable.js} +6 -6
  515. package/dist/src/util/array-row-iterable.js.map +1 -0
  516. package/dist/src/vtab/events.d.ts +3 -0
  517. package/dist/src/vtab/events.d.ts.map +1 -1
  518. package/dist/src/vtab/events.js.map +1 -1
  519. package/dist/src/vtab/manifest.d.ts +15 -0
  520. package/dist/src/vtab/manifest.d.ts.map +1 -1
  521. package/dist/src/vtab/memory/layer/manager.d.ts.map +1 -1
  522. package/dist/src/vtab/memory/layer/manager.js +3 -69
  523. package/dist/src/vtab/memory/layer/manager.js.map +1 -1
  524. package/dist/src/vtab/memory/module.d.ts +16 -1
  525. package/dist/src/vtab/memory/module.d.ts.map +1 -1
  526. package/dist/src/vtab/memory/module.js +62 -2
  527. package/dist/src/vtab/memory/module.js.map +1 -1
  528. package/dist/src/vtab/module.d.ts +26 -2
  529. package/dist/src/vtab/module.d.ts.map +1 -1
  530. package/dist/src/vtab/table.d.ts +8 -1
  531. package/dist/src/vtab/table.d.ts.map +1 -1
  532. package/dist/src/vtab/table.js.map +1 -1
  533. package/package.json +3 -3
  534. package/dist/src/util/working-table-iterable.d.ts.map +0 -1
  535. package/dist/src/util/working-table-iterable.js.map +0 -1
@@ -4,12 +4,25 @@ import { ProjectNode } from '../nodes/project-node.js';
4
4
  import { ArrayIndexNode } from '../nodes/array-index-node.js';
5
5
  import { LiteralNode } from '../nodes/scalar.js';
6
6
  import { buildExpression } from './expression.js';
7
- import { isWindowExpression } from './select-projections.js';
7
+ import { assertGroupedWindowCoverage, collectAggregateFunctionExprs, redirectToGroupKeys } from './select-aggregates.js';
8
+ import { findMatchingAggregate } from './function-call.js';
9
+ import { QuereusError } from '../../common/errors.js';
10
+ import { StatusCode } from '../../common/types.js';
11
+ import { expressionToString } from '../../emit/ast-stringify.js';
8
12
  import { CapabilityDetectors } from '../framework/characteristics.js';
9
13
  /**
10
- * Processes window functions and creates WindowNode(s) with proper projections
14
+ * Processes window functions and creates WindowNode(s) with proper projections.
15
+ *
16
+ * `selectListProjections` is the query's ONE select-list projection list — stars
17
+ * already expanded, in written order, window functions still present as raw
18
+ * `WindowFunctionCallNode` subtrees. It is rewritten (not rebuilt) into the
19
+ * projection placed above the WindowNode(s); see {@link buildWindowProjections}.
20
+ *
21
+ * `groupedWindowContext` is supplied only for a GROUPED query. The WindowNode sits
22
+ * ABOVE the AggregateNode, so its window specifications and function arguments run
23
+ * over the grouped rows and may only read what those rows carry.
11
24
  */
12
- export function buildWindowPhase(input, windowFunctions, selectContext, stmt) {
25
+ export function buildWindowPhase(input, windowFunctions, selectContext, selectListProjections, groupedWindowContext) {
13
26
  if (windowFunctions.length === 0) {
14
27
  return input;
15
28
  }
@@ -29,29 +42,118 @@ export function buildWindowPhase(input, windowFunctions, selectContext, stmt) {
29
42
  // TODO: Replace with SequencingNode for optimal performance
30
43
  // For now, proceed with WindowNode
31
44
  }
45
+ // Reject an aggregate this query never computed before trying to build it, so
46
+ // the failure names the unsupported construct.
47
+ for (const { func } of functions) {
48
+ rejectUncollectedAggregates(func, selectContext);
49
+ }
50
+ // In a grouped query the window runs over the AGGREGATE's rows, which carry only
51
+ // grouping keys and aggregate results. Several legal spellings of a grouping key
52
+ // nonetheless bind to a base-table attribute, because the scope these are built
53
+ // against falls through to the pre-aggregate select scope: a qualified `wg.a`
54
+ // against `group by a`, or the whole expression against `group by a || '!'`.
55
+ // Redirect every such subtree onto the aggregate's own output column.
56
+ const redirect = (expr) => groupedWindowContext
57
+ ? redirectToGroupKeys(expr, groupedWindowContext, selectContext.scope)
58
+ : expr;
32
59
  // CRITICAL: Build window specification expressions using the INPUT scope
33
60
  // This ensures expressions reference the correct input attribute IDs,
34
61
  // not premature output attribute IDs that don't exist in the runtime context
35
- const partitionExpressions = windowSpec.partitionBy.map(expr => buildExpression(selectContext, expr, false));
36
- const orderByExpressions = windowSpec.orderBy.map(orderClause => buildExpression(selectContext, orderClause.expr, false));
62
+ const partitionExpressions = windowSpec.partitionBy.map(expr => redirect(buildExpression(selectContext, expr, false)));
63
+ const orderByExpressions = windowSpec.orderBy.map(orderClause => redirect(buildExpression(selectContext, orderClause.expr, false)));
37
64
  // Build the function argument expressions FIRST (off the original nodes)
38
65
  // so each re-created WindowFunctionCallNode can be handed its argument
39
66
  // logical types for faithful return-type inference (window MIN/MAX).
40
- const functionArguments = buildWindowFunctionArguments(functions.map(({ func }) => func), selectContext);
67
+ const functionArguments = buildWindowFunctionArguments(functions.map(({ func }) => func), selectContext).map(args => args.map(redirect));
68
+ // After redirection, anything still naming one of this query's pre-grouping
69
+ // columns is a genuinely ungrouped reference, illegal for exactly the reason a
70
+ // bare column in the select list is, and must say so at plan time — otherwise it
71
+ // reaches the runtime as an attribute the aggregate row never had, and the query
72
+ // dies with an internal "no row context" error instead. A reference belonging to
73
+ // a subquery inside the specification, or correlated out to an ENCLOSING query,
74
+ // is not this query's business and passes through.
75
+ if (groupedWindowContext) {
76
+ for (const expr of partitionExpressions)
77
+ assertGroupedWindowCoverage(expr, groupedWindowContext);
78
+ for (const expr of orderByExpressions)
79
+ assertGroupedWindowCoverage(expr, groupedWindowContext);
80
+ for (const args of functionArguments) {
81
+ for (const arg of args)
82
+ assertGroupedWindowCoverage(arg, groupedWindowContext);
83
+ }
84
+ }
41
85
  // Create new WindowFunctionCallNode instances with alias + argument-type info
42
86
  const windowFuncsWithAlias = functions.map(({ func, alias }, i) => new WindowFunctionCallNode(func.scope, func.expression, func.functionName, func.isDistinct, alias, functionArguments[i].map(a => a.getType().logicalType)));
43
87
  // Now create the WindowNode with pre-compiled expressions
44
88
  currentInput = new WindowNode(selectContext.scope, currentInput, windowSpec, windowFuncsWithAlias, partitionExpressions, orderByExpressions, functionArguments);
45
89
  }
46
90
  // Create projections that select only the requested columns using direct array indexing
47
- const windowProjections = buildWindowProjections(stmt, currentInput, selectContext, windowFunctions);
91
+ const windowProjections = buildWindowProjections(selectListProjections, currentInput, selectContext, windowFunctions);
48
92
  if (windowProjections.length > 0) {
49
93
  currentInput = new ProjectNode(selectContext.scope, currentInput, windowProjections);
50
94
  }
51
95
  return currentInput;
52
96
  }
97
+ /**
98
+ * Rejects an aggregate inside a window function's OVER clause or arguments that the
99
+ * aggregate phase never collected.
100
+ *
101
+ * The supported spelling is one the SELECT list already computes — `select a,
102
+ * count(*) c, row_number() over (order by count(*) desc) … group by a` — because
103
+ * that `count(*)` resolves to the AggregateNode's own output column. An aggregate
104
+ * that appears ONLY here has nothing to resolve against; without this check it
105
+ * reaches `buildFunctionCall` with aggregates disallowed and fails with the generic
106
+ * "not allowed in this context", which reads like a bug rather than the limitation
107
+ * it is.
108
+ *
109
+ * NOTE: supporting the unsupported form means collecting these aggregates into the
110
+ * AggregateNode before the window phase runs, the way `collectOrderByAggregates`
111
+ * (select-aggregates.ts) already does for a top-level ORDER BY.
112
+ *
113
+ * NOTE: only the PARTITION BY / ORDER BY arms can fire today. A window function's
114
+ * ARGUMENTS are built once already, by `analyzeSelectColumns` → `buildExpression`'s
115
+ * `windowFunction` case, against the pre-aggregate context and with aggregates
116
+ * disallowed — so `sum(count(*)) over ()` dies there with the generic "not allowed
117
+ * in this context" long before this runs. The argument arm is kept because it
118
+ * becomes reachable the moment that early type-probe build stops rejecting
119
+ * aggregates; it is not load-bearing today.
120
+ *
121
+ * NOTE: this gate is only as precise as `findMatchingAggregate` (function-call.ts):
122
+ * a spelling that fingerprint-matches a SELECT-list aggregate passes; anything else
123
+ * — including a same-column-different-qualifier spelling like `sum(w.b)` against a
124
+ * SELECT list `sum(b)` — is rejected as uncollected, even though the value would be
125
+ * identical.
126
+ */
127
+ function rejectUncollectedAggregates(func, selectContext) {
128
+ const check = (expr, site) => {
129
+ for (const aggExpr of collectAggregateFunctionExprs(expr, selectContext)) {
130
+ if (findMatchingAggregate(selectContext, aggExpr))
131
+ continue;
132
+ throw new QuereusError(`Aggregate function ${aggExpr.name} in a window function's ${site} is only supported ` +
133
+ `when the same aggregate also appears in the SELECT list`, StatusCode.UNSUPPORTED, undefined, aggExpr.loc?.start.line, aggExpr.loc?.start.column);
134
+ }
135
+ };
136
+ const window = func.expression.window;
137
+ for (const partitionExpr of window?.partitionBy ?? [])
138
+ check(partitionExpr, 'PARTITION BY');
139
+ for (const orderClause of window?.orderBy ?? [])
140
+ check(orderClause.expr, 'ORDER BY');
141
+ for (const arg of func.expression.function.args ?? [])
142
+ check(arg, 'arguments');
143
+ }
53
144
  /**
54
145
  * Groups window functions by their window specification
146
+ *
147
+ * NOTE: the key is `JSON.stringify` over raw AST fragments, which include each
148
+ * fragment's source-location (`loc`) data — so two textually identical `over (…)`
149
+ * clauses at different source positions never key equal and this never actually
150
+ * groups anything; every window function gets its own WindowNode. That accident is
151
+ * currently load-bearing: {@link findWindowColumnIndex} matches a window function by
152
+ * name + spec only, so two same-named functions genuinely sharing one WindowNode
153
+ * would both resolve to the first one's output column. Stripping `loc` here (or
154
+ * comparing structurally) makes the grouping work and breaks the column matching —
155
+ * that change has to teach `findWindowColumnIndex` to match by node identity or
156
+ * position first.
55
157
  */
56
158
  function groupWindowFunctionsBySpec(windowFunctions) {
57
159
  const windowGroups = new Map();
@@ -97,39 +199,33 @@ function buildWindowFunctionArguments(windowFuncs, selectContext) {
97
199
  });
98
200
  }
99
201
  /**
100
- * Builds projections for window function output columns
202
+ * Rewrites the SELECT list into the projection that sits above the WindowNode(s).
203
+ *
204
+ * Every entry passes through — including expanded `*` columns and ordinary
205
+ * expressions, which are left exactly as built. Only entries containing a window
206
+ * function are rebuilt, and only by substituting each window result in place.
101
207
  */
102
- function buildWindowProjections(stmt, windowNode, selectContext, windowFunctions) {
103
- const windowProjections = [];
208
+ function buildWindowProjections(selectListProjections, windowNode, selectContext, windowFunctions) {
104
209
  const windowType = windowNode.getType();
105
210
  const sourceColumnCount = windowType.columns.length - windowFunctions.length;
106
- for (const column of stmt.columns) {
107
- if (column.type === 'column') {
108
- // Build each column expression once and reuse for both classification and projection
109
- const builtExpr = buildExpression(selectContext, column.expr, true);
110
- if (isWindowExpression(builtExpr)) {
111
- // Rewrite each window-function descendant into an ArrayIndexNode pointing
112
- // at its computed window-output column, preserving any surrounding
113
- // arithmetic / scalar wrapper (e.g. `1000 - row_number() over (...)`).
114
- // The top-level case (`row_number() over (...) as rn`) falls out naturally:
115
- // the whole tree is the window node, so the rewrite returns a bare
116
- // ArrayIndexNode, matching the prior behavior.
117
- const rewritten = rewriteWindowFunctions(builtExpr, windowFunctions, sourceColumnCount, windowType, selectContext.scope);
118
- windowProjections.push({
119
- node: rewritten,
120
- alias: column.alias
121
- });
122
- }
123
- else {
124
- // For regular columns, use the already-built expression
125
- windowProjections.push({
126
- node: builtExpr,
127
- alias: column.alias
128
- });
129
- }
130
- }
131
- }
132
- return windowProjections;
211
+ return selectListProjections.map(projection => {
212
+ // Rewrite each window-function descendant into an ArrayIndexNode pointing
213
+ // at its computed window-output column, preserving any surrounding
214
+ // arithmetic / scalar wrapper (e.g. `1000 - row_number() over (...)`).
215
+ const rewritten = rewriteWindowFunctions(projection.node, windowFunctions, sourceColumnCount, windowType, selectContext.scope);
216
+ if (rewritten === projection.node)
217
+ return projection;
218
+ // The rewrite replaces the authored expression with an ArrayIndexNode, whose
219
+ // own name is a bare index (`[2]`). An unaliased window column must keep the
220
+ // expression the user wrote as its output name, like every other unaliased
221
+ // select-list column (`select count(*) from t group by g` yields `count(*)`).
222
+ return {
223
+ ...projection,
224
+ node: rewritten,
225
+ alias: projection.alias ?? expressionToString(projection.node.expression),
226
+ attributeId: undefined,
227
+ };
228
+ });
133
229
  }
134
230
  /**
135
231
  * Recursively rewrites every WindowFunctionCallNode descendant of a scalar
@@ -184,6 +280,15 @@ function findWindowColumnIndex(windowNode, windowFunctions, sourceColumnCount) {
184
280
  }
185
281
  /**
186
282
  * Compares two window specifications for equality
283
+ *
284
+ * NOTE: like {@link groupWindowFunctionsBySpec}, this compares `JSON.stringify` of
285
+ * raw AST fragments including their source-location (`loc`) data, so two textually
286
+ * identical `over (…)` clauses written at different positions never compare equal.
287
+ * That is what keeps `sum(v) over (order by v) a, sum(v*10) over (order by v) b`
288
+ * resolving to two different window columns despite matching on function name.
289
+ * Making this comparison structural without also teaching
290
+ * {@link findWindowColumnIndex} to match by node identity or position collapses
291
+ * both onto the first function's column.
187
292
  */
188
293
  function compareWindowSpecs(originalWindow, funcWindow) {
189
294
  // Compare partition expressions
@@ -1 +1 @@
1
- {"version":3,"file":"select-window.js","sourceRoot":"","sources":["../../../../src/planner/building/select-window.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,UAAU,EAAmB,MAAM,yBAAyB,CAAC;AACtE,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,WAAW,EAAmB,MAAM,0BAA0B,CAAC;AACxE,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAE7D,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AAEtE;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAC/B,KAAyB,EACzB,eAAmE,EACnE,aAA8B,EAC9B,IAAoB;IAEpB,IAAI,eAAe,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAClC,OAAO,KAAK,CAAC;IACd,CAAC;IAED,IAAI,YAAY,GAAG,KAAK,CAAC;IAEzB,uDAAuD;IACvD,MAAM,YAAY,GAAG,0BAA0B,CAAC,eAAe,CAAC,CAAC;IAEjE,yDAAyD;IACzD,KAAK,MAAM,CAAC,cAAc,EAAE,SAAS,CAAC,IAAI,YAAY,EAAE,CAAC;QACxD,MAAM,SAAS,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC;QAC/B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ot":"","sources":["../../../../src/planner/building/select.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,GAAG,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,QAAQ,EAAE,KAAK,SAAS,EAAE,KAAK,kBAAkB,EAAE,KAAK,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAG/G,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAM9D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAUhD,OAAO,KAAK,EAAE,YAAY,EAAe,MAAM,sBAAsB,CAAC;AAMtE,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAwBrD;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC7B,GAAG,EAAE,eAAe,EACpB,IAAI,EAAE,GAAG,CAAC,UAAU,EACpB,UAAU,GAAE,GAAG,CAAC,MAAM,EAAE,YAAY,CAAa;AACjD;;;;;GAKG;AACH,oBAAoB,GAAE,OAAc,GACnC,QAAQ,CA2PV;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC9B,GAAG,EAAE,eAAe,EACpB,IAAI,EAAE,GAAG,CAAC,UAAU,GAClB,UAAU,CAQZ;AA6BD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,SAAS,CAAC,UAAU,EAAE,GAAG,CAAC,UAAU,EAAE,aAAa,EAAE,eAAe,EAAE,QAAQ,GAAE,GAAG,CAAC,MAAM,EAAE,YAAY,CAAa,GAAG,kBAAkB,CA6QzJ;AA6FD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,mBAAmB,CAClC,YAAY,EAAE,SAAS,MAAM,EAAE,EAC/B,SAAS,EAAE,SAAS,SAAS,EAAE,EAC/B,UAAU,EAAE,SAAS,SAAS,EAAE,EAChC,KAAK,EAAE,KAAK,GACV,cAAc,CAchB"}
1
+ {"version":3,"file":"select.d.ts","sourceRoot":"","sources":["../../../../src/planner/building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';
5
5
  import { buildTableReference } from './table.js';
6
6
  import { isMaintainedTable } from '../../schema/derivation.js';
7
+ import { isViewSchema } from '../../schema/view.js';
7
8
  import { AliasedScope } from '../scopes/aliased.js';
8
9
  import { RegisteredScope } from '../scopes/registered.js';
9
10
  import { MultiScope } from '../scopes/multi.js';
@@ -26,14 +27,15 @@ import { AssertedKeysNode } from '../nodes/asserted-keys-node.js';
26
27
  import { computeLensAssertedKeyFds } from '../../schema/lens-prover.js';
27
28
  import { buildLensAuxiliaryAccessMarker } from './lens-auxiliary-access.js';
28
29
  // Import decomposed functionality
29
- import { buildWithContext } from './select-context.js';
30
+ import { buildWithContext, enterStoredBodyEnv } from './select-context.js';
31
+ import { storedBodyContext } from '../stored-body-context.js';
30
32
  import { buildCompoundSelect } from './select-compound.js';
31
33
  import { analyzeSelectColumns, buildStarProjections } from './select-projections.js';
32
- import { buildAggregatePhase, buildFinalAggregateProjections } from './select-aggregates.js';
34
+ import { buildAggregatePhase, buildFinalAggregateProjections, buildGroupedWindowContext } from './select-aggregates.js';
33
35
  import { buildWindowPhase } from './select-window.js';
34
36
  import { buildFinalProjections, applyDistinct, applyOrderBy, applyLimitOffset, createProjectionOutputScope } from './select-modifiers.js';
35
37
  import { SortNode } from '../nodes/sort.js';
36
- import { buildSelectListAsts, resolveOrdinalReference } from './select-ordinal.js';
38
+ import { buildOrdinalAwareExpression, buildSelectListEntries } from './select-ordinal.js';
37
39
  import { buildInsertStmt } from './insert.js';
38
40
  import { buildUpdateStmt } from './update.js';
39
41
  import { buildDeleteStmt } from './delete.js';
@@ -54,12 +56,19 @@ export function buildSelectStmt(ctx, stmt, parentCTEs = new Map(),
54
56
  * expose only their declared columns.
55
57
  */
56
58
  preserveInputColumns = true) {
57
- // Apply schema path from statement if present
59
+ // A sub-select copied out of a stored view / MV body by the write-through lowering carries
60
+ // that body's whole naming environment on the AST node (`AST.StoredBodyEnv`, stamped by
61
+ // `mapNestedSelects` in `mutation/scope-transform.ts` from `buildViewMutation`). Re-enter
62
+ // it here — home schema, the body's declared `with schema` path, and the body's own
63
+ // leading `with` clause as this fragment's parent CTE namespace, in that order. A
64
+ // pass-through for every other statement; see `enterStoredBodyEnv`.
65
+ const { ctx: storedCtx, parentCTEs: storedParentCTEs } = enterStoredBodyEnv(ctx, stmt.storedBodyEnv, parentCTEs);
66
+ // A fragment's OWN `with schema` clause outranks the carried path.
58
67
  const contextWithSchemaPath = stmt.schemaPath
59
- ? { ...ctx, schemaPath: stmt.schemaPath }
60
- : ctx;
68
+ ? { ...storedCtx, schemaPath: stmt.schemaPath }
69
+ : storedCtx;
61
70
  // Phase 0: Handle WITH clause if present
62
- const { contextWithCTEs, cteNodes } = buildWithContext(contextWithSchemaPath, stmt, parentCTEs);
71
+ const { contextWithCTEs, cteNodes } = buildWithContext(contextWithSchemaPath, stmt, storedParentCTEs);
63
72
  // Handle compound set operations (UNION / INTERSECT / EXCEPT)
64
73
  if (stmt.compound) {
65
74
  return buildCompoundSelect(stmt, contextWithCTEs, cteNodes, (ctx, stmt, parentCTEs) => buildSelectStmt(ctx, stmt, parentCTEs));
@@ -106,12 +115,18 @@ preserveInputColumns = true) {
106
115
  // Build projections based on the SELECT list
107
116
  const projections = [];
108
117
  // Analyze SELECT columns
109
- const { projections: columnProjections, aggregates, windowFunctions, hasAggregates: hasAggregatesInSelect, hasWindowFunctions, hasWrappedAggregates } = analyzeSelectColumns(stmt.columns, selectContext, input);
118
+ const { projectionsByColumn, aggregates, windowFunctions, hasAggregates: hasAggregatesInSelect, hasWindowFunctions, hasWrappedAggregates } = analyzeSelectColumns(stmt.columns, selectContext, input);
110
119
  // `hasAggregates` may grow as buildAggregatePhase collects HAVING-only or
111
120
  // ORDER-BY-only aggregates; track it locally so the post-aggregate branch
112
121
  // is taken when those promote a non-aggregate query into an aggregate one.
113
122
  let hasAggregates = hasAggregatesInSelect;
114
- // Handle SELECT * separately. Keyed by the AST column so the aggregate path can
123
+ // Assemble the projection list in WRITTEN select-list order, expanding each star
124
+ // in place. Output columns follow the order the user wrote them — a star ahead of
125
+ // a named column emits its columns first, behind it emits them last — which is what
126
+ // SQLite/PostgreSQL do, and what the grouped path (`buildFinalAggregateProjections`,
127
+ // which walks `stmt.columns` itself) has always done.
128
+ //
129
+ // Star expansions are also kept keyed by their AST column so the aggregate path can
115
130
  // expand each star back to *its own* columns — a select list may hold more than
116
131
  // one star (`select a.*, b.* from a join b`).
117
132
  const starProjectionsByColumn = new Map();
@@ -120,19 +135,33 @@ preserveInputColumns = true) {
120
135
  const expanded = buildStarProjections(column, input, selectScope);
121
136
  starProjectionsByColumn.set(column, expanded);
122
137
  projections.push(...expanded);
138
+ continue;
123
139
  }
140
+ // Non-star column. Aggregate items produced no projection (they are routed
141
+ // into the aggregate phase instead) and are simply absent from the map.
142
+ const columnProjection = projectionsByColumn.get(column);
143
+ if (columnProjection)
144
+ projections.push(columnProjection);
124
145
  }
125
- // Add non-star projections
126
- projections.push(...columnProjections);
127
- // Build the source-order AST list of SELECT-list output columns (with stars expanded)
146
+ // Build the source-order list of SELECT-list output columns (with stars expanded)
128
147
  // for resolving GROUP BY / ORDER BY positional ordinals.
129
- const selectListAsts = buildSelectListAsts(stmt.columns, input);
148
+ const selectListEntries = buildSelectListEntries(stmt.columns, input);
130
149
  // Process aggregates if present
131
- const aggregateResult = buildAggregatePhase(input, stmt, selectContext, aggregates, hasAggregates, projections, hasWrappedAggregates, selectListAsts);
150
+ const aggregateResult = buildAggregatePhase(input, stmt, selectContext, aggregates, hasAggregates, projections, hasWrappedAggregates, selectListEntries, starProjectionsByColumn);
132
151
  input = aggregateResult.output;
133
152
  let preAggregateSort = aggregateResult.preAggregateSort;
134
153
  let orderByAppliedEarly = false;
135
154
  let aggregateProjectionScope;
155
+ // Set when the query is BOTH grouped and windowed: the grouped select-list
156
+ // projection list, to be projected above the window phase's WindowNode.
157
+ let windowSelectProjections;
158
+ // Also set only for a grouped, windowed query: what the aggregate's rows carry,
159
+ // so the window phase can redirect a window specification onto them and reject
160
+ // one that reads anything else.
161
+ let windowGroupedContext;
162
+ // The node whose output attributes ARE this SELECT's result columns, once one
163
+ // exists. A positional ORDER BY binds to its Nth attribute (see applyOrderBy).
164
+ let orderByOutputRelation;
136
165
  // Update context if we have aggregates
137
166
  if (aggregateResult.aggregateScope) {
138
167
  // HAVING-only or ORDER-BY-only aggregates may have promoted this into an
@@ -154,22 +183,50 @@ preserveInputColumns = true) {
154
183
  !preAggregateSort &&
155
184
  !hasWindowFunctions &&
156
185
  stmt.orderBy && stmt.orderBy.length > 0) {
157
- input = applyOrderBy(input, stmt, selectContext, preAggregateSort, undefined, true, selectListAsts);
186
+ input = applyOrderBy(input, stmt, selectContext, preAggregateSort, undefined, true, selectListEntries);
158
187
  orderByAppliedEarly = true;
159
188
  }
160
- // Build final projections if needed
161
- if (aggregateResult.needsFinalProjection && aggregateResult.aggregateNode && aggregateResult.groupByExpressions) {
189
+ // A grouped, windowed query's window specifications and function arguments are
190
+ // built against the aggregate-output scope but fall through to the pre-aggregate
191
+ // select scope for anything the aggregate does not carry. Some of what falls
192
+ // through is a legal grouping key under another spelling and some is a genuinely
193
+ // ungrouped column, so hand the window phase both halves: the maps that redirect
194
+ // the former onto the aggregate's own output columns, and the strict coverage
195
+ // test that rejects the latter at plan time. The aggregate's source relation goes
196
+ // along so both halves can tell which references belong to THIS query — a window
197
+ // specification may contain a subquery, whose own columns and whose correlated
198
+ // references to an enclosing query are neither redirected nor rejected.
199
+ if (hasWindowFunctions &&
200
+ aggregateResult.aggregateNode &&
201
+ aggregateResult.groupByExpressions &&
202
+ aggregateResult.groupByExpressions.length > 0) {
203
+ windowGroupedContext = buildGroupedWindowContext(aggregateResult.groupByExpressions, aggregateResult.aggregateNode.getAttributes(), aggregateResult.aggregateNode.getRelations()[0]);
204
+ }
205
+ // Build final projections if needed. A window function in the select list also
206
+ // forces one: the window phase projects the select list ABOVE its WindowNode, so
207
+ // the grouped select list must be materialized as a projection list here even when
208
+ // the AggregateNode's own output would otherwise have been the result shape.
209
+ if ((aggregateResult.needsFinalProjection || hasWindowFunctions) && aggregateResult.aggregateNode && aggregateResult.groupByExpressions) {
162
210
  const finalProjections = buildFinalAggregateProjections(stmt, selectContext, aggregateResult.aggregateScope, aggregateResult.aggregateNode, aggregates, aggregateResult.groupByExpressions, starProjectionsByColumn);
163
- // When HAVING-only or ORDER-BY-only aggregates were added, don't preserve
164
- // input columns so they are stripped from the output (they exist only for
165
- // those clauses).
166
- const preserveForAggregate = preserveInputColumns &&
167
- !aggregateResult.hasHavingOnlyAggregates &&
168
- !aggregateResult.hasOrderByOnlyAggregates;
169
- input = new ProjectNode(selectScope, input, finalProjections, undefined, undefined, preserveForAggregate);
170
- // Expose final-projection output column names (including SELECT-list aliases)
171
- // so subsequent ORDER BY can reference aliases like the non-aggregate path.
172
- aggregateProjectionScope = createProjectionOutputScope(input);
211
+ if (hasWindowFunctions) {
212
+ // Hand the grouped select list to the window phase instead of projecting it
213
+ // here: the WindowNode must sit between the aggregate output and the final
214
+ // projection, so there is exactly ONE projection and it is above the window.
215
+ windowSelectProjections = finalProjections;
216
+ }
217
+ else {
218
+ // When HAVING-only or ORDER-BY-only aggregates were added, don't preserve
219
+ // input columns so they are stripped from the output (they exist only for
220
+ // those clauses).
221
+ const preserveForAggregate = preserveInputColumns &&
222
+ !aggregateResult.hasHavingOnlyAggregates &&
223
+ !aggregateResult.hasOrderByOnlyAggregates;
224
+ input = new ProjectNode(selectScope, input, finalProjections, undefined, undefined, preserveForAggregate);
225
+ // Expose final-projection output column names (including SELECT-list aliases)
226
+ // so subsequent ORDER BY can reference aliases like the non-aggregate path.
227
+ aggregateProjectionScope = createProjectionOutputScope(input);
228
+ orderByOutputRelation = input;
229
+ }
173
230
  }
174
231
  }
175
232
  // Handle window functions if present
@@ -191,15 +248,14 @@ preserveInputColumns = true) {
191
248
  if (orderByClause.expr.type === 'column') {
192
249
  const orderColumn = orderByClause.expr.name.toLowerCase();
193
250
  if (!selectedColumns.has(orderColumn)) {
194
- // Apply ORDER BY before window projections
195
- const sortKeys = stmt.orderBy.map(orderBy => {
196
- const resolved = resolveOrdinalReference(orderBy.expr, selectListAsts, 'ORDER BY');
197
- return {
198
- expression: buildExpression(selectContext, resolved ?? orderBy.expr),
199
- direction: orderBy.direction,
200
- nulls: orderBy.nulls
201
- };
202
- });
251
+ // Apply ORDER BY before window projections. This sort sits BELOW the
252
+ // window projection, so a positional reference resolves through the
253
+ // select list (no output attributes exist yet to bind to).
254
+ const sortKeys = stmt.orderBy.map(orderBy => ({
255
+ expression: buildOrdinalAwareExpression(selectContext, orderBy.expr, selectListEntries, 'ORDER BY'),
256
+ direction: orderBy.direction,
257
+ nulls: orderBy.nulls
258
+ }));
203
259
  input = new SortNode(selectContext.scope, input, sortKeys);
204
260
  preWindowSort = true;
205
261
  break;
@@ -207,7 +263,10 @@ preserveInputColumns = true) {
207
263
  }
208
264
  }
209
265
  }
210
- input = buildWindowPhase(input, windowFunctions, selectContext, stmt);
266
+ input = buildWindowPhase(input, windowFunctions, selectContext, windowSelectProjections ?? projections, windowGroupedContext);
267
+ // The window phase ends in a ProjectNode over the SELECT list, so that node's
268
+ // attributes are this query's result columns.
269
+ orderByOutputRelation = input;
211
270
  // Update context to include window output columns
212
271
  const windowOutputScope = new RegisteredScope(selectContext.scope);
213
272
  const windowAttributes = input.getAttributes();
@@ -231,13 +290,16 @@ preserveInputColumns = true) {
231
290
  // attributes, which the AggregateNode does not output.
232
291
  const hasGrouping = Boolean(aggregateResult.aggregateScope);
233
292
  if (!hasGrouping && !hasWindowFunctions) {
234
- const finalResult = buildFinalProjections(input, projections, selectScope, stmt, selectContext, preserveInputColumns, selectListAsts);
293
+ const finalResult = buildFinalProjections(input, projections, selectScope, stmt, selectContext, preserveInputColumns, selectListEntries);
235
294
  input = finalResult.output;
236
295
  selectContext = finalResult.finalContext;
237
296
  preAggregateSort = finalResult.preAggregateSort;
297
+ // Either the final ProjectNode, or — for an identity `select *` — the source
298
+ // itself, whose attributes already ARE the select list.
299
+ orderByOutputRelation = finalResult.output;
238
300
  // Apply final modifiers with projection scope for column alias resolution
239
301
  input = applyDistinct(input, stmt, selectScope);
240
- input = applyOrderBy(input, stmt, selectContext, preAggregateSort, finalResult.projectionScope, false, selectListAsts);
302
+ input = applyOrderBy(input, stmt, selectContext, preAggregateSort, finalResult.projectionScope, false, selectListEntries, orderByOutputRelation);
241
303
  input = applyLimitOffset(input, stmt, selectContext, finalResult.projectionScope);
242
304
  }
243
305
  else {
@@ -249,7 +311,7 @@ preserveInputColumns = true) {
249
311
  if (!orderByAppliedEarly) {
250
312
  // In the aggregate path, ORDER BY may legally reference aggregates; in the
251
313
  // window path it may reference window outputs. Both are now in selectContext.
252
- input = applyOrderBy(input, stmt, selectContext, preAggregateSort, aggregateProjectionScope, hasAggregates, selectListAsts);
314
+ input = applyOrderBy(input, stmt, selectContext, preAggregateSort, aggregateProjectionScope, hasAggregates, selectListEntries, orderByOutputRelation);
253
315
  }
254
316
  input = applyLimitOffset(input, stmt, selectContext, aggregateProjectionScope);
255
317
  }
@@ -310,8 +372,12 @@ export function buildFrom(fromClause, parentContext, cteNodes = new Map()) {
310
372
  let columnScope;
311
373
  if (fromClause.type === 'table') {
312
374
  const tableName = fromClause.table.name.toLowerCase();
313
- // Check if this is a CTE reference
314
- if (cteNodes.has(tableName)) {
375
+ // Check if this is a CTE reference. A schema-qualified name (`main.c`) names a
376
+ // schema object, never a CTE — CTEs live in no schema — so a qualifier sends the
377
+ // name straight to the ordinary view / table resolution below, even when a
378
+ // same-named CTE is in scope. Matches the write path (`resolveCteTarget`) and
379
+ // SQLite / PostgreSQL.
380
+ if (!fromClause.table.schema && cteNodes.has(tableName)) {
315
381
  const cteNode = cteNodes.get(tableName);
316
382
  // Check if this is an internal recursive CTE reference
317
383
  if (CapabilityDetectors.isRecursiveCTERef(cteNode)) {
@@ -347,28 +413,27 @@ export function buildFrom(fromClause, parentContext, cteNodes = new Map()) {
347
413
  }
348
414
  }
349
415
  else {
350
- // Check if this is a view
351
- const schemaName = fromClause.table.schema || parentContext.db.schemaManager.getCurrentSchemaName();
352
- const viewSchema = parentContext.db.schemaManager.getView(schemaName, fromClause.table.name);
353
- let maintainedTable = viewSchema ? undefined : parentContext.db.schemaManager.getMaintainedTable(schemaName, fromClause.table.name);
354
- if (!viewSchema && !maintainedTable && !fromClause.table.schema) {
355
- // An unqualified name resolves through the schema path below
356
- // (buildTableReference); mirror that here so a path-resolved
357
- // maintained table still gets the stale re-validation guard.
358
- const pathResolved = parentContext.db.schemaManager.findTable(fromClause.table.name, undefined, parentContext.schemaPath);
359
- if (isMaintainedTable(pathResolved))
360
- maintainedTable = pathResolved;
361
- }
416
+ // Check if this is a view. An unqualified name resolves through the
417
+ // schema search path — one path entry at a time, that schema's tables
418
+ // and views checked together — exactly as `buildTableReference` below
419
+ // resolves a plain table name.
420
+ const resolvedItem = parentContext.db.schemaManager.findSchemaItem(fromClause.table.name, fromClause.table.schema, parentContext.schemaPath);
421
+ const viewSchema = isViewSchema(resolvedItem) ? resolvedItem : undefined;
422
+ const maintainedTable = !isViewSchema(resolvedItem) && isMaintainedTable(resolvedItem) ? resolvedItem : undefined;
362
423
  if (viewSchema) {
363
424
  // Build the view's body. The body is a QueryExpr — today only
364
425
  // SELECT and VALUES bodies plan; DML bodies are rejected at
365
426
  // CREATE VIEW plan time so we never get here with one.
427
+ // The body plans under the VIEW's home-schema path (not the
428
+ // caller's) so its unqualified source names resolve next to the
429
+ // view — independent of the reading statement's search path.
430
+ const viewBodyContext = storedBodyContext(parentContext, viewSchema.schemaName);
366
431
  let viewSelectNode;
367
432
  if (viewSchema.selectAst.type === 'select') {
368
- viewSelectNode = buildSelectStmt(parentContext, viewSchema.selectAst, cteNodes);
433
+ viewSelectNode = buildSelectStmt(viewBodyContext, viewSchema.selectAst);
369
434
  }
370
435
  else if (viewSchema.selectAst.type === 'values') {
371
- viewSelectNode = buildValuesStmt(parentContext, viewSchema.selectAst);
436
+ viewSelectNode = buildValuesStmt(viewBodyContext, viewSchema.selectAst);
372
437
  }
373
438
  else {
374
439
  throw new QuereusError(`View '${viewSchema.name}' has a ${viewSchema.selectAst.type.toUpperCase()} body, which is not yet supported.`, StatusCode.UNSUPPORTED);
@@ -433,13 +498,18 @@ export function buildFrom(fromClause, parentContext, cteNodes = new Map()) {
433
498
  if (maintainedTable.derivation.stale) {
434
499
  // Re-validate the body against current source schemas. An
435
500
  // incompatible change (dropped source, dropped column, …) makes
436
- // the body fail to plan — surface the staleness diagnostic.
501
+ // the body fail to plan — surface the staleness diagnostic. Under the
502
+ // MV's home-schema path, like every other body re-plan: the caller's
503
+ // path would make a perfectly resolvable non-`main` body look
504
+ // incompatibly changed, hiding the materialized rows behind a false
505
+ // staleness error.
506
+ const bodyContext = storedBodyContext(parentContext, maintainedTable.schemaName);
437
507
  try {
438
508
  if (maintainedTable.derivation.selectAst.type === 'select') {
439
- buildSelectStmt(parentContext, maintainedTable.derivation.selectAst, cteNodes);
509
+ buildSelectStmt(bodyContext, maintainedTable.derivation.selectAst);
440
510
  }
441
511
  else if (maintainedTable.derivation.selectAst.type === 'values') {
442
- buildValuesStmt(parentContext, maintainedTable.derivation.selectAst);
512
+ buildValuesStmt(bodyContext, maintainedTable.derivation.selectAst);
443
513
  }
444
514
  }
445
515
  catch (e) {