Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SortInfo.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.analysis;
16 
17 import java.util.List;
18 
19 import com.google.common.base.Preconditions;
20 import com.google.common.collect.Lists;
21 
29 public class SortInfo {
30  private List<Expr> orderingExprs_;
31  private final List<Boolean> isAscOrder_;
32  // True if "NULLS FIRST", false if "NULLS LAST", null if not specified.
33  private final List<Boolean> nullsFirstParams_;
34  // The single tuple that is materialized, sorted and output by a sort operator
35  // (i.e. SortNode or TopNNode)
37  // Input expressions materialized into sortTupleDesc_. One expr per slot in
38  // sortTupleDesc_.
39  private List<Expr> sortTupleSlotExprs_;
40 
41  public SortInfo(List<Expr> orderingExprs, List<Boolean> isAscOrder,
42  List<Boolean> nullsFirstParams) {
43  Preconditions.checkArgument(orderingExprs.size() == isAscOrder.size());
44  Preconditions.checkArgument(orderingExprs.size() == nullsFirstParams.size());
45  orderingExprs_ = orderingExprs;
46  isAscOrder_ = isAscOrder;
47  nullsFirstParams_ = nullsFirstParams;
48  }
49 
51  TupleDescriptor tupleDesc, List<Expr> tupleSlotExprs) {
52  sortTupleDesc_ = tupleDesc;
53  sortTupleSlotExprs_ = tupleSlotExprs;
54  for (int i = 0; i < sortTupleDesc_.getSlots().size(); ++i) {
55  SlotDescriptor slotDesc = sortTupleDesc_.getSlots().get(i);
56  slotDesc.setSourceExpr(sortTupleSlotExprs_.get(i));
57  }
58  }
59  public List<Expr> getOrderingExprs() { return orderingExprs_; }
60  public List<Boolean> getIsAscOrder() { return isAscOrder_; }
61  public List<Boolean> getNullsFirstParams() { return nullsFirstParams_; }
62  public List<Expr> getSortTupleSlotExprs() { return sortTupleSlotExprs_; }
64 
69  public List<Boolean> getNullsFirst() {
70  List<Boolean> nullsFirst = Lists.newArrayList();
71  for (int i = 0; i < orderingExprs_.size(); ++i) {
72  nullsFirst.add(OrderByElement.nullsFirst(nullsFirstParams_.get(i),
73  isAscOrder_.get(i)));
74  }
75  return nullsFirst;
76  }
77 
84  Preconditions.checkNotNull(sortTupleDesc_);
85  Preconditions.checkNotNull(sortTupleSlotExprs_);
86  Preconditions.checkState(sortTupleDesc_.isMaterialized());
87  analyzer.materializeSlots(orderingExprs_);
88  List<SlotDescriptor> sortTupleSlotDescs = sortTupleDesc_.getSlots();
89  List<Expr> materializedExprs = Lists.newArrayList();
90  for (int i = 0; i < sortTupleSlotDescs.size(); ++i) {
91  if (sortTupleSlotDescs.get(i).isMaterialized()) {
92  materializedExprs.add(sortTupleSlotExprs_.get(i));
93  }
94  }
95  List<Expr> substMaterializedExprs =
96  Expr.substituteList(materializedExprs, smap, analyzer, false);
97  analyzer.materializeSlots(substMaterializedExprs);
98  }
99 
101  orderingExprs_ = Expr.substituteList(orderingExprs_, smap, analyzer, false);
102  }
103 
107  public void checkConsistency() {
108  for (Expr orderingExpr: orderingExprs_) {
109  Preconditions.checkState(orderingExpr.isBound(sortTupleDesc_.getId()));
110  }
111  }
112 }
void materializeRequiredSlots(Analyzer analyzer, ExprSubstitutionMap smap)
Definition: SortInfo.java:83
void setMaterializedTupleInfo(TupleDescriptor tupleDesc, List< Expr > tupleSlotExprs)
Definition: SortInfo.java:50
final List< Boolean > nullsFirstParams_
Definition: SortInfo.java:33
final List< Boolean > isAscOrder_
Definition: SortInfo.java:31
void substituteOrderingExprs(ExprSubstitutionMap smap, Analyzer analyzer)
Definition: SortInfo.java:100
List< Boolean > getNullsFirstParams()
Definition: SortInfo.java:61
TupleDescriptor getSortTupleDescriptor()
Definition: SortInfo.java:63
SortInfo(List< Expr > orderingExprs, List< Boolean > isAscOrder, List< Boolean > nullsFirstParams)
Definition: SortInfo.java:41