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 ee4801dfe5 [CALCITE-7055] Invalid unparse for cast to array type in
StarRocks
ee4801dfe5 is described below
commit ee4801dfe55702962aca4dc7798d62e532367ab5
Author: Yu Xu <[email protected]>
AuthorDate: Thu Jun 12 18:59:51 2025 +0800
[CALCITE-7055] Invalid unparse for cast to array type in StarRocks
---
.../sql/SqlArrayWithAngleBracketsNameSpec.java | 46 ++++++++++++++++++++++
.../calcite/sql/dialect/StarRocksSqlDialect.java | 22 ++++++++++-
.../calcite/rel/rel2sql/RelToSqlConverterTest.java | 38 +++++++++++++++++-
3 files changed, 103 insertions(+), 3 deletions(-)
diff --git
a/core/src/main/java/org/apache/calcite/sql/SqlArrayWithAngleBracketsNameSpec.java
b/core/src/main/java/org/apache/calcite/sql/SqlArrayWithAngleBracketsNameSpec.java
new file mode 100644
index 0000000000..c84d4529fe
--- /dev/null
+++
b/core/src/main/java/org/apache/calcite/sql/SqlArrayWithAngleBracketsNameSpec.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.sql;
+
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.type.SqlTypeName;
+
+/**
+ * A SqlArrayWithAngleBracketsNameSpec to parse or unparse SQL ARRAY type to
{@code ARRAY<VARCHAR>}.
+ */
+public class SqlArrayWithAngleBracketsNameSpec extends
SqlCollectionTypeNameSpec {
+
+ /**
+ * Creates a {@code SqlArrayWithAngleBracketsNameSpec}.
+ *
+ * @param elementTypeName Type of the collection element
+ * @param collectionTypeName Collection type name
+ * @param pos Parser position, must not be null
+ */
+ public SqlArrayWithAngleBracketsNameSpec(SqlTypeNameSpec elementTypeName,
+ SqlTypeName collectionTypeName, SqlParserPos pos) {
+ super(elementTypeName, collectionTypeName, pos);
+ }
+
+ @Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec)
{
+ writer.keyword("ARRAY");
+ SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.FUN_CALL,
"<", ">");
+ this.getElementTypeName().unparse(writer, leftPrec, rightPrec);
+ writer.endList(frame);
+ }
+
+}
diff --git
a/core/src/main/java/org/apache/calcite/sql/dialect/StarRocksSqlDialect.java
b/core/src/main/java/org/apache/calcite/sql/dialect/StarRocksSqlDialect.java
index eab4e20be9..cce51df6f0 100644
--- a/core/src/main/java/org/apache/calcite/sql/dialect/StarRocksSqlDialect.java
+++ b/core/src/main/java/org/apache/calcite/sql/dialect/StarRocksSqlDialect.java
@@ -25,6 +25,7 @@
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.sql.SqlAbstractDateTimeLiteral;
import org.apache.calcite.sql.SqlAlienSystemTypeNameSpec;
+import org.apache.calcite.sql.SqlArrayWithAngleBracketsNameSpec;
import org.apache.calcite.sql.SqlBasicTypeNameSpec;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlDataTypeSpec;
@@ -32,10 +33,12 @@
import org.apache.calcite.sql.SqlLiteral;
import org.apache.calcite.sql.SqlMapTypeNameSpec;
import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlTypeNameSpec;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.fun.SqlFloorFunction;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.AbstractSqlType;
+import org.apache.calcite.sql.type.ArraySqlType;
import org.apache.calcite.sql.type.BasicSqlType;
import org.apache.calcite.sql.type.MapSqlType;
import org.apache.calcite.sql.type.SqlTypeName;
@@ -45,6 +48,8 @@
import static org.apache.calcite.util.RelToSqlConverterUtil.unparseHiveTrim;
+import static java.util.Objects.requireNonNull;
+
/**
* A <code>SqlDialect</code> implementation for the StarRocks database.
*/
@@ -191,12 +196,25 @@ public StarRocksSqlDialect(Context context) {
SqlDataTypeSpec keySpec = (SqlDataTypeSpec)
getCastSpec(mapSqlType.getKeyType());
SqlDataTypeSpec valueSpec =
(SqlDataTypeSpec) getCastSpec(mapSqlType.getValueType());
- @SuppressWarnings("argument.type.incompatible")
+ SqlDataTypeSpec nonNullKeySpec =
+ requireNonNull(keySpec, "keySpec");
+ SqlDataTypeSpec nonNullValueSpec =
+ requireNonNull(valueSpec, "valueSpec");
SqlMapTypeNameSpec sqlMapTypeNameSpec =
- new SqlMapTypeNameSpec(keySpec, valueSpec, SqlParserPos.ZERO);
+ new SqlMapTypeNameSpec(nonNullKeySpec, nonNullValueSpec,
SqlParserPos.ZERO);
return new SqlDataTypeSpec(sqlMapTypeNameSpec,
SqlParserPos.ZERO);
case ARRAY:
+ ArraySqlType arraySqlType = (ArraySqlType) type;
+ SqlDataTypeSpec arrayValueSpec =
+ (SqlDataTypeSpec) getCastSpec(arraySqlType.getComponentType());
+ SqlDataTypeSpec nonNullarrayValueSpec =
+ requireNonNull(arrayValueSpec, "arrayValueSpec");
+ SqlTypeNameSpec typeNameSpec =
+ new SqlArrayWithAngleBracketsNameSpec(
+ nonNullarrayValueSpec.getTypeNameSpec(),
+ arraySqlType.getSqlTypeName(), SqlParserPos.ZERO);
+ return new SqlDataTypeSpec(typeNameSpec, SqlParserPos.ZERO);
case MULTISET:
throw new UnsupportedOperationException("StarRocks dialect does not
support cast to "
+ type.getSqlTypeName());
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 1b8b9d5cdb..818994333d 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
@@ -2515,6 +2515,43 @@ private SqlDialect nonOrdinalDialect() {
.withPostgresql().ok(expectedPostgresql);
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7055">[CALCITE-7055]
+ * Invalid unparse for cast to array type in StarRocks</a>.
+ */
+ @Test void testCastArrayStarRocks() {
+ final String query = "select cast(array['a','b','c']"
+ + " as varchar array)";
+ final String expectedStarRocks =
+ "SELECT CAST(['a', 'b', 'c'] AS ARRAY< VARCHAR >)";
+ sql(query).withStarRocks().ok(expectedStarRocks);
+
+ final String query1 = "select cast(array[array['a'], array['b'],
array['c']]"
+ + " as varchar array array)";
+ final String expectedStarRocks1 =
+ "SELECT CAST([['a'],['b'],['c']] AS ARRAY< ARRAY< VARCHAR > >)";
+ sql(query1).withStarRocks().ok(expectedStarRocks1);
+
+ final String query2 = "select cast(array[MAP['a',1],MAP['b',2],MAP['c',3]]"
+ + " as MAP<varchar,integer> array)";
+ final String expectedStarRocks2 =
+ "SELECT CAST([MAP { 'a' : 1 }, MAP { 'b' : 2 }, MAP { 'c' : 3 }]"
+ + " AS ARRAY< MAP< VARCHAR, INT > >)";
+ sql(query2).withStarRocks().ok(expectedStarRocks2);
+
+ final String query3 = "select cast(MAP['a',ARRAY[1,2,3]]"
+ + " as MAP<varchar,integer array>)";
+ final String expectedStarRocks3 =
+ "SELECT CAST(MAP { 'a' :[1, 2, 3] } AS MAP< VARCHAR, ARRAY< INT > >)";
+ sql(query3).withStarRocks().ok(expectedStarRocks3);
+
+ final String query4 = "select cast(MAP['a',ARRAY[1.0,2.0,3.0]]"
+ + " as MAP<varchar,real array>)";
+ final String expectedStarRocks4 =
+ "SELECT CAST(MAP { 'a' :[1.0, 2.0, 3.0] } AS MAP< VARCHAR, ARRAY<
FLOAT > >)";
+ sql(query4).withStarRocks().ok(expectedStarRocks4);
+ }
+
/** Test case for
* <a
href="https://issues.apache.org/jira/browse/CALCITE-6088">[CALCITE-6088]
* SqlItemOperator fails in RelToSqlConverter</a>. */
@@ -9945,7 +9982,6 @@ private void checkLiteral2(String expression, String
expected) {
String query = "SELECT CAST(array[1,2,3] AS real array) FROM \"employee\"";
sql(query)
.withPhoenix().throws_("Phoenix dialect does not support cast to
ARRAY")
- .withStarRocks().throws_("StarRocks dialect does not support cast to
ARRAY")
.withHive().throws_("Hive dialect does not support cast to ARRAY");
String query1 = "SELECT CAST(MAP[1.0,2.0,3.0,4.0] AS MAP<FLOAT, REAL>)
FROM \"employee\"";