2023-10-10 16:37:49 +08:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2024-01-19 21:10:00 +08:00
|
|
|
import HiveSQL from 'src/parser/hive';
|
|
|
|
import { CaretPosition, SyntaxContextType } from 'src/parser/common/basic-parser-types';
|
|
|
|
import { commentOtherLine } from 'test/helper';
|
2023-10-10 16:37:49 +08:00
|
|
|
|
2023-10-13 11:16:36 +08:00
|
|
|
const syntaxSql = fs.readFileSync(
|
|
|
|
path.join(__dirname, 'fixtures', 'syntaxSuggestion.sql'),
|
|
|
|
'utf-8'
|
|
|
|
);
|
2023-10-10 16:37:49 +08:00
|
|
|
|
|
|
|
describe('Hive SQL Syntax Suggestion', () => {
|
|
|
|
const parser = new HiveSQL();
|
|
|
|
|
|
|
|
test('Validate Syntax SQL', () => {
|
|
|
|
expect(parser.validate(syntaxSql).length).not.toBe(0);
|
|
|
|
expect(parser.validate(syntaxSql).length).not.toBe(0);
|
|
|
|
expect(parser.validate(syntaxSql).length).not.toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Insert table ', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 1,
|
2023-10-13 11:16:36 +08:00
|
|
|
column: 18,
|
|
|
|
};
|
2023-11-28 21:18:44 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
|
|
|
);
|
2023-10-10 16:37:49 +08:00
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
2023-10-13 11:16:36 +08:00
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.', 'tb']);
|
2023-10-10 16:37:49 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Select table ', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 3,
|
2023-10-13 11:16:36 +08:00
|
|
|
column: 18,
|
|
|
|
};
|
2023-11-28 21:18:44 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
|
|
|
);
|
2023-10-10 16:37:49 +08:00
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
2023-10-13 11:16:36 +08:00
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']);
|
2023-10-10 16:37:49 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Create table ', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 5,
|
2023-10-13 11:16:36 +08:00
|
|
|
column: 17,
|
|
|
|
};
|
2023-11-28 21:18:44 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE_CREATE
|
|
|
|
);
|
2023-10-10 16:37:49 +08:00
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
2023-10-13 11:16:36 +08:00
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']);
|
2023-10-10 16:37:49 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('DROP table ', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 7,
|
2023-10-13 11:16:36 +08:00
|
|
|
column: 26,
|
|
|
|
};
|
2023-11-28 21:18:44 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
|
|
|
);
|
2023-10-10 16:37:49 +08:00
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
2023-10-13 11:16:36 +08:00
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.', 'a']);
|
2023-10-10 16:37:49 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Create view ', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 9,
|
2023-10-13 11:16:36 +08:00
|
|
|
column: 28,
|
|
|
|
};
|
2023-11-28 21:18:44 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.VIEW_CREATE
|
|
|
|
);
|
2023-10-10 16:37:49 +08:00
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
2023-10-13 11:16:36 +08:00
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.', 'v']);
|
2023-10-10 16:37:49 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Drop view ', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 11,
|
2023-10-13 11:16:36 +08:00
|
|
|
column: 15,
|
|
|
|
};
|
2023-11-28 21:18:44 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.VIEW
|
|
|
|
);
|
2023-10-10 16:37:49 +08:00
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
2023-10-13 11:16:36 +08:00
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.', 'v']);
|
2023-10-10 16:37:49 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Create function ', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 13,
|
2023-10-13 11:16:36 +08:00
|
|
|
column: 20,
|
|
|
|
};
|
2023-11-28 21:18:44 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.FUNCTION_CREATE
|
|
|
|
);
|
2023-10-10 16:37:49 +08:00
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
2023-10-13 11:16:36 +08:00
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['fn1']);
|
2023-10-10 16:37:49 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Use function', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 15,
|
2023-10-13 11:16:36 +08:00
|
|
|
column: 27,
|
|
|
|
};
|
2023-11-28 21:18:44 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.FUNCTION
|
|
|
|
);
|
2023-10-10 16:37:49 +08:00
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
2023-10-13 11:16:36 +08:00
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['calculate_age']);
|
2023-10-10 16:37:49 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Create database', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 17,
|
2023-10-13 11:16:36 +08:00
|
|
|
column: 19,
|
|
|
|
};
|
2023-11-28 21:18:44 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.DATABASE_CREATE
|
|
|
|
);
|
2023-10-10 16:37:49 +08:00
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
2023-10-13 11:16:36 +08:00
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db']);
|
2023-10-10 16:37:49 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Drop database', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 19,
|
2023-10-13 11:16:36 +08:00
|
|
|
column: 26,
|
|
|
|
};
|
2023-11-28 21:18:44 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.DATABASE
|
|
|
|
);
|
2023-10-10 16:37:49 +08:00
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
2023-10-13 11:16:36 +08:00
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['sch']);
|
2023-10-10 16:37:49 +08:00
|
|
|
});
|
2023-11-28 21:18:44 +08:00
|
|
|
|
|
|
|
test('Select column with no fill-in', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 23,
|
|
|
|
column: 8,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Insert into spec col', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 25,
|
|
|
|
column: 18,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Select order by', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 27,
|
|
|
|
column: 32,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['tb', '.', 'c']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Create Table new column', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 29,
|
|
|
|
column: 31,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN_CREATE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['n']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Merge into when matched', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 31,
|
|
|
|
column: 115,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([
|
|
|
|
'tablename',
|
|
|
|
'.',
|
|
|
|
'col',
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Alter Table rename col', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 33,
|
|
|
|
column: 31,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Alter Table rename col to', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 35,
|
|
|
|
column: 45,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN_CREATE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['new']);
|
|
|
|
});
|
2023-10-13 11:16:36 +08:00
|
|
|
});
|