This is an automated email from the ASF dual-hosted git repository.

dkuzmenko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git

commit 7033da1b3b638b689d16076c72023c1f8e0971cc
Author: Michael Smith <[email protected]>
AuthorDate: Mon Oct 16 13:30:18 2023 -0700

    HIVE-27887: Provide reasonable defaults for ResultSetMetaData (Michael 
Smith, reviewed by Attila Turoczy, Denys Kuzmenko)
    
    Fill in reasonable defaults for Hive SQL in ResultSetMetaData rather
    than throwing in exceptions as some consumers (NiFi) expect to be able
    to call getTableName and isSigned.
    
    Closes #4902
---
 .../apache/hive/jdbc/HiveResultSetMetaData.java    | 33 +++++++++++++++++-----
 jdbc/src/java/org/apache/hive/jdbc/JdbcColumn.java | 14 +++++++++
 2 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveResultSetMetaData.java 
b/jdbc/src/java/org/apache/hive/jdbc/HiveResultSetMetaData.java
index a8887a96d10..089f9dea80f 100644
--- a/jdbc/src/java/org/apache/hive/jdbc/HiveResultSetMetaData.java
+++ b/jdbc/src/java/org/apache/hive/jdbc/HiveResultSetMetaData.java
@@ -30,6 +30,8 @@ import org.apache.hadoop.hive.serde2.thrift.Type;
  *
  */
 public class HiveResultSetMetaData implements java.sql.ResultSetMetaData {
+  private static final String DOT = ".";
+
   private final List<String> columnNames;
   private final List<String> columnTypes;
   private final List<JdbcColumnAttributes> columnAttributes;
@@ -43,7 +45,9 @@ public class HiveResultSetMetaData implements 
java.sql.ResultSetMetaData {
   }
 
   public String getCatalogName(int column) throws SQLException {
-    throw new SQLFeatureNotSupportedException("Method not supported");
+    // Hive doesn't implement a catalog concept, see
+    // https://issues.apache.org/jira/browse/HIVE-3121.
+    return "";
   }
 
   private Type getHiveType(int column) throws SQLException {
@@ -95,11 +99,23 @@ public class HiveResultSetMetaData implements 
java.sql.ResultSetMetaData {
   }
 
   public String getSchemaName(int column) throws SQLException {
-    throw new SQLFeatureNotSupportedException("Method not supported");
+    String tableName = getTableName(column);
+    int index = tableName.lastIndexOf(DOT);
+    if (index >= 0) {
+      return tableName.substring(0, index);
+    }
+    // Impala usually doesn't return fully qualified column names. Return "" 
to avoid
+    // giving false results.
+    return "";
   }
 
   public String getTableName(int column) throws SQLException {
-    throw new SQLFeatureNotSupportedException("Method not supported");
+    String columnName = getColumnName(column);
+    int index = columnName.lastIndexOf(DOT);
+    if (index >= 0) {
+      return columnName.substring(0, index);
+    }
+    return "";
   }
 
   public boolean isAutoIncrement(int column) throws SQLException {
@@ -125,7 +141,8 @@ public class HiveResultSetMetaData implements 
java.sql.ResultSetMetaData {
   }
 
   public boolean isDefinitelyWritable(int column) throws SQLException {
-    throw new SQLFeatureNotSupportedException("Method not supported");
+    // Corollary of isReadOnly=true.
+    return false;
   }
 
   public int isNullable(int column) throws SQLException {
@@ -138,15 +155,17 @@ public class HiveResultSetMetaData implements 
java.sql.ResultSetMetaData {
   }
 
   public boolean isSearchable(int column) throws SQLException {
-    throw new SQLFeatureNotSupportedException("Method not supported");
+    // All columns can be used in where clauses.
+    return true;
   }
 
   public boolean isSigned(int column) throws SQLException {
-    throw new SQLFeatureNotSupportedException("Method not supported");
+    return JdbcColumn.columnSigned(getHiveType(column));
   }
 
   public boolean isWritable(int column) throws SQLException {
-    throw new SQLFeatureNotSupportedException("Method not supported");
+    // Corollary of isReadOnly=true.
+    return false;
   }
 
   public boolean isWrapperFor(Class<?> iface) throws SQLException {
diff --git a/jdbc/src/java/org/apache/hive/jdbc/JdbcColumn.java 
b/jdbc/src/java/org/apache/hive/jdbc/JdbcColumn.java
index d7a031c4421..8a9c5790025 100644
--- a/jdbc/src/java/org/apache/hive/jdbc/JdbcColumn.java
+++ b/jdbc/src/java/org/apache/hive/jdbc/JdbcColumn.java
@@ -372,6 +372,20 @@ public class JdbcColumn {
     }
   }
 
+  static boolean columnSigned(Type hiveType)
+      throws SQLException {
+    int columnType = hiveTypeToSqlType(hiveType);
+    switch(columnType) {
+    case Types.TINYINT:
+    case Types.SMALLINT:
+    case Types.INTEGER:
+    case Types.BIGINT:
+      return true;
+    default:
+      return false;
+    }
+  }
+
   public Integer getNumPrecRadix() {
     final String t = type.toLowerCase();
     switch (t) {

Reply via email to