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 d3d5f10dba [CALCITE-6941] Array/Map value constructor is unparsed 
incorrectly in ClickHouse
d3d5f10dba is described below

commit d3d5f10dbaf8078970c5a0ee51271108e449649a
Author: Yu Xu <[email protected]>
AuthorDate: Mon Apr 7 10:22:14 2025 +0800

    [CALCITE-6941] Array/Map value constructor is unparsed incorrectly in 
ClickHouse
---
 .../calcite/sql/dialect/ClickHouseSqlDialect.java   | 18 ++++++++++++++++++
 .../calcite/rel/rel2sql/RelToSqlConverterTest.java  | 21 ++++++++++++++++-----
 2 files changed, 34 insertions(+), 5 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/sql/dialect/ClickHouseSqlDialect.java 
b/core/src/main/java/org/apache/calcite/sql/dialect/ClickHouseSqlDialect.java
index 6b64725971..dcba521a57 100644
--- 
a/core/src/main/java/org/apache/calcite/sql/dialect/ClickHouseSqlDialect.java
+++ 
b/core/src/main/java/org/apache/calcite/sql/dialect/ClickHouseSqlDialect.java
@@ -216,6 +216,24 @@ private static SqlDataTypeSpec 
createSqlDataTypeSpecByName(String typeAlias,
     }
 
     switch (call.getKind()) {
+    case MAP_VALUE_CONSTRUCTOR:
+      writer.print(call.getOperator().getName().toLowerCase(Locale.ROOT));
+      final SqlWriter.Frame mapFrame = writer.startList("(", ")");
+      for (int i = 0; i < call.operandCount(); i++) {
+        writer.sep(",");
+        call.operand(i).unparse(writer, leftPrec, rightPrec);
+      }
+      writer.endList(mapFrame);
+      break;
+    case ARRAY_VALUE_CONSTRUCTOR:
+      writer.print(call.getOperator().getName().toLowerCase(Locale.ROOT));
+      final SqlWriter.Frame arrayFrame = writer.startList("(", ")");
+      for (SqlNode operand : call.getOperandList()) {
+        writer.sep(",");
+        operand.unparse(writer, leftPrec, rightPrec);
+      }
+      writer.endList(arrayFrame);
+      break;
     case FLOOR:
       if (call.operandCount() != 2) {
         super.unparseCall(writer, call, leftPrec, rightPrec);
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 ffa34ea95f..188806ceb9 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
@@ -9185,7 +9185,10 @@ private void checkLiteral2(String expression, String 
expected) {
 
   /** Test case for
    * <a 
href="https://issues.apache.org/jira/browse/CALCITE-6258";>[CALCITE-6258]
-   * Map value constructor is unparsed incorrectly for PrestoSqlDialect</a>.*/
+   * Map value constructor is unparsed incorrectly for PrestoSqlDialect</a>,
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-6941";>[CALCITE-6941]
+   * Array/Map value constructor is unparsed incorrectly in ClickHouse</a>.
+   * */
   @Test void testMapValueConstructor() {
     final String query = "SELECT MAP['k1', 'v1', 'k2', 'v2']";
     final String expectedPresto = "SELECT MAP (ARRAY['k1', 'k2'], ARRAY['v1', 
'v2'])\n"
@@ -9196,13 +9199,15 @@ private void checkLiteral2(String expression, String 
expected) {
         + "FROM (VALUES (0)) `t` (`ZERO`)";
     final String expectedHive = "SELECT MAP ('k1', 'v1', 'k2', 'v2')";
     final String expectedDoris = "SELECT MAP ('k1', 'v1', 'k2', 'v2')";
+    final String expectedClickHouse = "SELECT map('k1', 'v1', 'k2', 'v2')";
     sql(query)
         .withPresto().ok(expectedPresto)
         .withTrino().ok(expectedTrino)
         .withStarRocks().ok(expectedStarRocks)
         .withDoris().ok(expectedDoris)
         .withSpark().ok(expectedSpark)
-        .withHive().ok(expectedHive);
+        .withHive().ok(expectedHive)
+        .withClickHouse().ok(expectedClickHouse);
   }
 
   @Test void testMapValueConstructorWithArray() {
@@ -9212,10 +9217,12 @@ private void checkLiteral2(String expression, String 
expected) {
     final String expectedTrino = expectedPresto;
     final String expectedSpark = "SELECT MAP (ARRAY ('k1', 'k2'), ARRAY ('v1', 
'v2'))\n"
         + "FROM (VALUES (0)) `t` (`ZERO`)";
+    final String expectedClickHouse = "SELECT map(array('k1', 'k2'), 
array('v1', 'v2'))";
     sql(query)
         .withPresto().ok(expectedPresto)
         .withTrino().ok(expectedTrino)
-        .withSpark().ok(expectedSpark);
+        .withSpark().ok(expectedSpark)
+        .withClickHouse().ok(expectedClickHouse);
   }
 
   /** Test case for
@@ -9231,7 +9238,9 @@ private void checkLiteral2(String expression, String 
expected) {
   @Test void testHiveMapValueConstructorWithArray() {
     final String query = "SELECT MAP[1, ARRAY['v1', 'v2']]";
     final String expectedHive = "SELECT MAP (1, ARRAY ('v1', 'v2'))";
-    sql(query).withHive().ok(expectedHive);
+    final String expectedClickHouse = "SELECT map(1, array('v1', 'v2'))";
+    sql(query).withHive().ok(expectedHive)
+        .withClickHouse().ok(expectedClickHouse);
   }
 
   /** Test case for
@@ -9257,10 +9266,12 @@ private void checkLiteral2(String expression, String 
expected) {
     final String expectedSpark = "SELECT ARRAY (1, 2, 3)\n"
         + "FROM (VALUES (0)) `t` (`ZERO`)";
     final String expectedHive = "SELECT ARRAY (1, 2, 3)";
+    final String expectedClickHouse = "SELECT array(1, 2, 3)";
     sql(query).withStarRocks().ok(expectedStarRocks)
         .withDoris().ok(expectedStarRocks)
         .withSpark().ok(expectedSpark)
-        .withHive().ok(expectedHive);
+        .withHive().ok(expectedHive)
+        .withClickHouse().ok(expectedClickHouse);
   }
 
   @Test void testTrimWithBothSpecialCharacter() {

Reply via email to