Skip to content

Commit e007a5c

Browse files
committed
feat: mark as entityCollecting in getAllEntities context to allow empty column
1 parent ec65d4e commit e007a5c

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) {
@@ -557,29 +558,28 @@ export abstract class BasicSQL<
557558
}
558559

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

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

578+
const parseTree = parser.program();
579579
this.listen(collectListener, parseTree);
580580

581-
// parser.caretTokenIndex = -1;
582-
// parser.entityCollecting = false;
581+
parser.caretTokenIndex = -1;
582+
parser.entityCollecting = false;
583583

584584
return collectListener.getEntities();
585585
}

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)