Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions core/src/main/java/org/apache/calcite/rex/RexSimplify.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}
Expand Down
15 changes: 15 additions & 0 deletions core/src/test/java/org/apache/calcite/rex/RexProgramTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2938,6 +2939,20 @@ private void checkExponentialCnf(int n) {
checkSimplify(or(vBool(), vBool()), "?0.bool0");
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7927">[CALCITE-7927]
* RexSimplify removes required casts around nested dynamic parameters</a>. */
@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
* <a href="https://issues.apache.org/jira/browse/CALCITE-1289">[CALCITE-1289]
* RexUtil.simplifyCase() should account for nullability</a>. */
Expand Down
15 changes: 15 additions & 0 deletions core/src/test/java/org/apache/calcite/test/JdbcAdapterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,21 @@ class JdbcAdapterTest {
.runs();
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7927">[CALCITE-7927]
* RexSimplify removes required casts around nested dynamic parameters</a>. */
@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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2362,7 +2362,7 @@ LogicalProject(FAKE2=[ITEM($0, 'fake_col2')])
</Resource>
<Resource name="plan">
<![CDATA[
LogicalProject(EXPR$0=[CAST(?0):CHAR(1)])
LogicalProject(EXPR$0=[CAST(CAST(?0):INTEGER):CHAR(1)])
LogicalValues(tuples=[[{ 0 }]])
]]>
</Resource>
Expand Down