Search in sources :

Example 6 with Triple

use of org.apache.hyracks.algebricks.common.utils.Triple in project asterixdb by apache.

the class PushUnnestDownThroughUnionRule method rewritePost.

@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
    AbstractLogicalOperator unnest = (AbstractLogicalOperator) opRef.getValue();
    if (unnest.getOperatorTag() != LogicalOperatorTag.UNNEST) {
        return false;
    }
    UnnestOperator unnestOpRef = (UnnestOperator) opRef.getValue();
    Mutable<ILogicalOperator> unionOp = unnest.getInputs().get(0);
    AbstractLogicalOperator unionAbstractOp = (AbstractLogicalOperator) unionOp.getValue();
    if (unionAbstractOp.getOperatorTag() != LogicalOperatorTag.UNIONALL) {
        return false;
    }
    LogicalVariable unnestVar1 = context.newVar();
    UnnestOperator unnest1 = new UnnestOperator(unnestVar1, new MutableObject<ILogicalExpression>(unnestOpRef.getExpressionRef().getValue().cloneExpression()));
    LogicalVariable unnestVar2 = context.newVar();
    UnnestOperator unnest2 = new UnnestOperator(unnestVar2, new MutableObject<ILogicalExpression>(unnestOpRef.getExpressionRef().getValue().cloneExpression()));
    //Getting the two topmost branched and adding them as an input to the unnests:
    Mutable<ILogicalOperator> branch1 = unionAbstractOp.getInputs().get(0);
    ILogicalOperator agg1 = branch1.getValue();
    List<LogicalVariable> agg1_var = new ArrayList<LogicalVariable>();
    VariableUtilities.getLiveVariables(agg1, agg1_var);
    Mutable<ILogicalOperator> branch2 = unionAbstractOp.getInputs().get(1);
    ILogicalOperator agg2 = branch2.getValue();
    List<LogicalVariable> agg2_var = new ArrayList<LogicalVariable>();
    VariableUtilities.getLiveVariables(agg2, agg2_var);
    //Modifying the unnest so it has the right variable
    List<LogicalVariable> var_unnest_1 = new ArrayList<LogicalVariable>();
    unnest1.getExpressionRef().getValue().getUsedVariables(var_unnest_1);
    unnest1.getExpressionRef().getValue().substituteVar(var_unnest_1.get(0), agg1_var.get(0));
    List<LogicalVariable> var_unnest2 = new ArrayList<LogicalVariable>();
    unnest2.getExpressionRef().getValue().getUsedVariables(var_unnest2);
    unnest2.getExpressionRef().getValue().substituteVar(var_unnest2.get(0), agg2_var.get(0));
    unnest1.getInputs().add(branch1);
    unnest2.getInputs().add(branch2);
    context.computeAndSetTypeEnvironmentForOperator(unnest1);
    context.computeAndSetTypeEnvironmentForOperator(unnest2);
    //creating a new union operator with the updated logical variables
    List<Triple<LogicalVariable, LogicalVariable, LogicalVariable>> varMap = new ArrayList<Triple<LogicalVariable, LogicalVariable, LogicalVariable>>(1);
    Triple<LogicalVariable, LogicalVariable, LogicalVariable> union_triple_vars = new Triple<LogicalVariable, LogicalVariable, LogicalVariable>(unnestVar1, unnestVar2, unnestOpRef.getVariables().get(0));
    varMap.add(union_triple_vars);
    UnionAllOperator unionOpFinal = new UnionAllOperator(varMap);
    unionOpFinal.getInputs().add(new MutableObject<ILogicalOperator>(unnest1));
    unionOpFinal.getInputs().add(new MutableObject<ILogicalOperator>(unnest2));
    context.computeAndSetTypeEnvironmentForOperator(unionOpFinal);
    opRef.setValue(unionOpFinal);
    return true;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) Triple(org.apache.hyracks.algebricks.common.utils.Triple) UnnestOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) UnionAllOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnionAllOperator)

Example 7 with Triple

use of org.apache.hyracks.algebricks.common.utils.Triple in project asterixdb by apache.

the class NonTaggedDataFormat method partitioningEvaluatorFactory.

@SuppressWarnings("unchecked")
@Override
public Triple<IScalarEvaluatorFactory, ScalarFunctionCallExpression, IAType> partitioningEvaluatorFactory(ARecordType recType, List<String> fldName) throws AlgebricksException {
    String[] names = recType.getFieldNames();
    int n = names.length;
    if (fldName.size() > 1) {
        for (int i = 0; i < n; i++) {
            if (names[i].equals(fldName.get(0))) {
                IScalarEvaluatorFactory recordEvalFactory = new ColumnAccessEvalFactory(GlobalConfig.DEFAULT_INPUT_DATA_COLUMN);
                ArrayBackedValueStorage abvs = new ArrayBackedValueStorage();
                DataOutput dos = abvs.getDataOutput();
                try {
                    AInt32 ai = new AInt32(i);
                    SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(ai.getType()).serialize(ai, dos);
                } catch (HyracksDataException e) {
                    throw new AlgebricksException(e);
                }
                IScalarEvaluatorFactory fldIndexEvalFactory = new ConstantEvalFactory(Arrays.copyOf(abvs.getByteArray(), abvs.getLength()));
                IScalarEvaluatorFactory evalFactory = new FieldAccessByIndexEvalFactory(recordEvalFactory, fldIndexEvalFactory, recType);
                IFunctionInfo finfoAccess = BuiltinFunctions.getAsterixFunctionInfo(BuiltinFunctions.FIELD_ACCESS_BY_INDEX);
                ScalarFunctionCallExpression partitionFun = new ScalarFunctionCallExpression(finfoAccess, new MutableObject<ILogicalExpression>(new VariableReferenceExpression(METADATA_DUMMY_VAR)), new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(new AInt32(i)))));
                return new Triple<IScalarEvaluatorFactory, ScalarFunctionCallExpression, IAType>(evalFactory, partitionFun, recType.getFieldTypes()[i]);
            }
        }
    } else {
        IScalarEvaluatorFactory recordEvalFactory = new ColumnAccessEvalFactory(GlobalConfig.DEFAULT_INPUT_DATA_COLUMN);
        ArrayBackedValueStorage abvs = new ArrayBackedValueStorage();
        DataOutput dos = abvs.getDataOutput();
        AOrderedList as = new AOrderedList(fldName);
        try {
            SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(as.getType()).serialize(as, dos);
        } catch (HyracksDataException e) {
            throw new AlgebricksException(e);
        }
        IScalarEvaluatorFactory evalFactory = new FieldAccessNestedEvalFactory(recordEvalFactory, recType, fldName);
        IFunctionInfo finfoAccess = BuiltinFunctions.getAsterixFunctionInfo(BuiltinFunctions.FIELD_ACCESS_NESTED);
        ScalarFunctionCallExpression partitionFun = new ScalarFunctionCallExpression(finfoAccess, new MutableObject<ILogicalExpression>(new VariableReferenceExpression(METADATA_DUMMY_VAR)), new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(as))));
        return new Triple<IScalarEvaluatorFactory, ScalarFunctionCallExpression, IAType>(evalFactory, partitionFun, recType.getSubFieldType(fldName));
    }
    throw new AlgebricksException("Could not find field " + fldName + " in the schema.");
}
Also used : DataOutput(java.io.DataOutput) IFunctionInfo(org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo) ConstantEvalFactory(org.apache.hyracks.algebricks.runtime.evaluators.ConstantEvalFactory) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) AString(org.apache.asterix.om.base.AString) AInt32(org.apache.asterix.om.base.AInt32) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) Triple(org.apache.hyracks.algebricks.common.utils.Triple) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) AOrderedList(org.apache.asterix.om.base.AOrderedList) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) ColumnAccessEvalFactory(org.apache.hyracks.algebricks.runtime.evaluators.ColumnAccessEvalFactory) FieldAccessByIndexEvalFactory(org.apache.asterix.runtime.evaluators.functions.records.FieldAccessByIndexEvalFactory) FieldAccessNestedEvalFactory(org.apache.asterix.runtime.evaluators.functions.records.FieldAccessNestedEvalFactory) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)

Example 8 with Triple

use of org.apache.hyracks.algebricks.common.utils.Triple in project asterixdb by apache.

the class RemoveLeftOuterUnnestForLeftOuterJoinRule method checkGroupBy.

// Checks the group-by operator on top of the left outer join operator.
private Triple<Boolean, ILogicalExpression, ILogicalExpression> checkGroupBy(GroupByOperator gbyOperator, LogicalVariable varToUnnest) {
    Pair<Boolean, ILogicalOperator> checkNestedPlanResult = checkNestedPlan(gbyOperator);
    if (!checkNestedPlanResult.first) {
        return new Triple<>(false, null, null);
    }
    ILogicalOperator root = checkNestedPlanResult.second;
    if (root.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
        return new Triple<>(false, null, null);
    }
    // Checks aggregate.
    AggregateOperator agg = (AggregateOperator) root;
    Pair<Boolean, ILogicalExpression> listifyArgPair = checksAggregate(agg, varToUnnest);
    if (!listifyArgPair.first) {
        return new Triple<>(false, null, null);
    }
    // Checks select.
    ILogicalOperator rootInputOp = root.getInputs().get(0).getValue();
    if (rootInputOp.getOperatorTag() != LogicalOperatorTag.SELECT) {
        return new Triple<>(false, null, null);
    }
    SelectOperator select = (SelectOperator) rootInputOp;
    Pair<Boolean, ILogicalExpression> conditionArgPair = checkSelect(select);
    return new Triple<>(true, listifyArgPair.second, conditionArgPair.second);
}
Also used : Triple(org.apache.hyracks.algebricks.common.utils.Triple) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator)

Example 9 with Triple

use of org.apache.hyracks.algebricks.common.utils.Triple in project asterixdb by apache.

the class IsomorphismOperatorVisitor method visitUnionOperator.

@Override
public Boolean visitUnionOperator(UnionAllOperator op, ILogicalOperator arg) throws AlgebricksException {
    AbstractLogicalOperator aop = (AbstractLogicalOperator) arg;
    if (aop.getOperatorTag() != LogicalOperatorTag.UNIONALL) {
        return Boolean.FALSE;
    }
    UnionAllOperator unionOpArg = (UnionAllOperator) copyAndSubstituteVar(op, arg);
    List<Triple<LogicalVariable, LogicalVariable, LogicalVariable>> mapping = op.getVariableMappings();
    List<Triple<LogicalVariable, LogicalVariable, LogicalVariable>> mappingArg = unionOpArg.getVariableMappings();
    if (mapping.size() != mappingArg.size()) {
        return Boolean.FALSE;
    }
    return VariableUtilities.varListEqualUnordered(mapping, mappingArg);
}
Also used : Triple(org.apache.hyracks.algebricks.common.utils.Triple) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) UnionAllOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.UnionAllOperator)

Example 10 with Triple

use of org.apache.hyracks.algebricks.common.utils.Triple in project asterixdb by apache.

the class RemoveLeftOuterUnnestForLeftOuterJoinRule method checkUnnestAndGby.

// Checks left outer unnest and gby.
private Triple<Boolean, ILogicalExpression, ILogicalExpression> checkUnnestAndGby(LeftOuterUnnestOperator outerUnnest, GroupByOperator gbyOperator) {
    // Checks left outer unnest.
    Pair<Boolean, LogicalVariable> checkUnnestResult = checkUnnest(outerUnnest);
    if (!checkUnnestResult.first) {
        return new Triple<>(false, null, null);
    }
    // Checks group-by.
    LogicalVariable varToUnnest = checkUnnestResult.second;
    Triple<Boolean, ILogicalExpression, ILogicalExpression> checkGbyResult = checkGroupBy(gbyOperator, varToUnnest);
    if (!checkGbyResult.first) {
        return new Triple<>(false, null, null);
    }
    return checkGbyResult;
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) Triple(org.apache.hyracks.algebricks.common.utils.Triple) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)

Aggregations

Triple (org.apache.hyracks.algebricks.common.utils.Triple)15 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)9 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)8 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)7 UnionAllOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.UnionAllOperator)7 ArrayList (java.util.ArrayList)6 Mutable (org.apache.commons.lang3.mutable.Mutable)4 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)4 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)3 AString (org.apache.asterix.om.base.AString)2 ScalarFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression)2 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)2 SelectOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator)2 UnnestOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator)2 DataOutput (java.io.DataOutput)1 HashSet (java.util.HashSet)1 List (java.util.List)1 IAdapterFactory (org.apache.asterix.external.api.IAdapterFactory)1 FeedIntakeOperatorDescriptor (org.apache.asterix.external.operators.FeedIntakeOperatorDescriptor)1 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)1