Search in sources :

Example 1 with ASTNode

use of org.mvel2.ast.ASTNode in project pinot by linkedin.

the class TransformExpressionTree method buildTree.

/**
   * Given the root node for AST tree, builds the expression tree, and returns
   * the root node of the expression tree.
   *
   * @param rootNode Root Node of AST tree
   * @return Root node of the Expression tree
   */
public static TransformExpressionTree buildTree(AstNode rootNode) {
    TransformExpressionTree expressionTree;
    if (!rootNode.hasChildren()) {
        return new TransformExpressionTree(rootNode);
    } else {
        expressionTree = new TransformExpressionTree(rootNode);
        expressionTree._children = new ArrayList<>();
        for (AstNode child : rootNode.getChildren()) {
            expressionTree._children.add(buildTree(child));
        }
    }
    return expressionTree;
}
Also used : FunctionCallAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.FunctionCallAstNode) IdentifierAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.IdentifierAstNode) AstNode(com.linkedin.pinot.pql.parsers.pql2.ast.AstNode) LiteralAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.LiteralAstNode)

Example 2 with ASTNode

use of org.mvel2.ast.ASTNode in project pinot by linkedin.

the class Pql2AstListener method pushNode.

private void pushNode(AstNode node) {
    if (_rootNode == null) {
        _rootNode = node;
    }
    AstNode parentNode = null;
    if (!_nodeStack.isEmpty()) {
        parentNode = _nodeStack.peek();
    }
    if (parentNode != null) {
        parentNode.addChild(node);
    }
    node.setParent(parentNode);
    _nodeStack.push(node);
}
Also used : BetweenPredicateAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.BetweenPredicateAstNode) IdentifierAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.IdentifierAstNode) AstNode(com.linkedin.pinot.pql.parsers.pql2.ast.AstNode) FloatingPointLiteralAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.FloatingPointLiteralAstNode) HavingAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.HavingAstNode) OutputColumnAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.OutputColumnAstNode) OrderByAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.OrderByAstNode) OrderByExpressionAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.OrderByExpressionAstNode) StarExpressionAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.StarExpressionAstNode) LimitAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.LimitAstNode) SelectAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.SelectAstNode) WhereAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.WhereAstNode) IsPredicateAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.IsPredicateAstNode) StringLiteralAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.StringLiteralAstNode) ExpressionParenthesisGroupAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.ExpressionParenthesisGroupAstNode) FunctionCallAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.FunctionCallAstNode) IntegerLiteralAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.IntegerLiteralAstNode) TopAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.TopAstNode) OutputColumnListAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.OutputColumnListAstNode) BooleanOperatorAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.BooleanOperatorAstNode) BinaryMathOpAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.BinaryMathOpAstNode) ComparisonPredicateAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.ComparisonPredicateAstNode) TableNameAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.TableNameAstNode) InPredicateAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.InPredicateAstNode) PredicateParenthesisGroupAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.PredicateParenthesisGroupAstNode) GroupByAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.GroupByAstNode) PredicateListAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.PredicateListAstNode) StarColumnListAstNode(com.linkedin.pinot.pql.parsers.pql2.ast.StarColumnListAstNode)

Example 3 with ASTNode

use of org.mvel2.ast.ASTNode in project pinot by linkedin.

the class Pql2Compiler method compileToBrokerRequest.

@Override
public BrokerRequest compileToBrokerRequest(String expression) throws Pql2CompilationException {
    try {
        //
        CharStream charStream = new ANTLRInputStream(expression);
        PQL2Lexer lexer = new PQL2Lexer(charStream);
        lexer.setTokenFactory(new CommonTokenFactory(true));
        TokenStream tokenStream = new UnbufferedTokenStream<CommonToken>(lexer);
        PQL2Parser parser = new PQL2Parser(tokenStream);
        parser.setErrorHandler(new BailErrorStrategy());
        // Parse
        ParseTree parseTree = parser.root();
        ParseTreeWalker walker = new ParseTreeWalker();
        Pql2AstListener listener = new Pql2AstListener(expression);
        walker.walk(listener, parseTree);
        AstNode rootNode = listener.getRootNode();
        BrokerRequest brokerRequest = new BrokerRequest();
        rootNode.updateBrokerRequest(brokerRequest);
        return brokerRequest;
    } catch (Pql2CompilationException e) {
        throw e;
    } catch (Exception e) {
        throw new Pql2CompilationException(e.getMessage());
    }
}
Also used : TokenStream(org.antlr.v4.runtime.TokenStream) UnbufferedTokenStream(org.antlr.v4.runtime.UnbufferedTokenStream) CommonTokenFactory(org.antlr.v4.runtime.CommonTokenFactory) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) UnbufferedTokenStream(org.antlr.v4.runtime.UnbufferedTokenStream) CharStream(org.antlr.v4.runtime.CharStream) BrokerRequest(com.linkedin.pinot.common.request.BrokerRequest) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) AstNode(com.linkedin.pinot.pql.parsers.pql2.ast.AstNode)

Example 4 with ASTNode

use of org.mvel2.ast.ASTNode in project drools by kiegroup.

the class ConditionAnalyzer method analyzeNodeAccessor.

private Expression analyzeNodeAccessor(Accessor accessor, ASTNode node) {
    AccessorNode accessorNode;
    if (accessor instanceof DynamicGetAccessor) {
        accessorNode = (AccessorNode) ((DynamicGetAccessor) accessor).getSafeAccessor();
    } else if (accessor instanceof AccessorNode) {
        accessorNode = (AccessorNode) accessor;
    } else if (accessor instanceof CompiledExpression) {
        return analyzeNode(((CompiledExpression) accessor).getFirstNode());
    } else if (accessor instanceof ListCreator) {
        return analyzeListCreation(((ListCreator) accessor));
    } else if (accessor instanceof ArrayCreator) {
        return analyzeArrayCreation(((ArrayCreator) accessor));
    } else {
        throw new RuntimeException("Unknown accessor type: " + accessor);
    }
    if (accessorNode instanceof VariableAccessor) {
        if (isStaticAccessor(accessorNode)) {
            while (accessorNode instanceof VariableAccessor) {
                accessorNode = accessorNode.getNextNode();
            }
        } else {
            return analyzeNodeAccessor(accessorNode, node);
        }
    }
    while (accessorNode instanceof StaticReferenceAccessor) {
        StaticReferenceAccessor staticReferenceAccessor = ((StaticReferenceAccessor) accessorNode);
        Object literal = staticReferenceAccessor.getLiteral();
        accessorNode = accessorNode.getNextNode();
        if (accessorNode == null) {
            return new FixedExpression(literal.getClass(), literal);
        }
    }
    return analyzeExpressionNode(accessorNode, node, null);
}
Also used : AccessorNode(org.mvel2.compiler.AccessorNode) DynamicGetAccessor(org.mvel2.optimizers.dynamic.DynamicGetAccessor) IndexedVariableAccessor(org.mvel2.optimizers.impl.refl.nodes.IndexedVariableAccessor) VariableAccessor(org.mvel2.optimizers.impl.refl.nodes.VariableAccessor) ArrayCreator(org.mvel2.optimizers.impl.refl.collection.ArrayCreator) CompiledExpression(org.mvel2.compiler.CompiledExpression) ListCreator(org.mvel2.optimizers.impl.refl.collection.ListCreator) StaticReferenceAccessor(org.mvel2.optimizers.impl.refl.nodes.StaticReferenceAccessor)

Example 5 with ASTNode

use of org.mvel2.ast.ASTNode in project drools by kiegroup.

the class ConditionAnalyzer method analyzeCondition.

private Condition analyzeCondition(ASTNode node) {
    boolean isNegated = false;
    if (node instanceof Negation) {
        isNegated = true;
        ExecutableStatement statement = ((Negation) node).getStatement();
        if (statement instanceof ExecutableLiteral) {
            return new FixedValueCondition(!(Boolean) ((ExecutableLiteral) statement).getLiteral());
        }
        node = ((ExecutableAccessor) statement).getNode();
    }
    node = analyzeSubstatement(node);
    if (node instanceof LiteralNode && node.getEgressType() == Boolean.class) {
        boolean literalValue = (Boolean) node.getLiteralValue();
        return new FixedValueCondition(isNegated ? !literalValue : literalValue);
    }
    if (node instanceof Negation) {
        isNegated = !isNegated;
        node = ((ExecutableAccessor) ((Negation) node).getStatement()).getNode();
        node = analyzeSubstatement(node);
    }
    if (node instanceof And || node instanceof Or) {
        return analyzeCombinedCondition((BooleanNode) node, isNegated);
    }
    return analyzeSingleCondition(node, isNegated);
}
Also used : LiteralNode(org.mvel2.ast.LiteralNode) ExecutableStatement(org.mvel2.compiler.ExecutableStatement) Or(org.mvel2.ast.Or) Negation(org.mvel2.ast.Negation) And(org.mvel2.ast.And) ExecutableLiteral(org.mvel2.compiler.ExecutableLiteral)

Aggregations

ASTNode (org.mvel2.ast.ASTNode)37 CompileException (org.mvel2.CompileException)12 LiteralNode (org.mvel2.ast.LiteralNode)11 CompiledExpression (org.mvel2.compiler.CompiledExpression)8 Interceptor (org.mvel2.integration.Interceptor)8 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)8 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)8 HashMap (java.util.HashMap)7 BinaryOperation (org.mvel2.ast.BinaryOperation)7 WithNode (org.mvel2.ast.WithNode)7 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)7 ParserContext (org.mvel2.ParserContext)6 Substatement (org.mvel2.ast.Substatement)6 ExecutableLiteral (org.mvel2.compiler.ExecutableLiteral)6 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)6 AstNode (com.linkedin.pinot.pql.parsers.pql2.ast.AstNode)5 OperatorNode (org.mvel2.ast.OperatorNode)5 Union (org.mvel2.ast.Union)5 ExecutableAccessor (org.mvel2.compiler.ExecutableAccessor)5 MethodAccessor (org.mvel2.optimizers.impl.refl.nodes.MethodAccessor)5