2023-11-28 21:11:07 +08:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2023-12-19 19:22:27 +08:00
|
|
|
import { ImpalaSQL, CaretPosition, SyntaxContextType } from '../../../filters';
|
2023-12-13 11:33:47 +08:00
|
|
|
import { commentOtherLine } from '../../../helper';
|
2023-11-28 21:11:07 +08:00
|
|
|
|
|
|
|
const syntaxSql = fs.readFileSync(
|
|
|
|
path.join(__dirname, 'fixtures', 'syntaxSuggestion.sql'),
|
|
|
|
'utf-8'
|
|
|
|
);
|
|
|
|
|
|
|
|
describe('Impala SQL Syntax Suggestion', () => {
|
|
|
|
const parser = new ImpalaSQL();
|
|
|
|
|
|
|
|
test('Select table', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 1,
|
|
|
|
column: 20,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['cat', '.', 'a']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Function call', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 3,
|
|
|
|
column: 27,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.FUNCTION
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['calculate_age']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Alter database', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 5,
|
|
|
|
column: 19,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.DATABASE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['cat']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Alter table', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 7,
|
|
|
|
column: 21,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['my_table']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Alter table column', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 7,
|
|
|
|
column: 39,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['age']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Alter view', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 9,
|
|
|
|
column: 19,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.VIEW
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['my_view']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Drop view', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 11,
|
|
|
|
column: 12,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.VIEW
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['v']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Drop database', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 13,
|
|
|
|
column: 20,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.DATABASE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['my_db']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Drop table', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 15,
|
|
|
|
column: 20,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['my_table']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Drop function', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 17,
|
|
|
|
column: 22,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.FUNCTION
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['my_func']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('DELETE table', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 19,
|
|
|
|
column: 21,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['my_table']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Create view', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 21,
|
|
|
|
column: 15,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.VIEW_CREATE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['cv']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Create table', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 23,
|
|
|
|
column: 20,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE_CREATE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['cat', '.', 'db']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Create Function', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 25,
|
|
|
|
column: 20,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.FUNCTION_CREATE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['fnc']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Create database', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 27,
|
|
|
|
column: 25,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.DATABASE_CREATE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['FIRST_DB']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Show tables in', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 29,
|
|
|
|
column: 19,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['cat']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Show column stats table', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 31,
|
|
|
|
column: 22,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['vie']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Show create table', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 33,
|
|
|
|
column: 22,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['tb1']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Show create view', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 35,
|
|
|
|
column: 20,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.VIEW
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['v1']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Select group by', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 37,
|
|
|
|
column: 22,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['id']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Select order by', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 39,
|
|
|
|
column: 22,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['id']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Create Table column', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 41,
|
|
|
|
column: 36,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN_CREATE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['last_name']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Alert Table column', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 43,
|
|
|
|
column: 36,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN_CREATE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['age']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Create Table kudu', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 45,
|
|
|
|
column: 45,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-28 21:11:07 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN_CREATE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['id']);
|
|
|
|
});
|
2023-11-29 10:02:16 +08:00
|
|
|
|
|
|
|
test('Create Table partitions', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 47,
|
|
|
|
column: 49,
|
|
|
|
};
|
2023-12-13 11:33:47 +08:00
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(
|
|
|
|
commentOtherLine(syntaxSql, pos.lineNumber),
|
|
|
|
pos
|
|
|
|
)?.syntax;
|
2023-11-29 10:02:16 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.COLUMN_CREATE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['YEAR']);
|
|
|
|
});
|
2023-11-28 21:11:07 +08:00
|
|
|
});
|