15 package com.cloudera.impala.catalog;
17 import java.io.StringReader;
18 import java.util.ArrayList;
19 import java.util.List;
21 import org.apache.hadoop.hive.metastore.api.FieldSchema;
24 import com.cloudera.impala.analysis.SqlParser;
25 import com.cloudera.impala.analysis.SqlScanner;
27 import com.cloudera.impala.common.Pair;
28 import com.cloudera.impala.thrift.TColumnType;
29 import com.cloudera.impala.thrift.TPrimitiveType;
30 import com.cloudera.impala.thrift.TScalarType;
31 import com.cloudera.impala.thrift.TStructField;
32 import com.cloudera.impala.thrift.TTypeNode;
33 import com.cloudera.impala.thrift.TTypeNodeType;
34 import com.google.common.base.Preconditions;
35 import com.google.common.collect.Lists;
42 public abstract class Type {
75 integerTypes.add(
INT);
81 numericTypes.add(
INT);
83 numericTypes.add(
FLOAT);
88 supportedTypes.add(
NULL);
92 supportedTypes.add(
INT);
93 supportedTypes.add(
BIGINT);
94 supportedTypes.add(
FLOAT);
95 supportedTypes.add(
DOUBLE);
96 supportedTypes.add(
STRING);
98 supportedTypes.add(
CHAR);
119 public abstract String
toSql();
198 throw new IllegalStateException(
"getSlotSize() not implemented for type " +
toSql());
202 TColumnType container =
new TColumnType();
203 container.setTypes(
new ArrayList<TTypeNode>());
211 public abstract void toThrift(TColumnType container);
233 String stmt = String.format(
"CREATE TABLE $DUMMY ($DUMMY %s)", fs.getType());
234 SqlScanner input =
new SqlScanner(
new StringReader(stmt));
235 SqlParser
parser =
new SqlParser(input);
238 Object o = parser.parse().value;
241 throw new IllegalStateException(
"Couldn't parse create table stmt.");
243 createTableStmt = (CreateTableStmt) o;
246 throw new IllegalStateException(
"Invalid create table stmt.");
248 }
catch (Exception e) {
251 TypeDef typeDef = createTableStmt.getColumnDefs().
get(0).getTypeDef();
252 return typeDef.getType();
261 return ScalarType.isImplicitlyCastable(
275 return ScalarType.getAssignmentCompatibleType(
282 return toThrift(Lists.newArrayList(types));
285 public static List<TColumnType>
toThrift(ArrayList<Type> types) {
286 ArrayList<TColumnType> result = Lists.newArrayList();
287 for (
Type t: types) {
288 result.add(t.toThrift());
294 Preconditions.checkState(thrift.types.size() > 0);
295 Pair<Type, Integer> t =
fromThrift(thrift, 0);
296 Preconditions.checkState(t.second.equals(thrift.getTypesSize()));
305 protected static Pair<Type, Integer>
fromThrift(TColumnType col,
int nodeIdx) {
306 TTypeNode node = col.getTypes().
get(nodeIdx);
308 switch (node.getType()) {
310 Preconditions.checkState(node.isSetScalar_type());
311 TScalarType scalarType = node.getScalar_type();
312 if (scalarType.getType() == TPrimitiveType.CHAR) {
313 Preconditions.checkState(scalarType.isSetLen());
314 type = ScalarType.createCharType(scalarType.getLen());
315 }
else if (scalarType.getType() == TPrimitiveType.VARCHAR) {
316 Preconditions.checkState(scalarType.isSetLen());
317 type = ScalarType.createVarcharType(scalarType.getLen());
318 }
else if (scalarType.getType() == TPrimitiveType.DECIMAL) {
319 Preconditions.checkState(scalarType.isSetPrecision()
320 && scalarType.isSetPrecision());
321 type = ScalarType.createDecimalType(scalarType.getPrecision(),
322 scalarType.getScale());
324 type = ScalarType.createType(
325 PrimitiveType.fromThrift(scalarType.getType()));
331 Preconditions.checkState(nodeIdx + 1 < col.getTypesSize());
332 Pair<Type, Integer> childType =
fromThrift(col, nodeIdx + 1);
334 nodeIdx = childType.second;
338 Preconditions.checkState(nodeIdx + 2 < col.getTypesSize());
339 Pair<Type, Integer> keyType =
fromThrift(col, nodeIdx + 1);
340 Pair<Type, Integer> valueType =
fromThrift(col, keyType.second);
341 type =
new MapType(keyType.first, valueType.first);
342 nodeIdx = valueType.second;
346 Preconditions.checkState(nodeIdx + node.getStruct_fieldsSize() < col.getTypesSize());
347 ArrayList<StructField> structFields = Lists.newArrayList();
349 for (
int i = 0; i < node.getStruct_fieldsSize(); ++i) {
350 TStructField thriftField = node.getStruct_fields().
get(i);
351 String
name = thriftField.getName();
352 String comment = null;
353 if (thriftField.isSetComment()) comment = thriftField.getComment();
354 Pair<Type, Integer> res =
fromThrift(col, nodeIdx);
355 nodeIdx = res.second.intValue();
356 structFields.add(
new StructField(name, res.first, comment));
362 return new Pair<Type, Integer>(type, nodeIdx);
370 Preconditions.checkState(ttype.getTypesSize() == 1);
371 Preconditions.checkState(ttype.types.get(0).getType() == TTypeNodeType.SCALAR);
372 return ttype.types.get(0).scalar_type.getType();
391 return Integer.MAX_VALUE;
396 return t.getLength();
424 return t.decimalPrecision();
453 return t.decimalScale();
495 Preconditions.checkArgument(
false,
"Invalid non-scalar type " +
toSql());
500 case NULL_TYPE:
return java.sql.Types.NULL;
501 case BOOLEAN:
return java.sql.Types.BOOLEAN;
502 case TINYINT:
return java.sql.Types.TINYINT;
503 case SMALLINT:
return java.sql.Types.SMALLINT;
504 case INT:
return java.sql.Types.INTEGER;
505 case BIGINT:
return java.sql.Types.BIGINT;
506 case FLOAT:
return java.sql.Types.FLOAT;
507 case DOUBLE:
return java.sql.Types.DOUBLE;
508 case TIMESTAMP:
return java.sql.Types.TIMESTAMP;
509 case STRING:
return java.sql.Types.VARCHAR;
510 case CHAR:
return java.sql.Types.CHAR;
511 case VARCHAR:
return java.sql.Types.VARCHAR;
512 case BINARY:
return java.sql.Types.BINARY;
513 case DECIMAL:
return java.sql.Types.DECIMAL;
515 Preconditions.checkArgument(
false,
"Invalid primitive type " +
516 t.getPrimitiveType().
name());
boolean isWildcardDecimal()
static List< TColumnType > toThrift(Type[] types)
boolean isFullySpecifiedDecimal()
Integer getNumPrecRadix()
static ArrayList< ScalarType > getIntegerTypes()
static final ScalarType NULL
static final ScalarType BIGINT
static final ScalarType DATE
static final ScalarType CHAR
static final ScalarType VARCHAR
static final ScalarType STRING
static ScalarType createDecimalTypeInternal(int precision, int scale)
PrimitiveType getPrimitiveType()
static Type parseColumnType(FieldSchema fs)
static List< TColumnType > toThrift(ArrayList< Type > types)
static ArrayList< ScalarType > numericTypes
List< ColumnDef > getColumnDefs()
static final ScalarType BOOLEAN
static final ScalarType DEFAULT_DECIMAL
static ArrayList< ScalarType > integerTypes
static Type fromThrift(TColumnType thrift)
boolean matchesType(Type t)
boolean supportsTablePartitioning()
static final int DEFAULT_PRECISION
static boolean isImplicitlyCastable(Type t1, Type t2)
static final ScalarType SMALLINT
static ScalarType createCharType(int len)
static final ScalarType FLOAT
static ArrayList< ScalarType > supportedTypes
static PrimitiveType[][] compatibilityMatrix
TPrimitiveType getTPrimitiveType(TColumnType ttype)
static Pair< Type, Integer > fromThrift(TColumnType col, int nodeIdx)
boolean isFloatingPointType()
boolean isCollectionType()
static final ScalarType DOUBLE
boolean isWildcardVarchar()
static final int DEFAULT_SCALE
PrimitiveType getPrimitiveType()
static final ScalarType TINYINT
static ArrayList< ScalarType > getNumericTypes()
static final ScalarType INT
static final ScalarType DECIMAL
boolean isFixedLengthType()
static ScalarType createDecimalType()
boolean isDecimalOrNull()
Integer getDecimalDigits()
boolean isFixedPointType()
static final ScalarType DEFAULT_VARCHAR
static final ScalarType DATETIME
static final ScalarType INVALID
static final ScalarType BINARY
static final ScalarType TIMESTAMP
boolean isScalarType(PrimitiveType t)
static ArrayList< ScalarType > getSupportedTypes()
static Type getAssignmentCompatibleType(Type t1, Type t2)