2024-02-26 20:25:09 +08:00
|
|
|
import { AbstractParseTreeVisitor } from 'antlr4ng';
|
2024-01-19 21:10:00 +08:00
|
|
|
|
|
|
|
import HiveSQL from 'src/parser/hive';
|
|
|
|
import { HiveSqlParserVisitor } from 'src/lib/hive/HiveSqlParserVisitor';
|
2024-02-26 20:25:09 +08:00
|
|
|
import { ProgramContext, TableNameContext } from 'src/lib/hive/HiveSqlParser';
|
2020-09-11 17:39:10 +08:00
|
|
|
|
2023-09-06 15:15:04 +08:00
|
|
|
describe('HiveSQL Visitor Tests', () => {
|
2020-11-25 17:04:55 +08:00
|
|
|
const expectTableName = 'dm_gis.dlv_addr_tc_count';
|
|
|
|
const sql = `select citycode,tc,inc_day from ${expectTableName} where inc_day='20190501' limit 100;`;
|
2021-01-05 17:45:54 +08:00
|
|
|
const parser = new HiveSQL();
|
2020-09-11 17:39:10 +08:00
|
|
|
|
2023-10-16 17:59:28 +08:00
|
|
|
const parseTree = parser.parse(sql, (error) => {
|
2024-02-26 20:25:09 +08:00
|
|
|
console.error('Parse error:', error);
|
2020-09-11 17:39:10 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Visitor visitTableName', () => {
|
|
|
|
let result = '';
|
2023-09-06 15:15:04 +08:00
|
|
|
class MyVisitor extends AbstractParseTreeVisitor<any> implements HiveSqlParserVisitor<any> {
|
2023-05-30 14:44:03 +08:00
|
|
|
defaultResult() {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-02-26 20:25:09 +08:00
|
|
|
visitTableName(ctx: TableNameContext) {
|
|
|
|
result = ctx.getText().toLowerCase();
|
2020-09-11 17:39:10 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-25 17:04:55 +08:00
|
|
|
|
2023-05-04 10:13:05 +08:00
|
|
|
const visitor = new MyVisitor();
|
2024-01-19 21:10:00 +08:00
|
|
|
visitor.visit(parseTree as ProgramContext);
|
2020-09-11 17:39:10 +08:00
|
|
|
|
|
|
|
expect(result).toBe(expectTableName);
|
|
|
|
});
|
|
|
|
});
|