Skip to content

fix(plan): resolve HAVING aliases before ungrouped inputs - #30865

Merged
XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/fix-30795-having-aggregate-alias
Oct 22, 2026
Merged

XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/fix-30795-having-aggregate-alias

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Oct 22, 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 #30795.

What this PR does / why we need it:

SELECT k, SUM(v) AS s ... GROUP BY k HAVING s = 30 failed under ONLY_FULL_GROUP_BY when the input also contained a column named s. Early source qualification hid the projected alias.

Keep output-name resolution in HavingBinder, after actual GROUP BY identity and before ungrouped inputs. Aggregate and window arguments retain source scope and aggregate reuse. For deferred names from FULL JOIN USING, match the complete source expression, including its merged COALESCE identity, so both bare and compound grouping expressions preserve unmatched-side and NULL results. Restrict the new aggregate-alias rule to the current query level; parent source lookups retain correlation depth through the existing binder path. Strict MATRIXONE_NATIVE, nonaggregate HAVING, qualified sources and duplicate-output ambiguity policy remain unchanged.

Extend existing planner and public SQL fixtures for collisions, grouped/qualified sources, correlated references, local subquery shadowing, merged/compound groups, aggregate reuse, invalid grouping/nesting, NULL/empty input, ROLLUP and prepared reuse. The existing ONLY_FULL_GROUP_BY BVT saves/restores its incoming session mode, preventing cross-file contamination.

Validation (Go 1.27.1, Linux amd64, fresh repository-owned native build):

  • Retained exact-head versus merge-base SQL probes reproduce both review findings; additional compound USING-group probes fail on the old head and pass on the merge-base control.
  • Complete planner package with -race -count=1: 4,099 top-level tests passed, no skips or race reports (78.6s).
  • Relevant public SQL integration selection with -race -count=1: 5 roots and 89 subtests passed, including existing grouping, correlation and window consumers (12.1s). Expanded collision SQL fixture also passed normally.
  • Incremental go vet, Molint and configured golangci-lint over pkg/sql/plan and pkg/tests/sqlintegration passed; lint reported 0 issues. Molint exited 0 and retained four nonfatal diagnostics in unchanged files; none was on a PR file.
  • Ran ONLY_FULL_GROUP_BY and the three failed CI subquery BVT files together twice in normal comparison mode, with unchanged result assertions and equivalent -n -g -o -p flags. Each run: 712 successes, 17 existing ignored statements, 0 failures/abnormal results; database teardown checked between runs. No expected results were generated from output.

Changes remain in planning/name resolution, with guarded normalization only for merged USING alias candidates and no new per-row execution work or persisted state. The existing FULL USING correlated-name limitation also fails on the clean base and is outside this correction. Distributed Proxy CI and coverage aggregation on the updated commit remain pending; the original coverage job was blocked by its failed BVT producer, not a coverage-rate verdict.

@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 22, 2026 •

Copy link
Copy Markdown
Contributor Author

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

Commit: b38a3cab79630b01bd6320417b99a29fd3a29681

Review completed. Read the assessment.

@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: b38a3cab79630b01bd6320417b99a29fd3a29681

Assessment: request_changes

Reviewed the complete seven-file delta and affected callers. Keeping alias resolution in HavingBinder is a reasonable design, but the implementation breaks grouped-column precedence for merged USING columns and correlated references. Both regressions were reproduced against merge-base production-code controls.

Model: gpt-6-astra / xhigh

Session: 01a13dcf-1517-7f90-b3f9-c5f9f9e7031b · slot 0 · generation 1

  • P2 pkg/sql/plan/bind_context.go:748: With a(k,v) containing (1,10) and b(k) containing (2), execute SELECT SUM(a.v) AS k FROM a FULL OUTER JOIN b USING(k) GROUP BY k HAVING k=1 under ONLY_FULL_GROUP_BY.
    Impact: The candidate silently returns no rows instead of 10. Deferring the bare alias candidate skips the existing merged USING-column expansion, so HAVING resolves k to SUM(a.v) rather than the grouped COALESCE(a.k,b.k). Preserve merged group identity before alias resolution and add a regression covering both sides of the full join.
    Evidence: The candidate returned [] while the merge-base control returned [10]; HAVING k=2 similarly lost the expected NULL row. Explicit HAVING COALESCE(a.k,b.k)=1 passed on both. Retained sql-head.log and sql-base.log match receipts 1792675090877701429-4.json (exit 1) and 1792675155494455821-3.json (exit 0).

  • P2 pkg/sql/plan/having_binder.go:148: With g(k,v) containing (1,10),(2,20), execute SELECT SUM(v) AS k FROM g GROUP BY k HAVING EXISTS(SELECT 1 WHERE k=1) under ONLY_FULL_GROUP_BY.
    Impact: A previously valid correlated query now fails with 'Column remapping failed: cannot find column reference'. Parent correlation calls BindColRef directly, bypassing BindExpr's grouped-expression lookup; the expanded alias branch captures k as an ordinary aggregate reference. Preserve correlated source/group resolution and correlation depth before expanding output aliases.
    Evidence: The candidate failed remapping sum(g.v), whereas the merge-base control returned [10]. Qualifying the inner reference as g.k passed on both. Retained correlation-head.log and correlation-base.log match receipts 1792675235839837361-3.json (exit 1) and 1792675288463155503-3.json (exit 0).

Validation:

  • Artifacts are retained under /home/xupeng/.local/state/mo-bot/jobs/e9992199e95613284e08c1ff/attempt-1. Final audit verified the frozen head, clean source, native input hashes, overlay contents, commands, terminal receipts and log outcomes: review-audit-final.log; checks/1792675497999498422-4.json, exit 0.

  • Isolation preflight passed. Native prerequisites were freshly built through the repository Makefile from verified current-revision inputs, using assigned task caches.

  • Focused HAVING/ROLLUP planner tests passed. The complete planner package passed 4,099 top-level tests without skips: planner-package.log; checks/1792675240248664621-3.json, exit 0.

  • All 19 subtests of the PR's public SQL TestHavingAliasCollision passed. Its combined diagnostic command exited 1 because the additional merged-USING regression probes failed; this command is not counted as an overall pass.

  • Both regression comparisons used read-only Go overlays restoring exactly the two changed production files from the merge-base. Qualified-reference controls passed, and baseline diagnostic commands exited 0.

  • Changed Go files passed gofmt verification, and the base-to-head diff passed whitespace checks.

Limitations:

  • Frozen CI remained partly in progress. local_validation contained no retained worker evidence; author-reported race, MySQL and BVT results lacked raw artifacts and were not counted as independently verified.

  • The full SQL integration package, BVT harness and race suite were not rerun. The separate linked issue body was unavailable; its behavioral contract was assessed from the frozen PR description.

  • No performance benchmark was run. Source tracing places added work in planning and alias lookup, with no new per-row execution, storage I/O or concurrency machinery.

@XuPeng-SH

XuPeng-SH commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor Author

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

Commit: ee1b47589c4caa3dae9d2c08af52ef462d9e6182

Review completed. Read the assessment.

@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: ee1b47589c4caa3dae9d2c08af52ef462d9e6182

Assessment: approve

Reviewed the complete seven-file delta and affected callers. HavingBinder appropriately owns grouped-expression-before-alias resolution, preserving aggregate-input scope and correlation depth. Both previously reported regressions are resolved. The added normalization is confined to planning; no actionable defects remain.

Model: gpt-6-astra / xhigh

Session: 01a13dcf-1517-7f90-b3f9-c5f9f9e7031b · slot 0 · generation 1

Validation:

  • Isolation preflight passed; independently verified 200 native inputs and five build outputs before testing.

  • Full planner package passed: 4,099 root tests; two unrelated subtests skipped.

  • Public SQL validation passed: three roots and 73 subtests, including both prior regressions and 16 independent boundary cases covering compound grouping, merged USING expressions, nested correlations, aggregate inputs and rejection paths.

  • Changed-file formatting, diff whitespace and go vet for both affected packages passed.

  • Matched commands, clean-head fingerprints, exit receipts and retained logs in /home/xupeng/.local/state/mo-bot/jobs/1eaf6db836e7a97e8b16ed4e/attempt-1/validated-evidence.json. Complete coverage notes are in review-notes.json.

Limitations:

  • Supplied local_validation was empty and CI was pending; unretained author validation claims were not counted as passing evidence.

  • Local execution used one-CN SQL integration. Full distributed BVT, race instrumentation and performance benchmarks were not rerun; performance was assessed through affected call paths.

  • The linked issue body was absent; its documented reproduction and behavioral contract were assessed from the frozen PR evidence.

This branch was previously deployed

1 inactive deployment
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 size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: HAVING prefers a same-named source column over the SELECT aggregate alias, rejecting queries MySQL runs

2 participants