PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 1ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. package org.apache.hadoop.hive.jdbc;
  19. import java.sql.ResultSetMetaData;
  20. import java.sql.SQLException;
  21. import java.sql.Types;
  22. import java.util.List;
  23. import org.apache.hadoop.hive.serde.Constants;
  24. /**
  25. * HiveResultSetMetaData.
  26. *
  27. */
  28. public class HiveResultSetMetaData implements java.sql.ResultSetMetaData {
  29. private List<String> columnNames;
  30. private List<String> columnTypes;
  31. public HiveResultSetMetaData(List<String> columnNames,
  32. List<String> columnTypes) {
  33. this.columnNames = columnNames;
  34. this.columnTypes = columnTypes;
  35. }
  36. public String getCatalogName(int column) throws SQLException {
  37. throw new SQLException("Method not supported");
  38. }
  39. public String getColumnClassName(int column) throws SQLException {
  40. throw new SQLException("Method not supported");
  41. }
  42. public int getColumnCount() throws SQLException {
  43. return columnNames.size();
  44. }
  45. public int getColumnDisplaySize(int column) throws SQLException {
  46. // taking a stab at appropriate values
  47. switch (getColumnType(column)) {
  48. case Types.VARCHAR:
  49. case Types.BIGINT:
  50. return 32;
  51. case Types.TINYINT:
  52. return 2;
  53. case Types.BOOLEAN:
  54. return 8;
  55. case Types.DOUBLE:
  56. case Types.INTEGER:
  57. return 16;
  58. default:
  59. return 32;
  60. }
  61. }
  62. public String getColumnLabel(int column) throws SQLException {
  63. return columnNames.get(column - 1);
  64. }
  65. public String getColumnName(int column) throws SQLException {
  66. return columnNames.get(column - 1);
  67. }
  68. public int getColumnType(int column) throws SQLException {
  69. if (columnTypes == null) {
  70. throw new SQLException(
  71. "Could not determine column type name for ResultSet");
  72. }
  73. if (column < 1 || column > columnTypes.size()) {
  74. throw new SQLException("Invalid column value: " + column);
  75. }
  76. // we need to convert the thrift type to the SQL type
  77. String type = columnTypes.get(column - 1);
  78. // we need to convert the thrift type to the SQL type
  79. return hiveTypeToSqlType(type);
  80. }
  81. /**
  82. * Convert hive types to sql types.
  83. * @param type
  84. * @return Integer java.sql.Types values
  85. * @throws SQLException
  86. */
  87. public static int hiveTypeToSqlType(String type) throws SQLException {
  88. if ("string".equalsIgnoreCase(type)) {
  89. return Types.VARCHAR;
  90. } else if ("float".equalsIgnoreCase(type)) {
  91. return Types.FLOAT;
  92. } else if ("double".equalsIgnoreCase(type)) {
  93. return Types.DOUBLE;
  94. } else if ("boolean".equalsIgnoreCase(type)) {
  95. return Types.BOOLEAN;
  96. } else if ("tinyint".equalsIgnoreCase(type)) {
  97. return Types.TINYINT;
  98. } else if ("smallint".equalsIgnoreCase(type)) {
  99. return Types.SMALLINT;
  100. } else if ("int".equalsIgnoreCase(type)) {
  101. return Types.INTEGER;
  102. } else if ("bigint".equalsIgnoreCase(type)) {
  103. return Types.BIGINT;
  104. } else if (type.startsWith("map<")) {
  105. return Types.VARCHAR;
  106. } else if (type.startsWith("array<")) {
  107. return Types.VARCHAR;
  108. } else if (type.startsWith("struct<")) {
  109. return Types.VARCHAR;
  110. }
  111. throw new SQLException("Unrecognized column type: " + type);
  112. }
  113. public String getColumnTypeName(int column) throws SQLException {
  114. if (columnTypes == null) {
  115. throw new SQLException(
  116. "Could not determine column type name for ResultSet");
  117. }
  118. if (column < 1 || column > columnTypes.size()) {
  119. throw new SQLException("Invalid column value: " + column);
  120. }
  121. // we need to convert the Hive type to the SQL type name
  122. // TODO: this would be better handled in an enum
  123. String type = columnTypes.get(column - 1);
  124. if ("string".equalsIgnoreCase(type)) {
  125. return Constants.STRING_TYPE_NAME;
  126. } else if ("float".equalsIgnoreCase(type)) {
  127. return Constants.FLOAT_TYPE_NAME;
  128. } else if ("double".equalsIgnoreCase(type)) {
  129. return Constants.DOUBLE_TYPE_NAME;
  130. } else if ("boolean".equalsIgnoreCase(type)) {
  131. return Constants.BOOLEAN_TYPE_NAME;
  132. } else if ("tinyint".equalsIgnoreCase(type)) {
  133. return Constants.TINYINT_TYPE_NAME;
  134. } else if ("smallint".equalsIgnoreCase(type)) {
  135. return Constants.SMALLINT_TYPE_NAME;
  136. } else if ("int".equalsIgnoreCase(type)) {
  137. return Constants.INT_TYPE_NAME;
  138. } else if ("bigint".equalsIgnoreCase(type)) {
  139. return Constants.BIGINT_TYPE_NAME;
  140. } else if (type.startsWith("map<")) {
  141. return Constants.STRING_TYPE_NAME;
  142. } else if (type.startsWith("array<")) {
  143. return Constants.STRING_TYPE_NAME;
  144. } else if (type.startsWith("struct<")) {
  145. return Constants.STRING_TYPE_NAME;
  146. }
  147. throw new SQLException("Unrecognized column type: " + type);
  148. }
  149. public int getPrecision(int column) throws SQLException {
  150. if (Types.DOUBLE == getColumnType(column)) {
  151. return -1; // Do we have a precision limit?
  152. }
  153. return 0;
  154. }
  155. public int getScale(int column) throws SQLException {
  156. if (Types.DOUBLE == getColumnType(column)) {
  157. return -1; // Do we have a scale limit?
  158. }
  159. return 0;
  160. }
  161. public String getSchemaName(int column) throws SQLException {
  162. throw new SQLException("Method not supported");
  163. }
  164. public String getTableName(int column) throws SQLException {
  165. throw new SQLException("Method not supported");
  166. }
  167. public boolean isAutoIncrement(int column) throws SQLException {
  168. // Hive doesn't have an auto-increment concept
  169. return false;
  170. }
  171. public boolean isCaseSensitive(int column) throws SQLException {
  172. throw new SQLException("Method not supported");
  173. }
  174. public boolean isCurrency(int column) throws SQLException {
  175. // Hive doesn't support a currency type
  176. return false;
  177. }
  178. public boolean isDefinitelyWritable(int column) throws SQLException {
  179. throw new SQLException("Method not supported");
  180. }
  181. public int isNullable(int column) throws SQLException {
  182. // Hive doesn't have the concept of not-null
  183. return ResultSetMetaData.columnNullable;
  184. }
  185. public boolean isReadOnly(int column) throws SQLException {
  186. throw new SQLException("Method not supported");
  187. }
  188. public boolean isSearchable(int column) throws SQLException {
  189. throw new SQLException("Method not supported");
  190. }
  191. public boolean isSigned(int column) throws SQLException {
  192. throw new SQLException("Method not supported");
  193. }
  194. public boolean isWritable(int column) throws SQLException {
  195. throw new SQLException("Method not supported");
  196. }
  197. public boolean isWrapperFor(Class<?> iface) throws SQLException {
  198. throw new SQLException("Method not supported");
  199. }
  200. public <T> T unwrap(Class<T> iface) throws SQLException {
  201. throw new SQLException("Method not supported");
  202. }
  203. }