Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
HdfsPartitionFilter.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.ArrayList;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 
35 import com.cloudera.impala.thrift.TColumnValue;
36 import com.cloudera.impala.thrift.TResultRow;
37 import com.google.common.base.Preconditions;
38 import com.google.common.collect.Lists;
39 import com.google.common.collect.Maps;
40 
45 public class HdfsPartitionFilter {
46  private final static Logger LOG = LoggerFactory.getLogger(HdfsPartitionFilter.class);
47 
48  private final Expr predicate_;
49 
50  // lhs exprs of smap used in isMatch()
51  private final ArrayList<SlotRef> lhsSlotRefs_ = Lists.newArrayList();
52  // indices into Table.getColumns()
53  private final ArrayList<Integer> refdKeys_ = Lists.newArrayList();
54 
55  public HdfsPartitionFilter(Expr predicate, HdfsTable tbl, Analyzer analyzer) {
56  predicate_ = predicate;
57 
58  // populate lhsSlotRefs_ and refdKeys_
59  ArrayList<SlotId> refdSlots = Lists.newArrayList();
60  predicate.getIds(null, refdSlots);
61  HashMap<Column, SlotDescriptor> slotDescsByCol = Maps.newHashMap();
62  for (SlotId refdSlot: refdSlots) {
63  SlotDescriptor slotDesc = analyzer.getDescTbl().getSlotDesc(refdSlot);
64  slotDescsByCol.put(slotDesc.getColumn(), slotDesc);
65  }
66 
67  for (int i = 0; i < tbl.getNumClusteringCols(); ++i) {
68  Column col = tbl.getColumns().get(i);
69  SlotDescriptor slotDesc = slotDescsByCol.get(col);
70  if (slotDesc != null) {
71  lhsSlotRefs_.add(new SlotRef(slotDesc));
72  refdKeys_.add(i);
73  }
74  }
75  Preconditions.checkState(lhsSlotRefs_.size() == refdKeys_.size());
76  }
77 
82  public HashSet<Long> getMatchingPartitionIds(ArrayList<HdfsPartition> partitions,
83  Analyzer analyzer) throws InternalException {
84  HashSet<Long> result = new HashSet<Long>();
85  // List of predicates to evaluate
86  ArrayList<Expr> predicates = new ArrayList<Expr>(partitions.size());
87  long[] partitionIds = new long[partitions.size()];
88  int indx = 0;
89  for (HdfsPartition p: partitions) {
90  predicates.add(buildPartitionPredicate(p, analyzer));
91  partitionIds[indx++] = p.getId();
92  }
93  // Evaluate the predicates
94  TResultRow results = FeSupport.EvalPredicateBatch(predicates,
95  analyzer.getQueryCtx());
96  Preconditions.checkState(results.getColValsSize() == partitions.size());
97  indx = 0;
98  for (TColumnValue val: results.getColVals()) {
99  if (val.isBool_val()) result.add(partitionIds[indx]);
100  ++indx;
101  }
102  return result;
103  }
104 
109  private Expr buildPartitionPredicate(HdfsPartition partition, Analyzer analyzer)
110  throws InternalException {
111  // construct smap
113  for (int i = 0; i < refdKeys_.size(); ++i) {
114  sMap.put(
115  lhsSlotRefs_.get(i), partition.getPartitionValues().get(refdKeys_.get(i)));
116  }
117 
118  Expr literalPredicate = predicate_.substitute(sMap, analyzer, false);
119  LOG.trace("buildPartitionPredicate: " + literalPredicate.toSql() + " " +
120  literalPredicate.debugString());
121  Preconditions.checkState(literalPredicate.isConstant());
122  return literalPredicate;
123  }
124 }
HashSet< Long > getMatchingPartitionIds(ArrayList< HdfsPartition > partitions, Analyzer analyzer)
HdfsPartitionFilter(Expr predicate, HdfsTable tbl, Analyzer analyzer)
int SlotId
Definition: global-types.h:24
Expr buildPartitionPredicate(HdfsPartition partition, Analyzer analyzer)