This is an automated email from the ASF dual-hosted git repository.

danny0405 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/master by this push:
     new 3cff43a  [CALCITE-3231] Support ARRAY type for SqlDataTypeSpec
3cff43a is described below

commit 3cff43a053abde614e5ab00587d10831197dc6ee
Author: yuzhao.cyz <yuzhao....@alibaba-inc.com>
AuthorDate: Tue Aug 6 21:54:08 2019 +0800

    [CALCITE-3231] Support ARRAY type for SqlDataTypeSpec
---
 core/src/main/codegen/templates/Parser.jj          | 26 ++++++++++++++--------
 .../org/apache/calcite/sql/SqlDataTypeSpec.java    |  3 +++
 .../apache/calcite/sql/parser/SqlParserTest.java   |  9 ++++++++
 .../org/apache/calcite/test/SqlValidatorTest.java  |  7 ++++++
 4 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/core/src/main/codegen/templates/Parser.jj 
b/core/src/main/codegen/templates/Parser.jj
index dfeb2bd..6c19c34 100644
--- a/core/src/main/codegen/templates/Parser.jj
+++ b/core/src/main/codegen/templates/Parser.jj
@@ -4596,16 +4596,19 @@ SqlIdentifier TypeName() :
 }
 {
     (
-        LOOKAHEAD(2)
-        sqlTypeName = SqlTypeName(s) {
-            typeName = new SqlIdentifier(sqlTypeName.name(), s.end(this));
-        }
 <#-- additional types are included here -->
+<#-- make custom data types in front of Calcite core data types -->
 <#list parser.dataTypeParserMethods as method>
-    |
+        LOOKAHEAD(2)
         typeName = ${method}
+    |
 </#list>
+        LOOKAHEAD(2)
+        sqlTypeName = SqlTypeName(s) {
+            typeName = new SqlIdentifier(sqlTypeName.name(), s.end(this));
+        }
     |
+        LOOKAHEAD(2)
         typeName = CollectionsTypeName()
     |
         typeName = CompoundIdentifier()
@@ -4724,10 +4727,15 @@ SqlIdentifier CollectionsTypeName() :
 {
 }
 {
-    <MULTISET> {
-        return new SqlIdentifier(
-            SqlTypeName.MULTISET.name(), getPos());
-    }
+    (
+        <MULTISET> {
+            return new SqlIdentifier(SqlTypeName.MULTISET.name(), getPos());
+        }
+    |
+        <ARRAY> {
+            return new SqlIdentifier(SqlTypeName.ARRAY.name(), getPos());
+        }
+    )
 }
 
 /**
diff --git a/core/src/main/java/org/apache/calcite/sql/SqlDataTypeSpec.java 
b/core/src/main/java/org/apache/calcite/sql/SqlDataTypeSpec.java
index 08f0b72..0f4f70e 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlDataTypeSpec.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlDataTypeSpec.java
@@ -383,6 +383,9 @@ public class SqlDataTypeSpec extends SqlNode {
       case MULTISET:
         type = typeFactory.createMultisetType(type, -1);
         break;
+      case ARRAY:
+        type = typeFactory.createArrayType(type, -1);
+        break;
 
       default:
         throw Util.unexpected(collectionsSqlTypeName);
diff --git 
a/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java 
b/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java
index aa16124..3070da0 100644
--- a/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java
+++ b/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java
@@ -4701,6 +4701,15 @@ public class SqlParserTest {
         "(ARRAY[(ROW(1, 'a')), (ROW(2, 'b'))])");
   }
 
+  @Test public void testCastAsArrayType() {
+    checkExp("cast(a as int array)", "CAST(`A` AS INTEGER ARRAY)");
+    checkExp("cast(a as varchar(5) array)", "CAST(`A` AS VARCHAR(5) ARRAY)");
+    checkExpFails("cast(a as int array ^array^)",
+        "(?s).*Encountered \"array\" at line 1, column 21.\n.*");
+    checkExpFails("cast(a as int array^<^10>)",
+        "(?s).*Encountered \"<\" at line 1, column 20.\n.*");
+  }
+
   @Test public void testMapValueConstructor() {
     checkExp("map[1, 'x', 2, 'y']", "(MAP[1, 'x', 2, 'y'])");
     checkExp("map [1, 'x', 2, 'y']", "(MAP[1, 'x', 2, 'y'])");
diff --git a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java 
b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
index 15b2502..d92e78b 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
@@ -7703,6 +7703,13 @@ public class SqlValidatorTest extends 
SqlValidatorTestCase {
         .columnType("CHAR(3) ARRAY NOT NULL");
   }
 
+  @Test public void testCastAsArrayType() {
+    sql("select cast(array[1,null,2] as int array) from (values (1))")
+        .columnType("INTEGER NOT NULL ARRAY NOT NULL");
+    sql("select cast(array['1',null,'2'] as varchar(5) array) from (values 
(1))")
+        .columnType("VARCHAR(5) NOT NULL ARRAY NOT NULL");
+  }
+
   @Test public void testMultisetConstructor() {
     sql("select multiset[1,null,2] as a from (values (1))")
         .columnType("INTEGER MULTISET NOT NULL");

Reply via email to