Search in sources :

Example 1 with IOptimizationContext

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

the class LoadRecordFieldsRule method findAndEliminateRedundantFieldAccess.

/**
     * Rewrite
     * assign $x := field-access($y, "field")
     * assign $y := record-constructor { "field": Expr, ... }
     * into
     * assign $x := Expr
     * assign $y := record-constructor { "field": Expr, ... }
     */
private static boolean findAndEliminateRedundantFieldAccess(AssignOperator assign, IOptimizationContext context) throws AlgebricksException {
    ILogicalExpression expr = getFirstExpr(assign);
    AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expr;
    ILogicalExpression arg0 = f.getArguments().get(0).getValue();
    if (arg0.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
        return false;
    }
    VariableReferenceExpression vre = (VariableReferenceExpression) arg0;
    LogicalVariable recordVar = vre.getVariableReference();
    ILogicalExpression arg1 = f.getArguments().get(1).getValue();
    if (arg1.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
        return false;
    }
    IVariableTypeEnvironment typeEnvironment = context.getOutputTypeEnvironment(assign);
    ConstantExpression ce = (ConstantExpression) arg1;
    ILogicalExpression fldExpr;
    if (f.getFunctionIdentifier().equals(BuiltinFunctions.FIELD_ACCESS_BY_NAME)) {
        String fldName = ((AString) ((AsterixConstantValue) ce.getValue()).getObject()).getStringValue();
        fldExpr = findFieldExpression(assign, recordVar, fldName, typeEnvironment, (name, expression, env) -> findFieldByNameFromRecordConstructor(name, expression));
    } else if (f.getFunctionIdentifier().equals(BuiltinFunctions.FIELD_ACCESS_BY_INDEX)) {
        Integer fldIdx = ((AInt32) ((AsterixConstantValue) ce.getValue()).getObject()).getIntegerValue();
        fldExpr = findFieldExpression(assign, recordVar, fldIdx, typeEnvironment, LoadRecordFieldsRule::findFieldByIndexFromRecordConstructor);
    } else if (f.getFunctionIdentifier().equals(BuiltinFunctions.FIELD_ACCESS_NESTED)) {
        return false;
    } else {
        throw new IllegalStateException();
    }
    if (fldExpr == null) {
        return false;
    }
    // check the liveness of the new expression
    List<LogicalVariable> usedVariables = new ArrayList<>();
    fldExpr.getUsedVariables(usedVariables);
    List<LogicalVariable> liveInputVars = new ArrayList<>();
    VariableUtilities.getLiveVariables(assign, liveInputVars);
    usedVariables.removeAll(liveInputVars);
    if (usedVariables.isEmpty()) {
        assign.getExpressions().get(0).setValue(fldExpr);
        return true;
    } else {
        return false;
    }
}
Also used : LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) OperatorPropertiesUtil(org.apache.hyracks.algebricks.core.algebra.util.OperatorPropertiesUtil) IOptimizationContext(org.apache.hyracks.algebricks.core.algebra.base.IOptimizationContext) NestedTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator) AbstractOperatorWithNestedPlans(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractOperatorWithNestedPlans) ConstantExpressionUtil(org.apache.asterix.om.utils.ConstantExpressionUtil) LogicalExpressionTag(org.apache.hyracks.algebricks.core.algebra.base.LogicalExpressionTag) AbstractLogicalExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractLogicalExpression) VariableUtilities(org.apache.hyracks.algebricks.core.algebra.operators.logical.visitors.VariableUtilities) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ILogicalExpressionReferenceTransform(org.apache.hyracks.algebricks.core.algebra.visitors.ILogicalExpressionReferenceTransform) OperatorAnnotation(org.apache.asterix.algebra.base.OperatorAnnotation) ARecordType(org.apache.asterix.om.types.ARecordType) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AlgebricksBuiltinFunctions(org.apache.hyracks.algebricks.core.algebra.functions.AlgebricksBuiltinFunctions) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) IAlgebraicRewriteRule(org.apache.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule) FunctionalDependency(org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency) LinkedList(java.util.LinkedList) BuiltinFunctions(org.apache.asterix.om.functions.BuiltinFunctions) MutableObject(org.apache.commons.lang3.mutable.MutableObject) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AString(org.apache.asterix.om.base.AString) Iterator(java.util.Iterator) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) LogicalOperatorTag(org.apache.hyracks.algebricks.core.algebra.base.LogicalOperatorTag) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AInt32(org.apache.asterix.om.base.AInt32) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) List(java.util.List) AnalysisUtil(org.apache.asterix.optimizer.base.AnalysisUtil) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ArrayList(java.util.ArrayList) AString(org.apache.asterix.om.base.AString) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) AString(org.apache.asterix.om.base.AString) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment)

Example 2 with IOptimizationContext

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

the class AbstractHashJoinPOperator method getRequiredPropertiesForChildren.

@Override
public PhysicalRequirements getRequiredPropertiesForChildren(ILogicalOperator op, IPhysicalPropertiesVector reqdByParent, IOptimizationContext context) {
    // In a cost-based optimizer, we would also try to propagate the
    // parent's partitioning requirements.
    IPartitioningProperty pp1;
    IPartitioningProperty pp2;
    switch(partitioningType) {
        case PAIRWISE:
            pp1 = new UnorderedPartitionedProperty(new ListSet<>(keysLeftBranch), context.getComputationNodeDomain());
            pp2 = new UnorderedPartitionedProperty(new ListSet<>(keysRightBranch), context.getComputationNodeDomain());
            break;
        case BROADCAST:
            pp1 = new RandomPartitioningProperty(context.getComputationNodeDomain());
            pp2 = new BroadcastPartitioningProperty(context.getComputationNodeDomain());
            break;
        default:
            throw new IllegalStateException();
    }
    StructuralPropertiesVector[] pv = new StructuralPropertiesVector[2];
    pv[0] = OperatorPropertiesUtil.checkUnpartitionedAndGetPropertiesVector(op, new StructuralPropertiesVector(pp1, null));
    pv[1] = OperatorPropertiesUtil.checkUnpartitionedAndGetPropertiesVector(op, new StructuralPropertiesVector(pp2, null));
    IPartitioningRequirementsCoordinator prc;
    switch(kind) {
        case INNER:
            {
                prc = IPartitioningRequirementsCoordinator.EQCLASS_PARTITIONING_COORDINATOR;
                break;
            }
        case LEFT_OUTER:
            {
                prc = new IPartitioningRequirementsCoordinator() {

                    @Override
                    public Pair<Boolean, IPartitioningProperty> coordinateRequirements(IPartitioningProperty requirements, IPartitioningProperty firstDeliveredPartitioning, ILogicalOperator op, IOptimizationContext context) throws AlgebricksException {
                        if (firstDeliveredPartitioning != null && requirements != null && firstDeliveredPartitioning.getPartitioningType() == requirements.getPartitioningType()) {
                            switch(requirements.getPartitioningType()) {
                                case UNORDERED_PARTITIONED:
                                    {
                                        UnorderedPartitionedProperty upp1 = (UnorderedPartitionedProperty) firstDeliveredPartitioning;
                                        Set<LogicalVariable> set1 = upp1.getColumnSet();
                                        UnorderedPartitionedProperty uppreq = (UnorderedPartitionedProperty) requirements;
                                        Set<LogicalVariable> modifuppreq = new ListSet<LogicalVariable>();
                                        Map<LogicalVariable, EquivalenceClass> eqmap = context.getEquivalenceClassMap(op);
                                        Set<LogicalVariable> covered = new ListSet<LogicalVariable>();
                                        Set<LogicalVariable> keysCurrent = uppreq.getColumnSet();
                                        List<LogicalVariable> keysFirst = (keysRightBranch.containsAll(keysCurrent)) ? keysRightBranch : keysLeftBranch;
                                        List<LogicalVariable> keysSecond = keysFirst == keysRightBranch ? keysLeftBranch : keysRightBranch;
                                        for (LogicalVariable r : uppreq.getColumnSet()) {
                                            EquivalenceClass ecSnd = eqmap.get(r);
                                            boolean found = false;
                                            int j = 0;
                                            for (LogicalVariable rvar : keysFirst) {
                                                if (rvar == r || ecSnd != null && eqmap.get(rvar) == ecSnd) {
                                                    found = true;
                                                    break;
                                                }
                                                j++;
                                            }
                                            if (!found) {
                                                throw new IllegalStateException("Did not find a variable equivalent to " + r + " among " + keysFirst);
                                            }
                                            LogicalVariable v2 = keysSecond.get(j);
                                            EquivalenceClass ecFst = eqmap.get(v2);
                                            for (LogicalVariable vset1 : set1) {
                                                if (vset1 == v2 || ecFst != null && eqmap.get(vset1) == ecFst) {
                                                    covered.add(vset1);
                                                    modifuppreq.add(r);
                                                    break;
                                                }
                                            }
                                            if (covered.equals(set1)) {
                                                break;
                                            }
                                        }
                                        if (!covered.equals(set1)) {
                                            throw new AlgebricksException("Could not modify " + requirements + " to agree with partitioning property " + firstDeliveredPartitioning + " delivered by previous input operator.");
                                        }
                                        UnorderedPartitionedProperty upp2 = new UnorderedPartitionedProperty(modifuppreq, requirements.getNodeDomain());
                                        return new Pair<>(false, upp2);
                                    }
                                case ORDERED_PARTITIONED:
                                    {
                                        throw new NotImplementedException();
                                    }
                            }
                        }
                        return new Pair<>(true, requirements);
                    }
                };
                break;
            }
        default:
            {
                throw new IllegalStateException();
            }
    }
    return new PhysicalRequirements(pv, prc);
}
Also used : StructuralPropertiesVector(org.apache.hyracks.algebricks.core.algebra.properties.StructuralPropertiesVector) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) UnorderedPartitionedProperty(org.apache.hyracks.algebricks.core.algebra.properties.UnorderedPartitionedProperty) BroadcastPartitioningProperty(org.apache.hyracks.algebricks.core.algebra.properties.BroadcastPartitioningProperty) Set(java.util.Set) ListSet(org.apache.hyracks.algebricks.common.utils.ListSet) IOptimizationContext(org.apache.hyracks.algebricks.core.algebra.base.IOptimizationContext) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) NotImplementedException(org.apache.hyracks.algebricks.common.exceptions.NotImplementedException) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) IPartitioningRequirementsCoordinator(org.apache.hyracks.algebricks.core.algebra.properties.IPartitioningRequirementsCoordinator) IPartitioningProperty(org.apache.hyracks.algebricks.core.algebra.properties.IPartitioningProperty) PhysicalRequirements(org.apache.hyracks.algebricks.core.algebra.properties.PhysicalRequirements) ListSet(org.apache.hyracks.algebricks.common.utils.ListSet) List(java.util.List) RandomPartitioningProperty(org.apache.hyracks.algebricks.core.algebra.properties.RandomPartitioningProperty) Map(java.util.Map) EquivalenceClass(org.apache.hyracks.algebricks.core.algebra.base.EquivalenceClass) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 3 with IOptimizationContext

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

the class IntroduceSelectAccessMethodRule method intersectAllSecondaryIndexes.

/**
     * Construct all applicable secondary index-based access paths in the given selection plan and
     * intersect them using INTERSECT operator to guide to the common primary index search.
     * In case where the applicable index is one, we only construct one path.
     */
private boolean intersectAllSecondaryIndexes(List<Pair<IAccessMethod, Index>> chosenIndexes, Map<IAccessMethod, AccessMethodAnalysisContext> analyzedAMs, IOptimizationContext context) throws AlgebricksException {
    Pair<IAccessMethod, Index> chosenIndex = null;
    Optional<Pair<IAccessMethod, Index>> primaryIndex = chosenIndexes.stream().filter(pair -> pair.second.isPrimaryIndex()).findFirst();
    if (chosenIndexes.size() == 1) {
        chosenIndex = chosenIndexes.get(0);
    } else if (primaryIndex.isPresent()) {
        // one primary + secondary indexes, choose the primary index directly.
        chosenIndex = primaryIndex.get();
    }
    if (chosenIndex != null) {
        AccessMethodAnalysisContext analysisCtx = analyzedAMs.get(chosenIndex.first);
        return chosenIndex.first.applySelectPlanTransformation(afterSelectRefs, selectRef, subTree, chosenIndex.second, analysisCtx, context);
    }
    // Intersect all secondary indexes, and postpone the primary index search.
    Mutable<ILogicalExpression> conditionRef = selectOp.getCondition();
    List<ILogicalOperator> subRoots = new ArrayList<>();
    for (Pair<IAccessMethod, Index> pair : chosenIndexes) {
        AccessMethodAnalysisContext analysisCtx = analyzedAMs.get(pair.first);
        subRoots.add(pair.first.createSecondaryToPrimaryPlan(conditionRef, subTree, null, pair.second, analysisCtx, AccessMethodUtils.retainInputs(subTree.getDataSourceVariables(), subTree.getDataSourceRef().getValue(), afterSelectRefs), false, subTree.getDataSourceRef().getValue().getInputs().get(0).getValue().getExecutionMode() == ExecutionMode.UNPARTITIONED, context));
    }
    // Connect each secondary index utilization plan to a common intersect operator.
    ILogicalOperator primaryUnnestOp = connectAll2ndarySearchPlanWithIntersect(subRoots, context);
    subTree.getDataSourceRef().setValue(primaryUnnestOp);
    return primaryUnnestOp != null;
}
Also used : OperatorPropertiesUtil(org.apache.hyracks.algebricks.core.algebra.util.OperatorPropertiesUtil) CommitOperator(org.apache.asterix.algebra.operators.CommitOperator) IOptimizationContext(org.apache.hyracks.algebricks.core.algebra.base.IOptimizationContext) HashMap(java.util.HashMap) LogicalExpressionTag(org.apache.hyracks.algebricks.core.algebra.base.LogicalExpressionTag) ArrayList(java.util.ArrayList) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) OrderOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.OrderOperator) Map(java.util.Map) Index(org.apache.asterix.metadata.entities.Index) MutableObject(org.apache.commons.lang3.mutable.MutableObject) DelegateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.DelegateOperator) IntersectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.IntersectOperator) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) LogicalOperatorTag(org.apache.hyracks.algebricks.core.algebra.base.LogicalOperatorTag) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) List(java.util.List) MetadataProvider(org.apache.asterix.metadata.declared.MetadataProvider) TreeMap(java.util.TreeMap) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment) AbstractLogicalOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator) SelectOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) Optional(java.util.Optional) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) Pair(org.apache.hyracks.algebricks.common.utils.Pair) ExecutionMode(org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator.ExecutionMode) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) ArrayList(java.util.ArrayList) Index(org.apache.asterix.metadata.entities.Index) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 4 with IOptimizationContext

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

the class HeuristicCompilerFactoryBuilder method create.

@Override
public ICompilerFactory create() {
    return new ICompilerFactory() {

        @Override
        public ICompiler createCompiler(final ILogicalPlan plan, final IMetadataProvider<?, ?> metadata, int varCounter) {
            final IOptimizationContext oc = optCtxFactory.createOptimizationContext(varCounter, expressionEvalSizeComputer, mergeAggregationExpressionFactory, expressionTypeComputer, missableTypeComputer, conflictingTypeResolver, physicalOptimizationConfig, clusterLocations);
            oc.setMetadataDeclarations(metadata);
            final HeuristicOptimizer opt = new HeuristicOptimizer(plan, logicalRewrites, physicalRewrites, oc);
            return new ICompiler() {

                @Override
                public void optimize() throws AlgebricksException {
                    opt.optimize();
                }

                @Override
                public JobSpecification createJob(Object appContext, IJobletEventListenerFactory jobEventListenerFactory) throws AlgebricksException {
                    AlgebricksConfig.ALGEBRICKS_LOGGER.fine("Starting Job Generation.\n");
                    JobGenContext context = new JobGenContext(null, metadata, appContext, serializerDeserializerProvider, hashFunctionFactoryProvider, hashFunctionFamilyProvider, comparatorFactoryProvider, typeTraitProvider, binaryBooleanInspectorFactory, binaryIntegerInspectorFactory, printerProvider, missingWriterFactory, normalizedKeyComputerFactoryProvider, expressionRuntimeProvider, expressionTypeComputer, oc, expressionEvalSizeComputer, partialAggregationTypeComputer, predEvaluatorFactoryProvider, physicalOptimizationConfig.getFrameSize(), clusterLocations);
                    PlanCompiler pc = new PlanCompiler(context);
                    return pc.compilePlan(plan, null, jobEventListenerFactory);
                }
            };
        }
    };
}
Also used : PlanCompiler(org.apache.hyracks.algebricks.core.jobgen.impl.PlanCompiler) IOptimizationContext(org.apache.hyracks.algebricks.core.algebra.base.IOptimizationContext) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) IMetadataProvider(org.apache.hyracks.algebricks.core.algebra.metadata.IMetadataProvider) HeuristicOptimizer(org.apache.hyracks.algebricks.core.rewriter.base.HeuristicOptimizer) IJobletEventListenerFactory(org.apache.hyracks.api.job.IJobletEventListenerFactory) JobGenContext(org.apache.hyracks.algebricks.core.jobgen.impl.JobGenContext)

Aggregations

IOptimizationContext (org.apache.hyracks.algebricks.core.algebra.base.IOptimizationContext)4 List (java.util.List)3 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)3 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)3 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Mutable (org.apache.commons.lang3.mutable.Mutable)2 MutableObject (org.apache.commons.lang3.mutable.MutableObject)2 Pair (org.apache.hyracks.algebricks.common.utils.Pair)2 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)2 LogicalExpressionTag (org.apache.hyracks.algebricks.core.algebra.base.LogicalExpressionTag)2 LogicalOperatorTag (org.apache.hyracks.algebricks.core.algebra.base.LogicalOperatorTag)2 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)2 IVariableTypeEnvironment (org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment)2 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)2 FunctionIdentifier (org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier)2 AbstractLogicalOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator)2 SelectOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator)2 OperatorPropertiesUtil (org.apache.hyracks.algebricks.core.algebra.util.OperatorPropertiesUtil)2