@@ -3,6 +3,11 @@ import { FlinkSqlLexer } from 'src/lib/flink/FlinkSqlLexer';
33import { ErrorListener } from 'src/parser/common/parseErrorListener' ;
44import { CommonTokenStream } from 'antlr4ng' ;
55
6+ type DfaInterpreter = { decisionToDFA : Array < { states : { length : number } } > } ;
7+
8+ const countDfaStates = ( interpreter : DfaInterpreter ) =>
9+ interpreter . decisionToDFA . reduce ( ( count , dfa ) => count + dfa . states . length , 0 ) ;
10+
611describe ( 'BasicSQL unit tests' , ( ) => {
712 const flink = new FlinkSQL ( ) ;
813 test ( 'Create lexer' , ( ) => {
@@ -81,6 +86,31 @@ describe('BasicSQL unit tests', () => {
8186 expect ( errors . length ) . not . toBe ( 0 ) ;
8287 } ) ;
8388
89+ test ( 'Reset shared DFA caches and reparse the same input' , ( ) => {
90+ const parser = new FlinkSQL ( ) ;
91+ const sql = 'SELECT a, b FROM tbl1 WHERE a = 1 ORDER BY b' ;
92+
93+ expect ( parser . validate ( sql ) ) . toHaveLength ( 0 ) ;
94+
95+ const parserInternals = parser as unknown as {
96+ _lexer : { interpreter : DfaInterpreter } ;
97+ _parser : { interpreter : DfaInterpreter } ;
98+ } ;
99+
100+ expect ( countDfaStates ( parserInternals . _lexer . interpreter ) ) . toBeGreaterThan ( 0 ) ;
101+ expect ( countDfaStates ( parserInternals . _parser . interpreter ) ) . toBeGreaterThan ( 0 ) ;
102+
103+ parser . resetDfaCache ( ) ;
104+
105+ expect ( countDfaStates ( parserInternals . _lexer . interpreter ) ) . toBe ( 0 ) ;
106+ expect ( countDfaStates ( parserInternals . _parser . interpreter ) ) . toBe ( 0 ) ;
107+ expect ( parser . getParsedInput ( ) ) . toBe ( '' ) ;
108+
109+ expect ( parser . validate ( sql ) ) . toHaveLength ( 0 ) ;
110+ expect ( countDfaStates ( parserInternals . _lexer . interpreter ) ) . toBeGreaterThan ( 0 ) ;
111+ expect ( countDfaStates ( parserInternals . _parser . interpreter ) ) . toBeGreaterThan ( 0 ) ;
112+ } ) ;
113+
84114 test ( 'Get All tokens' , ( ) => {
85115 const sql = 'SELECT * FROM tbl1;' ;
86116 const tokens = flink . getAllTokens ( sql ) ;
0 commit comments