15 package com.cloudera.impala.planner;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.Comparator;
20 import java.util.List;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
30 import com.cloudera.impala.common.Pair;
31 import com.cloudera.impala.thrift.TExplainLevel;
32 import com.cloudera.impala.thrift.TExpr;
33 import com.cloudera.impala.thrift.TPlanNode;
34 import com.cloudera.impala.thrift.TPlanNodeType;
35 import com.cloudera.impala.thrift.TUnionNode;
36 import com.google.common.base.Preconditions;
37 import com.google.common.collect.Lists;
44 private final static Logger
LOG = LoggerFactory.getLogger(UnionNode.class);
63 super(
id, Lists.newArrayList(tupleId),
"UNION");
74 resultExprLists_.add(baseTblResultExprs);
75 if (baseTblResultExprs != null) {
78 Preconditions.checkState(tupleIds_.size() == 1,
tupleIds_.size());
84 super.computeStats(analyzer);
89 if (child.cardinality_ > 0) {
98 LOG.debug(
"stats Union: cardinality=" + Long.toString(
cardinality_));
117 "Operands can only be reordered on the fragmented plan.");
120 List<Pair<Long, Integer>> memByChildIdx = Lists.newArrayList();
121 for (
int i = 0; i < children_.size(); ++i) {
123 child.computeCosts(analyzer.getQueryCtx().request.getQuery_options());
124 memByChildIdx.add(
new Pair<Long, Integer>(child.getPerHostMemCost(), i));
127 Collections.sort(memByChildIdx,
128 new Comparator<Pair<Long, Integer>>() {
129 public int compare(Pair<Long, Integer> a, Pair<Long, Integer> b) {
130 PlanNode aNode = children_.get(a.second);
131 PlanNode bNode = children_.get(b.second);
134 if (aNode instanceof ScanNode && !(bNode instanceof ScanNode))
return 1;
135 long diff = b.first - a.first;
136 return (diff < 0 ? -1 : (diff > 0 ? 1 : 0));
140 List<List<Expr>> newResultExprLists = Lists.newArrayList();
141 ArrayList<PlanNode> newChildren = Lists.newArrayList();
142 for (Pair<Long, Integer> p: memByChildIdx) {
143 newResultExprLists.add(resultExprLists_.get(p.second));
144 newChildren.add(children_.get(p.second));
147 children_ = newChildren;
166 materializedResultExprLists_.clear();
167 Preconditions.checkState(resultExprLists_.size() == children_.size());
168 List<SlotDescriptor> slots = analyzer.getDescTbl().getTupleDesc(
tupleId_).getSlots();
169 for (
int i = 0; i < resultExprLists_.size(); ++i) {
170 List<Expr> exprList = resultExprLists_.get(i);
171 List<Expr> newExprList = Lists.newArrayList();
172 for (
int j = 0; j < exprList.size(); ++j) {
173 if (slots.get(j).isMaterialized()) newExprList.add(exprList.get(j));
175 materializedResultExprLists_.add(
176 Expr.substituteList(newExprList, getChild(i).getOutputSmap(), analyzer,
true));
178 Preconditions.checkState(
179 materializedResultExprLists_.size() == getChildren().size());
181 materializedConstExprLists_.clear();
183 List<Expr> newExprList = Lists.newArrayList();
184 for (
int i = 0; i < exprList.size(); ++i) {
185 if (slots.get(i).isMaterialized()) newExprList.add(exprList.get(i));
187 materializedConstExprLists_.add(newExprList);
193 Preconditions.checkState(materializedResultExprLists_.size() == children_.size());
194 List<List<TExpr>> texprLists = Lists.newArrayList();
196 texprLists.add(Expr.treesToThrift(exprList));
198 List<List<TExpr>> constTexprLists = Lists.newArrayList();
200 constTexprLists.add(Expr.treesToThrift(constTexprList));
202 msg.union_node =
new TUnionNode(
tupleId_.asInt(), texprLists, constTexprLists);
203 msg.node_type = TPlanNodeType.UNION_NODE;
208 TExplainLevel detailLevel) {
209 StringBuilder output =
new StringBuilder();
210 output.append(String.format(
"%s%s:%s\n", prefix, id_.toString(),
displayName_));
217 output.append(detailPrefix +
"constant-operands=" + constExprLists_.size() +
"\n");
219 return output.toString();
void computeStats(Analyzer analyzer)
String getNodeExplainString(String prefix, String detailPrefix, TExplainLevel detailLevel)
List< List< Expr > > materializedResultExprLists_
ArrayList< TupleId > tupleIds_
void addChild(PlanNode node, List< Expr > baseTblResultExprs)
String getExplainString()
List< List< Expr > > constExprLists_
List< List< Expr > > resultExprLists_
void reorderOperands(Analyzer analyzer)
void addConstExprList(List< Expr > exprs)
void computeMemLayout(Analyzer analyzer)
void toThrift(TPlanNode msg)
void init(Analyzer analyzer)
List< List< Expr > > materializedConstExprLists_
UnionNode(PlanNodeId id, TupleId tupleId)
static long addCardinalities(long a, long b)