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

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


The following commit(s) were added to refs/heads/main by this push:
     new d21c5c9c17 [CALCITE-6453] Simplify casts which are result of constant 
reduction
d21c5c9c17 is described below

commit d21c5c9c17d6733468800f652ab15932e84a012e
Author: kkasa <kk...@cloudera.com>
AuthorDate: Mon Jul 1 17:10:47 2024 +0200

    [CALCITE-6453] Simplify casts which are result of constant reduction
---
 core/src/main/java/org/apache/calcite/rex/RexSimplify.java    | 10 ++++++++--
 core/src/test/java/org/apache/calcite/rex/RexProgramTest.java |  8 ++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)

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 1f6733ba2b..1ef3bdc91d 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
@@ -2275,8 +2275,14 @@ public class RexSimplify {
           (RexLiteral) e.getOperands().get(1))
           : rexBuilder.makeCast(e.getType(), operand, safe, safe);
       executor.reduce(rexBuilder, ImmutableList.of(simplifiedExpr), 
reducedValues);
-      return requireNonNull(
-          Iterables.getOnlyElement(reducedValues));
+      RexNode reducedRexNode = 
requireNonNull(Iterables.getOnlyElement(reducedValues));
+      if (reducedRexNode.isA(SqlKind.CAST)) {
+        RexNode reducedOperand = ((RexCall) 
reducedRexNode).getOperands().get(0);
+        if (sameTypeOrNarrowsNullability(reducedRexNode.getType(), 
reducedOperand.getType())) {
+          return reducedOperand;
+        }
+      }
+      return reducedRexNode;
     default:
       if (operand == e.getOperands().get(0)) {
         return e;
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 ba7e4f18bf..c23f3cb164 100644
--- a/core/src/test/java/org/apache/calcite/rex/RexProgramTest.java
+++ b/core/src/test/java/org/apache/calcite/rex/RexProgramTest.java
@@ -3837,4 +3837,12 @@ class RexProgramTest extends RexProgramTestBase {
     checkSimplify(add(zero, sub(nullInt, nullInt)), "null:INTEGER");
   }
 
+  @Test void testSimplifyCastWithConstantReduction() {
+    RexNode dateStr = literal("2020-10-30");
+    RelDataType nullableDateType =
+        
typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.DATE),
 true);
+    RexNode cast = rexBuilder.makeCast(nullableDateType, dateStr);
+    checkSimplify(cast, "2020-10-30");
+  }
+
 }

Reply via email to