Browse Source

HUE-2838 [metastore] Offer to filter partition on the list of partitions page

Update describe_partitions view to accept partition filter parameters, but default partition_spec to None. Change describe_partitions to use form-encoded data instead of JSON.
Jenny Kim 10 years ago
parent
commit
9b9caf3173

+ 5 - 5
apps/beeswax/src/beeswax/server/dbms.py

@@ -192,7 +192,7 @@ class HiveServer2Dbms(object):
       limit = min(100, BROWSE_PARTITIONED_TABLE_LIMIT.get())
       partition_query = ""
       if table.partition_keys:
-        partitions = self.get_partitions(database, table, 1)
+        partitions = self.get_partitions(database, table, partition_spec=None, max_parts=1)
         partition_query = 'WHERE ' + ' AND '.join(["%s='%s'" % (table.partition_keys[idx].name, key) for idx, key in enumerate(partitions[0].values)])
       hql = "SELECT * FROM `%s`.`%s` %s LIMIT %s" % (database, table.name, partition_query, limit)
       query = hql_query(hql)
@@ -575,16 +575,16 @@ class HiveServer2Dbms(object):
     return self.client.close(handle)
 
 
-  def get_partitions(self, db_name, table, max_parts=None, reverse_sort=True):
+  def get_partitions(self, db_name, table, partition_spec=None, max_parts=None, reverse_sort=True):
     if max_parts is None or max_parts > BROWSE_PARTITIONED_TABLE_LIMIT.get():
       max_parts = BROWSE_PARTITIONED_TABLE_LIMIT.get()
 
-    return self.client.get_partitions(db_name, table.name, max_parts, reverse_sort)
+    return self.client.get_partitions(db_name, table.name, partition_spec, max_parts, reverse_sort)
 
 
   def get_partition(self, db_name, table_name, partition_id):
     table = self.get_table(db_name, table_name)
-    partitions = self.get_partitions(db_name, table, max_parts=None)
+    partitions = self.get_partitions(db_name, table, partition_spec=None, max_parts=None)
 
     partition_query = ""
     for idx, key in enumerate(partitions[partition_id].values):
@@ -597,7 +597,7 @@ class HiveServer2Dbms(object):
 
   def describe_partition(self, db_name, table_name, partition_id):
     table = self.get_table(db_name, table_name)
-    partitions = self.get_partitions(db_name, table, max_parts=None)
+    partitions = self.get_partitions(db_name, table, partition_spec=None, max_parts=None)
 
     parts = ["%s='%s'" % (table.partition_keys[idx].name, key) for idx, key in enumerate(partitions[partition_id].values)]
     partition_spec = ','.join(parts)

+ 9 - 5
apps/beeswax/src/beeswax/server/hive_server2_lib.py

@@ -757,7 +757,7 @@ class HiveServerClient:
       return 'Server does not support GetLog()'
 
 
-  def get_partitions(self, database, table_name, max_parts, reverse_sort=True):
+  def get_partitions(self, database, table_name, partition_spec=None, max_parts=None, reverse_sort=True):
     table = self.get_table(database, table_name)
 
     if max_parts is None or max_parts <= 0:
@@ -765,9 +765,13 @@ class HiveServerClient:
     else:
       max_rows = 1000 if max_parts <= 250 else max_parts
 
-    partitionTable = self.execute_query_statement('SHOW PARTITIONS `%s`.`%s`' % (database, table_name), max_rows=max_rows)
+    query = 'SHOW PARTITIONS `%s`.`%s`' % (database, table_name)
+    if partition_spec:
+      query += ' PARTITION(%s)' % partition_spec
 
-    partitions = [PartitionValueCompatible(partition, table) for partition in partitionTable.rows()][-max_parts:]
+    partition_table = self.execute_query_statement(query, max_rows=max_rows)
+
+    partitions = [PartitionValueCompatible(partition, table) for partition in partition_table.rows()][-max_parts:]
 
     if reverse_sort:
       partitions.reverse()
@@ -994,8 +998,8 @@ class HiveServerClientCompatible(object):
   def get_partition(self, *args, **kwargs): raise NotImplementedError()
 
 
-  def get_partitions(self, database, table_name, max_parts, reverse_sort=True):
-    return self._client.get_partitions(database, table_name, max_parts, reverse_sort)
+  def get_partitions(self, database, table_name, partition_spec, max_parts, reverse_sort=True):
+    return self._client.get_partitions(database, table_name, partition_spec, max_parts, reverse_sort)
 
 
   def alter_partition(self, db_name, tbl_name, new_part): raise NotImplementedError()

+ 9 - 2
apps/metastore/src/metastore/views.py

@@ -154,7 +154,7 @@ def describe_table(request, database, table):
 
   partitions = None
   if app_name != 'impala' and table.partition_keys:
-    partitions = db.get_partitions(database, table, max_parts=None)
+    partitions = db.get_partitions(database, table, partition_spec=None, max_parts=None)
 
   try:
     table_data = db.get_sample(database, table)
@@ -257,12 +257,19 @@ def describe_partitions(request, database, table):
   db = dbms.get(request.user)
 
   table_obj = db.get_table(database, table)
+
   if not table_obj.partition_keys:
     raise PopupException(_("Table '%(table)s' is not partitioned.") % {'table': table})
 
   reverse_sort = request.REQUEST.get("sort", "desc").lower() == "desc"
 
-  partitions = db.get_partitions(database, table_obj, max_parts=None, reverse_sort=reverse_sort)
+  partition_filters = {}
+  for part in table_obj.partition_keys:
+    if request.REQUEST.get(part.name):
+      partition_filters[part.name] = request.REQUEST.get(part.name)
+  partition_spec = ','.join(["%s='%s'" % (k, v) for k, v in partition_filters.items()])
+
+  partitions = db.get_partitions(database, table_obj, partition_spec, max_parts=None, reverse_sort=reverse_sort)
 
   return render("describe_partitions.mako", request, {
     'breadcrumbs': [{