Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
OrderByElement.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.collect.Lists;
20 
21 
25 public class OrderByElement {
26  private Expr expr_;
27  private final boolean isAsc_;
28  // Represents the NULLs ordering specified: true when "NULLS FIRST", false when
29  // "NULLS LAST", and null if not specified.
30  private final Boolean nullsFirstParam_;
31 
38  public OrderByElement(Expr expr, boolean isAsc, Boolean nullsFirstParam) {
39  super();
40  this.expr_ = expr;
41  this.isAsc_ = isAsc;
42  this.nullsFirstParam_ = nullsFirstParam;
43  }
44 
45  public Expr getExpr() { return expr_; }
46  public void setExpr(Expr e) { expr_ = e; }
47  public boolean isAsc() { return isAsc_; }
48  public Boolean getNullsFirstParam() { return nullsFirstParam_; }
49  public boolean nullsFirst() { return nullsFirst(nullsFirstParam_, isAsc_); }
50 
51  public String toSql() {
52  StringBuilder strBuilder = new StringBuilder();
53  strBuilder.append(expr_.toSql());
54  strBuilder.append(isAsc_ ? " ASC" : " DESC");
55  // When ASC and NULLS LAST or DESC and NULLS FIRST, we do not print NULLS FIRST/LAST
56  // because it is the default behavior and we want to avoid printing NULLS FIRST/LAST
57  // whenever possible as it is incompatible with Hive (SQL compatibility with Hive is
58  // important for views).
59  if (nullsFirstParam_ != null) {
60  if (isAsc_ && nullsFirstParam_) {
61  // If ascending, nulls are last by default, so only add if nulls first.
62  strBuilder.append(" NULLS FIRST");
63  } else if (!isAsc_ && !nullsFirstParam_) {
64  // If descending, nulls are first by default, so only add if nulls last.
65  strBuilder.append(" NULLS LAST");
66  }
67  }
68  return strBuilder.toString();
69  }
70 
71  @Override
72  public boolean equals(Object obj) {
73  if (obj == null) return false;
74  if (obj.getClass() != this.getClass()) return false;
76  boolean nullsFirstEqual =
77  (nullsFirstParam_ == null) == (o.nullsFirstParam_ == null);
78  if (nullsFirstParam_ != null && nullsFirstEqual) {
79  nullsFirstEqual = nullsFirstParam_.equals(o.nullsFirstParam_);
80  }
81  return expr_.equals(o.expr_) && isAsc_ == o.isAsc_ && nullsFirstEqual;
82  }
83 
84  @Override
85  public OrderByElement clone() {
87  expr_.clone(), isAsc_,
88  nullsFirstParam_ != null ? new Boolean(nullsFirstParam_.booleanValue()) : null);
89  return clone;
90  }
91 
92 
102  public static boolean nullsFirst(Boolean nullsFirstParam, boolean isAsc) {
103  return nullsFirstParam == null ? !isAsc : nullsFirstParam;
104  }
105 
110  public static List<OrderByElement> substitute(List<OrderByElement> src,
111  ExprSubstitutionMap smap, Analyzer analyzer) {
112  List<OrderByElement> result = Lists.newArrayListWithCapacity(src.size());
113  for (OrderByElement element: src) {
114  result.add(new OrderByElement(element.getExpr().substitute(smap, analyzer, false),
115  element.isAsc_, element.nullsFirstParam_));
116  }
117  return result;
118  }
119 
123  public static List<Expr> getOrderByExprs(List<OrderByElement> src) {
124  List<Expr> result = Lists.newArrayListWithCapacity(src.size());
125  for (OrderByElement element: src) {
126  result.add(element.getExpr());
127  }
128  return result;
129  }
130 
135  public static List<OrderByElement> reverse(List<OrderByElement> src) {
136  List<OrderByElement> result = Lists.newArrayListWithCapacity(src.size());
137  for (int i = 0; i < src.size(); ++i) {
138  OrderByElement element = src.get(i);
139  OrderByElement reverseElement =
140  new OrderByElement(element.getExpr().clone(), !element.isAsc_,
141  Boolean.valueOf(!nullsFirst(element.nullsFirstParam_, element.isAsc_)));
142  result.add(reverseElement);
143  }
144  return result;
145  }
146 }
static List< Expr > getOrderByExprs(List< OrderByElement > src)
static List< OrderByElement > substitute(List< OrderByElement > src, ExprSubstitutionMap smap, Analyzer analyzer)
OrderByElement(Expr expr, boolean isAsc, Boolean nullsFirstParam)
static boolean nullsFirst(Boolean nullsFirstParam, boolean isAsc)
static List< OrderByElement > reverse(List< OrderByElement > src)