2023-05-30 14:44:03 +08:00
|
|
|
import { AbstractParseTreeVisitor } from 'antlr4ts/tree/AbstractParseTreeVisitor';
|
2023-09-06 15:15:04 +08:00
|
|
|
import { HiveSqlParserVisitor } from '../../../src/lib/hive/HiveSqlParserVisitor';
|
2023-05-04 10:13:05 +08:00
|
|
|
import HiveSQL from '../../../src/parser/hive';
|
2023-09-06 15:15:04 +08:00
|
|
|
import { ProgramContext } 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) => {
|
2020-09-11 17:39:10 +08:00
|
|
|
console.log('Parse error:', error);
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
visitTableName(ctx) {
|
|
|
|
result = ctx.text.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();
|
2023-10-16 17:59:28 +08:00
|
|
|
visitor.visit(parseTree as ProgramContext);
|
2020-09-11 17:39:10 +08:00
|
|
|
|
|
|
|
expect(result).toBe(expectTableName);
|
|
|
|
});
|
|
|
|
});
|