You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by GitBox <gi...@apache.org> on 2020/07/29 07:25:55 UTC

[GitHub] [hive] cravani commented on a change in pull request #1328: HIVE-23873: Querying Hive JDBCStorageHandler table fails with NPE whe…

cravani commented on a change in pull request #1328:
URL: https://github.com/apache/hive/pull/1328#discussion_r461806183



##########
File path: jdbc-handler/src/main/java/org/apache/hive/storage/jdbc/dao/JdbcRecordIterator.java
##########
@@ -59,9 +63,26 @@ public JdbcRecordIterator(Connection conn, PreparedStatement ps, ResultSet rs, C
       fieldNamesProperty = Preconditions.checkNotNull(conf.get(Constants.JDBC_QUERY_FIELD_NAMES));
       fieldTypesProperty = Preconditions.checkNotNull(conf.get(Constants.JDBC_QUERY_FIELD_TYPES));
     } else {
-      fieldNamesProperty = Preconditions.checkNotNull(conf.get(serdeConstants.LIST_COLUMNS));
-      fieldTypesProperty = Preconditions.checkNotNull(conf.get(serdeConstants.LIST_COLUMN_TYPES));
+        try {

Review comment:
       fixed :)

##########
File path: jdbc-handler/src/main/java/org/apache/hive/storage/jdbc/dao/JdbcRecordIterator.java
##########
@@ -59,9 +63,26 @@ public JdbcRecordIterator(Connection conn, PreparedStatement ps, ResultSet rs, C
       fieldNamesProperty = Preconditions.checkNotNull(conf.get(Constants.JDBC_QUERY_FIELD_NAMES));
       fieldTypesProperty = Preconditions.checkNotNull(conf.get(Constants.JDBC_QUERY_FIELD_TYPES));
     } else {
-      fieldNamesProperty = Preconditions.checkNotNull(conf.get(serdeConstants.LIST_COLUMNS));
-      fieldTypesProperty = Preconditions.checkNotNull(conf.get(serdeConstants.LIST_COLUMN_TYPES));
+        try {
+          if(conf.get(Constants.JDBC_QUERY) == null)    {
+            ResultSetMetaData metadata = rs.getMetaData();
+            int numColumns = metadata.getColumnCount();
+            List<String> columnNames = new ArrayList<String>(numColumns);
+            for (int i = 0; i < numColumns; i++) {
+              columnNames.add(metadata.getColumnName(i + 1));
+            }
+            fieldNamesProperty = String.join(",",columnNames);
+          }
+          else

Review comment:
       { & }. are required? I was assuming since we have single statement within conditional block, it should be fine.

##########
File path: jdbc-handler/src/main/java/org/apache/hive/storage/jdbc/dao/JdbcRecordIterator.java
##########
@@ -59,9 +63,26 @@ public JdbcRecordIterator(Connection conn, PreparedStatement ps, ResultSet rs, C
       fieldNamesProperty = Preconditions.checkNotNull(conf.get(Constants.JDBC_QUERY_FIELD_NAMES));
       fieldTypesProperty = Preconditions.checkNotNull(conf.get(Constants.JDBC_QUERY_FIELD_TYPES));
     } else {
-      fieldNamesProperty = Preconditions.checkNotNull(conf.get(serdeConstants.LIST_COLUMNS));
-      fieldTypesProperty = Preconditions.checkNotNull(conf.get(serdeConstants.LIST_COLUMN_TYPES));
+        try {
+          if(conf.get(Constants.JDBC_QUERY) == null)    {

Review comment:
       Fixed.

##########
File path: jdbc-handler/src/main/java/org/apache/hive/storage/jdbc/dao/JdbcRecordIterator.java
##########
@@ -59,9 +63,26 @@ public JdbcRecordIterator(Connection conn, PreparedStatement ps, ResultSet rs, C
       fieldNamesProperty = Preconditions.checkNotNull(conf.get(Constants.JDBC_QUERY_FIELD_NAMES));
       fieldTypesProperty = Preconditions.checkNotNull(conf.get(Constants.JDBC_QUERY_FIELD_TYPES));
     } else {
-      fieldNamesProperty = Preconditions.checkNotNull(conf.get(serdeConstants.LIST_COLUMNS));
-      fieldTypesProperty = Preconditions.checkNotNull(conf.get(serdeConstants.LIST_COLUMN_TYPES));
+        try {
+          if(conf.get(Constants.JDBC_QUERY) == null)    {
+            ResultSetMetaData metadata = rs.getMetaData();
+            int numColumns = metadata.getColumnCount();
+            List<String> columnNames = new ArrayList<String>(numColumns);
+            for (int i = 0; i < numColumns; i++) {
+              columnNames.add(metadata.getColumnName(i + 1));
+            }
+            fieldNamesProperty = String.join(",",columnNames);
+          }
+          else
+            fieldNamesProperty = Preconditions.checkNotNull(conf.get(serdeConstants.LIST_COLUMNS));
+        }
+        catch (Exception e) {
+          LOGGER.error("Error while trying to get column names.", e);
+          throw new HiveJdbcDatabaseAccessException("Error while trying to get column names: " + e.getMessage(), e);
+        }
+        fieldTypesProperty = Preconditions.checkNotNull(conf.get(serdeConstants.LIST_COLUMN_TYPES));
     }
+    LOGGER.debug("Iterator ColumnNames = "+fieldNamesProperty);

Review comment:
       fixed.

##########
File path: jdbc-handler/src/main/java/org/apache/hive/storage/jdbc/dao/JdbcRecordIterator.java
##########
@@ -59,9 +63,26 @@ public JdbcRecordIterator(Connection conn, PreparedStatement ps, ResultSet rs, C
       fieldNamesProperty = Preconditions.checkNotNull(conf.get(Constants.JDBC_QUERY_FIELD_NAMES));
       fieldTypesProperty = Preconditions.checkNotNull(conf.get(Constants.JDBC_QUERY_FIELD_TYPES));
     } else {
-      fieldNamesProperty = Preconditions.checkNotNull(conf.get(serdeConstants.LIST_COLUMNS));
-      fieldTypesProperty = Preconditions.checkNotNull(conf.get(serdeConstants.LIST_COLUMN_TYPES));
+        try {
+          if(conf.get(Constants.JDBC_QUERY) == null)    {
+            ResultSetMetaData metadata = rs.getMetaData();
+            int numColumns = metadata.getColumnCount();
+            List<String> columnNames = new ArrayList<String>(numColumns);
+            for (int i = 0; i < numColumns; i++) {
+              columnNames.add(metadata.getColumnName(i + 1));
+            }
+            fieldNamesProperty = String.join(",",columnNames);
+          }
+          else

Review comment:
       Ah, Updated the code with  { & }
   :)




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org