feat: update alter and drop statements (#94)

* feat: add UNIQUE keyword

* feat: support ifExist, addConstraint, dropConstraint and addUnique of alter table statement

* feat: support ifExist, addConstraint, dropConstraint and addUnique of alter table statement

* tests: add tests of alter statements

* docs: add comments for sql syntax

* build: optimize promote of build/antlr4

* ci: add max_old_space_size

* ci: add max_old_space_size

* ci: update tests script
This commit is contained in:
Ziv 2023-05-17 10:00:35 +08:00 committed by GitHub
parent 10645087de
commit fbee70cde5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 4341 additions and 3802 deletions

View File

@ -62,20 +62,16 @@ jobs:
path: node_modules
key: node_modules-${{ hashFiles('**/package-temp-dir/yarn.lock') }}
- name: Test utils
run: npx jest test/utils
- name: Test FLinkSQL
run: npx jest test/parser/flinksql
- name: Test SparkSQL
run: npx jest test/parser/spark
- name: Test GenericSQL
run: npx jest test/parser/generic
- name: Test HiveSQL
run: npx jest test/parser/hive
- name: Test PGSQL
run: npx jest test/parser/pgsql
- name: Test PLSQL
run: npx jest test/parser/plsql
- name: Run Units Test
run: |
export NODE_OPTIONS="--max_old_space_size=4096"
npx jest test/utils
npx jest test/parser/flinksql
npx jest test/parser/spark
npx jest test/parser/generic
npx jest test/parser/hive
npx jest test/parser/pgsql
npx jest test/parser/plsql
build:
runs-on: ubuntu-latest
needs: [setup]

View File

@ -43,7 +43,7 @@ if (argv.all) { // build all: yarn antlr4 --all
if (supportedLanguage) {
compile(argv.lang);
} else {
console.error('Unsupported language: ' + argv.lang);
console.error('Invalid language: ' + argv.lang + ', supported languages:\n' + entry.join(', '));
}
} else {
console.error('Please to specify the language, just like: yarn antlr4 --lang flinksql');

View File

@ -265,6 +265,7 @@ CATALOGS: 'CATALOGS';
VIEWS: 'VIEWS';
JARS: 'JARS';
PRIMARY: 'PRIMARY';
UNIQUE: 'UNIQUE';
KEY: 'KEY';
PERIOD: 'PERIOD';
SYSTEM_TIME: 'SYSTEM_TIME';

View File

@ -277,19 +277,37 @@ jarFileName
;
// Alter statements
// Just for simple alter table statements,
// it only includes rename, set key, add constraint, drop constraint, add unique
alterTable
: ALTER TABLE uid (renameDefinition | setKeyValueDefinition)
: ALTER TABLE ifExists? uid (renameDefinition | setKeyValueDefinition | addConstraint | dropConstraint | addUnique)
;
renameDefinition
: RENAME TO uid
: RENAME uid? TO uid
;
setKeyValueDefinition
: SET tablePropertyList
;
addConstraint
: ADD CONSTRAINT constraintName PRIMARY KEY columnNameList notForced?
;
dropConstraint
: DROP CONSTRAINT constraintName
;
addUnique
: ADD UNIQUE columnNameList
;
notForced
: NOT ENFORCED
;
alertView
: ALTER VIEW uid (renameDefinition | AS queryStatement)
;

File diff suppressed because one or more lines are too long

View File

@ -254,78 +254,79 @@ CATALOGS=253
VIEWS=254
JARS=255
PRIMARY=256
KEY=257
PERIOD=258
SYSTEM_TIME=259
ENFORCED=260
METADATA=261
VIRTUAL=262
ZONE=263
STRING=264
ARRAY=265
MAP=266
CHAR=267
VARCHAR=268
BINARY=269
VARBINARY=270
BYTES=271
DECIMAL=272
DEC=273
NUMERIC=274
TINYINT=275
SMALLINT=276
INT=277
INTEGER=278
BIGINT=279
FLOAT=280
DOUBLE=281
DATE=282
TIME=283
TIMESTAMP=284
TIMESTAMP_LTZ=285
MULTISET=286
BOOLEAN=287
RAW=288
ROW=289
NULL=290
DATETIME=291
EQUAL_SYMBOL=292
GREATER_SYMBOL=293
LESS_SYMBOL=294
EXCLAMATION_SYMBOL=295
BIT_NOT_OP=296
BIT_OR_OP=297
BIT_AND_OP=298
BIT_XOR_OP=299
DOT=300
LS_BRACKET=301
RS_BRACKET=302
LR_BRACKET=303
RR_BRACKET=304
COMMA=305
SEMICOLON=306
AT_SIGN=307
SINGLE_QUOTE_SYMB=308
DOUBLE_QUOTE_SYMB=309
REVERSE_QUOTE_SYMB=310
COLON_SYMB=311
ASTERISK_SIGN=312
UNDERLINE_SIGN=313
HYPNEN_SIGN=314
ADD_SIGN=315
PENCENT_SIGN=316
DOUBLE_VERTICAL_SIGN=317
DOUBLE_HYPNEN_SIGN=318
SLASH_SIGN=319
DOT_ID=320
PLUS_DOT_ID=321
STRING_LITERAL=322
DIG_LITERAL=323
REAL_LITERAL=324
BIT_STRING=325
ID_LITERAL=326
PLUS_ID_LITERAL=327
FILE_PATH=328
UNIQUE=257
KEY=258
PERIOD=259
SYSTEM_TIME=260
ENFORCED=261
METADATA=262
VIRTUAL=263
ZONE=264
STRING=265
ARRAY=266
MAP=267
CHAR=268
VARCHAR=269
BINARY=270
VARBINARY=271
BYTES=272
DECIMAL=273
DEC=274
NUMERIC=275
TINYINT=276
SMALLINT=277
INT=278
INTEGER=279
BIGINT=280
FLOAT=281
DOUBLE=282
DATE=283
TIME=284
TIMESTAMP=285
TIMESTAMP_LTZ=286
MULTISET=287
BOOLEAN=288
RAW=289
ROW=290
NULL=291
DATETIME=292
EQUAL_SYMBOL=293
GREATER_SYMBOL=294
LESS_SYMBOL=295
EXCLAMATION_SYMBOL=296
BIT_NOT_OP=297
BIT_OR_OP=298
BIT_AND_OP=299
BIT_XOR_OP=300
DOT=301
LS_BRACKET=302
RS_BRACKET=303
LR_BRACKET=304
RR_BRACKET=305
COMMA=306
SEMICOLON=307
AT_SIGN=308
SINGLE_QUOTE_SYMB=309
DOUBLE_QUOTE_SYMB=310
REVERSE_QUOTE_SYMB=311
COLON_SYMB=312
ASTERISK_SIGN=313
UNDERLINE_SIGN=314
HYPNEN_SIGN=315
ADD_SIGN=316
PENCENT_SIGN=317
DOUBLE_VERTICAL_SIGN=318
DOUBLE_HYPNEN_SIGN=319
SLASH_SIGN=320
DOT_ID=321
PLUS_DOT_ID=322
STRING_LITERAL=323
DIG_LITERAL=324
REAL_LITERAL=325
BIT_STRING=326
ID_LITERAL=327
PLUS_ID_LITERAL=328
FILE_PATH=329
'SELECT'=4
'FROM'=5
'ADD'=6
@ -579,66 +580,67 @@ FILE_PATH=328
'VIEWS'=254
'JARS'=255
'PRIMARY'=256
'KEY'=257
'PERIOD'=258
'SYSTEM_TIME'=259
'ENFORCED'=260
'METADATA'=261
'VIRTUAL'=262
'ZONE'=263
'STRING'=264
'ARRAY'=265
'MAP'=266
'CHAR'=267
'VARCHAR'=268
'BINARY'=269
'VARBINARY'=270
'BYTES'=271
'DECIMAL'=272
'DEC'=273
'NUMERIC'=274
'TINYINT'=275
'SMALLINT'=276
'INT'=277
'INTEGER'=278
'BIGINT'=279
'FLOAT'=280
'DOUBLE'=281
'DATE'=282
'TIME'=283
'TIMESTAMP'=284
'TIMESTAMP_LTZ'=285
'MULTISET'=286
'BOOLEAN'=287
'RAW'=288
'ROW'=289
'NULL'=290
'DATETIME'=291
'='=292
'>'=293
'<'=294
'!'=295
'~'=296
'|'=297
'&'=298
'^'=299
'.'=300
'['=301
']'=302
'('=303
')'=304
','=305
';'=306
'@'=307
'\''=308
'"'=309
'`'=310
':'=311
'*'=312
'_'=313
'-'=314
'+'=315
'%'=316
'||'=317
'--'=318
'/'=319
'UNIQUE'=257
'KEY'=258
'PERIOD'=259
'SYSTEM_TIME'=260
'ENFORCED'=261
'METADATA'=262
'VIRTUAL'=263
'ZONE'=264
'STRING'=265
'ARRAY'=266
'MAP'=267
'CHAR'=268
'VARCHAR'=269
'BINARY'=270
'VARBINARY'=271
'BYTES'=272
'DECIMAL'=273
'DEC'=274
'NUMERIC'=275
'TINYINT'=276
'SMALLINT'=277
'INT'=278
'INTEGER'=279
'BIGINT'=280
'FLOAT'=281
'DOUBLE'=282
'DATE'=283
'TIME'=284
'TIMESTAMP'=285
'TIMESTAMP_LTZ'=286
'MULTISET'=287
'BOOLEAN'=288
'RAW'=289
'ROW'=290
'NULL'=291
'DATETIME'=292
'='=293
'>'=294
'<'=295
'!'=296
'~'=297
'|'=298
'&'=299
'^'=300
'.'=301
'['=302
']'=303
'('=304
')'=305
','=306
';'=307
'@'=308
'\''=309
'"'=310
'`'=311
':'=312
'*'=313
'_'=314
'-'=315
'+'=316
'%'=317
'||'=318
'--'=319
'/'=320

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -254,78 +254,79 @@ CATALOGS=253
VIEWS=254
JARS=255
PRIMARY=256
KEY=257
PERIOD=258
SYSTEM_TIME=259
ENFORCED=260
METADATA=261
VIRTUAL=262
ZONE=263
STRING=264
ARRAY=265
MAP=266
CHAR=267
VARCHAR=268
BINARY=269
VARBINARY=270
BYTES=271
DECIMAL=272
DEC=273
NUMERIC=274
TINYINT=275
SMALLINT=276
INT=277
INTEGER=278
BIGINT=279
FLOAT=280
DOUBLE=281
DATE=282
TIME=283
TIMESTAMP=284
TIMESTAMP_LTZ=285
MULTISET=286
BOOLEAN=287
RAW=288
ROW=289
NULL=290
DATETIME=291
EQUAL_SYMBOL=292
GREATER_SYMBOL=293
LESS_SYMBOL=294
EXCLAMATION_SYMBOL=295
BIT_NOT_OP=296
BIT_OR_OP=297
BIT_AND_OP=298
BIT_XOR_OP=299
DOT=300
LS_BRACKET=301
RS_BRACKET=302
LR_BRACKET=303
RR_BRACKET=304
COMMA=305
SEMICOLON=306
AT_SIGN=307
SINGLE_QUOTE_SYMB=308
DOUBLE_QUOTE_SYMB=309
REVERSE_QUOTE_SYMB=310
COLON_SYMB=311
ASTERISK_SIGN=312
UNDERLINE_SIGN=313
HYPNEN_SIGN=314
ADD_SIGN=315
PENCENT_SIGN=316
DOUBLE_VERTICAL_SIGN=317
DOUBLE_HYPNEN_SIGN=318
SLASH_SIGN=319
DOT_ID=320
PLUS_DOT_ID=321
STRING_LITERAL=322
DIG_LITERAL=323
REAL_LITERAL=324
BIT_STRING=325
ID_LITERAL=326
PLUS_ID_LITERAL=327
FILE_PATH=328
UNIQUE=257
KEY=258
PERIOD=259
SYSTEM_TIME=260
ENFORCED=261
METADATA=262
VIRTUAL=263
ZONE=264
STRING=265
ARRAY=266
MAP=267
CHAR=268
VARCHAR=269
BINARY=270
VARBINARY=271
BYTES=272
DECIMAL=273
DEC=274
NUMERIC=275
TINYINT=276
SMALLINT=277
INT=278
INTEGER=279
BIGINT=280
FLOAT=281
DOUBLE=282
DATE=283
TIME=284
TIMESTAMP=285
TIMESTAMP_LTZ=286
MULTISET=287
BOOLEAN=288
RAW=289
ROW=290
NULL=291
DATETIME=292
EQUAL_SYMBOL=293
GREATER_SYMBOL=294
LESS_SYMBOL=295
EXCLAMATION_SYMBOL=296
BIT_NOT_OP=297
BIT_OR_OP=298
BIT_AND_OP=299
BIT_XOR_OP=300
DOT=301
LS_BRACKET=302
RS_BRACKET=303
LR_BRACKET=304
RR_BRACKET=305
COMMA=306
SEMICOLON=307
AT_SIGN=308
SINGLE_QUOTE_SYMB=309
DOUBLE_QUOTE_SYMB=310
REVERSE_QUOTE_SYMB=311
COLON_SYMB=312
ASTERISK_SIGN=313
UNDERLINE_SIGN=314
HYPNEN_SIGN=315
ADD_SIGN=316
PENCENT_SIGN=317
DOUBLE_VERTICAL_SIGN=318
DOUBLE_HYPNEN_SIGN=319
SLASH_SIGN=320
DOT_ID=321
PLUS_DOT_ID=322
STRING_LITERAL=323
DIG_LITERAL=324
REAL_LITERAL=325
BIT_STRING=326
ID_LITERAL=327
PLUS_ID_LITERAL=328
FILE_PATH=329
'SELECT'=4
'FROM'=5
'ADD'=6
@ -579,66 +580,67 @@ FILE_PATH=328
'VIEWS'=254
'JARS'=255
'PRIMARY'=256
'KEY'=257
'PERIOD'=258
'SYSTEM_TIME'=259
'ENFORCED'=260
'METADATA'=261
'VIRTUAL'=262
'ZONE'=263
'STRING'=264
'ARRAY'=265
'MAP'=266
'CHAR'=267
'VARCHAR'=268
'BINARY'=269
'VARBINARY'=270
'BYTES'=271
'DECIMAL'=272
'DEC'=273
'NUMERIC'=274
'TINYINT'=275
'SMALLINT'=276
'INT'=277
'INTEGER'=278
'BIGINT'=279
'FLOAT'=280
'DOUBLE'=281
'DATE'=282
'TIME'=283
'TIMESTAMP'=284
'TIMESTAMP_LTZ'=285
'MULTISET'=286
'BOOLEAN'=287
'RAW'=288
'ROW'=289
'NULL'=290
'DATETIME'=291
'='=292
'>'=293
'<'=294
'!'=295
'~'=296
'|'=297
'&'=298
'^'=299
'.'=300
'['=301
']'=302
'('=303
')'=304
','=305
';'=306
'@'=307
'\''=308
'"'=309
'`'=310
':'=311
'*'=312
'_'=313
'-'=314
'+'=315
'%'=316
'||'=317
'--'=318
'/'=319
'UNIQUE'=257
'KEY'=258
'PERIOD'=259
'SYSTEM_TIME'=260
'ENFORCED'=261
'METADATA'=262
'VIRTUAL'=263
'ZONE'=264
'STRING'=265
'ARRAY'=266
'MAP'=267
'CHAR'=268
'VARCHAR'=269
'BINARY'=270
'VARBINARY'=271
'BYTES'=272
'DECIMAL'=273
'DEC'=274
'NUMERIC'=275
'TINYINT'=276
'SMALLINT'=277
'INT'=278
'INTEGER'=279
'BIGINT'=280
'FLOAT'=281
'DOUBLE'=282
'DATE'=283
'TIME'=284
'TIMESTAMP'=285
'TIMESTAMP_LTZ'=286
'MULTISET'=287
'BOOLEAN'=288
'RAW'=289
'ROW'=290
'NULL'=291
'DATETIME'=292
'='=293
'>'=294
'<'=295
'!'=296
'~'=297
'|'=298
'&'=299
'^'=300
'.'=301
'['=302
']'=303
'('=304
')'=305
','=306
';'=307
'@'=308
'\''=309
'"'=310
'`'=311
':'=312
'*'=313
'_'=314
'-'=315
'+'=316
'%'=317
'||'=318
'--'=319
'/'=320

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
// Generated from /Users/zhenglin/Documents/parser/dt-sql-parser/src/grammar/flinksql/FlinkSqlParser.g4 by ANTLR 4.12.0
// Generated from /Users/ziv/github.com/dt-sql-parser/src/grammar/flinksql/FlinkSqlParser.g4 by ANTLR 4.12.0
import {ParseTreeListener} from "antlr4";
@ -64,6 +64,10 @@ import { JarFileNameContext } from "./FlinkSqlParser";
import { AlterTableContext } from "./FlinkSqlParser";
import { RenameDefinitionContext } from "./FlinkSqlParser";
import { SetKeyValueDefinitionContext } from "./FlinkSqlParser";
import { AddConstraintContext } from "./FlinkSqlParser";
import { DropConstraintContext } from "./FlinkSqlParser";
import { AddUniqueContext } from "./FlinkSqlParser";
import { NotForcedContext } from "./FlinkSqlParser";
import { AlertViewContext } from "./FlinkSqlParser";
import { AlterDatabaseContext } from "./FlinkSqlParser";
import { AlterFunctionContext } from "./FlinkSqlParser";
@ -801,6 +805,46 @@ export default class FlinkSqlParserListener extends ParseTreeListener {
* @param ctx the parse tree
*/
exitSetKeyValueDefinition?: (ctx: SetKeyValueDefinitionContext) => void;
/**
* Enter a parse tree produced by `FlinkSqlParser.addConstraint`.
* @param ctx the parse tree
*/
enterAddConstraint?: (ctx: AddConstraintContext) => void;
/**
* Exit a parse tree produced by `FlinkSqlParser.addConstraint`.
* @param ctx the parse tree
*/
exitAddConstraint?: (ctx: AddConstraintContext) => void;
/**
* Enter a parse tree produced by `FlinkSqlParser.dropConstraint`.
* @param ctx the parse tree
*/
enterDropConstraint?: (ctx: DropConstraintContext) => void;
/**
* Exit a parse tree produced by `FlinkSqlParser.dropConstraint`.
* @param ctx the parse tree
*/
exitDropConstraint?: (ctx: DropConstraintContext) => void;
/**
* Enter a parse tree produced by `FlinkSqlParser.addUnique`.
* @param ctx the parse tree
*/
enterAddUnique?: (ctx: AddUniqueContext) => void;
/**
* Exit a parse tree produced by `FlinkSqlParser.addUnique`.
* @param ctx the parse tree
*/
exitAddUnique?: (ctx: AddUniqueContext) => void;
/**
* Enter a parse tree produced by `FlinkSqlParser.notForced`.
* @param ctx the parse tree
*/
enterNotForced?: (ctx: NotForcedContext) => void;
/**
* Exit a parse tree produced by `FlinkSqlParser.notForced`.
* @param ctx the parse tree
*/
exitNotForced?: (ctx: NotForcedContext) => void;
/**
* Enter a parse tree produced by `FlinkSqlParser.alertView`.
* @param ctx the parse tree

View File

@ -1,4 +1,4 @@
// Generated from /Users/zhenglin/Documents/parser/dt-sql-parser/src/grammar/flinksql/FlinkSqlParser.g4 by ANTLR 4.12.0
// Generated from /Users/ziv/github.com/dt-sql-parser/src/grammar/flinksql/FlinkSqlParser.g4 by ANTLR 4.12.0
import {ParseTreeVisitor} from 'antlr4';
@ -64,6 +64,10 @@ import { JarFileNameContext } from "./FlinkSqlParser";
import { AlterTableContext } from "./FlinkSqlParser";
import { RenameDefinitionContext } from "./FlinkSqlParser";
import { SetKeyValueDefinitionContext } from "./FlinkSqlParser";
import { AddConstraintContext } from "./FlinkSqlParser";
import { DropConstraintContext } from "./FlinkSqlParser";
import { AddUniqueContext } from "./FlinkSqlParser";
import { NotForcedContext } from "./FlinkSqlParser";
import { AlertViewContext } from "./FlinkSqlParser";
import { AlterDatabaseContext } from "./FlinkSqlParser";
import { AlterFunctionContext } from "./FlinkSqlParser";
@ -558,6 +562,30 @@ export default class FlinkSqlParserVisitor<Result> extends ParseTreeVisitor<Resu
* @return the visitor result
*/
visitSetKeyValueDefinition?: (ctx: SetKeyValueDefinitionContext) => Result;
/**
* Visit a parse tree produced by `FlinkSqlParser.addConstraint`.
* @param ctx the parse tree
* @return the visitor result
*/
visitAddConstraint?: (ctx: AddConstraintContext) => Result;
/**
* Visit a parse tree produced by `FlinkSqlParser.dropConstraint`.
* @param ctx the parse tree
* @return the visitor result
*/
visitDropConstraint?: (ctx: DropConstraintContext) => Result;
/**
* Visit a parse tree produced by `FlinkSqlParser.addUnique`.
* @param ctx the parse tree
* @return the visitor result
*/
visitAddUnique?: (ctx: AddUniqueContext) => Result;
/**
* Visit a parse tree produced by `FlinkSqlParser.notForced`.
* @param ctx the parse tree
* @return the visitor result
*/
visitNotForced?: (ctx: NotForcedContext) => Result;
/**
* Visit a parse tree produced by `FlinkSqlParser.alertView`.
* @param ctx the parse tree

View File

@ -1 +1,3 @@
ALTER DATABASE tempDB SET ("key1"="value1");
alter database db1 set ('key1' = 'value1','key2.a' = 'value2.a');

View File

@ -1,7 +1,15 @@
ALTER FUNCTION tempFunction AS 'SimpleUdf';
ALTER TEMPORARY FUNCTION IF EXISTS tempFunction AS 'SimpleUdf';
alter temporary function function1 as 'org.apache.flink.function.function1';
alter temporary function function1 as 'org.apache.flink.function.function1' language scala;
alter temporary system function function1 as 'org.apache.flink.function.function1';
alter temporary system function function1 as 'org.apache.flink.function.function1' language java;
ALTER TEMPORARY SYSTEM FUNCTION IF EXISTS tempFunction AS 'SimpleUdf';
ALTER TEMPORARY FUNCTION IF EXISTS tempFunction AS 'SimpleUdf';
ALTER FUNCTION myudf AS 'com.example.MyUdf' LANGUAGE PYTHON;

View File

@ -1,7 +1,65 @@
ALTER TABLE
Orders RENAME TO NewOrders;
-- Refer: https://github.com/apache/flink/blob/master/flink-table/flink-sql-parser/src/test/java/org/apache/flink/sql/parser/FlinkSqlParserImplTest.java#L2016
-- Just for simple alter table statements, it not include alter table columns statements
alter table
t1 rename to t2;
alter table
if exists t1 rename to t2;
alter table
c1.d1.t1 rename to t2;
alter table
if exists c1.d1.t1 rename to t2;
alter table
t1 rename a to b;
alter table
if exists t1 rename a to b;
alter table
if exists t1 rename a.x to a.y;
alter table
t1
set
('key1' = 'value1');
alter table
if exists t1
set
('key1' = 'value1');
alter table
t1
add
constraint ct1 primary key(a, b);
alter table
t1
add
constraint ct1 primary key(a, b) not enforced;
alter table
if exists t1
add
constraint ct1 primary key(a, b) not enforced;
alter table
t1
add
unique(a, b);
alter table
if exists t1
add
unique(a, b);
alter table
t1 drop constraint ct1;
alter table
if exists t1 drop constraint ct1;
ALTER TABLE
sample_table
SET
('key1' = 'value2');