Skip to content

fix(aggexec): support YEAR input for variance/stddev aggregates (#30181) - #30365

Merged
XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
daviszhen:fix-30181-year-statistical-agg
Oct 16, 2026
Merged

XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
daviszhen:fix-30181-year-statistical-agg

Conversation

@daviszhen

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • ALTER
  • HOT-FIX
  • FEATURE
  • BUG-FIX
  • DOC
  • TEST
  • REFACTOR
  • MERGE-REQUEST-FIX

Which issue(s) this PR fixes:

issue #30181

What this PR does / why we need it:

The statistical-aggregate binder accepts YEAR through its numeric
type set, but the executor constructor (makeVarStdDevExec in
pkg/sql/colexec/aggexec/vardev2.go) had no T_year branch and
panicked with internal error: unsupported type 'YEAR' for var/stddev.
So SELECT VAR_POP(y) FROM t over a YEAR column failed even though the
query is valid and works in MySQL 8.0 (VAR_POP over years
2020/2021/2022 = 0.6666666666666666).

This PR routes T_year to the float64-state executor over the
underlying types.MoYear (int16) storage value — the same approach
SUM/AVG already use for YEAR — by:

  • adding types.MoYear to the value-type unions of the
    variance/stddev executor
  • adding the case types.T_year branch to makeVarStdDevExec

The YEAR numeric value is used (not date-part decomposition), matching
MySQL semantics; VAR_POP(y) and VAR_POP(CAST(y AS DOUBLE)) return
identical results, including NULL and empty-set handling, and DISTINCT
works via byte-exact payload equality (unit-tested).

Tests

  • unit: TestVarianceYearResults covers var_pop / var_samp /
    stddev_pop / stddev_samp for YEAR in resident and distinct modes
  • BVT: YEAR block in func_aggr_var_pop covering all four aggregates,
    NULL rows and the empty-set case

…ixorigin#30181)

The statistical-aggregate binder accepts YEAR through its numeric type
set, but the executor constructor (makeVarStdDevExec) had no YEAR branch
and panicked with "unsupported type 'YEAR' for var/stddev". This made
SELECT VAR_POP/STDDEV_* over a YEAR column fail while the same query
succeeds in MySQL 8.0.

Route T_year to the float64-state executor over the underlying
types.MoYear (int16) value, matching how SUM/AVG already handle YEAR.
The YEAR numeric value is used, so VAR_POP over years 2020/2021/2022
returns 0.6666666666666666 as in MySQL.

Tests:
- unit test covering var_pop/var_samp/stddev_pop/stddev_samp for YEAR,
  resident and distinct modes
- BVT coverage including NULL and empty-set handling
@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 →

@daviszhen

Copy link
Copy Markdown
Contributor Author

Reviewed head 86d9b7ebb964d6bf684615ebdcdf1d59d2b54100 against base b24d1b311555e0b6f3d56eb37f9a2e9617e9a5b7.

Recommendation: approve from code review, subject to completion of the remaining required CI. No confirmed blocking findings.

The new T_year dispatch uses types.MoYear, matching YEAR's actual int16 representation rather than reinterpreting the vector as a plain int16 type. Its numeric values are exactly representable as float64. I also traced the resident, DISTINCT, partial-state merge, serialization/spill, and NULL/singleton paths; this change reuses their existing behavior without introducing a different state layout.

The added unit cases cover all four variance/stddev variants, with and without DISTINCT. The SQL regression also covers the variance alias, NULL input, and an empty result. As a non-blocking follow-up, add YEAR-specific partial-merge and serialization/spill round trips, including all-NULL/singleton groups and YEAR 0000, so future state changes cannot silently regress the newly supported type.

Validation: git diff --check passed. On this head, shared build, SCA, UT coverage, aggregate coverage, and both active multi-CN BVT jobs passed; the ordinary Ubuntu UT job is still running. Local variance tests were attempted, but dependency compilation failed with no space left on device, so I am not claiming an independent local test pass or a full SQL rerun.

Ordinary review comment only; no formal review-state change.

@daviszhen

Copy link
Copy Markdown
Contributor Author

Follow-up review of latest head 89d27c6c748d1e7a1ff641f3ed31ff3647625fb3 against 575d57ae5546aa788707707ffe0af7f128d91f1a. Recommendation: approve from code review, subject to the new required CI completing successfully. No confirmed blocking finding in this change.

The head updated during review. I checked the merge from main: the net four-file PR patch has the same stable patch ID as the previously reviewed 86d9b7ebb9, and I reran the targeted tests on the new head rather than relying only on the earlier run.

Independent local validation now passes:

  • Existing TestVariance* tests, including all four YEAR aggregate variants with and without DISTINCT.
  • Review-only YEAR probes covering BulkFill/Fill/BatchFill, Merge/BatchMerge, intermediate-state serialization and merge, empty/all-NULL/singleton groups, duplicates, YEAR 0000, and the 1901/2155 boundaries. The expected values use an independent integer pairwise-difference calculation.
  • Legacy wire/state probes also pass with an appropriate legacy precision tolerance. The initial stricter probe exposed approximately 3.39e-10 relative error, but a control test confirms bit-identical results with the existing int16 legacy path; this is not a YEAR-specific regression. The modern path passes the tighter tolerance.
  • git diff --check passes.

The previous head's Ubuntu UT job did eventually fail, but its diagnostics show the aggexec package and every TestVarianceYearResults subcase passed. The failing package was pkg/tests/issues: TestIssue26875MultipleUniqueSetNullActionsTerminate hit a context deadline, followed by bad/closed-connection failures in the multilevel cascade cases. These logs do not establish a regression caused by the YEAR dispatch change; they also do not prove the failure is harmless or fixed.

The new head's CI is still queued/running, so the earlier green checks must not be treated as a completed run for this merge commit. Please wait for that run and investigate any repeated foreign-key/cluster timeout separately. Keeping the added YEAR merge/serialization edge cases as committed regressions would still be a useful non-blocking follow-up.

Ordinary review comment only; no formal approval submitted or source changes pushed. The additional tests were review-only files in an isolated worktree; no full local BVT pass is claimed.

@XuPeng-SH

XuPeng-SH commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

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

Commit: 89d27c6c748d1e7a1ff641f3ed31ff3647625fb3

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH 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.

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

Commit: 89d27c6c748d1e7a1ff641f3ed31ff3647625fb3

Assessment: approve

Reviewed all four changed files and affected callers. YEAR dispatch correctly reuses the existing variance/stddev implementation, preserving numeric semantics, state layouts and cleanup without adding per-row work to existing type paths. No actionable findings remain.

Model: gpt-6-astra / xhigh

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

Validation:

  • Variance tests passed, including all eight new YEAR cases; receipt 1792144092675003105-3.json.

  • Full aggexec package passed, including 64 diagnostic YEAR scenarios covering DISTINCT, modern/legacy state, merge, wire/spill round trips and boundary/NULL/empty/singleton inputs; receipt 1792144160008963370-3.json.

  • Merge-base control reproduced the original unsupported-YEAR panic; receipt 1792144195478918238-3.json. This was an expected baseline failure.

  • Public MySQL-protocol checks passed the new 13-statement SQL block twice, plus aliases, CAST equivalence, boundary values and cleanup; receipt 1792144203332000292-3.json.

  • Frozen exact-head CI records successful required checks. Clean sources, native provenance, diagnostic inputs and receipt/log correspondence were verified in /home/xupeng/.local/state/mo-bot/jobs/88c6328cb9f414a055e90098/attempt-1/evidence-audit.json; final audit receipt 1792144313219555514-4.json.

Limitations:

  • Additional edge tests are retained diagnostic overlays, not committed regressions.

  • Local public validation used an embedded-cluster MySQL endpoint; it was not a full mo-tester run, mixed-version deployment test or MySQL-server comparison.

  • No performance benchmark was needed: existing execution loops and allocation policies are unchanged.

@mergify mergify Bot added the queued label Oct 16, 2026
@mergify

mergify Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-16 09:53 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-10-16 10:59 UTC · at ccda682d06ffad37652ab31bc3344f5821b0b312

This pull request spent 1 hour 5 minutes 45 seconds in the queue, with no time running CI.

Reason

Pull request #30365 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

This branch was previously deployed

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

Labels

dequeued kind/bug Something isn't working size/S Denotes a PR that changes [10,99] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants