From afef8e6d72533df170e5e862fd2a31708a55a52d Mon Sep 17 00:00:00 2001 From: Erindcl Date: Fri, 20 Nov 2020 17:08:17 +0800 Subject: [PATCH] feat(flink): add grammar rules that pass the test --- src/grammar/flinksql/FlinkSqlParser.g4 | 58 +++++++++++++++++++++----- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/src/grammar/flinksql/FlinkSqlParser.g4 b/src/grammar/flinksql/FlinkSqlParser.g4 index f6ca930..2652eb8 100644 --- a/src/grammar/flinksql/FlinkSqlParser.g4 +++ b/src/grammar/flinksql/FlinkSqlParser.g4 @@ -157,19 +157,27 @@ valuesRowDefinition // Select statements queryStatement - : + : ( + selectClause | selectStatement + ) orderByCaluse? limitClause ; selectStatement - : SELECT setQuantifier? - (ASTERISK_SIGN | projectItemDefinition (COMMA projectItemDefinition)*) - FROM tableExpression + : selectClause fromClause whereClause? groupByClause? havingClause? + ; + +selectClause + : SELECT setQuantifier? (ASTERISK_SIGN | projectItemDefinition (COMMA projectItemDefinition)*) ; projectItemDefinition : expression (AS? uid)? | uid '.' '*' ; +fromClause + : FROM tableExpression + ; + tableExpression : tableReference (COMMA tableReference)* ; @@ -182,6 +190,39 @@ tablePrimary : TABLE? uid ; +whereClause + : WHERE booleanExpression + ; + +groupByClause + : GROUP BY groupItemDefinition (COMMA groupItemDefinition)* + ; + +groupItemDefinition + : expression + | LR_BRACKET RR_BRACKET + | LR_BRACKET expression (COMMA expression)* RR_BRACKET + | CUBE LR_BRACKET expression (COMMA expression)* RR_BRACKET + | ROLLUP LR_BRACKET expression (COMMA expression)* RR_BRACKET + | GROUPING SETS LR_BRACKET groupItemDefinition (COMMA groupItemDefinition)* RR_BRACKET + ; + +havingClause + : HAVING booleanExpression + ; + +orderByCaluse + : ORDER BY orderItemDefition (COMMA orderItemDefition)* + ; + +orderItemDefition + : expression (ASC | DESC) + ; + +limitClause + : LIMIT (ALL | limit=expression) + ; + // expression expression @@ -227,7 +268,7 @@ primaryExpression | FIRST '(' expression (IGNORE NULLS)? ')' #first | LAST '(' expression (IGNORE NULLS)? ')' #last | POSITION '(' substr=valueExpression IN str=valueExpression ')' #position - | constant #constantDefault + // | constant #constantDefault | ASTERISK #star // | qualifiedName '.' ASTERISK #star // | '(' namedExpression (',' namedExpression)+ ')' #rowConstructor @@ -236,8 +277,8 @@ primaryExpression // (FILTER '(' WHERE where=booleanExpression ')')? (OVER windowSpec)? #functionCall // | identifier '->' expression #lambda // | '(' identifier (',' identifier)+ ')' '->' expression #lambda - | value=primaryExpression LS_BRACKET index=valueExpression RS_BRACKET #subscript - // | identifier #columnReference + // | value=primaryExpression LS_BRACKET index=valueExpression RS_BRACKET #subscript + | identifier #columnReference // | base=primaryExpression '.' fieldName=identifier #dereference | '(' expression ')' #parenthesizedExpression // | EXTRACT '(' field=identifier FROM source=valueExpression ')' #extract @@ -685,9 +726,6 @@ RR_BRACKET: ')'; COMMA: ','; SEMICOLON: ';'; AT_SIGN: '@'; -ZERO_DECIMAL: '0'; -ONE_DECIMAL: '1'; -TWO_DECIMAL: '2'; SINGLE_QUOTE_SYMB: '\''; DOUBLE_QUOTE_SYMB: '"'; REVERSE_QUOTE_SYMB: '`';