2023-10-12 17:08:21 +08:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
import { CaretPosition, SyntaxContextType } from '../../../../src/parser/common/basic-parser-types';
|
|
|
|
import SparkSQL from '../../../../src/parser/spark';
|
|
|
|
|
2023-10-13 11:16:36 +08:00
|
|
|
const syntaxSql = fs.readFileSync(
|
|
|
|
path.join(__dirname, 'fixtures', 'syntaxSuggestion.sql'),
|
|
|
|
'utf-8'
|
|
|
|
);
|
2023-10-12 17:08:21 +08:00
|
|
|
|
|
|
|
describe('Spark SQL Syntax Suggestion', () => {
|
|
|
|
const parser = new SparkSQL();
|
|
|
|
|
|
|
|
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,
|
|
|
|
column: 18,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
|
|
|
);
|
2023-10-12 17:08:21 +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-12 17:08:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Select table ', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 3,
|
|
|
|
column: 18,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
|
|
|
);
|
2023-10-12 17:08:21 +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-12 17:08:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Create table ', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 5,
|
|
|
|
column: 17,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE_CREATE
|
|
|
|
);
|
2023-10-12 17:08:21 +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-12 17:08:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('DROP table ', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 7,
|
|
|
|
column: 26,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
|
|
|
);
|
2023-10-12 17:08:21 +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-12 17:08:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Create view ', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 9,
|
|
|
|
column: 28,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.VIEW_CREATE
|
|
|
|
);
|
2023-10-12 17:08:21 +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-12 17:08:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Drop view ', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 11,
|
|
|
|
column: 15,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.VIEW
|
|
|
|
);
|
2023-10-12 17:08:21 +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-12 17:08:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Create function ', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 13,
|
|
|
|
column: 20,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.FUNCTION_CREATE
|
|
|
|
);
|
2023-10-12 17:08:21 +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-12 17:08:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Use function', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 15,
|
|
|
|
column: 27,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.FUNCTION
|
|
|
|
);
|
2023-10-12 17:08:21 +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-12 17:08:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Create database', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 17,
|
|
|
|
column: 19,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.DATABASE_CREATE
|
|
|
|
);
|
2023-10-12 17:08:21 +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-12 17:08:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Drop database', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 19,
|
|
|
|
column: 26,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
|
2023-10-13 11:16:36 +08:00
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.DATABASE
|
|
|
|
);
|
2023-10-12 17:08:21 +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-12 17:08:21 +08:00
|
|
|
});
|
|
|
|
});
|