Search in sources :

Example 1 with AbstractBinaryCorrelateClause

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

the class SqlppExpressionToPlanTranslator method visit.

@Override
public Pair<ILogicalOperator, LogicalVariable> visit(FromTerm fromTerm, Mutable<ILogicalOperator> tupSource) throws CompilationException {
    LogicalVariable fromVar = context.newVarFromExpression(fromTerm.getLeftVariable());
    Expression fromExpr = fromTerm.getLeftExpression();
    Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(fromExpr, tupSource);
    ILogicalOperator unnestOp;
    if (fromTerm.hasPositionalVariable()) {
        LogicalVariable pVar = context.newVarFromExpression(fromTerm.getPositionalVariable());
        // We set the positional variable type as BIGINT type.
        unnestOp = new UnnestOperator(fromVar, new MutableObject<ILogicalExpression>(makeUnnestExpression(eo.first)), pVar, BuiltinType.AINT64, new PositionWriter());
    } else {
        unnestOp = new UnnestOperator(fromVar, new MutableObject<ILogicalExpression>(makeUnnestExpression(eo.first)));
    }
    unnestOp.getInputs().add(eo.second);
    // Processes joins, unnests, and nests.
    Mutable<ILogicalOperator> topOpRef = new MutableObject<>(unnestOp);
    if (fromTerm.hasCorrelateClauses()) {
        for (AbstractBinaryCorrelateClause correlateClause : fromTerm.getCorrelateClauses()) {
            if (correlateClause.getClauseType() == ClauseType.UNNEST_CLAUSE) {
                // Correlation is allowed.
                topOpRef = new MutableObject<>(correlateClause.accept(this, topOpRef).first);
            } else {
                // Correlation is dis-allowed.
                uncorrelatedLeftBranchStack.push(topOpRef);
                topOpRef = new MutableObject<>(correlateClause.accept(this, tupSource).first);
            }
        }
    }
    return new Pair<>(topOpRef.getValue(), fromVar);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) LeftOuterUnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractBinaryCorrelateClause(org.apache.asterix.lang.sqlpp.clause.AbstractBinaryCorrelateClause) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) AggregateFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) UnnestingFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) MutableObject(org.apache.commons.lang3.mutable.MutableObject) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 2 with AbstractBinaryCorrelateClause

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

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

the class FreeVariableVisitor method visit.

@Override
public Void visit(FromTerm fromTerm, Collection<VariableExpr> freeVars) throws CompilationException {
    // The encountered binding variables so far in the fromterm.
    Collection<VariableExpr> bindingVariables = new HashSet<>();
    // Visit the left expression of a from term.
    fromTerm.getLeftExpression().accept(this, freeVars);
    // Adds binding variables.
    bindingVariables.add(fromTerm.getLeftVariable());
    if (fromTerm.hasPositionalVariable()) {
        bindingVariables.add(fromTerm.getPositionalVariable());
    }
    // Visits join/unnest/nest clauses.
    for (AbstractBinaryCorrelateClause correlateClause : fromTerm.getCorrelateClauses()) {
        Collection<VariableExpr> correlateFreeVars = new HashSet<>();
        correlateClause.accept(this, correlateFreeVars);
        if (correlateClause.getClauseType() != ClauseType.JOIN_CLAUSE) {
            // Correlation is allowed if the clause is not a join clause,
            // therefore we remove left-side binding variables for these cases.
            correlateFreeVars.removeAll(bindingVariables);
            // Adds binding variables.
            bindingVariables.add(correlateClause.getRightVariable());
            if (correlateClause.hasPositionalVariable()) {
                bindingVariables.add(correlateClause.getPositionalVariable());
            }
        }
        freeVars.addAll(correlateFreeVars);
    }
    return null;
}
Also used : AbstractBinaryCorrelateClause(org.apache.asterix.lang.sqlpp.clause.AbstractBinaryCorrelateClause) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) HashSet(java.util.HashSet)

Example 4 with AbstractBinaryCorrelateClause

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

the class AbstractSqlppExpressionScopingVisitor method visit.

@Override
public Expression visit(FromTerm fromTerm, ILangExpression arg) throws CompilationException {
    scopeChecker.createNewScope();
    // Visit the left expression of a from term.
    fromTerm.setLeftExpression(visit(fromTerm.getLeftExpression(), fromTerm));
    // Registers the data item variable.
    VariableExpr leftVar = fromTerm.getLeftVariable();
    addNewVarSymbolToScope(scopeChecker.getCurrentScope(), leftVar.getVar());
    // Registers the positional variable
    if (fromTerm.hasPositionalVariable()) {
        VariableExpr posVar = fromTerm.getPositionalVariable();
        addNewVarSymbolToScope(scopeChecker.getCurrentScope(), posVar.getVar());
    }
    // Visits join/unnest/nest clauses.
    for (AbstractBinaryCorrelateClause correlateClause : fromTerm.getCorrelateClauses()) {
        correlateClause.accept(this, fromTerm);
    }
    return null;
}
Also used : AbstractBinaryCorrelateClause(org.apache.asterix.lang.sqlpp.clause.AbstractBinaryCorrelateClause) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr)

Example 5 with AbstractBinaryCorrelateClause

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

the class SqlppInlineUdfsVisitor method visit.

@Override
public Boolean visit(FromTerm fromTerm, List<FunctionDecl> func) throws CompilationException {
    boolean changed = false;
    Pair<Boolean, Expression> p = inlineUdfsInExpr(fromTerm.getLeftExpression(), func);
    fromTerm.setLeftExpression(p.second);
    changed |= p.first;
    for (AbstractBinaryCorrelateClause correlateClause : fromTerm.getCorrelateClauses()) {
        changed |= correlateClause.accept(this, func);
    }
    return changed;
}
Also used : AbstractBinaryCorrelateClause(org.apache.asterix.lang.sqlpp.clause.AbstractBinaryCorrelateClause) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression)

Aggregations

AbstractBinaryCorrelateClause (org.apache.asterix.lang.sqlpp.clause.AbstractBinaryCorrelateClause)9 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)5 Expression (org.apache.asterix.lang.common.base.Expression)4 CaseExpression (org.apache.asterix.lang.sqlpp.expression.CaseExpression)4 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)4 ArrayList (java.util.ArrayList)3 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)3 FromTerm (org.apache.asterix.lang.sqlpp.clause.FromTerm)2 Pair (org.apache.hyracks.algebricks.common.utils.Pair)2 HashSet (java.util.HashSet)1 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)1 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)1 VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)1 Mutable (org.apache.commons.lang3.mutable.Mutable)1 MutableObject (org.apache.commons.lang3.mutable.MutableObject)1 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)1 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)1 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)1 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)1 AggregateFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression)1