Search in sources :

Example 36 with Expression

use of org.apache.asterix.lang.common.base.Expression in project asterixdb by apache.

the class OperatorExpressionVisitor method visit.

@Override
public Expression visit(OperatorExpr operatorExpr, ILangExpression arg) throws CompilationException {
    List<Expression> newExprList = new ArrayList<>();
    for (Expression expr : operatorExpr.getExprList()) {
        newExprList.add(expr.accept(this, operatorExpr));
    }
    operatorExpr.setExprList(newExprList);
    OperatorType opType = operatorExpr.getOpList().get(0);
    switch(opType) {
        // There can only be one LIKE/NOT_LIKE/IN/NOT_IN in an operator expression (according to the grammar).
        case LIKE:
        case NOT_LIKE:
            return processLikeOperator(operatorExpr, opType);
        case IN:
        case NOT_IN:
            return processInOperator(operatorExpr, opType);
        case CONCAT:
            // There can be multiple "||"s in one operator expression (according to the grammar).
            return processConcatOperator(operatorExpr);
        case BETWEEN:
        case NOT_BETWEEN:
            return processBetweenOperator(operatorExpr, opType);
        default:
            break;
    }
    return operatorExpr;
}
Also used : Expression(org.apache.asterix.lang.common.base.Expression) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) ArrayList(java.util.ArrayList) OperatorType(org.apache.asterix.lang.common.struct.OperatorType)

Example 37 with Expression

use of org.apache.asterix.lang.common.base.Expression in project asterixdb by apache.

the class SqlppQueryRewriter method inlineDeclaredUdfs.

protected void inlineDeclaredUdfs() throws CompilationException {
    List<FunctionSignature> funIds = new ArrayList<FunctionSignature>();
    for (FunctionDecl fdecl : declaredFunctions) {
        funIds.add(fdecl.getSignature());
    }
    List<FunctionDecl> usedStoredFunctionDecls = new ArrayList<>();
    for (Expression topLevelExpr : topExpr.getDirectlyEnclosedExpressions()) {
        usedStoredFunctionDecls.addAll(FunctionUtil.retrieveUsedStoredFunctions(metadataProvider, topLevelExpr, funIds, null, expr -> getFunctionCalls(expr), func -> functionRepository.getFunctionDecl(func), signature -> FunctionMapUtil.normalizeBuiltinFunctionSignature(signature, false)));
    }
    declaredFunctions.addAll(usedStoredFunctionDecls);
    if (!declaredFunctions.isEmpty()) {
        SqlppInlineUdfsVisitor visitor = new SqlppInlineUdfsVisitor(context, new SqlppFunctionBodyRewriterFactory(), /* the rewriter for function bodies expressions*/
        declaredFunctions, metadataProvider);
        while (topExpr.accept(visitor, declaredFunctions)) {
        // loop until no more changes
        }
    }
    declaredFunctions.removeAll(usedStoredFunctionDecls);
}
Also used : UnnestClause(org.apache.asterix.lang.sqlpp.clause.UnnestClause) OperatorExpressionVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.OperatorExpressionVisitor) JoinClause(org.apache.asterix.lang.sqlpp.clause.JoinClause) FromTerm(org.apache.asterix.lang.sqlpp.clause.FromTerm) VariableCheckAndRewriteVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.VariableCheckAndRewriteVisitor) SqlppGlobalAggregationSugarVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SqlppGlobalAggregationSugarVisitor) InlineColumnAliasVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.InlineColumnAliasVisitor) SelectElement(org.apache.asterix.lang.sqlpp.clause.SelectElement) SelectRegular(org.apache.asterix.lang.sqlpp.clause.SelectRegular) FunctionParser(org.apache.asterix.lang.sqlpp.parser.FunctionParser) NestClause(org.apache.asterix.lang.sqlpp.clause.NestClause) ArrayList(java.util.ArrayList) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) HavingClause(org.apache.asterix.lang.sqlpp.clause.HavingClause) IReturningStatement(org.apache.asterix.lang.common.base.IReturningStatement) ISqlppVisitor(org.apache.asterix.lang.sqlpp.visitor.base.ISqlppVisitor) FunctionUtil(org.apache.asterix.lang.common.util.FunctionUtil) LetClause(org.apache.asterix.lang.common.clause.LetClause) SubstituteGroupbyExpressionWithVariableVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SubstituteGroupbyExpressionWithVariableVisitor) SqlppListInputFunctionRewriteVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SqlppListInputFunctionRewriteVisitor) SetOperationRight(org.apache.asterix.lang.sqlpp.struct.SetOperationRight) SelectBlock(org.apache.asterix.lang.sqlpp.clause.SelectBlock) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) SelectSetOperation(org.apache.asterix.lang.sqlpp.clause.SelectSetOperation) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl) CompilationException(org.apache.asterix.common.exceptions.CompilationException) SelectClause(org.apache.asterix.lang.sqlpp.clause.SelectClause) SqlppInlineUdfsVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SqlppInlineUdfsVisitor) SqlppGroupByVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SqlppGroupByVisitor) Expression(org.apache.asterix.lang.common.base.Expression) Set(java.util.Set) LangRewritingContext(org.apache.asterix.lang.common.rewrites.LangRewritingContext) Projection(org.apache.asterix.lang.sqlpp.clause.Projection) AbstractBinaryCorrelateClause(org.apache.asterix.lang.sqlpp.clause.AbstractBinaryCorrelateClause) SqlppParserFactory(org.apache.asterix.lang.sqlpp.parser.SqlppParserFactory) IQueryRewriter(org.apache.asterix.lang.common.base.IQueryRewriter) SqlppBuiltinFunctionRewriteVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SqlppBuiltinFunctionRewriteVisitor) GatherFunctionCallsVisitor(org.apache.asterix.lang.common.visitor.GatherFunctionCallsVisitor) List(java.util.List) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) GenerateColumnNameVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.GenerateColumnNameVisitor) FunctionMapUtil(org.apache.asterix.lang.sqlpp.util.FunctionMapUtil) InlineWithExpressionVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.InlineWithExpressionVisitor) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) FromClause(org.apache.asterix.lang.sqlpp.clause.FromClause) IndependentSubquery(org.apache.asterix.lang.sqlpp.expression.IndependentSubquery) SetOperationVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SetOperationVisitor) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) ArrayList(java.util.ArrayList) SqlppInlineUdfsVisitor(org.apache.asterix.lang.sqlpp.rewrites.visitor.SqlppInlineUdfsVisitor) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl)

Example 38 with Expression

use of org.apache.asterix.lang.common.base.Expression in project asterixdb by apache.

the class SqlppInlineUdfsVisitor method extractLetBindingVariableExpressionMappings.

private Map<Expression, Expression> extractLetBindingVariableExpressionMappings(List<LetClause> letClauses) throws CompilationException {
    Map<Expression, Expression> varExprMap = new HashMap<>();
    for (LetClause lc : letClauses) {
        // inline let variables one by one iteratively.
        lc.setBindingExpr(SqlppRewriteUtil.substituteExpression(lc.getBindingExpr(), varExprMap, context));
        varExprMap.put(lc.getVarExpr(), lc.getBindingExpr());
    }
    return varExprMap;
}
Also used : LetClause(org.apache.asterix.lang.common.clause.LetClause) HashMap(java.util.HashMap) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression)

Example 39 with Expression

use of org.apache.asterix.lang.common.base.Expression in project asterixdb by apache.

the class SqlppListInputFunctionRewriteVisitor method visit.

@Override
public Expression visit(CallExpr callExpr, ILangExpression arg) throws CompilationException {
    List<Expression> newExprList = new ArrayList<>();
    for (Expression expr : callExpr.getExprList()) {
        newExprList.add(expr.accept(this, arg));
    }
    callExpr.setExprList(newExprList);
    return FunctionMapUtil.normalizedListInputFunctions(callExpr);
}
Also used : Expression(org.apache.asterix.lang.common.base.Expression) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) ArrayList(java.util.ArrayList)

Example 40 with Expression

use of org.apache.asterix.lang.common.base.Expression in project asterixdb by apache.

the class SubstituteGroupbyExpressionVisitor method visit.

@Override
public Expression visit(SelectBlock selectBlock, ILangExpression arg) throws CompilationException {
    if (selectBlock.hasGroupbyClause()) {
        Map<Expression, Expression> map = new HashMap<>();
        for (GbyVariableExpressionPair gbyKeyPair : selectBlock.getGroupbyClause().getGbyPairList()) {
            Expression gbyKeyExpr = gbyKeyPair.getExpr();
            if (gbyKeyExpr.getKind() != Kind.VARIABLE_EXPRESSION) {
                map.put(gbyKeyExpr, gbyKeyPair.getVar());
            }
        }
        // Creates a substitution visitor.
        SubstituteGroupbyExpressionVisitor visitor = new SubstituteGroupbyExpressionVisitor(context, map);
        // Rewrites LET/HAVING/SELECT clauses.
        if (selectBlock.hasLetClausesAfterGroupby()) {
            for (LetClause letClause : selectBlock.getLetListAfterGroupby()) {
                letClause.accept(this, arg);
            }
        }
        if (selectBlock.hasHavingClause()) {
            selectBlock.getHavingClause().accept(visitor, arg);
        }
        selectBlock.getSelectClause().accept(visitor, arg);
        SelectExpression selectExpression = (SelectExpression) arg;
        // For SET operation queries, the GROUP BY key variables will not substitute ORDER BY nor LIMIT expressions.
        if (!selectExpression.getSelectSetOperation().hasRightInputs()) {
            if (selectExpression.hasOrderby()) {
                selectExpression.getOrderbyClause().accept(visitor, arg);
            }
            if (selectExpression.hasLimit()) {
                selectExpression.getLimitClause().accept(visitor, arg);
            }
        }
    }
    return super.visit(selectBlock, arg);
}
Also used : LetClause(org.apache.asterix.lang.common.clause.LetClause) HashMap(java.util.HashMap) Expression(org.apache.asterix.lang.common.base.Expression) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression)

Aggregations

Expression (org.apache.asterix.lang.common.base.Expression)105 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)75 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)52 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)41 ArrayList (java.util.ArrayList)37 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)36 Pair (org.apache.hyracks.algebricks.common.utils.Pair)35 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)32 CaseExpression (org.apache.asterix.lang.sqlpp.expression.CaseExpression)32 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)22 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)19 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)19 FLWOGRExpression (org.apache.asterix.lang.aql.expression.FLWOGRExpression)17 Mutable (org.apache.commons.lang3.mutable.Mutable)17 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)17 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)16 AggregateFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression)16 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)16 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)16 UnnestingFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression)16