paimon icon indicating copy to clipboard operation
paimon copied to clipboard

[core] Fix integer overflow in FieldSumAgg causing silent data corruption

Open dubin555 opened this issue 8 months ago • 0 comments

Purpose

FieldSumAgg performs unchecked arithmetic for TINYINT, SMALLINT, INTEGER, and BIGINT types. When the accumulator overflows, the result silently wraps around (e.g., Byte.MAX_VALUE + 1 becomes -128), producing incorrect aggregation results without any error or warning. This is silent data corruption in the merge-tree SUM aggregation path.

The root cause differs by type:

  • TINYINT/SMALLINT: Java promotes operands to int for arithmetic, then the (byte) or (short) cast silently truncates the overflow bits.
  • INTEGER/BIGINT: Standard + and - wrap around at MAX_VALUE / MIN_VALUE per Java spec.

This fix adds overflow detection to all three affected methods (agg, retract, negative):

  • For INTEGER and BIGINT: uses Math.addExact / Math.subtractExact / Math.negateExact, which throw ArithmeticException on overflow.
  • For TINYINT and SMALLINT: uses range-checked helper methods that perform arithmetic in int width and verify the result fits in byte / short range before casting.

This is consistent with the overflow-protection pattern already used in DecimalUtils.add() and DecimalUtils.subtract() in the same codebase.

Tests

  • testFieldSumByteOverflow — TINYINT: verifies normal addition works, positive overflow throws ArithmeticException, negative overflow throws, and retract overflow throws.
  • testFieldSumShortOverflow — SMALLINT: same coverage.
  • testFieldSumIntOverflow — INTEGER: same coverage.
  • testFieldSumLongOverflow — BIGINT: same coverage.

API and Format

No API or storage format changes.

Documentation

No new feature introduced.

Generative AI tooling

Generated-by: Claude Code 1.0.0

dubin555 avatar Mar 03 '26 11:03 dubin555