Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PartitionKeyValue.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 
18 import com.google.common.base.Preconditions;
19 
24 public class PartitionKeyValue {
25  // Name of partitioning column.
26  private final String colName_;
27  // Value of partitioning column. Set to null for dynamic inserts.
28  private final Expr value_;
29  // Evaluation of value for static partition keys, null otherwise. Set in analyze().
31 
32  public PartitionKeyValue(String colName, Expr value) {
33  this.colName_ = colName.toLowerCase();
34  this.value_ = value;
35  }
36 
37  public void analyze(Analyzer analyzer) throws AnalysisException {
38  if (isStatic() && !value_.isConstant()) {
39  throw new AnalysisException(
40  String.format("Non-constant expressions are not supported " +
41  "as static partition-key values in '%s'.", toString()));
42  }
43  if (value_ == null) return;
44  value_.analyze(analyzer);
45  literalValue_ = LiteralExpr.create(value_, analyzer.getQueryCtx());
46  }
47 
48  public String getColName() { return colName_; }
49  public Expr getValue() { return value_; }
51  public boolean isDynamic() { return value_ == null; }
52  public boolean isStatic() { return !isDynamic(); }
53 
54  @Override
55  public String toString() {
56  return isStatic() ? colName_ + "=" + value_.toSql() : colName_;
57  }
58 
63  public String toPredicateSql() {
64  String ident = ToSqlUtils.getIdentSql(colName_);
65  if (literalValue_ instanceof NullLiteral ||
66  literalValue_.getStringValue().isEmpty()) {
67  return ident + " IS NULL";
68  }
69  return isStatic() ? ident + "=" + value_.toSql() : ident;
70  }
71 
77  public static String getPartitionKeyValueString(LiteralExpr literalValue,
78  String nullPartitionKeyValue) {
79  Preconditions.checkNotNull(literalValue);
80  if (literalValue instanceof NullLiteral || literalValue.getStringValue().isEmpty()) {
81  return nullPartitionKeyValue;
82  }
83  return literalValue.getStringValue();
84  }
85 }
static String getPartitionKeyValueString(LiteralExpr literalValue, String nullPartitionKeyValue)