lava-oushudb-dt-sql-parser/src/parser/trinosql.ts

142 lines
5.1 KiB
TypeScript
Raw Normal View History

import { Token } from 'antlr4ts';
import { CandidatesCollection } from 'antlr4-c3';
import { TrinoSqlLexer } from '../lib/trinosql/TrinoSqlLexer';
import {
TrinoSqlParser,
ProgramContext,
SingleStatementContext,
} from '../lib/trinosql/TrinoSqlParser';
import { TrinoSqlListener } from '../lib/trinosql/TrinoSqlListener';
feat: support trino(presto) sql language (#105) * feat(trino): intergrate prestoGrammar to dt-sql-parser * feat(trino): add trinoSQl test framework * feat(trino): intergrate test files for trinoSQL * test(trino): support alterStatement test * test(trino): support alter table set authorization statement * feat(trino): complete trinosql alter unit tests * test(trino): complete dropStatement unit cases for trinosql * test(trino): complete create statement unit cases for trinoSQL * test(trino): complete insertStatement unit cases for trinoSQl * test(trino): dropStatement test files changed to short line split * test(trino): complete selectStatement unit cases and grammar check * test(trino): complete commentStatement unit case for trinoSQL * test(trino): complete analyze commit and call clause unit case * test(trino): complete delete deny and describe statement unit case * test(trino): complete explain execute and grant statement unit case * feat(trino): improve GRANT Role grammar * test(trino): complete show statement unit case * test(trino): complete truncateTable startTransaction update and values statement unit case * test(trino): improve update statement test cases * test(trino): complete revoke revoke roles and rollback statement unit case * test(trino): add set statement test case * feat: generator new trino parser and lexer file * feat(trino): improve alter statement grammar * test(trino): complete alter statement unit cases * feat(trino): support case-insensitive lexers * fix(trino): rm unless gen files * test(trino): complete merge and reset session statement unit cases * test(trino): complete merge anduse statement unit cases * test(trino): complete prepare and refresh materialized view statement unit cases * test(trino): improve statement unit cases * test(trino): complete match recognize statement unit cases * test(trino): complete window with row pattern recognition statement unit cases
2023-05-24 15:07:02 +08:00
import BasicParser from './common/basicParser';
import { Suggestions, SyntaxContextType, SyntaxSuggestion } from './common/basic-parser-types';
export default class TrinoSQL extends BasicParser<TrinoSqlLexer, ProgramContext, TrinoSqlParser> {
protected createLexerFormCharStream(charStreams) {
const lexer = new TrinoSqlLexer(charStreams);
feat: support trino(presto) sql language (#105) * feat(trino): intergrate prestoGrammar to dt-sql-parser * feat(trino): add trinoSQl test framework * feat(trino): intergrate test files for trinoSQL * test(trino): support alterStatement test * test(trino): support alter table set authorization statement * feat(trino): complete trinosql alter unit tests * test(trino): complete dropStatement unit cases for trinosql * test(trino): complete create statement unit cases for trinoSQL * test(trino): complete insertStatement unit cases for trinoSQl * test(trino): dropStatement test files changed to short line split * test(trino): complete selectStatement unit cases and grammar check * test(trino): complete commentStatement unit case for trinoSQL * test(trino): complete analyze commit and call clause unit case * test(trino): complete delete deny and describe statement unit case * test(trino): complete explain execute and grant statement unit case * feat(trino): improve GRANT Role grammar * test(trino): complete show statement unit case * test(trino): complete truncateTable startTransaction update and values statement unit case * test(trino): improve update statement test cases * test(trino): complete revoke revoke roles and rollback statement unit case * test(trino): add set statement test case * feat: generator new trino parser and lexer file * feat(trino): improve alter statement grammar * test(trino): complete alter statement unit cases * feat(trino): support case-insensitive lexers * fix(trino): rm unless gen files * test(trino): complete merge and reset session statement unit cases * test(trino): complete merge anduse statement unit cases * test(trino): complete prepare and refresh materialized view statement unit cases * test(trino): improve statement unit cases * test(trino): complete match recognize statement unit cases * test(trino): complete window with row pattern recognition statement unit cases
2023-05-24 15:07:02 +08:00
return lexer;
}
protected createParserFromTokenStream(tokenStream) {
const parser = new TrinoSqlParser(tokenStream);
feat: support trino(presto) sql language (#105) * feat(trino): intergrate prestoGrammar to dt-sql-parser * feat(trino): add trinoSQl test framework * feat(trino): intergrate test files for trinoSQL * test(trino): support alterStatement test * test(trino): support alter table set authorization statement * feat(trino): complete trinosql alter unit tests * test(trino): complete dropStatement unit cases for trinosql * test(trino): complete create statement unit cases for trinoSQL * test(trino): complete insertStatement unit cases for trinoSQl * test(trino): dropStatement test files changed to short line split * test(trino): complete selectStatement unit cases and grammar check * test(trino): complete commentStatement unit case for trinoSQL * test(trino): complete analyze commit and call clause unit case * test(trino): complete delete deny and describe statement unit case * test(trino): complete explain execute and grant statement unit case * feat(trino): improve GRANT Role grammar * test(trino): complete show statement unit case * test(trino): complete truncateTable startTransaction update and values statement unit case * test(trino): improve update statement test cases * test(trino): complete revoke revoke roles and rollback statement unit case * test(trino): add set statement test case * feat: generator new trino parser and lexer file * feat(trino): improve alter statement grammar * test(trino): complete alter statement unit cases * feat(trino): support case-insensitive lexers * fix(trino): rm unless gen files * test(trino): complete merge and reset session statement unit cases * test(trino): complete merge anduse statement unit cases * test(trino): complete prepare and refresh materialized view statement unit cases * test(trino): improve statement unit cases * test(trino): complete match recognize statement unit cases * test(trino): complete window with row pattern recognition statement unit cases
2023-05-24 15:07:02 +08:00
return parser;
}
protected get splitListener() {
return new TrinoSqlSplitListener();
}
protected preferredRules: Set<number> = new Set([
TrinoSqlParser.RULE_catalogName,
TrinoSqlParser.RULE_catalogNameCreate,
TrinoSqlParser.RULE_schemaName,
TrinoSqlParser.RULE_schemaNameCreate,
TrinoSqlParser.RULE_tableName,
TrinoSqlParser.RULE_tableNameCreate,
TrinoSqlParser.RULE_viewName,
TrinoSqlParser.RULE_viewNameCreate,
TrinoSqlParser.RULE_functionName,
TrinoSqlParser.RULE_columnName,
TrinoSqlParser.RULE_columnNameCreate,
]);
protected processCandidates(
candidates: CandidatesCollection,
allTokens: Token[],
caretTokenIndex: number,
tokenIndexOffset: number
): Suggestions<Token> {
const originalSyntaxSuggestions: SyntaxSuggestion<Token>[] = [];
const keywords: string[] = [];
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: SyntaxContextType;
switch (ruleType) {
case TrinoSqlParser.RULE_catalogName: {
syntaxContextType = SyntaxContextType.CATALOG;
break;
}
case TrinoSqlParser.RULE_schemaName: {
syntaxContextType = SyntaxContextType.DATABASE;
break;
}
case TrinoSqlParser.RULE_schemaNameCreate: {
syntaxContextType = SyntaxContextType.DATABASE_CREATE;
break;
}
case TrinoSqlParser.RULE_tableName: {
syntaxContextType = SyntaxContextType.TABLE;
break;
}
case TrinoSqlParser.RULE_tableNameCreate: {
syntaxContextType = SyntaxContextType.TABLE_CREATE;
break;
}
case TrinoSqlParser.RULE_viewName: {
syntaxContextType = SyntaxContextType.VIEW;
break;
}
case TrinoSqlParser.RULE_viewNameCreate: {
syntaxContextType = SyntaxContextType.VIEW_CREATE;
break;
}
case TrinoSqlParser.RULE_functionName: {
syntaxContextType = SyntaxContextType.FUNCTION;
break;
}
case TrinoSqlParser.RULE_columnNameCreate: {
syntaxContextType = SyntaxContextType.COLUMN_CREATE;
break;
}
case TrinoSqlParser.RULE_columnName: {
syntaxContextType = SyntaxContextType.COLUMN;
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 (symbolicName && symbolicName.startsWith('KW_')) {
const keyword =
displayName.startsWith("'") && displayName.endsWith("'")
? displayName.slice(1, -1)
: displayName;
keywords.push(keyword);
}
}
return {
syntax: originalSyntaxSuggestions,
keywords,
};
}
feat: support trino(presto) sql language (#105) * feat(trino): intergrate prestoGrammar to dt-sql-parser * feat(trino): add trinoSQl test framework * feat(trino): intergrate test files for trinoSQL * test(trino): support alterStatement test * test(trino): support alter table set authorization statement * feat(trino): complete trinosql alter unit tests * test(trino): complete dropStatement unit cases for trinosql * test(trino): complete create statement unit cases for trinoSQL * test(trino): complete insertStatement unit cases for trinoSQl * test(trino): dropStatement test files changed to short line split * test(trino): complete selectStatement unit cases and grammar check * test(trino): complete commentStatement unit case for trinoSQL * test(trino): complete analyze commit and call clause unit case * test(trino): complete delete deny and describe statement unit case * test(trino): complete explain execute and grant statement unit case * feat(trino): improve GRANT Role grammar * test(trino): complete show statement unit case * test(trino): complete truncateTable startTransaction update and values statement unit case * test(trino): improve update statement test cases * test(trino): complete revoke revoke roles and rollback statement unit case * test(trino): add set statement test case * feat: generator new trino parser and lexer file * feat(trino): improve alter statement grammar * test(trino): complete alter statement unit cases * feat(trino): support case-insensitive lexers * fix(trino): rm unless gen files * test(trino): complete merge and reset session statement unit cases * test(trino): complete merge anduse statement unit cases * test(trino): complete prepare and refresh materialized view statement unit cases * test(trino): improve statement unit cases * test(trino): complete match recognize statement unit cases * test(trino): complete window with row pattern recognition statement unit cases
2023-05-24 15:07:02 +08:00
}
export class TrinoSqlSplitListener implements TrinoSqlListener {
private _statementsContext: SingleStatementContext[] = [];
exitSingleStatement = (ctx: SingleStatementContext) => {
this._statementsContext.push(ctx);
};
get statementsContext() {
return this._statementsContext;
}
}