Skip to content

[CALCITE-7927] Preserve nested casts around dynamic parameters - #5402

Open
atharvrastogi1411 wants to merge 1 commit into
apache:mainfrom
atharvrastogi1411:codex/calcite-7927-nested-dynamic-param-casts
Open

atharvrastogi1411 wants to merge 1 commit into
apache:mainfrom
atharvrastogi1411:codex/calcite-7927-nested-dynamic-param-casts

Conversation

@atharvrastogi1411

Copy link
Copy Markdown

Summary

Fixes CALCITE-7927.

Extend the dynamic-parameter protection introduced by CALCITE-6346 to both nested-cast elimination paths in RexSimplify.simplifyCast: removing both casts and removing only the inner cast. Neither shortcut should discard a cast based solely on a dynamic parameter's declared type.

For example, the JDBC adapter currently simplifies:

CAST(CAST(? AS VARCHAR(1)) AS CHAR(1))

to:

CAST(? AS CHAR(1))

With "longer-value" bound using setString, the original expression returns "l" in HSQLDB, but the simplified JDBC query returns "longer-value". Preserving the nested casts restores the correct result. CALCITE-7118's Java executor parameter conversion does not protect JDBC pushdown.

Tests

  • Add a Rex simplification regression covering both nested-cast elimination paths.
  • Add an HSQLDB JDBC adapter regression checking the actual truncated result and the pushed-down SQL.
  • Confirm both new tests fail before the production fix.
  • Update the existing testDynamicParameterDoubleCast golden to retain its inner INTEGER cast.

Validation passed: 1,050 tests discovered, 0 failures, 4 skipped; both core Checkstyle tasks passed.

./gradlew --no-daemon :core:test \
  --tests org.apache.calcite.rex.RexProgramTest \
  --tests org.apache.calcite.rex.RexLosslessCastTest \
  --tests org.apache.calcite.rex.RexExecutorTest \
  --tests org.apache.calcite.test.JdbcAdapterTest \
  --tests org.apache.calcite.test.SqlToRelConverterTest \
  :core:checkstyleMain :core:checkstyleTest

The full multi-module build was not run.

@mihaibudiu

Copy link
Copy Markdown
Contributor

What is the semantics of CAST(x as CHAR(1)) in SQL?

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.

3 participants