Skip to content

Feat/keywords filter #433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/parser/flink/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ export class FlinkSQL extends BasicSQL<FlinkSqlLexer, ProgramContext, FlinkSqlPa
return new FlinkSqlParser(tokenStream);
}

/**
* The rules that keywords you don't want to be suggested.
*/
protected excludeKeywordRules = new Set([
FlinkSqlParser.RULE_nonReservedKeywords,
FlinkSqlParser.RULE_reservedKeywordsUsedAsFuncName,
FlinkSqlParser.RULE_reservedKeywordsFollowParamsUsedAsFuncName,
FlinkSqlParser.RULE_reservedKeywordsNoParamsUsedAsFuncName,
FlinkSqlParser.RULE_reservedKeywordsUsedAsFuncParam,
]);

protected preferredRules = new Set([
FlinkSqlParser.RULE_catalogPath, // catalog name
FlinkSqlParser.RULE_databasePath, // database name
Expand All @@ -47,6 +58,7 @@ export class FlinkSQL extends BasicSQL<FlinkSqlLexer, ProgramContext, FlinkSqlPa
FlinkSqlParser.RULE_columnNameCreate,
FlinkSqlParser.RULE_tablePropertyKey,
FlinkSqlParser.RULE_tablePropertyValue,
...this.excludeKeywordRules,
]);

protected get splitListener() {
Expand Down
6 changes: 6 additions & 0 deletions src/parser/hive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export class HiveSQL extends BasicSQL<HiveSqlLexer, ProgramContext, HiveSqlParse
return new HiveSqlParser(tokenStream);
}

/**
* The rules that keywords you don't want to be suggested.
*/
protected excludeKeywordRules = new Set([HiveSqlParser.RULE_nonReserved]);

protected preferredRules: Set<number> = new Set([
HiveSqlParser.RULE_dbSchemaName, // db or schema name
HiveSqlParser.RULE_dbSchemaNameCreate, // db or schema name that will be created
Expand All @@ -43,6 +48,7 @@ export class HiveSQL extends BasicSQL<HiveSqlLexer, ProgramContext, HiveSqlParse
HiveSqlParser.RULE_columnName,
HiveSqlParser.RULE_columnNamePath,
HiveSqlParser.RULE_columnNameCreate,
...this.excludeKeywordRules,
]);

protected get splitListener() {
Expand Down
6 changes: 6 additions & 0 deletions src/parser/impala/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export class ImpalaSQL extends BasicSQL<ImpalaSqlLexer, ProgramContext, ImpalaSq
return new ImpalaSqlParser(tokenStream);
}

/**
* The rules that keywords you don't want to be suggested.
*/
protected excludeKeywordRules = new Set([ImpalaSqlParser.RULE_nonReserved]);

protected preferredRules: Set<number> = new Set([
ImpalaSqlParser.RULE_functionNameCreate,
ImpalaSqlParser.RULE_tableNameCreate,
Expand All @@ -41,6 +46,7 @@ export class ImpalaSQL extends BasicSQL<ImpalaSqlLexer, ProgramContext, ImpalaSq
ImpalaSqlParser.RULE_databaseNamePath,
ImpalaSqlParser.RULE_columnNamePath,
ImpalaSqlParser.RULE_columnName,
...this.excludeKeywordRules,
]);

protected get splitListener() {
Expand Down
6 changes: 6 additions & 0 deletions src/parser/mysql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export class MySQL extends BasicSQL<MySqlLexer, ProgramContext, MySqlParser> {
return new MySqlParser(tokenStream);
}

/**
* The rules that keywords you don't want to be suggested.
*/
protected excludeKeywordRules = new Set([MySqlParser.RULE_keywordsCanBeId]);

protected preferredRules: Set<number> = new Set([
MySqlParser.RULE_databaseName,
MySqlParser.RULE_databaseNameCreate,
Expand All @@ -40,6 +45,7 @@ export class MySQL extends BasicSQL<MySqlLexer, ProgramContext, MySqlParser> {
MySqlParser.RULE_functionNameCreate,
MySqlParser.RULE_columnName,
MySqlParser.RULE_columnNameCreate,
...this.excludeKeywordRules,
]);

protected get splitListener() {
Expand Down
12 changes: 12 additions & 0 deletions src/parser/postgresql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ export class PostgreSQL extends BasicSQL<PostgreSqlLexer, ProgramContext, Postgr
return new PostgreSqlParser(tokenStream);
}

/**
* The rules that keywords you don't want to be suggested.
*/
protected excludeKeywordRules = new Set([
PostgreSqlParser.RULE_nonReservedWord,
PostgreSqlParser.RULE_identifier,
PostgreSqlParser.RULE_reservedKeyword,
PostgreSqlParser.RULE_typeFuncNameKeyword,
PostgreSqlParser.RULE_colNameKeyword,
]);

protected preferredRules: Set<number> = new Set([
PostgreSqlParser.RULE_tableNameCreate, // table name
PostgreSqlParser.RULE_tableName, // table name that will be created
Expand All @@ -46,6 +57,7 @@ export class PostgreSQL extends BasicSQL<PostgreSqlLexer, ProgramContext, Postgr
PostgreSqlParser.RULE_columnNameCreate, // column name that will be created
PostgreSqlParser.RULE_columnName, // column name
PostgreSqlParser.RULE_columnNamePath, // column name
...this.excludeKeywordRules,
]);

protected get splitListener() {
Expand Down
10 changes: 10 additions & 0 deletions src/parser/spark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ export class SparkSQL extends BasicSQL<SparkSqlLexer, ProgramContext, SparkSqlPa
return new SparkSqlParser(tokenStream);
}

/**
* The rules that keywords you don't want to be suggested.
*/
protected excludeKeywordRules = new Set([
SparkSqlParser.RULE_strictIdentifier,
SparkSqlParser.RULE_strictNonReserved,
SparkSqlParser.RULE_nonReserved,
]);

protected preferredRules: Set<number> = new Set([
SparkSqlParser.RULE_namespaceName,
SparkSqlParser.RULE_namespaceNameCreate,
Expand All @@ -41,6 +50,7 @@ export class SparkSQL extends BasicSQL<SparkSqlLexer, ProgramContext, SparkSqlPa
SparkSqlParser.RULE_columnName,
SparkSqlParser.RULE_columnNamePath,
SparkSqlParser.RULE_columnNameCreate,
...this.excludeKeywordRules,
]);

protected get splitListener() {
Expand Down
6 changes: 6 additions & 0 deletions src/parser/trino/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export class TrinoSQL extends BasicSQL<TrinoSqlLexer, ProgramContext, TrinoSqlPa
return new TrinoSemanticContextCollector(input, caretPosition, allTokens, options);
}

/**
* The rules that keywords you don't want to be suggested.
*/
protected excludeKeywordRules = new Set([TrinoSqlParser.RULE_nonReserved]);

protected preferredRules: Set<number> = new Set([
TrinoSqlParser.RULE_catalogRef,
TrinoSqlParser.RULE_catalogNameCreate,
Expand All @@ -64,6 +69,7 @@ export class TrinoSQL extends BasicSQL<TrinoSqlLexer, ProgramContext, TrinoSqlPa
TrinoSqlParser.RULE_columnRef,
TrinoSqlParser.RULE_columnName,
TrinoSqlParser.RULE_columnNameCreate,
...this.excludeKeywordRules,
]);

protected processCandidates(
Expand Down
2 changes: 2 additions & 0 deletions test/parser/flink/suggestion/fixtures/tokenSuggestion.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ CREATE TABLE IF NOT EXISTS
CREATE TABLE tb (id

);

SELECT id FROM ;
12 changes: 12 additions & 0 deletions test/parser/flink/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,16 @@ describe('Flink SQL Token Suggestion', () => {
)?.keywords;
expect(suggestion.length).not.toBe(0);
});

test('filter unreserved keywords', () => {
const pos: CaretPosition = {
lineNumber: 16,
column: 17,
};
const suggestion = flink.getSuggestionAtCaretPosition(
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toMatchUnorderedArray(['TABLE', 'LATERAL', 'UNNEST']);
});
});
2 changes: 2 additions & 0 deletions test/parser/hive/suggestion/fixtures/tokenSuggestion.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ CREATE TABLE IF NOT EXISTS
CREATE TABLE tb (id

);

SELECT id FROM ;
12 changes: 12 additions & 0 deletions test/parser/hive/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,16 @@ describe('Hive SQL Token Suggestion', () => {
)?.keywords;
expect(suggestion.length).not.toBe(0);
});

test('filter unreserved keywords', () => {
const pos: CaretPosition = {
lineNumber: 28,
column: 16,
};
const suggestion = hive.getSuggestionAtCaretPosition(
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toMatchUnorderedArray(['TABLE', 'UNIQUEJOIN']);
});
});
2 changes: 2 additions & 0 deletions test/parser/impala/suggestion/fixtures/tokenSuggestion.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ CREATE TABLE IF NOT EXISTS;
CREATE TABLE tb (id

);

SELECT id FROM ;
12 changes: 12 additions & 0 deletions test/parser/impala/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,16 @@ describe('Impala SQL Token Suggestion', () => {
)?.keywords;
expect(suggestion.length).not.toBe(0);
});

test('filter unreserved keywords', () => {
const pos: CaretPosition = {
lineNumber: 19,
column: 17,
};
const suggestion = impala.getSuggestionAtCaretPosition(
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toMatchUnorderedArray(['LATERAL', 'UNNEST']);
});
});
2 changes: 2 additions & 0 deletions test/parser/mysql/suggestion/fixtures/tokenSuggestion.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ CREATE TABLE IF NOT EXISTS
CREATE TABLE tb (id

);

SELECT id FROM ;
12 changes: 12 additions & 0 deletions test/parser/mysql/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,16 @@ describe('MySQL Token Suggestion', () => {
)?.keywords;
expect(suggestion.length).not.toBe(0);
});

test('filter unreserved keywords', () => {
const pos: CaretPosition = {
lineNumber: 24,
column: 17,
};
const suggestion = mysql.getSuggestionAtCaretPosition(
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toMatchUnorderedArray(['JSON_TABLE', 'LATERAL', 'SELECT']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ CREATE TABLE IF NOT EXISTS;
CREATE TABLE tb (id

);

SELECT id as FROM a1
12 changes: 12 additions & 0 deletions test/parser/postgresql/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,16 @@ describe('Postgres SQL Token Suggestion', () => {
)?.keywords;
expect(suggestion.length).not.toBe(0);
});

test('filter unreserved keywords', () => {
const pos: CaretPosition = {
lineNumber: 17,
column: 14,
};
const suggestion = postgresql.getSuggestionAtCaretPosition(
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toEqual([]);
});
});
2 changes: 2 additions & 0 deletions test/parser/spark/suggestion/fixtures/tokenSuggestion.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ CREATE TABLE IF NOT EXISTS
CREATE TABLE tb (id

);

SELECT id FROM ;
12 changes: 12 additions & 0 deletions test/parser/spark/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,16 @@ describe('Spark SQL Token Suggestion', () => {
)?.keywords;
expect(suggestion.length).not.toBe(0);
});

test('filter unreserved keywords', () => {
const pos: CaretPosition = {
lineNumber: 26,
column: 17,
};
const suggestion = spark.getSuggestionAtCaretPosition(
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toMatchUnorderedArray(['LATERAL', 'IDENTIFIER', 'VALUES']);
});
});
2 changes: 2 additions & 0 deletions test/parser/trino/suggestion/fixtures/tokenSuggestion.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ CREATE TABLE IF NOT EXISTS ;
CREATE TABLE tb (id

);

SELECT id FROM ;
12 changes: 12 additions & 0 deletions test/parser/trino/suggestion/tokenSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,16 @@ describe('Trino SQL Token Suggestion', () => {
)?.keywords;
expect(suggestion.length).not.toBe(0);
});

test('filter unreserved keywords', () => {
const pos: CaretPosition = {
lineNumber: 21,
column: 17,
};
const suggestion = trino.getSuggestionAtCaretPosition(
commentOtherLine(tokenSql, pos.lineNumber),
pos
)?.keywords;
expect(suggestion).toMatchUnorderedArray(['LATERAL', 'UNNEST', 'JSON_TABLE', 'TABLE']);
});
});