Skip to content

[CALCITE-7880] Unqualified common column of JOIN USING has the left column's type, so arithmetic on it can overflow - #5361

Merged
mihaibudiu merged 1 commit into
apache:mainfrom
xgbtxy:fix/calcite-7880-common-column-overflow
Oct 28, 2026
Merged

mihaibudiu merged 1 commit into
apache:mainfrom
xgbtxy:fix/calcite-7880-common-column-overflow

Conversation

@xgbtxy

@xgbtxy xgbtxy commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-7880

Changes Proposed

SELECT k + 1 AS x
FROM (VALUES (CAST(2147483647 AS INTEGER))) AS d(k)
JOIN (VALUES (CAST(2147483647 AS BIGINT))) AS f(k) USING (k)

When an INTEGER key joins a BIGINT key with USING, the unqualified common column can retain the selected input's narrower type. For example, adding 1 to the common key 2147483647 returns -2147483648, while SELECT * exposes the key as BIGINT.

Cast the selected common-column reference to the comparison common type, using the same type-selection rule as star expansion. INNER/LEFT still select the left input and RIGHT still selects the right input, retaining CALCITE-2659's removal of redundant COALESCE. FULL JOIN and explicitly qualified input references retain their existing handling. The change preserves the selected expression's existing nullability.

The regression tests execute INTEGER/BIGINT inputs in both orders across INNER, LEFT, RIGHT, FULL and NATURAL joins, assert JDBC result types and qualified-column controls, and check nullable/unmatched rows for LEFT/RIGHT joins. Validator coverage includes direct common-column references with identifier expansion disabled and arithmetic with expansion enabled.

Validation on Windows with JDK 17 for tests and the project-default JDK 25 compilation toolchain:

  • gradlew.bat --no-daemon --no-parallel --max-workers=4 build passed, including formatting, Checkstyle, bytecode/API checks and the default test suites. JUnit XML reports 22,263 passed, zero failures/errors and 751 skipped entries; extra external-integration and slow-test profiles were not enabled.

  • The focused run passed 24 tests (19 new cases plus 5 existing tests). On unmodified main, the new 19-case matrix had 9 failures and 10 passing controls.

  • Added a readable Quidem regression with the exact issue SQL and a qualified BIGINT control. Against the original validator, only the unqualified query failed; with this revision, all 47 core Quidem files pass.

AI assistance: implementation, test development and local validation used OpenAI Codex.

Copilot AI balanced review requested due to automatic review settings October 25, 2026 16:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

/** Regression test for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7880">[CALCITE-7880]
* Arithmetic on an unqualified USING column can overflow its common type</a>. */
@ParameterizedTest

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are hard to read for a human, but let's keep them since they are parameterized.
But can you add a comment explaining how you have validated that their results are correct?

Can you also add at least one quidem test containing the example of the issue?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 9650c0bdb. The Java comments now derive the expected rows from the matched/unmatched keys and explain the type controls. sql/join.iq contains the exact issue query with result and type assertions, preceded by a qualified f.k + 1 BIGINT arithmetic control.

Against the original validator, the qualified control passed and only the unqualified query differed: INTEGER/-2147483648 instead of BIGINT/2147483648. With the fix, all 47 core Quidem files pass. The complete default gradlew build also passed: 22,263 passing test records, zero failures/errors, with existing skips retained.

The two upstream workflows currently require repository approval. Could you approve this revision for CI?

.ok();
}

/** Tests that a common column has the same widened type with and without

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you need to use the same standard format for the javadoc describing the issue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the standard CALCITE-7880 issue Javadoc to this validator regression in 9650c0bdb.


@ParameterizedTest
@MethodSource("joinUsingMixedNumericNulls")
void testJoinUsingMixedNumericCommonColumnWithNulls(String join, String leftType,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one needs the same comment pointing to the JIRA issue

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the standard CALCITE-7880 issue Javadoc here in 9650c0bdb, along with the derivation of the LEFT/RIGHT expected rows.

@mihaibudiu

Copy link
Copy Markdown
Contributor

please squash the commits for merging

@mihaibudiu mihaibudiu added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Oct 26, 2026
…olumn's type, so arithmetic on it can overflow

Cast the selected common-column reference to the comparison common type
before using it in an expression. Keep the chosen-side optimization for
INNER, LEFT and RIGHT joins, and preserve FULL JOIN handling.

Add JDBC and validator coverage, the exact issue query and its qualified
BIGINT control in Quidem, and comments deriving the expected rows and types.

Generated-by: OpenAI Codex
@xgbtxy
xgbtxy force-pushed the fix/calcite-7880-common-column-overflow branch from 9650c0b to 529cc0d Compare October 28, 2026 03:55
@xgbtxy

xgbtxy commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Squashed into 529cc0d. The code tree is identical to the reviewed 9650c0b revision.

@xgbtxy
xgbtxy force-pushed the fix/calcite-7880-common-column-overflow branch from 529cc0d to 580d7af Compare October 28, 2026 05:40
@xgbtxy

xgbtxy commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Fixed the commit-message line endings in 580d7af. LintTest.testLintLog rejected the CRLF ending in my squashed commit message. This update only normalizes that message to LF; the code tree is unchanged.

@sonarqubecloud

Copy link
Copy Markdown

@mihaibudiu
mihaibudiu merged commit 68f1613 into apache:main Oct 28, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LGTM-will-merge-soon Overall PR looks OK. Only minor things left.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants