Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TypeDef.java
Go to the documentation of this file.
1 // Copyright 2015 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.Set;
18 
19 import org.apache.hadoop.hive.metastore.MetaStoreUtils;
20 
29 import com.google.common.base.Preconditions;
30 import com.google.common.collect.Sets;
31 
35 public class TypeDef implements ParseNode {
36  private boolean isAnalyzed_;
37  private final Type parsedType_;
38 
39  public TypeDef(Type parsedType) {
40  parsedType_ = parsedType;
41  }
42 
43  @Override
44  public void analyze(Analyzer analyzer) throws AnalysisException {
45  if (isAnalyzed_) return;
46  analyze(parsedType_, analyzer);
47  isAnalyzed_ = true;
48  }
49 
50  private void analyze(Type type, Analyzer analyzer) throws AnalysisException {
51  if (type.isScalarType()) {
52  analyzeScalarType((ScalarType) type, analyzer);
53  } else if (type.isStructType()) {
54  analyzeStructType((StructType) type, analyzer);
55  } else if (type.isArrayType()) {
56  ArrayType arrayType = (ArrayType) type;
57  analyze(arrayType.getItemType(), analyzer);
58  } else {
59  Preconditions.checkState(type.isMapType());
60  analyzeMapType((MapType) type, analyzer);
61  }
62  }
63 
64  private void analyzeScalarType(ScalarType scalarType, Analyzer analyzer)
65  throws AnalysisException {
66  PrimitiveType type = scalarType.getPrimitiveType();
67  switch (type) {
68  case CHAR:
69  case VARCHAR: {
70  String name;
71  int maxLen;
72  if (type == PrimitiveType.VARCHAR) {
73  name = "Varchar";
75  } else if (type == PrimitiveType.CHAR) {
76  name = "Char";
78  } else {
79  Preconditions.checkState(false);
80  return;
81  }
82  int len = scalarType.getLength();
83  if (len <= 0) {
84  throw new AnalysisException(name + " size must be > 0: " + len);
85  }
86  if (scalarType.getLength() > maxLen) {
87  throw new AnalysisException(
88  name + " size must be <= " + maxLen + ": " + len);
89  }
90  break;
91  }
92  case DECIMAL: {
93  int precision = scalarType.decimalPrecision();
94  int scale = scalarType.decimalScale();
95  if (precision > ScalarType.MAX_PRECISION) {
96  throw new AnalysisException("Decimal precision must be <= " +
97  ScalarType.MAX_PRECISION + ": " + precision);
98  }
99  if (precision == 0) {
100  throw new AnalysisException("Decimal precision must be > 0: " + precision);
101  }
102  if (scale > precision) {
103  throw new AnalysisException("Decimal scale (" + scale + ") must be <= " +
104  "precision (" + precision + ")");
105  }
106  }
107  default: break;
108  }
109  }
110 
111  private void analyzeStructType(StructType structType, Analyzer analyzer)
112  throws AnalysisException {
113  // Check for duplicate field names.
114  Set<String> fieldNames = Sets.newHashSet();
115  for (StructField f: structType.getFields()) {
116  analyze(f.getType(), analyzer);
117  if (!fieldNames.add(f.getName().toLowerCase())) {
118  throw new AnalysisException(
119  String.format("Duplicate field name '%s' in struct '%s'",
120  f.getName(), toSql()));
121  }
122  // Check whether the column name meets the Metastore's requirements.
123  if (!MetaStoreUtils.validateName(f.getName().toLowerCase())) {
124  throw new AnalysisException("Invalid struct field name: " + f.getName());
125  }
126  }
127  }
128 
129  private void analyzeMapType(MapType mapType, Analyzer analyzer)
130  throws AnalysisException {
131  analyze(mapType.getKeyType(), analyzer);
132  if (mapType.getKeyType().isComplexType()) {
133  throw new AnalysisException(
134  "Map type cannot have a complex-typed key: " + mapType.toSql());
135  }
136  analyze(mapType.getValueType(), analyzer);
137  }
138 
139  public Type getType() { return parsedType_; }
140 
141  @Override
142  public String toString() { return parsedType_.toSql(); }
143 
144  @Override
145  public String toSql() { return parsedType_.toSql(); }
146 }
void analyzeStructType(StructType structType, Analyzer analyzer)
Definition: TypeDef.java:111
void analyze(Type type, Analyzer analyzer)
Definition: TypeDef.java:50
void analyzeScalarType(ScalarType scalarType, Analyzer analyzer)
Definition: TypeDef.java:64
PrimitiveType
Definition: types.h:27
void analyze(Analyzer analyzer)
Definition: TypeDef.java:44
void analyzeMapType(MapType mapType, Analyzer analyzer)
Definition: TypeDef.java:129
string name
Definition: cpu-info.cc:50