diff --git a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
index c8e62cb7453..d28dc715309 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
@@ -2704,7 +2704,10 @@ && sameTypeOrNarrowsNullability(e.getType(), operand.getType())
// would not have the padding
RexNode intExpr = ((RexCall) operand).operands.get(0);
- if ((intExpr.getType().getSqlTypeName() == SqlTypeName.CHAR
+ // As for a direct cast, a dynamic parameter's declared type does not
+ // justify removing casts that may be needed by the runtime or adapter.
+ if (intExpr.getKind() != SqlKind.DYNAMIC_PARAM
+ && (intExpr.getType().getSqlTypeName() == SqlTypeName.CHAR
|| operand.getType().getSqlTypeName() != SqlTypeName.CHAR)
&& sameTypeOrNarrowsNullability(e.getType(), intExpr.getType())) {
// Remove both casts (A-like cases): the outer type is the source type,
@@ -2713,7 +2716,8 @@ && sameTypeOrNarrowsNullability(e.getType(), intExpr.getType())) {
// parses the padded text back
return intExpr;
}
- if (RexUtil.isLosslessCast(intExpr.getType(), operand.getType())
+ if (intExpr.getKind() != SqlKind.DYNAMIC_PARAM
+ && RexUtil.isLosslessCast(intExpr.getType(), operand.getType())
&& canRemoveInnerCast(intExpr.getType(), operand.getType(), e.getType())) {
return rexBuilder.makeCast(e.getParserPosition(), e.getType(), intExpr);
}
diff --git a/core/src/test/java/org/apache/calcite/rex/RexProgramTest.java b/core/src/test/java/org/apache/calcite/rex/RexProgramTest.java
index fe1fd1c241e..3599b9de78d 100644
--- a/core/src/test/java/org/apache/calcite/rex/RexProgramTest.java
+++ b/core/src/test/java/org/apache/calcite/rex/RexProgramTest.java
@@ -82,6 +82,7 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.hasToString;
+import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
@@ -2938,6 +2939,20 @@ private void checkExponentialCnf(int n) {
checkSimplify(or(vBool(), vBool()), "?0.bool0");
}
+ /** Test case for
+ * [CALCITE-7927]
+ * RexSimplify removes required casts around nested dynamic parameters. */
+ @Test void testSimplifyNestedDynamicParamCasts() {
+ final RelDataType charType = typeFactory.createSqlType(SqlTypeName.CHAR, 1);
+ final RelDataType varcharType = typeFactory.createSqlType(SqlTypeName.VARCHAR, 1);
+ final RexNode parameter = rexBuilder.makeDynamicParam(charType, 0);
+ final RexNode innerCast = rexBuilder.makeAbstractCast(varcharType, parameter, false);
+ assertAll(
+ () -> checkSimplifyUnchanged(rexBuilder.makeAbstractCast(charType, innerCast, false)),
+ () -> checkSimplifyUnchanged(rexBuilder.makeAbstractCast(
+ typeFactory.createSqlType(SqlTypeName.VARCHAR), innerCast, false)));
+ }
+
/** Unit test for
* [CALCITE-1289]
* RexUtil.simplifyCase() should account for nullability. */
diff --git a/core/src/test/java/org/apache/calcite/test/JdbcAdapterTest.java b/core/src/test/java/org/apache/calcite/test/JdbcAdapterTest.java
index c8e540dc326..5c3ba9c2aff 100644
--- a/core/src/test/java/org/apache/calcite/test/JdbcAdapterTest.java
+++ b/core/src/test/java/org/apache/calcite/test/JdbcAdapterTest.java
@@ -1061,6 +1061,21 @@ class JdbcAdapterTest {
.runs();
}
+ /** Test case for
+ * [CALCITE-7927]
+ * RexSimplify removes required casts around nested dynamic parameters. */
+ @Test void testNestedDynamicParamCastTruncates() {
+ CalciteAssert.model(JdbcTest.SCOTT_MODEL)
+ .enable(CalciteAssert.DB == DatabaseInstance.HSQLDB)
+ .query("select cast(cast(? as varchar(1)) as char(1)) as c\n"
+ + "from scott.dept where deptno = 10")
+ .consumesPreparedStatement(p -> p.setString(1, "longer-value"))
+ .returns("C=l\n")
+ .planHasSql("SELECT CAST(CAST(? AS VARCHAR(1)) AS CHAR(1)) AS \"C\"\n"
+ + "FROM \"SCOTT\".\"DEPT\"\n"
+ + "WHERE CAST(\"DEPTNO\" AS INTEGER) = 10");
+ }
+
@Test void testLastValueOver() {
CalciteAssert
.model(FoodmartSchema.FOODMART_MODEL)
diff --git a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
index 39d005983b4..8e4f5f002c8 100644
--- a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
@@ -2362,7 +2362,7 @@ LogicalProject(FAKE2=[ITEM($0, 'fake_col2')])