Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Column.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.catalog;
16 
17 import org.apache.hadoop.hive.metastore.api.ColumnStatisticsData;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 
21 import com.cloudera.impala.thrift.TColumn;
22 import com.cloudera.impala.thrift.TColumnStats;
23 import com.google.common.base.Objects;
24 import com.google.common.base.Preconditions;
25 
30 public class Column {
31  private final static Logger LOG = LoggerFactory.getLogger(Column.class);
32 
33  protected final String name_;
34  protected final Type type_;
35  protected final String comment_;
36  protected int position_; // in table
37 
38  protected final ColumnStats stats_;
39 
40  public Column(String name, Type type, int position) {
41  this(name, type, null, position);
42  }
43 
44  public Column(String name, Type type, String comment, int position) {
45  name_ = name;
46  type_ = type;
47  comment_ = comment;
48  position_ = position;
49  stats_ = new ColumnStats(type);
50  }
51 
52  public String getComment() { return comment_; }
53  public String getName() { return name_; }
54  public Type getType() { return type_; }
55  public int getPosition() { return position_; }
56  public void setPosition(int position) { this.position_ = position; }
57  public ColumnStats getStats() { return stats_; }
58 
59  public boolean updateStats(ColumnStatisticsData statsData) {
60  boolean statsDataCompatibleWithColType = stats_.update(type_, statsData);
61  LOG.debug("col stats: " + name_ + " #distinct=" + stats_.getNumDistinctValues());
62  return statsDataCompatibleWithColType;
63  }
64 
65  public void updateStats(TColumnStats statsData) {
66  stats_.update(type_, statsData);
67  }
68 
69  @Override
70  public String toString() {
71  return Objects.toStringHelper(this.getClass())
72  .add("name_", name_)
73  .add("type_", type_)
74  .add("comment_", comment_)
75  .add("stats", stats_)
76  .add("position_", position_).toString();
77  }
78 
79  public static Column fromThrift(TColumn columnDesc) {
80  String comment = columnDesc.isSetComment() ? columnDesc.getComment() : null;
81  Preconditions.checkState(columnDesc.isSetPosition());
82  int position = columnDesc.getPosition();
83  Column col;
84  if (columnDesc.isIs_hbase_column()) {
85  // HBase table column. The HBase column qualifier (column name) is not be set for
86  // the HBase row key, so it being set in the thrift struct is not a precondition.
87  Preconditions.checkState(columnDesc.isSetColumn_family());
88  Preconditions.checkState(columnDesc.isSetIs_binary());
89  col = new HBaseColumn(columnDesc.getColumnName(), columnDesc.getColumn_family(),
90  columnDesc.getColumn_qualifier(), columnDesc.isIs_binary(),
91  Type.fromThrift(columnDesc.getColumnType()), comment, position);
92  } else {
93  // Hdfs table column.
94  col = new Column(columnDesc.getColumnName(),
95  Type.fromThrift(columnDesc.getColumnType()), comment, position);
96  }
97  if (columnDesc.isSetCol_stats()) col.updateStats(columnDesc.getCol_stats());
98  return col;
99  }
100 
101  public TColumn toThrift() {
102  TColumn colDesc = new TColumn(name_, type_.toThrift());
103  if (comment_ != null) colDesc.setComment(comment_);
104  colDesc.setPosition(position_);
105  colDesc.setCol_stats(getStats().toThrift());
106  return colDesc;
107  }
108 }
Column(String name, Type type, int position)
Definition: Column.java:40
static final Logger LOG
Definition: Column.java:31
Column(String name, Type type, String comment, int position)
Definition: Column.java:44
static Type fromThrift(TColumnType thrift)
Definition: Type.java:293
void updateStats(TColumnStats statsData)
Definition: Column.java:65
void setPosition(int position)
Definition: Column.java:56
boolean updateStats(ColumnStatisticsData statsData)
Definition: Column.java:59
static Column fromThrift(TColumn columnDesc)
Definition: Column.java:79
string name
Definition: cpu-info.cc:50