feat: collect entity (#265)
* feat: add text and word utils * feat: add entity collector class * refactor: rename SyntaxContextType to EntityContextType * refactor: improve EntityCollector * feat: improve mysql parser grammar * feat: add mysql entity collector * test: mysql entity collector tests * feat: remove useless method * feat: improve spark grammar file * feat: add spark entity collector * test: spark entity collector unit tests * feat: remove useless code * feat: add queryStatement label * feat: add crateDatabaseStmt * feat: add trino entity collector * feat: rename trinosql to trino * test: trino collect entity unit tests * test: fix spark test * feat(impala): support impale entity collector (#256) * Feat/collect entity hive (#263) * feat(hive): support hive collect entity * feat(hive): update tableAllColumns * feat: replace antlr4ts with antlr4ng * feat(pgsql): pgsql collect entity (#268) * feat(pgsql): pgsql collect entity * feat(pgsql): optimize some name --------- Co-authored-by: zhaoge <> * feat: get word text by token.text * feat: supprt collect db/function and add splitListener (#270) * feat: supprt collect db/function and add splitListner * feat: remove SplitListener interface in baseParser to use SplitListener in root * fix(mysql): fix show create xxx not celloct as createXXXEntity type * test: fix pgsql unit tests * Feat/error recover predicate (#274) * feat: optimize pgsql grammar * feat: add sql parser base * feat: apply SQLParserBase * feat: add geAllEntities method * test: test collect table when missing column * feat: compose collect and suggestion (#276) * feat: mark stmt which contain caret * test: correct name of getAllEntities * test: remove misscolumn unit tests * test: add suggestionWithEntity tests * feat: flink collect entity (#277) * feat: improve flink sql parser * feat: support flink entity collector * test: flink entity collect unit test * feat: move combine entities to parent class --------- Co-authored-by: 霜序 <976060700@qq.com> Co-authored-by: XCynthia <942884029@qq.com>
This commit is contained in:
752
test/parser/hive/contextCollect/entityCollector.test.ts
Normal file
752
test/parser/hive/contextCollect/entityCollector.test.ts
Normal file
@ -0,0 +1,752 @@
|
||||
import { ParseTreeListener } from 'antlr4ng';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { HiveSqlParserListener } from 'src/lib/hive/HiveSqlParserListener';
|
||||
import { EntityContextType } from 'src/parser/common/basic-parser-types';
|
||||
import HiveSQL, { HiveEntityCollector } from 'src/parser/hive';
|
||||
import { HiveSqlSplitListener } from 'src/parser/hive/hiveSplitListener';
|
||||
import { StmtContextType } from 'src/parser/common/entityCollector';
|
||||
|
||||
const commonSql = fs.readFileSync(path.join(__dirname, 'fixtures', 'common.sql'), 'utf-8');
|
||||
|
||||
describe('Hive entity collector tests', () => {
|
||||
const hiveSql = new HiveSQL();
|
||||
const parseTree = hiveSql.parse(commonSql);
|
||||
const splitListener = new HiveSqlSplitListener();
|
||||
hiveSql.listen(splitListener as HiveSqlParserListener, parseTree);
|
||||
|
||||
test('validate common sql', () => {
|
||||
expect(hiveSql.validate(commonSql).length).toBe(0);
|
||||
});
|
||||
|
||||
test('split results', () => {
|
||||
expect(splitListener.statementsContext.length).toBe(18);
|
||||
});
|
||||
|
||||
test('create table by like', () => {
|
||||
const columnCreateTableContext = splitListener.statementsContext[0];
|
||||
|
||||
const collectListener = new HiveEntityCollector(commonSql);
|
||||
hiveSql.listen(collectListener as ParseTreeListener, columnCreateTableContext);
|
||||
|
||||
const allEntities = collectListener.getEntities();
|
||||
|
||||
expect(allEntities.length).toBe(2);
|
||||
|
||||
const tableCreateEntity = allEntities[0];
|
||||
const tableLikeEntity = allEntities[1];
|
||||
|
||||
expect(tableCreateEntity.entityContextType).toBe(EntityContextType.TABLE_CREATE);
|
||||
expect(tableCreateEntity.text).toBe('copy_table');
|
||||
expect(tableCreateEntity.position).toEqual({
|
||||
endColumn: 48,
|
||||
endIndex: 46,
|
||||
line: 1,
|
||||
startColumn: 38,
|
||||
startIndex: 37,
|
||||
});
|
||||
|
||||
expect(tableCreateEntity.belongStmt.stmtContextType).toBe(
|
||||
StmtContextType.CREATE_TABLE_STMT
|
||||
);
|
||||
expect(tableCreateEntity.belongStmt.position).toEqual({
|
||||
endColumn: 66,
|
||||
endIndex: 64,
|
||||
startLine: 1,
|
||||
endLine: 1,
|
||||
startIndex: 0,
|
||||
startColumn: 1,
|
||||
});
|
||||
|
||||
expect(tableCreateEntity.relatedEntities).not.toBeNull();
|
||||
expect(tableCreateEntity.relatedEntities[0]).toEqual(tableLikeEntity);
|
||||
expect(tableCreateEntity.columns).toBeNull();
|
||||
|
||||
expect(tableLikeEntity.entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(tableLikeEntity.text).toBe('origin_table');
|
||||
expect(tableLikeEntity.belongStmt).toBe(tableCreateEntity.belongStmt);
|
||||
});
|
||||
|
||||
test('create table by columns', () => {
|
||||
const columnCreateTableContext = splitListener.statementsContext[1];
|
||||
|
||||
const collectListener = new HiveEntityCollector(commonSql);
|
||||
hiveSql.listen(collectListener as ParseTreeListener, columnCreateTableContext);
|
||||
|
||||
const allEntities = collectListener.getEntities();
|
||||
|
||||
expect(allEntities.length).toBe(1);
|
||||
|
||||
const tableCreateEntity = allEntities[0];
|
||||
|
||||
expect(tableCreateEntity.entityContextType).toBe(EntityContextType.TABLE_CREATE);
|
||||
expect(tableCreateEntity.text).toBe('list_bucket_multiple');
|
||||
expect(tableCreateEntity.position).toEqual({
|
||||
endColumn: 67,
|
||||
endIndex: 133,
|
||||
line: 3,
|
||||
startColumn: 47,
|
||||
startIndex: 114,
|
||||
});
|
||||
|
||||
expect(tableCreateEntity.belongStmt.stmtContextType).toBe(
|
||||
StmtContextType.CREATE_TABLE_STMT
|
||||
);
|
||||
expect(tableCreateEntity.belongStmt.position).toEqual({
|
||||
endColumn: 132,
|
||||
endIndex: 198,
|
||||
endLine: 3,
|
||||
startColumn: 1,
|
||||
startIndex: 68,
|
||||
startLine: 3,
|
||||
});
|
||||
|
||||
expect(tableCreateEntity.relatedEntities).toBeNull();
|
||||
expect(tableCreateEntity.columns).not.toBeNull();
|
||||
expect(tableCreateEntity.columns.length).toBe(3);
|
||||
tableCreateEntity.columns.forEach((columEntity) => {
|
||||
expect(columEntity.entityContextType).toBe(EntityContextType.COLUMN_CREATE);
|
||||
expect(columEntity.belongStmt).toBe(tableCreateEntity.belongStmt);
|
||||
expect(columEntity.text).toBe(
|
||||
commonSql.slice(columEntity.position.startIndex, columEntity.position.endIndex + 1)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
test('create table by select', () => {
|
||||
const columnCreateTableContext = splitListener.statementsContext[2];
|
||||
|
||||
const collectListener = new HiveEntityCollector(commonSql);
|
||||
hiveSql.listen(collectListener as ParseTreeListener, columnCreateTableContext);
|
||||
|
||||
const allEntities = collectListener.getEntities();
|
||||
|
||||
expect(allEntities.length).toBe(2);
|
||||
|
||||
const tableCreateEntity = allEntities[0];
|
||||
const tableFromEntity = allEntities[1];
|
||||
|
||||
expect(tableCreateEntity.entityContextType).toBe(EntityContextType.TABLE_CREATE);
|
||||
expect(tableCreateEntity.text).toBe('derived_table');
|
||||
expect(tableCreateEntity.position).toEqual({
|
||||
endColumn: 41,
|
||||
endIndex: 241,
|
||||
line: 5,
|
||||
startColumn: 28,
|
||||
startIndex: 229,
|
||||
});
|
||||
|
||||
expect(tableCreateEntity.belongStmt.stmtContextType).toBe(
|
||||
StmtContextType.CREATE_TABLE_STMT
|
||||
);
|
||||
expect(tableCreateEntity.belongStmt.position).toEqual({
|
||||
endColumn: 17,
|
||||
endIndex: 279,
|
||||
endLine: 9,
|
||||
startColumn: 1,
|
||||
startIndex: 202,
|
||||
startLine: 5,
|
||||
});
|
||||
|
||||
expect(tableCreateEntity.relatedEntities).not.toBeNull();
|
||||
expect(tableCreateEntity.relatedEntities[0]).toBe(tableFromEntity);
|
||||
expect(tableCreateEntity.columns).toBeNull();
|
||||
|
||||
expect(tableFromEntity.entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(tableFromEntity.text).toBe('origin_table');
|
||||
expect(tableFromEntity.belongStmt.stmtContextType).toBe(StmtContextType.SELECT_STMT);
|
||||
});
|
||||
|
||||
test('create view by select', () => {
|
||||
const columnCreateTableContext = splitListener.statementsContext[3];
|
||||
|
||||
const collectListener = new HiveEntityCollector(commonSql);
|
||||
hiveSql.listen(collectListener as ParseTreeListener, columnCreateTableContext);
|
||||
|
||||
const allEntities = collectListener.getEntities();
|
||||
|
||||
expect(allEntities.length).toBe(2);
|
||||
|
||||
const viewCreateEntity = allEntities[0];
|
||||
const viewSelectEntity = allEntities[1];
|
||||
|
||||
expect(viewCreateEntity.entityContextType).toBe(EntityContextType.VIEW_CREATE);
|
||||
expect(viewCreateEntity.text).toBe('mydb.bro_view');
|
||||
expect(viewCreateEntity.position).toEqual({
|
||||
endColumn: 26,
|
||||
endIndex: 307,
|
||||
line: 11,
|
||||
startColumn: 13,
|
||||
startIndex: 295,
|
||||
});
|
||||
|
||||
expect(viewCreateEntity.belongStmt.stmtContextType).toBe(StmtContextType.CREATE_VIEW_STMT);
|
||||
expect(viewCreateEntity.belongStmt.position).toEqual({
|
||||
endColumn: 31,
|
||||
endIndex: 338,
|
||||
endLine: 12,
|
||||
startColumn: 1,
|
||||
startIndex: 283,
|
||||
startLine: 11,
|
||||
});
|
||||
|
||||
expect(viewCreateEntity.relatedEntities).not.toBeNull();
|
||||
expect(viewCreateEntity.relatedEntities[0]).toBe(viewSelectEntity);
|
||||
expect(viewCreateEntity.columns).toBeNull();
|
||||
|
||||
expect(viewSelectEntity.entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(viewSelectEntity.text).toBe('mydb.sale_tbl');
|
||||
expect(viewSelectEntity.belongStmt.stmtContextType).toBe(StmtContextType.SELECT_STMT);
|
||||
});
|
||||
|
||||
test('create view columns by select', () => {
|
||||
const columnCreateTableContext = splitListener.statementsContext[4];
|
||||
|
||||
const collectListener = new HiveEntityCollector(commonSql);
|
||||
hiveSql.listen(collectListener as ParseTreeListener, columnCreateTableContext);
|
||||
|
||||
const allEntities = collectListener.getEntities();
|
||||
|
||||
expect(allEntities.length).toBe(2);
|
||||
|
||||
const viewCreateEntity = allEntities[0];
|
||||
const viewSelectEntity = allEntities[1];
|
||||
|
||||
expect(viewCreateEntity.entityContextType).toBe(EntityContextType.VIEW_CREATE);
|
||||
expect(viewCreateEntity.text).toBe('mydb.task_view');
|
||||
expect(viewCreateEntity.position).toEqual({
|
||||
endColumn: 27,
|
||||
endIndex: 367,
|
||||
line: 14,
|
||||
startColumn: 13,
|
||||
startIndex: 354,
|
||||
});
|
||||
|
||||
expect(viewCreateEntity.belongStmt.stmtContextType).toBe(StmtContextType.CREATE_VIEW_STMT);
|
||||
expect(viewCreateEntity.belongStmt.position).toEqual({
|
||||
endColumn: 21,
|
||||
endIndex: 596,
|
||||
endLine: 25,
|
||||
startColumn: 1,
|
||||
startIndex: 342,
|
||||
startLine: 14,
|
||||
});
|
||||
|
||||
expect(viewCreateEntity.relatedEntities).not.toBeNull();
|
||||
expect(viewCreateEntity.relatedEntities[0]).toBe(viewSelectEntity);
|
||||
expect(viewCreateEntity.columns).not.toBeNull();
|
||||
expect(viewCreateEntity.columns.length).toBe(3);
|
||||
viewCreateEntity.columns.forEach((columEntity) => {
|
||||
expect(columEntity.entityContextType).toBe(EntityContextType.COLUMN_CREATE);
|
||||
expect(columEntity.belongStmt).toBe(viewCreateEntity.belongStmt);
|
||||
expect(columEntity.text).toBe(
|
||||
commonSql.slice(columEntity.position.startIndex, columEntity.position.endIndex + 1)
|
||||
);
|
||||
});
|
||||
|
||||
expect(viewSelectEntity.entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(viewSelectEntity.text).toBe('task_tbl');
|
||||
expect(viewSelectEntity.belongStmt.stmtContextType).toBe(StmtContextType.SELECT_STMT);
|
||||
});
|
||||
|
||||
test('create materialized view by select', () => {
|
||||
const columnCreateTableContext = splitListener.statementsContext[5];
|
||||
|
||||
const collectListener = new HiveEntityCollector(commonSql);
|
||||
hiveSql.listen(collectListener as ParseTreeListener, columnCreateTableContext);
|
||||
|
||||
const allEntities = collectListener.getEntities();
|
||||
|
||||
expect(allEntities.length).toBe(2);
|
||||
|
||||
const viewCreateEntity = allEntities[0];
|
||||
const viewSelectEntity = allEntities[1];
|
||||
|
||||
expect(viewCreateEntity.entityContextType).toBe(EntityContextType.VIEW_CREATE);
|
||||
expect(viewCreateEntity.text).toBe('mydb.bro_view');
|
||||
expect(viewCreateEntity.position).toEqual({
|
||||
endColumn: 53,
|
||||
endIndex: 651,
|
||||
line: 27,
|
||||
startColumn: 40,
|
||||
startIndex: 639,
|
||||
});
|
||||
|
||||
expect(viewCreateEntity.belongStmt.stmtContextType).toBe(StmtContextType.CREATE_VIEW_STMT);
|
||||
expect(viewCreateEntity.belongStmt.position).toEqual({
|
||||
endColumn: 31,
|
||||
endIndex: 715,
|
||||
endLine: 30,
|
||||
startColumn: 1,
|
||||
startIndex: 600,
|
||||
startLine: 27,
|
||||
});
|
||||
|
||||
expect(viewCreateEntity.relatedEntities).not.toBeNull();
|
||||
expect(viewCreateEntity.relatedEntities[0]).toBe(viewSelectEntity);
|
||||
expect(viewCreateEntity.columns).toBeNull();
|
||||
|
||||
expect(viewSelectEntity.entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(viewSelectEntity.text).toBe('mydb.sale_tbl');
|
||||
expect(viewSelectEntity.belongStmt.stmtContextType).toBe(StmtContextType.SELECT_STMT);
|
||||
});
|
||||
|
||||
test('select table default', () => {
|
||||
const columnCreateTableContext = splitListener.statementsContext[6];
|
||||
|
||||
const collectListener = new HiveEntityCollector(commonSql);
|
||||
hiveSql.listen(collectListener as ParseTreeListener, columnCreateTableContext);
|
||||
|
||||
const allEntities = collectListener.getEntities();
|
||||
|
||||
expect(allEntities.length).toBe(1);
|
||||
|
||||
const selectTableEntity = allEntities[0];
|
||||
|
||||
expect(selectTableEntity.entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(selectTableEntity.text).toBe('table_name_1');
|
||||
expect(selectTableEntity.position).toEqual({
|
||||
endColumn: 36,
|
||||
endIndex: 753,
|
||||
line: 32,
|
||||
startColumn: 24,
|
||||
startIndex: 742,
|
||||
});
|
||||
|
||||
expect(selectTableEntity.belongStmt.stmtContextType).toBe(StmtContextType.SELECT_STMT);
|
||||
expect(selectTableEntity.belongStmt.position).toEqual({
|
||||
endColumn: 36,
|
||||
endIndex: 753,
|
||||
endLine: 32,
|
||||
startColumn: 1,
|
||||
startIndex: 719,
|
||||
startLine: 32,
|
||||
});
|
||||
|
||||
expect(selectTableEntity.columns).toBeNull();
|
||||
expect(selectTableEntity.relatedEntities).toBeNull();
|
||||
});
|
||||
|
||||
test('select table with join', () => {
|
||||
const columnCreateTableContext = splitListener.statementsContext[7];
|
||||
|
||||
const collectListener = new HiveEntityCollector(commonSql);
|
||||
hiveSql.listen(collectListener as ParseTreeListener, columnCreateTableContext);
|
||||
|
||||
const allEntities = collectListener.getEntities();
|
||||
|
||||
expect(allEntities.length).toBe(2);
|
||||
|
||||
const selectTableEntity = allEntities[0];
|
||||
const joinTableEntity = allEntities[1];
|
||||
|
||||
expect(selectTableEntity.entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(selectTableEntity.text).toBe('a');
|
||||
expect(selectTableEntity.position).toEqual({
|
||||
endColumn: 18,
|
||||
endIndex: 773,
|
||||
line: 34,
|
||||
startColumn: 17,
|
||||
startIndex: 773,
|
||||
});
|
||||
|
||||
expect(selectTableEntity.belongStmt.stmtContextType).toBe(StmtContextType.SELECT_STMT);
|
||||
expect(selectTableEntity.belongStmt.position).toEqual({
|
||||
endColumn: 74,
|
||||
endIndex: 829,
|
||||
endLine: 34,
|
||||
startColumn: 1,
|
||||
startIndex: 757,
|
||||
startLine: 34,
|
||||
});
|
||||
|
||||
expect(selectTableEntity.columns).toBeNull();
|
||||
expect(selectTableEntity.relatedEntities).toBeNull();
|
||||
|
||||
expect(selectTableEntity.belongStmt).toEqual(joinTableEntity.belongStmt);
|
||||
expect(joinTableEntity.text).toBe('b');
|
||||
expect(joinTableEntity.columns).toBeNull();
|
||||
expect(joinTableEntity.relatedEntities).toBeNull();
|
||||
});
|
||||
|
||||
test('from select table', () => {
|
||||
const columnCreateTableContext = splitListener.statementsContext[8];
|
||||
|
||||
const collectListener = new HiveEntityCollector(commonSql);
|
||||
hiveSql.listen(collectListener as ParseTreeListener, columnCreateTableContext);
|
||||
|
||||
const allEntities = collectListener.getEntities();
|
||||
|
||||
expect(allEntities.length).toBe(1);
|
||||
|
||||
const selectTableEntity = allEntities[0];
|
||||
|
||||
expect(selectTableEntity.entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(selectTableEntity.text).toBe('table_name_1');
|
||||
expect(selectTableEntity.position).toEqual({
|
||||
endColumn: 18,
|
||||
endIndex: 849,
|
||||
line: 36,
|
||||
startColumn: 6,
|
||||
startIndex: 838,
|
||||
});
|
||||
|
||||
expect(selectTableEntity.belongStmt.stmtContextType).toBe(StmtContextType.SELECT_STMT);
|
||||
expect(selectTableEntity.belongStmt.position).toEqual({
|
||||
endColumn: 36,
|
||||
endIndex: 867,
|
||||
endLine: 36,
|
||||
startColumn: 1,
|
||||
startIndex: 833,
|
||||
startLine: 36,
|
||||
});
|
||||
|
||||
expect(selectTableEntity.columns).toBeNull();
|
||||
expect(selectTableEntity.relatedEntities).toBeNull();
|
||||
});
|
||||
|
||||
test('from select table with join', () => {
|
||||
const columnCreateTableContext = splitListener.statementsContext[9];
|
||||
|
||||
const collectListener = new HiveEntityCollector(commonSql);
|
||||
hiveSql.listen(collectListener as ParseTreeListener, columnCreateTableContext);
|
||||
|
||||
const allEntities = collectListener.getEntities();
|
||||
|
||||
expect(allEntities.length).toBe(2);
|
||||
|
||||
const selectTableEntity = allEntities[0];
|
||||
const joinTableEntity = allEntities[1];
|
||||
|
||||
expect(selectTableEntity.entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(selectTableEntity.text).toBe('a');
|
||||
expect(selectTableEntity.position).toEqual({
|
||||
endColumn: 7,
|
||||
endIndex: 876,
|
||||
line: 38,
|
||||
startColumn: 6,
|
||||
startIndex: 876,
|
||||
});
|
||||
|
||||
expect(selectTableEntity.belongStmt.stmtContextType).toBe(StmtContextType.SELECT_STMT);
|
||||
expect(selectTableEntity.belongStmt.position).toEqual({
|
||||
endColumn: 74,
|
||||
endIndex: 943,
|
||||
endLine: 38,
|
||||
startColumn: 1,
|
||||
startIndex: 871,
|
||||
startLine: 38,
|
||||
});
|
||||
|
||||
expect(selectTableEntity.columns).toBeNull();
|
||||
expect(selectTableEntity.relatedEntities).toBeNull();
|
||||
|
||||
expect(selectTableEntity.belongStmt).toEqual(joinTableEntity.belongStmt);
|
||||
expect(joinTableEntity.text).toBe('b');
|
||||
expect(joinTableEntity.columns).toBeNull();
|
||||
expect(joinTableEntity.relatedEntities).toBeNull();
|
||||
});
|
||||
|
||||
test('insert table with values', () => {
|
||||
const columnCreateTableContext = splitListener.statementsContext[10];
|
||||
|
||||
const collectListener = new HiveEntityCollector(commonSql);
|
||||
hiveSql.listen(collectListener as ParseTreeListener, columnCreateTableContext);
|
||||
|
||||
const allEntities = collectListener.getEntities();
|
||||
|
||||
expect(allEntities.length).toBe(1);
|
||||
|
||||
const insertTableEntity = allEntities[0];
|
||||
|
||||
expect(insertTableEntity.entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(insertTableEntity.text).toBe('students');
|
||||
expect(insertTableEntity.position).toEqual({
|
||||
endColumn: 27,
|
||||
endIndex: 972,
|
||||
line: 40,
|
||||
startColumn: 19,
|
||||
startIndex: 965,
|
||||
});
|
||||
|
||||
expect(insertTableEntity.belongStmt.stmtContextType).toBe(StmtContextType.INSERT_STMT);
|
||||
expect(insertTableEntity.belongStmt.position).toEqual({
|
||||
endColumn: 66,
|
||||
endIndex: 1045,
|
||||
endLine: 41,
|
||||
startColumn: 1,
|
||||
startIndex: 947,
|
||||
startLine: 40,
|
||||
});
|
||||
|
||||
expect(insertTableEntity.columns).toBeNull();
|
||||
expect(insertTableEntity.relatedEntities).toBeNull();
|
||||
});
|
||||
|
||||
test('insert table use select', () => {
|
||||
const columnCreateTableContext = splitListener.statementsContext[11];
|
||||
|
||||
const collectListener = new HiveEntityCollector(commonSql);
|
||||
hiveSql.listen(collectListener as ParseTreeListener, columnCreateTableContext);
|
||||
|
||||
const allEntities = collectListener.getEntities();
|
||||
|
||||
expect(allEntities.length).toBe(2);
|
||||
|
||||
const insertTableEntity = allEntities[0];
|
||||
const fromTableEntity = allEntities[1];
|
||||
|
||||
expect(insertTableEntity.entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(insertTableEntity.text).toBe('table_name');
|
||||
expect(insertTableEntity.position).toEqual({
|
||||
endColumn: 23,
|
||||
endIndex: 1070,
|
||||
line: 43,
|
||||
startColumn: 13,
|
||||
startIndex: 1061,
|
||||
});
|
||||
|
||||
expect(insertTableEntity.belongStmt.stmtContextType).toBe(StmtContextType.INSERT_STMT);
|
||||
expect(insertTableEntity.belongStmt.position).toEqual({
|
||||
endColumn: 18,
|
||||
endIndex: 1183,
|
||||
endLine: 46,
|
||||
startColumn: 1,
|
||||
startIndex: 1049,
|
||||
startLine: 43,
|
||||
});
|
||||
|
||||
expect(insertTableEntity.columns).toBeNull();
|
||||
expect(insertTableEntity.relatedEntities).toBeNull();
|
||||
|
||||
expect(fromTableEntity.belongStmt.stmtContextType).toBe(StmtContextType.SELECT_STMT);
|
||||
expect(fromTableEntity.text).toBe('source_table');
|
||||
expect(fromTableEntity.belongStmt.parentStmt).toEqual(insertTableEntity.belongStmt);
|
||||
expect(fromTableEntity.belongStmt.rootStmt).toBe(insertTableEntity.belongStmt);
|
||||
});
|
||||
|
||||
test('from insert table use select', () => {
|
||||
const columnCreateTableContext = splitListener.statementsContext[12];
|
||||
|
||||
const collectListener = new HiveEntityCollector(commonSql);
|
||||
hiveSql.listen(collectListener as ParseTreeListener, columnCreateTableContext);
|
||||
|
||||
const allEntities = collectListener.getEntities();
|
||||
|
||||
expect(allEntities.length).toBe(2);
|
||||
|
||||
const fromTableEntity = allEntities[0];
|
||||
const insertTableEntity = allEntities[1];
|
||||
|
||||
expect(insertTableEntity.entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(insertTableEntity.text).toBe('page_view');
|
||||
expect(insertTableEntity.position).toEqual({
|
||||
endColumn: 33,
|
||||
endIndex: 1241,
|
||||
line: 49,
|
||||
startColumn: 24,
|
||||
startIndex: 1233,
|
||||
});
|
||||
|
||||
expect(insertTableEntity.belongStmt.stmtContextType).toBe(StmtContextType.INSERT_STMT);
|
||||
expect(insertTableEntity.belongStmt.position).toEqual({
|
||||
endColumn: 93,
|
||||
endIndex: 1370,
|
||||
endLine: 50,
|
||||
startColumn: 1,
|
||||
startIndex: 1187,
|
||||
startLine: 48,
|
||||
});
|
||||
|
||||
expect(insertTableEntity.columns).toBeNull();
|
||||
expect(insertTableEntity.relatedEntities).toBeNull();
|
||||
|
||||
expect(fromTableEntity.belongStmt.stmtContextType).toBe(StmtContextType.INSERT_STMT);
|
||||
expect(fromTableEntity.text).toBe('page_view_stg');
|
||||
expect(fromTableEntity.belongStmt).toEqual(insertTableEntity.belongStmt);
|
||||
});
|
||||
|
||||
test('create db', () => {
|
||||
const columnCreateTableContext = splitListener.statementsContext[13];
|
||||
|
||||
const collectListener = new HiveEntityCollector(commonSql);
|
||||
hiveSql.listen(collectListener as ParseTreeListener, columnCreateTableContext);
|
||||
|
||||
const allEntities = collectListener.getEntities();
|
||||
|
||||
expect(allEntities.length).toBe(1);
|
||||
|
||||
const dbEntity = allEntities[0];
|
||||
|
||||
expect(dbEntity.entityContextType).toBe(EntityContextType.DATABASE_CREATE);
|
||||
expect(dbEntity.text).toBe('mydb');
|
||||
expect(dbEntity.position).toEqual({
|
||||
endColumn: 21,
|
||||
endIndex: 1393,
|
||||
line: 52,
|
||||
startColumn: 17,
|
||||
startIndex: 1390,
|
||||
});
|
||||
|
||||
expect(dbEntity.belongStmt.stmtContextType).toBe(StmtContextType.CREATE_DATABASE_STMT);
|
||||
expect(dbEntity.belongStmt.position).toEqual({
|
||||
endColumn: 21,
|
||||
endIndex: 1393,
|
||||
endLine: 52,
|
||||
startColumn: 1,
|
||||
startIndex: 1374,
|
||||
startLine: 52,
|
||||
});
|
||||
|
||||
expect(dbEntity.columns).toBeNull();
|
||||
expect(dbEntity.relatedEntities).toBeNull();
|
||||
});
|
||||
|
||||
test('create remote db', () => {
|
||||
const columnCreateTableContext = splitListener.statementsContext[14];
|
||||
|
||||
const collectListener = new HiveEntityCollector(commonSql);
|
||||
hiveSql.listen(collectListener as ParseTreeListener, columnCreateTableContext);
|
||||
|
||||
const allEntities = collectListener.getEntities();
|
||||
|
||||
expect(allEntities.length).toBe(1);
|
||||
|
||||
const dbEntity = allEntities[0];
|
||||
|
||||
expect(dbEntity.entityContextType).toBe(EntityContextType.DATABASE_CREATE);
|
||||
expect(dbEntity.text).toBe('mydb');
|
||||
expect(dbEntity.position).toEqual({
|
||||
endColumn: 28,
|
||||
endIndex: 1423,
|
||||
line: 54,
|
||||
startColumn: 24,
|
||||
startIndex: 1420,
|
||||
});
|
||||
|
||||
expect(dbEntity.belongStmt.stmtContextType).toBe(StmtContextType.CREATE_DATABASE_STMT);
|
||||
expect(dbEntity.belongStmt.position).toEqual({
|
||||
endColumn: 28,
|
||||
endIndex: 1423,
|
||||
endLine: 54,
|
||||
startColumn: 1,
|
||||
startIndex: 1397,
|
||||
startLine: 54,
|
||||
});
|
||||
|
||||
expect(dbEntity.columns).toBeNull();
|
||||
expect(dbEntity.relatedEntities).toBeNull();
|
||||
});
|
||||
|
||||
test('show locks db', () => {
|
||||
const dbContext = splitListener.statementsContext[15];
|
||||
|
||||
const collectListener = new HiveEntityCollector(commonSql);
|
||||
hiveSql.listen(collectListener as ParseTreeListener, dbContext);
|
||||
|
||||
const allEntities = collectListener.getEntities();
|
||||
|
||||
expect(allEntities.length).toBe(1);
|
||||
|
||||
const dbEntity = allEntities[0];
|
||||
|
||||
expect(dbEntity.entityContextType).toBe(EntityContextType.DATABASE);
|
||||
expect(dbEntity.text).toBe('db1');
|
||||
expect(dbEntity.position).toEqual({
|
||||
endColumn: 24,
|
||||
endIndex: 1449,
|
||||
line: 56,
|
||||
startColumn: 21,
|
||||
startIndex: 1447,
|
||||
});
|
||||
|
||||
expect(dbEntity.belongStmt.stmtContextType).toBe(StmtContextType.COMMON_STMT);
|
||||
expect(dbEntity.belongStmt.position).toEqual({
|
||||
endColumn: 25,
|
||||
endIndex: 1450,
|
||||
endLine: 56,
|
||||
startColumn: 1,
|
||||
startIndex: 1427,
|
||||
startLine: 56,
|
||||
});
|
||||
|
||||
expect(dbEntity.columns).toBeNull();
|
||||
expect(dbEntity.relatedEntities).toBeNull();
|
||||
});
|
||||
|
||||
test('create function', () => {
|
||||
const functionCreateContext = splitListener.statementsContext[16];
|
||||
|
||||
const collectListener = new HiveEntityCollector(commonSql);
|
||||
hiveSql.listen(collectListener as ParseTreeListener, functionCreateContext);
|
||||
|
||||
const allEntities = collectListener.getEntities();
|
||||
|
||||
expect(allEntities.length).toBe(1);
|
||||
|
||||
const functionEntity = allEntities[0];
|
||||
|
||||
expect(functionEntity.entityContextType).toBe(EntityContextType.FUNCTION_CREATE);
|
||||
expect(functionEntity.text).toBe('base_analizer');
|
||||
expect(functionEntity.position).toEqual({
|
||||
endColumn: 30,
|
||||
endIndex: 1481,
|
||||
line: 58,
|
||||
startColumn: 17,
|
||||
startIndex: 1469,
|
||||
});
|
||||
|
||||
expect(functionEntity.belongStmt.stmtContextType).toBe(
|
||||
StmtContextType.CREATE_FUNCTION_STMT
|
||||
);
|
||||
expect(functionEntity.belongStmt.position).toEqual({
|
||||
endColumn: 30,
|
||||
endIndex: 1481,
|
||||
endLine: 58,
|
||||
startColumn: 17,
|
||||
startIndex: 1469,
|
||||
startLine: 58,
|
||||
});
|
||||
|
||||
expect(functionEntity.columns).toBeNull();
|
||||
expect(functionEntity.relatedEntities).toBeNull();
|
||||
});
|
||||
|
||||
test('create temporary function', () => {
|
||||
const functionCreateContext = splitListener.statementsContext[17];
|
||||
|
||||
const collectListener = new HiveEntityCollector(commonSql);
|
||||
hiveSql.listen(collectListener as ParseTreeListener, functionCreateContext);
|
||||
|
||||
const allEntities = collectListener.getEntities();
|
||||
|
||||
expect(allEntities.length).toBe(1);
|
||||
|
||||
const functionEntity = allEntities[0];
|
||||
|
||||
expect(functionEntity.entityContextType).toBe(EntityContextType.FUNCTION_CREATE);
|
||||
expect(functionEntity.text).toBe('flat_analizer');
|
||||
expect(functionEntity.position).toEqual({
|
||||
endColumn: 40,
|
||||
endIndex: 1549,
|
||||
line: 60,
|
||||
startColumn: 27,
|
||||
startIndex: 1537,
|
||||
});
|
||||
|
||||
expect(functionEntity.belongStmt.stmtContextType).toBe(
|
||||
StmtContextType.CREATE_FUNCTION_STMT
|
||||
);
|
||||
expect(functionEntity.belongStmt.position).toEqual({
|
||||
endColumn: 40,
|
||||
endIndex: 1549,
|
||||
endLine: 60,
|
||||
startColumn: 27,
|
||||
startIndex: 1537,
|
||||
startLine: 60,
|
||||
});
|
||||
|
||||
expect(functionEntity.columns).toBeNull();
|
||||
expect(functionEntity.relatedEntities).toBeNull();
|
||||
});
|
||||
});
|
60
test/parser/hive/contextCollect/fixtures/common.sql
Normal file
60
test/parser/hive/contextCollect/fixtures/common.sql
Normal file
@ -0,0 +1,60 @@
|
||||
CREATE TEMPORARY TABLE IF NOT EXISTS copy_table LIKE origin_table;
|
||||
|
||||
CREATE TEMPORARY EXTERNAL TABLE IF NOT EXISTS list_bucket_multiple (col1 STRING, col2 INT, col3 STRING) COMMENT 'this is a comment';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS derived_table AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
origin_table;
|
||||
|
||||
CREATE VIEW mydb.bro_view
|
||||
AS SELECT * FROM mydb.sale_tbl;
|
||||
|
||||
CREATE VIEW mydb.task_view (
|
||||
taskId COMMENT '任务id',
|
||||
taskName COMMENT '任务名称',
|
||||
taskRunTime COMMENT '任务运行时长'
|
||||
)
|
||||
COMMENT '一个任务信息视图'
|
||||
TBLPROPERTIES(
|
||||
'author'='hayden'
|
||||
)
|
||||
AS SELECT DISTINCT id, `name`, runtime
|
||||
FROM task_tbl
|
||||
WHERE type='day';
|
||||
|
||||
CREATE MATERIALIZED VIEW IF NOT EXISTS mydb.bro_view
|
||||
DISABLE REWRITE
|
||||
COMMENT '一个测试视图'
|
||||
AS SELECT * FROM mydb.sale_tbl;
|
||||
|
||||
SELECT col1, col2 FROM table_name_1;
|
||||
|
||||
SELECT a.* FROM a JOIN b ON (a.id = b.id AND a.department = b.department);
|
||||
|
||||
FROM table_name_1 SELECT col1, col2;
|
||||
|
||||
FROM a JOIN b ON (a.id = b.id AND a.department = b.department) SELECT a.*;
|
||||
|
||||
INSERT INTO TABLE students(a,b,c)
|
||||
VALUES ('fred flintstone', 35, 1.28), ('barney rubble', 32, 2.32);
|
||||
|
||||
INSERT INTO table_name PARTITION (country, state)
|
||||
SELECT col1, col2,
|
||||
CONCAT(country, '_', state) AS country_state
|
||||
FROM source_table;
|
||||
|
||||
FROM page_view_stg pvs
|
||||
INSERT OVERWRITE TABLE page_view PARTITION(dt='2008-06-08', country)
|
||||
SELECT pvs.viewTime, pvs.userid, pvs.page_url, pvs.referrer_url, null, null, pvs.ip, pvs.cnt;
|
||||
|
||||
CREATE DATABASE mydb;
|
||||
|
||||
CREATE REMOTE DATABASE mydb;
|
||||
|
||||
SHOW LOCKS DATABASE db1;
|
||||
|
||||
CREATE FUNCTION base_analizer AS 'com.udf.BaseFieldUDF';
|
||||
|
||||
CREATE TEMPORARY FUNCTION flat_analizer AS 'com.udtf.EventJsonUDTF';
|
@ -0,0 +1,23 @@
|
||||
SELECT FROM tb1
|
||||
|
||||
SELECT col1, col2, FROM tb
|
||||
|
||||
FROM table_name_1 SELECT ; -- TODO: request semicolon
|
||||
|
||||
FROM table_name_1 SELECT col1, col2, ; -- TODO: request semicolon
|
||||
|
||||
FROM a JOIN b ON (a.id = b.id AND a.department = b.department) SELECT ; -- TODO: request semicolon
|
||||
|
||||
FROM a JOIN b ON (a.id = b.id AND a.department = b.department) SELECT a.*, ; -- TODO: request semicolon
|
||||
|
||||
FROM page_view_stg pvs INSERT OVERWRITE TABLE page_view PARTITION(dt='2008-06-08', country) SELECT ; -- TODO: request semicolon
|
||||
|
||||
FROM page_view_stg pvs INSERT OVERWRITE TABLE page_view PARTITION(dt='2008-06-08', country) SELECT id, ; -- TODO: request semicolon
|
||||
|
||||
INSERT INTO insert_tb PARTITION (country, state) SELECT col1, col2, country, state FROM ( SELECT FROM inside_tb ) subquery
|
||||
|
||||
INSERT INTO insert_tb PARTITION (country, state) SELECT col1, col2, country, state FROM ( SELECT id, FROM inside_tb ) subquery
|
||||
|
||||
CREATE TABLE IF NOT EXISTS derived_table AS SELECT FROM origin_table
|
||||
|
||||
CREATE TABLE IF NOT EXISTS derived_table AS SELECT id, FROM origin_table
|
@ -32,4 +32,10 @@ MERGE INTO tablename USING tablename2 ON (tablename.id = tablename2.id) WHEN MAT
|
||||
|
||||
ALTER TABLE tbl CHANGE COLUMN ;
|
||||
|
||||
ALTER TABLE tbl CHANGE COLUMN tbl.oldcol new ;
|
||||
ALTER TABLE tbl CHANGE COLUMN tbl.oldcol new ;
|
||||
|
||||
FROM table_name_1 SELECT col1, col2;
|
||||
|
||||
FROM a JOIN b ON (a.id = b.id AND a.department = b.department) SELECT a.*;
|
||||
|
||||
FROM page_view_stg INSERT;
|
@ -1,7 +1,7 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import HiveSQL from 'src/parser/hive';
|
||||
import { CaretPosition, SyntaxContextType } from 'src/parser/common/basic-parser-types';
|
||||
import { CaretPosition, EntityContextType } from 'src/parser/common/basic-parser-types';
|
||||
|
||||
const syntaxSql = fs.readFileSync(
|
||||
path.join(__dirname, 'fixtures', 'multipleStatement.sql'),
|
||||
@ -18,7 +18,7 @@ describe('HiveSQL Multiple Statements Syntax Suggestion', () => {
|
||||
};
|
||||
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
||||
(syn) => syn.syntaxContextType === EntityContextType.TABLE
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -32,7 +32,7 @@ describe('HiveSQL Multiple Statements Syntax Suggestion', () => {
|
||||
};
|
||||
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE_CREATE
|
||||
(syn) => syn.syntaxContextType === EntityContextType.TABLE_CREATE
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -46,7 +46,7 @@ describe('HiveSQL Multiple Statements Syntax Suggestion', () => {
|
||||
};
|
||||
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
||||
(syn) => syn.syntaxContextType === EntityContextType.TABLE
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -60,7 +60,7 @@ describe('HiveSQL Multiple Statements Syntax Suggestion', () => {
|
||||
};
|
||||
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
||||
(syn) => syn.syntaxContextType === EntityContextType.TABLE
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
|
310
test/parser/hive/suggestion/suggestionWithEntity.test.ts
Normal file
310
test/parser/hive/suggestion/suggestionWithEntity.test.ts
Normal file
@ -0,0 +1,310 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import HiveSQL from 'src/parser/hive';
|
||||
import { CaretPosition, EntityContextType } from 'src/parser/common/basic-parser-types';
|
||||
import { commentOtherLine } from 'test/helper';
|
||||
|
||||
const syntaxSql = fs.readFileSync(
|
||||
path.join(__dirname, 'fixtures', 'suggestionWithEntity.sql'),
|
||||
'utf-8'
|
||||
);
|
||||
|
||||
describe('Hive SQL Syntax Suggestion with collect entity', () => {
|
||||
const hive = new HiveSQL();
|
||||
|
||||
test('Validate Syntax SQL', () => {
|
||||
expect(hive.validate(syntaxSql).length).not.toBe(0);
|
||||
});
|
||||
|
||||
test('select with no columns', () => {
|
||||
const pos: CaretPosition = {
|
||||
lineNumber: 1,
|
||||
column: 8,
|
||||
};
|
||||
const sql = commentOtherLine(syntaxSql, pos.lineNumber);
|
||||
|
||||
const syntaxes = hive.getSuggestionAtCaretPosition(sql, pos)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
|
||||
);
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
|
||||
|
||||
const entities = hive.getAllEntities(sql, pos);
|
||||
expect(entities.length).toBe(1);
|
||||
expect(entities[0].text).toBe('tb1');
|
||||
expect(entities[0].entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(entities[0].belongStmt.isContainCaret).toBeTruthy();
|
||||
});
|
||||
|
||||
test('select with columns with columns and trailing comma', () => {
|
||||
const pos: CaretPosition = {
|
||||
lineNumber: 3,
|
||||
column: 20,
|
||||
};
|
||||
const sql = commentOtherLine(syntaxSql, pos.lineNumber);
|
||||
|
||||
const syntaxes = hive.getSuggestionAtCaretPosition(sql, pos)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
|
||||
);
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
|
||||
|
||||
const entities = hive.getAllEntities(sql, pos);
|
||||
expect(entities.length).toBe(1);
|
||||
expect(entities[0].text).toBe('tb');
|
||||
expect(entities[0].entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(entities[0].belongStmt.isContainCaret).toBeTruthy();
|
||||
});
|
||||
|
||||
test('from table select with no column', () => {
|
||||
const pos: CaretPosition = {
|
||||
lineNumber: 5,
|
||||
column: 26,
|
||||
};
|
||||
const sql = commentOtherLine(syntaxSql, pos.lineNumber);
|
||||
|
||||
const syntaxes = hive.getSuggestionAtCaretPosition(sql, pos)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
|
||||
);
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
|
||||
|
||||
const entities = hive.getAllEntities(sql, pos);
|
||||
expect(entities.length).toBe(1);
|
||||
expect(entities[0].text).toBe('table_name_1');
|
||||
expect(entities[0].entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(entities[0].belongStmt.rootStmt.isContainCaret).toBeTruthy();
|
||||
});
|
||||
|
||||
test('from table select with with columns and trailing comma', () => {
|
||||
const pos: CaretPosition = {
|
||||
lineNumber: 7,
|
||||
column: 38,
|
||||
};
|
||||
const sql = commentOtherLine(syntaxSql, pos.lineNumber);
|
||||
|
||||
const syntaxes = hive.getSuggestionAtCaretPosition(sql, pos)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
|
||||
);
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
|
||||
|
||||
const entities = hive.getAllEntities(sql, pos);
|
||||
expect(entities.length).toBe(1);
|
||||
expect(entities[0].text).toBe('table_name_1');
|
||||
expect(entities[0].entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(entities[0].belongStmt.rootStmt.isContainCaret).toBeTruthy();
|
||||
});
|
||||
|
||||
test('from joined table select with no column', () => {
|
||||
const pos: CaretPosition = {
|
||||
lineNumber: 9,
|
||||
column: 71,
|
||||
};
|
||||
const sql = commentOtherLine(syntaxSql, pos.lineNumber);
|
||||
|
||||
const syntaxes = hive.getSuggestionAtCaretPosition(sql, pos)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
|
||||
);
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
|
||||
|
||||
const entities = hive.getAllEntities(sql, pos);
|
||||
expect(entities.length).toBe(2);
|
||||
expect(entities[0].text).toBe('a');
|
||||
expect(entities[0].entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(entities[0].belongStmt.isContainCaret).toBeFalsy();
|
||||
expect(entities[0].belongStmt.rootStmt.isContainCaret).toBeTruthy();
|
||||
|
||||
expect(entities[1].text).toBe('b');
|
||||
expect(entities[1].entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(entities[1].belongStmt.isContainCaret).toBeFalsy();
|
||||
expect(entities[1].belongStmt.rootStmt.isContainCaret).toBeTruthy();
|
||||
});
|
||||
|
||||
test('from joined table select with columns and trailing comma', () => {
|
||||
const pos: CaretPosition = {
|
||||
lineNumber: 11,
|
||||
column: 76,
|
||||
};
|
||||
const sql = commentOtherLine(syntaxSql, pos.lineNumber);
|
||||
|
||||
const syntaxes = hive.getSuggestionAtCaretPosition(sql, pos)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
|
||||
);
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
|
||||
|
||||
const entities = hive.getAllEntities(sql, pos);
|
||||
expect(entities.length).toBe(2);
|
||||
expect(entities[0].text).toBe('a');
|
||||
expect(entities[0].entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(entities[0].belongStmt.isContainCaret).toBeFalsy();
|
||||
expect(entities[0].belongStmt.rootStmt.isContainCaret).toBeTruthy();
|
||||
|
||||
expect(entities[1].text).toBe('b');
|
||||
expect(entities[1].entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(entities[1].belongStmt.isContainCaret).toBeFalsy();
|
||||
expect(entities[1].belongStmt.rootStmt.isContainCaret).toBeTruthy();
|
||||
});
|
||||
|
||||
test('from table insert into table select no columns', () => {
|
||||
const pos: CaretPosition = {
|
||||
lineNumber: 13,
|
||||
column: 100,
|
||||
};
|
||||
const sql = commentOtherLine(syntaxSql, pos.lineNumber);
|
||||
|
||||
const syntaxes = hive.getSuggestionAtCaretPosition(sql, pos)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
|
||||
);
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
|
||||
|
||||
const entities = hive.getAllEntities(sql, pos);
|
||||
expect(entities.length).toBe(2);
|
||||
expect(entities[0].text).toBe('page_view_stg');
|
||||
expect(entities[0].entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(entities[0].belongStmt.isContainCaret).toBeFalsy();
|
||||
expect(entities[0].belongStmt.rootStmt.isContainCaret).toBeTruthy();
|
||||
|
||||
expect(entities[1].text).toBe('page_view');
|
||||
expect(entities[1].entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(entities[1].belongStmt.isContainCaret).toBeFalsy();
|
||||
expect(entities[1].belongStmt.rootStmt.isContainCaret).toBeTruthy();
|
||||
});
|
||||
|
||||
test('from table insert into table select with column and trailing comma', () => {
|
||||
const pos: CaretPosition = {
|
||||
lineNumber: 15,
|
||||
column: 104,
|
||||
};
|
||||
const sql = commentOtherLine(syntaxSql, pos.lineNumber);
|
||||
|
||||
const syntaxes = hive.getSuggestionAtCaretPosition(sql, pos)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
|
||||
);
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
|
||||
|
||||
const entities = hive.getAllEntities(sql, pos);
|
||||
expect(entities.length).toBe(2);
|
||||
expect(entities[0].text).toBe('page_view_stg');
|
||||
expect(entities[0].entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(entities[0].belongStmt.isContainCaret).toBeFalsy();
|
||||
expect(entities[0].belongStmt.rootStmt.isContainCaret).toBeTruthy();
|
||||
|
||||
expect(entities[1].text).toBe('page_view');
|
||||
expect(entities[1].entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(entities[1].belongStmt.isContainCaret).toBeFalsy();
|
||||
expect(entities[1].belongStmt.rootStmt.isContainCaret).toBeTruthy();
|
||||
});
|
||||
|
||||
test('insert into from nested query with no column', () => {
|
||||
const pos: CaretPosition = {
|
||||
lineNumber: 17,
|
||||
column: 98,
|
||||
};
|
||||
const sql = commentOtherLine(syntaxSql, pos.lineNumber);
|
||||
|
||||
const syntaxes = hive.getSuggestionAtCaretPosition(sql, pos)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
|
||||
);
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
|
||||
|
||||
const entities = hive.getAllEntities(sql, pos);
|
||||
expect(entities.length).toBe(2);
|
||||
expect(entities[0].text).toBe('insert_tb');
|
||||
expect(entities[0].entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(entities[0].belongStmt.isContainCaret).toBeTruthy();
|
||||
|
||||
expect(entities[1].text).toBe('inside_tb');
|
||||
expect(entities[1].entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(entities[1].belongStmt.isContainCaret).toBeTruthy();
|
||||
});
|
||||
|
||||
test('insert into from nested query with columns and trailing comma', () => {
|
||||
const pos: CaretPosition = {
|
||||
lineNumber: 19,
|
||||
column: 102,
|
||||
};
|
||||
const sql = commentOtherLine(syntaxSql, pos.lineNumber);
|
||||
|
||||
const syntaxes = hive.getSuggestionAtCaretPosition(sql, pos)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
|
||||
);
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
|
||||
|
||||
const entities = hive.getAllEntities(sql, pos);
|
||||
expect(entities.length).toBe(2);
|
||||
expect(entities[0].text).toBe('insert_tb');
|
||||
expect(entities[0].entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(entities[0].belongStmt.isContainCaret).toBeTruthy();
|
||||
|
||||
expect(entities[1].text).toBe('inside_tb');
|
||||
expect(entities[1].entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(entities[1].belongStmt.isContainCaret).toBeTruthy();
|
||||
});
|
||||
|
||||
test('create table as select with no column', () => {
|
||||
const pos: CaretPosition = {
|
||||
lineNumber: 21,
|
||||
column: 52,
|
||||
};
|
||||
const sql = commentOtherLine(syntaxSql, pos.lineNumber);
|
||||
|
||||
const syntaxes = hive.getSuggestionAtCaretPosition(sql, pos)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
|
||||
);
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
|
||||
|
||||
const entities = hive.getAllEntities(sql, pos);
|
||||
expect(entities.length).toBe(2);
|
||||
expect(entities[0].text).toBe('derived_table');
|
||||
expect(entities[0].entityContextType).toBe(EntityContextType.TABLE_CREATE);
|
||||
expect(entities[0].belongStmt.isContainCaret).toBeTruthy();
|
||||
|
||||
expect(entities[1].text).toBe('origin_table');
|
||||
expect(entities[1].entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(entities[1].belongStmt.isContainCaret).toBeTruthy();
|
||||
});
|
||||
|
||||
test('create table as select with columns and trailing comma', () => {
|
||||
const pos: CaretPosition = {
|
||||
lineNumber: 23,
|
||||
column: 56,
|
||||
};
|
||||
const sql = commentOtherLine(syntaxSql, pos.lineNumber);
|
||||
|
||||
const syntaxes = hive.getSuggestionAtCaretPosition(sql, pos)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
|
||||
);
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
|
||||
|
||||
const entities = hive.getAllEntities(sql, pos);
|
||||
expect(entities.length).toBe(2);
|
||||
expect(entities[0].text).toBe('derived_table');
|
||||
expect(entities[0].entityContextType).toBe(EntityContextType.TABLE_CREATE);
|
||||
expect(entities[0].belongStmt.isContainCaret).toBeTruthy();
|
||||
|
||||
expect(entities[1].text).toBe('origin_table');
|
||||
expect(entities[1].entityContextType).toBe(EntityContextType.TABLE);
|
||||
expect(entities[1].belongStmt.isContainCaret).toBeTruthy();
|
||||
});
|
||||
});
|
@ -1,7 +1,7 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import HiveSQL from 'src/parser/hive';
|
||||
import { CaretPosition, SyntaxContextType } from 'src/parser/common/basic-parser-types';
|
||||
import { CaretPosition, EntityContextType } from 'src/parser/common/basic-parser-types';
|
||||
import { commentOtherLine } from 'test/helper';
|
||||
|
||||
const syntaxSql = fs.readFileSync(
|
||||
@ -28,7 +28,7 @@ describe('Hive SQL Syntax Suggestion', () => {
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
||||
(syn) => syn.syntaxContextType === EntityContextType.TABLE
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -45,7 +45,7 @@ describe('Hive SQL Syntax Suggestion', () => {
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
||||
(syn) => syn.syntaxContextType === EntityContextType.TABLE
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -62,7 +62,7 @@ describe('Hive SQL Syntax Suggestion', () => {
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE_CREATE
|
||||
(syn) => syn.syntaxContextType === EntityContextType.TABLE_CREATE
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -79,7 +79,7 @@ describe('Hive SQL Syntax Suggestion', () => {
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
||||
(syn) => syn.syntaxContextType === EntityContextType.TABLE
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -96,7 +96,7 @@ describe('Hive SQL Syntax Suggestion', () => {
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.VIEW_CREATE
|
||||
(syn) => syn.syntaxContextType === EntityContextType.VIEW_CREATE
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -113,7 +113,7 @@ describe('Hive SQL Syntax Suggestion', () => {
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.VIEW
|
||||
(syn) => syn.syntaxContextType === EntityContextType.VIEW
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -130,7 +130,7 @@ describe('Hive SQL Syntax Suggestion', () => {
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.FUNCTION_CREATE
|
||||
(syn) => syn.syntaxContextType === EntityContextType.FUNCTION_CREATE
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -147,7 +147,7 @@ describe('Hive SQL Syntax Suggestion', () => {
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.FUNCTION
|
||||
(syn) => syn.syntaxContextType === EntityContextType.FUNCTION
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -164,7 +164,7 @@ describe('Hive SQL Syntax Suggestion', () => {
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.DATABASE_CREATE
|
||||
(syn) => syn.syntaxContextType === EntityContextType.DATABASE_CREATE
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -181,7 +181,7 @@ describe('Hive SQL Syntax Suggestion', () => {
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.DATABASE
|
||||
(syn) => syn.syntaxContextType === EntityContextType.DATABASE
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -198,7 +198,7 @@ describe('Hive SQL Syntax Suggestion', () => {
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -215,7 +215,7 @@ describe('Hive SQL Syntax Suggestion', () => {
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -232,7 +232,7 @@ describe('Hive SQL Syntax Suggestion', () => {
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -249,7 +249,7 @@ describe('Hive SQL Syntax Suggestion', () => {
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN_CREATE
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN_CREATE
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -266,7 +266,7 @@ describe('Hive SQL Syntax Suggestion', () => {
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -287,7 +287,7 @@ describe('Hive SQL Syntax Suggestion', () => {
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
@ -304,10 +304,61 @@ describe('Hive SQL Syntax Suggestion', () => {
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN_CREATE
|
||||
(syn) => syn.syntaxContextType === EntityContextType.COLUMN_CREATE
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['new']);
|
||||
});
|
||||
|
||||
test('From Table Select', () => {
|
||||
const pos: CaretPosition = {
|
||||
lineNumber: 37,
|
||||
column: 18,
|
||||
};
|
||||
const syntaxes = parser.getSuggestionAtCaretPosition(
|
||||
commentOtherLine(syntaxSql, pos.lineNumber),
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === EntityContextType.TABLE
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['table_name_1']);
|
||||
});
|
||||
|
||||
test('From Table Select join', () => {
|
||||
const pos: CaretPosition = {
|
||||
lineNumber: 39,
|
||||
column: 14,
|
||||
};
|
||||
const syntaxes = parser.getSuggestionAtCaretPosition(
|
||||
commentOtherLine(syntaxSql, pos.lineNumber),
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === EntityContextType.TABLE
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['b']);
|
||||
});
|
||||
|
||||
test('From Table Insert', () => {
|
||||
const pos: CaretPosition = {
|
||||
lineNumber: 41,
|
||||
column: 19,
|
||||
};
|
||||
const syntaxes = parser.getSuggestionAtCaretPosition(
|
||||
commentOtherLine(syntaxSql, pos.lineNumber),
|
||||
pos
|
||||
)?.syntax;
|
||||
const suggestion = syntaxes?.find(
|
||||
(syn) => syn.syntaxContextType === EntityContextType.TABLE
|
||||
);
|
||||
|
||||
expect(suggestion).not.toBeUndefined();
|
||||
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['page_view_stg']);
|
||||
});
|
||||
});
|
||||
|
@ -239,3 +239,8 @@ EXPLAIN LOCKS UPDATE target SET b = 1 WHERE p IN (SELECT t.q1 FROM source t WHER
|
||||
|
||||
-- LanguageManual Explain -- User-level Explain Output
|
||||
EXPLAIN select sum(hash(key)), sum(hash(value)) from src_orc_merge_test_part where ds='2012-01-03' and ts='2012-01-03+14:46:31';
|
||||
|
||||
-- FROM xx SELECT
|
||||
FROM table_name_1 SELECT col1, col2;
|
||||
|
||||
FROM a JOIN b ON (a.id = b.id AND a.department = b.department) SELECT a.*;
|
Reference in New Issue
Block a user