Feat/spark g4 (#168)

* feat: spark g4 test

* fix: fixed build lint

---------

Co-authored-by: liuyi <liuyi@dtstack.com>
Co-authored-by: dilu <dilu@dtstack.com>
This commit is contained in:
Frank 2023-10-08 14:23:06 +08:00 committed by GitHub
parent 0a9a7d15d3
commit 05da14d007
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 26129 additions and 25144 deletions

View File

@ -26,44 +26,6 @@ lexer grammar SparkSqlLexer;
*/ */
public has_unclosed_bracketed_comment = false; public has_unclosed_bracketed_comment = false;
/**
* Verify whether current token is a valid decimal token (which contains dot).
* Returns true if the character that follows the token is not a digit or letter or underscore.
*
* For example:
* For char stream "2.3", "2." is not a valid decimal token, because it is followed by digit '3'.
* For char stream "2.3_", "2.3" is not a valid decimal token, because it is followed by '_'.
* For char stream "2.3W", "2.3" is not a valid decimal token, because it is followed by 'W'.
* For char stream "12.0D 34.E2+0.12 " 12.0D is a valid decimal token because it is followed
* by a space. 34.E2 is a valid decimal token because it is followed by symbol '+'
* which is not a digit or letter or underscore.
*/
public isValidDecimal() {
const nextChar = _input.LA(1);
if (nextChar >= 'A' && nextChar <= 'Z' || nextChar >= '0' && nextChar <= '9' ||
nextChar == '_') {
return false;
} else {
return true;
}
}
/**
* This method will be called when we see '/*' and try to match it as a bracketed comment.
* If the next character is '+', it should be parsed as hint later, and we cannot match
* it as a bracketed comment.
*
* Returns true if the next character is '+'.
*/
public isHint() {
const nextChar = _input.LA(1);
if (nextChar == '+') {
return true;
} else {
return false;
}
}
/** /**
* This method will be called when the character stream ends and try to find out the * This method will be called when the character stream ends and try to find out the
* unclosed bracketed comment. * unclosed bracketed comment.
@ -71,7 +33,7 @@ lexer grammar SparkSqlLexer;
* and we set the flag and fail later. * and we set the flag and fail later.
*/ */
public markUnclosedComment() { public markUnclosedComment() {
has_unclosed_bracketed_comment = true; this.has_unclosed_bracketed_comment = true;
} }
} }
@ -488,26 +450,26 @@ INTEGER_VALUE
EXPONENT_VALUE EXPONENT_VALUE
: DIGIT+ EXPONENT : DIGIT+ EXPONENT
| DECIMAL_DIGITS EXPONENT {isValidDecimal()}? | DECIMAL_DIGITS EXPONENT
; ;
DECIMAL_VALUE DECIMAL_VALUE
: DECIMAL_DIGITS {isValidDecimal()}? : DECIMAL_DIGITS
; ;
FLOAT_LITERAL FLOAT_LITERAL
: DIGIT+ EXPONENT? 'F' : DIGIT+ EXPONENT? 'F'
| DECIMAL_DIGITS EXPONENT? 'F' {isValidDecimal()}? | DECIMAL_DIGITS EXPONENT? 'F'
; ;
DOUBLE_LITERAL DOUBLE_LITERAL
: DIGIT+ EXPONENT? 'D' : DIGIT+ EXPONENT? 'D'
| DECIMAL_DIGITS EXPONENT? 'D' {isValidDecimal()}? | DECIMAL_DIGITS EXPONENT? 'D'
; ;
BIGDECIMAL_LITERAL BIGDECIMAL_LITERAL
: DIGIT+ EXPONENT? 'BD' : DIGIT+ EXPONENT? 'BD'
| DECIMAL_DIGITS EXPONENT? 'BD' {isValidDecimal()}? | DECIMAL_DIGITS EXPONENT? 'BD'
; ;
IDENTIFIER IDENTIFIER
@ -540,7 +502,7 @@ SIMPLE_COMMENT
; ;
BRACKETED_COMMENT BRACKETED_COMMENT
: '/*' {!isHint()}? ( BRACKETED_COMMENT | . )*? ('*/' | {markUnclosedComment();} EOF) -> channel(HIDDEN) : '/*' ( BRACKETED_COMMENT | . )*? ('*/' | {this.markUnclosedComment();} EOF) -> channel(HIDDEN)
; ;
WS WS

View File

@ -1166,11 +1166,15 @@ windowSpec
RIGHT_PAREN RIGHT_PAREN
; ;
/**
* replace start identifier with start_ in grammar.
* https://github.com/tunnelvisionlabs/antlr4ts/issues/417
*/
windowFrame windowFrame
: frameType=KW_RANGE start=frameBound : frameType=KW_RANGE start_=frameBound
| frameType=KW_ROWS start=frameBound | frameType=KW_ROWS start_=frameBound
| frameType=KW_RANGE KW_BETWEEN start=frameBound KW_AND end=frameBound | frameType=KW_RANGE KW_BETWEEN start_=frameBound KW_AND end=frameBound
| frameType=KW_ROWS KW_BETWEEN start=frameBound KW_AND end=frameBound | frameType=KW_ROWS KW_BETWEEN start_=frameBound KW_AND end=frameBound
; ;
frameBound frameBound

View File

@ -8,13 +8,13 @@ export * from './lib/hive/HiveSqlParserListener';
export * from './lib/hive/HiveSqlParserVisitor'; export * from './lib/hive/HiveSqlParserVisitor';
export * from './lib/plsql/PlSqlParserListener'; export * from './lib/plsql/PlSqlParserListener';
export * from './lib/plsql/PlSqlParserVisitor'; export * from './lib/plsql/PlSqlParserVisitor';
export * from './lib/spark/SparkSqlVisitor'; export * from './lib/spark/SparkSqlParserVisitor';
export * from './lib/spark/SparkSqlListener'; export * from './lib/spark/SparkSqlParserListener';
export * from './lib/pgsql/PostgreSQLParserListener'; export * from './lib/pgsql/PostgreSQLParserListener';
export * from './lib/pgsql/PostgreSQLParserVisitor'; export * from './lib/pgsql/PostgreSQLParserVisitor';
export * from './lib/trinosql/TrinoSqlListener'; export * from './lib/trinosql/TrinoSqlListener';
export * from './lib/trinosql/TrinoSqlVisitor'; export * from './lib/trinosql/TrinoSqlVisitor';
export { SyntaxContextType } from './parser/common/basic-parser-types' export { SyntaxContextType } from './parser/common/basic-parser-types';
export type * from './parser/common/basic-parser-types'; export type * from './parser/common/basic-parser-types';

File diff suppressed because one or more lines are too long

View File

@ -1,566 +0,0 @@
T__0=1
T__1=2
T__2=3
T__3=4
T__4=5
T__5=6
T__6=7
T__7=8
T__8=9
T__9=10
ADD=11
AFTER=12
ALL=13
ALTER=14
ANALYZE=15
AND=16
ANTI=17
ANY=18
ARCHIVE=19
ARRAY=20
AS=21
ASC=22
AT=23
AUTHORIZATION=24
BETWEEN=25
BOTH=26
BUCKET=27
BUCKETS=28
BY=29
CACHE=30
CASCADE=31
CASE=32
CAST=33
CHANGE=34
CHECK=35
CLEAR=36
CLUSTER=37
CLUSTERED=38
CODEGEN=39
COLLATE=40
COLLECTION=41
COLUMN=42
COLUMNS=43
COMMENT=44
COMMIT=45
COMPACT=46
COMPACTIONS=47
COMPUTE=48
CONCATENATE=49
CONSTRAINT=50
COST=51
CREATE=52
CROSS=53
CUBE=54
CURRENT=55
CURRENT_DATE=56
CURRENT_TIME=57
CURRENT_TIMESTAMP=58
CURRENT_USER=59
DATA=60
DATABASE=61
DATABASES=62
DBPROPERTIES=63
DEFINED=64
DELETE=65
DELIMITED=66
DESC=67
DESCRIBE=68
DFS=69
DIRECTORIES=70
DIRECTORY=71
DISTINCT=72
DISTRIBUTE=73
DIV=74
DROP=75
ELSE=76
END=77
ESCAPE=78
ESCAPED=79
EXCEPT=80
EXCHANGE=81
EXISTS=82
EXPLAIN=83
EXPORT=84
EXTENDED=85
EXTERNAL=86
EXTRACT=87
FALSE=88
FETCH=89
FIELDS=90
FILTER=91
FILEFORMAT=92
FIRST=93
FOLLOWING=94
FOR=95
FOREIGN=96
FORMAT=97
FORMATTED=98
FROM=99
FULL=100
FUNCTION=101
FUNCTIONS=102
GLOBAL=103
GRANT=104
GROUP=105
GROUPING=106
HAVING=107
IF=108
IGNORE=109
IMPORT=110
IN=111
INDEX=112
INDEXES=113
INNER=114
INPATH=115
INPUTFORMAT=116
INSERT=117
INTERSECT=118
INTERVAL=119
INTO=120
IS=121
ITEMS=122
JOIN=123
KEYS=124
LAST=125
LATERAL=126
LAZY=127
LEADING=128
LEFT=129
LIKE=130
LIMIT=131
LINES=132
LIST=133
LOAD=134
LOCAL=135
LOCATION=136
LOCK=137
LOCKS=138
LOGICAL=139
MACRO=140
MAP=141
MATCHED=142
MERGE=143
MSCK=144
NAMESPACE=145
NAMESPACES=146
NATURAL=147
NO=148
NOT=149
NULL=150
NULLS=151
OF=152
ON=153
ONLY=154
OPTION=155
OPTIONS=156
OR=157
ORDER=158
OUT=159
OUTER=160
OUTPUTFORMAT=161
OVER=162
OVERLAPS=163
OVERLAY=164
OVERWRITE=165
PARTITION=166
PARTITIONED=167
PARTITIONS=168
PERCENTLIT=169
PIVOT=170
PLACING=171
POSITION=172
PRECEDING=173
PRIMARY=174
PRINCIPALS=175
PROPERTIES=176
PURGE=177
QUERY=178
RANGE=179
RECORDREADER=180
RECORDWRITER=181
RECOVER=182
REDUCE=183
REFERENCES=184
REFRESH=185
RENAME=186
REPAIR=187
REPLACE=188
RESET=189
RESTRICT=190
REVOKE=191
RIGHT=192
RLIKE=193
ROLE=194
ROLES=195
ROLLBACK=196
ROLLUP=197
ROW=198
ROWS=199
SCHEMA=200
SELECT=201
SEMI=202
SEPARATED=203
SERDE=204
SERDEPROPERTIES=205
SESSION_USER=206
SET=207
SETMINUS=208
SETS=209
SHOW=210
SKEWED=211
SOME=212
SORT=213
SORTED=214
START=215
STATISTICS=216
STORED=217
STRATIFY=218
STRUCT=219
SUBSTR=220
SUBSTRING=221
TABLE=222
TABLES=223
TABLESAMPLE=224
TBLPROPERTIES=225
TEMPORARY=226
TERMINATED=227
THEN=228
TIME=229
TO=230
TOUCH=231
TRAILING=232
TRANSACTION=233
TRANSACTIONS=234
TRANSFORM=235
TRIM=236
TRUE=237
TRUNCATE=238
TYPE=239
UNARCHIVE=240
UNBOUNDED=241
UNCACHE=242
UNION=243
UNIQUE=244
UNKNOWN=245
UNLOCK=246
UNSET=247
UPDATE=248
USE=249
USER=250
USING=251
VALUES=252
VIEW=253
VIEWS=254
WHEN=255
WHERE=256
WINDOW=257
WITH=258
ZONE=259
EQ=260
NSEQ=261
NEQ=262
NEQJ=263
LT=264
LTE=265
GT=266
GTE=267
PLUS=268
MINUS=269
ASTERISK=270
SLASH=271
PERCENT=272
TILDE=273
AMPERSAND=274
PIPE=275
CONCAT_PIPE=276
HAT=277
SEMICOLON=278
STRING=279
BIGINT_LITERAL=280
SMALLINT_LITERAL=281
TINYINT_LITERAL=282
INTEGER_VALUE=283
EXPONENT_VALUE=284
DECIMAL_VALUE=285
FLOAT_LITERAL=286
DOUBLE_LITERAL=287
BIGDECIMAL_LITERAL=288
IDENTIFIER=289
BACKQUOTED_IDENTIFIER=290
CUSTOM_VARS=291
SIMPLE_COMMENT=292
BRACKETED_COMMENT=293
WS=294
UNRECOGNIZED=295
'('=1
')'=2
','=3
'.'=4
'/*+'=5
'*/'=6
'->'=7
'['=8
']'=9
':'=10
'ADD'=11
'AFTER'=12
'ALL'=13
'ALTER'=14
'ANALYZE'=15
'AND'=16
'ANTI'=17
'ANY'=18
'ARCHIVE'=19
'ARRAY'=20
'AS'=21
'ASC'=22
'AT'=23
'AUTHORIZATION'=24
'BETWEEN'=25
'BOTH'=26
'BUCKET'=27
'BUCKETS'=28
'BY'=29
'CACHE'=30
'CASCADE'=31
'CASE'=32
'CAST'=33
'CHANGE'=34
'CHECK'=35
'CLEAR'=36
'CLUSTER'=37
'CLUSTERED'=38
'CODEGEN'=39
'COLLATE'=40
'COLLECTION'=41
'COLUMN'=42
'COLUMNS'=43
'COMMENT'=44
'COMMIT'=45
'COMPACT'=46
'COMPACTIONS'=47
'COMPUTE'=48
'CONCATENATE'=49
'CONSTRAINT'=50
'COST'=51
'CREATE'=52
'CROSS'=53
'CUBE'=54
'CURRENT'=55
'CURRENT_DATE'=56
'CURRENT_TIME'=57
'CURRENT_TIMESTAMP'=58
'CURRENT_USER'=59
'DATA'=60
'DATABASE'=61
'DBPROPERTIES'=63
'DEFINED'=64
'DELETE'=65
'DELIMITED'=66
'DESC'=67
'DESCRIBE'=68
'DFS'=69
'DIRECTORIES'=70
'DIRECTORY'=71
'DISTINCT'=72
'DISTRIBUTE'=73
'DIV'=74
'DROP'=75
'ELSE'=76
'END'=77
'ESCAPE'=78
'ESCAPED'=79
'EXCEPT'=80
'EXCHANGE'=81
'EXISTS'=82
'EXPLAIN'=83
'EXPORT'=84
'EXTENDED'=85
'EXTERNAL'=86
'EXTRACT'=87
'FALSE'=88
'FETCH'=89
'FIELDS'=90
'FILTER'=91
'FILEFORMAT'=92
'FIRST'=93
'FOLLOWING'=94
'FOR'=95
'FOREIGN'=96
'FORMAT'=97
'FORMATTED'=98
'FROM'=99
'FULL'=100
'FUNCTION'=101
'FUNCTIONS'=102
'GLOBAL'=103
'GRANT'=104
'GROUP'=105
'GROUPING'=106
'HAVING'=107
'IF'=108
'IGNORE'=109
'IMPORT'=110
'IN'=111
'INDEX'=112
'INDEXES'=113
'INNER'=114
'INPATH'=115
'INPUTFORMAT'=116
'INSERT'=117
'INTERSECT'=118
'INTERVAL'=119
'INTO'=120
'IS'=121
'ITEMS'=122
'JOIN'=123
'KEYS'=124
'LAST'=125
'LATERAL'=126
'LAZY'=127
'LEADING'=128
'LEFT'=129
'LIKE'=130
'LIMIT'=131
'LINES'=132
'LIST'=133
'LOAD'=134
'LOCAL'=135
'LOCATION'=136
'LOCK'=137
'LOCKS'=138
'LOGICAL'=139
'MACRO'=140
'MAP'=141
'MATCHED'=142
'MERGE'=143
'MSCK'=144
'NAMESPACE'=145
'NAMESPACES'=146
'NATURAL'=147
'NO'=148
'NULL'=150
'NULLS'=151
'OF'=152
'ON'=153
'ONLY'=154
'OPTION'=155
'OPTIONS'=156
'OR'=157
'ORDER'=158
'OUT'=159
'OUTER'=160
'OUTPUTFORMAT'=161
'OVER'=162
'OVERLAPS'=163
'OVERLAY'=164
'OVERWRITE'=165
'PARTITION'=166
'PARTITIONED'=167
'PARTITIONS'=168
'PERCENT'=169
'PIVOT'=170
'PLACING'=171
'POSITION'=172
'PRECEDING'=173
'PRIMARY'=174
'PRINCIPALS'=175
'PROPERTIES'=176
'PURGE'=177
'QUERY'=178
'RANGE'=179
'RECORDREADER'=180
'RECORDWRITER'=181
'RECOVER'=182
'REDUCE'=183
'REFERENCES'=184
'REFRESH'=185
'RENAME'=186
'REPAIR'=187
'REPLACE'=188
'RESET'=189
'RESTRICT'=190
'REVOKE'=191
'RIGHT'=192
'ROLE'=194
'ROLES'=195
'ROLLBACK'=196
'ROLLUP'=197
'ROW'=198
'ROWS'=199
'SCHEMA'=200
'SELECT'=201
'SEMI'=202
'SEPARATED'=203
'SERDE'=204
'SERDEPROPERTIES'=205
'SESSION_USER'=206
'SET'=207
'MINUS'=208
'SETS'=209
'SHOW'=210
'SKEWED'=211
'SOME'=212
'SORT'=213
'SORTED'=214
'START'=215
'STATISTICS'=216
'STORED'=217
'STRATIFY'=218
'STRUCT'=219
'SUBSTR'=220
'SUBSTRING'=221
'TABLE'=222
'TABLES'=223
'TABLESAMPLE'=224
'TBLPROPERTIES'=225
'TERMINATED'=227
'THEN'=228
'TIME'=229
'TO'=230
'TOUCH'=231
'TRAILING'=232
'TRANSACTION'=233
'TRANSACTIONS'=234
'TRANSFORM'=235
'TRIM'=236
'TRUE'=237
'TRUNCATE'=238
'TYPE'=239
'UNARCHIVE'=240
'UNBOUNDED'=241
'UNCACHE'=242
'UNION'=243
'UNIQUE'=244
'UNKNOWN'=245
'UNLOCK'=246
'UNSET'=247
'UPDATE'=248
'USE'=249
'USER'=250
'USING'=251
'VALUES'=252
'VIEW'=253
'VIEWS'=254
'WHEN'=255
'WHERE'=256
'WINDOW'=257
'WITH'=258
'ZONE'=259
'<=>'=261
'<>'=262
'!='=263
'<'=264
'>'=266
'+'=268
'-'=269
'*'=270
'/'=271
'%'=272
'~'=273
'&'=274
'|'=275
'||'=276
'^'=277
';'=278

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,741 @@
SEMICOLON=1
LEFT_PAREN=2
RIGHT_PAREN=3
COMMA=4
DOT=5
LEFT_BRACKET=6
RIGHT_BRACKET=7
KW_ADD=8
KW_AFTER=9
KW_ALL=10
KW_ALTER=11
KW_ALWAYS=12
KW_ANALYZE=13
KW_AND=14
KW_ANTI=15
KW_ANY=16
KW_ANY_VALUE=17
KW_ARCHIVE=18
KW_ARRAY=19
KW_AS=20
KW_ASC=21
KW_AT=22
KW_AUTHORIZATION=23
KW_BETWEEN=24
KW_BIGINT=25
KW_BINARY=26
KW_BOOLEAN=27
KW_BOTH=28
KW_BUCKET=29
KW_BUCKETS=30
KW_BY=31
KW_BYTE=32
KW_CACHE=33
KW_CASCADE=34
KW_CASE=35
KW_CAST=36
KW_CATALOG=37
KW_CATALOGS=38
KW_CHANGE=39
KW_CHAR=40
KW_CHARACTER=41
KW_CHECK=42
KW_CLEAR=43
KW_CLUSTER=44
KW_CLUSTERED=45
KW_CODEGEN=46
KW_COLLATE=47
KW_COLLECTION=48
KW_COLUMN=49
KW_COLUMNS=50
KW_COMMENT=51
KW_COMMIT=52
KW_COMPACT=53
KW_COMPACTIONS=54
KW_COMPUTE=55
KW_CONCATENATE=56
KW_CONSTRAINT=57
KW_COST=58
KW_CREATE=59
KW_CROSS=60
KW_CUBE=61
KW_CURRENT=62
KW_CURRENT_DATE=63
KW_CURRENT_TIME=64
KW_CURRENT_TIMESTAMP=65
KW_CURRENT_USER=66
KW_DAY=67
KW_DAYS=68
KW_DAYOFYEAR=69
KW_DATA=70
KW_DATE=71
KW_DATABASE=72
KW_DATABASES=73
KW_DATEADD=74
KW_DATE_ADD=75
KW_DATEDIFF=76
KW_DATE_DIFF=77
KW_DBPROPERTIES=78
KW_DEC=79
KW_DECIMAL=80
KW_DECLARE=81
KW_DEFAULT=82
KW_DEFINED=83
KW_DELETE=84
KW_DELIMITED=85
KW_DESC=86
KW_DESCRIBE=87
KW_DFS=88
KW_DIRECTORIES=89
KW_DIRECTORY=90
KW_DISTINCT=91
KW_DISTRIBUTE=92
KW_DIV=93
KW_DOUBLE=94
KW_DROP=95
KW_ELSE=96
KW_END=97
KW_ESCAPE=98
KW_ESCAPED=99
KW_EXCEPT=100
KW_EXCHANGE=101
KW_EXCLUDE=102
KW_EXISTS=103
KW_EXPLAIN=104
KW_EXPORT=105
KW_EXTENDED=106
KW_EXTERNAL=107
KW_EXTRACT=108
KW_FALSE=109
KW_FETCH=110
KW_FIELDS=111
KW_FILTER=112
KW_FILEFORMAT=113
KW_FIRST=114
KW_FLOAT=115
KW_FOLLOWING=116
KW_FOR=117
KW_FOREIGN=118
KW_FORMAT=119
KW_FORMATTED=120
KW_FROM=121
KW_FULL=122
KW_FUNCTION=123
KW_FUNCTIONS=124
KW_GENERATED=125
KW_GLOBAL=126
KW_GRANT=127
KW_GROUP=128
KW_GROUPING=129
KW_HAVING=130
KW_BINARY_HEX=131
KW_HOUR=132
KW_HOURS=133
KW_IDENTIFIER_KW=134
KW_IF=135
KW_IGNORE=136
KW_IMPORT=137
KW_IN=138
KW_INCLUDE=139
KW_INDEX=140
KW_INDEXES=141
KW_INNER=142
KW_INPATH=143
KW_INPUTFORMAT=144
KW_INSERT=145
KW_INTERSECT=146
KW_INTERVAL=147
KW_INT=148
KW_INTEGER=149
KW_INTO=150
KW_IS=151
KW_ITEMS=152
KW_JOIN=153
KW_KEYS=154
KW_LAST=155
KW_LATERAL=156
KW_LAZY=157
KW_LEADING=158
KW_LEFT=159
KW_LIKE=160
KW_ILIKE=161
KW_LIMIT=162
KW_LINES=163
KW_LIST=164
KW_LOAD=165
KW_LOCAL=166
KW_LOCATION=167
KW_LOCK=168
KW_LOCKS=169
KW_LOGICAL=170
KW_LONG=171
KW_MACRO=172
KW_MAP=173
KW_MATCHED=174
KW_MERGE=175
KW_MICROSECOND=176
KW_MICROSECONDS=177
KW_MILLISECOND=178
KW_MILLISECONDS=179
KW_MINUTE=180
KW_MINUTES=181
KW_MONTH=182
KW_MONTHS=183
KW_MSCK=184
KW_NAME=185
KW_NAMESPACE=186
KW_NAMESPACES=187
KW_NANOSECOND=188
KW_NANOSECONDS=189
KW_NATURAL=190
KW_NO=191
KW_NOT=192
KW_NULL=193
KW_NULLS=194
KW_NUMERIC=195
KW_OF=196
KW_OFFSET=197
KW_ON=198
KW_ONLY=199
KW_OPTION=200
KW_OPTIONS=201
KW_OR=202
KW_ORDER=203
KW_OUT=204
KW_OUTER=205
KW_OUTPUTFORMAT=206
KW_OVER=207
KW_OVERLAPS=208
KW_OVERLAY=209
KW_OVERWRITE=210
KW_PARTITION=211
KW_PARTITIONED=212
KW_PARTITIONS=213
KW_PERCENTILE_CONT=214
KW_PERCENTILE_DISC=215
KW_PERCENTLIT=216
KW_PIVOT=217
KW_PLACING=218
KW_POSITION=219
KW_PRECEDING=220
KW_PRIMARY=221
KW_PRINCIPALS=222
KW_PROPERTIES=223
KW_PURGE=224
KW_QUARTER=225
KW_QUERY=226
KW_RANGE=227
KW_REAL=228
KW_RECORDREADER=229
KW_RECORDWRITER=230
KW_RECOVER=231
KW_REDUCE=232
KW_REFERENCES=233
KW_REFRESH=234
KW_RENAME=235
KW_REPAIR=236
KW_REPEATABLE=237
KW_REPLACE=238
KW_RESET=239
KW_RESPECT=240
KW_RESTRICT=241
KW_REVOKE=242
KW_RIGHT=243
KW_RLIKE=244
KW_ROLE=245
KW_ROLES=246
KW_ROLLBACK=247
KW_ROLLUP=248
KW_ROW=249
KW_ROWS=250
KW_SECOND=251
KW_SECONDS=252
KW_SCHEMA=253
KW_SCHEMAS=254
KW_SELECT=255
KW_SEMI=256
KW_SEPARATED=257
KW_SERDE=258
KW_SERDEPROPERTIES=259
KW_SESSION_USER=260
KW_SET=261
KW_SETMINUS=262
KW_SETS=263
KW_SHORT=264
KW_SHOW=265
KW_SINGLE=266
KW_SKEWED=267
KW_SMALLINT=268
KW_SOME=269
KW_SORT=270
KW_SORTED=271
KW_SOURCE=272
KW_START=273
KW_STATISTICS=274
KW_STORED=275
KW_STRATIFY=276
KW_STRING=277
KW_STRUCT=278
KW_SUBSTR=279
KW_SUBSTRING=280
KW_SYNC=281
KW_SYSTEM_TIME=282
KW_SYSTEM_VERSION=283
KW_TABLE=284
KW_TABLES=285
KW_TABLESAMPLE=286
KW_TARGET=287
KW_TBLPROPERTIES=288
KW_TEMPORARY=289
KW_TERMINATED=290
KW_THEN=291
KW_TIME=292
KW_TIMEDIFF=293
KW_TIMESTAMP=294
KW_TIMESTAMP_LTZ=295
KW_TIMESTAMP_NTZ=296
KW_TIMESTAMPADD=297
KW_TIMESTAMPDIFF=298
KW_TINYINT=299
KW_TO=300
KW_TOUCH=301
KW_TRAILING=302
KW_TRANSACTION=303
KW_TRANSACTIONS=304
KW_TRANSFORM=305
KW_TRIM=306
KW_TRUE=307
KW_TRUNCATE=308
KW_TRY_CAST=309
KW_TYPE=310
KW_UNARCHIVE=311
KW_UNBOUNDED=312
KW_UNCACHE=313
KW_UNION=314
KW_UNIQUE=315
KW_UNKNOWN=316
KW_UNLOCK=317
KW_UNPIVOT=318
KW_UNSET=319
KW_UPDATE=320
KW_USE=321
KW_USER=322
KW_USING=323
KW_VALUES=324
KW_VARCHAR=325
KW_VAR=326
KW_VARIABLE=327
KW_VERSION=328
KW_VIEW=329
KW_VIEWS=330
KW_VOID=331
KW_WEEK=332
KW_WEEKS=333
KW_WHEN=334
KW_WHERE=335
KW_WINDOW=336
KW_WITH=337
KW_WITHIN=338
KW_YEAR=339
KW_YEARS=340
KW_ZONE=341
EQ=342
NSEQ=343
NEQ=344
NEQJ=345
LT=346
LTE=347
GT=348
GTE=349
PLUS=350
MINUS=351
ASTERISK=352
SLASH=353
PERCENT=354
TILDE=355
AMPERSAND=356
PIPE=357
CONCAT_PIPE=358
HAT=359
COLON=360
ARROW=361
FAT_ARROW=362
HENT_START=363
HENT_END=364
QUESTION=365
STRING_LITERAL=366
DOUBLEQUOTED_STRING=367
BIGINT_LITERAL=368
SMALLINT_LITERAL=369
TINYINT_LITERAL=370
INTEGER_VALUE=371
EXPONENT_VALUE=372
DECIMAL_VALUE=373
FLOAT_LITERAL=374
DOUBLE_LITERAL=375
BIGDECIMAL_LITERAL=376
IDENTIFIER=377
BACKQUOTED_IDENTIFIER=378
SIMPLE_COMMENT=379
BRACKETED_COMMENT=380
WS=381
UNRECOGNIZED=382
';'=1
'('=2
')'=3
','=4
'.'=5
'['=6
']'=7
'ADD'=8
'AFTER'=9
'ALL'=10
'ALTER'=11
'ALWAYS'=12
'ANALYZE'=13
'AND'=14
'ANTI'=15
'ANY'=16
'ANY_VALUE'=17
'ARCHIVE'=18
'ARRAY'=19
'AS'=20
'ASC'=21
'AT'=22
'AUTHORIZATION'=23
'BETWEEN'=24
'BIGINT'=25
'BINARY'=26
'BOOLEAN'=27
'BOTH'=28
'BUCKET'=29
'BUCKETS'=30
'BY'=31
'BYTE'=32
'CACHE'=33
'CASCADE'=34
'CASE'=35
'CAST'=36
'CATALOG'=37
'CATALOGS'=38
'CHANGE'=39
'CHAR'=40
'CHARACTER'=41
'CHECK'=42
'CLEAR'=43
'CLUSTER'=44
'CLUSTERED'=45
'CODEGEN'=46
'COLLATE'=47
'COLLECTION'=48
'COLUMN'=49
'COLUMNS'=50
'COMMENT'=51
'COMMIT'=52
'COMPACT'=53
'COMPACTIONS'=54
'COMPUTE'=55
'CONCATENATE'=56
'CONSTRAINT'=57
'COST'=58
'CREATE'=59
'CROSS'=60
'CUBE'=61
'CURRENT'=62
'CURRENT_DATE'=63
'CURRENT_TIME'=64
'CURRENT_TIMESTAMP'=65
'CURRENT_USER'=66
'DAY'=67
'DAYS'=68
'DAYOFYEAR'=69
'DATA'=70
'DATE'=71
'DATABASE'=72
'DATABASES'=73
'DATEADD'=74
'DATE_ADD'=75
'DATEDIFF'=76
'DATE_DIFF'=77
'DBPROPERTIES'=78
'DEC'=79
'DECIMAL'=80
'DECLARE'=81
'DEFAULT'=82
'DEFINED'=83
'DELETE'=84
'DELIMITED'=85
'DESC'=86
'DESCRIBE'=87
'DFS'=88
'DIRECTORIES'=89
'DIRECTORY'=90
'DISTINCT'=91
'DISTRIBUTE'=92
'DIV'=93
'DOUBLE'=94
'DROP'=95
'ELSE'=96
'END'=97
'ESCAPE'=98
'ESCAPED'=99
'EXCEPT'=100
'EXCHANGE'=101
'EXCLUDE'=102
'EXISTS'=103
'EXPLAIN'=104
'EXPORT'=105
'EXTENDED'=106
'EXTERNAL'=107
'EXTRACT'=108
'FALSE'=109
'FETCH'=110
'FIELDS'=111
'FILTER'=112
'FILEFORMAT'=113
'FIRST'=114
'FLOAT'=115
'FOLLOWING'=116
'FOR'=117
'FOREIGN'=118
'FORMAT'=119
'FORMATTED'=120
'FROM'=121
'FULL'=122
'FUNCTION'=123
'FUNCTIONS'=124
'GENERATED'=125
'GLOBAL'=126
'GRANT'=127
'GROUP'=128
'GROUPING'=129
'HAVING'=130
'X'=131
'HOUR'=132
'HOURS'=133
'IDENTIFIER'=134
'IF'=135
'IGNORE'=136
'IMPORT'=137
'IN'=138
'INCLUDE'=139
'INDEX'=140
'INDEXES'=141
'INNER'=142
'INPATH'=143
'INPUTFORMAT'=144
'INSERT'=145
'INTERSECT'=146
'INTERVAL'=147
'INT'=148
'INTEGER'=149
'INTO'=150
'IS'=151
'ITEMS'=152
'JOIN'=153
'KEYS'=154
'LAST'=155
'LATERAL'=156
'LAZY'=157
'LEADING'=158
'LEFT'=159
'LIKE'=160
'ILIKE'=161
'LIMIT'=162
'LINES'=163
'LIST'=164
'LOAD'=165
'LOCAL'=166
'LOCATION'=167
'LOCK'=168
'LOCKS'=169
'LOGICAL'=170
'LONG'=171
'MACRO'=172
'MAP'=173
'MATCHED'=174
'MERGE'=175
'MICROSECOND'=176
'MICROSECONDS'=177
'MILLISECOND'=178
'MILLISECONDS'=179
'MINUTE'=180
'MINUTES'=181
'MONTH'=182
'MONTHS'=183
'MSCK'=184
'NAME'=185
'NAMESPACE'=186
'NAMESPACES'=187
'NANOSECOND'=188
'NANOSECONDS'=189
'NATURAL'=190
'NO'=191
'NULL'=193
'NULLS'=194
'NUMERIC'=195
'OF'=196
'OFFSET'=197
'ON'=198
'ONLY'=199
'OPTION'=200
'OPTIONS'=201
'OR'=202
'ORDER'=203
'OUT'=204
'OUTER'=205
'OUTPUTFORMAT'=206
'OVER'=207
'OVERLAPS'=208
'OVERLAY'=209
'OVERWRITE'=210
'PARTITION'=211
'PARTITIONED'=212
'PARTITIONS'=213
'PERCENTILE_CONT'=214
'PERCENTILE_DISC'=215
'PERCENT'=216
'PIVOT'=217
'PLACING'=218
'POSITION'=219
'PRECEDING'=220
'PRIMARY'=221
'PRINCIPALS'=222
'PROPERTIES'=223
'PURGE'=224
'QUARTER'=225
'QUERY'=226
'RANGE'=227
'REAL'=228
'RECORDREADER'=229
'RECORDWRITER'=230
'RECOVER'=231
'REDUCE'=232
'REFERENCES'=233
'REFRESH'=234
'RENAME'=235
'REPAIR'=236
'REPEATABLE'=237
'REPLACE'=238
'RESET'=239
'RESPECT'=240
'RESTRICT'=241
'REVOKE'=242
'RIGHT'=243
'ROLE'=245
'ROLES'=246
'ROLLBACK'=247
'ROLLUP'=248
'ROW'=249
'ROWS'=250
'SECOND'=251
'SECONDS'=252
'SCHEMA'=253
'SCHEMAS'=254
'SELECT'=255
'SEMI'=256
'SEPARATED'=257
'SERDE'=258
'SERDEPROPERTIES'=259
'SESSION_USER'=260
'SET'=261
'MINUS'=262
'SETS'=263
'SHORT'=264
'SHOW'=265
'SINGLE'=266
'SKEWED'=267
'SMALLINT'=268
'SOME'=269
'SORT'=270
'SORTED'=271
'SOURCE'=272
'START'=273
'STATISTICS'=274
'STORED'=275
'STRATIFY'=276
'STRING'=277
'STRUCT'=278
'SUBSTR'=279
'SUBSTRING'=280
'SYNC'=281
'SYSTEM_TIME'=282
'SYSTEM_VERSION'=283
'TABLE'=284
'TABLES'=285
'TABLESAMPLE'=286
'TARGET'=287
'TBLPROPERTIES'=288
'TERMINATED'=290
'THEN'=291
'TIME'=292
'TIMEDIFF'=293
'TIMESTAMP'=294
'TIMESTAMP_LTZ'=295
'TIMESTAMP_NTZ'=296
'TIMESTAMPADD'=297
'TIMESTAMPDIFF'=298
'TINYINT'=299
'TO'=300
'TOUCH'=301
'TRAILING'=302
'TRANSACTION'=303
'TRANSACTIONS'=304
'TRANSFORM'=305
'TRIM'=306
'TRUE'=307
'TRUNCATE'=308
'TRY_CAST'=309
'TYPE'=310
'UNARCHIVE'=311
'UNBOUNDED'=312
'UNCACHE'=313
'UNION'=314
'UNIQUE'=315
'UNKNOWN'=316
'UNLOCK'=317
'UNPIVOT'=318
'UNSET'=319
'UPDATE'=320
'USE'=321
'USER'=322
'USING'=323
'VALUES'=324
'VARCHAR'=325
'VAR'=326
'VARIABLE'=327
'VERSION'=328
'VIEW'=329
'VIEWS'=330
'VOID'=331
'WEEK'=332
'WEEKS'=333
'WHEN'=334
'WHERE'=335
'WINDOW'=336
'WITH'=337
'WITHIN'=338
'YEAR'=339
'YEARS'=340
'ZONE'=341
'<=>'=343
'<>'=344
'!='=345
'<'=346
'>'=348
'+'=350
'-'=351
'*'=352
'/'=353
'%'=354
'~'=355
'&'=356
'|'=357
'||'=358
'^'=359
':'=360
'->'=361
'=>'=362
'/*+'=363
'*/'=364
'?'=365

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
import { SparkSqlListener } from '../../../src/lib/spark/SparkSqlListener'; import { SparkSqlParserListener } from '../../../src/lib/spark/SparkSqlParserListener';
import SparkSQL from '../../../src/parser/spark'; import SparkSQL from '../../../src/parser/spark';
describe('Spark SQL Listener Tests', () => { describe('Spark SQL Listener Tests', () => {
@ -8,10 +8,10 @@ describe('Spark SQL Listener Tests', () => {
const parserTree = parser.parse(sql); const parserTree = parser.parse(sql);
test('Listener enterTableName', () => { test('Listener exitRelationPrimary', () => {
let result = ''; let result = '';
class MyListener implements SparkSqlListener { class MyListener implements SparkSqlParserListener {
enterTableName = (ctx): void => { exitRelationPrimary = (ctx): void => {
result = ctx.text.toLowerCase(); result = ctx.text.toLowerCase();
} }
} }

View File

@ -1,324 +0,0 @@
import SparkSQL from '../../../src/parser/spark';
const error = console.log.bind(console, '***** error\n');
const validateTest = (sqls) => {
const parser = new SparkSQL();
sqls.forEach((sql, i) => {
const result = parser.validate(sql);
if (result.length !== 0) {
error(i, sql);
error(result);
}
expect(result.find(i => i.message)).toBeUndefined();
});
};
describe('Spark SQL Syntax Tests', () => {
test('ALTER Statement', () => {
const sqls = [
`ALTER DATABASE inventory SET DBPROPERTIES ('Edited-by' = 'John', 'Edit-date' = '01/01/2001');`,
`ALTER TABLE Student RENAME TO StudentInfo;`,
`ALTER VIEW tempdb1.v1 RENAME TO tempdb1.v2;`,
];
validateTest(sqls);
});
test('CREATE Statement', () => {
const sqls = [
`CREATE DATABASE IF NOT EXISTS customer_db;`,
`CREATE FUNCTION simple_udf AS 'SimpleUdf'
USING JAR '/tmp/SimpleUdf.jar';`,
`CREATE OR REPLACE FUNCTION simple_udf AS 'SimpleUdfR'
USING JAR '/tmp/SimpleUdfR.jar';`,
`CREATE TABLE student (id INT, name STRING, age INT) USING CSV;`,
`CREATE TABLE student (id INT, name STRING, age INT)
USING CSV
PARTITIONED BY (age)
CLUSTERED BY (Id) INTO 4 buckets;`,
`CREATE OR REPLACE VIEW experienced_employee
(ID COMMENT 'Unique identification number', Name)
COMMENT 'View for experienced employees'
AS SELECT id, name FROM all_employee
WHERE working_years > 5;`,
`CREATE GLOBAL TEMPORARY VIEW IF NOT EXISTS subscribed_movies
AS SELECT mo.member_id, mb.full_name, mo.movie_title
FROM movies AS mo INNER JOIN members AS mb
ON mo.member_id = mb.id;`,
];
validateTest(sqls);
});
test('DROP Statement', () => {
const sqls = [
`DROP DATABASE inventory_db CASCADE;`,
`DROP DATABASE IF EXISTS inventory_db CASCADE;`,
`DROP FUNCTION test_avg;`,
`DROP TEMPORARY FUNCTION IF EXISTS test_avg;`,
`DROP TABLE userdb.employeetable;`,
`DROP TABLE IF EXISTS employeetable;`,
`DROP VIEW userdb.employeeView;`,
`DROP VIEW IF EXISTS employeeView;`,
];
validateTest(sqls);
});
test('TRUNCATE Statement', () => {
const sqls = [
`TRUNCATE TABLE Student partition(age=10);`,
];
validateTest(sqls);
});
test('REPAIR TABLE Statement', () => {
const sqls = [
`MSCK REPAIR TABLE t1;`,
];
validateTest(sqls);
});
test('USE Database Statement', () => {
const sqls = [
`USE userdb;`,
];
validateTest(sqls);
});
test('INSERT Statement', () => {
const sqls = [
`INSERT INTO students VALUES
('Amy Smith', '123 Park Ave, San Jose', 111111);`,
`INSERT INTO students TABLE visiting_students;`,
`INSERT OVERWRITE students VALUES
('Ashua Hill', '456 Erica Ct, Cupertino', 111111),
('Brian Reed', '723 Kern Ave, Palo Alto', 222222);`,
`INSERT OVERWRITE students TABLE visiting_students;`,
`INSERT OVERWRITE students
FROM applicants SELECT name, address, id applicants WHERE qualified = true;`,
`INSERT OVERWRITE DIRECTORY '/tmp/destination'
USING parquet
OPTIONS (col1 1, col2 2, col3 'test')
SELECT * FROM test_table;`,
`INSERT OVERWRITE DIRECTORY
USING parquet
OPTIONS ('path' '/tmp/destination', col1 1, col2 2, col3 'test')
SELECT * FROM test_table;`,
`INSERT OVERWRITE LOCAL DIRECTORY '/tmp/destination'
STORED AS orc
SELECT * FROM test_table;`,
`INSERT OVERWRITE LOCAL DIRECTORY '/tmp/destination'
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
SELECT * FROM test_table;`,
];
validateTest(sqls);
});
test('LOAD Statement', () => {
const sqls = [
`LOAD DATA LOCAL INPATH '/user/hive/warehouse/students' OVERWRITE INTO TABLE test_load;`,
`LOAD DATA LOCAL INPATH '/user/hive/warehouse/test_partition/c2=2/c3=3'
OVERWRITE INTO TABLE test_load_partition PARTITION (c2=2, c3=3);`,
];
validateTest(sqls);
});
test('SELECT WHERE, GROUP BY, HAVING, ORDER BY Statement', () => {
const sqls = [
`SELECT * FROM person WHERE id > 200 ORDER BY id;`,
`SELECT * FROM person AS parent
WHERE EXISTS (
SELECT 1 FROM person AS child
WHERE parent.id = child.id AND child.age IS NULL
);`,
`SELECT id, sum(quantity) FROM dealer GROUP BY id ORDER BY id;`,
`SELECT city, car_model, sum(quantity) AS sum FROM dealer
GROUP BY city, car_model WITH CUBE
ORDER BY city, car_model;`,
`SELECT city, sum(quantity) AS sum FROM dealer GROUP BY city HAVING city = 'Fremont';`,
`SELECT sum(quantity) AS sum FROM dealer HAVING sum(quantity) > 10;`,
`SELECT name, age FROM person ORDER BY age;`,
`SELECT * FROM person ORDER BY name ASC, age DESC;`,
];
validateTest(sqls);
});
test('SELECT SORT BY, CLUSTER BY, DISTRIBUTE BY, LIMIT Statement', () => {
const sqls = [
`SELECT /*+ REPARTITION(zip_code) */ age, name, zip_code FROM person SORT BY age DESC NULLS FIRST;`,
`SELECT /*+ REPARTITION(zip_code) */ name, age, zip_code FROM person
SORT BY name ASC, age DESC;`,
`SELECT age, name FROM person CLUSTER BY age;`,
`SELECT age, name FROM person DISTRIBUTE BY age;`,
`SELECT name, age FROM person ORDER BY name LIMIT length('SPARK');`,
`SELECT name, age FROM person ORDER BY name LIMIT length('SPARK');`,
];
validateTest(sqls);
});
test('SELECT Common Table Expression Statement', () => {
const sqls = [
`SELECT * FROM t WHERE x = 1 AND y = 2;`,
`SELECT max(c) FROM (
WITH t(c) AS (SELECT 1)
SELECT * FROM t
);`,
`CREATE VIEW v AS
WITH t(a, b, c, d) AS (SELECT 1, 2, 3, 4)
SELECT * FROM t;`,
`SET spark.sql.legacy.ctePrecedencePolicy = CORRECTED;
WITH
t AS (SELECT 1),
t2 AS (
WITH t AS (SELECT 2)
SELECT * FROM t
)
SELECT * FROM t2;`,
];
validateTest(sqls);
});
test('SELECT HINTS, INLINE TABLE, JOIN, LIKE Predicate, Set Operators, Sampling Queries Statement', () => {
const sqls = [
`SELECT /*+ REPARTITION_BY_RANGE(3, c) */ * FROM t;`,
`SELECT /*+ BROADCAST(t1), MERGE(t1, t2) */ * FROM t1 INNER JOIN t2 ON t1.key = t2.key;`,
`SELECT * FROM VALUES ("one", array(0, 1)), ("two", array(2, 3)) AS data(a, b);`,
`SELECT * FROM employee ANTI JOIN department ON employee.deptno = department.deptno;`,
`SELECT * FROM person WHERE name LIKE '%\\_%';`,
`SELECT * FROM person WHERE name LIKE '%$_%' ESCAPE '$';`,
`SELECT c FROM number1 EXCEPT ALL (SELECT c FROM number2);`,
`(SELECT c FROM number1) INTERSECT ALL (SELECT c FROM number2);`,
`SELECT c FROM number1 UNION ALL (SELECT c FROM number2);`,
`SELECT * FROM test TABLESAMPLE (5 ROWS);`,
`SELECT * FROM test TABLESAMPLE (BUCKET 4 OUT OF 10);`,
];
validateTest(sqls);
});
test('SELECT Table-valued Functions, Window Functions, CASE, LATERAL VIEW, PIVOT Statement', () => {
const sqls = [
`SELECT * FROM range(6 + cos(3));`,
`SELECT inline(array(struct(1, 'a'), struct(2, 'b')));`,
`SELECT * FROM test LATERAL VIEW explode (ARRAY(3,4)) AS c2;`,
`SELECT name, dept, RANK() OVER (PARTITION BY dept ORDER BY salary) AS rank FROM employees;`,
`SELECT name, dept, salary, MIN(salary) OVER (PARTITION BY dept ORDER BY salary) AS min
FROM employees;`,
`SELECT id, CASE id WHEN 100 then 'bigger' WHEN id > 300 THEN '300' ELSE 'small' END FROM person;`,
`SELECT * FROM person
WHERE
CASE 1 = 1
WHEN 100 THEN 'big'
WHEN 200 THEN 'bigger'
WHEN 300 THEN 'biggest'
ELSE 'small'
END = 'small';`,
`SELECT * FROM person
LATERAL VIEW EXPLODE(ARRAY(30, 60)) tabelName AS c_age
LATERAL VIEW EXPLODE(ARRAY(40, 80)) AS d_age;`,
`SELECT * FROM person
LATERAL VIEW OUTER EXPLODE(ARRAY()) tabelName AS c_age;`,
`SELECT * FROM person
PIVOT (
SUM(age) AS a, AVG(class) AS c
FOR name IN ('John' AS john, 'Mike' AS mike)
);`,
];
validateTest(sqls);
});
test('EXPLAIN Statement', () => {
const sqls = [
`EXPLAIN select k, sum(v) from values (1, 2), (1, 3) t(k, v) group by k;`,
`EXPLAIN EXTENDED select k, sum(v) from values (1, 2), (1, 3) t(k, v) group by k;`,
];
validateTest(sqls);
});
test('ANALYZE Statement', () => {
const sqls = [
`ANALYZE TABLE students COMPUTE STATISTICS NOSCAN;`,
`ANALYZE TABLE students COMPUTE STATISTICS FOR COLUMNS name;`,
];
validateTest(sqls);
});
test('CACHE TABLE, UNCACHE TABLE, CLEAR CACHE, REFRESH TABLE, REFRESH Statement', () => {
const sqls = [
`CACHE TABLE testCache OPTIONS ('storageLevel' 'DISK_ONLY') SELECT * FROM testData;`,
`UNCACHE TABLE t1;`,
`CLEAR CACHE;`,
`REFRESH TABLE tbl1;`,
`REFRESH "hdfs://path/to/table";`,
];
validateTest(sqls);
});
test('DESCRIBE DATABASE, TABLE, FUNCTION, QUERY Statement', () => {
const sqls = [
`DESCRIBE DATABASE employees;`,
`DESCRIBE DATABASE EXTENDED employees;`,
`DESCRIBE TABLE customer;`,
`DESCRIBE TABLE EXTENDED customer PARTITION (state = 'AR');`,
`DESC FUNCTION abs;`,
`DESC FUNCTION max;`,
`DESC FUNCTION EXTENDED explode;`,
`DESCRIBE QUERY SELECT age, sum(age) FROM person GROUP BY age;`,
`DESCRIBE QUERY WITH all_names_cte
AS (SELECT name from person) SELECT * FROM all_names_cte;`,
`DESC QUERY VALUES(100, 'John', 10000.20D) AS employee(id, name, salary);`,
];
validateTest(sqls);
});
test('SHOW COLUMNS, CREATE TABLE, DATABASES, FUNCTIONS, PARTITIONS, TABLE EXTENDED, TABLES, TBLPROPERTIES, VIEWS Statement', () => {
const sqls = [
`SHOW COLUMNS IN customer;`,
`SHOW CREATE TABLE test;`,
`SHOW DATABASES LIKE 'pay*';`,
`SHOW SCHEMAS;`,
`SHOW FUNCTIONS trim;`,
`SHOW SYSTEM FUNCTIONS salesdb.max;`,
`SHOW FUNCTIONS LIKE 't[a-z][a-z][a-z]';`,
`SHOW PARTITIONS customer;`,
`SHOW PARTITIONS customer PARTITION (city = 'San Jose');`,
`SHOW TABLE EXTENDED LIKE 'employee';`,
`SHOW TABLES;`,
`SHOW TABLES FROM userdb;`,
`SHOW TABLES LIKE 'sam*|suj';`,
`SHOW TBLPROPERTIES customer ('created.date');`,
`SHOW VIEWS;`,
`SHOW VIEWS LIKE 'sam|suj|temp*';`,
];
validateTest(sqls);
});
test('SET, RESET Statement', () => {
const sqls = [
`SET spark.sql.variable.substitute=false;`,
`RESET`,
];
validateTest(sqls);
});
test('ADD, LIST Statement', () => {
const sqls = [
`ADD FILE /tmp/test;`,
`ADD FILE "/path/to/some/directory";`,
`ADD JAR /tmp/test.jar;`,
`ADD JAR '/some/other.jar';`,
`LIST FILE;`,
`LIST FILE /tmp/test /some/random/file /another/random/file;`,
`LIST JAR;`,
`LIST JAR /tmp/test.jar /some/random.jar /another/random.jar;`,
];
validateTest(sqls);
});
test('Customizing variables with the ${} symbol', () => {
const sqls = [
`select * from \${tb};`,
`select a as \${b_} from tb_test;`,
'select a as ${bb} from ${tt}',
];
validateTest(sqls);
});
});

View File

@ -1,5 +1,5 @@
import { AbstractParseTreeVisitor } from 'antlr4ts/tree/AbstractParseTreeVisitor'; import { AbstractParseTreeVisitor } from 'antlr4ts/tree/AbstractParseTreeVisitor';
import { SparkSqlVisitor } from '../../../src/lib/spark/SparkSqlVisitor'; import { SparkSqlParserVisitor } from '../../../src/lib/spark/SparkSqlParserVisitor';
import SparkSQL from '../../../src/parser/spark'; import SparkSQL from '../../../src/parser/spark';
describe('Spark SQL Visitor Tests', () => { describe('Spark SQL Visitor Tests', () => {
@ -11,13 +11,13 @@ describe('Spark SQL Visitor Tests', () => {
console.log('Parse error:', error); console.log('Parse error:', error);
}); });
test('Visitor visitTableName', () => { test('Visitor visitRelationPrimary', () => {
let result = ''; let result = '';
class MyVisitor extends AbstractParseTreeVisitor<any> implements SparkSqlVisitor<any> { class MyVisitor extends AbstractParseTreeVisitor<any> implements SparkSqlParserVisitor<any> {
protected defaultResult() { protected defaultResult() {
return result; return result;
} }
visitTableName = (ctx): void => { visitRelationPrimary = (ctx): void => {
result = ctx.text.toLowerCase(); result = ctx.text.toLowerCase();
} }
} }