Skip to content

Commit

Permalink
[CALCITE-6147] CAST(CAST(EMPNO AS VARCHAR) AS INT) should be simplifi…
Browse files Browse the repository at this point in the history
…ed to EMPNO

Close apache#3555
  • Loading branch information
libenchao committed Dec 8, 2023
1 parent 1f6022b commit cdf3f5c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
5 changes: 4 additions & 1 deletion core/src/main/java/org/apache/calcite/rex/RexUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@

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;

/**
Expand Down Expand Up @@ -1697,7 +1699,8 @@ public static boolean isLosslessCast(RelDataType source, RelDataType target) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2130,7 +2130,7 @@ private SqlDialect nonOrdinalDialect() {
@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);
}
Expand All @@ -2151,17 +2151,17 @@ private SqlDialect nonOrdinalDialect() {
+ "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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down

0 comments on commit cdf3f5c

Please sign in to comment.