This is an automated email from the ASF dual-hosted git repository.
mbudiu 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 71131ab0ea [CALCITE-6762] Preserving the CAST conversion for operands
in Presto
71131ab0ea is described below
commit 71131ab0ead715b575a00462b6a80814990e7e5b
Author: ClownXC <[email protected]>
AuthorDate: Sun Jan 5 02:03:59 2025 +0800
[CALCITE-6762] Preserving the CAST conversion for operands in Presto
---
.../calcite/sql/dialect/PrestoSqlDialect.java | 16 ++++++++++++++++
.../calcite/rel/rel2sql/RelToSqlConverterTest.java | 21 +++++++++++++++++++++
2 files changed, 37 insertions(+)
diff --git
a/core/src/main/java/org/apache/calcite/sql/dialect/PrestoSqlDialect.java
b/core/src/main/java/org/apache/calcite/sql/dialect/PrestoSqlDialect.java
index e1fd1784e5..1987c889a9 100644
--- a/core/src/main/java/org/apache/calcite/sql/dialect/PrestoSqlDialect.java
+++ b/core/src/main/java/org/apache/calcite/sql/dialect/PrestoSqlDialect.java
@@ -20,6 +20,9 @@ import org.apache.calcite.avatica.util.Casing;
import org.apache.calcite.config.NullCollation;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexUtil;
import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlDialect;
@@ -33,6 +36,8 @@ import org.apache.calcite.sql.fun.SqlLibraryOperators;
import org.apache.calcite.sql.fun.SqlMapValueConstructor;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
import org.apache.calcite.util.RelToSqlConverterUtil;
import com.google.common.collect.ImmutableList;
@@ -84,6 +89,17 @@ public class PrestoSqlDialect extends SqlDialect {
unparseUsingLimit(writer, offset, fetch);
}
+ @Override public boolean supportsImplicitTypeCoercion(RexCall call) {
+ RexNode rexNode = call.getOperands().get(0);
+ return super.supportsImplicitTypeCoercion(call)
+ && RexUtil.isLiteral(rexNode, false)
+ && (rexNode.getType().getSqlTypeName() == SqlTypeName.VARCHAR
+ || rexNode.getType().getSqlTypeName() == SqlTypeName.CHAR)
+ && !SqlTypeUtil.isNumeric(call.type)
+ && !SqlTypeUtil.isDate(call.type)
+ && !SqlTypeUtil.isTimestamp(call.type);
+ }
+
/** Unparses offset/fetch using "OFFSET offset LIMIT fetch " syntax. */
private static void unparseUsingLimit(SqlWriter writer, @Nullable SqlNode
offset,
@Nullable SqlNode fetch) {
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 d757deec68..340dc323d3 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
@@ -7975,6 +7975,27 @@ class RelToSqlConverterTest {
sql(query).withClickHouse().ok(expectedSql);
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-6762">[CALCITE-6762]
+ * Preserving the CAST conversion for operands in Presto</a>. */
+ @Test void testImplicitTypeCoercion() {
+ final String query = "select \"employee_id\" "
+ + "from \"foodmart\".\"employee\" "
+ + "where 10 = cast('10' as int) and \"birth_date\" = cast('1914-02-02'
as date) or "
+ + "\"hire_date\" = cast('1996-01-01 '||'00:00:00' as timestamp)";
+ final String expected = "SELECT \"employee_id\"\n"
+ + "FROM \"foodmart\".\"employee\"\n"
+ + "WHERE 10 = '10' AND \"birth_date\" = '1914-02-02' OR \"hire_date\"
= '1996-01-01 ' || "
+ + "'00:00:00'";
+ final String expectedPresto = "SELECT \"employee_id\"\n"
+ + "FROM \"foodmart\".\"employee\"\n"
+ + "WHERE 10 = CAST('10' AS INTEGER) AND \"birth_date\" =
CAST('1914-02-02' AS DATE) OR "
+ + "\"hire_date\" = CAST('1996-01-01 ' || '00:00:00' AS TIMESTAMP)";
+ sql(query)
+ .ok(expected)
+ .withPresto().ok(expectedPresto);
+ }
+
@Test void testDialectQuoteStringLiteral() {
dialects().forEach((dialect, databaseProduct) -> {
assertThat(dialect.quoteStringLiteral(""), is("''"));