Skip to content

Commit ba6fbbb

Browse files
committed
feat: mark as entityCollecting in getAllEntities context to allow empty column
1 parent 3684ae7 commit ba6fbbb

File tree

3 files changed

+32
-35
lines changed

3 files changed

+32
-35
lines changed

src/parser/common/basicSQL.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export abstract class BasicSQL<
134134
public createParser(input: string, errorListener?: ErrorListener) {
135135
const lexer = this.createLexer(input, errorListener);
136136
const tokenStream = new CommonTokenStream(lexer);
137+
tokenStream.fill();
137138
const parser = this.createParserFromTokenStream(tokenStream);
138139
parser.interpreter.predictionMode = PredictionMode.SLL;
139140
if (errorListener) {
@@ -558,29 +559,28 @@ export abstract class BasicSQL<
558559
}
559560

560561
public getAllEntities(input: string, caretPosition?: CaretPosition): EntityContext[] | null {
561-
const allTokens = this.getAllTokens(input);
562+
/**
563+
* Create a new parser to generate brand new parse tree.
564+
* And the new parse tree should not effect cached parse tree which is used by validate and getSuggestionAtCaretPosition method.
565+
*/
566+
const parser = this.createParser(input);
567+
const allTokens = (parser.tokenStream as CommonTokenStream).getTokens();
568+
562569
const caretTokenIndex = caretPosition
563570
? findCaretTokenIndex(caretPosition, allTokens)
564571
: void 0;
565-
566572
const collectListener = this.createEntityCollector(input, allTokens, caretTokenIndex);
567-
// const parser = this.createParserWithCache(input);
568573

569-
// parser.entityCollecting = true;
570-
// if(caretPosition) {
571-
// const allTokens = this.getAllTokens(input);
572-
// const tokenIndex = findCaretTokenIndex(caretPosition, allTokens);
573-
// parser.caretTokenIndex = tokenIndex;
574-
// }
575-
576-
// const parseTree = parser.program();
577-
578-
const parseTree = this.parseWithCache(input);
574+
parser.entityCollecting = true;
575+
if (caretPosition && caretTokenIndex !== undefined) {
576+
parser.caretTokenIndex = caretTokenIndex;
577+
}
579578

579+
const parseTree = parser.program();
580580
this.listen(collectListener, parseTree);
581581

582-
// parser.caretTokenIndex = -1;
583-
// parser.entityCollecting = false;
582+
parser.caretTokenIndex = -1;
583+
parser.entityCollecting = false;
584584

585585
return collectListener.getEntities();
586586
}

test/parser/impala/suggestion/suggestionWithEntity.test.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,14 @@ describe('Impala SQL Syntax Suggestion with collect entity', () => {
7070
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
7171

7272
const entities = impala.getAllEntities(sql, pos);
73-
expect(entities.length).toBe(1);
73+
expect(entities.length).toBe(2);
7474
expect(entities[0].text).toBe('insert_tb');
7575
expect(entities[0].entityContextType).toBe(EntityContextType.TABLE);
7676
expect(entities[0].belongStmt.isContainCaret).toBeTruthy();
7777

78-
// TODO:
79-
// expect(entities[1].text).toBe('from_tb');
80-
// expect(entities[1].entityContextType).toBe(EntityContextType.TABLE);
81-
// expect(entities[1].belongStmt.isContainCaret).toBeTruthy();
78+
expect(entities[1].text).toBe('from_tb');
79+
expect(entities[1].entityContextType).toBe(EntityContextType.TABLE);
80+
expect(entities[1].belongStmt.isContainCaret).toBeTruthy();
8281
});
8382

8483
test('insert into table as select with trailing comma', () => {
@@ -121,14 +120,14 @@ describe('Impala SQL Syntax Suggestion with collect entity', () => {
121120
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
122121

123122
const entities = impala.getAllEntities(sql, pos);
124-
expect(entities.length).toBe(1);
123+
expect(entities.length).toBe(2);
125124
expect(entities[0].text).toBe('sorted_census_data');
126125
expect(entities[0].entityContextType).toBe(EntityContextType.TABLE_CREATE);
127126
expect(entities[0].belongStmt.isContainCaret).toBeTruthy();
128-
// TODO:
129-
// expect(entities[1].text).toBe('unsorted_census_data');
130-
// expect(entities[1].entityContextType).toBe(EntityContextType.TABLE);
131-
// expect(entities[1].belongStmt.isContainCaret).toBeTruthy();
127+
128+
expect(entities[1].text).toBe('unsorted_census_data');
129+
expect(entities[1].entityContextType).toBe(EntityContextType.TABLE);
130+
expect(entities[1].belongStmt.isContainCaret).toBeTruthy();
132131
});
133132

134133
test('create table as select with trailing comma', () => {

test/parser/trino/suggestion/suggestionWithEntity.test.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,14 @@ describe('PostgreSql Syntax Suggestion with collect entity', () => {
7070
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
7171

7272
const entities = trino.getAllEntities(sql, pos);
73-
expect(entities.length).toBe(1);
73+
expect(entities.length).toBe(2);
7474
expect(entities[0].text).toBe('insert_tb');
7575
expect(entities[0].entityContextType).toBe(EntityContextType.TABLE);
7676
expect(entities[0].belongStmt.isContainCaret).toBeTruthy();
7777

78-
// TODO:
79-
// expect(entities[1].text).toBe('from_tb');
80-
// expect(entities[1].entityContextType).toBe(EntityContextType.TABLE);
81-
// expect(entities[1].belongStmt.isContainCaret).toBeTruthy();
78+
expect(entities[1].text).toBe('from_tb');
79+
expect(entities[1].entityContextType).toBe(EntityContextType.TABLE);
80+
expect(entities[1].belongStmt.isContainCaret).toBeTruthy();
8281
});
8382

8483
test('insert into table as select with trailing comma', () => {
@@ -121,15 +120,14 @@ describe('PostgreSql Syntax Suggestion with collect entity', () => {
121120
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
122121

123122
const entities = trino.getAllEntities(sql, pos);
124-
expect(entities.length).toBe(1);
123+
expect(entities.length).toBe(2);
125124
expect(entities[0].text).toBe('sorted_census_data');
126125
expect(entities[0].entityContextType).toBe(EntityContextType.TABLE_CREATE);
127126
expect(entities[0].belongStmt.isContainCaret).toBeTruthy();
128127

129-
// TODO:
130-
// expect(entities[1].text).toBe('unsorted_census_data');
131-
// expect(entities[1].entityContextType).toBe(EntityContextType.TABLE);
132-
// expect(entities[1].belongStmt.isContainCaret).toBeTruthy();
128+
expect(entities[1].text).toBe('unsorted_census_data');
129+
expect(entities[1].entityContextType).toBe(EntityContextType.TABLE);
130+
expect(entities[1].belongStmt.isContainCaret).toBeTruthy();
133131
});
134132

135133
test('create table as select with trailing comma', () => {

0 commit comments

Comments
 (0)