Răsfoiți Sursa

HUE-8362 [rdbms] Explain button is not working (#712)

Ivan Dzikovsky 7 ani în urmă
părinte
comite
d0cd7f911e

+ 8 - 1
desktop/libs/librdbms/src/librdbms/server/rdbms_base_lib.py

@@ -17,6 +17,8 @@
 
 import logging
 
+from librdbms.design import SQLdesign
+
 
 LOG = logging.getLogger(__name__)
 
@@ -83,7 +85,12 @@ class BaseRDMSClient(object):
 
 
   def explain(self, query):
-    q = query.get_query_statement(0)
+    if isinstance(query, SQLdesign):
+      # Backward compatibility with rdbms app
+      q = query.get_query_statement(0)
+    else:
+      q = query
+
     if q.upper().startswith('EXPLAIN'):
       return self.execute_statement(q)
     else:

+ 29 - 0
desktop/libs/notebook/src/notebook/connectors/rdbms.py

@@ -174,6 +174,35 @@ class RdbmsApi(Api):
   def get_browse_query(self, snippet, database, table, partition_spec=None):
     return "SELECT * FROM `%s`.`%s` LIMIT 1000" % (database, table)
 
+  @query_error_handler
+  def explain(self, notebook, snippet):
+    query_server = dbms.get_query_server_config(server=self.interpreter)
+    db = dbms.get(self.user, query_server)
+
+    db.use(snippet['database'])
+    result = db.explain(snippet['statement'])
+
+    rows = list(result.rows())
+    cols = result.cols()
+
+    # Prettify output
+    explanation = ""
+    cols_pretty = [(col + ":  ") for col in cols]
+    col_width = max(len(col) for col in cols_pretty)
+    for index, col in enumerate(cols_pretty):
+      lines = []
+      for row in rows:
+        lines += str(row[index]).split("\n")
+      explanation += col.ljust(col_width) + lines[0] + "\n"
+      for line in lines[1:]:
+        explanation += (" " * col_width) + line + "\n"
+
+    return {
+      'status': 0,
+      'explanation': explanation,
+      'statement': snippet['statement'],
+    }
+
 
 class Assist():