Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
StructType.java
Go to the documentation of this file.
1 package com.cloudera.impala.catalog;
2 
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 
6 import com.cloudera.impala.thrift.TColumnType;
7 import com.cloudera.impala.thrift.TStructField;
8 import com.cloudera.impala.thrift.TTypeNode;
9 import com.cloudera.impala.thrift.TTypeNodeType;
10 import com.google.common.base.Joiner;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Lists;
13 import com.google.common.collect.Maps;
14 
18 public class StructType extends Type {
19  private final HashMap<String, StructField> fieldMap_ = Maps.newHashMap();
20  private final ArrayList<StructField> fields_;
21 
22  public StructType(ArrayList<StructField> fields) {
23  Preconditions.checkNotNull(fields);
24  fields_ = fields;
25  for (int i = 0; i < fields_.size(); ++i) {
26  fields_.get(i).setPosition(i);
27  fieldMap_.put(fields_.get(i).getName().toLowerCase(), fields_.get(i));
28  }
29  }
30 
31  public StructType() {
32  fields_ = Lists.newArrayList();
33  }
34 
35  @Override
36  public String toSql() {
37  ArrayList<String> fieldsSql = Lists.newArrayList();
38  for (StructField f: fields_) {
39  fieldsSql.add(f.toSql());
40  }
41  return String.format("STRUCT<%s>", Joiner.on(",").join(fieldsSql));
42  }
43 
44  public void addField(StructField field) {
45  field.setPosition(fields_.size());
46  fields_.add(field);
47  fieldMap_.put(field.getName().toLowerCase(), field);
48  }
49 
50  public ArrayList<StructField> getFields() { return fields_; }
51 
52  public StructField getField(String fieldName) {
53  return fieldMap_.get(fieldName.toLowerCase());
54  }
55 
56  public void clearFields() {
57  fields_.clear();
58  fieldMap_.clear();
59  }
60 
61  @Override
62  public boolean equals(Object other) {
63  if (!(other instanceof StructType)) return false;
64  StructType otherStructType = (StructType) other;
65  return otherStructType.getFields().equals(fields_);
66  }
67 
68  @Override
69  public void toThrift(TColumnType container) {
70  TTypeNode node = new TTypeNode();
71  container.types.add(node);
72  Preconditions.checkNotNull(fields_);
73  Preconditions.checkNotNull(!fields_.isEmpty());
74  node.setType(TTypeNodeType.STRUCT);
75  node.setStruct_fields(new ArrayList<TStructField>());
76  for (StructField field: fields_) {
77  field.toThrift(container, node);
78  }
79  }
80 }
void addField(StructField field)
Definition: StructType.java:44
final ArrayList< StructField > fields_
Definition: StructType.java:20
void toThrift(TColumnType container)
Definition: StructType.java:69
final HashMap< String, StructField > fieldMap_
Definition: StructType.java:19
StructType(ArrayList< StructField > fields)
Definition: StructType.java:22
StructField getField(String fieldName)
Definition: StructType.java:52
ArrayList< StructField > getFields()
Definition: StructType.java:50