2023-12-19 19:22:27 +08:00
|
|
|
import { CommonTokenStream, ErrorListener, FlinkSQL, FlinkSqlLexer } from '../filters';
|
2023-10-16 17:59:28 +08:00
|
|
|
|
|
|
|
describe('BasicParser unit tests', () => {
|
|
|
|
const flinkParser = new FlinkSQL();
|
|
|
|
test('Create lexer', () => {
|
|
|
|
const sql = 'SELECT * FROM tb1';
|
|
|
|
const lexer = flinkParser.createLexer(sql);
|
|
|
|
|
|
|
|
expect(lexer).not.toBeUndefined();
|
|
|
|
expect(lexer).not.toBeNull();
|
|
|
|
});
|
|
|
|
|
2023-12-11 17:34:49 +08:00
|
|
|
test('Create lexer with errorListener', () => {
|
2023-10-16 17:59:28 +08:00
|
|
|
const sql = '袋鼠云数栈UED团队';
|
|
|
|
const errors: any[] = [];
|
2023-12-11 17:34:49 +08:00
|
|
|
const errorListener: ErrorListener<any> = (err) => {
|
2023-10-16 17:59:28 +08:00
|
|
|
errors.push(err);
|
|
|
|
};
|
2023-12-11 17:34:49 +08:00
|
|
|
const lexer = flinkParser.createLexer(sql, errorListener);
|
2023-10-16 17:59:28 +08:00
|
|
|
const tokenStream = new CommonTokenStream(lexer);
|
|
|
|
tokenStream.fill();
|
|
|
|
expect(errors.length).not.toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Create parser', () => {
|
|
|
|
const sql = 'SELECT * FROM tb1';
|
|
|
|
const parser = flinkParser.createParser(sql);
|
|
|
|
|
|
|
|
expect(parser).not.toBeUndefined();
|
|
|
|
expect(parser).not.toBeNull();
|
|
|
|
});
|
|
|
|
|
2023-12-11 17:34:49 +08:00
|
|
|
test('Create parser with errorListener (lexer error)', () => {
|
2023-10-16 17:59:28 +08:00
|
|
|
const sql = '袋鼠云数栈UED团队';
|
|
|
|
const errors: any[] = [];
|
2023-12-11 17:34:49 +08:00
|
|
|
const errorListener: ErrorListener<any> = (err) => {
|
2023-10-16 17:59:28 +08:00
|
|
|
errors.push(err);
|
|
|
|
};
|
2023-12-11 17:34:49 +08:00
|
|
|
const parser = flinkParser.createParser(sql, errorListener);
|
2023-10-16 17:59:28 +08:00
|
|
|
parser.program();
|
|
|
|
expect(errors.length).not.toBe(0);
|
|
|
|
});
|
|
|
|
|
2023-12-11 17:34:49 +08:00
|
|
|
test('Create parser with errorListener (parse error)', () => {
|
2023-10-16 17:59:28 +08:00
|
|
|
const sql = 'SHOW TA';
|
|
|
|
const errors: any[] = [];
|
2023-12-11 17:34:49 +08:00
|
|
|
const errorListener: ErrorListener<any> = (err) => {
|
2023-10-16 17:59:28 +08:00
|
|
|
errors.push(err);
|
|
|
|
};
|
2023-12-11 17:34:49 +08:00
|
|
|
const parser = flinkParser.createParser(sql, errorListener);
|
2023-10-16 17:59:28 +08:00
|
|
|
parser.program();
|
|
|
|
expect(errors.length).not.toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Parse right input', () => {
|
|
|
|
const sql = 'SELECT * FROM tb1';
|
|
|
|
const errors: any[] = [];
|
2023-12-11 17:34:49 +08:00
|
|
|
const errorListener: ErrorListener<any> = (err) => {
|
2023-10-16 17:59:28 +08:00
|
|
|
errors.push(err);
|
|
|
|
};
|
2023-12-11 17:34:49 +08:00
|
|
|
const parseTree = flinkParser.parse(sql, errorListener);
|
2023-10-16 17:59:28 +08:00
|
|
|
|
|
|
|
expect(parseTree).not.toBeUndefined();
|
|
|
|
expect(parseTree).not.toBeNull();
|
|
|
|
expect(errors.length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Parse wrong input', () => {
|
|
|
|
const sql = '袋鼠云数栈UED团队';
|
|
|
|
const errors: any[] = [];
|
2023-12-11 17:34:49 +08:00
|
|
|
const errorListener: ErrorListener<any> = (err) => {
|
2023-10-16 17:59:28 +08:00
|
|
|
errors.push(err);
|
|
|
|
};
|
2023-12-11 17:34:49 +08:00
|
|
|
const parseTree = flinkParser.parse(sql, errorListener);
|
2023-10-16 17:59:28 +08:00
|
|
|
|
|
|
|
expect(parseTree).not.toBeUndefined();
|
|
|
|
expect(parseTree).not.toBeNull();
|
|
|
|
expect(errors.length).not.toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Get All tokens', () => {
|
|
|
|
const sql = 'SELECT * FROM tbl1;';
|
|
|
|
const tokens = flinkParser.getAllTokens(sql);
|
|
|
|
|
|
|
|
expect(tokens.length).toBe(8);
|
|
|
|
expect(tokens[0].type).toBe(FlinkSqlLexer.KW_SELECT);
|
|
|
|
expect(tokens[1].type).toBe(FlinkSqlLexer.SPACE);
|
|
|
|
expect(tokens[2].type).toBe(FlinkSqlLexer.ASTERISK_SIGN);
|
|
|
|
expect(tokens[3].type).toBe(FlinkSqlLexer.SPACE);
|
|
|
|
expect(tokens[4].type).toBe(FlinkSqlLexer.KW_FROM);
|
|
|
|
expect(tokens[5].type).toBe(FlinkSqlLexer.SPACE);
|
|
|
|
expect(tokens[6].type).toBe(FlinkSqlLexer.ID_LITERAL);
|
|
|
|
expect(tokens[7].type).toBe(FlinkSqlLexer.SEMICOLON);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Get All tokens with error', () => {
|
|
|
|
const sql = '袋鼠云数栈UED团队';
|
|
|
|
const tokens = flinkParser.getAllTokens(sql);
|
|
|
|
expect(tokens.length).toBe(1);
|
|
|
|
expect(tokens[0].type).toBe(FlinkSqlLexer.ID_LITERAL);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Split sql', () => {
|
|
|
|
const sql = 'SHOW TABLES;\nSELECT * FROM tb;';
|
|
|
|
const sqlSlices = flinkParser.splitSQLByStatement(sql);
|
|
|
|
|
|
|
|
expect(sqlSlices.length).toBe(2);
|
|
|
|
|
|
|
|
expect(sqlSlices[0].text).toBe('SHOW TABLES;');
|
|
|
|
expect(sql.slice(sqlSlices[0].startIndex, sqlSlices[0].endIndex + 1)).toBe(
|
|
|
|
sqlSlices[0].text
|
|
|
|
);
|
|
|
|
expect(sqlSlices[0].startLine).toBe(1);
|
|
|
|
expect(sqlSlices[0].endLine).toBe(1);
|
|
|
|
|
|
|
|
expect(sqlSlices[1].text).toBe('SELECT * FROM tb;');
|
|
|
|
expect(sql.slice(sqlSlices[1].startIndex, sqlSlices[1].endIndex + 1)).toBe(
|
|
|
|
sqlSlices[1].text
|
|
|
|
);
|
|
|
|
expect(sqlSlices[1].startLine).toBe(2);
|
|
|
|
expect(sqlSlices[1].endLine).toBe(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Split sql with errors', () => {
|
|
|
|
const sql = 'SHOW TABLES;\nSELECT * FOM tb;';
|
|
|
|
const sqlSlices = flinkParser.splitSQLByStatement(sql);
|
|
|
|
|
|
|
|
expect(sqlSlices).toBeNull();
|
|
|
|
});
|
|
|
|
});
|