Search in sources :

Example 1 with FromTerm

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

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

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

the class SqlppCloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(FromClause fromClause, VariableSubstitutionEnvironment env) throws CompilationException {
    VariableSubstitutionEnvironment currentEnv = new VariableSubstitutionEnvironment(env);
    List<FromTerm> newFromTerms = new ArrayList<>();
    for (FromTerm fromTerm : fromClause.getFromTerms()) {
        Pair<ILangExpression, VariableSubstitutionEnvironment> p = fromTerm.accept(this, currentEnv);
        newFromTerms.add((FromTerm) p.first);
        // A right from term could be correlated from a left from term,
        // therefore we propagate the substitution environment.
        currentEnv = p.second;
    }
    return new Pair<>(new FromClause(newFromTerms), currentEnv);
}
Also used : VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) FromClause(org.apache.asterix.lang.sqlpp.clause.FromClause) ArrayList(java.util.ArrayList) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) FromTerm(org.apache.asterix.lang.sqlpp.clause.FromTerm) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 4 with FromTerm

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

the class SqlppCloneAndSubstituteVariablesVisitor method visit.

@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(FromTerm fromTerm, VariableSubstitutionEnvironment env) throws CompilationException {
    VariableExpr leftVar = fromTerm.getLeftVariable();
    VariableExpr newLeftVar = generateNewVariable(context, leftVar);
    VariableExpr newLeftPosVar = fromTerm.hasPositionalVariable() ? generateNewVariable(context, fromTerm.getPositionalVariable()) : null;
    Expression newLeftExpr = (Expression) visitUnnesBindingExpression(fromTerm.getLeftExpression(), env).first;
    List<AbstractBinaryCorrelateClause> newCorrelateClauses = new ArrayList<>();
    VariableSubstitutionEnvironment currentEnv = new VariableSubstitutionEnvironment(env);
    currentEnv.removeSubstitution(newLeftVar);
    if (newLeftPosVar != null) {
        currentEnv.removeSubstitution(newLeftPosVar);
    }
    for (AbstractBinaryCorrelateClause correlateClause : fromTerm.getCorrelateClauses()) {
        if (correlateClause.getClauseType() == ClauseType.UNNEST_CLAUSE) {
            // The right-hand-side of unnest could be correlated with the left side,
            // therefore we propagate the substitution environment of the left-side.
            Pair<ILangExpression, VariableSubstitutionEnvironment> p = correlateClause.accept(this, currentEnv);
            currentEnv = p.second;
            newCorrelateClauses.add((AbstractBinaryCorrelateClause) p.first);
        } else {
            // The right-hand-side of join and nest could not be correlated with the left side,
            // therefore we propagate the original substitution environment.
            newCorrelateClauses.add((AbstractBinaryCorrelateClause) correlateClause.accept(this, env).first);
            // Join binding variables should be removed for further traversal.
            currentEnv.removeSubstitution(correlateClause.getRightVariable());
            if (correlateClause.hasPositionalVariable()) {
                currentEnv.removeSubstitution(correlateClause.getPositionalVariable());
            }
        }
    }
    return new Pair<>(new FromTerm(newLeftExpr, newLeftVar, newLeftPosVar, newCorrelateClauses), currentEnv);
}
Also used : VariableSubstitutionEnvironment(org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment) AbstractBinaryCorrelateClause(org.apache.asterix.lang.sqlpp.clause.AbstractBinaryCorrelateClause) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) 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) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) FromTerm(org.apache.asterix.lang.sqlpp.clause.FromTerm) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 5 with FromTerm

use of org.apache.asterix.lang.sqlpp.clause.FromTerm 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)

Aggregations

FromTerm (org.apache.asterix.lang.sqlpp.clause.FromTerm)11 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)6 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)5 ArrayList (java.util.ArrayList)4 Expression (org.apache.asterix.lang.common.base.Expression)4 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)4 FromClause (org.apache.asterix.lang.sqlpp.clause.FromClause)4 SelectBlock (org.apache.asterix.lang.sqlpp.clause.SelectBlock)3 SelectClause (org.apache.asterix.lang.sqlpp.clause.SelectClause)3 SelectElement (org.apache.asterix.lang.sqlpp.clause.SelectElement)3 SelectSetOperation (org.apache.asterix.lang.sqlpp.clause.SelectSetOperation)3 SetOperationInput (org.apache.asterix.lang.sqlpp.struct.SetOperationInput)3 VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)2 AbstractBinaryCorrelateClause (org.apache.asterix.lang.sqlpp.clause.AbstractBinaryCorrelateClause)2 CaseExpression (org.apache.asterix.lang.sqlpp.expression.CaseExpression)2 Pair (org.apache.hyracks.algebricks.common.utils.Pair)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 FunctionSignature (org.apache.asterix.common.functions.FunctionSignature)1 LimitClause (org.apache.asterix.lang.common.clause.LimitClause)1