2023-06-09 11:22:53 +08:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
import { CaretPosition, SyntaxContextType } from '../../../../src/parser/common/basic-parser-types';
|
2023-10-13 11:16:36 +08:00
|
|
|
import FlinkSQL from '../../../../src/parser/flinksql';
|
2023-11-28 21:18:44 +08:00
|
|
|
import { commentOtherLine } from '../../../helper';
|
2023-06-09 11:22:53 +08:00
|
|
|
|
2023-10-13 11:16:36 +08:00
|
|
|
const syntaxSql = fs.readFileSync(
|
|
|
|
path.join(__dirname, 'fixtures', 'syntaxSuggestion.sql'),
|
|
|
|
'utf-8'
|
|
|
|
);
|
2023-06-09 11:22:53 +08:00
|
|
|
|
|
|
|
describe('Flink SQL Syntax Suggestion', () => {
|
|
|
|
const parser = new FlinkSQL();
|
|
|
|
|
|
|
|
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);
|
2023-10-13 11:16:36 +08:00
|
|
|
});
|
2023-06-09 11:22:53 +08:00
|
|
|
|
2023-10-11 17:15:06 +08:00
|
|
|
test('Drop catalog', () => {
|
2023-06-09 11:22:53 +08:00
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 1,
|
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.CATALOG
|
|
|
|
);
|
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(['cat']);
|
2023-10-11 17:15:06 +08:00
|
|
|
});
|
2023-06-09 11:22:53 +08:00
|
|
|
|
|
|
|
test('Select table', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 3,
|
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.TABLE
|
|
|
|
);
|
2023-10-24 14:37:27 +08:00
|
|
|
|
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(['cat', '.']);
|
|
|
|
});
|
2023-06-09 11:22:53 +08:00
|
|
|
|
|
|
|
test('Create table', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 5,
|
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.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(['cat', '.', 'db']);
|
|
|
|
});
|
2023-06-09 11:22:53 +08:00
|
|
|
|
|
|
|
test('Show tables from', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 7,
|
2023-10-13 11:16:36 +08:00
|
|
|
column: 21,
|
|
|
|
};
|
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(['cat']);
|
|
|
|
});
|
2023-06-09 11:22:53 +08:00
|
|
|
|
|
|
|
test('Alter database', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 9,
|
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.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(['cat', '.']);
|
|
|
|
});
|
2023-06-09 11:22:53 +08:00
|
|
|
|
2023-10-11 17:15:06 +08:00
|
|
|
test('Drop view', () => {
|
2023-06-09 11:22:53 +08:00
|
|
|
const pos: CaretPosition = {
|
2023-10-11 17:15:06 +08:00
|
|
|
lineNumber: 11,
|
2023-10-13 11:16:36 +08:00
|
|
|
column: 12,
|
|
|
|
};
|
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(['v']);
|
2023-10-11 17:15:06 +08:00
|
|
|
});
|
2023-06-12 15:21:27 +08:00
|
|
|
|
2023-10-11 17:15:06 +08:00
|
|
|
test('Select view', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 13,
|
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-11 17:15:06 +08:00
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
2023-10-13 11:16:36 +08:00
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
|
2023-10-11 17:15:06 +08:00
|
|
|
});
|
2023-10-13 11:16:36 +08:00
|
|
|
|
2023-10-11 17:15:06 +08:00
|
|
|
test('Create view', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 15,
|
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_CREATE
|
|
|
|
);
|
2023-10-11 17:15:06 +08:00
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
2023-10-13 11:16:36 +08:00
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['cv']);
|
2023-10-11 17:15:06 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Function call', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 17,
|
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-11 17:15:06 +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-11 17:15:06 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Create Function', () => {
|
2023-06-12 15:21:27 +08:00
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 19,
|
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(['fnc']);
|
2023-10-11 17:15:06 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Show columns from view', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 21,
|
2023-10-13 11:16:36 +08:00
|
|
|
column: 22,
|
|
|
|
};
|
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-06-09 11:22:53 +08:00
|
|
|
|
2023-10-11 17:15:06 +08:00
|
|
|
expect(suggestion).not.toBeUndefined();
|
2023-10-13 11:16:36 +08:00
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['vie']);
|
2023-10-11 17:15:06 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Show create table', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 23,
|
2023-10-13 11:16:36 +08:00
|
|
|
column: 22,
|
|
|
|
};
|
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-11 17:15:06 +08:00
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
2023-10-13 11:16:36 +08:00
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['tb1']);
|
2023-10-11 17:15:06 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Show create view', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 25,
|
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.VIEW
|
|
|
|
);
|
2023-10-11 17:15:06 +08:00
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
2023-10-13 11:16:36 +08:00
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['v1']);
|
2023-10-11 17:15:06 +08:00
|
|
|
});
|
2023-11-28 21:18:44 +08:00
|
|
|
|
|
|
|
test('Select columns', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 27,
|
|
|
|
column: 11,
|
|
|
|
};
|
|
|
|
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(['col']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Select columns from table', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 29,
|
|
|
|
column: 11,
|
|
|
|
};
|
|
|
|
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(['col']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Select multiple columns from table', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 31,
|
|
|
|
column: 13,
|
|
|
|
};
|
|
|
|
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 column', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 33,
|
|
|
|
column: 27,
|
|
|
|
};
|
|
|
|
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 group by column', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 35,
|
|
|
|
column: 30,
|
|
|
|
};
|
|
|
|
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', '.']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Insert into spec columns', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 37,
|
|
|
|
column: 26,
|
|
|
|
};
|
|
|
|
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('Watermark for', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 39,
|
|
|
|
column: 56,
|
|
|
|
};
|
|
|
|
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('Create table new columns', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 41,
|
|
|
|
column: 25,
|
|
|
|
};
|
|
|
|
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([]);
|
|
|
|
});
|
2023-10-13 11:16:36 +08:00
|
|
|
});
|