Atharv Rastogi created CALCITE-7927:
---------------------------------------

             Summary: RexSimplify removes required casts around nested dynamic 
parameters
                 Key: CALCITE-7927
                 URL: https://issues.apache.org/jira/browse/CALCITE-7927
             Project: Calcite
          Issue Type: Bug
            Reporter: Atharv Rastogi


RexSimplify protects a direct CAST around a dynamic parameter from elimination, 
following CALCITE-6346. However, that protection does not cover the two 
nested-cast simplification branches: removing both casts, or removing only the 
inner cast.

These branches use the parameter’s declared type and precision to determine 
whether a cast is lossless. Removing a cast can change results when the 
simplified expression is pushed down through the JDBC adapter.

Reproduction:

 
{code:java}
SELECT CAST(CAST(? AS VARCHAR(1)) AS CHAR(1))
FROM T{code}
 

T contains one row. Bind the parameter using setString(1, "longer-value").

With HSQLDB:
 * Direct execution of the original SQL returns "l".
 * Execution through Calcite’s JDBC adapter returns "longer-value".

Calcite simplifies the expression to:

 
{code:java}
CAST(? AS CHAR(1)){code}
 

Preserving the original nested casts restores the result to "l".

The related rewrite below also occurs when the parameter has a declared CHAR(1) 
or VARCHAR(1) type:

 
{code:java}
CAST(CAST(? AS VARCHAR(1)) AS VARCHAR)
→ CAST(? AS VARCHAR){code}
 

CALCITE-7118 makes Calcite’s Java executor enforce parameter 
padding/truncation, but JDBC pushdown does not use that execution path. 
Therefore, that fix does not prevent this JDBC failure.

Suggested fix:

Extend the existing dynamic-parameter protection to both nested-cast 
elimination branches in RexSimplify.simplifyCast. Do not remove these casts 
based solely on a RexDynamicParam’s declared type.

Validation:

Reproduced against current upstream main at f8963be2. Applying this protection 
in an isolated probe preserves the nested casts and makes the JDBC result match 
direct database execution. Add focused simplifier tests for both branches and a 
JDBC result-comparison regression test.

Related issues:
CALCITE-6346 — JdbcAdapter: Cast for dynamic filter arguments is lost
CALCITE-7118 — Rex-to-Lix Translation fails to correctly truncate/pad 
RexDynamicParam values



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to