diff --git a/src/parser/flink/index.ts b/src/parser/flink/index.ts index 53b2bdcc..ebed25e6 100644 --- a/src/parser/flink/index.ts +++ b/src/parser/flink/index.ts @@ -29,6 +29,17 @@ export class FlinkSQL extends BasicSQL = new Set([ HiveSqlParser.RULE_dbSchemaName, // db or schema name HiveSqlParser.RULE_dbSchemaNameCreate, // db or schema name that will be created @@ -43,6 +48,7 @@ export class HiveSQL extends BasicSQL = new Set([ ImpalaSqlParser.RULE_functionNameCreate, ImpalaSqlParser.RULE_tableNameCreate, @@ -41,6 +46,7 @@ export class ImpalaSQL extends BasicSQL { 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 = new Set([ MySqlParser.RULE_databaseName, MySqlParser.RULE_databaseNameCreate, @@ -40,6 +45,7 @@ export class MySQL extends BasicSQL { MySqlParser.RULE_functionNameCreate, MySqlParser.RULE_columnName, MySqlParser.RULE_columnNameCreate, + ...this.excludeKeywordRules, ]); protected get splitListener() { diff --git a/src/parser/postgresql/index.ts b/src/parser/postgresql/index.ts index a62276cf..b8c85522 100644 --- a/src/parser/postgresql/index.ts +++ b/src/parser/postgresql/index.ts @@ -30,6 +30,17 @@ export class PostgreSQL extends BasicSQL = new Set([ PostgreSqlParser.RULE_tableNameCreate, // table name PostgreSqlParser.RULE_tableName, // table name that will be created @@ -46,6 +57,7 @@ export class PostgreSQL extends BasicSQL = new Set([ SparkSqlParser.RULE_namespaceName, SparkSqlParser.RULE_namespaceNameCreate, @@ -41,6 +50,7 @@ export class SparkSQL extends BasicSQL = new Set([ TrinoSqlParser.RULE_catalogRef, TrinoSqlParser.RULE_catalogNameCreate, @@ -64,6 +69,7 @@ export class TrinoSQL extends BasicSQL { )?.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']); + }); }); diff --git a/test/parser/hive/suggestion/fixtures/tokenSuggestion.sql b/test/parser/hive/suggestion/fixtures/tokenSuggestion.sql index b7d1854b..84834322 100644 --- a/test/parser/hive/suggestion/fixtures/tokenSuggestion.sql +++ b/test/parser/hive/suggestion/fixtures/tokenSuggestion.sql @@ -24,3 +24,5 @@ CREATE TABLE IF NOT EXISTS CREATE TABLE tb (id ); + +SELECT id FROM ; \ No newline at end of file diff --git a/test/parser/hive/suggestion/tokenSuggestion.test.ts b/test/parser/hive/suggestion/tokenSuggestion.test.ts index 6eb2b2b4..a8d65f7a 100644 --- a/test/parser/hive/suggestion/tokenSuggestion.test.ts +++ b/test/parser/hive/suggestion/tokenSuggestion.test.ts @@ -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']); + }); }); diff --git a/test/parser/impala/suggestion/fixtures/tokenSuggestion.sql b/test/parser/impala/suggestion/fixtures/tokenSuggestion.sql index f6b45e13..3f6d446e 100644 --- a/test/parser/impala/suggestion/fixtures/tokenSuggestion.sql +++ b/test/parser/impala/suggestion/fixtures/tokenSuggestion.sql @@ -15,3 +15,5 @@ CREATE TABLE IF NOT EXISTS; CREATE TABLE tb (id ); + +SELECT id FROM ; \ No newline at end of file diff --git a/test/parser/impala/suggestion/tokenSuggestion.test.ts b/test/parser/impala/suggestion/tokenSuggestion.test.ts index 68b24d85..ac142d4b 100644 --- a/test/parser/impala/suggestion/tokenSuggestion.test.ts +++ b/test/parser/impala/suggestion/tokenSuggestion.test.ts @@ -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']); + }); }); diff --git a/test/parser/mysql/suggestion/fixtures/tokenSuggestion.sql b/test/parser/mysql/suggestion/fixtures/tokenSuggestion.sql index 817d559c..18f75476 100644 --- a/test/parser/mysql/suggestion/fixtures/tokenSuggestion.sql +++ b/test/parser/mysql/suggestion/fixtures/tokenSuggestion.sql @@ -20,3 +20,5 @@ CREATE TABLE IF NOT EXISTS CREATE TABLE tb (id ); + +SELECT id FROM ; diff --git a/test/parser/mysql/suggestion/tokenSuggestion.test.ts b/test/parser/mysql/suggestion/tokenSuggestion.test.ts index f252f7db..29319fca 100644 --- a/test/parser/mysql/suggestion/tokenSuggestion.test.ts +++ b/test/parser/mysql/suggestion/tokenSuggestion.test.ts @@ -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']); + }); }); diff --git a/test/parser/postgresql/suggestion/fixtures/tokenSuggestion.sql b/test/parser/postgresql/suggestion/fixtures/tokenSuggestion.sql index a0abe5f8..928d5adc 100644 --- a/test/parser/postgresql/suggestion/fixtures/tokenSuggestion.sql +++ b/test/parser/postgresql/suggestion/fixtures/tokenSuggestion.sql @@ -13,3 +13,5 @@ CREATE TABLE IF NOT EXISTS; CREATE TABLE tb (id ); + +SELECT id as FROM a1 \ No newline at end of file diff --git a/test/parser/postgresql/suggestion/tokenSuggestion.test.ts b/test/parser/postgresql/suggestion/tokenSuggestion.test.ts index 7f7b1b8e..742f6372 100644 --- a/test/parser/postgresql/suggestion/tokenSuggestion.test.ts +++ b/test/parser/postgresql/suggestion/tokenSuggestion.test.ts @@ -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([]); + }); }); diff --git a/test/parser/spark/suggestion/fixtures/tokenSuggestion.sql b/test/parser/spark/suggestion/fixtures/tokenSuggestion.sql index aa9b0e52..7dcb272a 100644 --- a/test/parser/spark/suggestion/fixtures/tokenSuggestion.sql +++ b/test/parser/spark/suggestion/fixtures/tokenSuggestion.sql @@ -22,3 +22,5 @@ CREATE TABLE IF NOT EXISTS CREATE TABLE tb (id ); + +SELECT id FROM ; \ No newline at end of file diff --git a/test/parser/spark/suggestion/tokenSuggestion.test.ts b/test/parser/spark/suggestion/tokenSuggestion.test.ts index 7eb2c874..56266b74 100644 --- a/test/parser/spark/suggestion/tokenSuggestion.test.ts +++ b/test/parser/spark/suggestion/tokenSuggestion.test.ts @@ -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']); + }); }); diff --git a/test/parser/trino/suggestion/fixtures/tokenSuggestion.sql b/test/parser/trino/suggestion/fixtures/tokenSuggestion.sql index d3398ec9..5e2f21f8 100644 --- a/test/parser/trino/suggestion/fixtures/tokenSuggestion.sql +++ b/test/parser/trino/suggestion/fixtures/tokenSuggestion.sql @@ -17,3 +17,5 @@ CREATE TABLE IF NOT EXISTS ; CREATE TABLE tb (id ); + +SELECT id FROM ; \ No newline at end of file diff --git a/test/parser/trino/suggestion/tokenSuggestion.test.ts b/test/parser/trino/suggestion/tokenSuggestion.test.ts index f1b72666..5ca64f11 100644 --- a/test/parser/trino/suggestion/tokenSuggestion.test.ts +++ b/test/parser/trino/suggestion/tokenSuggestion.test.ts @@ -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']); + }); });