Repository navigation
fix(aggregate): align exact AVG types with MySQL - #27873
Conversation
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
|
已更新分支并处理 failing checks 与代码冲突:
本机定向 Go 测试受 macOS 原生依赖头文件缺失影响,已由 CI 重新验证。 |
|
Addressed in commit adde979. Reconciled the #27869 overflow fixtures with the exact AVG contract: DECIMAL(38,10) now uses the required DECIMAL(42,14) Decimal128-to-Decimal256 path, so promoted boundary cases and window/BVT expectations are successful results rather than obsolete Decimal128 overflow errors. Updated the PK-group-elimination assertions accordingly, retained narrow-target decAvg error coverage, and kept Decimal256 declared-precision overflow coverage. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
Deep re-review of exact head af506cc077f9d29ce9ceee727cce8d5ed78090cd against base c46d897e9645b80178568ef0783dd8e99e527222, including the complete AVG type/execution change, the increment after my prior requested-changes head adde9798bc69, ordinary/DISTINCT/window/distributed merge paths, negative rounding, Decimal128-to-256 promotion, high-scale inputs, allocation/error cleanup, internal consumers, and constant-expression counterexamples.
The four prior blockers are materially addressed: exact AVG now rounds once at the declared scale; valid high-scale Decimal128 input is promoted without truncating its source scale; direct literal metadata is value-sensitive; and the common INT32 path has a dedicated production fast finalizer plus benchmark coverage. I found no new lifecycle, deadlock, or common-path performance blocker. One correctness/type-system boundary remains:
-
[P1][correctness/metadata] Integer constant-expression precision is unbounded, but integer AVG is always declared as Decimal128.
avgIntegerConstantPrecisionrecursively adds precision for multiplication/addition (base_binder.go:3636-3665) and writes it directly into the integer argument type.AvgReturnTypethen unconditionally returnsDECIMAL128(precision+4,4)for every integer OID (sumavg2.go:35-40) without the Decimal128 limit/promotion used by decimal inputs.A valid, non-overflowing counterexample is:
SELECT AVG(0 * 100000000 * 100000000 * 100000000 * 100000000);
The expression value is exactly zero, while the new AST rule derives precision
1+9+9+9+9 = 37, so AVG is declared asDECIMAL128(41,4). That width is not representable by the Decimal128 public type; CTAS and protocol metadata can therefore expose an invalid schema even though execution data is trivial. Addition/cancellation chains can reach the same boundary.This must be closed coherently, not by changing only the return OID:
flushExactAvgcurrently states that native integer AVG always returns Decimal128, and its non-Decimal128 branch callsdecAvg[Decimal128], which rejects a Decimal256 result type (sumavg2.go:873-917). Define a bounded type rule for integer constant expressions and make planner, factory/state, ordinary/DISTINCT/window finalization, CTAS, and protocol metadata agree. Add boundary tests immediately below/above precision 38, including a zero/cancellation expression so value overflow cannot mask the metadata defect.
Validation note: exact-head substantive CI is terminal green and current main has no post-base overlap in the touched production paths. Focused local compilation was blocked by this isolated worktree's missing third-party usearch.h while building its exact CGo prerequisite, so I do not claim a local test pass; the blocker above follows directly from the exact production type equations and existing test-confirmed integer-expression path.
|
Addressed the remaining wide integer constant-expression AVG boundary in commit
Validation:
|
|
Follow-up: the branch was amended to |
|
Follow-up for the coverage check: commit |
|
CI run 33646744442 is now green. The Ubuntu UT, SCA, both BVT jobs, UT Coverage, and final Coverage merge all passed on head |
|
当前 PR 与最新 main 存在 merge conflict,暂无法进入 review 队列。请 rebase 最新 main 并解决冲突后重新 push,完成后我再继续 review。 |
# Conflicts: # pkg/sql/plan/bind_bool_numeric_aggregate_test.go
What type of PR is this?
What this PR does / why we need it
Continues #24436 by aligning the public
AVGcontract for exact numeric inputs with MySQL instead of applying a CTAS-only schema patch.AVG(INT) -> DECIMAL(14,4)andAVG(BIGINT) -> DECIMAL(23,4).precision + 4andscale + 4, e.g.AVG(DECIMAL(20,6)) -> DECIMAL(24,10).This fixes both metadata and value semantics. For example, AVG over integer values
1, 2, 4now returns2.3333rather than a DOUBLE approximation.SUM precision rules are intentionally out of scope and will be handled separately.
Which issue(s) this PR fixes
Related to #24436
Special notes for your reviewer
The Decimal128-to-Decimal256 promotion is required for inputs such as
DECIMAL(38,19): clamping the result to DECIMAL128 would either discard integer capacity or lose the required four additional fractional digits.Testing
make buildgo test ./pkg/sql/colexec/aggexec -count=1go test ./pkg/sql/plan ./pkg/sql/colexec/window -count=1-g -n), includingfunc_aggr_avg, numeric/decimal/decimal256, prepared aggregates, grouping, DISTINCT, CTAS, and the full window caseDECIMAL(38,19)promotion