Skip to content
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

feat: #410 optimize processCandidates tokenIndexOffset #411

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
11 changes: 4 additions & 7 deletions src/parser/common/basicSQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,13 @@ export abstract class BasicSQL<
/**
* Convert candidates to suggestions
* @param candidates candidate list
* @param allTokens all tokens from input
* @param allTokens slice all tokens from input by tokenIndexOffset
* @param caretTokenIndex tokenIndex of caretPosition
* @param tokenIndexOffset offset of the tokenIndex in the candidates compared to the tokenIndex in allTokens
*/
protected abstract processCandidates(
candidates: CandidatesCollection,
allTokens: Token[],
caretTokenIndex: number,
tokenIndexOffset: number
caretTokenIndex: number
): Suggestions<Token>;

/**
Expand Down Expand Up @@ -420,9 +418,8 @@ export abstract class BasicSQL<
const candidates = core.collectCandidates(newTokenIndex, c3Context);
const originalSuggestions = this.processCandidates(
candidates,
allTokens,
newTokenIndex,
tokenIndexOffset
allTokens.slice(tokenIndexOffset),
newTokenIndex
);

const syntaxSuggestions: SyntaxSuggestion<WordRange>[] = originalSuggestions.syntax.map(
Expand Down
9 changes: 2 additions & 7 deletions src/parser/flink/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,14 @@ export class FlinkSQL extends BasicSQL<FlinkSqlLexer, ProgramContext, FlinkSqlPa
protected processCandidates(
candidates: CandidatesCollection,
allTokens: Token[],
caretTokenIndex: number,
tokenIndexOffset: number
caretTokenIndex: number
): Suggestions<Token> {
const originalSyntaxSuggestions: SyntaxSuggestion<Token>[] = [];
const keywords: string[] = [];

for (let candidate of candidates.rules) {
const [ruleType, candidateRule] = candidate;
const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
const tokenRanges = allTokens.slice(
startTokenIndex,
caretTokenIndex + tokenIndexOffset + 1
);
const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1);

let syntaxContextType: EntityContextType | StmtContextType | undefined = void 0;
switch (ruleType) {
Expand Down
9 changes: 2 additions & 7 deletions src/parser/hive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,13 @@ export class HiveSQL extends BasicSQL<HiveSqlLexer, ProgramContext, HiveSqlParse
protected processCandidates(
candidates: CandidatesCollection,
allTokens: Token[],
caretTokenIndex: number,
tokenIndexOffset: number
caretTokenIndex: number
): Suggestions<Token> {
const originalSyntaxSuggestions: SyntaxSuggestion<Token>[] = [];
const keywords: string[] = [];
for (let candidate of candidates.rules) {
const [ruleType, candidateRule] = candidate;
const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
const tokenRanges = allTokens.slice(
startTokenIndex,
caretTokenIndex + tokenIndexOffset + 1
);
const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1);

let syntaxContextType: EntityContextType | StmtContextType | undefined = void 0;
switch (ruleType) {
Expand Down
9 changes: 2 additions & 7 deletions src/parser/impala/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,13 @@ export class ImpalaSQL extends BasicSQL<ImpalaSqlLexer, ProgramContext, ImpalaSq
protected processCandidates(
candidates: CandidatesCollection,
allTokens: Token[],
caretTokenIndex: number,
tokenIndexOffset: number
caretTokenIndex: number
): Suggestions<Token> {
const originalSyntaxSuggestions: SyntaxSuggestion<Token>[] = [];
const keywords: string[] = [];
for (let candidate of candidates.rules) {
const [ruleType, candidateRule] = candidate;
const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
const tokenRanges = allTokens.slice(
startTokenIndex,
caretTokenIndex + tokenIndexOffset + 1
);
const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1);

let syntaxContextType: EntityContextType | StmtContextType | undefined = void 0;
switch (ruleType) {
Expand Down
9 changes: 2 additions & 7 deletions src/parser/mysql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,14 @@ export class MySQL extends BasicSQL<MySqlLexer, ProgramContext, MySqlParser> {
protected processCandidates(
candidates: CandidatesCollection,
allTokens: Token[],
caretTokenIndex: number,
tokenIndexOffset: number
caretTokenIndex: number
): Suggestions<Token> {
const originalSyntaxSuggestions: SyntaxSuggestion<Token>[] = [];
const keywords: string[] = [];

for (const candidate of candidates.rules) {
const [ruleType, candidateRule] = candidate;
const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
const tokenRanges = allTokens.slice(
startTokenIndex,
caretTokenIndex + tokenIndexOffset + 1
);
const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1);

let syntaxContextType: EntityContextType | StmtContextType | undefined = void 0;
switch (ruleType) {
Expand Down
9 changes: 2 additions & 7 deletions src/parser/postgresql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,13 @@ export class PostgreSQL extends BasicSQL<PostgreSqlLexer, ProgramContext, Postgr
protected processCandidates(
candidates: CandidatesCollection,
allTokens: Token[],
caretTokenIndex: number,
tokenIndexOffset: number
caretTokenIndex: number
): Suggestions<Token> {
const originalSyntaxSuggestions: SyntaxSuggestion<Token>[] = [];
const keywords: string[] = [];
for (let candidate of candidates.rules) {
const [ruleType, candidateRule] = candidate;
const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
const tokenRanges = allTokens.slice(
startTokenIndex,
caretTokenIndex + tokenIndexOffset + 1
);
const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1);

let syntaxContextType: EntityContextType | StmtContextType | undefined = void 0;
switch (ruleType) {
Expand Down
9 changes: 2 additions & 7 deletions src/parser/spark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,14 @@ export class SparkSQL extends BasicSQL<SparkSqlLexer, ProgramContext, SparkSqlPa
protected processCandidates(
candidates: CandidatesCollection,
allTokens: Token[],
caretTokenIndex: number,
tokenIndexOffset: number
caretTokenIndex: number
): Suggestions<Token> {
const originalSyntaxSuggestions: SyntaxSuggestion<Token>[] = [];
const keywords: string[] = [];

for (const candidate of candidates.rules) {
const [ruleType, candidateRule] = candidate;
const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
const tokenRanges = allTokens.slice(
startTokenIndex,
caretTokenIndex + tokenIndexOffset + 1
);
const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1);

let syntaxContextType: EntityContextType | StmtContextType | undefined = void 0;
switch (ruleType) {
Expand Down
9 changes: 2 additions & 7 deletions src/parser/trino/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,14 @@ export class TrinoSQL extends BasicSQL<TrinoSqlLexer, ProgramContext, TrinoSqlPa
protected processCandidates(
candidates: CandidatesCollection,
allTokens: Token[],
caretTokenIndex: number,
tokenIndexOffset: number
caretTokenIndex: number
): Suggestions<Token> {
const originalSyntaxSuggestions: SyntaxSuggestion<Token>[] = [];
const keywords: string[] = [];

for (let candidate of candidates.rules) {
const [ruleType, candidateRule] = candidate;
const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
const tokenRanges = allTokens.slice(
startTokenIndex,
caretTokenIndex + tokenIndexOffset + 1
);
const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1);

let syntaxContextType: EntityContextType | StmtContextType | undefined = void 0;
switch (ruleType) {
Expand Down
20 changes: 19 additions & 1 deletion test/parser/flink/suggestion/multipleStatement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,25 @@ describe('FlinkSQL Multiple Statements Syntax Suggestion', () => {
);

expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']);
expect(suggestion?.wordRanges?.length).toBe(2);
expect(suggestion?.wordRanges).toEqual([
{
text: 'db',
line: 16,
startIndex: 306,
endIndex: 307,
startColumn: 14,
endColumn: 16,
},
{
text: '.',
line: 16,
startIndex: 308,
endIndex: 308,
startColumn: 16,
endColumn: 17,
},
]);
});

test('Insert into table ', () => {
Expand Down
12 changes: 11 additions & 1 deletion test/parser/flink/suggestion/syntaxSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,17 @@ describe('Flink SQL Syntax Suggestion', () => {
);

expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['cat']);
expect(suggestion?.wordRanges?.length).toBe(1);
expect(suggestion?.wordRanges).toEqual([
{
text: 'cat',
line: 1,
startIndex: 13,
endIndex: 15,
startColumn: 14,
endColumn: 17,
},
]);
});

test('Select table', () => {
Expand Down
20 changes: 19 additions & 1 deletion test/parser/hive/suggestion/multipleStatement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,25 @@ describe('HiveSQL Multiple Statements Syntax Suggestion', () => {
);

expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']);
expect(suggestion?.wordRanges?.length).toBe(2);
expect(suggestion?.wordRanges).toEqual([
{
text: 'db',
line: 9,
startIndex: 272,
endIndex: 273,
startColumn: 14,
endColumn: 16,
},
{
text: '.',
line: 9,
startIndex: 274,
endIndex: 274,
startColumn: 16,
endColumn: 17,
},
]);
});

test('Insert into table ', () => {
Expand Down
28 changes: 27 additions & 1 deletion test/parser/hive/suggestion/syntaxSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,33 @@ describe('Hive SQL Syntax Suggestion', () => {
);

expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.', 'tb']);
expect(suggestion?.wordRanges?.length).toBe(3);
expect(suggestion?.wordRanges).toEqual([
{
text: 'db',
line: 1,
startIndex: 12,
endIndex: 13,
startColumn: 13,
endColumn: 15,
},
{
text: '.',
line: 1,
startIndex: 14,
endIndex: 14,
startColumn: 15,
endColumn: 16,
},
{
text: 'tb',
line: 1,
startIndex: 15,
endIndex: 16,
startColumn: 16,
endColumn: 18,
},
]);
});

test('Select table ', () => {
Expand Down
20 changes: 19 additions & 1 deletion test/parser/impala/suggestion/multipleStatement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,25 @@ describe('ImpalaSQL Multiple Statements Syntax Suggestion', () => {
);

expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']);
expect(suggestion?.wordRanges?.length).toBe(2);
expect(suggestion?.wordRanges).toEqual([
{
text: 'db',
line: 9,
startIndex: 203,
endIndex: 204,
startColumn: 14,
endColumn: 16,
},
{
text: '.',
line: 9,
startIndex: 205,
endIndex: 205,
startColumn: 16,
endColumn: 17,
},
]);
});

test('Insert into table ', () => {
Expand Down
28 changes: 27 additions & 1 deletion test/parser/impala/suggestion/syntaxSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,33 @@ describe('Impala SQL Syntax Suggestion', () => {
);

expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['cat', '.', 'a']);
expect(suggestion?.wordRanges?.length).toBe(3);
expect(suggestion?.wordRanges).toEqual([
{
text: 'cat',
line: 1,
startIndex: 14,
endIndex: 16,
startColumn: 15,
endColumn: 18,
},
{
text: '.',
line: 1,
startIndex: 17,
endIndex: 17,
startColumn: 18,
endColumn: 19,
},
{
text: 'a',
line: 1,
startIndex: 18,
endIndex: 18,
startColumn: 19,
endColumn: 20,
},
]);
});

test('Function call', () => {
Expand Down
20 changes: 19 additions & 1 deletion test/parser/mysql/suggestion/multipleStatement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,25 @@ describe('MySQL Multiple Statements Syntax Suggestion', () => {
);

expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']);
expect(suggestion?.wordRanges?.length).toBe(2);
expect(suggestion?.wordRanges).toEqual([
{
text: 'db',
line: 9,
startIndex: 306,
endIndex: 307,
startColumn: 14,
endColumn: 16,
},
{
text: '.',
line: 9,
startIndex: 308,
endIndex: 308,
startColumn: 16,
endColumn: 17,
},
]);
});

test('Insert into table ', () => {
Expand Down
Loading