Search in sources :

Example 11 with AstNode

use of com.linkedin.pinot.pql.parsers.pql2.ast.AstNode in project drools by kiegroup.

the class MvelConditionEvaluator method evaluateIfNecessary.

private void evaluateIfNecessary(InternalFactHandle handle, InternalWorkingMemory workingMemory, Tuple tuple, ASTNode node) {
    if (!isEvaluated(node)) {
        ASTNode next = node.nextASTNode;
        node.nextASTNode = null;
        evaluate(asCompiledExpression(node), handle, workingMemory, tuple);
        node.nextASTNode = next;
    }
}
Also used : ASTNode(org.mvel2.ast.ASTNode)

Example 12 with AstNode

use of com.linkedin.pinot.pql.parsers.pql2.ast.AstNode in project mvel by mikebrock.

the class MVELInterpretedRuntime method parseAndExecuteInterpreted.

/**
 * Main interpreter loop.
 *
 * @return value
 */
private Object parseAndExecuteInterpreted() {
    ASTNode tk = null;
    int operator;
    lastWasIdentifier = false;
    try {
        while ((tk = nextToken()) != null) {
            holdOverRegister = null;
            if (lastWasIdentifier && lastNode.isDiscard()) {
                stk.discard();
            }
            /**
             * If we are at the beginning of a statement, then we immediately push the first token
             * onto the stack.
             */
            if (stk.isEmpty()) {
                stk.push(tk.getReducedValue(ctx, ctx, variableFactory));
                /**
                 * If this is a substatement, we need to move the result into the d-stack to preserve
                 * proper execution order.
                 */
                if (tk instanceof Substatement && (tk = nextToken()) != null) {
                    if (isArithmeticOperator(operator = tk.getOperator())) {
                        stk.push(nextToken().getReducedValue(ctx, ctx, variableFactory), operator);
                        if (procBooleanOperator(arithmeticFunctionReduction(operator)) == -1)
                            return stk.peek();
                        else
                            continue;
                    }
                } else {
                    continue;
                }
            }
            if (variableFactory.tiltFlag()) {
                return stk.pop();
            }
            switch(procBooleanOperator(operator = tk.getOperator())) {
                case RETURN:
                    variableFactory.setTiltFlag(true);
                    return stk.pop();
                case OP_TERMINATE:
                    return stk.peek();
                case OP_RESET_FRAME:
                    continue;
                case OP_OVERFLOW:
                    if (!tk.isOperator()) {
                        if (!(stk.peek() instanceof Class)) {
                            throw new CompileException("unexpected token or unknown identifier:" + tk.getName(), expr, st);
                        }
                        variableFactory.createVariable(tk.getName(), null, (Class) stk.peek());
                    }
                    continue;
            }
            stk.push(nextToken().getReducedValue(ctx, ctx, variableFactory), operator);
            switch((operator = arithmeticFunctionReduction(operator))) {
                case OP_TERMINATE:
                    return stk.peek();
                case OP_RESET_FRAME:
                    continue;
            }
            if (procBooleanOperator(operator) == OP_TERMINATE)
                return stk.peek();
        }
        if (holdOverRegister != null) {
            return holdOverRegister;
        }
    } catch (CompileException e) {
        throw ErrorUtil.rewriteIfNeeded(e, expr, start);
    } catch (NullPointerException e) {
        if (tk != null && tk.isOperator()) {
            CompileException ce = new CompileException("incomplete statement: " + tk.getName() + " (possible use of reserved keyword as identifier: " + tk.getName() + ")", expr, st, e);
            ce.setExpr(expr);
            ce.setLineNumber(line);
            ce.setCursor(cursor);
            throw ce;
        } else {
            throw e;
        }
    }
    return stk.peek();
}
Also used : ASTNode(org.mvel2.ast.ASTNode) Substatement(org.mvel2.ast.Substatement)

Example 13 with AstNode

use of com.linkedin.pinot.pql.parsers.pql2.ast.AstNode in project mvel by mikebrock.

the class MVELRuntime method execute.

/**
 * Main interpreter.
 *
 * @param debugger        Run in debug mode
 * @param expression      The compiled expression object
 * @param ctx             The root context object
 * @param variableFactory The variable factory to be injected
 * @return The resultant value
 * @see org.mvel2.MVEL
 */
public static Object execute(boolean debugger, final CompiledExpression expression, final Object ctx, VariableResolverFactory variableFactory) {
    Object v1, v2;
    ExecutionStack stk = new ExecutionStack();
    variableFactory.setTiltFlag(false);
    ASTNode tk = expression.getFirstNode();
    Integer operator;
    if (tk == null)
        return null;
    try {
        do {
            if (tk.fields == -1) {
                /**
                 * This may seem silly and redundant, however, when an MVEL script recurses into a block
                 * or substatement, a new runtime loop is entered.   Since the debugger state is not
                 * passed through the AST, it is not possible to forward the state directly.  So when we
                 * encounter a debugging symbol, we check the thread local to see if there is are registered
                 * breakpoints.  If we find them, we assume that we are debugging.
                 *
                 * The consequence of this of course, is that it's not ideal to compileShared expressions with
                 * debugging symbols which you plan to use in a production enviroment.
                 */
                if (debugger || (debugger = hasDebuggerContext())) {
                    try {
                        debuggerContext.get().checkBreak((LineLabel) tk, variableFactory, expression);
                    } catch (NullPointerException e) {
                    // do nothing for now.  this isn't as calus as it seems.
                    }
                }
                continue;
            } else if (stk.isEmpty()) {
                stk.push(tk.getReducedValueAccelerated(ctx, ctx, variableFactory));
            }
            if (variableFactory.tiltFlag()) {
                return stk.pop();
            }
            switch(operator = tk.getOperator()) {
                case RETURN:
                    variableFactory.setTiltFlag(true);
                    return stk.pop();
                case NOOP:
                    continue;
                case TERNARY:
                    if (!stk.popBoolean()) {
                        // noinspection StatementWithEmptyBody
                        while (tk.nextASTNode != null && !(tk = tk.nextASTNode).isOperator(TERNARY_ELSE)) ;
                    }
                    stk.clear();
                    continue;
                case TERNARY_ELSE:
                    return stk.pop();
                case END_OF_STMT:
                    /**
                     * If the program doesn't end here then we wipe anything off the stack that remains.
                     * Althought it may seem like intuitive stack optimizations could be leveraged by
                     * leaving hanging values on the stack,  trust me it's not a good idea.
                     */
                    if (tk.nextASTNode != null) {
                        stk.clear();
                    }
                    continue;
            }
            stk.push(tk.nextASTNode.getReducedValueAccelerated(ctx, ctx, variableFactory), operator);
            try {
                while (stk.isReduceable()) {
                    if ((Integer) stk.peek() == CHOR) {
                        stk.pop();
                        v1 = stk.pop();
                        v2 = stk.pop();
                        if (!isEmpty(v2) || !isEmpty(v1)) {
                            stk.clear();
                            stk.push(!isEmpty(v2) ? v2 : v1);
                        } else
                            stk.push(null);
                    } else {
                        stk.op();
                    }
                }
            } catch (ClassCastException e) {
                throw new CompileException("syntax error or incomptable types", new char[0], 0, e);
            } catch (CompileException e) {
                throw e;
            } catch (Exception e) {
                throw new CompileException("failed to compileShared sub expression", new char[0], 0, e);
            }
        } while ((tk = tk.nextASTNode) != null);
        return stk.peek();
    } catch (NullPointerException e) {
        if (tk != null && tk.isOperator() && tk.nextASTNode != null) {
            throw new CompileException("incomplete statement: " + tk.getName() + " (possible use of reserved keyword as identifier: " + tk.getName() + ")", tk.getExpr(), tk.getStart());
        } else {
            throw e;
        }
    }
}
Also used : ExecutionStack(org.mvel2.util.ExecutionStack) ASTNode(org.mvel2.ast.ASTNode)

Example 14 with AstNode

use of com.linkedin.pinot.pql.parsers.pql2.ast.AstNode in project pinot by linkedin.

the class Pql2AstListener method popNode.

private void popNode() {
    AstNode topNode = _nodeStack.pop();
    topNode.doneProcessingChildren();
}
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 15 with AstNode

use of com.linkedin.pinot.pql.parsers.pql2.ast.AstNode in project pinot by linkedin.

the class Pql2Compiler method compileToExpressionTree.

@Override
public TransformExpressionTree compileToExpressionTree(String expression) {
    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.expression();
    ParseTreeWalker walker = new ParseTreeWalker();
    Pql2AstListener listener = new Pql2AstListener(expression);
    walker.walk(listener, parseTree);
    final AstNode rootNode = listener.getRootNode();
    return TransformExpressionTree.buildTree(rootNode);
}
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) 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)

Aggregations

ASTNode (org.mvel2.ast.ASTNode)14 AstNode (com.linkedin.pinot.pql.parsers.pql2.ast.AstNode)5 Interceptor (org.mvel2.integration.Interceptor)4 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)4 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)4 FunctionCallAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.FunctionCallAstNode)3 IdentifierAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.IdentifierAstNode)3 HashMap (java.util.HashMap)3 BetweenPredicateAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.BetweenPredicateAstNode)2 BinaryMathOpAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.BinaryMathOpAstNode)2 BooleanOperatorAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.BooleanOperatorAstNode)2 ComparisonPredicateAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.ComparisonPredicateAstNode)2 ExpressionParenthesisGroupAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.ExpressionParenthesisGroupAstNode)2 FloatingPointLiteralAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.FloatingPointLiteralAstNode)2 GroupByAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.GroupByAstNode)2 HavingAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.HavingAstNode)2 InPredicateAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.InPredicateAstNode)2 IntegerLiteralAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.IntegerLiteralAstNode)2 IsPredicateAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.IsPredicateAstNode)2 LimitAstNode (com.linkedin.pinot.pql.parsers.pql2.ast.LimitAstNode)2