UNPKG

dt-sql-parser

Version:

SQL Parsers for BigData, built with antlr4

127 lines (126 loc) 5.79 kB
import { FlinkSqlLexer } from '../../lib/flink/FlinkSqlLexer'; import { FlinkSqlParser } from '../../lib/flink/FlinkSqlParser'; import { BasicSQL } from '../common/basicSQL'; import { EntityContextType } from '../common/types'; import { FlinkEntityCollector } from './flinkEntityCollector'; import { FlinkErrorListener } from './flinkErrorListener'; import { FlinkSqlSplitListener } from './flinkSplitListener'; export { FlinkEntityCollector, FlinkSqlSplitListener }; export class FlinkSQL extends BasicSQL { constructor() { super(...arguments); this.preferredRules = new Set([ FlinkSqlParser.RULE_catalogPath, // catalog name FlinkSqlParser.RULE_databasePath, // database name FlinkSqlParser.RULE_databasePathCreate, // database name that will be created FlinkSqlParser.RULE_tablePath, // table name FlinkSqlParser.RULE_tablePathCreate, // table name that will be created FlinkSqlParser.RULE_viewPath, // view name path FlinkSqlParser.RULE_viewPathCreate, // viewName that will be created FlinkSqlParser.RULE_functionName, // functionName FlinkSqlParser.RULE_functionNameWithParams, // functionName FlinkSqlParser.RULE_reservedKeywordsFollowParamsUsedAsFuncName, // functionName FlinkSqlParser.RULE_reservedKeywordsNoParamsUsedAsFuncName, // functionName FlinkSqlParser.RULE_functionNameCreate, // functionName that will be created FlinkSqlParser.RULE_columnName, FlinkSqlParser.RULE_columnNameCreate, ]); } createLexerFromCharStream(charStreams) { return new FlinkSqlLexer(charStreams); } createParserFromTokenStream(tokenStream) { return new FlinkSqlParser(tokenStream); } get splitListener() { return new FlinkSqlSplitListener(); } createErrorListener(_errorListener) { const parserContext = this; return new FlinkErrorListener(_errorListener, parserContext, this.preferredRules); } createEntityCollector(input, allTokens, caretTokenIndex) { return new FlinkEntityCollector(input, allTokens, caretTokenIndex); } processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) { const originalSyntaxSuggestions = []; const keywords = []; for (let candidate of candidates.rules) { const [ruleType, candidateRule] = candidate; const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset; const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 1); let syntaxContextType = void 0; switch (ruleType) { case FlinkSqlParser.RULE_catalogPath: { syntaxContextType = EntityContextType.CATALOG; break; } case FlinkSqlParser.RULE_databasePath: { syntaxContextType = EntityContextType.DATABASE; break; } case FlinkSqlParser.RULE_databasePathCreate: { syntaxContextType = EntityContextType.DATABASE_CREATE; break; } case FlinkSqlParser.RULE_tablePath: { syntaxContextType = EntityContextType.TABLE; break; } case FlinkSqlParser.RULE_tablePathCreate: { syntaxContextType = EntityContextType.TABLE_CREATE; break; } case FlinkSqlParser.RULE_viewPath: { syntaxContextType = EntityContextType.VIEW; break; } case FlinkSqlParser.RULE_viewPathCreate: { syntaxContextType = EntityContextType.VIEW_CREATE; break; } case FlinkSqlParser.RULE_functionName: case FlinkSqlParser.RULE_functionNameWithParams: case FlinkSqlParser.RULE_reservedKeywordsFollowParamsUsedAsFuncName: case FlinkSqlParser.RULE_reservedKeywordsNoParamsUsedAsFuncName: { syntaxContextType = EntityContextType.FUNCTION; break; } case FlinkSqlParser.RULE_functionNameCreate: { syntaxContextType = EntityContextType.FUNCTION_CREATE; break; } case FlinkSqlParser.RULE_columnName: { syntaxContextType = EntityContextType.COLUMN; break; } case FlinkSqlParser.RULE_columnNameCreate: { syntaxContextType = EntityContextType.COLUMN_CREATE; break; } default: break; } if (syntaxContextType) { originalSyntaxSuggestions.push({ syntaxContextType, wordRanges: tokenRanges, }); } } for (let candidate of candidates.tokens) { const symbolicName = this._parser.vocabulary.getSymbolicName(candidate[0]); const displayName = this._parser.vocabulary.getDisplayName(candidate[0]); if (displayName && symbolicName && symbolicName.startsWith('KW_')) { const keyword = displayName.startsWith("'") && displayName.endsWith("'") ? displayName.slice(1, -1) : displayName; keywords.push(keyword); } } return { syntax: originalSyntaxSuggestions, keywords, }; } }