@@ -3,6 +3,7 @@ import path from 'path';
33import { FlinkSQL } from 'src/parser/flink' ;
44import { CaretPosition , EntityContextType } from 'src/parser/common/types' ;
55import { commentOtherLine } from 'test/helper' ;
6+ import { CommonEntityContext , TableDeclareType } from 'src/parser/common/entityCollector' ;
67
78const syntaxSql = fs . readFileSync (
89 path . join ( __dirname , 'fixtures' , 'suggestionWithEntity.sql' ) ,
@@ -52,10 +53,12 @@ describe('Flink SQL Syntax Suggestion with collect entity', () => {
5253 expect ( suggestion ?. wordRanges . map ( ( token ) => token . text ) ) . toEqual ( [ ] ) ;
5354
5455 const entities = flink . getAllEntities ( sql , pos ) ;
55- expect ( entities . length ) . toBe ( 1 ) ;
56+ expect ( entities . length ) . toBe ( 2 ) ;
5657 expect ( entities [ 0 ] . text ) . toBe ( 'tb' ) ;
5758 expect ( entities [ 0 ] . entityContextType ) . toBe ( EntityContextType . TABLE ) ;
5859 expect ( entities [ 0 ] . belongStmt . isContainCaret ) . toBeTruthy ( ) ;
60+
61+ expect ( entities [ 1 ] . entityContextType ) . toBe ( EntityContextType . QUERY_RESULT ) ;
5962 } ) ;
6063
6164 test ( 'insert into from nested query with no column' , ( ) => {
@@ -73,14 +76,28 @@ describe('Flink SQL Syntax Suggestion with collect entity', () => {
7376 expect ( suggestion ?. wordRanges . map ( ( token ) => token . text ) ) . toEqual ( [ ] ) ;
7477
7578 const entities = flink . getAllEntities ( sql , pos ) ;
76- expect ( entities . length ) . toBe ( 2 ) ;
77- expect ( entities [ 0 ] . text ) . toBe ( 'insert_tb' ) ;
78- expect ( entities [ 0 ] . entityContextType ) . toBe ( EntityContextType . TABLE ) ;
79- expect ( entities [ 0 ] . belongStmt . isContainCaret ) . toBeTruthy ( ) ;
79+ expect ( entities . length ) . toBe ( 4 ) ;
8080
81- expect ( entities [ 1 ] . text ) . toBe ( 'inside_tb' ) ;
82- expect ( entities [ 1 ] . entityContextType ) . toBe ( EntityContextType . TABLE ) ;
83- expect ( entities [ 1 ] . belongStmt . isContainCaret ) . toBeTruthy ( ) ;
81+ const insertTableEntity = entities . find (
82+ ( e ) => e . entityContextType === EntityContextType . TABLE && e . text === 'insert_tb'
83+ ) ;
84+ const insideTableEntity = entities . find (
85+ ( e ) => e . entityContextType === EntityContextType . TABLE && e . text === 'inside_tb'
86+ ) ;
87+ const derivedTableEntity = entities . find (
88+ ( e : CommonEntityContext ) =>
89+ e . entityContextType === EntityContextType . TABLE &&
90+ e . declareType === TableDeclareType . EXPRESSION
91+ ) ;
92+ const queryResultEntity = entities . find (
93+ ( e ) => e . entityContextType === EntityContextType . QUERY_RESULT
94+ ) ;
95+
96+ expect ( insideTableEntity . belongStmt . isContainCaret ) . toBeTruthy ( ) ;
97+ expect ( insertTableEntity . belongStmt . isContainCaret ) . toBeTruthy ( ) ;
98+ expect ( derivedTableEntity . belongStmt . isContainCaret ) . toBeTruthy ( ) ;
99+
100+ expect ( queryResultEntity . text ) . toBe ( 'col1, col2, country, state' ) ;
84101 } ) ;
85102
86103 test ( 'insert into from nested query with columns and trailing comma' , ( ) => {
@@ -98,14 +115,24 @@ describe('Flink SQL Syntax Suggestion with collect entity', () => {
98115 expect ( suggestion ?. wordRanges . map ( ( token ) => token . text ) ) . toEqual ( [ ] ) ;
99116
100117 const entities = flink . getAllEntities ( sql , pos ) ;
101- expect ( entities . length ) . toBe ( 2 ) ;
102- expect ( entities [ 0 ] . text ) . toBe ( 'insert_tb' ) ;
103- expect ( entities [ 0 ] . entityContextType ) . toBe ( EntityContextType . TABLE ) ;
104- expect ( entities [ 0 ] . belongStmt . isContainCaret ) . toBeTruthy ( ) ;
105118
106- expect ( entities [ 1 ] . text ) . toBe ( 'inside_tb' ) ;
107- expect ( entities [ 1 ] . entityContextType ) . toBe ( EntityContextType . TABLE ) ;
108- expect ( entities [ 1 ] . belongStmt . isContainCaret ) . toBeTruthy ( ) ;
119+ expect ( entities . length ) . toBe ( 5 ) ;
120+
121+ const insertTableEntity = entities . find (
122+ ( e ) => e . entityContextType === EntityContextType . TABLE && e . text === 'insert_tb'
123+ ) ;
124+ const insideTableEntity = entities . find (
125+ ( e ) => e . entityContextType === EntityContextType . TABLE && e . text === 'inside_tb'
126+ ) ;
127+ const derivedTableEntity = entities . find (
128+ ( e : CommonEntityContext ) =>
129+ e . entityContextType === EntityContextType . TABLE &&
130+ e . declareType === TableDeclareType . EXPRESSION
131+ ) ;
132+
133+ expect ( insertTableEntity . belongStmt . isContainCaret ) . toBeTruthy ( ) ;
134+ expect ( insideTableEntity . belongStmt . isContainCaret ) . toBeTruthy ( ) ;
135+ expect ( derivedTableEntity . belongStmt . isContainCaret ) . toBeTruthy ( ) ;
109136 } ) ;
110137
111138 test ( 'create table as select with no column' , ( ) => {
@@ -124,12 +151,13 @@ describe('Flink SQL Syntax Suggestion with collect entity', () => {
124151
125152 const entities = flink . getAllEntities ( sql , pos ) ;
126153 expect ( entities . length ) . toBe ( 2 ) ;
127- expect ( entities [ 0 ] . text ) . toBe ( 'derived_table' ) ;
128- expect ( entities [ 0 ] . entityContextType ) . toBe ( EntityContextType . TABLE_CREATE ) ;
154+
155+ expect ( entities [ 0 ] . text ) . toBe ( 'origin_table' ) ;
156+ expect ( entities [ 0 ] . entityContextType ) . toBe ( EntityContextType . TABLE ) ;
129157 expect ( entities [ 0 ] . belongStmt . isContainCaret ) . toBeTruthy ( ) ;
130158
131- expect ( entities [ 1 ] . text ) . toBe ( 'origin_table ' ) ;
132- expect ( entities [ 1 ] . entityContextType ) . toBe ( EntityContextType . TABLE ) ;
159+ expect ( entities [ 1 ] . text ) . toBe ( 'derived_table ' ) ;
160+ expect ( entities [ 1 ] . entityContextType ) . toBe ( EntityContextType . TABLE_CREATE ) ;
133161 expect ( entities [ 1 ] . belongStmt . isContainCaret ) . toBeTruthy ( ) ;
134162 } ) ;
135163
@@ -148,14 +176,22 @@ describe('Flink SQL Syntax Suggestion with collect entity', () => {
148176 expect ( suggestion ?. wordRanges . map ( ( token ) => token . text ) ) . toEqual ( [ ] ) ;
149177
150178 const entities = flink . getAllEntities ( sql , pos ) ;
151- expect ( entities . length ) . toBe ( 2 ) ;
152- expect ( entities [ 0 ] . text ) . toBe ( 'derived_table' ) ;
153- expect ( entities [ 0 ] . entityContextType ) . toBe ( EntityContextType . TABLE_CREATE ) ;
154- expect ( entities [ 0 ] . belongStmt . isContainCaret ) . toBeTruthy ( ) ;
179+ expect ( entities . length ) . toBe ( 3 ) ;
155180
156- expect ( entities [ 1 ] . text ) . toBe ( 'origin_table' ) ;
157- expect ( entities [ 1 ] . entityContextType ) . toBe ( EntityContextType . TABLE ) ;
158- expect ( entities [ 1 ] . belongStmt . isContainCaret ) . toBeTruthy ( ) ;
181+ const derivedTableEntity = entities . find (
182+ ( e ) =>
183+ e . entityContextType === EntityContextType . TABLE_CREATE && e . text === 'derived_table'
184+ ) ;
185+ const originTableEntity = entities . find (
186+ ( e ) => e . entityContextType === EntityContextType . TABLE && e . text === 'origin_table'
187+ ) ;
188+ const queryResultEntity = entities . find (
189+ ( e ) => e . entityContextType === EntityContextType . QUERY_RESULT
190+ ) ;
191+
192+ expect ( derivedTableEntity . belongStmt . isContainCaret ) . toBeTruthy ( ) ;
193+ expect ( originTableEntity . belongStmt . isContainCaret ) . toBeTruthy ( ) ;
194+ expect ( queryResultEntity . text ) . toBe ( 'id,' ) ;
159195 } ) ;
160196
161197 test ( 'isContainCaret should be truthy if caret position is whitespace at the end of statement' , ( ) => {
0 commit comments