Search in sources :

Example 1 with MergeJoinWork

use of org.apache.hadoop.hive.ql.plan.MergeJoinWork in project hive by apache.

the class CrossProductCheck method checkTezReducer.

private void checkTezReducer(TezWork tzWrk) throws SemanticException {
    for (BaseWork wrk : tzWrk.getAllWork()) {
        if (wrk instanceof MergeJoinWork) {
            wrk = ((MergeJoinWork) wrk).getMainWork();
        }
        if (!(wrk instanceof ReduceWork)) {
            continue;
        }
        ReduceWork rWork = (ReduceWork) wrk;
        Operator<? extends OperatorDesc> reducer = ((ReduceWork) wrk).getReducer();
        if (reducer instanceof JoinOperator || reducer instanceof CommonMergeJoinOperator) {
            Map<Integer, ExtractReduceSinkInfo.Info> rsInfo = new HashMap<Integer, ExtractReduceSinkInfo.Info>();
            for (Map.Entry<Integer, String> e : rWork.getTagToInput().entrySet()) {
                rsInfo.putAll(getReducerInfo(tzWrk, rWork.getName(), e.getValue()));
            }
            checkForCrossProduct(rWork.getName(), reducer, rsInfo);
        }
    }
}
Also used : CommonMergeJoinOperator(org.apache.hadoop.hive.ql.exec.CommonMergeJoinOperator) MapJoinOperator(org.apache.hadoop.hive.ql.exec.MapJoinOperator) AbstractMapJoinOperator(org.apache.hadoop.hive.ql.exec.AbstractMapJoinOperator) JoinOperator(org.apache.hadoop.hive.ql.exec.JoinOperator) MergeJoinWork(org.apache.hadoop.hive.ql.plan.MergeJoinWork) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ReduceWork(org.apache.hadoop.hive.ql.plan.ReduceWork) BaseWork(org.apache.hadoop.hive.ql.plan.BaseWork) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CommonMergeJoinOperator(org.apache.hadoop.hive.ql.exec.CommonMergeJoinOperator)

Example 2 with MergeJoinWork

use of org.apache.hadoop.hive.ql.plan.MergeJoinWork in project hive by apache.

the class DagUtils method createVertex.

/**
 * Create a vertex from a given work object.
 *
 * @param conf JobConf to be used to this execution unit
 * @param work The instance of BaseWork representing the actual work to be performed
 * by this vertex.
 * @param scratchDir HDFS scratch dir for this execution unit.
 * @param fileSystem FS corresponding to scratchDir and LocalResources
 * @param ctx This query's context
 * @return Vertex
 */
@SuppressWarnings("deprecation")
public Vertex createVertex(JobConf conf, BaseWork work, Path scratchDir, FileSystem fileSystem, Context ctx, boolean hasChildren, TezWork tezWork, VertexType vertexType, Map<String, LocalResource> localResources) throws Exception {
    Vertex v = null;
    // BaseWork.
    if (work instanceof MapWork) {
        v = createVertex(conf, (MapWork) work, fileSystem, scratchDir, ctx, vertexType, localResources);
    } else if (work instanceof ReduceWork) {
        v = createVertex(conf, (ReduceWork) work, fileSystem, scratchDir, ctx, localResources);
    } else if (work instanceof MergeJoinWork) {
        v = createVertex(conf, (MergeJoinWork) work, fileSystem, scratchDir, ctx, vertexType, localResources);
        // set VertexManagerPlugin if whether it's a cross product destination vertex
        List<String> crossProductSources = new ArrayList<>();
        for (BaseWork parentWork : tezWork.getParents(work)) {
            if (tezWork.getEdgeType(parentWork, work) == EdgeType.XPROD_EDGE) {
                crossProductSources.add(parentWork.getName());
            }
        }
        if (!crossProductSources.isEmpty()) {
            CartesianProductConfig cpConfig = new CartesianProductConfig(crossProductSources);
            v.setVertexManagerPlugin(VertexManagerPluginDescriptor.create(CartesianProductVertexManager.class.getName()).setUserPayload(cpConfig.toUserPayload(new TezConfiguration(conf))));
        // parallelism shouldn't be set for cartesian product vertex
        }
    } else {
        // something is seriously wrong if this is happening
        throw new HiveException(ErrorMsg.GENERIC_ERROR.getErrorCodedMsg());
    }
    // initialize stats publisher if necessary
    if (work.isGatheringStats()) {
        StatsPublisher statsPublisher;
        StatsFactory factory = StatsFactory.newFactory(conf);
        if (factory != null) {
            StatsCollectionContext sCntxt = new StatsCollectionContext(conf);
            sCntxt.setStatsTmpDirs(Utilities.getStatsTmpDirs(work, conf));
            statsPublisher = factory.getStatsPublisher();
            if (!statsPublisher.init(sCntxt)) {
                // creating stats table if not exists
                if (HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVE_STATS_RELIABLE)) {
                    throw new HiveException(ErrorMsg.STATSPUBLISHER_INITIALIZATION_ERROR.getErrorCodedMsg());
                }
            }
        }
    }
    // final vertices need to have at least one output
    if (!hasChildren) {
        v.addDataSink("out_" + work.getName(), new DataSinkDescriptor(OutputDescriptor.create(MROutput.class.getName()).setUserPayload(TezUtils.createUserPayloadFromConf(conf)), null, null));
    }
    return v;
}
Also used : StatsCollectionContext(org.apache.hadoop.hive.ql.stats.StatsCollectionContext) Vertex(org.apache.tez.dag.api.Vertex) PreWarmVertex(org.apache.tez.dag.api.PreWarmVertex) MergeJoinWork(org.apache.hadoop.hive.ql.plan.MergeJoinWork) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) ArrayList(java.util.ArrayList) CartesianProductVertexManager(org.apache.tez.runtime.library.cartesianproduct.CartesianProductVertexManager) ReduceWork(org.apache.hadoop.hive.ql.plan.ReduceWork) DataSinkDescriptor(org.apache.tez.dag.api.DataSinkDescriptor) StatsPublisher(org.apache.hadoop.hive.ql.stats.StatsPublisher) StatsFactory(org.apache.hadoop.hive.ql.stats.StatsFactory) MapWork(org.apache.hadoop.hive.ql.plan.MapWork) CartesianProductConfig(org.apache.tez.runtime.library.cartesianproduct.CartesianProductConfig) BaseWork(org.apache.hadoop.hive.ql.plan.BaseWork) TezConfiguration(org.apache.tez.dag.api.TezConfiguration)

Example 3 with MergeJoinWork

use of org.apache.hadoop.hive.ql.plan.MergeJoinWork in project hive by apache.

the class DagUtils method createVertex.

/**
 * Create a vertex from a given work object.
 *
 * @param conf JobConf to be used to this execution unit
 * @param workUnit The instance of BaseWork representing the actual work to be performed
 * by this vertex.
 * @param scratchDir HDFS scratch dir for this execution unit.
 * @return Vertex
 */
@SuppressWarnings("deprecation")
public Vertex createVertex(JobConf conf, BaseWork workUnit, Path scratchDir, TezWork tezWork, Map<String, LocalResource> localResources) throws Exception {
    Vertex vertex;
    // simply dispatch the call to the right method for the actual (sub-) type of
    // BaseWork.
    VertexType vertexType = tezWork.getVertexType(workUnit);
    if (workUnit instanceof MapWork) {
        vertex = createVertexFromMapWork(conf, (MapWork) workUnit, scratchDir, vertexType);
    } else if (workUnit instanceof ReduceWork) {
        vertex = createVertexFromReduceWork(conf, (ReduceWork) workUnit, scratchDir);
    } else if (workUnit instanceof MergeJoinWork) {
        vertex = createVertexFromMergeWork(conf, (MergeJoinWork) workUnit, scratchDir, vertexType);
        // set VertexManagerPlugin if whether it's a cross product destination vertex
        List<String> crossProductSources = new ArrayList<>();
        for (BaseWork parentWork : tezWork.getParents(workUnit)) {
            if (tezWork.getEdgeType(parentWork, workUnit) == EdgeType.XPROD_EDGE) {
                crossProductSources.add(parentWork.getName());
            }
        }
        if (!crossProductSources.isEmpty()) {
            CartesianProductConfig cpConfig = new CartesianProductConfig(crossProductSources);
            vertex.setVertexManagerPlugin(VertexManagerPluginDescriptor.create(CartesianProductVertexManager.class.getName()).setUserPayload(cpConfig.toUserPayload(new TezConfiguration(conf))));
        // parallelism shouldn't be set for cartesian product vertex
        }
    } else {
        // something is seriously wrong if this is happening
        throw new HiveException(ErrorMsg.GENERIC_ERROR.getErrorCodedMsg());
    }
    VertexExecutionContext vertexExecutionContext = createVertexExecutionContext(workUnit);
    vertex.addTaskLocalFiles(localResources);
    vertex.setTaskLaunchCmdOpts(getContainerJavaOpts(conf));
    vertex.setExecutionContext(vertexExecutionContext);
    // initialize stats publisher if necessary
    if (workUnit.isGatheringStats()) {
        StatsPublisher statsPublisher;
        StatsFactory factory = StatsFactory.newFactory(conf);
        if (factory != null) {
            StatsCollectionContext sCntxt = new StatsCollectionContext(conf);
            sCntxt.setStatsTmpDirs(Utilities.getStatsTmpDirs(workUnit, conf));
            statsPublisher = factory.getStatsPublisher();
            if (!statsPublisher.init(sCntxt)) {
                // creating stats table if not exists
                if (HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVE_STATS_RELIABLE)) {
                    throw new HiveException(ErrorMsg.STATSPUBLISHER_INITIALIZATION_ERROR.getErrorCodedMsg());
                }
            }
        }
    }
    final Class outputKlass;
    if (HiveOutputFormatImpl.class.getName().equals(conf.get("mapred.output.format.class"))) {
        // Hive uses this output format, when it is going to write all its data through FS operator
        outputKlass = NullMROutput.class;
    } else {
        outputKlass = MROutput.class;
    }
    // If there is a fileSink add a DataSink to the vertex
    boolean hasFileSink = workUnit.getAllOperators().stream().anyMatch(o -> o instanceof FileSinkOperator);
    // final vertices need to have at least one output
    boolean endVertex = tezWork.getLeaves().contains(workUnit);
    if (endVertex || hasFileSink) {
        OutputCommitterDescriptor ocd = null;
        String committer = HiveConf.getVar(conf, ConfVars.TEZ_MAPREDUCE_OUTPUT_COMMITTER);
        if (committer != null && !committer.isEmpty()) {
            ocd = OutputCommitterDescriptor.create(committer);
        }
        vertex.addDataSink("out_" + workUnit.getName(), new DataSinkDescriptor(OutputDescriptor.create(outputKlass.getName()).setUserPayload(vertex.getProcessorDescriptor().getUserPayload()), ocd, null));
    }
    return vertex;
}
Also used : StatsCollectionContext(org.apache.hadoop.hive.ql.stats.StatsCollectionContext) Vertex(org.apache.tez.dag.api.Vertex) PreWarmVertex(org.apache.tez.dag.api.PreWarmVertex) MergeJoinWork(org.apache.hadoop.hive.ql.plan.MergeJoinWork) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) FileSinkOperator(org.apache.hadoop.hive.ql.exec.FileSinkOperator) VertexExecutionContext(org.apache.tez.dag.api.Vertex.VertexExecutionContext) OutputCommitterDescriptor(org.apache.tez.dag.api.OutputCommitterDescriptor) ArrayList(java.util.ArrayList) CartesianProductVertexManager(org.apache.tez.runtime.library.cartesianproduct.CartesianProductVertexManager) ReduceWork(org.apache.hadoop.hive.ql.plan.ReduceWork) DataSinkDescriptor(org.apache.tez.dag.api.DataSinkDescriptor) StatsPublisher(org.apache.hadoop.hive.ql.stats.StatsPublisher) StatsFactory(org.apache.hadoop.hive.ql.stats.StatsFactory) MapWork(org.apache.hadoop.hive.ql.plan.MapWork) HiveOutputFormatImpl(org.apache.hadoop.hive.ql.io.HiveOutputFormatImpl) CartesianProductConfig(org.apache.tez.runtime.library.cartesianproduct.CartesianProductConfig) BaseWork(org.apache.hadoop.hive.ql.plan.BaseWork) VertexType(org.apache.hadoop.hive.ql.plan.TezWork.VertexType) TezConfiguration(org.apache.tez.dag.api.TezConfiguration)

Example 4 with MergeJoinWork

use of org.apache.hadoop.hive.ql.plan.MergeJoinWork in project hive by apache.

the class TezCompiler method setInputFormat.

@Override
protected void setInputFormat(Task<?> task) {
    if (task instanceof TezTask) {
        TezWork work = ((TezTask) task).getWork();
        List<BaseWork> all = work.getAllWork();
        for (BaseWork w : all) {
            if (w instanceof MergeJoinWork) {
                MergeJoinWork mj = (MergeJoinWork) w;
                setInputFormatForMapWork(mj.getMainWork());
                for (BaseWork bw : mj.getBaseWorkList()) {
                    setInputFormatForMapWork(bw);
                }
            } else {
                setInputFormatForMapWork(w);
            }
        }
    } else if (task instanceof ConditionalTask) {
        List<Task<?>> listTasks = ((ConditionalTask) task).getListTasks();
        for (Task<?> tsk : listTasks) {
            setInputFormat(tsk);
        }
    }
    if (task.getChildTasks() != null) {
        for (Task<?> childTask : task.getChildTasks()) {
            setInputFormat(childTask);
        }
    }
}
Also used : MergeJoinWork(org.apache.hadoop.hive.ql.plan.MergeJoinWork) ConditionalTask(org.apache.hadoop.hive.ql.exec.ConditionalTask) TezTask(org.apache.hadoop.hive.ql.exec.tez.TezTask) Task(org.apache.hadoop.hive.ql.exec.Task) ConditionalTask(org.apache.hadoop.hive.ql.exec.ConditionalTask) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) TezTask(org.apache.hadoop.hive.ql.exec.tez.TezTask) BaseWork(org.apache.hadoop.hive.ql.plan.BaseWork) TezWork(org.apache.hadoop.hive.ql.plan.TezWork)

Example 5 with MergeJoinWork

use of org.apache.hadoop.hive.ql.plan.MergeJoinWork in project hive by apache.

the class GenTezWork method process.

@Override
public Object process(Node nd, Stack<Node> stack, NodeProcessorCtx procContext, Object... nodeOutputs) throws SemanticException {
    GenTezProcContext context = (GenTezProcContext) procContext;
    assert context != null && context.currentTask != null && context.currentRootOperator != null;
    // Operator is a file sink or reduce sink. Something that forces
    // a new vertex.
    Operator<?> operator = (Operator<?>) nd;
    // root is the start of the operator pipeline we're currently
    // packing into a vertex, typically a table scan, union or join
    Operator<?> root = context.currentRootOperator;
    LOG.debug("Root operator: " + root);
    LOG.debug("Leaf operator: " + operator);
    if (context.clonedReduceSinks.contains(operator)) {
        // just skip and keep going
        return null;
    }
    TezWork tezWork = context.currentTask.getWork();
    // Right now the work graph is pretty simple. If there is no
    // Preceding work we have a root and will generate a map
    // vertex. If there is a preceding work we will generate
    // a reduce vertex
    BaseWork work;
    if (context.rootToWorkMap.containsKey(root)) {
        // will result into a vertex with multiple FS or RS operators.
        if (context.childToWorkMap.containsKey(operator)) {
            // if we've seen both root and child, we can bail.
            // clear out the mapjoin set. we don't need it anymore.
            context.currentMapJoinOperators.clear();
            // clear out the union set. we don't need it anymore.
            context.currentUnionOperators.clear();
            return null;
        } else {
            // At this point we don't have to do anything special. Just
            // run through the regular paces w/o creating a new task.
            work = context.rootToWorkMap.get(root);
        }
    } else {
        // create a new vertex
        if (context.preceedingWork == null) {
            work = utils.createMapWork(context, root, tezWork, null);
        } else {
            work = GenTezUtils.createReduceWork(context, root, tezWork);
        }
        context.rootToWorkMap.put(root, work);
    }
    // this is where we set the sort columns that we will be using for KeyValueInputMerge
    if (operator instanceof DummyStoreOperator) {
        work.addSortCols(root.getOpTraits().getSortCols().get(0));
    }
    if (!context.childToWorkMap.containsKey(operator)) {
        List<BaseWork> workItems = new LinkedList<BaseWork>();
        workItems.add(work);
        context.childToWorkMap.put(operator, workItems);
    } else {
        context.childToWorkMap.get(operator).add(work);
    }
    // which can affect the working of all downstream transformations.
    if (context.currentMergeJoinOperator != null) {
        // we are currently walking the big table side of the merge join. we need to create or hook up
        // merge join work.
        MergeJoinWork mergeJoinWork = null;
        if (context.opMergeJoinWorkMap.containsKey(context.currentMergeJoinOperator)) {
            // we have found a merge work corresponding to this closing operator. Hook up this work.
            mergeJoinWork = context.opMergeJoinWorkMap.get(context.currentMergeJoinOperator);
        } else {
            // we need to create the merge join work
            mergeJoinWork = new MergeJoinWork();
            mergeJoinWork.setMergeJoinOperator(context.currentMergeJoinOperator);
            tezWork.add(mergeJoinWork);
            context.opMergeJoinWorkMap.put(context.currentMergeJoinOperator, mergeJoinWork);
        }
        // connect the work correctly.
        work.addSortCols(root.getOpTraits().getSortCols().get(0));
        mergeJoinWork.addMergedWork(work, null, context.leafOperatorToFollowingWork);
        Operator<? extends OperatorDesc> parentOp = getParentFromStack(context.currentMergeJoinOperator, stack);
        // Set the big table position. Both the reduce work and merge join operator
        // should be set with the same value.
        // int pos = context.currentMergeJoinOperator.getTagForOperator(parentOp);
        int pos = context.currentMergeJoinOperator.getConf().getBigTablePosition();
        work.setTag(pos);
        context.currentMergeJoinOperator.getConf().setBigTablePosition(pos);
        tezWork.setVertexType(work, VertexType.MULTI_INPUT_UNINITIALIZED_EDGES);
        for (BaseWork parentWork : tezWork.getParents(work)) {
            TezEdgeProperty edgeProp = tezWork.getEdgeProperty(parentWork, work);
            tezWork.disconnect(parentWork, work);
            tezWork.connect(parentWork, mergeJoinWork, edgeProp);
        }
        for (BaseWork childWork : tezWork.getChildren(work)) {
            TezEdgeProperty edgeProp = tezWork.getEdgeProperty(work, childWork);
            tezWork.disconnect(work, childWork);
            tezWork.connect(mergeJoinWork, childWork, edgeProp);
        }
        tezWork.remove(work);
        context.rootToWorkMap.put(root, mergeJoinWork);
        context.childToWorkMap.get(operator).remove(work);
        context.childToWorkMap.get(operator).add(mergeJoinWork);
        work = mergeJoinWork;
        context.currentMergeJoinOperator = null;
    }
    // remember which mapjoin operator links with which work
    if (!context.currentMapJoinOperators.isEmpty()) {
        for (MapJoinOperator mj : context.currentMapJoinOperators) {
            // so we can later run the same logic that is run in ReduceSinkMapJoinProc.
            if (mj.getConf().isDynamicPartitionHashJoin()) {
                // Since this is a dynamic partitioned hash join, the work for this join should be a ReduceWork
                ReduceWork reduceWork = (ReduceWork) work;
                int bigTablePosition = mj.getConf().getPosBigTable();
                reduceWork.setTag(bigTablePosition);
                // Use context.mapJoinParentMap to get the original RS parents, because
                // the MapJoin's parents may have been replaced by dummy operator.
                List<Operator<?>> mapJoinOriginalParents = context.mapJoinParentMap.get(mj);
                if (mapJoinOriginalParents == null) {
                    throw new SemanticException("Unexpected error - context.mapJoinParentMap did not have an entry for " + mj);
                }
                for (int pos = 0; pos < mapJoinOriginalParents.size(); ++pos) {
                    // This processing only needs to happen for the small tables
                    if (pos == bigTablePosition) {
                        continue;
                    }
                    Operator<?> parentOp = mapJoinOriginalParents.get(pos);
                    context.smallTableParentToMapJoinMap.put(parentOp, mj);
                    ReduceSinkOperator parentRS = (ReduceSinkOperator) parentOp;
                    // TableDesc needed for dynamic partitioned hash join
                    GenMapRedUtils.setKeyAndValueDesc(reduceWork, parentRS);
                    // has its ReduceSink parent removed.
                    if (!context.mapJoinToUnprocessedSmallTableReduceSinks.get(mj).contains(parentRS)) {
                        // This reduce sink has been processed already, so the work for the parentRS exists
                        BaseWork parentWork = ReduceSinkMapJoinProc.getMapJoinParentWork(context, parentRS);
                        int tag = parentRS.getConf().getTag();
                        tag = (tag == -1 ? 0 : tag);
                        reduceWork.getTagToInput().put(tag, parentWork.getName());
                    }
                }
            }
            LOG.debug("Processing map join: " + mj);
            // mapjoin later
            if (!context.mapJoinWorkMap.containsKey(mj)) {
                List<BaseWork> workItems = new LinkedList<BaseWork>();
                workItems.add(work);
                context.mapJoinWorkMap.put(mj, workItems);
            } else {
                context.mapJoinWorkMap.get(mj).add(work);
            }
            /*
         * this happens in case of map join operations.
         * The tree looks like this:
         *
         *        RS <--- we are here perhaps
         *        |
         *     MapJoin
         *     /     \
         *   RS       TS
         *  /
         * TS
         *
         * If we are at the RS pointed above, and we may have already visited the
         * RS following the TS, we have already generated work for the TS-RS.
         * We need to hook the current work to this generated work.
         */
            if (context.linkOpWithWorkMap.containsKey(mj)) {
                Map<BaseWork, TezEdgeProperty> linkWorkMap = context.linkOpWithWorkMap.get(mj);
                if (linkWorkMap != null) {
                    // Note: it's not quite clear why this is done inside this if. Seems like it should be on the top level.
                    if (context.linkChildOpWithDummyOp.containsKey(mj)) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Adding dummy ops to work: " + work.getName() + ": " + context.linkChildOpWithDummyOp.get(mj));
                        }
                        for (Operator<?> dummy : context.linkChildOpWithDummyOp.get(mj)) {
                            work.addDummyOp((HashTableDummyOperator) dummy);
                        }
                    }
                    for (Entry<BaseWork, TezEdgeProperty> parentWorkMap : linkWorkMap.entrySet()) {
                        BaseWork parentWork = parentWorkMap.getKey();
                        LOG.debug("connecting " + parentWork.getName() + " with " + work.getName());
                        TezEdgeProperty edgeProp = parentWorkMap.getValue();
                        tezWork.connect(parentWork, work, edgeProp);
                        if (edgeProp.getEdgeType() == EdgeType.CUSTOM_EDGE) {
                            tezWork.setVertexType(work, VertexType.INITIALIZED_EDGES);
                        }
                        // of the downstream work
                        for (ReduceSinkOperator r : context.linkWorkWithReduceSinkMap.get(parentWork)) {
                            if (!context.mapJoinParentMap.get(mj).contains(r)) {
                                // already connected this RS operator or we will connect it at subsequent pass.
                                continue;
                            }
                            if (r.getConf().getOutputName() != null) {
                                LOG.debug("Cloning reduce sink " + r + " for multi-child broadcast edge");
                                // we've already set this one up. Need to clone for the next work.
                                r = (ReduceSinkOperator) OperatorFactory.getAndMakeChild(r.getCompilationOpContext(), (ReduceSinkDesc) r.getConf().clone(), new RowSchema(r.getSchema()), r.getParentOperators());
                                context.clonedReduceSinks.add(r);
                            }
                            r.getConf().setOutputName(work.getName());
                            context.connectedReduceSinks.add(r);
                        }
                    }
                }
            }
        }
        // clear out the set. we don't need it anymore.
        context.currentMapJoinOperators.clear();
    }
    // we might have to connect parent work with this work later.
    for (Operator<?> parent : new ArrayList<Operator<?>>(root.getParentOperators())) {
        LOG.debug("Removing {} as parent from {}", parent, root);
        context.leafOperatorToFollowingWork.remove(parent);
        context.leafOperatorToFollowingWork.put(parent, work);
        root.removeParent(parent);
    }
    if (!context.currentUnionOperators.isEmpty()) {
        // if there are union all operators, it means that the walking context contains union all operators.
        // please see more details of context.currentUnionOperator in GenTezWorkWalker
        UnionWork unionWork;
        if (context.unionWorkMap.containsKey(operator)) {
            // since we've passed this operator before.
            assert operator.getChildOperators().isEmpty();
            unionWork = (UnionWork) context.unionWorkMap.get(operator);
            // finally connect the union work with work
            connectUnionWorkWithWork(unionWork, work, tezWork, context);
        } else {
            // we've not seen this terminal before. we need to check
            // rootUnionWorkMap which contains the information of mapping the root
            // operator of a union work to a union work
            unionWork = context.rootUnionWorkMap.get(root);
            if (unionWork == null) {
                // if unionWork is null, it means it is the first time. we need to
                // create a union work object and add this work to it. Subsequent
                // work should reference the union and not the actual work.
                unionWork = GenTezUtils.createUnionWork(context, root, operator, tezWork);
                // finally connect the union work with work
                connectUnionWorkWithWork(unionWork, work, tezWork, context);
            }
        }
        context.currentUnionOperators.clear();
        work = unionWork;
    }
    // reasons. Roots are data sources, leaves are data sinks. I know.
    if (context.leafOperatorToFollowingWork.containsKey(operator)) {
        BaseWork followingWork = context.leafOperatorToFollowingWork.get(operator);
        long bytesPerReducer = context.conf.getLongVar(HiveConf.ConfVars.BYTESPERREDUCER);
        LOG.debug("Second pass. Leaf operator: " + operator + " has common downstream work: " + followingWork);
        if (operator instanceof DummyStoreOperator) {
            // this is the small table side.
            assert (followingWork instanceof MergeJoinWork);
            MergeJoinWork mergeJoinWork = (MergeJoinWork) followingWork;
            CommonMergeJoinOperator mergeJoinOp = mergeJoinWork.getMergeJoinOperator();
            work.setTag(mergeJoinOp.getTagForOperator(operator));
            mergeJoinWork.addMergedWork(null, work, context.leafOperatorToFollowingWork);
            tezWork.setVertexType(mergeJoinWork, VertexType.MULTI_INPUT_UNINITIALIZED_EDGES);
            for (BaseWork parentWork : tezWork.getParents(work)) {
                TezEdgeProperty edgeProp = tezWork.getEdgeProperty(parentWork, work);
                tezWork.disconnect(parentWork, work);
                tezWork.connect(parentWork, mergeJoinWork, edgeProp);
            }
            work = mergeJoinWork;
        } else {
            // need to add this branch to the key + value info
            assert operator instanceof ReduceSinkOperator && ((followingWork instanceof ReduceWork) || (followingWork instanceof MergeJoinWork) || followingWork instanceof UnionWork);
            ReduceSinkOperator rs = (ReduceSinkOperator) operator;
            ReduceWork rWork = null;
            if (followingWork instanceof MergeJoinWork) {
                MergeJoinWork mergeJoinWork = (MergeJoinWork) followingWork;
                rWork = (ReduceWork) mergeJoinWork.getMainWork();
            } else if (followingWork instanceof UnionWork) {
                // this can only be possible if there is merge work followed by the union
                UnionWork unionWork = (UnionWork) followingWork;
                int index = getFollowingWorkIndex(tezWork, unionWork, rs);
                BaseWork baseWork = tezWork.getChildren(unionWork).get(index);
                if (baseWork instanceof MergeJoinWork) {
                    MergeJoinWork mergeJoinWork = (MergeJoinWork) baseWork;
                    // disconnect the connection to union work and connect to merge work
                    followingWork = mergeJoinWork;
                    rWork = (ReduceWork) mergeJoinWork.getMainWork();
                } else {
                    rWork = (ReduceWork) baseWork;
                }
            } else {
                rWork = (ReduceWork) followingWork;
            }
            GenMapRedUtils.setKeyAndValueDesc(rWork, rs);
            // remember which parent belongs to which tag
            int tag = rs.getConf().getTag();
            rWork.getTagToInput().put(tag == -1 ? 0 : tag, work.getName());
            // remember the output name of the reduce sink
            rs.getConf().setOutputName(rWork.getName());
            // For dynamic partitioned hash join, run the ReduceSinkMapJoinProc logic for any
            // ReduceSink parents that we missed.
            MapJoinOperator mj = context.smallTableParentToMapJoinMap.get(rs);
            if (mj != null) {
                // Only need to run the logic for tables we missed
                if (context.mapJoinToUnprocessedSmallTableReduceSinks.get(mj).contains(rs)) {
                    // ReduceSinkMapJoinProc logic does not work unless the ReduceSink is connected as
                    // a parent of the MapJoin, but at this point we have already removed all of the
                    // parents from the MapJoin.
                    // Try temporarily adding the RS as a parent
                    ArrayList<Operator<?>> tempMJParents = new ArrayList<Operator<?>>();
                    tempMJParents.add(rs);
                    mj.setParentOperators(tempMJParents);
                    // ReduceSink also needs MapJoin as child
                    List<Operator<?>> rsChildren = rs.getChildOperators();
                    rsChildren.add(mj);
                    // Since the MapJoin has had all of its other parents removed at this point,
                    // it would be bad here if processReduceSinkToHashJoin() tries to do anything
                    // with the RS parent based on its position in the list of parents.
                    ReduceSinkMapJoinProc.processReduceSinkToHashJoin(rs, mj, context);
                    // Remove any parents from MapJoin again
                    mj.removeParents();
                // TODO: do we also need to remove the MapJoin from the list of RS's children?
                }
            }
            if (!context.connectedReduceSinks.contains(rs)) {
                // add dependency between the two work items
                TezEdgeProperty edgeProp;
                EdgeType edgeType = GenTezUtils.determineEdgeType(work, followingWork, rs);
                if (rWork.isAutoReduceParallelism()) {
                    edgeProp = new TezEdgeProperty(context.conf, edgeType, true, rWork.isSlowStart(), rWork.getMinReduceTasks(), rWork.getMaxReduceTasks(), bytesPerReducer);
                } else {
                    edgeProp = new TezEdgeProperty(edgeType);
                    edgeProp.setSlowStart(rWork.isSlowStart());
                }
                tezWork.connect(work, followingWork, edgeProp);
                context.connectedReduceSinks.add(rs);
            }
        }
    } else {
        LOG.debug("First pass. Leaf operator: " + operator);
    }
    // the next item will be a new root.
    if (!operator.getChildOperators().isEmpty()) {
        assert operator.getChildOperators().size() == 1;
        context.parentOfRoot = operator;
        context.currentRootOperator = operator.getChildOperators().get(0);
        context.preceedingWork = work;
    }
    return null;
}
Also used : CommonMergeJoinOperator(org.apache.hadoop.hive.ql.exec.CommonMergeJoinOperator) ReduceSinkOperator(org.apache.hadoop.hive.ql.exec.ReduceSinkOperator) MapJoinOperator(org.apache.hadoop.hive.ql.exec.MapJoinOperator) Operator(org.apache.hadoop.hive.ql.exec.Operator) DummyStoreOperator(org.apache.hadoop.hive.ql.exec.DummyStoreOperator) HashTableDummyOperator(org.apache.hadoop.hive.ql.exec.HashTableDummyOperator) TezEdgeProperty(org.apache.hadoop.hive.ql.plan.TezEdgeProperty) ArrayList(java.util.ArrayList) BaseWork(org.apache.hadoop.hive.ql.plan.BaseWork) CommonMergeJoinOperator(org.apache.hadoop.hive.ql.exec.CommonMergeJoinOperator) MapJoinOperator(org.apache.hadoop.hive.ql.exec.MapJoinOperator) MergeJoinWork(org.apache.hadoop.hive.ql.plan.MergeJoinWork) RowSchema(org.apache.hadoop.hive.ql.exec.RowSchema) DummyStoreOperator(org.apache.hadoop.hive.ql.exec.DummyStoreOperator) UnionWork(org.apache.hadoop.hive.ql.plan.UnionWork) ReduceWork(org.apache.hadoop.hive.ql.plan.ReduceWork) EdgeType(org.apache.hadoop.hive.ql.plan.TezEdgeProperty.EdgeType) LinkedList(java.util.LinkedList) ReduceSinkOperator(org.apache.hadoop.hive.ql.exec.ReduceSinkOperator) TezWork(org.apache.hadoop.hive.ql.plan.TezWork)

Aggregations

MergeJoinWork (org.apache.hadoop.hive.ql.plan.MergeJoinWork)7 BaseWork (org.apache.hadoop.hive.ql.plan.BaseWork)6 ReduceWork (org.apache.hadoop.hive.ql.plan.ReduceWork)5 ArrayList (java.util.ArrayList)4 CommonMergeJoinOperator (org.apache.hadoop.hive.ql.exec.CommonMergeJoinOperator)3 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)3 MapWork (org.apache.hadoop.hive.ql.plan.MapWork)3 TezWork (org.apache.hadoop.hive.ql.plan.TezWork)3 StatsCollectionContext (org.apache.hadoop.hive.ql.stats.StatsCollectionContext)3 StatsFactory (org.apache.hadoop.hive.ql.stats.StatsFactory)3 StatsPublisher (org.apache.hadoop.hive.ql.stats.StatsPublisher)3 DataSinkDescriptor (org.apache.tez.dag.api.DataSinkDescriptor)3 PreWarmVertex (org.apache.tez.dag.api.PreWarmVertex)3 Vertex (org.apache.tez.dag.api.Vertex)3 LinkedList (java.util.LinkedList)2 DummyStoreOperator (org.apache.hadoop.hive.ql.exec.DummyStoreOperator)2 MapJoinOperator (org.apache.hadoop.hive.ql.exec.MapJoinOperator)2 Operator (org.apache.hadoop.hive.ql.exec.Operator)2 TezEdgeProperty (org.apache.hadoop.hive.ql.plan.TezEdgeProperty)2 TezConfiguration (org.apache.tez.dag.api.TezConfiguration)2