15 package com.cloudera.impala.util;
17 import static org.apache.avro.Schema.Type.BOOLEAN;
18 import static org.apache.avro.Schema.Type.DOUBLE;
19 import static org.apache.avro.Schema.Type.FLOAT;
20 import static org.apache.avro.Schema.Type.INT;
21 import static org.apache.avro.Schema.Type.LONG;
22 import static org.apache.avro.Schema.Type.STRING;
24 import java.util.Collections;
25 import java.util.Hashtable;
26 import java.util.List;
29 import org.apache.avro.Schema;
30 import org.apache.avro.SchemaParseException;
31 import org.codehaus.jackson.JsonNode;
36 import com.google.common.base.Preconditions;
37 import com.google.common.collect.Lists;
48 Map<Schema.Type,
Type> typeMap =
new Hashtable<Schema.Type,
Type>();
65 public static List<Column>
parse(String schemaStr)
throws SchemaParseException {
66 Schema.Parser avroSchemaParser =
new Schema.Parser();
67 Schema schema = avroSchemaParser.parse(schemaStr);
68 if (!schema.getType().equals(Schema.Type.RECORD)) {
69 throw new UnsupportedOperationException(
"Schema for table must be of type " +
70 "RECORD. Received type: " + schema.getType());
72 List<Column> cols = Lists.newArrayList();
73 for (
int i = 0; i < schema.getFields().size(); ++i) {
74 Schema.Field field = schema.getFields().
get(i);
91 Schema.Type type = schema.getType();
93 return avroToImpalaPrimitiveTypeMap_.get(type);
100 if (decimalType != null)
return decimalType;
109 throw new UnsupportedOperationException(String.format(
110 "Unsupported type '%s' of column '%s'", type.getName(), colName));
120 return schema.getType().equals(Schema.Type.UNION) && schema.getTypes().size() == 2 &&
121 (schema.getTypes().
get(0).getType().equals(Schema.Type.NULL) ||
122 schema.getTypes().
get(1).getType().equals(Schema.Type.NULL));
130 List<Schema> types = schema.getTypes();
131 return types.get(0).getType().equals(Schema.Type.NULL) ? types.get(1) : types.get(0);
144 Preconditions.checkState(schema.getType() == Schema.Type.BYTES);
145 String logicalType = schema.getProp(
"logicalType");
146 if (logicalType != null && logicalType.equalsIgnoreCase(
"decimal")) {
150 if (scale == null) scale = 0;
154 if (precision == null) {
155 throw new SchemaParseException(
156 "No 'precision' property specified for 'decimal' logicalType");
158 return ScalarType.createDecimalType(precision, scale);
170 throws SchemaParseException {
171 JsonNode node = schema.getJsonProp(propName);
172 if (node == null)
return null;
173 int propValue = node.getValueAsInt(-1);
175 throw new SchemaParseException(String.format(
"Invalid decimal '%s' " +
176 "property value: %s", propName, node.getValueAsText()));
static final ScalarType BIGINT
static final ScalarType STRING
static final Map< Schema.Type, Type > avroToImpalaPrimitiveTypeMap_
static final ScalarType BOOLEAN
static Schema getColumnType(Schema schema)
static final ScalarType FLOAT
static final ScalarType DOUBLE
static final ScalarType INT
static Type getTypeInfo(Schema schema, String colName)
static Integer getDecimalProp(Schema schema, String propName)
static boolean isNullableType(Schema schema)
static List< Column > parse(String schemaStr)
static Type getDecimalType(Schema schema)