Search in sources :

Example 1 with WhereClause

use of org.apache.asterix.lang.common.clause.WhereClause in project asterixdb by apache.

the class SqlppDeleteRewriteVisitor 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);
    // From clause.
    VariableExpr var = deleteStmt.getVariableExpr();
    FromTerm fromTerm = new FromTerm(callExpression, var, null, null);
    @SuppressWarnings("unchecked") FromClause fromClause = new FromClause(Collections.singletonList(fromTerm));
    // Where clause.
    WhereClause whereClause = null;
    Expression condition = deleteStmt.getCondition();
    if (condition != null) {
        whereClause = new WhereClause(condition);
    }
    // Select clause.
    VariableExpr returnExpr = new VariableExpr(var.getVar());
    returnExpr.setIsNewVar(false);
    SelectElement selectElement = new SelectElement(returnExpr);
    SelectClause selectClause = new SelectClause(selectElement, null, false);
    // Construct the select expression.
    SelectBlock selectBlock = new SelectBlock(selectClause, fromClause, null, whereClause, null, null, null);
    SelectSetOperation selectSetOperation = new SelectSetOperation(new SetOperationInput(selectBlock, null), null);
    SelectExpression selectExpression = new SelectExpression(null, selectSetOperation, null, null, false);
    Query query = new Query(false, false, selectExpression, 0);
    query.setBody(selectExpression);
    // return the delete statement.
    deleteStmt.setQuery(query);
    return null;
}
Also used : SelectElement(org.apache.asterix.lang.sqlpp.clause.SelectElement) SelectClause(org.apache.asterix.lang.sqlpp.clause.SelectClause) Query(org.apache.asterix.lang.common.statement.Query) ArrayList(java.util.ArrayList) WhereClause(org.apache.asterix.lang.common.clause.WhereClause) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) Identifier(org.apache.asterix.lang.common.struct.Identifier) StringLiteral(org.apache.asterix.lang.common.literal.StringLiteral) SelectBlock(org.apache.asterix.lang.sqlpp.clause.SelectBlock) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) FromClause(org.apache.asterix.lang.sqlpp.clause.FromClause) SetOperationInput(org.apache.asterix.lang.sqlpp.struct.SetOperationInput) SelectSetOperation(org.apache.asterix.lang.sqlpp.clause.SelectSetOperation) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) CallExpr(org.apache.asterix.lang.common.expression.CallExpr) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) FromTerm(org.apache.asterix.lang.sqlpp.clause.FromTerm)

Example 2 with WhereClause

use of org.apache.asterix.lang.common.clause.WhereClause in project asterixdb by apache.

the class CloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(WhereClause wc, VariableSubstitutionEnvironment env) throws CompilationException {
    Pair<ILangExpression, VariableSubstitutionEnvironment> p1 = wc.getWhereExpr().accept(this, env);
    WhereClause newW = new WhereClause((Expression) p1.first);
    return new Pair<>(newW, p1.second);
}
Also used : VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) WhereClause(org.apache.asterix.lang.common.clause.WhereClause) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair) QuantifiedPair(org.apache.asterix.lang.common.struct.QuantifiedPair)

Example 3 with WhereClause

use of org.apache.asterix.lang.common.clause.WhereClause 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 4 with WhereClause

use of org.apache.asterix.lang.common.clause.WhereClause 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 5 with WhereClause

use of org.apache.asterix.lang.common.clause.WhereClause in project asterixdb by apache.

the class SqlppCloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(SelectBlock selectBlock, VariableSubstitutionEnvironment env) throws CompilationException {
    Pair<ILangExpression, VariableSubstitutionEnvironment> newFrom = null;
    Pair<ILangExpression, VariableSubstitutionEnvironment> newLet;
    Pair<ILangExpression, VariableSubstitutionEnvironment> newWhere = null;
    Pair<ILangExpression, VariableSubstitutionEnvironment> newGroupby = null;
    Pair<ILangExpression, VariableSubstitutionEnvironment> newHaving = null;
    Pair<ILangExpression, VariableSubstitutionEnvironment> newSelect;
    List<LetClause> newLetClauses = new ArrayList<>();
    List<LetClause> newLetClausesAfterGby = new ArrayList<>();
    VariableSubstitutionEnvironment currentEnv = new VariableSubstitutionEnvironment(env);
    if (selectBlock.hasFromClause()) {
        newFrom = selectBlock.getFromClause().accept(this, currentEnv);
        currentEnv = newFrom.second;
    }
    if (selectBlock.hasLetClauses()) {
        for (LetClause letClause : selectBlock.getLetList()) {
            newLet = letClause.accept(this, currentEnv);
            currentEnv = newLet.second;
            newLetClauses.add(letClause);
        }
    }
    if (selectBlock.hasWhereClause()) {
        newWhere = selectBlock.getWhereClause().accept(this, currentEnv);
        currentEnv = newWhere.second;
    }
    if (selectBlock.hasGroupbyClause()) {
        newGroupby = selectBlock.getGroupbyClause().accept(this, currentEnv);
        currentEnv = newGroupby.second;
        if (selectBlock.hasLetClausesAfterGroupby()) {
            for (LetClause letClauseAfterGby : selectBlock.getLetListAfterGroupby()) {
                newLet = letClauseAfterGby.accept(this, currentEnv);
                currentEnv = newLet.second;
                newLetClausesAfterGby.add(letClauseAfterGby);
            }
        }
    }
    if (selectBlock.hasHavingClause()) {
        newHaving = selectBlock.getHavingClause().accept(this, currentEnv);
        currentEnv = newHaving.second;
    }
    newSelect = selectBlock.getSelectClause().accept(this, currentEnv);
    currentEnv = newSelect.second;
    FromClause fromClause = newFrom == null ? null : (FromClause) newFrom.first;
    WhereClause whereClause = newWhere == null ? null : (WhereClause) newWhere.first;
    GroupbyClause groupbyClause = newGroupby == null ? null : (GroupbyClause) newGroupby.first;
    HavingClause havingClause = newHaving == null ? null : (HavingClause) newHaving.first;
    return new Pair<>(new SelectBlock((SelectClause) newSelect.first, fromClause, newLetClauses, whereClause, groupbyClause, newLetClausesAfterGby, havingClause), currentEnv);
}
Also used : SelectClause(org.apache.asterix.lang.sqlpp.clause.SelectClause) LetClause(org.apache.asterix.lang.common.clause.LetClause) ArrayList(java.util.ArrayList) WhereClause(org.apache.asterix.lang.common.clause.WhereClause) VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) SelectBlock(org.apache.asterix.lang.sqlpp.clause.SelectBlock) FromClause(org.apache.asterix.lang.sqlpp.clause.FromClause) HavingClause(org.apache.asterix.lang.sqlpp.clause.HavingClause) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Aggregations

WhereClause (org.apache.asterix.lang.common.clause.WhereClause)6 ArrayList (java.util.ArrayList)5 Expression (org.apache.asterix.lang.common.base.Expression)3 GroupbyClause (org.apache.asterix.lang.common.clause.GroupbyClause)3 LetClause (org.apache.asterix.lang.common.clause.LetClause)3 FromClause (org.apache.asterix.lang.sqlpp.clause.FromClause)3 SelectBlock (org.apache.asterix.lang.sqlpp.clause.SelectBlock)3 SelectClause (org.apache.asterix.lang.sqlpp.clause.SelectClause)3 FunctionSignature (org.apache.asterix.common.functions.FunctionSignature)2 ForClause (org.apache.asterix.lang.aql.clause.ForClause)2 FLWOGRExpression (org.apache.asterix.lang.aql.expression.FLWOGRExpression)2 Clause (org.apache.asterix.lang.common.base.Clause)2 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)2 CallExpr (org.apache.asterix.lang.common.expression.CallExpr)2 LiteralExpr (org.apache.asterix.lang.common.expression.LiteralExpr)2 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)2 StringLiteral (org.apache.asterix.lang.common.literal.StringLiteral)2 VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)2 Query (org.apache.asterix.lang.common.statement.Query)2 Identifier (org.apache.asterix.lang.common.struct.Identifier)2