15 package com.cloudera.impala.analysis;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.ListIterator;
25 import com.cloudera.impala.common.TreeNode;
26 import com.google.common.base.Preconditions;
27 import com.google.common.base.Predicates;
28 import com.google.common.collect.Lists;
29 import com.google.common.collect.Sets;
93 super.analyze(analyzer);
94 this.analyzer_ = analyzer;
104 limitElement_.analyze(analyzer);
122 ArrayList<Expr> orderingExprs = Lists.newArrayList();
123 ArrayList<Boolean> isAscOrder = Lists.newArrayList();
124 ArrayList<Boolean> nullsFirstParams = Lists.newArrayList();
128 if (orderByElement.getExpr().contains(Predicates.instanceOf(Subquery.class))) {
130 "Subqueries are not supported in the ORDER BY clause.");
134 orderingExprs.add(orderByElement.getExpr().
clone());
135 isAscOrder.add(Boolean.valueOf(orderByElement.isAsc()));
136 nullsFirstParams.add(orderByElement.getNullsFirstParam());
140 if (ambiguousAlias != null) {
142 "' in ORDER BY clause is ambiguous");
144 orderingExprs = Expr.trySubstituteList(orderingExprs,
aliasSmap_, analyzer,
false);
148 " in nested queries.");
157 StringBuilder strBuilder =
new StringBuilder();
158 strBuilder.append(
"Ignoring ORDER BY clause without LIMIT or OFFSET: ");
159 strBuilder.append(
"ORDER BY ");
160 strBuilder.append(orderByElements_.get(0).
toSql());
161 for (
int i = 1; i < orderByElements_.size(); ++i) {
162 strBuilder.append(
", ").append(orderByElements_.get(i).toSql());
164 strBuilder.append(
".\nAn ORDER BY appearing in a view, subquery, union operand, ");
165 strBuilder.append(
"or an insert/ctas statement has no effect on the query result ");
166 strBuilder.append(
"unless a LIMIT and/or OFFSET is used in conjunction ");
167 strBuilder.append(
"with the ORDER BY.");
168 analyzer.addWarning(strBuilder.toString());
189 Set<SlotRef> sourceSlots = Sets.newHashSet();
190 TreeNode.collect(
resultExprs_, Predicates.instanceOf(SlotRef.class), sourceSlots);
191 TreeNode.collect(sortInfo_.getOrderingExprs(), Predicates.instanceOf(
SlotRef.class),
194 TupleDescriptor sortTupleDesc = analyzer.getDescTbl().createTupleDescriptor(
"sort");
195 List<Expr> sortTupleExprs = Lists.newArrayList();
196 sortTupleDesc.setIsMaterialized(
true);
200 for (
SlotRef origSlotRef: sourceSlots) {
203 analyzer.copySlotDescriptor(origSlotDesc, sortTupleDesc);
205 substOrderBy.put(origSlotRef, cloneRef);
206 analyzer.createAuxEquivPredicate(cloneRef, origSlotRef);
207 sortTupleExprs.add(origSlotRef);
210 resultExprs_ = Expr.substituteList(
resultExprs_, substOrderBy, analyzer,
false);
211 sortInfo_.substituteOrderingExprs(substOrderBy, analyzer);
212 sortInfo_.setMaterializedTupleInfo(sortTupleDesc, sortTupleExprs);
220 for (
Expr exp: exprs) {
233 ListIterator<Expr> i = exprs.listIterator();
234 while (i.hasNext()) {
235 Expr expr = i.next();
237 expr.analyze(analyzer);
238 if (!expr.
getType().isIntegerType())
continue;
239 long pos = ((NumericLiteral) expr).getLongValue();
242 errorPrefix +
": ordinal must be >= 1: " + expr.
toSql());
246 errorPrefix +
": ordinal exceeds number of items in select list: "
250 i.set(resultExprs_.get((int) pos - 1).clone());
273 public boolean hasLimit() {
return limitElement_.getLimitExpr() != null; }
274 public long getLimit() {
return limitElement_.getLimit(); }
275 public boolean hasOffset() {
return limitElement_.getOffsetExpr() != null; }
276 public long getOffset() {
return limitElement_.getOffset(); }
282 Preconditions.checkState(limit >= 0);
304 List<SlotId> slotIds = Lists.newArrayList();
305 for (
Expr e: exprs) {
306 e.getIds(null, slotIds);
308 analyzer.getDescTbl().markSlotsMaterialized(slotIds);
316 return withClause_ != null ? withClause_.clone() : null;
WithClause getWithClause()
abstract QueryStmt clone()
void substituteOrdinals(List< Expr > exprs, String errorPrefix, Analyzer analyzer)
static final ScalarType BIGINT
ArrayList< OrderByElement > cloneOrderByElements()
abstract void materializeRequiredSlots(Analyzer analyzer)
ArrayList< Expr > resultExprs_
LimitElement limitElement_
void createSortTupleInfo(Analyzer analyzer)
ArrayList< Expr > getResultExprs()
boolean evaluateOrderBy()
boolean hasOrderByClause()
final ExprSubstitutionMap aliasSmap_
Expr getFirstAmbiguousAlias(List< Expr > exprs)
ArrayList< Expr > baseTblResultExprs_
void materializeSlots(Analyzer analyzer, List< Expr > exprs)
void analyze(Analyzer analyzer)
ArrayList< Expr > getBaseTblResultExprs()
ArrayList< OrderByElement > orderByElements_
abstract void getMaterializedTupleIds(ArrayList< TupleId > tupleIdList)
WithClause cloneWithClause()
void setWithClause(WithClause withClause)
void createSortInfo(Analyzer analyzer)
void analyzeLimit(Analyzer analyzer)
abstract ArrayList< String > getColLabels()
void setLimit(long limit)
final ArrayList< Expr > ambiguousAliasList_
QueryStmt(ArrayList< OrderByElement > orderByElements, LimitElement limitElement)