Skip to content

Commit

Permalink
[CALCITE-6771] Convert Type from FLOAT to DOUBLE in PrestoDialect
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaochen-zhou committed Jan 9, 2025
1 parent 71131ab commit fba35ad
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.rex.RexUtil;
import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlBasicTypeNameSpec;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlDataTypeSpec;
import org.apache.calcite.sql.SqlDialect;
import org.apache.calcite.sql.SqlIntervalQualifier;
import org.apache.calcite.sql.SqlKind;
Expand Down Expand Up @@ -146,7 +148,14 @@ private static void unparseUsingLimit(SqlWriter writer, @Nullable SqlNode offset
}

@Override public @Nullable SqlNode getCastSpec(RelDataType type) {
return super.getCastSpec(type);
switch (type.getSqlTypeName()) {
// PRESTO only supports REAL、DOUBLE for floating point types.
case FLOAT:
return new SqlDataTypeSpec(
new SqlBasicTypeNameSpec(SqlTypeName.DOUBLE, SqlParserPos.ZERO), SqlParserPos.ZERO);
default:
return super.getCastSpec(type);
}
}

@Override public void unparseCall(SqlWriter writer, SqlCall call,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9093,6 +9093,20 @@ private void checkLiteral2(String expression, String expected) {
sql(query).withOracle().ok(oracle);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6771">[CALCITE-6771]
* Convert Type from FLOAT to DOUBLE in PrestoDialect </a>. */
@Test void testPrestoFloatingPointTypesCast() {
String query = "SELECT CAST(\"department_id\" AS float), "
+ "CAST(\"department_id\" AS double), "
+ "CAST(\"department_id\" AS real) FROM \"employee\"";
String expected = "SELECT CAST(\"department_id\" AS DOUBLE), "
+ "CAST(\"department_id\" AS DOUBLE), "
+ "CAST(\"department_id\" AS REAL)\nFROM \"foodmart\".\"employee\"";
sql(query)
.withPresto().ok(expected);
}

/** Fluid interface to run tests. */
static class Sql {
private final CalciteAssert.SchemaSpec schemaSpec;
Expand Down

0 comments on commit fba35ad

Please sign in to comment.