Ver código fonte

HUE-9070 [editor] API for retrieving Table Primary Keys

Romain 6 anos atrás
pai
commit
3edcf3a026

+ 12 - 0
apps/beeswax/src/beeswax/server/hive_server2_lib.py

@@ -137,6 +137,18 @@ class HiveServerTable(Table):
       # Not partitioned
       return []
 
+  @property
+  def primary_keys(self):
+    rows = self.describe
+    try:
+      col_row_index = list(map(itemgetter('col_name'), rows)).index('# Primary Key') + 2
+      keys = rows[col_row_index:]
+    except:
+      # No info (e.g. IMPALA-8291)
+      keys = []
+
+    return [PartitionKeyCompatible(row['data_type'], 'NULL', row['comment']) for row in keys]
+
   @property
   def comment(self):
     return HiveServerTRow(self.table, self.table_schema).col('REMARKS')

+ 37 - 0
apps/beeswax/src/beeswax/server/hive_server2_lib_tests.py

@@ -167,3 +167,40 @@ class TestHiveServerTable():
       assert_equal(table.partition_keys[0].name, 'date')
       assert_equal(table.partition_keys[0].type, 'string')
       assert_equal(table.partition_keys[0].comment, '')
+
+
+  def test_primary_keys_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:', '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', 'default.pk', 'pk_165400321_1572980510006_0', 'id1', 'id2'], 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'], 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), 2)
+      assert_equal(table.primary_keys[0].name, 'id1')
+      assert_equal(table.primary_keys[0].type, 'NULL')
+      assert_equal(table.primary_keys[0].comment, 'NULL')
+
+      assert_equal(table.primary_keys[1].name, 'id2')
+      assert_equal(table.primary_keys[1].type, 'NULL')
+      assert_equal(table.primary_keys[1].comment, 'NULL')