feat(flink): update flinkSql grammar and add some test

This commit is contained in:
Erindcl 2020-12-03 20:03:25 +08:00
parent 04c4c40397
commit c9d88d00a6
7 changed files with 1530 additions and 1475 deletions

View File

@ -349,7 +349,8 @@ PLUS_ID_LITERAL: PLUS_ID_LITERAL_FRAG;
fragment EXPONENT_NUM_PART: 'E' [-+]? DEC_DIGIT+; fragment EXPONENT_NUM_PART: 'E' [-+]? DEC_DIGIT+;
fragment ID_LITERAL_FRAG: [A-Z_0-9a-z]*?[A-Z_a-z]+?[A-Z_0-9a-z]*; fragment ID_LITERAL_FRAG: [A-Z_0-9a-z]*?[A-Z_a-z]+?[A-Z_0-9a-z]*;
fragment PLUS_ID_LITERAL_FRAG: ('a'..'z' | '0'..'9' | '_' | '*' | '@' | '#' | '^' | '$' | '%' | '&')*?[A-Z_a-z]+?('a'..'z' | '0'..'9' | '_' | '*' | '@' | '#' | '^' | '$' | '%' | '&')*; // fragment PLUS_ID_LITERAL_FRAG: ('a'..'z' | '0'..'9' | '_' | '*' | '@' | '#' | '^' | '$' | '%' | '&')*?[A-Z_a-z]+?('a'..'z' | '0'..'9' | '_' | '*' | '@' | '#' | '^' | '$' | '%' | '&')*;
fragment PLUS_ID_LITERAL_FRAG: ('a'..'z' | '0'..'9' | '_' | '*' | '@' | '#' | '^' | '$' | '%' | '&')+;
fragment DEC_DIGIT: [0-9]; fragment DEC_DIGIT: [0-9];
fragment DEC_LETTER: [A-Za-z]; fragment DEC_LETTER: [A-Za-z];
fragment DQUOTA_STRING: '"' ( '\\'. | '""' | ~('"'| '\\') )* '"'; fragment DQUOTA_STRING: '"' ( '\\'. | '""' | ~('"'| '\\') )* '"';

View File

@ -238,7 +238,7 @@ selectClause
; ;
projectItemDefinition projectItemDefinition
: expression (AS? uid)? : expression (AS? expression)?
; ;
fromClause fromClause
@ -247,7 +247,7 @@ fromClause
tableExpression tableExpression
: tableReference (COMMA tableReference)* : tableReference (COMMA tableReference)*
| tableExpression NATURAL? (LEFT | RIGHT | FULL)? JOIN tableExpression joinCondition? | tableExpression NATURAL? (LEFT | RIGHT | FULL | INNER)? JOIN tableExpression joinCondition?
; ;
tableReference tableReference

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 one or more lines are too long

View File

@ -171,7 +171,7 @@ var serializedATN = ["\u0003\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964",
"\u00ff\u0003\u0002\u0104\u011b\u0003\u0002\u00f7\u00f8\u0004\u0002\n", "\u00ff\u0003\u0002\u0104\u011b\u0003\u0002\u00f7\u00f8\u0004\u0002\n",
"\n\u00f9\u00f9\u0004\u0002\u00ae\u00ae\u00fb\u00fb\u0004\u0002\u00bf", "\n\u00f9\u00f9\u0004\u0002\u00ae\u00ae\u00fb\u00fb\u0004\u0002\u00bf",
"\u00bf\u00c1\u00c1\u0004\u0002OO\u0093\u0093\u0004\u0002_`bb\u0004\u0002", "\u00bf\u00c1\u00c1\u0004\u0002OO\u0093\u0093\u0004\u0002_`bb\u0004\u0002",
"2245\u0003\u0002%&\u0004\u0002CCEE\u0003\u0002\n\u000b\u0003\u0002\"", "1245\u0003\u0002%&\u0004\u0002CCEE\u0003\u0002\n\u000b\u0003\u0002\"",
"#\u0004\u0002\u0120\u0120\u0132\u0133\u0006\u0002\u0086\u0086\u0130", "#\u0004\u0002\u0120\u0120\u0132\u0133\u0006\u0002\u0086\u0086\u0130",
"\u0130\u0134\u0134\u0137\u0137\u0004\u0002\u0132\u0133\u0135\u0135\u0003", "\u0130\u0134\u0134\u0137\u0137\u0004\u0002\u0132\u0133\u0135\u0135\u0003",
"\u0002\u0132\u0133\u0003\u0002\u013b\u013c\u0004\u0002\u013b\u013b\u013e", "\u0002\u0132\u0133\u0003\u0002\u013b\u013c\u0004\u0002\u013b\u013b\u013e",
@ -479,7 +479,7 @@ var serializedATN = ["\u0003\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964",
"\u0282\u0003\u0002\u0002\u0002\u028ba\u0003\u0002\u0002\u0002\u028c", "\u0282\u0003\u0002\u0002\u0002\u028ba\u0003\u0002\u0002\u0002\u028c",
"\u0291\u0005\u0088E\u0002\u028d\u028f\u0007\t\u0002\u0002\u028e\u028d", "\u0291\u0005\u0088E\u0002\u028d\u028f\u0007\t\u0002\u0002\u028e\u028d",
"\u0003\u0002\u0002\u0002\u028e\u028f\u0003\u0002\u0002\u0002\u028f\u0290", "\u0003\u0002\u0002\u0002\u028e\u028f\u0003\u0002\u0002\u0002\u028f\u0290",
"\u0003\u0002\u0002\u0002\u0290\u0292\u0005\u00bc_\u0002\u0291\u028e", "\u0003\u0002\u0002\u0002\u0290\u0292\u0005\u0088E\u0002\u0291\u028e",
"\u0003\u0002\u0002\u0002\u0291\u0292\u0003\u0002\u0002\u0002\u0292c", "\u0003\u0002\u0002\u0002\u0291\u0292\u0003\u0002\u0002\u0002\u0292c",
"\u0003\u0002\u0002\u0002\u0293\u0294\u0007\u0007\u0002\u0002\u0294\u0295", "\u0003\u0002\u0002\u0002\u0293\u0294\u0007\u0007\u0002\u0002\u0294\u0295",
"\u0005f4\u0002\u0295e\u0003\u0002\u0002\u0002\u0296\u0297\b4\u0001\u0002", "\u0005f4\u0002\u0295e\u0003\u0002\u0002\u0002\u0296\u0297\b4\u0001\u0002",
@ -6873,12 +6873,15 @@ function ProjectItemDefinitionContext(parser, parent, invokingState) {
ProjectItemDefinitionContext.prototype = Object.create(antlr4.ParserRuleContext.prototype); ProjectItemDefinitionContext.prototype = Object.create(antlr4.ParserRuleContext.prototype);
ProjectItemDefinitionContext.prototype.constructor = ProjectItemDefinitionContext; ProjectItemDefinitionContext.prototype.constructor = ProjectItemDefinitionContext;
ProjectItemDefinitionContext.prototype.expression = function() { ProjectItemDefinitionContext.prototype.expression = function(i) {
return this.getTypedRuleContext(ExpressionContext,0); if(i===undefined) {
}; i = null;
}
ProjectItemDefinitionContext.prototype.uid = function() { if(i===null) {
return this.getTypedRuleContext(UidContext,0); return this.getTypedRuleContexts(ExpressionContext);
} else {
return this.getTypedRuleContext(ExpressionContext,i);
}
}; };
ProjectItemDefinitionContext.prototype.AS = function() { ProjectItemDefinitionContext.prototype.AS = function() {
@ -6932,7 +6935,7 @@ FlinkSqlParser.prototype.projectItemDefinition = function() {
} }
this.state = 654; this.state = 654;
this.uid(); this.expression();
} }
} catch (re) { } catch (re) {
@ -7098,6 +7101,10 @@ TableExpressionContext.prototype.FULL = function() {
return this.getToken(FlinkSqlParser.FULL, 0); return this.getToken(FlinkSqlParser.FULL, 0);
}; };
TableExpressionContext.prototype.INNER = function() {
return this.getToken(FlinkSqlParser.INNER, 0);
};
TableExpressionContext.prototype.enterRule = function(listener) { TableExpressionContext.prototype.enterRule = function(listener) {
if(listener instanceof FlinkSqlParserListener ) { if(listener instanceof FlinkSqlParserListener ) {
listener.enterTableExpression(this); listener.enterTableExpression(this);
@ -7177,10 +7184,10 @@ FlinkSqlParser.prototype.tableExpression = function(_p) {
this.state = 674; this.state = 674;
this._errHandler.sync(this); this._errHandler.sync(this);
_la = this._input.LA(1); _la = this._input.LA(1);
if(((((_la - 48)) & ~0x1f) == 0 && ((1 << (_la - 48)) & ((1 << (FlinkSqlParser.LEFT - 48)) | (1 << (FlinkSqlParser.RIGHT - 48)) | (1 << (FlinkSqlParser.FULL - 48)))) !== 0)) { if(((((_la - 47)) & ~0x1f) == 0 && ((1 << (_la - 47)) & ((1 << (FlinkSqlParser.INNER - 47)) | (1 << (FlinkSqlParser.LEFT - 47)) | (1 << (FlinkSqlParser.RIGHT - 47)) | (1 << (FlinkSqlParser.FULL - 47)))) !== 0)) {
this.state = 673; this.state = 673;
_la = this._input.LA(1); _la = this._input.LA(1);
if(!(((((_la - 48)) & ~0x1f) == 0 && ((1 << (_la - 48)) & ((1 << (FlinkSqlParser.LEFT - 48)) | (1 << (FlinkSqlParser.RIGHT - 48)) | (1 << (FlinkSqlParser.FULL - 48)))) !== 0))) { if(!(((((_la - 47)) & ~0x1f) == 0 && ((1 << (_la - 47)) & ((1 << (FlinkSqlParser.INNER - 47)) | (1 << (FlinkSqlParser.LEFT - 47)) | (1 << (FlinkSqlParser.RIGHT - 47)) | (1 << (FlinkSqlParser.FULL - 47)))) !== 0))) {
this._errHandler.recoverInline(this); this._errHandler.recoverInline(this);
} }
else { else {

View File

@ -1,4 +1,5 @@
import SQLParser from '../../../src/parser/flinksql'; import SQLParser from '../../../src/parser/flinksql';
import sqlMockData from '../../mock/sql';
describe('FlinkSQL Syntax Tests', () => { describe('FlinkSQL Syntax Tests', () => {
const parser = new SQLParser(); const parser = new SQLParser();
@ -14,7 +15,6 @@ describe('FlinkSQL Syntax Tests', () => {
); );
`; `;
const result = parser.validate(sql); const result = parser.validate(sql);
console.log(result);
expect(result.length).toBe(0); expect(result.length).toBe(0);
}); });
test('Test simple CreateDatabase Statement', () => { test('Test simple CreateDatabase Statement', () => {
@ -172,4 +172,60 @@ describe('FlinkSQL Syntax Tests', () => {
const result = parser.validate(sql); const result = parser.validate(sql);
expect(result.length).toBe(0); expect(result.length).toBe(0);
}); });
// todo 字段中包含特殊字符 如$符号 内容匹配不准确
// test('Test complex sql Statement one', () => {
// const sql = sqlMockData.sqlStrOne;
// const result = parser.validate(sql);
// console.log(result);
// expect(result.length).toBe(0);
// });
test('Test complex sql Statement two', () => {
const sql = sqlMockData.sqlStrTwo;
const result = parser.validate(sql);
console.log(result);
expect(result.length).toBe(0);
});
test('Test complex sql Statement three', () => {
const sql = sqlMockData.sqlStrThree;
const result = parser.validate(sql);
console.log(result);
expect(result.length).toBe(0);
});
test('Test complex sql Statement four', () => {
const sql = sqlMockData.sqlStrFour;
const result = parser.validate(sql);
console.log(result);
expect(result.length).toBe(0);
});
test('Test complex sql Statement five', () => {
const sql = sqlMockData.sqlStrFive;
const result = parser.validate(sql);
console.log(result);
expect(result.length).toBe(0);
});
test('Test complex sql Statement six', () => {
const sql = sqlMockData.sqlStrSix;
const result = parser.validate(sql);
console.log(result);
expect(result.length).toBe(0);
});
test('Test complex sql Statement seven', () => {
const sql = sqlMockData.sqlStrSeven;
const result = parser.validate(sql);
console.log(result);
expect(result.length).toBe(0);
});
test('Test complex sql Statement eight', () => {
const sql = sqlMockData.sqlStrEight;
const result = parser.validate(sql);
console.log(result);
expect(result.length).toBe(0);
});
// test('Test complex sql Statement nine', () => {
// const sql = sqlMockData.allSqlStr;
// const result = parser.validate(sql);
// console.log(result);
// expect(result.length).toBe(0);
// });
}); });