From a083d09d4f9c66d4dcc0306a7cc2ed9baab5bdfd Mon Sep 17 00:00:00 2001
From: ClownXC <598457447@qq.com>
Date: Fri, 24 Jan 2025 16:00:11 +0800
Subject: [PATCH] [CALCITE-6796] Convert Type from BINARY to VARBINARY in
PrestoDialect
---
.../calcite/sql/dialect/PrestoSqlDialect.java | 4 ++++
.../calcite/rel/rel2sql/RelToSqlConverterTest.java | 13 +++++++++++++
2 files changed, 17 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 6dfeec8029ff..f739e7e891e5 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
@@ -153,6 +153,10 @@ private static void unparseUsingLimit(SqlWriter writer, @Nullable SqlNode offset
case FLOAT:
return new SqlDataTypeSpec(
new SqlBasicTypeNameSpec(SqlTypeName.DOUBLE, SqlParserPos.ZERO), SqlParserPos.ZERO);
+ // https://prestodb.io/docs/current/language/types.html#varbinary
+ case BINARY:
+ return new SqlDataTypeSpec(
+ new SqlBasicTypeNameSpec(SqlTypeName.VARBINARY, SqlParserPos.ZERO), SqlParserPos.ZERO);
default:
return super.getCastSpec(type);
}
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 2ce249b4d476..cdc28ec24c7d 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
@@ -9146,6 +9146,17 @@ private void checkLiteral2(String expression, String expected) {
sql(query).withOracle().ok(oracle);
}
+ /** Test case for
+ * [CALCITE-6796]
+ * Convert Type from BINARY to VARBINARY in PrestoDialect. */
+ @Test void testPrestoBinaryCast() {
+ String query = "SELECT cast(cast(\"employee_id\" as varchar) as binary)"
+ + "from \"foodmart\".\"reserve_employee\" ";
+ String expected = "SELECT CAST(CAST(\"employee_id\" AS VARCHAR) AS VARBINARY)"
+ + "\nFROM \"foodmart\".\"reserve_employee\"";
+ sql(query).withPresto().ok(expected);
+ }
+
/** Test case for
* [CALCITE-6771]
* Convert Type from FLOAT to DOUBLE in PrestoDialect. */
@@ -9160,6 +9171,8 @@ private void checkLiteral2(String expression, String expected) {
.withPresto().ok(expected);
}
+
+
/** Fluid interface to run tests. */
static class Sql {
private final CalciteAssert.SchemaSpec schemaSpec;