Search in sources :

Example 71 with Expression

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

the class ClauseComparator method mergeConsecutiveWhereClauses.

// Merge consecutive "where" clauses.
private void mergeConsecutiveWhereClauses(List<Clause> clauses) throws CompilationException {
    List<Clause> results = new ArrayList<Clause>();
    int size = clauses.size();
    for (int index = 0; index < size; ) {
        Clause clause = clauses.get(index);
        if (clause.getClauseType() != ClauseType.WHERE_CLAUSE) {
            results.add(clause);
            ++index;
        } else {
            List<Expression> expressions = new ArrayList<Expression>();
            Clause firstWhereClause = clause;
            do {
                WhereClause whereClause = (WhereClause) clause;
                expressions.add(whereClause.getWhereExpr());
                if (++index >= size) {
                    break;
                }
                clause = clauses.get(index);
            } while (clause.getClauseType() == ClauseType.WHERE_CLAUSE);
            if (expressions.size() > 1) {
                OperatorExpr newWhereExpr = new OperatorExpr();
                newWhereExpr.setExprList(expressions);
                newWhereExpr.setCurrentop(true);
                for (int operatorIndex = 0; operatorIndex < expressions.size(); ++operatorIndex) {
                    newWhereExpr.addOperator("and");
                }
                results.add(new WhereClause(newWhereExpr));
            } else {
                results.add(firstWhereClause);
            }
        }
    }
    clauses.clear();
    clauses.addAll(results);
}
Also used : OperatorExpr(org.apache.asterix.lang.common.expression.OperatorExpr) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) Expression(org.apache.asterix.lang.common.base.Expression) ArrayList(java.util.ArrayList) WhereClause(org.apache.asterix.lang.common.clause.WhereClause) ForClause(org.apache.asterix.lang.aql.clause.ForClause) DistinctClause(org.apache.asterix.lang.aql.clause.DistinctClause) GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) LetClause(org.apache.asterix.lang.common.clause.LetClause) Clause(org.apache.asterix.lang.common.base.Clause) WhereClause(org.apache.asterix.lang.common.clause.WhereClause)

Example 72 with Expression

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

the class ClauseComparator method extractDefinedCollectionVariables.

// Extracts the variables to be substituted with a path access.
private Map<VariableExpr, Expression> extractDefinedCollectionVariables(List<Clause> clauses, GroupbyClause cuttingGbyClause, String generatedAlias) {
    Map<VariableExpr, Expression> varExprMap = new HashMap<VariableExpr, Expression>();
    List<VariableExpr> varToSubstitute = collectProducedVariablesFromGroupby(cuttingGbyClause);
    int gbyIndex = clauses.indexOf(cuttingGbyClause);
    for (int i = gbyIndex + 1; i < clauses.size(); i++) {
        Clause cl = clauses.get(i);
        if (cl.getClauseType() == ClauseType.LET_CLAUSE) {
            varToSubstitute.add(((LetClause) cl).getVarExpr());
        }
    }
    for (VariableExpr var : varToSubstitute) {
        varExprMap.put(var, new FieldAccessor(new VariableExpr(new VarIdentifier(generatedAlias)), new VarIdentifier(var.getVar().getValue().substring(1))));
    }
    return varExprMap;
}
Also used : HashMap(java.util.HashMap) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) Expression(org.apache.asterix.lang.common.base.Expression) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ForClause(org.apache.asterix.lang.aql.clause.ForClause) DistinctClause(org.apache.asterix.lang.aql.clause.DistinctClause) GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) LetClause(org.apache.asterix.lang.common.clause.LetClause) Clause(org.apache.asterix.lang.common.base.Clause) WhereClause(org.apache.asterix.lang.common.clause.WhereClause) FieldAccessor(org.apache.asterix.lang.common.expression.FieldAccessor)

Example 73 with Expression

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

the class AqlDeleteRewriteVisitor method visit.

@Override
public Void visit(DeleteStatement deleteStmt, Void visitArg) {
    List<Expression> arguments = new ArrayList<>();
    Identifier dataverseName = deleteStmt.getDataverseName();
    Identifier datasetName = deleteStmt.getDatasetName();
    String arg = dataverseName == null ? datasetName.getValue() : dataverseName.getValue() + "." + datasetName.getValue();
    LiteralExpr argumentLiteral = new LiteralExpr(new StringLiteral(arg));
    arguments.add(argumentLiteral);
    CallExpr callExpression = new CallExpr(new FunctionSignature(FunctionConstants.ASTERIX_NS, "dataset", 1), arguments);
    List<Clause> clauseList = new ArrayList<>();
    VariableExpr var = deleteStmt.getVariableExpr();
    Clause forClause = new ForClause(var, callExpression);
    clauseList.add(forClause);
    Clause whereClause = null;
    Expression condition = deleteStmt.getCondition();
    if (condition != null) {
        whereClause = new WhereClause(condition);
        clauseList.add(whereClause);
    }
    VariableExpr returnExpr = new VariableExpr(var.getVar());
    returnExpr.setIsNewVar(false);
    FLWOGRExpression flowgr = new FLWOGRExpression(clauseList, returnExpr);
    Query query = new Query(false);
    query.setBody(flowgr);
    deleteStmt.setQuery(query);
    return null;
}
Also used : Query(org.apache.asterix.lang.common.statement.Query) ArrayList(java.util.ArrayList) WhereClause(org.apache.asterix.lang.common.clause.WhereClause) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) ForClause(org.apache.asterix.lang.aql.clause.ForClause) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) Identifier(org.apache.asterix.lang.common.struct.Identifier) StringLiteral(org.apache.asterix.lang.common.literal.StringLiteral) Expression(org.apache.asterix.lang.common.base.Expression) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) CallExpr(org.apache.asterix.lang.common.expression.CallExpr) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ForClause(org.apache.asterix.lang.aql.clause.ForClause) Clause(org.apache.asterix.lang.common.base.Clause) WhereClause(org.apache.asterix.lang.common.clause.WhereClause)

Example 74 with Expression

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

the class AqlQueryRewriter method inlineDeclaredUdfs.

private void inlineDeclaredUdfs() throws CompilationException {
    if (topStatement == null) {
        return;
    }
    List<FunctionSignature> funIds = new ArrayList<FunctionSignature>();
    for (FunctionDecl fdecl : declaredFunctions) {
        funIds.add(fdecl.getSignature());
    }
    List<FunctionDecl> storedFunctionDecls = new ArrayList<>();
    for (Expression topLevelExpr : topStatement.getDirectlyEnclosedExpressions()) {
        storedFunctionDecls.addAll(FunctionUtil.retrieveUsedStoredFunctions(metadataProvider, topLevelExpr, funIds, null, expr -> getFunctionCalls(expr), func -> functionParser.getFunctionDecl(func), signature -> CommonFunctionMapUtil.normalizeBuiltinFunctionSignature(signature)));
        declaredFunctions.addAll(storedFunctionDecls);
    }
    if (!declaredFunctions.isEmpty()) {
        AQLInlineUdfsVisitor visitor = new AQLInlineUdfsVisitor(context, new AQLRewriterFactory(), declaredFunctions, metadataProvider);
        while (topStatement.accept(visitor, declaredFunctions)) {
        // loop until no more changes
        }
    }
    declaredFunctions.removeAll(storedFunctionDecls);
}
Also used : IAQLVisitor(org.apache.asterix.lang.aql.visitor.base.IAQLVisitor) ForClause(org.apache.asterix.lang.aql.clause.ForClause) DistinctClause(org.apache.asterix.lang.aql.clause.DistinctClause) ArrayList(java.util.ArrayList) GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) IReturningStatement(org.apache.asterix.lang.common.base.IReturningStatement) FunctionUtil(org.apache.asterix.lang.common.util.FunctionUtil) LetClause(org.apache.asterix.lang.common.clause.LetClause) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) Kind(org.apache.asterix.lang.common.base.Expression.Kind) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl) CompilationException(org.apache.asterix.common.exceptions.CompilationException) AQLParserFactory(org.apache.asterix.lang.aql.parser.AQLParserFactory) Clause(org.apache.asterix.lang.common.base.Clause) Expression(org.apache.asterix.lang.common.base.Expression) Set(java.util.Set) LangRewritingContext(org.apache.asterix.lang.common.rewrites.LangRewritingContext) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) CommonFunctionMapUtil(org.apache.asterix.lang.common.util.CommonFunctionMapUtil) IQueryRewriter(org.apache.asterix.lang.common.base.IQueryRewriter) GatherFunctionCallsVisitor(org.apache.asterix.lang.common.visitor.GatherFunctionCallsVisitor) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) List(java.util.List) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) AqlBuiltinFunctionRewriteVisitor(org.apache.asterix.lang.aql.rewrites.visitor.AqlBuiltinFunctionRewriteVisitor) AQLInlineUdfsVisitor(org.apache.asterix.lang.aql.visitor.AQLInlineUdfsVisitor) UnionExpr(org.apache.asterix.lang.aql.expression.UnionExpr) FunctionParser(org.apache.asterix.lang.aql.parser.FunctionParser) AQLInlineUdfsVisitor(org.apache.asterix.lang.aql.visitor.AQLInlineUdfsVisitor) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) Expression(org.apache.asterix.lang.common.base.Expression) ArrayList(java.util.ArrayList) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl)

Example 75 with Expression

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

the class RangeMapBuilder method parseHint.

public static IRangeMap parseHint(Object hint) throws CompilationException {
    ArrayBackedValueStorage abvs = new ArrayBackedValueStorage();
    DataOutput out = abvs.getDataOutput();
    ;
    abvs.reset();
    IParser parser = parserFactory.createParser((String) hint);
    List<Statement> hintStatements = parser.parse();
    if (hintStatements.size() != 1) {
        throw new CompilationException("Only one range statement is allowed for the range hint.");
    }
    // Translate the query into a Range Map
    if (hintStatements.get(0).getKind() != Statement.Kind.QUERY) {
        throw new CompilationException("Not a proper query for the range hint.");
    }
    Query q = (Query) hintStatements.get(0);
    if (q.getBody().getKind() != Kind.LIST_CONSTRUCTOR_EXPRESSION) {
        throw new CompilationException("The range hint must be a list.");
    }
    List<Expression> el = ((ListConstructor) q.getBody()).getExprList();
    int[] offsets = new int[el.size()];
    // Loop over list of literals
    for (int i = 0; i < el.size(); ++i) {
        Expression item = el.get(i);
        if (item.getKind() == Kind.LITERAL_EXPRESSION) {
            parseLiteralToBytes(item, out);
            offsets[i] = abvs.getLength();
        }
    // TODO Add support for composite fields.
    }
    return new RangeMap(1, abvs.getByteArray(), offsets);
}
Also used : DataOutput(java.io.DataOutput) CompilationException(org.apache.asterix.common.exceptions.CompilationException) Query(org.apache.asterix.lang.common.statement.Query) Statement(org.apache.asterix.lang.common.base.Statement) ListConstructor(org.apache.asterix.lang.common.expression.ListConstructor) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IRangeMap(org.apache.hyracks.dataflow.common.data.partition.range.IRangeMap) RangeMap(org.apache.hyracks.dataflow.common.data.partition.range.RangeMap) Expression(org.apache.asterix.lang.common.base.Expression) IParser(org.apache.asterix.lang.common.base.IParser)

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