Skip to content

fix: cast outer projection expr to declared field type in SingleDistinctToGroupBy - #26211

Open
jdymitarai wants to merge 1 commit into
apache:mainfrom
jdymitarai:fix-single-distinct-decimal-type-26193
Open

jdymitarai wants to merge 1 commit into
apache:mainfrom
jdymitarai:fix-single-distinct-decimal-type-26193

Conversation

@jdymitarai

Copy link
Copy Markdown

Closes #26193.

When SingleDistinctToGroupBy rewrites an aggregate containing both DISTINCT and non-distinct expressions such as sum over Decimal128, the non-distinct expression is split into inner and outer phases. The outer phase evaluates sum over the already-widened Decimal output of the inner aggregation, which widens precision a second time and causes the resulting schema to diverge from the original aggregate schema.

To maintain schema consistency, the outer projection now checks if the data type produced by outer_aggr differs from the field type declared in the original schema, wrapping the projected expression with a cast back to the declared field type when a mismatch occurs.

All optimizer unit tests including a dedicated regression test reproducing the two-phase decimal precision expansion pass cleanly.

@github-actions github-actions Bot added the optimizer Optimizer rules label Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Optimizer rules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sum of a decimal returns a wider type than the plan schema declares when the query has a DISTINCT aggregate

1 participant