PageRenderTime 35ms CodeModel.GetById 8ms app.highlight 22ms RepoModel.GetById 2ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/jdbc/src/java/org/apache/hadoop/hive/jdbc/HiveResultSetMetaData.java

#
Java | 242 lines | 168 code | 39 blank | 35 comment | 55 complexity | e004bf3b7cc7d880ade4262ce6d2aae4 MD5 | raw file
  1/**
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18
 19package org.apache.hadoop.hive.jdbc;
 20
 21import java.sql.ResultSetMetaData;
 22import java.sql.SQLException;
 23import java.sql.Types;
 24import java.util.List;
 25
 26import org.apache.hadoop.hive.serde.Constants;
 27
 28/**
 29 * HiveResultSetMetaData.
 30 *
 31 */
 32public class HiveResultSetMetaData implements java.sql.ResultSetMetaData {
 33  private List<String> columnNames;
 34  private List<String> columnTypes;
 35
 36  public HiveResultSetMetaData(List<String> columnNames,
 37      List<String> columnTypes) {
 38    this.columnNames = columnNames;
 39    this.columnTypes = columnTypes;
 40  }
 41
 42  public String getCatalogName(int column) throws SQLException {
 43    throw new SQLException("Method not supported");
 44  }
 45
 46  public String getColumnClassName(int column) throws SQLException {
 47    throw new SQLException("Method not supported");
 48  }
 49
 50  public int getColumnCount() throws SQLException {
 51    return columnNames.size();
 52  }
 53
 54  public int getColumnDisplaySize(int column) throws SQLException {
 55    // taking a stab at appropriate values
 56    switch (getColumnType(column)) {
 57    case Types.VARCHAR:
 58    case Types.BIGINT:
 59      return 32;
 60    case Types.TINYINT:
 61      return 2;
 62    case Types.BOOLEAN:
 63      return 8;
 64    case Types.DOUBLE:
 65    case Types.INTEGER:
 66      return 16;
 67    default:
 68      return 32;
 69    }
 70  }
 71
 72  public String getColumnLabel(int column) throws SQLException {
 73    return columnNames.get(column - 1);
 74  }
 75
 76  public String getColumnName(int column) throws SQLException {
 77    return columnNames.get(column - 1);
 78  }
 79
 80  public int getColumnType(int column) throws SQLException {
 81    if (columnTypes == null) {
 82      throw new SQLException(
 83          "Could not determine column type name for ResultSet");
 84    }
 85
 86    if (column < 1 || column > columnTypes.size()) {
 87      throw new SQLException("Invalid column value: " + column);
 88    }
 89
 90    // we need to convert the thrift type to the SQL type
 91    String type = columnTypes.get(column - 1);
 92
 93    // we need to convert the thrift type to the SQL type
 94    return hiveTypeToSqlType(type);
 95  }
 96
 97  /**
 98   * Convert hive types to sql types.
 99   * @param type
100   * @return Integer java.sql.Types values
101   * @throws SQLException
102   */
103  public static int hiveTypeToSqlType(String type) throws SQLException {
104    if ("string".equalsIgnoreCase(type)) {
105      return Types.VARCHAR;
106    } else if ("float".equalsIgnoreCase(type)) {
107      return Types.FLOAT;
108    } else if ("double".equalsIgnoreCase(type)) {
109      return Types.DOUBLE;
110    } else if ("boolean".equalsIgnoreCase(type)) {
111      return Types.BOOLEAN;
112    } else if ("tinyint".equalsIgnoreCase(type)) {
113      return Types.TINYINT;
114    } else if ("smallint".equalsIgnoreCase(type)) {
115      return Types.SMALLINT;
116    } else if ("int".equalsIgnoreCase(type)) {
117      return Types.INTEGER;
118    } else if ("bigint".equalsIgnoreCase(type)) {
119      return Types.BIGINT;
120    } else if (type.startsWith("map<")) {
121      return Types.VARCHAR;
122    } else if (type.startsWith("array<")) {
123      return Types.VARCHAR;
124    } else if (type.startsWith("struct<")) {
125      return Types.VARCHAR;
126    }
127    throw new SQLException("Unrecognized column type: " + type);
128  }
129
130
131  public String getColumnTypeName(int column) throws SQLException {
132    if (columnTypes == null) {
133      throw new SQLException(
134          "Could not determine column type name for ResultSet");
135    }
136
137    if (column < 1 || column > columnTypes.size()) {
138      throw new SQLException("Invalid column value: " + column);
139    }
140
141    // we need to convert the Hive type to the SQL type name
142    // TODO: this would be better handled in an enum
143    String type = columnTypes.get(column - 1);
144    if ("string".equalsIgnoreCase(type)) {
145      return Constants.STRING_TYPE_NAME;
146    } else if ("float".equalsIgnoreCase(type)) {
147      return Constants.FLOAT_TYPE_NAME;
148    } else if ("double".equalsIgnoreCase(type)) {
149      return Constants.DOUBLE_TYPE_NAME;
150    } else if ("boolean".equalsIgnoreCase(type)) {
151      return Constants.BOOLEAN_TYPE_NAME;
152    } else if ("tinyint".equalsIgnoreCase(type)) {
153      return Constants.TINYINT_TYPE_NAME;
154    } else if ("smallint".equalsIgnoreCase(type)) {
155      return Constants.SMALLINT_TYPE_NAME;
156    } else if ("int".equalsIgnoreCase(type)) {
157      return Constants.INT_TYPE_NAME;
158    } else if ("bigint".equalsIgnoreCase(type)) {
159      return Constants.BIGINT_TYPE_NAME;
160    } else if (type.startsWith("map<")) {
161      return Constants.STRING_TYPE_NAME;
162    } else if (type.startsWith("array<")) {
163      return Constants.STRING_TYPE_NAME;
164    } else if (type.startsWith("struct<")) {
165      return Constants.STRING_TYPE_NAME;
166    }
167
168    throw new SQLException("Unrecognized column type: " + type);
169  }
170
171  public int getPrecision(int column) throws SQLException {
172    if (Types.DOUBLE == getColumnType(column)) {
173      return -1; // Do we have a precision limit?
174    }
175
176    return 0;
177  }
178
179  public int getScale(int column) throws SQLException {
180    if (Types.DOUBLE == getColumnType(column)) {
181      return -1; // Do we have a scale limit?
182    }
183
184    return 0;
185  }
186
187  public String getSchemaName(int column) throws SQLException {
188    throw new SQLException("Method not supported");
189  }
190
191  public String getTableName(int column) throws SQLException {
192    throw new SQLException("Method not supported");
193  }
194
195  public boolean isAutoIncrement(int column) throws SQLException {
196    // Hive doesn't have an auto-increment concept
197    return false;
198  }
199
200  public boolean isCaseSensitive(int column) throws SQLException {
201    throw new SQLException("Method not supported");
202  }
203
204  public boolean isCurrency(int column) throws SQLException {
205    // Hive doesn't support a currency type
206    return false;
207  }
208
209  public boolean isDefinitelyWritable(int column) throws SQLException {
210    throw new SQLException("Method not supported");
211  }
212
213  public int isNullable(int column) throws SQLException {
214    // Hive doesn't have the concept of not-null
215    return ResultSetMetaData.columnNullable;
216  }
217
218  public boolean isReadOnly(int column) throws SQLException {
219    throw new SQLException("Method not supported");
220  }
221
222  public boolean isSearchable(int column) throws SQLException {
223    throw new SQLException("Method not supported");
224  }
225
226  public boolean isSigned(int column) throws SQLException {
227    throw new SQLException("Method not supported");
228  }
229
230  public boolean isWritable(int column) throws SQLException {
231    throw new SQLException("Method not supported");
232  }
233
234  public boolean isWrapperFor(Class<?> iface) throws SQLException {
235    throw new SQLException("Method not supported");
236  }
237
238  public <T> T unwrap(Class<T> iface) throws SQLException {
239    throw new SQLException("Method not supported");
240  }
241
242}