Search in sources :

Example 16 with LogicalVariable

use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable in project asterixdb by apache.

the class LangExpressionToPlanTranslator method visit.

@Override
public Pair<ILogicalOperator, LogicalVariable> visit(RecordConstructor rc, Mutable<ILogicalOperator> tupSource) throws CompilationException {
    AbstractFunctionCallExpression f = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.OPEN_RECORD_CONSTRUCTOR));
    LogicalVariable v1 = context.newVar();
    AssignOperator a = new AssignOperator(v1, new MutableObject<>(f));
    Mutable<ILogicalOperator> topOp = tupSource;
    for (FieldBinding fb : rc.getFbList()) {
        Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo1 = langExprToAlgExpression(fb.getLeftExpr(), topOp);
        f.getArguments().add(new MutableObject<>(eo1.first));
        topOp = eo1.second;
        Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo2 = langExprToAlgExpression(fb.getRightExpr(), topOp);
        f.getArguments().add(new MutableObject<>(eo2.first));
        topOp = eo2.second;
    }
    a.getInputs().add(topOp);
    return new Pair<>(a, v1);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) FieldBinding(org.apache.asterix.lang.common.expression.FieldBinding) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) 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 17 with LogicalVariable

use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable in project asterixdb by apache.

the class SqlppExpressionToPlanTranslator method generateUnnestForBinaryCorrelateRightBranch.

private Pair<ILogicalOperator, LogicalVariable> generateUnnestForBinaryCorrelateRightBranch(AbstractBinaryCorrelateClause binaryCorrelate, Mutable<ILogicalOperator> inputOpRef, boolean innerUnnest) throws CompilationException {
    LogicalVariable rightVar = context.newVarFromExpression(binaryCorrelate.getRightVariable());
    Expression rightExpr = binaryCorrelate.getRightExpression();
    Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(rightExpr, inputOpRef);
    ILogicalOperator unnestOp;
    if (binaryCorrelate.hasPositionalVariable()) {
        LogicalVariable pVar = context.newVarFromExpression(binaryCorrelate.getPositionalVariable());
        // We set the positional variable type as BIGINT type.
        unnestOp = innerUnnest ? new UnnestOperator(rightVar, new MutableObject<>(makeUnnestExpression(eo.first)), pVar, BuiltinType.AINT64, new PositionWriter()) : new LeftOuterUnnestOperator(rightVar, new MutableObject<>(makeUnnestExpression(eo.first)), pVar, BuiltinType.AINT64, new PositionWriter());
    } else {
        unnestOp = innerUnnest ? new UnnestOperator(rightVar, new MutableObject<>(makeUnnestExpression(eo.first))) : new LeftOuterUnnestOperator(rightVar, new MutableObject<>(makeUnnestExpression(eo.first)));
    }
    unnestOp.getInputs().add(eo.second);
    return new Pair<>(unnestOp, rightVar);
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) 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) 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) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) LeftOuterUnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestOperator) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 18 with LogicalVariable

use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable in project asterixdb by apache.

the class SqlppExpressionToPlanTranslator method processSelectClause.

// Generates the return expression for a select clause.
private Pair<ILogicalOperator, LogicalVariable> processSelectClause(SelectBlock selectBlock, Mutable<ILogicalOperator> tupSrc) throws CompilationException {
    SelectClause selectClause = selectBlock.getSelectClause();
    Expression returnExpr;
    if (selectClause.selectElement()) {
        returnExpr = selectClause.getSelectElement().getExpression();
    } else {
        returnExpr = generateReturnExpr(selectClause, selectBlock);
    }
    Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(returnExpr, tupSrc);
    LogicalVariable returnVar;
    ILogicalOperator returnOperator;
    if (returnExpr.getKind() == Kind.VARIABLE_EXPRESSION) {
        VariableExpr varExpr = (VariableExpr) returnExpr;
        returnOperator = eo.second.getValue();
        returnVar = context.getVar(varExpr.getVar().getId());
    } else {
        returnVar = context.newVar();
        returnOperator = new AssignOperator(returnVar, new MutableObject<ILogicalExpression>(eo.first));
        returnOperator.getInputs().add(eo.second);
    }
    if (selectClause.distinct()) {
        DistinctOperator distinctOperator = new DistinctOperator(mkSingletonArrayList(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(returnVar))));
        distinctOperator.getInputs().add(new MutableObject<ILogicalOperator>(returnOperator));
        return new Pair<>(distinctOperator, returnVar);
    } else {
        return new Pair<>(returnOperator, returnVar);
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) SelectClause(org.apache.asterix.lang.sqlpp.clause.SelectClause) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) DistinctOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.DistinctOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) 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) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) MutableObject(org.apache.commons.lang3.mutable.MutableObject) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 19 with LogicalVariable

use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable in project asterixdb by apache.

the class StaticTypeCastUtil method injectCastToRelaxType.

private static boolean injectCastToRelaxType(Mutable<ILogicalExpression> expRef, IAType inputFieldType, IVariableTypeEnvironment env) throws AlgebricksException {
    ILogicalExpression argExpr = expRef.getValue();
    List<LogicalVariable> parameterVars = new ArrayList<LogicalVariable>();
    argExpr.getUsedVariables(parameterVars);
    // we need to handle open fields recursively by their default
    // types
    // for list, their item type is any
    // for record, their
    boolean castInjected = false;
    if (argExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL || argExpr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
        IAType reqFieldType = inputFieldType;
        // do not enforce nested type in the case of no-used variables
        switch(inputFieldType.getTypeTag()) {
            case OBJECT:
                reqFieldType = DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE;
                break;
            case ARRAY:
                reqFieldType = DefaultOpenFieldType.NESTED_OPEN_AORDERED_LIST_TYPE;
                break;
            case MULTISET:
                reqFieldType = DefaultOpenFieldType.NESTED_OPEN_AUNORDERED_LIST_TYPE;
                break;
            default:
                break;
        }
        // do not enforce nested type in the case of no-used variables
        if (!inputFieldType.equals(reqFieldType) && !parameterVars.isEmpty()) {
            //inject dynamic type casting
            injectCastFunction(FunctionUtil.getFunctionInfo(BuiltinFunctions.CAST_TYPE), reqFieldType, inputFieldType, expRef, argExpr);
            castInjected = true;
        }
        //recursively rewrite function arguments
        if (argExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL && TypeCastUtils.getRequiredType((AbstractFunctionCallExpression) argExpr) == null && reqFieldType != null) {
            if (castInjected) {
                //rewrite the arg expression inside the dynamic cast
                ScalarFunctionCallExpression argFunc = (ScalarFunctionCallExpression) argExpr;
                rewriteFuncExpr(argFunc, inputFieldType, inputFieldType, env);
            } else {
                //rewrite arg
                ScalarFunctionCallExpression argFunc = (ScalarFunctionCallExpression) argExpr;
                rewriteFuncExpr(argFunc, reqFieldType, inputFieldType, env);
            }
        }
    }
    return castInjected;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) IAType(org.apache.asterix.om.types.IAType) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Example 20 with LogicalVariable

use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable in project asterixdb by apache.

the class SqlppExpressionToPlanTranslator method visit.

@Override
public Pair<ILogicalOperator, LogicalVariable> visit(Query q, Mutable<ILogicalOperator> tupSource) throws CompilationException {
    Expression queryBody = q.getBody();
    if (queryBody.getKind() == Kind.SELECT_EXPRESSION) {
        SelectExpression selectExpr = (SelectExpression) queryBody;
        if (q.isTopLevel()) {
            selectExpr.setSubquery(false);
        }
        return queryBody.accept(this, tupSource);
    } else {
        LogicalVariable var = context.newVar();
        Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(queryBody, tupSource);
        AssignOperator assignOp = new AssignOperator(var, new MutableObject<ILogicalExpression>(eo.first));
        assignOp.getInputs().add(eo.second);
        ProjectOperator projectOp = new ProjectOperator(var);
        projectOp.getInputs().add(new MutableObject<ILogicalOperator>(assignOp));
        return new Pair<>(projectOp, var);
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) 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) ProjectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.ProjectOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Aggregations

LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)376 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)196 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)182 ArrayList (java.util.ArrayList)171 Mutable (org.apache.commons.lang3.mutable.Mutable)136 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)127 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)92 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)79 Pair (org.apache.hyracks.algebricks.common.utils.Pair)75 HashSet (java.util.HashSet)60 MutableObject (org.apache.commons.lang3.mutable.MutableObject)60 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)60 ILogicalPlan (org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan)54 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)46 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)36 GroupByOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator)33 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)32 AggregateOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator)28 FunctionalDependency (org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency)28 IAType (org.apache.asterix.om.types.IAType)27