PageRenderTime 20ms CodeModel.GetById 14ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/optimizer/unionproc/UnionProcessor.java

#
Java | 90 lines | 39 code | 9 blank | 42 comment | 0 complexity | 2e274ae09f21d8e61e4cf9bf0cfe4d9c MD5 | raw file
 1/**
 2 * Licensed to the Apache Software Foundation (ASF) under one
 3 * or more contributor license agreements.  See the NOTICE file
 4 * distributed with this work for additional information
 5 * regarding copyright ownership.  The ASF licenses this file
 6 * to you under the Apache License, Version 2.0 (the
 7 * "License"); you may not use this file except in compliance
 8 * with the License.  You may obtain a copy of the License at
 9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.hadoop.hive.ql.optimizer.unionproc;
20
21import java.util.ArrayList;
22import java.util.LinkedHashMap;
23import java.util.Map;
24
25import org.apache.hadoop.hive.ql.lib.DefaultRuleDispatcher;
26import org.apache.hadoop.hive.ql.lib.Dispatcher;
27import org.apache.hadoop.hive.ql.lib.GraphWalker;
28import org.apache.hadoop.hive.ql.lib.Node;
29import org.apache.hadoop.hive.ql.lib.NodeProcessor;
30import org.apache.hadoop.hive.ql.lib.PreOrderWalker;
31import org.apache.hadoop.hive.ql.lib.Rule;
32import org.apache.hadoop.hive.ql.lib.RuleRegExp;
33import org.apache.hadoop.hive.ql.optimizer.Transform;
34import org.apache.hadoop.hive.ql.parse.ParseContext;
35import org.apache.hadoop.hive.ql.parse.SemanticException;
36
37/**
38 * Implementation of the union processor. This can be enhanced later on.
39 * Currently, it does the following: Identify if both the subqueries of UNION
40 * are map-only. Store that fact in the unionDesc/UnionOperator. If either of
41 * the sub-query involves a map-reduce job, a FS is introduced on top of the
42 * UNION. This can be later optimized to clone all the operators above the
43 * UNION.
44 * 
45 * The parse Context is not changed.
46 */
47public class UnionProcessor implements Transform {
48
49  /**
50   * empty constructor.
51   */
52  public UnionProcessor() {
53  }
54
55  /**
56   * Transform the query tree. For each union, store the fact whether both the
57   * sub-queries are map-only
58   * 
59   * @param pCtx
60   *          the current parse context
61   */
62  public ParseContext transform(ParseContext pCtx) throws SemanticException {
63    // create a walker which walks the tree in a DFS manner while maintaining
64    // the operator stack.
65    Map<Rule, NodeProcessor> opRules = new LinkedHashMap<Rule, NodeProcessor>();
66    opRules.put(new RuleRegExp(new String("R1"), "RS%.*UNION%"),
67        UnionProcFactory.getMapRedUnion());
68    opRules.put(new RuleRegExp(new String("R2"), "UNION%.*UNION%"),
69        UnionProcFactory.getUnknownUnion());
70    opRules.put(new RuleRegExp(new String("R3"), "TS%.*UNION%"),
71        UnionProcFactory.getMapUnion());
72    opRules.put(new RuleRegExp(new String("R3"), "MAPJOIN%.*UNION%"),
73        UnionProcFactory.getMapJoinUnion());
74
75    // The dispatcher fires the processor for the matching rule and passes the
76    // context along
77    UnionProcContext uCtx = new UnionProcContext();
78    Dispatcher disp = new DefaultRuleDispatcher(UnionProcFactory.getNoUnion(),
79        opRules, uCtx);
80    GraphWalker ogw = new PreOrderWalker(disp);
81
82    // Create a list of topop nodes
83    ArrayList<Node> topNodes = new ArrayList<Node>();
84    topNodes.addAll(pCtx.getTopOps().values());
85    ogw.startWalking(topNodes, null);
86    pCtx.setUCtx(uCtx);
87
88    return pCtx;
89  }
90}