@Override public Node visitInterval(SqlBaseParser.IntervalContext context) { return new IntervalLiteral( getLocation(context), ((StringLiteral) visit(context.string())).getValue(), Optional.ofNullable(context.sign) .map(AstBuilder::getIntervalSign) .orElse(IntervalLiteral.Sign.POSITIVE), getIntervalFieldType((Token) context.from.getChild(0).getPayload()), Optional.ofNullable(context.to) .map((x) -> x.getChild(0).getPayload()) .map(Token.class::cast) .map(AstBuilder::getIntervalFieldType)); }
return "INTERVAL " + getIntervalFieldType((Token) type.from.getChild(0).getPayload()) + " TO " + getIntervalFieldType((Token) type.to.getChild(0).getPayload());
@Override public Node visitSampledRelation(SqlBaseParser.SampledRelationContext context) { Relation child = (Relation) visit(context.aliasedRelation()); if (context.TABLESAMPLE() == null) { return child; } return new SampledRelation( getLocation(context), child, getSamplingMethod((Token) context.sampleType().getChild(0).getPayload()), (Expression) visit(context.percentage)); }
@Override public void exitNonReserved(SqlBaseParser.NonReservedContext context) { // we can't modify the tree during rule enter/exit event handling unless we're dealing with a terminal. // Otherwise, ANTLR gets confused an fires spurious notifications. if (!(context.getChild(0) instanceof TerminalNode)) { int rule = ((ParserRuleContext) context.getChild(0)).getRuleIndex(); throw new AssertionError("nonReserved can only contain tokens. Found nested rule: " + ruleNames.get(rule)); } // replace nonReserved words with IDENT tokens context.getParent().removeLastChild(); Token token = (Token) context.getChild(0).getPayload(); context.getParent().addChild(new CommonToken( new Pair<>(token.getTokenSource(), token.getInputStream()), SqlBaseLexer.IDENTIFIER, token.getChannel(), token.getStartIndex(), token.getStopIndex())); } }
private Object getPayload(ParseTree tree) { if (tree.getChildCount() == 0) { return tree.getPayload(); } else { String ruleName = tree.getClass().getSimpleName().replace("Context", ""); return Character.toLowerCase(ruleName.charAt(0)) + ruleName.substring(1); } }
private static void collectToken(ParseTree node, List<Token> token) { for(int i=0; i < node.getChildCount(); i++) { ParseTree child = node.getChild(i); if(child.getPayload() instanceof Token) { token.add((Token) child.getPayload()); } else { collectToken(child, token); } } }
@Override public Node visitInterval(SqlBaseParser.IntervalContext context) { return new IntervalLiteral( getLocation(context), unquote(context.STRING().getText()), Optional.ofNullable(context.sign) .map(AstBuilder::getIntervalSign) .orElse(IntervalLiteral.Sign.POSITIVE), getIntervalFieldType((Token) context.from.getChild(0).getPayload()), Optional.ofNullable(context.to) .map((x) -> x.getChild(0).getPayload()) .map(Token.class::cast) .map(AstBuilder::getIntervalFieldType)); }
@Override public Node visitInterval(SqlBaseParser.IntervalContext context) { return new IntervalLiteral( getLocation(context), ((StringLiteral) visit(context.string())).getValue(), Optional.ofNullable(context.sign) .map(AstBuilder::getIntervalSign) .orElse(IntervalLiteral.Sign.POSITIVE), getIntervalFieldType((Token) context.from.getChild(0).getPayload()), Optional.ofNullable(context.to) .map((x) -> x.getChild(0).getPayload()) .map(Token.class::cast) .map(AstBuilder::getIntervalFieldType)); }
@Override public Node visitInterval(SqlBaseParser.IntervalContext context) { return new IntervalLiteral( getLocation(context), ((StringLiteral) visit(context.string())).getValue(), Optional.ofNullable(context.sign) .map(AstBuilder::getIntervalSign) .orElse(IntervalLiteral.Sign.POSITIVE), getIntervalFieldType((Token) context.from.getChild(0).getPayload()), Optional.ofNullable(context.to) .map((x) -> x.getChild(0).getPayload()) .map(Token.class::cast) .map(AstBuilder::getIntervalFieldType)); }
@Override public Node visitInterval(SqlBaseParser.IntervalContext context) { return new IntervalLiteral( getLocation(context), ((StringLiteral) visit(context.string())).getValue(), Optional.ofNullable(context.sign) .map(AstBuilder::getIntervalSign) .orElse(IntervalLiteral.Sign.POSITIVE), getIntervalFieldType((Token) context.from.getChild(0).getPayload()), Optional.ofNullable(context.to) .map((x) -> x.getChild(0).getPayload()) .map(Token.class::cast) .map(AstBuilder::getIntervalFieldType)); }
@Override public Node visitSampledRelation(SqlBaseParser.SampledRelationContext context) { Relation child = (Relation) visit(context.aliasedRelation()); if (context.TABLESAMPLE() == null) { return child; } Optional<List<Expression>> stratifyOn = Optional.empty(); if (context.STRATIFY() != null) { stratifyOn = Optional.of(visit(context.stratify, Expression.class)); } return new SampledRelation( getLocation(context), child, getSamplingMethod((Token) context.sampleType().getChild(0).getPayload()), (Expression) visit(context.percentage), context.RESCALED() != null, stratifyOn); }
@Override public Node visitSampledRelation(SqlBaseParser.SampledRelationContext context) { Relation child = (Relation) visit(context.aliasedRelation()); if (context.TABLESAMPLE() == null) { return child; } return new SampledRelation( getLocation(context), child, getSamplingMethod((Token) context.sampleType().getChild(0).getPayload()), (Expression) visit(context.percentage)); }
@Override public Node visitSampledRelation(SqlBaseParser.SampledRelationContext context) { Relation child = (Relation) visit(context.aliasedRelation()); if (context.TABLESAMPLE() == null) { return child; } return new SampledRelation( getLocation(context), child, getSamplingMethod((Token) context.sampleType().getChild(0).getPayload()), (Expression) visit(context.percentage)); }
@Override public Node visitSampledRelation(SqlBaseParser.SampledRelationContext context) { Relation child = (Relation) visit(context.aliasedRelation()); if (context.TABLESAMPLE() == null) { return child; } return new SampledRelation( getLocation(context), child, getSamplingMethod((Token) context.sampleType().getChild(0).getPayload()), (Expression) visit(context.percentage)); }
@Override public void exitNonReserved(SqlBaseParser.NonReservedContext context) { // we can't modify the tree during rule enter/exit event handling unless we're dealing with a terminal. // Otherwise, ANTLR gets confused an fires spurious notifications. if (!(context.getChild(0) instanceof TerminalNode)) { int rule = ((ParserRuleContext) context.getChild(0)).getRuleIndex(); throw new AssertionError( "nonReserved can only contain tokens. Found nested rule: " + ruleNames .get(rule)); } // replace nonReserved words with IDENT tokens context.getParent().removeLastChild(); Token token = (Token) context.getChild(0).getPayload(); context.getParent().addChild(new CommonToken( new Pair<>(token.getTokenSource(), token.getInputStream()), SqlBaseLexer.IDENTIFIER, token.getChannel(), token.getStartIndex(), token.getStopIndex())); } }
@Override public void exitNonReserved(BQLBaseParser.NonReservedContext context) { // We can't modify the tree during rule enter/exit event handling unless we're dealing with a terminal. // Otherwise, ANTLR gets confused an fires spurious notifications. if (!(context.getChild(0) instanceof TerminalNode)) { int rule = ((ParserRuleContext) context.getChild(0)).getRuleIndex(); throw new AssertionError("NonReserved can only contain tokens. Found nested rule: " + ruleNames.get(rule)); } // Replace nonReserved words with IDENT tokens. context.getParent().removeLastChild(); Token token = (Token) context.getChild(0).getPayload(); context.getParent().addChild(new CommonToken( new Pair<>(token.getTokenSource(), token.getInputStream()), BQLBaseLexer.IDENTIFIER, token.getChannel(), token.getStartIndex(), token.getStopIndex())); } }
@Override public void exitNonReserved(SqlBaseParser.NonReservedContext context) { // we can't modify the tree during rule enter/exit event handling unless we're dealing with a terminal. // Otherwise, ANTLR gets confused an fires spurious notifications. if (!(context.getChild(0) instanceof TerminalNode)) { int rule = ((ParserRuleContext) context.getChild(0)).getRuleIndex(); throw new AssertionError("nonReserved can only contain tokens. Found nested rule: " + ruleNames.get(rule)); } // replace nonReserved words with IDENT tokens context.getParent().removeLastChild(); Token token = (Token) context.getChild(0).getPayload(); context.getParent().addChild(new CommonToken( new Pair<>(token.getTokenSource(), token.getInputStream()), SqlBaseLexer.IDENTIFIER, token.getChannel(), token.getStartIndex(), token.getStopIndex())); } }
@Override public void exitNonReserved(SqlBaseParser.NonReservedContext context) { // we can't modify the tree during rule enter/exit event handling unless we're dealing with a terminal. // Otherwise, ANTLR gets confused an fires spurious notifications. if (!(context.getChild(0) instanceof TerminalNode)) { int rule = ((ParserRuleContext) context.getChild(0)).getRuleIndex(); throw new AssertionError("nonReserved can only contain tokens. Found nested rule: " + ruleNames.get(rule)); } // replace nonReserved words with IDENT tokens context.getParent().removeLastChild(); Token token = (Token) context.getChild(0).getPayload(); context.getParent().addChild(new CommonToken( new Pair<>(token.getTokenSource(), token.getInputStream()), SqlBaseLexer.IDENTIFIER, token.getChannel(), token.getStartIndex(), token.getStopIndex())); } }
@Override public void exitQuotedIdentifier(SqlBaseParser.QuotedIdentifierContext context) { // Remove quotes context.getParent().removeLastChild(); Token token = (Token) context.getChild(0).getPayload(); context.getParent().addChild(new CommonToken( new Pair<>(token.getTokenSource(), token.getInputStream()), SqlBaseLexer.IDENTIFIER, token.getChannel(), token.getStartIndex() + 1, token.getStopIndex() - 1)); }
@Override public void exitNonReserved(SqlBaseParser.NonReservedContext context) { // replace nonReserved words with IDENT tokens context.getParent().removeLastChild(); Token token = (Token) context.getChild(0).getPayload(); context.getParent().addChild(new CommonToken( new Pair<>(token.getTokenSource(), token.getInputStream()), SqlBaseLexer.IDENTIFIER, token.getChannel(), token.getStartIndex(), token.getStopIndex())); } }