浏览代码

HUE-9189 [hive] Support expanding tables with single Primary Keys

Romain 5 年之前
父节点
当前提交
4eae9fdeb8

+ 6 - 2
apps/beeswax/src/beeswax/server/hive_server2_lib.py

@@ -141,8 +141,12 @@ class HiveServerTable(Table):
     rows = self.describe
     try:
       col_row_index = list(map(itemgetter('col_name'), rows)).index('# Primary Key') + 3
-      keys = rows[col_row_index:]
-    except:
+      try:
+        end_cols_index = list(map(itemgetter('col_name'), rows[col_row_index:])).index('')
+        keys = rows[col_row_index:][:end_cols_index]
+      except ValueError:
+        keys = rows[col_row_index:]  # There was no other constraints afterwards
+    except Exception:
       # No info (e.g. IMPALA-8291)
       keys = []
 

+ 34 - 1
apps/beeswax/src/beeswax/server/hive_server2_lib_tests.py

@@ -346,7 +346,40 @@ class TestHiveServerTable():
       assert_equal(table.partition_keys[0].comment, '')
 
 
-  def test_primary_keys_hive(self):
+  def test_single_primary_key_hive(self):
+
+      table_results = Mock()
+      table_schema = Mock()
+      desc_results = Mock(
+        columns=[
+          # Dump of `DESCRIBE FORMATTED table`
+          Mock(stringVal=Mock(values=['# col_name', '', 'code', 'description', 'total_emp', 'salary', '', '# Partition Information', '# col_name', 'date', '', '# Detailed Table Information', 'Database:', 'OwnerType:', 'Owner:', 'CreateTime:', 'LastAccessTime:', 'Retention:', 'Location:', 'Table Type:', 'Table Parameters:', '', '', '', '', '', '', '', '', '', '', '# Storage Information', 'SerDe Library:', 'InputFormat:', 'OutputFormat:', 'Compressed:', 'Num Buckets:', 'Bucket Columns:', 'Sort Columns:', 'Storage Desc Params:', '', '', '# Constraints', '', '# Primary Key', 'Table:', 'Constraint Name:', 'Column Name:', ''], nulls='')),
+          Mock(stringVal=Mock(values=['data_type', 'NULL', 'string', 'string', 'int', 'int', 'NULL', 'NULL', 'data_type', 'string', 'NULL', 'NULL', 'default', 'USER', 'hive', 'Mon Nov 04 07:44:10 PST 2019', 'UNKNOWN', '0', 'hdfs://nightly7x-unsecure-1.vpc.cloudera.com:8020/warehouse/tablespace/managed/hive/sample_07', 'MANAGED_TABLE', 'NULL', 'COLUMN_STATS_ACCURATE', 'bucketing_version', 'numFiles', 'numRows', 'rawDataSize', 'totalSize', 'transactional', 'transactional_properties', 'transient_lastDdlTime', 'NULL', 'NULL', 'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe', 'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat', 'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat', 'No', '-1', '[]', '[]', 'NULL', 'serialization.format', 'NULL', 'NULL', 'NULL', 'NULL', 'default.pk', 'pk_165400321_1572980510006_0', 'id1 ', 'NULL'], nulls='')),
+          Mock(stringVal=Mock(values=['comment', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'comment', '', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', '{\"BASIC_STATS\":\"true\",\"COLUMN_STATS\":{\"code\":\"true\",\"description\":\"true\",\"salary\":\"true\",\"total_emp\":\"true\"}}', '2', '1', '822', '3288', '48445', 'true', 'insert_only', '1572882268', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', '1', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL'], nulls='')),
+        ]
+      )
+      desc_schema = Mock(
+        columns=[
+          Mock(columnName='col_name'),
+          Mock(columnName='data_type'),
+          Mock(columnName='comment')
+        ]
+      )
+
+      table = HiveServerTable(
+        table_results=table_results,
+        table_schema=table_schema,
+        desc_results=desc_results,
+        desc_schema=desc_schema
+      )
+
+      assert_equal(len(table.primary_keys), 1)
+      assert_equal(table.primary_keys[0].name, 'id1')
+      assert_equal(table.primary_keys[0].type, 'NULL')
+      assert_equal(table.primary_keys[0].comment, 'NULL')
+
+
+  def test_multi_primary_keys_hive(self):
 
       table_results = Mock()
       table_schema = Mock()