2024-03-27 10:33:25 +08:00
|
|
|
import { FlinkSQL } from 'src/parser/flink';
|
|
|
|
import { FlinkSqlLexer } from 'src/lib/flink/FlinkSqlLexer';
|
2024-01-19 21:10:00 +08:00
|
|
|
import { ErrorListener } from 'src/parser/common/parseErrorListener';
|
2024-02-26 20:25:09 +08:00
|
|
|
import { CommonTokenStream } from 'antlr4ng';
|
2023-10-16 17:59:28 +08:00
|
|
|
|
2024-03-27 10:33:25 +08:00
|
|
|
describe('BasicSQL unit tests', () => {
|
|
|
|
const flink = new FlinkSQL();
|
2023-10-16 17:59:28 +08:00
|
|
|
test('Create lexer', () => {
|
|
|
|
const sql = 'SELECT * FROM tb1';
|
2024-03-27 10:33:25 +08:00
|
|
|
const lexer = flink.createLexer(sql);
|
2023-10-16 17:59:28 +08:00
|
|
|
|
|
|
|
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团队';
|
2024-03-27 19:04:16 +08:00
|
|
|
const errors = [];
|
|
|
|
const errorListener: ErrorListener = (err) => {
|
2023-10-16 17:59:28 +08:00
|
|
|
errors.push(err);
|
|
|
|
};
|
2024-03-27 10:33:25 +08:00
|
|
|
const lexer = flink.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';
|
2024-03-27 10:33:25 +08:00
|
|
|
const parser = flink.createParser(sql);
|
2023-10-16 17:59:28 +08:00
|
|
|
|
|
|
|
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团队';
|
2024-03-27 19:04:16 +08:00
|
|
|
const errors = [];
|
|
|
|
const errorListener: ErrorListener = (err) => {
|
2023-10-16 17:59:28 +08:00
|
|
|
errors.push(err);
|
|
|
|
};
|
2024-03-27 10:33:25 +08:00
|
|
|
const parser = flink.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';
|
2024-03-27 19:04:16 +08:00
|
|
|
const errors = [];
|
|
|
|
const errorListener: ErrorListener = (err) => {
|
2023-10-16 17:59:28 +08:00
|
|
|
errors.push(err);
|
|
|
|
};
|
2024-03-27 10:33:25 +08:00
|
|
|
const parser = flink.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';
|
2024-03-27 19:04:16 +08:00
|
|
|
const errors = [];
|
|
|
|
const errorListener: ErrorListener = (err) => {
|
2023-10-16 17:59:28 +08:00
|
|
|
errors.push(err);
|
|
|
|
};
|
2024-03-27 10:33:25 +08:00
|
|
|
const parseTree = flink.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团队';
|
2024-03-27 19:04:16 +08:00
|
|
|
const errors = [];
|
|
|
|
const errorListener: ErrorListener = (err) => {
|
2023-10-16 17:59:28 +08:00
|
|
|
errors.push(err);
|
|
|
|
};
|
2024-03-27 10:33:25 +08:00
|
|
|
const parseTree = flink.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;';
|
2024-03-27 10:33:25 +08:00
|
|
|
const tokens = flink.getAllTokens(sql);
|
2023-10-16 17:59:28 +08:00
|
|
|
|
|
|
|
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团队';
|
2024-03-27 10:33:25 +08:00
|
|
|
const tokens = flink.getAllTokens(sql);
|
2023-10-16 17:59:28 +08:00
|
|
|
expect(tokens.length).toBe(1);
|
|
|
|
expect(tokens[0].type).toBe(FlinkSqlLexer.ID_LITERAL);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Split sql', () => {
|
|
|
|
const sql = 'SHOW TABLES;\nSELECT * FROM tb;';
|
2024-03-27 10:33:25 +08:00
|
|
|
const sqlSlices = flink.splitSQLByStatement(sql);
|
2023-10-16 17:59:28 +08:00
|
|
|
|
|
|
|
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;';
|
2024-03-27 10:33:25 +08:00
|
|
|
const sqlSlices = flink.splitSQLByStatement(sql);
|
2023-10-16 17:59:28 +08:00
|
|
|
|
|
|
|
expect(sqlSlices).toBeNull();
|
|
|
|
});
|
|
|
|
});
|