Skip to content

[BUG] Silent integer and long overflow wrapping in Calcite arithmetic path #5164

Closed

Description

@penghuo

Query Information

PPL Command/Query:

source=bounty-types | where int_field = 2147483647 | eval overflow = int_field + 1 | fields int_field, overflow
source=bounty-types | where long_field = 9223372036854775807 | eval overflow = long_field + 1 | fields long_field, overflow

Expected Result:
Overflow should not silently wrap. Engine should either return validated error or safe overflow handling.

Actual Result:

  • 2147483647 + 1 returns -2147483648
  • 9223372036854775807 + 1 returns -9223372036854775808
    Both return HTTP 200 with wrapped values.

Dataset Information

Dataset/Schema Type

  • OpenTelemetry (OTEL)
  • Simple Schema for Observability (SS4O)
  • Open Cybersecurity Schema Framework (OCSF)
  • Custom (details below)

Index Mapping

{
  "mappings": {
    "properties": {
      "int_field": { "type": "integer" },
      "long_field": { "type": "long" }
    }
  }
}

Sample Data

{
  "int_field": 2147483647,
  "long_field": 9223372036854775807
}

Bug Description

Issue Summary:
Integer and long arithmetic in eval wraps on overflow without warning.

Steps to Reproduce:

  1. Create index with int_field and long_field.
  2. Insert a max-value record.
  3. Run both queries above.
  4. Observe wrapped negative results with HTTP 200.

Impact:
Silent numeric corruption in analytical results.

Activity

  1. added
    bug
    PPL
    on Feb 24, 2026
  2. added 2 commits that reference this issue on Mar 5, 2026
    722075c
    59578cf
  3. penghuo commented on Mar 5, 2026

    @penghuo
    CollaboratorAuthor

    Investigation Summary

    Root cause: This is a Calcite engine behavior, not specific to the PPL/SQL plugin. Standard Calcite arithmetic operators (SqlStdOperatorTable.PLUS/MINUS/MULTIPLY) generate plain Java +/-/* in the Enumerable code path, which silently wrap on integer/long overflow. Confirmed by reproducing the same wrapping behavior in Calcite's own CsvTest — SELECT iv, iv+1 FROM STATUS with iv = 2147483647 returns -2147483648.

    The V2 (legacy) engine does not have this issue — it already uses Math.addExact/subtractExact/multiplyExact.

    Prototype Fix

    A draft PR is available at #5204 demonstrating a Calcite-native fix using ConvertToChecked — Calcite's built-in visitor that rewrites PLUS → CHECKED_PLUS, MINUS → CHECKED_MINUS, TIMES → CHECKED_MULTIPLY. These checked operators generate SqlFunctions.checkedPlus(int, int) → Math.addExact() calls that throw ArithmeticException on overflow. The fix is controlled by a plugins.calcite.checked_arithmetic.enabled setting (default: true).

    This aligns with how most SQL databases handle overflow (PostgreSQL, SQL Server, Oracle, BigQuery all error on integer overflow).

    Why We Are Holding Off

    1. Not a critical blocker. The overflow wrapping only manifests when computing boundary-value integer/long arithmetic in eval expressions. This is an edge case that does not block typical analytical workloads.

    2. Auto-resolved by PPL-to-SQL translation. The long-term architecture direction is for PPL to translate to SQL, which then goes through Calcite's standard SQL pipeline. Once that path is in place, Calcite's SqlConformance.checkedArithmetic() can be enabled at the conformance level, and ConvertToChecked is applied automatically during Prepare.prepareSql() — no plugin-level changes needed.

    The draft PR remains available if the fix is needed before the PPL-to-SQL translation is complete.

  4. added
    triage/parked
    on Apr 27, 2026
  5. added 7 commits that reference this issue on Jul 1, 2026
    2d2ef9f
    fc2e0f2
    447b958
    c35a804
    da07e56
    8a628ff
    1102cb2
  6. moved this from Not Started to Done in PPL Trackeron Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    PPLbuggood-for-agenttriage/parked

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions