Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DescribeResultFactory.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.service;
16 
17 import org.apache.hadoop.hive.metastore.api.FieldSchema;
18 import org.apache.hadoop.hive.ql.metadata.formatting.MetaDataFormatUtils;
19 
22 import com.cloudera.impala.thrift.TColumnValue;
23 import com.cloudera.impala.thrift.TDescribeTableOutputStyle;
24 import com.cloudera.impala.thrift.TDescribeTableResult;
25 import com.cloudera.impala.thrift.TResultRow;
26 import com.google.common.collect.Lists;
27 
28 /*
29  * Builds results for DESCRIBE statements by constructing and populating a
30  * TDescribeTableResult object.
31  */
32 public class DescribeResultFactory {
33  // Number of columns in each row of the DESCRIBE FORMATTED result set.
34  private final static int NUM_DESC_FORMATTED_RESULT_COLS = 3;
35 
36  public static TDescribeTableResult buildDescribeTableResult(Table table,
37  TDescribeTableOutputStyle outputFormat) {
38  switch (outputFormat) {
39  case MINIMAL: return describeTableMinimal(table);
40  case FORMATTED: return describeTableFormatted(table);
41  default: throw new UnsupportedOperationException(
42  "Unknown TDescribeTableOutputStyle value: " + outputFormat);
43  }
44  }
45 
46  /*
47  * Builds results for a DESCRIBE <table> command. This consists of the column
48  * definition for each column in the table.
49  */
50  private static TDescribeTableResult describeTableMinimal(Table table) {
51  TDescribeTableResult descResult = new TDescribeTableResult();
52  descResult.results = Lists.newArrayList();
53 
54  // Get description of all the table's columns (includes partition columns).
55  for (Column column: table.getColumnsInHiveOrder()) {
56  TColumnValue colNameCol = new TColumnValue();
57  colNameCol.setString_val(column.getName());
58  TColumnValue dataTypeCol = new TColumnValue();
59  dataTypeCol.setString_val(column.getType().toSql().toLowerCase());
60  TColumnValue commentCol = new TColumnValue();
61  commentCol.setString_val(column.getComment() != null ? column.getComment() : "");
62  descResult.results.add(
63  new TResultRow(Lists.newArrayList(colNameCol, dataTypeCol, commentCol)));
64  }
65  return descResult;
66  }
67 
68  /*
69  * Builds a TDescribeTableResult that contains the result of a DESCRIBE FORMATTED
70  * <table> command. For the formatted describe output the goal is to be exactly the
71  * same as what Hive (via HiveServer2) outputs, for compatibility reasons. To do this,
72  * Hive's MetadataFormatUtils class is used to build the results.
73  */
74  private static TDescribeTableResult describeTableFormatted(Table table) {
75  TDescribeTableResult descResult = new TDescribeTableResult();
76  descResult.results = Lists.newArrayList();
77 
78  org.apache.hadoop.hive.metastore.api.Table msTable =
79  table.getMetaStoreTable().deepCopy();
80  // Fixup the metastore table so the output of DESCRIBE FORMATTED matches Hive's.
81  // This is to distinguish between empty comments and no comments (value is null).
82  for (FieldSchema fs: msTable.getSd().getCols())
83  fs.setComment(table.getColumn(fs.getName()).getComment());
84  for (FieldSchema fs: msTable.getPartitionKeys()) {
85  fs.setComment(table.getColumn(fs.getName()).getComment());
86  }
87 
88  // To avoid initializing any of the SerDe classes in the metastore table Thrift
89  // struct, create the ql.metadata.Table object by calling the empty c'tor and
90  // then calling setTTable().
91  org.apache.hadoop.hive.ql.metadata.Table hiveTable =
92  new org.apache.hadoop.hive.ql.metadata.Table();
93  hiveTable.setTTable(msTable);
94  StringBuilder sb = new StringBuilder();
95  // First add all the columns (includes partition columns).
96  sb.append(MetaDataFormatUtils.getAllColumnsInformation(msTable.getSd().getCols(),
97  msTable.getPartitionKeys(), true, false, true));
98  // Add the extended table metadata information.
99  sb.append(MetaDataFormatUtils.getTableInformation(hiveTable));
100 
101  for (String line: sb.toString().split("\n")) {
102  // To match Hive's HiveServer2 output, split each line into multiple column
103  // values based on the field delimiter.
104  String[] columns = line.split(MetaDataFormatUtils.FIELD_DELIM);
105  TResultRow resultRow = new TResultRow();
106  for (int i = 0; i < NUM_DESC_FORMATTED_RESULT_COLS; ++i) {
107  TColumnValue colVal = new TColumnValue();
108  colVal.setString_val(null);
109  if (columns.length > i) {
110  // Add the column value.
111  colVal.setString_val(columns[i]);
112  }
113  resultRow.addToColVals(colVal);
114  }
115  descResult.results.add(resultRow);
116  }
117  return descResult;
118  }
119 }
ArrayList< Column > getColumnsInHiveOrder()
Definition: Table.java:373
static TDescribeTableResult describeTableMinimal(Table table)
static TDescribeTableResult describeTableFormatted(Table table)
static TDescribeTableResult buildDescribeTableResult(Table table, TDescribeTableOutputStyle outputFormat)