twalthr commented on a change in pull request #8548: [FLINK-6962] [table] Add a 
create table SQL DDL
URL: https://github.com/apache/flink/pull/8548#discussion_r287803308
 
 

 ##########
 File path: 
flink-table/flink-sql-parser/src/main/codegen/includes/parserImpls.ftl
 ##########
 @@ -0,0 +1,347 @@
+<#--
+// 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.
+-->
+
+void TableColumn(TableCreationContext context) :
+{
+}
+{
+    (
+        TableColumn2(context.columnList)
+    |
+        context.watermark = Watermark()
+    |
+        context.primaryKeyList = PrimaryKey()
+    |
+        UniqueKey(context.uniqueKeysList)
+    |
+        ComputedColumn(context)
+    )
+}
+
+void ComputedColumn(TableCreationContext context) :
+{
+    SqlNode identifier;
+    SqlNode expr;
+    boolean hidden = false;
+    SqlParserPos pos;
+}
+{
+    identifier = SimpleIdentifier() {pos = getPos();}
+    <AS>
+    expr = Expression(ExprContext.ACCEPT_SUB_QUERY) {
+        expr = SqlStdOperatorTable.AS.createCall(Span.of(identifier, 
expr).pos(), expr, identifier);
+        context.columnList.add(expr);
+    }
+}
+
+void TableColumn2(List<SqlNode> list) :
+{
+    SqlParserPos pos;
+    SqlIdentifier name;
+    SqlDataTypeSpec type;
+    SqlCharStringLiteral comment = null;
+    boolean isHeader = false;
+}
+{
+    name = SimpleIdentifier()
+    type = DataType()
+    (
+        <NULL> { type = type.withNullable(true); }
+    |
+        <NOT> <NULL> { type = type.withNullable(false); }
+    |
+        { type = type.withNullable(true); }
+    )
+    [ <HEADER> { isHeader = true; } ]
+    [ <COMMENT> <QUOTED_STRING> {
+        String p = SqlParserUtil.parseString(token.image);
+        comment = SqlLiteral.createCharString(p, getPos());
+    }]
+    {
+        SqlTableColumn tableColumn = new SqlTableColumn(name, type, comment, 
getPos());
+        tableColumn.setHeader(isHeader);
+        list.add(tableColumn);
+    }
+}
+
+SqlNodeList PrimaryKey() :
+{
+    List<SqlNode> pkList = new ArrayList<SqlNode>();
+
+    SqlParserPos pos;
+    SqlIdentifier columnName;
+}
+{
+    <PRIMARY> { pos = getPos(); } <KEY> <LPAREN>
+        columnName = SimpleIdentifier() { pkList.add(columnName); }
+        (<COMMA> columnName = SimpleIdentifier() { pkList.add(columnName); })*
+    <RPAREN>
+    {
+        return new SqlNodeList(pkList, pos.plus(getPos()));
+    }
+}
+
+void UniqueKey(List<SqlNodeList> list) :
+{
+    List<SqlNode> ukList = new ArrayList<SqlNode>();
+    SqlParserPos pos;
+    SqlIdentifier columnName;
+}
+{
+    <UNIQUE> { pos = getPos(); } <LPAREN>
+        columnName = SimpleIdentifier() { ukList.add(columnName); }
+        (<COMMA> columnName = SimpleIdentifier() { ukList.add(columnName); })*
+    <RPAREN>
+    {
+        SqlNodeList uk = new SqlNodeList(ukList, pos.plus(getPos()));
+        list.add(uk);
+    }
+}
+
+SqlWatermark Watermark() :
+{
+    SqlIdentifier watermarkName = null;
+    SqlIdentifier columnName;
+    SqlWatermarkStrategy strategy;
+    int delayDef = -1;
+    SqlTimeUnit timeUnit = null;
+}
+{
+    <WATERMARK>
+    [watermarkName = SimpleIdentifier()]
+    <FOR>
+    columnName = SimpleIdentifier()
+    <AS>
+    (
+        <BOUNDED> <WITH> <DELAY>
+            {
+                strategy = SqlWatermarkStrategy.BOUNDED_WITH_DELAY;
+                delayDef = UnsignedIntLiteral();
+            }
+            (
+                <DAY> { timeUnit = SqlTimeUnit.DAY; }
+            |   <HOUR> { timeUnit = SqlTimeUnit.HOUR; }
+            |   <MINUTE> { timeUnit = SqlTimeUnit.MINUTE; }
+            |   <SECOND> { timeUnit = SqlTimeUnit.SECOND; }
+            |   <MILLISECOND> { timeUnit = SqlTimeUnit.MILLISECOND; }
+            )
+    |   <ASCENDING> { strategy = SqlWatermarkStrategy.ASCENDING; }
+    |   <FROM_SOURCE> { strategy = SqlWatermarkStrategy.FROM_SOURCE; }
+    )
+    {
+        return new SqlWatermark(
+                watermarkName,
+                columnName,
+                strategy,
+                delayDef,
+                timeUnit,
+                getPos());
+    }
+}
+
+SqlNode PropertyValue() :
+{
+    SqlIdentifier key;
+    SqlNode value;
+    SqlParserPos pos;
+}
+{
+    key = CompoundIdentifier()
+    { pos = getPos(); }
+    <EQ> value = StringLiteral()
+    {
+        return new SqlProperty(key, value, getPos());
+    }
+}
+
+SqlNode SqlCreateTable() :
+{
+    final SqlParserPos startPos;
+    SqlIdentifier tableName;
+    String tableType = null;
+    SqlNodeList primaryKeyList = null;
+    List<SqlNodeList> uniqueKeysList = null;
+    SqlNodeList columnList = SqlNodeList.EMPTY;
+    SqlWatermark watermark = null;
+       SqlCharStringLiteral comment = null;
+
+    SqlNodeList propertyList = null;
+    SqlNodeList partitionColumns = null;
+
+    SqlParserPos pos;
+}
+{
+    <CREATE> { startPos = getPos(); }
+
+    <TABLE>
+
+    tableName = CompoundIdentifier()
+    [
+        <LPAREN> { pos = getPos(); TableCreationContext ctx = new 
TableCreationContext();}
+        TableColumn(ctx)
+        (
+            <COMMA> TableColumn(ctx)
+        )*
+        {
+            pos = pos.plus(getPos());
+            columnList = new SqlNodeList(ctx.columnList, pos);
+            primaryKeyList = ctx.primaryKeyList;
+            uniqueKeysList = ctx.uniqueKeysList;
+            watermark = ctx.watermark;
+            tableType = ctx.tableType;
+        }
+        <RPAREN>
+    ]
+    [ <COMMENT> <QUOTED_STRING> {
+        String p = SqlParserUtil.parseString(token.image);
+        comment = SqlLiteral.createCharString(p, getPos());
+    }]
+    [
+        <PARTITIONED> <BY>
+            {
+                SqlNode column;
+                List<SqlNode> partitionKey = new ArrayList<SqlNode>();
+                pos = getPos();
+
+            }
+            <LPAREN>
+            [
+                column = SimpleIdentifier()
+                {
+                    partitionKey.add(column);
+                }
+                (
+                    <COMMA> column = SimpleIdentifier()
+                        {
+                            partitionKey.add(column);
+                        }
+                )*
+            ]
+            <RPAREN>
+            {
+                partitionColumns = new SqlNodeList(partitionKey, 
pos.plus(getPos()));
+            }
+    ]
+    [
+        <WITH>
+            {
+                SqlNode property;
+                List<SqlNode> proList = new ArrayList<SqlNode>();
+                pos = getPos();
+            }
+            <LPAREN>
+            [
+                property = PropertyValue()
+                {
+                proList.add(property);
+                }
+                (
+                <COMMA> property = PropertyValue()
+                    {
+                    proList.add(property);
+                    }
+                )*
+            ]
+            <RPAREN>
+        {  propertyList = new SqlNodeList(proList, pos.plus(getPos())); }
+    ]
+
+    {
+        return new SqlCreateTable(startPos.plus(getPos()),
+                tableType,
+                tableName,
+                columnList,
+                primaryKeyList,
+                uniqueKeysList,
+                watermark,
+                propertyList,
+                partitionColumns,
+                comment);
+    }
+}
+
+SqlNode SqlDropTable() :
+{
+    SqlParserPos pos;
+    SqlIdentifier tableName = null;
+    boolean ifExists = false;
+}
+{
+    <DROP> { pos = getPos(); }
+
+    <TABLE>
+
+    [<IF> <EXISTS> { ifExists = true; } ]
+
+    tableName = CompoundIdentifier()
+
+    {
+         return new SqlDropTable(pos, tableName, ifExists);
+    }
+}
+
+SqlIdentifier SqlArrayType() :
+{
+    SqlParserPos pos;
+    SqlDataTypeSpec elementType;
+}
+{
+    <ARRAY> { pos = getPos(); }
+    <LT> elementType = DataType()
+    <GT>
+    {
+        return new SqlArrayType(pos, elementType);
+    }
+}
+
+SqlIdentifier SqlMapType() :
+{
+    SqlParserPos pos;
+    SqlDataTypeSpec keyType;
+    SqlDataTypeSpec valType;
+}
+{
+    <MAP> { pos = getPos(); }
+    <LT> keyType = DataType()
+    <COMMA> valType = DataType()
+    <GT>
+    {
+        return new SqlMapType(pos, keyType, valType);
+    }
+}
+
+SqlIdentifier SqlStructType() :
+{
+    SqlParserPos pos;
+    List<SqlIdentifier> fieldNames = new ArrayList<SqlIdentifier>();
+    List<SqlDataTypeSpec> fieldTypes = new ArrayList<SqlDataTypeSpec>();
+}
+{
+    <STRUCT> { pos = getPos(); SqlIdentifier fName; SqlDataTypeSpec fType;}
 
 Review comment:
   Please align the parser with the new type system described in FLIP-37: 
https://docs.google.com/document/d/1a9HUb6OaBIoj9IRfbILcMFPrOL7ALeZ3rVI66dvA2_U/edit#
   
   The parser should be able to parse all serialized representations of the 
data types listed in `org.apache.flink.table.types.LogicalTypesTest`.
   
   Data types such as `STRUCT` are not SQL standard. Instead, Flink builds on 
top of `ROW` and adds comment functionality there.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to