Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
AnalyticEvalNode.java
Go to the documentation of this file.
1 // Copyright 2014 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 
30 import com.cloudera.impala.thrift.TAnalyticNode;
31 import com.cloudera.impala.thrift.TExplainLevel;
32 import com.cloudera.impala.thrift.TPlanNode;
33 import com.cloudera.impala.thrift.TPlanNodeType;
34 import com.cloudera.impala.thrift.TQueryOptions;
35 import com.google.common.base.Joiner;
36 import com.google.common.base.Objects;
37 import com.google.common.base.Preconditions;
38 import com.google.common.collect.Lists;
39 
43 public class AnalyticEvalNode extends PlanNode {
44  private final static Logger LOG = LoggerFactory.getLogger(AnalyticEvalNode.class);
45 
46  // List of tuple ids materialized by the originating SelectStmt (i.e., what is returned
47  // by SelectStmt.getMaterializedTupleIds()).
48  // Needed for getting unassigned conjuncts from the analyzer.
49  private final List<TupleId> stmtTupleIds_;
50 
51  private List<Expr> analyticFnCalls_;
52 
53  // Partitioning exprs from the AnalyticInfo
54  private final List<Expr> partitionExprs_;
55 
56  // TODO: Remove when the BE uses partitionByLessThan rather than the exprs
57  private List<Expr> substitutedPartitionExprs_;
58  private List<OrderByElement> orderByElements_;
60 
61  // Logical output tuple for the analytic exprs of the originating stmt.
63 
64  // Physical tuples used/produced by this analytic node.
67 
68  // maps from the logical output slots in logicalTupleDesc_ to their corresponding
69  // physical output slots in outputTupleDesc_
71 
72  // predicates constructed from partitionExprs_/orderingExprs_ to
73  // compare input to buffered tuples
74  private final Expr partitionByEq_;
75  private final Expr orderByEq_;
77 
79  PlanNodeId id, PlanNode input, List<TupleId> stmtTupleIds,
80  List<Expr> analyticFnCalls, List<Expr> partitionExprs,
81  List<OrderByElement> orderByElements, AnalyticWindow analyticWindow,
82  TupleDescriptor logicalTupleDesc, TupleDescriptor intermediateTupleDesc,
83  TupleDescriptor outputTupleDesc, ExprSubstitutionMap logicalToPhysicalSmap,
84  Expr partitionByEq, Expr orderByEq, TupleDescriptor bufferedTupleDesc) {
85  super(id, input.getTupleIds(), "ANALYTIC");
86  Preconditions.checkState(!tupleIds_.contains(outputTupleDesc.getId()));
87  // we're materializing the input row augmented with the analytic output tuple
88  tupleIds_.add(outputTupleDesc.getId());
89  stmtTupleIds_ = stmtTupleIds;
90  analyticFnCalls_ = analyticFnCalls;
91  partitionExprs_ = partitionExprs;
92  orderByElements_ = orderByElements;
93  analyticWindow_ = analyticWindow;
94  logicalTupleDesc_ = logicalTupleDesc;
95  intermediateTupleDesc_ = intermediateTupleDesc;
96  outputTupleDesc_ = outputTupleDesc;
97  logicalToPhysicalSmap_ = logicalToPhysicalSmap;
98  partitionByEq_ = partitionByEq;
99  orderByEq_ = orderByEq;
100  bufferedTupleDesc_ = bufferedTupleDesc;
101  children_.add(input);
102  nullableTupleIds_.addAll(input.getNullableTupleIds());
103  }
104 
105  @Override
106  public boolean isBlockingNode() { return true; }
107  public List<Expr> getPartitionExprs() { return partitionExprs_; }
108  public List<OrderByElement> getOrderByElements() { return orderByElements_; }
109 
110  @Override
111  public void init(Analyzer analyzer) throws InternalException {
112  computeMemLayout(analyzer);
113  intermediateTupleDesc_.computeMemLayout();
114 
115  // we add the analyticInfo's smap to the combined smap of our child
117  createDefaultSmap(analyzer);
118 
119  // Do not assign any conjuncts here: the conjuncts out of our SelectStmt's
120  // Where clause have already been assigned, and conjuncts coming out of an
121  // enclosing scope need to be evaluated *after* all analytic computations.
122 
123  // do this at the end so it can take all conjuncts into account
124  computeStats(analyzer);
125 
126  LOG.trace("desctbl: " + analyzer.getDescTbl().debugString());
127 
128  // point fn calls, partition and ordering exprs at our input
130  analyticFnCalls_ = Expr.substituteList(analyticFnCalls_, childSmap, analyzer, false);
131  substitutedPartitionExprs_ = Expr.substituteList(partitionExprs_, childSmap,
132  analyzer, false);
133  orderByElements_ = OrderByElement.substitute(orderByElements_, childSmap, analyzer);
134  LOG.trace("evalnode: " + debugString());
135  }
136 
137  @Override
138  protected void computeStats(Analyzer analyzer) {
139  super.computeStats(analyzer);
140  cardinality_ = getChild(0).cardinality_;
141  }
142 
143  @Override
144  protected String debugString() {
145  List<String> orderByElementStrs = Lists.newArrayList();
146  for (OrderByElement element: orderByElements_) {
147  orderByElementStrs.add(element.toSql());
148  }
149  return Objects.toStringHelper(this)
150  .add("analyticFnCalls", Expr.debugString(analyticFnCalls_))
151  .add("partitionExprs", Expr.debugString(partitionExprs_))
152  .add("subtitutedPartitionExprs", Expr.debugString(substitutedPartitionExprs_))
153  .add("orderByElements", Joiner.on(", ").join(orderByElementStrs))
154  .add("window", analyticWindow_)
155  .add("intermediateTid", intermediateTupleDesc_.getId())
156  .add("outputTid", outputTupleDesc_.getId())
157  .add("partitionByEq",
158  partitionByEq_ != null ? partitionByEq_.debugString() : "null")
159  .add("orderByEq",
160  orderByEq_ != null ? orderByEq_.debugString() : "null")
161  .addValue(super.debugString())
162  .toString();
163  }
164 
165  @Override
166  protected void toThrift(TPlanNode msg) {
167  msg.node_type = TPlanNodeType.ANALYTIC_EVAL_NODE;
168  msg.analytic_node = new TAnalyticNode();
169  msg.analytic_node.setIntermediate_tuple_id(intermediateTupleDesc_.getId().asInt());
170  msg.analytic_node.setOutput_tuple_id(outputTupleDesc_.getId().asInt());
171  msg.analytic_node.setPartition_exprs(Expr.treesToThrift(substitutedPartitionExprs_));
172  msg.analytic_node.setOrder_by_exprs(
173  Expr.treesToThrift(OrderByElement.getOrderByExprs(orderByElements_)));
174  msg.analytic_node.setAnalytic_functions(Expr.treesToThrift(analyticFnCalls_));
175  if (analyticWindow_ == null) {
176  if (!orderByElements_.isEmpty()) {
177  msg.analytic_node.setWindow(AnalyticWindow.DEFAULT_WINDOW.toThrift());
178  }
179  } else {
180  // TODO: Window boundaries should have range_offset_predicate set
181  msg.analytic_node.setWindow(analyticWindow_.toThrift());
182  }
183  if (partitionByEq_ != null) {
184  msg.analytic_node.setPartition_by_eq(partitionByEq_.treeToThrift());
185  }
186  if (orderByEq_ != null) {
187  msg.analytic_node.setOrder_by_eq(orderByEq_.treeToThrift());
188  }
189  if (bufferedTupleDesc_ != null) {
190  msg.analytic_node.setBuffered_tuple_id(bufferedTupleDesc_.getId().asInt());
191  }
192  }
193 
194  @Override
195  protected String getNodeExplainString(String prefix, String detailPrefix,
196  TExplainLevel detailLevel) {
197  StringBuilder output = new StringBuilder();
198  output.append(String.format("%s%s", prefix, getDisplayLabel()));
199  output.append("\n");
200  if (detailLevel.ordinal() >= TExplainLevel.STANDARD.ordinal()) {
201  output.append(detailPrefix + "functions: ");
202  List<String> strings = Lists.newArrayList();
203  for (Expr fnCall: analyticFnCalls_) {
204  strings.add(fnCall.toSql());
205  }
206  output.append(Joiner.on(", ").join(strings));
207  output.append("\n");
208 
209  if (!partitionExprs_.isEmpty()) {
210  output.append(detailPrefix + "partition by: ");
211  strings.clear();
212  for (Expr partitionExpr: partitionExprs_) {
213  strings.add(partitionExpr.toSql());
214  }
215  output.append(Joiner.on(", ").join(strings));
216  output.append("\n");
217  }
218 
219  if (!orderByElements_.isEmpty()) {
220  output.append(detailPrefix + "order by: ");
221  strings.clear();
222  for (OrderByElement element: orderByElements_) {
223  strings.add(element.toSql());
224  }
225  output.append(Joiner.on(", ").join(strings));
226  output.append("\n");
227  }
228 
229  if (analyticWindow_ != null) {
230  output.append(detailPrefix + "window: ");
231  output.append(analyticWindow_.toSql());
232  output.append("\n");
233  }
234 
235  if (!conjuncts_.isEmpty()) {
236  output.append(
237  detailPrefix + "predicates: " + getExplainString(conjuncts_) + "\n");
238  }
239  }
240  return output.toString();
241  }
242 
243  @Override
244  public void computeCosts(TQueryOptions queryOptions) {
245  Preconditions.checkNotNull(fragment_,
246  "PlanNode must be placed into a fragment before calling this method.");
247  // TODO: come up with estimate based on window
248  perHostMemCost_ = 0;
249  }
250 }
AnalyticEvalNode(PlanNodeId id, PlanNode input, List< TupleId > stmtTupleIds, List< Expr > analyticFnCalls, List< Expr > partitionExprs, List< OrderByElement > orderByElements, AnalyticWindow analyticWindow, TupleDescriptor logicalTupleDesc, TupleDescriptor intermediateTupleDesc, TupleDescriptor outputTupleDesc, ExprSubstitutionMap logicalToPhysicalSmap, Expr partitionByEq, Expr orderByEq, TupleDescriptor bufferedTupleDesc)
ExprSubstitutionMap getCombinedChildSmap()
Definition: PlanNode.java:410
int TupleId
Definition: global-types.h:23
ArrayList< TupleId > getTupleIds()
Definition: PlanNode.java:196
void createDefaultSmap(Analyzer analyzer)
Definition: PlanNode.java:425
void computeMemLayout(Analyzer analyzer)
Definition: PlanNode.java:475
String getNodeExplainString(String prefix, String detailPrefix, TExplainLevel detailLevel)
void computeCosts(TQueryOptions queryOptions)
ExprSubstitutionMap outputSmap_
Definition: PlanNode.java:93