feat: FlinkSQL supports auto complete (#115)

* feat: add antlr4-c3 dependencies

* feat: distinguish table, catalog and database from uid

* feat: move semicolon from sqlStatements to sqlStatement

* chore: move antlr4ts-cli to devDependencies

* feat: improve basic parser and support suggestions of token and syntax

* feat: implement suggest method in sql parsers

* test: flink sql suggestion test cases

* feat: optimize ts defination of suggestion

* feat: add split listener and optimize performance of auto-completion

* test: supplementary flink suggestion unit tests
This commit is contained in:
Hayden
2023-06-09 11:22:53 +08:00
committed by GitHub
parent 2637f90295
commit 1b02ff5d75
25 changed files with 4521 additions and 3418 deletions

View File

@ -0,0 +1,11 @@
INSERT INTO cat.db.tb
SELECT * FROM cat.db
CREATE TABLE cat.db ;
SHOW TABLES FROM cat
ALTER DATABASE cat.
USE DATABASE cat.

View File

@ -0,0 +1,7 @@
SELECT * FROM aa.bb;
USE
;
CREATE
;
SHOW

View File

@ -0,0 +1,90 @@
import fs from 'fs';
import path from 'path';
import { CaretPosition, SyntaxContextType } from '../../../../src/parser/common/basic-parser-types';
import FlinkSQL from '../../../../src/parser/flinksql'
const syntaxSql = fs.readFileSync(path.join(__dirname, 'fixtures', 'syntaxSuggestion.sql'), 'utf-8');
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);
})
test('Insert table ', () => {
const pos: CaretPosition = {
lineNumber: 1,
column: 22
}
const suggestion = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax?.[0];
expect(suggestion?.syntaxContextType === SyntaxContextType.TABLE)
expect(suggestion?.wordRanges.map(token => token.text))
.toEqual([ 'cat', '.', 'db', '.', 'tb' ])
})
test('Select table', () => {
const pos: CaretPosition = {
lineNumber: 3,
column: 21
}
const suggestion = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax?.[0];
expect(suggestion?.syntaxContextType === SyntaxContextType.TABLE)
expect(suggestion?.wordRanges.map(token => token.text))
.toEqual([ 'cat', '.', 'db' ])
})
test('Create table', () => {
const pos: CaretPosition = {
lineNumber: 5,
column: 20
}
const suggestion = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax?.[0];
expect(suggestion?.syntaxContextType === SyntaxContextType.TABLE_CREATE)
expect(suggestion?.wordRanges.map(token => token.text))
.toEqual([ 'cat', '.', 'db' ])
})
test('Show tables from', () => {
const pos: CaretPosition = {
lineNumber: 7,
column: 21
}
const suggestion = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax?.[0];
expect(suggestion?.syntaxContextType === SyntaxContextType.TABLE)
expect(suggestion?.wordRanges.map(token => token.text))
.toEqual([ 'cat' ])
})
test('Alter database', () => {
const pos: CaretPosition = {
lineNumber: 9,
column: 20
}
const suggestion = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax?.[0];
expect(suggestion?.syntaxContextType === SyntaxContextType.DATABASE)
expect(suggestion?.wordRanges.map(token => token.text))
.toEqual([ 'cat', '.' ])
})
test('Use database', () => {
const pos: CaretPosition = {
lineNumber: 9,
column: 20
}
const suggestion = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax?.[0];
expect(suggestion?.syntaxContextType === SyntaxContextType.DATABASE)
expect(suggestion?.wordRanges.map(token => token.text))
.toEqual([ 'cat', '.' ])
})
})

View File

@ -0,0 +1,57 @@
import fs from 'fs';
import path from 'path';
import { CaretPosition } from '../../../../src/parser/common/basic-parser-types';
import FlinkSQL from '../../../../src/parser/flinksql'
const tokenSql = fs.readFileSync(path.join(__dirname, 'fixtures', 'tokenSuggestion.sql'), 'utf-8');
describe('Flink SQL Syntax Suggestion', () => {
const parser = new FlinkSQL();
test('Use Statement ', () => {
const pos: CaretPosition = {
lineNumber: 3,
column: 5
}
const suggestion = parser.getSuggestionAtCaretPosition(tokenSql, pos)?.keywords;
expect(suggestion)
.toEqual([ 'MODULES', 'CATALOG' ])
})
test('Create Statement ', () => {
const pos: CaretPosition = {
lineNumber: 5,
column: 8
}
const suggestion = parser.getSuggestionAtCaretPosition(tokenSql, pos)?.keywords;
expect(suggestion)
.toEqual([ 'CATALOG', 'FUNCTION', 'TEMPORARY', 'VIEW', 'DATABASE', 'TABLE' ])
})
test('Show Statement ', () => {
const pos: CaretPosition = {
lineNumber: 7,
column: 6
}
const suggestion = parser.getSuggestionAtCaretPosition(tokenSql, pos)?.keywords;
expect(suggestion)
.toEqual([
'MODULES',
'FULL',
'FUNCTIONS',
'USER',
'CREATE',
'COLUMNS',
'TABLES',
'CURRENT',
'CATALOGS',
'DATABASES',
'JARS',
'VIEWS'
])
})
})