15 package com.cloudera.impala.planner;
17 import java.util.List;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
30 import com.cloudera.impala.thrift.TExplainLevel;
31 import com.cloudera.impala.thrift.TPlanNode;
32 import com.cloudera.impala.thrift.TPlanNodeType;
33 import com.cloudera.impala.thrift.TQueryOptions;
34 import com.cloudera.impala.thrift.TSortInfo;
35 import com.cloudera.impala.thrift.TSortNode;
36 import com.google.common.base.Joiner;
37 import com.google.common.base.Objects;
38 import com.google.common.base.Preconditions;
39 import com.google.common.collect.Lists;
48 private final static Logger
LOG = LoggerFactory.getLogger(SortNode.class);
98 List<SlotDescriptor> sortTupleSlots = info_.getSortTupleDescriptor().getSlots();
99 List<Expr> slotExprs = info_.getSortTupleSlotExprs();
100 Preconditions.checkState(sortTupleSlots.size() == slotExprs.size());
103 for (
int i = 0; i < slotExprs.size(); ++i) {
104 if (!sortTupleSlots.get(i).isMaterialized())
continue;
105 resolvedTupleExprs_.add(slotExprs.get(i));
106 outputSmap_.put(slotExprs.get(i),
new SlotRef(sortTupleSlots.get(i)));
109 resolvedTupleExprs_ =
116 outputSmap_ = ExprSubstitutionMap.compose(childSmap,
outputSmap_, analyzer);
118 info_.substituteOrderingExprs(
outputSmap_, analyzer);
119 info_.checkConsistency();
121 LOG.trace(
"sort id " + tupleIds_.get(0).toString() +
" smap: "
122 + outputSmap_.debugString());
128 super.computeStats(analyzer);
130 LOG.debug(
"stats Sort: cardinality=" + Long.toString(
cardinality_));
135 List<String> strings = Lists.newArrayList();
137 strings.add(isAsc ?
"a" :
"d");
139 return Objects.toStringHelper(
this)
141 .add(
"is_asc",
"[" + Joiner.on(
" ").join(strings) +
"]")
142 .add(
"nulls_first",
"[" + Joiner.on(
" ").join(info_.getNullsFirst()) +
"]")
144 .addValue(super.debugString())
150 msg.node_type = TPlanNodeType.SORT_NODE;
153 Preconditions.checkState(tupleIds_.size() == 1,
154 "Incorrect size for tupleIds_ in SortNode");
156 TSortNode sort_node =
new TSortNode(sort_info,
useTopN_);
158 msg.sort_node = sort_node;
163 TExplainLevel detailLevel) {
164 StringBuilder output =
new StringBuilder();
165 output.append(String.format(
"%s%s:%s%s\n", prefix, id_.toString(),
167 if (detailLevel.ordinal() >= TExplainLevel.STANDARD.ordinal()) {
168 output.append(detailPrefix +
"order by: ");
169 for (
int i = 0; i < info_.getOrderingExprs().size(); ++i) {
170 if (i > 0) output.append(
", ");
171 output.append(info_.getOrderingExprs().
get(i).toSql() +
" ");
172 output.append(info_.getIsAscOrder().
get(i) ?
"ASC" :
"DESC");
174 Boolean nullsFirstParam = info_.getNullsFirstParams().
get(i);
175 if (nullsFirstParam != null) {
176 output.append(nullsFirstParam ?
" NULLS FIRST" :
" NULLS LAST");
181 return output.toString();
187 return String.format(
" [LIMIT=%s OFFSET=%s]",
limit_,
offset_);
189 return String.format(
" [LIMIT=%s]",
limit_);
195 return offset_ != 0 ? prefix +
"offset: " + Long.toString(
offset_) +
"\n" :
"";
211 double fullInputSize = getChild(0).cardinality_ *
avgRowSize_;
212 boolean hasVarLenSlots =
false;
214 if (slotDesc.isMaterialized() && !slotDesc.getType().isFixedLengthType()) {
215 hasVarLenSlots =
true;
221 long blockSize = BackendConfig.INSTANCE.getReadSize();
225 if (hasVarLenSlots) blockSize *= 2;
226 double numInputBlocks = Math.ceil(fullInputSize / blockSize);
227 perHostMemCost_ = blockSize * (long) Math.ceil(Math.sqrt(numInputBlocks));
void computeStats(Analyzer analyzer)
static String getDisplayName(boolean isTopN, boolean isMergeOnly)
DataPartition inputPartition_
List< Boolean > getIsAscOrder()
static List< TExpr > treesToThrift(List<?extends Expr > exprs)
String getNodeExplainDetail(TExplainLevel detailLevel)
void setInputPartition(DataPartition inputPartition)
void assignConjuncts(Analyzer analyzer)
void computeCosts(TQueryOptions queryOptions)
DataPartition getInputPartition()
ExprSubstitutionMap getCombinedChildSmap()
List< Expr > resolvedTupleExprs_
SortNode(PlanNodeId id, PlanNode input, SortInfo info, boolean useTopN, long offset)
void init(Analyzer analyzer)
void setOffset(long offset)
String getNodeExplainString(String prefix, String detailPrefix, TExplainLevel detailLevel)
TupleDescriptor getSortTupleDescriptor()
void computeMemLayout(Analyzer analyzer)
String getOffsetExplainString(String prefix)
long capAtLimit(long cardinality)
uint8_t offset[7 *64-sizeof(uint64_t)]
void setIsAnalyticSort(boolean v)
List< Expr > getOrderingExprs()
void toThrift(TPlanNode msg)
ExprSubstitutionMap outputSmap_