Search in sources :

Example 1 with SelectSetOperation

use of org.apache.asterix.lang.sqlpp.clause.SelectSetOperation in project asterixdb by apache.

the class SqlppGroupBySugarVisitor method wrapAggregationArgument.

private Expression wrapAggregationArgument(Expression argExpr) throws CompilationException {
    Expression expr = argExpr;
    Set<VariableExpr> freeVars = SqlppRewriteUtil.getFreeVariable(expr);
    VariableExpr fromBindingVar = new VariableExpr(context.newVariable());
    FromTerm fromTerm = new FromTerm(groupVar, fromBindingVar, null, null);
    FromClause fromClause = new FromClause(Collections.singletonList(fromTerm));
    // Maps field variable expressions to field accesses.
    Map<Expression, Expression> varExprMap = new HashMap<>();
    for (VariableExpr usedVar : freeVars) {
        // Reference to a field in the group variable.
        if (fieldVars.contains(usedVar)) {
            // Rewrites to a reference to a field in the group variable.
            varExprMap.put(usedVar, new FieldAccessor(fromBindingVar, SqlppVariableUtil.toUserDefinedVariableName(usedVar.getVar())));
        }
    }
    // Select clause.
    SelectElement selectElement = new SelectElement(SqlppRewriteUtil.substituteExpression(expr, varExprMap, context));
    SelectClause selectClause = new SelectClause(selectElement, null, false);
    // Construct the select expression.
    SelectBlock selectBlock = new SelectBlock(selectClause, fromClause, null, null, null, null, null);
    SelectSetOperation selectSetOperation = new SelectSetOperation(new SetOperationInput(selectBlock, null), null);
    return new SelectExpression(null, selectSetOperation, null, null, true);
}
Also used : SelectElement(org.apache.asterix.lang.sqlpp.clause.SelectElement) SelectClause(org.apache.asterix.lang.sqlpp.clause.SelectClause) HashMap(java.util.HashMap) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) FieldAccessor(org.apache.asterix.lang.common.expression.FieldAccessor) SelectBlock(org.apache.asterix.lang.sqlpp.clause.SelectBlock) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) 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) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) FromTerm(org.apache.asterix.lang.sqlpp.clause.FromTerm)

Example 2 with SelectSetOperation

use of org.apache.asterix.lang.sqlpp.clause.SelectSetOperation in project asterixdb by apache.

the class SetOperationVisitor method visit.

@Override
public Expression visit(SelectExpression selectExpression, ILangExpression arg) throws CompilationException {
    // Recursively visit nested select expressions.
    SelectSetOperation selectSetOperation = selectExpression.getSelectSetOperation();
    if (!selectSetOperation.hasRightInputs() || !(selectExpression.hasOrderby() || selectExpression.hasLimit())) {
        return super.visit(selectExpression, arg);
    }
    OrderbyClause orderBy = selectExpression.getOrderbyClause();
    LimitClause limit = selectExpression.getLimitClause();
    // Wraps the set operation part with a subquery.
    SelectExpression nestedSelectExpression = new SelectExpression(null, selectSetOperation, null, null, true);
    // Binding variable for the subquery.
    VariableExpr newBindingVar = new VariableExpr(context.newVariable());
    FromTerm newFromTerm = new FromTerm(nestedSelectExpression, newBindingVar, null, null);
    FromClause newFromClause = new FromClause(new ArrayList<>(Collections.singletonList(newFromTerm)));
    SelectClause selectClause = new SelectClause(new SelectElement(newBindingVar), null, false);
    SelectBlock selectBlock = new SelectBlock(selectClause, newFromClause, null, null, null, null, null);
    SelectSetOperation newSelectSetOperation = new SelectSetOperation(new SetOperationInput(selectBlock, null), null);
    // Puts together the generated select-from-where query and order by/limit.
    SelectExpression newSelectExpression = new SelectExpression(selectExpression.getLetList(), newSelectSetOperation, orderBy, limit, selectExpression.isSubquery());
    return super.visit(newSelectExpression, arg);
}
Also used : LimitClause(org.apache.asterix.lang.common.clause.LimitClause) SelectClause(org.apache.asterix.lang.sqlpp.clause.SelectClause) SelectElement(org.apache.asterix.lang.sqlpp.clause.SelectElement) SelectBlock(org.apache.asterix.lang.sqlpp.clause.SelectBlock) FromClause(org.apache.asterix.lang.sqlpp.clause.FromClause) SetOperationInput(org.apache.asterix.lang.sqlpp.struct.SetOperationInput) SelectSetOperation(org.apache.asterix.lang.sqlpp.clause.SelectSetOperation) OrderbyClause(org.apache.asterix.lang.common.clause.OrderbyClause) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) FromTerm(org.apache.asterix.lang.sqlpp.clause.FromTerm)

Example 3 with SelectSetOperation

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

use of org.apache.asterix.lang.sqlpp.clause.SelectSetOperation in project asterixdb by apache.

the class SqlppCloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(SelectSetOperation selectSetOperation, VariableSubstitutionEnvironment env) throws CompilationException {
    SetOperationInput leftInput = selectSetOperation.getLeftInput();
    SetOperationInput newLeftInput;
    Pair<ILangExpression, VariableSubstitutionEnvironment> leftResult;
    // Sets the left input.
    if (leftInput.selectBlock()) {
        leftResult = leftInput.getSelectBlock().accept(this, env);
        newLeftInput = new SetOperationInput((SelectBlock) leftResult.first, null);
    } else {
        leftResult = leftInput.getSubquery().accept(this, env);
        newLeftInput = new SetOperationInput(null, (SelectExpression) leftResult.first);
    }
    // Sets the right input
    List<SetOperationRight> newRightInputs = new ArrayList<>();
    if (selectSetOperation.hasRightInputs()) {
        for (SetOperationRight right : selectSetOperation.getRightInputs()) {
            SetOperationInput newRightInput;
            SetOperationInput rightInput = right.getSetOperationRightInput();
            if (rightInput.selectBlock()) {
                Pair<ILangExpression, VariableSubstitutionEnvironment> rightResult = rightInput.getSelectBlock().accept(this, env);
                newRightInput = new SetOperationInput((SelectBlock) rightResult.first, null);
            } else {
                Pair<ILangExpression, VariableSubstitutionEnvironment> rightResult = rightInput.getSubquery().accept(this, env);
                newRightInput = new SetOperationInput(null, (SelectExpression) rightResult.first);
            }
            newRightInputs.add(new SetOperationRight(right.getSetOpType(), right.isSetSemantics(), newRightInput));
        }
    }
    SelectSetOperation newSelectSetOperation = new SelectSetOperation(newLeftInput, newRightInputs);
    return new Pair<>(newSelectSetOperation, selectSetOperation.hasRightInputs() ? env : leftResult.second);
}
Also used : VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) SelectBlock(org.apache.asterix.lang.sqlpp.clause.SelectBlock) SetOperationInput(org.apache.asterix.lang.sqlpp.struct.SetOperationInput) SelectSetOperation(org.apache.asterix.lang.sqlpp.clause.SelectSetOperation) ArrayList(java.util.ArrayList) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) SetOperationRight(org.apache.asterix.lang.sqlpp.struct.SetOperationRight) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 5 with SelectSetOperation

use of org.apache.asterix.lang.sqlpp.clause.SelectSetOperation in project asterixdb by apache.

the class DeepCopyVisitor method visit.

@Override
public SelectSetOperation visit(SelectSetOperation selectSetOperation, Void arg) throws CompilationException {
    SetOperationInput leftInput = selectSetOperation.getLeftInput();
    SetOperationInput newLeftInput;
    if (leftInput.selectBlock()) {
        newLeftInput = new SetOperationInput((SelectBlock) leftInput.accept(this, arg), null);
    } else {
        newLeftInput = new SetOperationInput(null, (SelectExpression) leftInput.accept(this, arg));
    }
    List<SetOperationRight> rightInputs = new ArrayList<>();
    for (SetOperationRight right : selectSetOperation.getRightInputs()) {
        SetOperationInput newRightInput;
        SetOperationInput setOpRightInput = right.getSetOperationRightInput();
        if (setOpRightInput.selectBlock()) {
            newRightInput = new SetOperationInput((SelectBlock) setOpRightInput.accept(this, arg), null);
        } else {
            newRightInput = new SetOperationInput(null, (SelectExpression) setOpRightInput.accept(this, arg));
        }
        rightInputs.add(new SetOperationRight(right.getSetOpType(), right.isSetSemantics(), newRightInput));
    }
    return new SelectSetOperation(newLeftInput, rightInputs);
}
Also used : SelectBlock(org.apache.asterix.lang.sqlpp.clause.SelectBlock) SetOperationInput(org.apache.asterix.lang.sqlpp.struct.SetOperationInput) SelectSetOperation(org.apache.asterix.lang.sqlpp.clause.SelectSetOperation) ArrayList(java.util.ArrayList) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) SetOperationRight(org.apache.asterix.lang.sqlpp.struct.SetOperationRight)

Aggregations

SelectSetOperation (org.apache.asterix.lang.sqlpp.clause.SelectSetOperation)8 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)8 ArrayList (java.util.ArrayList)6 SetOperationInput (org.apache.asterix.lang.sqlpp.struct.SetOperationInput)6 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)5 SelectBlock (org.apache.asterix.lang.sqlpp.clause.SelectBlock)5 Expression (org.apache.asterix.lang.common.base.Expression)3 LimitClause (org.apache.asterix.lang.common.clause.LimitClause)3 OrderbyClause (org.apache.asterix.lang.common.clause.OrderbyClause)3 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)3 FromClause (org.apache.asterix.lang.sqlpp.clause.FromClause)3 FromTerm (org.apache.asterix.lang.sqlpp.clause.FromTerm)3 SelectClause (org.apache.asterix.lang.sqlpp.clause.SelectClause)3 SelectElement (org.apache.asterix.lang.sqlpp.clause.SelectElement)3 SetOperationRight (org.apache.asterix.lang.sqlpp.struct.SetOperationRight)3 LetClause (org.apache.asterix.lang.common.clause.LetClause)2 VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)2 Pair (org.apache.hyracks.algebricks.common.utils.Pair)2 HashMap (java.util.HashMap)1 CompilationException (org.apache.asterix.common.exceptions.CompilationException)1