PageRenderTime 27ms CodeModel.GetById 9ms app.highlight 14ms RepoModel.GetById 2ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/plan/JoinDesc.java

#
Java | 365 lines | 223 code | 59 blank | 83 comment | 24 complexity | 9b9b94a7abdb06a912cda104034b7dbb 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.plan;
 20
 21import java.io.Serializable;
 22import java.util.ArrayList;
 23import java.util.LinkedHashMap;
 24import java.util.List;
 25import java.util.Map;
 26
 27/**
 28 * Join operator Descriptor implementation.
 29 *
 30 */
 31@Explain(displayName = "Join Operator")
 32public class JoinDesc implements Serializable {
 33  private static final long serialVersionUID = 1L;
 34  public static final int INNER_JOIN = 0;
 35  public static final int LEFT_OUTER_JOIN = 1;
 36  public static final int RIGHT_OUTER_JOIN = 2;
 37  public static final int FULL_OUTER_JOIN = 3;
 38  public static final int UNIQUE_JOIN = 4;
 39  public static final int LEFT_SEMI_JOIN = 5;
 40
 41  // used to handle skew join
 42  private boolean handleSkewJoin = false;
 43  private int skewKeyDefinition = -1;
 44  private Map<Byte, String> bigKeysDirMap;
 45  private Map<Byte, Map<Byte, String>> smallKeysDirMap;
 46  private Map<Byte, TableDesc> skewKeysValuesTables;
 47
 48  // alias to key mapping
 49  private Map<Byte, List<ExprNodeDesc>> exprs;
 50
 51  // alias to filter mapping
 52  private Map<Byte, List<ExprNodeDesc>> filters;
 53
 54  // used for create joinOutputObjectInspector
 55  protected List<String> outputColumnNames;
 56
 57  // key:column output name, value:tag
 58  private transient Map<String, Byte> reversedExprs;
 59
 60  // No outer join involved
 61  protected boolean noOuterJoin;
 62
 63  protected JoinCondDesc[] conds;
 64
 65  protected Byte[] tagOrder;
 66  private TableDesc keyTableDesc;
 67
 68  public JoinDesc() {
 69  }
 70
 71  public JoinDesc(final Map<Byte, List<ExprNodeDesc>> exprs,
 72      List<String> outputColumnNames, final boolean noOuterJoin,
 73      final JoinCondDesc[] conds, final Map<Byte, List<ExprNodeDesc>> filters) {
 74    this.exprs = exprs;
 75    this.outputColumnNames = outputColumnNames;
 76    this.noOuterJoin = noOuterJoin;
 77    this.conds = conds;
 78    this.filters = filters;
 79
 80    tagOrder = new Byte[exprs.size()];
 81    for (int i = 0; i < tagOrder.length; i++) {
 82      tagOrder[i] = (byte) i;
 83    }
 84  }
 85
 86  public JoinDesc(final Map<Byte, List<ExprNodeDesc>> exprs,
 87      List<String> outputColumnNames, final boolean noOuterJoin,
 88      final JoinCondDesc[] conds) {
 89    this(exprs, outputColumnNames, noOuterJoin, conds, null);
 90  }
 91
 92  public JoinDesc(final Map<Byte, List<ExprNodeDesc>> exprs,
 93      List<String> outputColumnNames) {
 94    this(exprs, outputColumnNames, true, null);
 95  }
 96
 97  public JoinDesc(final Map<Byte, List<ExprNodeDesc>> exprs,
 98      List<String> outputColumnNames, final JoinCondDesc[] conds) {
 99    this(exprs, outputColumnNames, true, conds, null);
100  }
101
102  public JoinDesc(JoinDesc clone) {
103    this.bigKeysDirMap = clone.bigKeysDirMap;
104    this.conds = clone.conds;
105    this.exprs = clone.exprs;
106    this.handleSkewJoin = clone.handleSkewJoin;
107    this.keyTableDesc = clone.keyTableDesc;
108    this.noOuterJoin = clone.noOuterJoin;
109    this.outputColumnNames = clone.outputColumnNames;
110    this.reversedExprs = clone.reversedExprs;
111    this.skewKeyDefinition = clone.skewKeyDefinition;
112    this.skewKeysValuesTables = clone.skewKeysValuesTables;
113    this.smallKeysDirMap = clone.smallKeysDirMap;
114    this.tagOrder = clone.tagOrder;
115    this.filters = clone.filters;
116  }
117
118  public Map<Byte, List<ExprNodeDesc>> getExprs() {
119    return exprs;
120  }
121
122  public Map<String, Byte> getReversedExprs() {
123    return reversedExprs;
124  }
125
126  public void setReversedExprs(Map<String, Byte> reversedExprs) {
127    this.reversedExprs = reversedExprs;
128  }
129
130  @Explain(displayName = "condition expressions")
131  public Map<Byte, String> getExprsStringMap() {
132    if (getExprs() == null) {
133      return null;
134    }
135
136    LinkedHashMap<Byte, String> ret = new LinkedHashMap<Byte, String>();
137
138    for (Map.Entry<Byte, List<ExprNodeDesc>> ent : getExprs().entrySet()) {
139      StringBuilder sb = new StringBuilder();
140      boolean first = true;
141      if (ent.getValue() != null) {
142        for (ExprNodeDesc expr : ent.getValue()) {
143          if (!first) {
144            sb.append(" ");
145          }
146
147          first = false;
148          sb.append("{");
149          sb.append(expr.getExprString());
150          sb.append("}");
151        }
152      }
153      ret.put(ent.getKey(), sb.toString());
154    }
155
156    return ret;
157  }
158
159  public void setExprs(final Map<Byte, List<ExprNodeDesc>> exprs) {
160    this.exprs = exprs;
161  }
162
163  /**
164   * Get the string representation of filters.
165   *
166   * Returns null if they are no filters.
167   *
168   * @return Map from alias to filters on the alias.
169   */
170  @Explain(displayName = "filter predicates")
171  public Map<Byte, String> getFiltersStringMap() {
172    if (getFilters() == null || getFilters().size() == 0) {
173      return null;
174    }
175
176    LinkedHashMap<Byte, String> ret = new LinkedHashMap<Byte, String>();
177    boolean filtersPresent = false;
178
179    for (Map.Entry<Byte, List<ExprNodeDesc>> ent : getFilters().entrySet()) {
180      StringBuilder sb = new StringBuilder();
181      boolean first = true;
182      if (ent.getValue() != null) {
183        if (ent.getValue().size() != 0) {
184          filtersPresent = true;
185        }
186        for (ExprNodeDesc expr : ent.getValue()) {
187          if (!first) {
188            sb.append(" ");
189          }
190
191          first = false;
192          sb.append("{");
193          sb.append(expr.getExprString());
194          sb.append("}");
195        }
196      }
197      ret.put(ent.getKey(), sb.toString());
198    }
199
200    if (filtersPresent) {
201      return ret;
202    } else {
203      return null;
204    }
205  }
206
207
208  public Map<Byte, List<ExprNodeDesc>> getFilters() {
209    return filters;
210  }
211
212  public void setFilters(Map<Byte, List<ExprNodeDesc>> filters) {
213    this.filters = filters;
214  }
215
216  @Explain(displayName = "outputColumnNames")
217  public List<String> getOutputColumnNames() {
218    return outputColumnNames;
219  }
220
221  public void setOutputColumnNames(
222      List<String> outputColumnNames) {
223    this.outputColumnNames = outputColumnNames;
224  }
225
226  public boolean getNoOuterJoin() {
227    return noOuterJoin;
228  }
229
230  public void setNoOuterJoin(final boolean noOuterJoin) {
231    this.noOuterJoin = noOuterJoin;
232  }
233
234  @Explain(displayName = "condition map")
235  public List<JoinCondDesc> getCondsList() {
236    if (conds == null) {
237      return null;
238    }
239
240    ArrayList<JoinCondDesc> l = new ArrayList<JoinCondDesc>();
241    for (JoinCondDesc cond : conds) {
242      l.add(cond);
243    }
244
245    return l;
246  }
247
248  public JoinCondDesc[] getConds() {
249    return conds;
250  }
251
252  public void setConds(final JoinCondDesc[] conds) {
253    this.conds = conds;
254  }
255
256  /**
257   * The order in which tables should be processed when joining.
258   *
259   * @return Array of tags
260   */
261  public Byte[] getTagOrder() {
262    return tagOrder;
263  }
264
265  /**
266   * The order in which tables should be processed when joining.
267   *
268   * @param tagOrder
269   *          Array of tags
270   */
271  public void setTagOrder(Byte[] tagOrder) {
272    this.tagOrder = tagOrder;
273  }
274
275  @Explain(displayName = "handleSkewJoin")
276  public boolean getHandleSkewJoin() {
277    return handleSkewJoin;
278  }
279
280  /**
281   * set to handle skew join in this join op.
282   *
283   * @param handleSkewJoin
284   */
285  public void setHandleSkewJoin(boolean handleSkewJoin) {
286    this.handleSkewJoin = handleSkewJoin;
287  }
288
289  /**
290   * @return mapping from tbl to dir for big keys.
291   */
292  public Map<Byte, String> getBigKeysDirMap() {
293    return bigKeysDirMap;
294  }
295
296  /**
297   * set the mapping from tbl to dir for big keys.
298   *
299   * @param bigKeysDirMap
300   */
301  public void setBigKeysDirMap(Map<Byte, String> bigKeysDirMap) {
302    this.bigKeysDirMap = bigKeysDirMap;
303  }
304
305  /**
306   * @return mapping from tbl to dir for small keys
307   */
308  public Map<Byte, Map<Byte, String>> getSmallKeysDirMap() {
309    return smallKeysDirMap;
310  }
311
312  /**
313   * set the mapping from tbl to dir for small keys.
314   *
315   * @param bigKeysDirMap
316   */
317  public void setSmallKeysDirMap(Map<Byte, Map<Byte, String>> smallKeysDirMap) {
318    this.smallKeysDirMap = smallKeysDirMap;
319  }
320
321  /**
322   * @return skew key definition. If we see a key's associated entries' number
323   *         is bigger than this, we will define this key as a skew key.
324   */
325  public int getSkewKeyDefinition() {
326    return skewKeyDefinition;
327  }
328
329  /**
330   * set skew key definition.
331   *
332   * @param skewKeyDefinition
333   */
334  public void setSkewKeyDefinition(int skewKeyDefinition) {
335    this.skewKeyDefinition = skewKeyDefinition;
336  }
337
338  /**
339   * @return the table desc for storing skew keys and their corresponding value;
340   */
341  public Map<Byte, TableDesc> getSkewKeysValuesTables() {
342    return skewKeysValuesTables;
343  }
344
345  /**
346   * @param skewKeysValuesTable
347   *          set the table desc for storing skew keys and their corresponding
348   *          value;
349   */
350  public void setSkewKeysValuesTables(Map<Byte, TableDesc> skewKeysValuesTables) {
351    this.skewKeysValuesTables = skewKeysValuesTables;
352  }
353
354  public boolean isNoOuterJoin() {
355    return noOuterJoin;
356  }
357
358  public void setKeyTableDesc(TableDesc keyTblDesc) {
359    keyTableDesc = keyTblDesc;
360  }
361
362  public TableDesc getKeyTableDesc() {
363    return keyTableDesc;
364  }
365}