You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by li...@apache.org on 2023/12/08 05:08:21 UTC
(calcite) branch main updated: [CALCITE-6147] CAST(CAST(EMPNO AS VARCHAR) AS INT) should be simplified to EMPNO
This is an automated email from the ASF dual-hosted git repository.
libenchao 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 cdf3f5cb6b [CALCITE-6147] CAST(CAST(EMPNO AS VARCHAR) AS INT) should be simplified to EMPNO
cdf3f5cb6b is described below
commit cdf3f5cb6b31ae135c0f72d181761a0d82866903
Author: Benchao Li <li...@gmail.com>
AuthorDate: Fri Dec 1 13:42:21 2023 +0800
[CALCITE-6147] CAST(CAST(EMPNO AS VARCHAR) AS INT) should be simplified to EMPNO
Close apache/calcite#3555
---
.../main/java/org/apache/calcite/rex/RexUtil.java | 5 ++++-
.../calcite/rel/rel2sql/RelToSqlConverterTest.java | 20 ++++++++++----------
.../org/apache/calcite/rex/RexLosslessCastTest.java | 6 ++++++
3 files changed, 20 insertions(+), 11 deletions(-)
diff --git a/core/src/main/java/org/apache/calcite/rex/RexUtil.java b/core/src/main/java/org/apache/calcite/rex/RexUtil.java
index 6077329498..0ae2fc31fe 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexUtil.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexUtil.java
@@ -72,6 +72,8 @@ import java.util.function.Predicate;
import static com.google.common.collect.ImmutableList.toImmutableList;
+import static org.apache.calcite.rel.type.RelDataType.PRECISION_NOT_SPECIFIED;
+
import static java.util.Objects.requireNonNull;
/**
@@ -1697,7 +1699,8 @@ public class RexUtil {
if (source.getScale() != -1 && source.getScale() != 0) {
sourceLength += source.getScale() + 1; // include decimal mark
}
- return target.getPrecision() >= sourceLength;
+ final int targetPrecision = target.getPrecision();
+ return targetPrecision == PRECISION_NOT_SPECIFIED || targetPrecision >= sourceLength;
}
// Return FALSE by default
return false;
diff --git a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
index 8f10a1386a..709e83e698 100644
--- a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
+++ b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
@@ -2130,7 +2130,7 @@ class RelToSqlConverterTest {
@Test void testHiveCastAsInt() {
String query = "select cast( cast(\"employee_id\" as varchar) as int) "
+ "from \"foodmart\".\"reserve_employee\" ";
- final String expected = "SELECT CAST(CAST(employee_id AS VARCHAR) AS INT)\n"
+ final String expected = "SELECT employee_id\n"
+ "FROM foodmart.reserve_employee";
sql(query).withHive().ok(expected);
}
@@ -2151,17 +2151,17 @@ class RelToSqlConverterTest {
+ "cast(cast(\"employee_id\" as varchar) as time), "
+ "cast(cast(\"employee_id\" as varchar) as boolean) "
+ "from \"foodmart\".\"reserve_employee\" ";
- final String expected = "SELECT CAST(CAST(employee_id AS STRING) AS INT64), "
- + "CAST(CAST(employee_id AS STRING) AS INT64), "
- + "CAST(CAST(employee_id AS STRING) AS INT64), "
- + "CAST(CAST(employee_id AS STRING) AS INT64), "
- + "CAST(CAST(employee_id AS STRING) AS FLOAT64), "
- + "CAST(CAST(employee_id AS STRING) AS STRING), "
+ final String expected = "SELECT CAST(employee_id AS INT64), "
+ + "CAST(employee_id AS INT64), "
+ + "CAST(employee_id AS INT64), "
+ + "employee_id, "
+ + "CAST(employee_id AS FLOAT64), "
+ + "CAST(employee_id AS STRING), "
+ "CAST(CAST(employee_id AS STRING) AS BYTES), "
+ "CAST(CAST(employee_id AS STRING) AS BYTES), "
- + "CAST(CAST(employee_id AS STRING) AS TIMESTAMP), "
- + "CAST(CAST(employee_id AS STRING) AS FLOAT64), "
- + "CAST(CAST(employee_id AS STRING) AS NUMERIC), "
+ + "CAST(employee_id AS TIMESTAMP), "
+ + "CAST(employee_id AS FLOAT64), "
+ + "CAST(employee_id AS NUMERIC), "
+ "CAST(CAST(employee_id AS STRING) AS DATE), "
+ "CAST(CAST(employee_id AS STRING) AS TIME), "
+ "CAST(CAST(employee_id AS STRING) AS BOOL)\n"
diff --git a/core/src/test/java/org/apache/calcite/rex/RexLosslessCastTest.java b/core/src/test/java/org/apache/calcite/rex/RexLosslessCastTest.java
index 7124ac1938..334b3d1ed4 100644
--- a/core/src/test/java/org/apache/calcite/rex/RexLosslessCastTest.java
+++ b/core/src/test/java/org/apache/calcite/rex/RexLosslessCastTest.java
@@ -40,6 +40,7 @@ class RexLosslessCastTest extends RexProgramTestBase {
final RelDataType charType6 = typeFactory.createSqlType(SqlTypeName.CHAR, 6);
final RelDataType varCharType10 = typeFactory.createSqlType(SqlTypeName.VARCHAR, 10);
final RelDataType varCharType11 = typeFactory.createSqlType(SqlTypeName.VARCHAR, 11);
+ final RelDataType varcharType = typeFactory.createSqlType(SqlTypeName.VARCHAR);
// Negative
assertThat(RexUtil.isLosslessCast(rexBuilder.makeInputRef(intType, 0)), is(false));
@@ -125,6 +126,10 @@ class RexLosslessCastTest extends RexProgramTestBase {
RexUtil.isLosslessCast(
rexBuilder.makeCast(
varCharType11, rexBuilder.makeInputRef(varCharType10, 0))), is(true));
+ assertThat(
+ RexUtil.isLosslessCast(
+ rexBuilder.makeCast(
+ varcharType, rexBuilder.makeInputRef(intType, 0))), is(true));
}
@Test void removeRedundantCast() {
@@ -151,6 +156,7 @@ class RexLosslessCastTest extends RexProgramTestBase {
checkSimplify(
cast(cast(cast(core, tInt()), tBigInt()), tInt()),
"?0.notNullInt0");
+ checkSimplify(cast(cast(vInt(), tVarchar()), tInt()), "CAST(?0.int0):INTEGER NOT NULL");
}
@Test void removeLosslesssCastChar() {