Skip to content

fix(core)!: correct SUM and SUM0 helper results - #1262

Draft
bvolpato wants to merge 1 commit into
substrait-io:mainfrom
bvolpato:bvolpato/fix-sum-helper-types
Draft

fix(core)!: correct SUM and SUM0 helper results#1262
bvolpato wants to merge 1 commit into
substrait-io:mainfrom
bvolpato:bvolpato/fix-sum-helper-types

Conversation

@bvolpato

@bvolpato bvolpato commented Sep 3, 2026

Copy link
Copy Markdown
Member

The field overload of sum0 currently emits sum, so an empty input returns null rather than zero. SUM also retains narrow input types instead of using the declared wider result, and floating-point SUM0 incorrectly declares i64.

For an i32 input, these helpers must emit nullable i64 for SUM and the sum0 function with required i64 for SUM0:

SubstraitBuilder builder = new SubstraitBuilder();
NamedScan scan = builder.namedScan(
    List.of("t"), List.of("v"), List.of(TypeCreator.REQUIRED.I32));
builder.sum(scan, 0).getFunction().outputType();
builder.sum0(scan, 0).getFunction().declaration().name();

Delegate the field overload to sum0 and derive the shared arithmetic helper's output type from its function declaration. This follows spec v0.102.0: integer sums use i64, floating-point sums use fp64, SUM is nullable, and SUM0 is required. Standard MIN/MAX/AVG results retain their existing types.

Downsides

The helpers now follow the supplied extension catalog's return declarations, including custom catalog overrides, rather than imposing their own result types.

BREAKING CHANGE: SUM over i8/i16/i32 or fp32 now reports the spec's wider result type, and floating-point SUM0 reports fp64 instead of i64. Callers that assume the old aggregate output schema must use the returned type. sum0(Rel, int) now emits sum0 rather than sum.

Select sum0 in both overloads and derive arithmetic aggregate output
types from the configured function declarations.

BREAKING CHANGE: SUM widens narrow integer and floating-point results
according to the spec. Floating-point SUM0 returns fp64, and sum0(Rel,int)
now emits sum0. Custom return declarations are honored by these helpers.
@bvolpato
bvolpato force-pushed the bvolpato/fix-sum-helper-types branch from 2026afd to 25a08dc Compare September 4, 2026 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant