This is an automated email from the ASF dual-hosted git repository.

chunwei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/master by this push:
     new 2088488  [CALCITE-4118] RexSimplify might remove CAST from RexNode 
incorrectly
2088488 is described below

commit 2088488ac8327b19512a76a122cae2961fc551c3
Author: Chunwei Lei <chunwei.l...@gmail.com>
AuthorDate: Mon Jul 27 21:58:34 2020 +0800

    [CALCITE-4118] RexSimplify might remove CAST from RexNode incorrectly
---
 .../main/java/org/apache/calcite/rex/RexSimplify.java   |  5 ++++-
 .../org/apache/calcite/rex/RexProgramBuilderBase.java   | 17 +++++++++++++++++
 .../java/org/apache/calcite/rex/RexProgramTest.java     |  4 ++++
 3 files changed, 25 insertions(+), 1 deletion(-)

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 35ded74..5f94eb1 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
@@ -27,6 +27,7 @@ import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.sql.SqlKind;
 import org.apache.calcite.sql.SqlOperator;
 import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.type.SqlTypeCoercionRule;
 import org.apache.calcite.sql.type.SqlTypeName;
 import org.apache.calcite.sql.type.SqlTypeUtil;
 import org.apache.calcite.util.Bug;
@@ -1917,7 +1918,9 @@ public class RexSimplify {
       if (RexUtil.isLosslessCast(intExpr.getType(), operand.getType())
           && (e.getType().getSqlTypeName() == 
operand.getType().getSqlTypeName()
           || e.getType().getSqlTypeName() == SqlTypeName.CHAR
-          || operand.getType().getSqlTypeName() != SqlTypeName.CHAR)) {
+          || operand.getType().getSqlTypeName() != SqlTypeName.CHAR)
+          && SqlTypeCoercionRule.instance()
+          .canApplyFrom(intExpr.getType().getSqlTypeName(), 
e.getType().getSqlTypeName())) {
         return rexBuilder.makeCast(e.getType(), intExpr);
       }
     }
diff --git 
a/core/src/test/java/org/apache/calcite/rex/RexProgramBuilderBase.java 
b/core/src/test/java/org/apache/calcite/rex/RexProgramBuilderBase.java
index 2179cc2..1148414 100644
--- a/core/src/test/java/org/apache/calcite/rex/RexProgramBuilderBase.java
+++ b/core/src/test/java/org/apache/calcite/rex/RexProgramBuilderBase.java
@@ -61,6 +61,7 @@ public abstract class RexProgramBuilderBase {
   protected RexLiteral nullSmallInt;
   protected RexLiteral nullVarchar;
   protected RexLiteral nullDecimal;
+  protected RexLiteral nullVarbinary;
 
   private RelDataType nullableBool;
   private RelDataType nonNullableBool;
@@ -77,6 +78,9 @@ public abstract class RexProgramBuilderBase {
   private RelDataType nullableDecimal;
   private RelDataType nonNullableDecimal;
 
+  private RelDataType nullableVarbinary;
+  private RelDataType nonNullableVarbinary;
+
   // Note: JUnit 4 creates new instance for each test method,
   // so we initialize these structures on demand
   // It maps non-nullable type to struct of (10 nullable, 10 non-nullable) 
fields
@@ -142,6 +146,10 @@ public abstract class RexProgramBuilderBase {
     nonNullableDecimal = typeFactory.createSqlType(SqlTypeName.DECIMAL);
     nullableDecimal = 
typeFactory.createTypeWithNullability(nonNullableDecimal, true);
     nullDecimal = rexBuilder.makeNullLiteral(nullableDecimal);
+
+    nonNullableVarbinary = typeFactory.createSqlType(SqlTypeName.VARBINARY);
+    nullableVarbinary = 
typeFactory.createTypeWithNullability(nonNullableVarbinary, true);
+    nullVarbinary = rexBuilder.makeNullLiteral(nullableVarbinary);
   }
 
   private RexDynamicParam getDynamicParam(RelDataType type, String 
fieldNamePrefix) {
@@ -430,6 +438,15 @@ public abstract class RexProgramBuilderBase {
     return type;
   }
 
+  protected RelDataType tVarbinary() {
+    return nonNullableVarbinary;
+  }
+
+  protected RelDataType tVarbinary(boolean nullable) {
+    return nullable ? nullableVarbinary : nonNullableVarbinary;
+  }
+
+
   protected RelDataType tArray(RelDataType elemType) {
     return typeFactory.createArrayType(elemType, -1);
   }
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 1fdd8d1..5307ac0 100644
--- a/core/src/test/java/org/apache/calcite/rex/RexProgramTest.java
+++ b/core/src/test/java/org/apache/calcite/rex/RexProgramTest.java
@@ -2737,4 +2737,8 @@ class RexProgramTest extends RexProgramTestBase {
     checkSimplifyUnchanged(rexBuilder.makeCall(opPolicyAny, vIntNotNull()));
     checkSimplify3(rexBuilder.makeCall(opPolicyAny, nullInt), "null:BOOLEAN", 
"false", "true");
   }
+
+  @Test void testSimplifyVarbinary() {
+    checkSimplifyUnchanged(cast(cast(vInt(), tVarchar(true, 100)), 
tVarbinary(true)));
+  }
 }

Reply via email to