diff --git a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java index b7362fb..57c53cb 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java @@ -95,6 +95,7 @@ private static final String testDbName = "testjdbcdriver"; private static final String defaultDbName = "default"; private static final String tableName = "testjdbcdrivertbl"; + private static final String tableNameWithPk = "pktable"; private static final String tableComment = "Simple table"; private static final String viewName = "testjdbcdriverview"; private static final String viewComment = "Simple view"; @@ -138,6 +139,8 @@ private static void createTestTables(Statement stmt, String testDbName) throws S stmt.execute("create table " + tableName + " (under_col int comment 'the under column', value string) comment '" + tableComment + "'"); + stmt.execute("create table " + tableNameWithPk + + " (a STRING, b STRING, primary key (a) disable novalidate) "); // load data stmt.execute("load data local inpath '" + dataFilePath.toString() + "' into table " + tableName); @@ -2047,6 +2050,30 @@ public void testPrimaryKeys() throws SQLException { } /** + * test testPrimaryKeysNotNull() + * @throws SQLException + */ + @Test + public void testPrimaryKeysNotNull() throws SQLException { + DatabaseMetaData dbmd = con.getMetaData(); + assertNotNull(dbmd); + ResultSet rs = dbmd.getColumns(null, testDbName, tableNameWithPk, "%"); + int index = 0; + while (rs.next()) { + String isNullable = rs.getString("IS_NULLABLE"); + if (index == 0) { + assertEquals(isNullable, "NO"); + } else if (index == 1) { + assertEquals(isNullable, "YES"); + } else { + throw new SQLException("Unexpected column."); + } + index++; + } + rs.close(); + } + + /** * test getImportedKeys() * @throws SQLException */ diff --git a/service/src/java/org/apache/hive/service/cli/operation/GetColumnsOperation.java b/service/src/java/org/apache/hive/service/cli/operation/GetColumnsOperation.java index c075179..e6fce1b 100644 --- a/service/src/java/org/apache/hive/service/cli/operation/GetColumnsOperation.java +++ b/service/src/java/org/apache/hive/service/cli/operation/GetColumnsOperation.java @@ -23,13 +23,17 @@ import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import java.util.regex.Pattern; import org.apache.hadoop.hive.conf.HiveConf.ConfVars; import org.apache.hadoop.hive.metastore.IMetaStoreClient; +import org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest; +import org.apache.hadoop.hive.metastore.api.SQLPrimaryKey; import org.apache.hadoop.hive.metastore.api.Table; import org.apache.hadoop.hive.ql.metadata.TableIterable; import org.apache.hadoop.hive.ql.security.authorization.plugin.HiveOperationType; @@ -164,6 +168,11 @@ public void runInternal() throws HiveSQLException { TableSchema schema = new TableSchema(metastoreClient.getSchema(dbName, table.getTableName())); + List primaryKeys = metastoreClient.getPrimaryKeys(new PrimaryKeysRequest(dbName, table.getTableName())); + Set pkColNames = new HashSet<>(); + for(SQLPrimaryKey key : primaryKeys) { + pkColNames.add(key.getColumn_name().toLowerCase()); + } for (ColumnDescriptor column : schema.getColumnDescriptors()) { if (columnPattern != null && !columnPattern.matcher(column.getName()).matches()) { continue; @@ -186,7 +195,7 @@ public void runInternal() throws HiveSQLException { null, // SQL_DATETIME_SUB null, // CHAR_OCTET_LENGTH column.getOrdinalPosition(), // ORDINAL_POSITION - "YES", // IS_NULLABLE + pkColNames.contains(column.getName().toLowerCase()) ? "NO" : "YES", // IS_NULLABLE null, // SCOPE_CATALOG null, // SCOPE_SCHEMA null, // SCOPE_TABLE