Skip to content

Commit 9fc91a5

Browse files
author
Erindcl
committed
feat(flink): add drop/alter test add add part of queryStatement
1 parent 158e235 commit 9fc91a5

12 files changed

Lines changed: 4612 additions & 127 deletions

src/grammar/flinksql/FlinkSqlLexer.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ SINGLE_QUOTE_SYMB: '\'';
332332
DOUBLE_QUOTE_SYMB: '"';
333333
REVERSE_QUOTE_SYMB: '`';
334334
COLON_SYMB: ':';
335+
ASTERISK_SIGN: '*';
335336

336337

337338
fragment ID_LITERAL: [A-Z_0-9a-z]*?[A-Z_a-z]+?[A-Z_0-9a-z]*;

src/grammar/flinksql/FlinkSqlParser.g4

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
parser grammar FlinkSqlParser;
1+
grammar FlinkSqlParser;
22

33
options { tokenVocab=FlinkSqlLexer; }
44

@@ -27,7 +27,7 @@ ddlStatement
2727
;
2828

2929
dmlStatement
30-
: selectStatement | insertStatement
30+
: queryStatement | insertStatement
3131
;
3232

3333

@@ -110,25 +110,74 @@ alterFunction
110110
// Drop statements
111111

112112
dropTable
113-
: DROP TABLE ifExists uid
113+
: DROP TABLE ifExists? uid
114114
;
115115

116116
dropDatabase
117-
: DROP DATABASE ifExists uid dropType=(RESTRICT | CASCADE)?
117+
: DROP DATABASE ifExists? uid dropType=(RESTRICT | CASCADE)?
118118
;
119119

120120
dropView
121-
: DROP TEMPORARY? VIEW ifExists uid
121+
: DROP TEMPORARY? VIEW ifExists? uid
122122
;
123123

124124
dropFunction
125-
: DROP (TEMPORARY|TEMPORARY SYSTEM)? FUNCTION ifExists uid
125+
: DROP (TEMPORARY|TEMPORARY SYSTEM)? FUNCTION ifExists? uid
126126
;
127127

128128

129129
// Select statements
130130

131+
queryStatement
132+
: valuesDefinition
133+
| (
134+
selectStatement
135+
| selectWithoutFromDefinition
136+
// | queryStatement UNION ALL? queryStatement
137+
// | queryStatement EXCEPT queryStatement
138+
// | queryStatement INTERSECT queryStatement
139+
) queryOrderByDefinition? queryLimitDefinition? queryOffsetDefinition? queryFetchDefinition?
140+
;
141+
131142
selectStatement
143+
: SELECT (ALL | DISTINCT)?
144+
(ASTERISK_SIGN | projectItemDefinition (COMMA projectItemDefinition)*)
145+
FROM tableExpression
146+
;
147+
148+
projectItemDefinition // expression (AS? columnAlias)? | tableAlias . *
149+
:
150+
;
151+
152+
tableExpression
153+
:
154+
;
155+
156+
selectWithoutFromDefinition
157+
:
158+
;
159+
160+
queryOrderByDefinition
161+
: ORDER BY orderItemDefition (COMMA orderItemDefition)*
162+
;
163+
164+
orderItemDefition // expression (ASC | DESC)?
165+
:
166+
;
167+
168+
queryLimitDefinition
169+
: LIMIT (countDefinition | ALL)
170+
;
171+
172+
countDefinition
173+
:
174+
;
175+
176+
queryOffsetDefinition // OFFSET start (ROW | ROWS)
177+
:
178+
;
179+
180+
queryFetchDefinition // FETCH (FIRST | NEXT) countDefinition? (ROW | ROWS) ONLY
132181
:
133182
;
134183

@@ -153,7 +202,7 @@ valuesDefinition
153202
: VALUES valuesRowDefinition (COMMA valuesRowDefinition)*
154203
;
155204

156-
// TODO 匹配所有的值 任意value
205+
// TODO 匹配所有的值 任意value 即:(val1 [, val2, ...])
157206
valuesRowDefinition
158207
: LR_BRACKET
159208
.*?

src/lib/flinksql/FlinkSqlParser.interp

Lines changed: 4 additions & 4 deletions
Large diffs are not rendered by default.

src/lib/flinksql/FlinkSqlParser.tokens

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
T__0=1
12
SPACE=1
23
SPEC_MYSQL_COMMENT=2
34
COMMENT_INPUT=3
@@ -303,6 +304,7 @@ SINGLE_QUOTE_SYMB=302
303304
DOUBLE_QUOTE_SYMB=303
304305
REVERSE_QUOTE_SYMB=304
305306
COLON_SYMB=305
307+
','=1
306308
'SELECT'=5
307309
'FROM'=6
308310
'ADD'=7
@@ -590,7 +592,6 @@ COLON_SYMB=305
590592
'.'=293
591593
'('=294
592594
')'=295
593-
','=296
594595
';'=297
595596
'@'=298
596597
'0'=299
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
token literal names:
2+
null
3+
','
4+
5+
token symbolic names:
6+
null
7+
null
8+
9+
rule names:
10+
T__0
11+
12+
channel names:
13+
DEFAULT_TOKEN_CHANNEL
14+
HIDDEN
15+
16+
mode names:
17+
DEFAULT_MODE
18+
19+
atn:
20+
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 3, 7, 8, 1, 4, 2, 9, 2, 3, 2, 3, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 6, 2, 3, 3, 2, 2, 2, 3, 5, 3, 2, 2, 2, 5, 6, 7, 46, 2, 2, 6, 4, 3, 2, 2, 2, 3, 2, 2]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Generated from /Users/erindeng/Desktop/dt-sql-parser/src/grammar/flinksql/FlinkSqlParser.g4 by ANTLR 4.8
2+
// jshint ignore: start
3+
var antlr4 = require('antlr4/index');
4+
5+
6+
7+
var serializedATN = ["\u0003\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964",
8+
"\u0002\u0003\u0007\b\u0001\u0004\u0002\t\u0002\u0003\u0002\u0003\u0002",
9+
"\u0002\u0002\u0003\u0003\u0003\u0003\u0002\u0002\u0002\u0006\u0002\u0003",
10+
"\u0003\u0002\u0002\u0002\u0003\u0005\u0003\u0002\u0002\u0002\u0005\u0006",
11+
"\u0007.\u0002\u0002\u0006\u0004\u0003\u0002\u0002\u0002\u0003\u0002",
12+
"\u0002"].join("");
13+
14+
15+
var atn = new antlr4.atn.ATNDeserializer().deserialize(serializedATN);
16+
17+
var decisionsToDFA = atn.decisionToState.map( function(ds, index) { return new antlr4.dfa.DFA(ds, index); });
18+
19+
function FlinkSqlParserLexer(input) {
20+
antlr4.Lexer.call(this, input);
21+
this._interp = new antlr4.atn.LexerATNSimulator(this, atn, decisionsToDFA, new antlr4.PredictionContextCache());
22+
return this;
23+
}
24+
25+
FlinkSqlParserLexer.prototype = Object.create(antlr4.Lexer.prototype);
26+
FlinkSqlParserLexer.prototype.constructor = FlinkSqlParserLexer;
27+
28+
Object.defineProperty(FlinkSqlParserLexer.prototype, "atn", {
29+
get : function() {
30+
return atn;
31+
}
32+
});
33+
34+
FlinkSqlParserLexer.EOF = antlr4.Token.EOF;
35+
FlinkSqlParserLexer.T__0 = 1;
36+
37+
FlinkSqlParserLexer.prototype.channelNames = [ "DEFAULT_TOKEN_CHANNEL", "HIDDEN" ];
38+
39+
FlinkSqlParserLexer.prototype.modeNames = [ "DEFAULT_MODE" ];
40+
41+
FlinkSqlParserLexer.prototype.literalNames = [ null, "','" ];
42+
43+
FlinkSqlParserLexer.prototype.symbolicNames = [ ];
44+
45+
FlinkSqlParserLexer.prototype.ruleNames = [ "T__0" ];
46+
47+
FlinkSqlParserLexer.prototype.grammarFileName = "FlinkSqlParser.g4";
48+
49+
50+
exports.FlinkSqlParserLexer = FlinkSqlParserLexer;
51+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
T__0=1
2+
','=1

0 commit comments

Comments
 (0)