UNPKG

dt-sql-parser

Version:

SQL Parsers for BigData, built with antlr4

122 lines (121 loc) 5.51 kB
import { processTokenCandidates } from '../common/tokenUtils'; import { ImpalaSqlLexer } from '../../lib/impala/ImpalaSqlLexer'; import { ImpalaSqlParser } from '../../lib/impala/ImpalaSqlParser'; import { BasicSQL } from '../common/basicSQL'; import { EntityContextType, } from '../common/types'; import { ImpalaEntityCollector } from './impalaEntityCollector'; import { ImpalaErrorListener } from './ImpalaErrorListener'; import { ImpalaSqlSplitListener } from './impalaSplitListener'; import { ImpalaSemanticContextCollector } from './impalaSemanticContextCollector'; export { ImpalaEntityCollector, ImpalaSqlSplitListener }; export class ImpalaSQL extends BasicSQL { constructor() { super(...arguments); this.preferredRules = new Set([ ImpalaSqlParser.RULE_functionNameCreate, ImpalaSqlParser.RULE_tableNameCreate, ImpalaSqlParser.RULE_viewNameCreate, ImpalaSqlParser.RULE_databaseNameCreate, ImpalaSqlParser.RULE_columnNamePathCreate, ImpalaSqlParser.RULE_tableNamePath, ImpalaSqlParser.RULE_functionNamePath, ImpalaSqlParser.RULE_viewNamePath, ImpalaSqlParser.RULE_databaseNamePath, ImpalaSqlParser.RULE_columnNamePath, ImpalaSqlParser.RULE_columnName, ]); } createLexerFromCharStream(charStreams) { return new ImpalaSqlLexer(charStreams); } createParserFromTokenStream(tokenStream) { return new ImpalaSqlParser(tokenStream); } get splitListener() { return new ImpalaSqlSplitListener(); } createErrorListener(_errorListener) { const parserContext = this; return new ImpalaErrorListener(_errorListener, parserContext, this.preferredRules); } createEntityCollector(input, allTokens, caretTokenIndex) { return new ImpalaEntityCollector(input, allTokens, caretTokenIndex); } createSemanticContextCollector(input, caretPosition, allTokens, options) { return new ImpalaSemanticContextCollector(input, caretPosition, allTokens, options); } processCandidates(candidates, allTokens, caretTokenIndex) { const originalSyntaxSuggestions = []; const keywords = []; for (let candidate of candidates.rules) { const [ruleType, candidateRule] = candidate; const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1); let syntaxContextType = void 0; switch (ruleType) { case ImpalaSqlParser.RULE_functionNameCreate: { syntaxContextType = EntityContextType.FUNCTION_CREATE; break; } case ImpalaSqlParser.RULE_tableNameCreate: { syntaxContextType = EntityContextType.TABLE_CREATE; break; } case ImpalaSqlParser.RULE_databaseNameCreate: { syntaxContextType = EntityContextType.DATABASE_CREATE; break; } case ImpalaSqlParser.RULE_viewNameCreate: { syntaxContextType = EntityContextType.VIEW_CREATE; break; } case ImpalaSqlParser.RULE_columnNamePathCreate: { syntaxContextType = EntityContextType.COLUMN_CREATE; break; } case ImpalaSqlParser.RULE_databaseNamePath: { syntaxContextType = EntityContextType.DATABASE; break; } case ImpalaSqlParser.RULE_tableNamePath: { syntaxContextType = EntityContextType.TABLE; break; } case ImpalaSqlParser.RULE_viewNamePath: { syntaxContextType = EntityContextType.VIEW; break; } case ImpalaSqlParser.RULE_functionNamePath: { syntaxContextType = EntityContextType.FUNCTION; break; } case ImpalaSqlParser.RULE_columnNamePath: { syntaxContextType = EntityContextType.COLUMN; } case ImpalaSqlParser.RULE_columnName: { if (candidateRule.ruleList.includes(ImpalaSqlParser.RULE_columnItem) || candidateRule.ruleList.includes(ImpalaSqlParser.RULE_havingClause) || candidateRule.ruleList.includes(ImpalaSqlParser.RULE_whereClause) || candidateRule.ruleList.includes(ImpalaSqlParser.RULE_whenClause) || candidateRule.ruleList.includes(ImpalaSqlParser.RULE_partitionByClause) || candidateRule.ruleList.includes(ImpalaSqlParser.RULE_relation)) { syntaxContextType = EntityContextType.COLUMN; } } default: break; } if (syntaxContextType) { originalSyntaxSuggestions.push({ syntaxContextType, wordRanges: tokenRanges, }); } } const processedKeywords = processTokenCandidates(this._parser, candidates.tokens); keywords.push(...processedKeywords); return { syntax: originalSyntaxSuggestions, keywords, }; } }