Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SelectListItem.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.base.Joiner;
20 import com.google.common.base.Preconditions;
21 
23  private final Expr expr_;
24  private String alias_;
25 
26  // for "[path.]*" (excludes trailing '*')
27  private final List<String> rawPath_;
28  private final boolean isStar_;
29 
30  public SelectListItem(Expr expr, String alias) {
31  super();
32  Preconditions.checkNotNull(expr);
33  expr_ = expr;
34  alias_ = alias;
35  isStar_ = false;
36  rawPath_ = null;
37  }
38 
39  // select list item corresponding to path_to_struct.*
40  static public SelectListItem createStarItem(List<String> rawPath) {
41  return new SelectListItem(rawPath);
42  }
43 
44  private SelectListItem(List<String> path) {
45  super();
46  expr_ = null;
47  isStar_ = true;
48  rawPath_ = path;
49  }
50 
51  public Expr getExpr() { return expr_; }
52  public boolean isStar() { return isStar_; }
53  public String getAlias() { return alias_; }
54  public void setAlias(String alias) { alias_ = alias; }
55  public List<String> getRawPath() { return rawPath_; }
56 
57  @Override
58  public String toString() {
59  if (!isStar_) {
60  Preconditions.checkNotNull(expr_);
61  return expr_.toSql() + ((alias_ != null) ? " " + alias_ : "");
62  } else if (rawPath_ != null) {
63  Preconditions.checkState(isStar_);
64  return Joiner.on(".").join(rawPath_) + ".*";
65  } else {
66  return "*";
67  }
68  }
69 
70  public String toSql() {
71  if (!isStar_) {
72  Preconditions.checkNotNull(expr_);
73  // Enclose aliases in quotes if Hive cannot parse them without quotes.
74  // This is needed for view compatibility between Impala and Hive.
75  String aliasSql = null;
76  if (alias_ != null) aliasSql = ToSqlUtils.getIdentSql(alias_);
77  return expr_.toSql() + ((aliasSql != null) ? " " + aliasSql : "");
78  } else if (rawPath_ != null) {
79  Preconditions.checkState(isStar_);
80  StringBuilder result = new StringBuilder();
81  for (String p: rawPath_) {
82  if (result.length() > 0) result.append(".");
83  result.append(ToSqlUtils.getIdentSql(p.toLowerCase()));
84  }
85  result.append(".*");
86  return result.toString();
87  } else {
88  return "*";
89  }
90  }
91 
104  public String toColumnLabel(int selectListPos, boolean useHiveColLabels) {
105  if (alias_ != null) return alias_.toLowerCase();
106  if (expr_ instanceof SlotRef) {
107  SlotRef slotRef = (SlotRef) expr_;
108  return Joiner.on(".").join(slotRef.getResolvedPath().getRawPath());
109  }
110  // Optionally return auto-generated column label.
111  if (useHiveColLabels) return "_c" + selectListPos;
112  // Abbreviate the toSql() for analytic exprs.
113  if (expr_ instanceof AnalyticExpr) {
114  AnalyticExpr expr = (AnalyticExpr) expr_;
115  return expr.getFnCall().toSql() + " OVER(...)";
116  }
117  return expr_.toSql().toLowerCase();
118  }
119 
120  @Override
122  if (isStar_) return createStarItem(rawPath_);
123  return new SelectListItem(expr_.clone().reset(), alias_);
124  }
125 
126 }
string path("/usr/lib/sasl2:/usr/lib64/sasl2:/usr/local/lib/sasl2:/usr/lib/x86_64-linux-gnu/sasl2")
static SelectListItem createStarItem(List< String > rawPath)
String toColumnLabel(int selectListPos, boolean useHiveColLabels)