Skip to content

fix(sql): lower scalar window inputs and repair NULL and empty reuse - #29918

Merged
XuPeng-SH merged 6 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/issue29877-window-subqueries
Oct 12, 2026
Merged

XuPeng-SH merged 6 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/issue29877-window-subqueries

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Fixes #29877
Fixes #29878
Fixes #29917

What this PR does / why we need it:

Window inputs such as ROW_NUMBER() OVER (PARTITION BY id ORDER BY (SELECT NULL)) were accepted by binding but reached execution without scalar-subquery lowering. Literal NULL peer keys also failed, and restored NULL execution exposed payload-free vector cleanup and empty-input reuse defects.

Design and behavior

  • Lower each window's function, partition and order inputs through the existing scalar-subquery owner, over the post-aggregation input, before adding any window operator. Keep the existing correlation, empty-result, row-cardinality and COUNT reconstruction paths; no-FROM scalars add no JOIN.
  • Enforce one column for standalone scalar subqueries before lowering can discard additional columns. Preserve multi-column row comparisons, IN and EXISTS through their existing consumer classification.
  • Preserve earlier peer boundaries when a constant NULL key contributes no additional boundary. Reuse the nullable comparator and vector Copy ownership for domainless ANY NULL; unsupported non-NULL ANY remains rejected.
  • Clear logical vector length on cleanup even when NULL vectors have no payload. At end of input, evaluate a window only when its retained batch is nonempty, preserving normal capacity reuse and cleanup.

Compatibility: invalid SET @v=(SELECT a,b ...) now fails on column count instead of silently taking the first column or reaching a row-count error. An independently executed MySQL 8.0.46 control rejects this query for zero, one and multiple rows too. The existing sequence BVT now explicitly checks rejection and preservation of the previously assigned variable value; valid single-column assignments remain covered. Row comparisons and EXISTS still accept multiple columns. There are no persisted-format, catalog or wire-layout changes.

Validation

Rebased onto main at 41ac6242b7620aa069db91492db1d41584319b05 without conflicts. Final code was checked with Go 1.27.1 and fresh, matching CPU native artifacts.

  • Full normal and -race -count=1 UT: pkg/compare, pkg/container/vector, pkg/partition, pkg/sort, pkg/sql/plan, pkg/sql/colexec/window, and pkg/sql/colexec/partition.
  • Actual SQL protocol regression, normal/race: TestWindowScalarSubqueryInputs (5.543s / 9.359s). Covers scalar/window keys, correlation, grouped/HAVING input, empty/multiple rows and columns, NULL peers, stacked windows, ROWS/RANGE, prepared nonempty → empty → nonempty reuse, batch boundaries, and failed SET preserving session state.
  • BVT normal comparison on an owned standalone instance: sequence/seq_func2.sql 80/80 twice, window scalar inputs 22/22 twice, and existing default-frame controls 10/10. No failed, ignored or abnormal statements. Independent teardown checks found no leftover test tables, sequences or window database.
  • Focused lifecycle race repetitions: constant-NULL vector cleanup and empty window reuse 100 runs each; the actual protocol regression 4 runs, selected from its measured duration within a 30s budget. All passed.
  • Additional public probes: table-backed untyped NULL scalars with zero/one row as window ORDER/PARTITION keys produce the expected peer/rank results. Session sort_spill_mem=1 controls with NULL order/partition keys also return the expected results.
  • Incremental SCA over the complete PR Go delta and direct consumers: gofmt/diff checks, go vet, repository molint, and configured golangci-lint 2.14.0. 0 introduced findings; golangci 0 issues. Eight molint diagnostics are identical with the complete PR Go delta replaced by exact base sources; they are not new findings.
  • Independent design and final overall review: actual gpt-6.1-sol with xhigh; no unresolved material blocker.

The latest CI correction changes tests and their expectations only; it adds no production work, allocation or special execution path. The overall change adds no worker, cache, state machine or per-row allocation. This is a functional correction; no query-throughput improvement is claimed.

Production: 74 insertions / 6 deletions; tests/SQL/results: 523 insertions / 18 deletions; documentation: 0. Fixtures and common owners are reused. These checks do not claim a full local multi-CN Compose run or a passing remote CI run; remote CI remains to be evaluated.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH

XuPeng-SH commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: 94e21a4c462ca82ad90c52941187f67f99582230

Assessment: approve

已完成全部 17 个文件及相关调用者、职责归属、错误与复用路径的审查,无确认的未解决缺陷。窗口输入复用既有标量子查询降级逻辑,执行顺序正确;NULL 比较、边界保留和空输入清理修复保持原有所有权,没有引入重复状态机。

Model: gpt-6-astra / xhigh

Session: 01a12d8f-7dea-77c2-a565-38d45b5564e4 · slot 0 · generation 0

Validation:

  • 确认 HEAD 和 merge-base 精确匹配;两次 attempt 补丁一致,复用上一轮已完成检查。git diff --check 通过,源码工作区保持干净。

  • 核对标量列数、行比较/IN/EXISTS 兼容性、聚合/HAVING 后的窗口输入、COUNT 空结果、多个窗口生产者,以及 Prepare→Call→Reset/Free 生命周期。

  • 复用冻结的当前 HEAD CI:共享构建、Ubuntu/x86 UT、Linux/arm64 SCA、覆盖率及 PROXY/PESSIMISTIC 两组多 CN BVT 成功;未将 skipped 项计为通过。

  • 复用作者报告的七个相关包正常/race 验证、真实 SQL 协议测试、生命周期各 100 次 race 验证,以及 sequence 80/80 两次、窗口输入 22/22 两次和默认帧 10/10 BVT。

  • 审查测试的结果与错误断言、失败 SET 保留 session 状态、准备语句空/非空复用、跨批次边界及清理。冻结评论无实质性待解决评审意见。

  • 完整变更映射、证据来源和审查记录保存于 attempt-2/review-record.json。

Limitations:

  • 冻结材料未包含关联 issue 正文;依据 PR 陈述的行为契约、源码和测试核对。

  • CI 原始日志和作者运行产物未随材料提供;复用了精确 HEAD 的终态快照及详细作者验证记录,本轮未重复执行 native/SQL 测试。

  • 没有吞吐基准,不宣称性能提升或已测得无退化;代码审查未发现此次增量引入每行分配、额外 I/O 或新的持续增长状态。

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: 94e21a4c462ca82ad90c52941187f67f99582230

Assessment: approve

已完成全部 17 个文件及相关调用者、职责归属、错误与复用路径的审查,无确认的未解决缺陷。窗口输入复用既有标量子查询降级逻辑,执行顺序正确;NULL 比较、边界保留和空输入清理修复保持原有所有权,没有引入重复状态机。

Model: gpt-6-astra / xhigh

Session: 01a12d8f-7dea-77c2-a565-38d45b5564e4 · slot 0 · generation 0

Validation:

  • 确认 HEAD 和 merge-base 精确匹配;两次 attempt 补丁一致,复用上一轮已完成检查。git diff --check 通过,源码工作区保持干净。

  • 核对标量列数、行比较/IN/EXISTS 兼容性、聚合/HAVING 后的窗口输入、COUNT 空结果、多个窗口生产者,以及 Prepare→Call→Reset/Free 生命周期。

  • 复用冻结的当前 HEAD CI:共享构建、Ubuntu/x86 UT、Linux/arm64 SCA、覆盖率及 PROXY/PESSIMISTIC 两组多 CN BVT 成功;未将 skipped 项计为通过。

  • 复用作者报告的七个相关包正常/race 验证、真实 SQL 协议测试、生命周期各 100 次 race 验证,以及 sequence 80/80 两次、窗口输入 22/22 两次和默认帧 10/10 BVT。

  • 审查测试的结果与错误断言、失败 SET 保留 session 状态、准备语句空/非空复用、跨批次边界及清理。冻结评论无实质性待解决评审意见。

  • 完整变更映射、证据来源和审查记录保存于 attempt-2/review-record.json。

Limitations:

  • 冻结材料未包含关联 issue 正文;依据 PR 陈述的行为契约、源码和测试核对。

  • CI 原始日志和作者运行产物未随材料提供;复用了精确 HEAD 的终态快照及详细作者验证记录,本轮未重复执行 native/SQL 测试。

  • 没有吞吐基准,不宣称性能提升或已测得无退化;代码审查未发现此次增量引入每行分配、额外 I/O 或新的持续增长状态。

@aunjgr aunjgr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 94e21a4 against merge-base 41ac624. No blocking finding.

Window scalar producers are lowered over post-aggregation input before any window operator, retaining the existing scalar correlation/cardinality and COUNT owners. The standalone scalar column-count check does not take over row-comparison or EXISTS consumers. Constant NULL keys preserve previously established peer boundaries, domainless NULL uses nullable comparison/copy without admitting arbitrary ANY values, and cleanup/empty-input reuse no longer retains logical rows or evaluates an empty retained batch.

The tests cover grouped/HAVING and correlated inputs, independent window producers, NULL peers and frames, scalar row/column errors, session state after failed SET, batch boundaries, and prepared nonempty/empty/nonempty reuse. The changed sequence golden explicitly checks rejected invalid assignments rather than accepting generated results.

Validation: reused the reported owning normal/race packages, lifecycle repetitions and real protocol/BVT evidence; current-head CI passes. I did not rerun a native service or multi-CN topology locally.

@XuPeng-SH
XuPeng-SH merged commit 5a5501b into matrixorigin:main Oct 12, 2026
@XuPeng-SH
XuPeng-SH deleted the codex/issue29877-window-subqueries branch October 12, 2026 03:07
XuPeng-SH added a commit that referenced this pull request Oct 12, 2026
## What type of PR is this?

- [x] Test and CI

## Which issue(s) this PR fixes:

Fixes #29925

## What this PR does / why we need it:

Adds regression coverage for LAG/LEAD explicit NULL defaults across
partitions of sizes two and one. The cleanup fix already merged in
#29918 clears payload-free NULL-vector lengths; these tests verify the
reported SQL behavior and protect that reuse contract.

SQL tests check untyped NULL, typed DOUBLE NULL, omitted, zero and
row-dependent defaults, asserting every value and NULL bit. Vector
coverage adds DOUBLE and shrinking, empty and growing lengths, retaining
provenance and memory-cleanup assertions. Production code is unchanged.

Validation: focused normal and race checks passed 11 top-level tests per
mode across vector, window and SQL integration, including all ten new
SQL cases and existing cancellation/error/lifecycle controls, with no
skips or race reports. Incremental gofmt and go vet passed; configured
golangci-lint reported 0 issues. Molint ran over the same three
packages; its two unsafe-import diagnostics are in unchanged files
verified byte-identical to the base.

Tests used Go 1.27.1, CGO_ENABLED=1 and fresh native artifacts. Local
SQL fixtures used a diagnostic configuration overlay changing only the
Unix socket pathname to avoid Linux path-length limits; TCP query
execution and assertions were unchanged. The final gpt-6.1-sol/xhigh
review accepted the exact committed candidate. Remote CI remains
pending.

Closes #29925

<!-- mo-bot-work:v1
d588c0dab2bbd7a86c7e440568816522c76cb3533c546f4709cf5879e68851ca -->

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

This branch had an error being deployed

1 failed deployment
ci — 9671c525 Deployed Oct 12, 2026 by XuPeng-SH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working kind/enhancement kind/test-ci size/L Denotes a PR that changes [500,999] lines

Projects

None yet

3 participants