Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DataPartition.java
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.planner;
16 
17 import java.util.List;
18 
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 
25 import com.cloudera.impala.thrift.TDataPartition;
26 import com.cloudera.impala.thrift.TPartitionType;
27 import com.google.common.base.Joiner;
28 import com.google.common.base.Objects;
29 import com.google.common.base.Preconditions;
30 import com.google.common.collect.Lists;
31 
39 public class DataPartition {
40  private final static Logger LOG = LoggerFactory.getLogger(DataPartition.class);
41 
42  private final TPartitionType type_;
43 
44  // for hash partition: exprs used to compute hash value
45  private List<Expr> partitionExprs_;
46 
47  public DataPartition(TPartitionType type, List<Expr> exprs) {
48  Preconditions.checkNotNull(exprs);
49  Preconditions.checkState(!exprs.isEmpty());
50  Preconditions.checkState(type == TPartitionType.HASH_PARTITIONED
51  || type == TPartitionType.RANGE_PARTITIONED);
52  type_ = type;
53  partitionExprs_ = exprs;
54  }
55 
56  public DataPartition(TPartitionType type) {
57  Preconditions.checkState(type == TPartitionType.UNPARTITIONED
58  || type == TPartitionType.RANDOM);
59  type_ = type;
60  partitionExprs_ = Lists.newArrayList();
61  }
62 
63  public final static DataPartition UNPARTITIONED =
64  new DataPartition(TPartitionType.UNPARTITIONED);
65 
66  public final static DataPartition RANDOM =
67  new DataPartition(TPartitionType.RANDOM);
68 
69  public boolean isPartitioned() { return type_ != TPartitionType.UNPARTITIONED; }
70  public boolean isHashPartitioned() { return type_ == TPartitionType.HASH_PARTITIONED; }
71  public TPartitionType getType() { return type_; }
72  public List<Expr> getPartitionExprs() { return partitionExprs_; }
73 
74  public void substitute(ExprSubstitutionMap smap, Analyzer analyzer) {
75  partitionExprs_ = Expr.substituteList(partitionExprs_, smap, analyzer, false);
76  }
77 
78  public TDataPartition toThrift() {
79  TDataPartition result = new TDataPartition(type_);
80  if (partitionExprs_ != null) {
81  result.setPartition_exprs(Expr.treesToThrift(partitionExprs_));
82  }
83  return result;
84  }
85 
86  @Override
87  public boolean equals(Object obj) {
88  if (obj == null) return false;
89  if (obj.getClass() != this.getClass()) return false;
90  DataPartition other = (DataPartition) obj;
91  if (type_ != other.type_) return false;
92  return Expr.equalLists(partitionExprs_, other.partitionExprs_);
93  }
94 
95  public String debugString() {
96  return Objects.toStringHelper(this)
97  .add("type_", type_)
98  .addValue(Expr.debugString(partitionExprs_))
99  .toString();
100  }
101 
102  public String getExplainString() {
103  StringBuilder str = new StringBuilder();
104  str.append(getPartitionShortName(type_));
105  if (!partitionExprs_.isEmpty()) {
106  List<String> strings = Lists.newArrayList();
107  for (Expr expr: partitionExprs_) {
108  strings.add(expr.toSql());
109  }
110  str.append("(" + Joiner.on(",").join(strings) +")");
111  }
112  return str.toString();
113  }
114 
115  private String getPartitionShortName(TPartitionType partition) {
116  switch (partition) {
117  case RANDOM: return "RANDOM";
118  case HASH_PARTITIONED: return "HASH";
119  case RANGE_PARTITIONED: return "RANGE";
120  case UNPARTITIONED: return "UNPARTITIONED";
121  default: return "";
122  }
123  }
124 }
static final DataPartition UNPARTITIONED
void substitute(ExprSubstitutionMap smap, Analyzer analyzer)
DataPartition(TPartitionType type, List< Expr > exprs)
String getPartitionShortName(TPartitionType partition)