You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2019/09/09 18:20:39 UTC

[GitHub] [incubator-superset] etr2460 commented on a change in pull request #8172: Allow users to estimate query cost before executing it

etr2460 commented on a change in pull request #8172: Allow users to estimate query cost before executing it
URL: https://github.com/apache/incubator-superset/pull/8172#discussion_r322383757
 
 

 ##########
 File path: superset/db_engine_specs/presto.py
 ##########
 @@ -373,6 +377,55 @@ def select_star(
             presto_cols,
         )
 
+    @classmethod
+    def estimate_statement_cost(cls, statement, database, cursor, user_name):
+        db_engine_spec = database.db_engine_spec
+        parsed_query = ParsedQuery(statement)
+        sql = parsed_query.stripped()
+
+        SQL_QUERY_MUTATOR = config.get("SQL_QUERY_MUTATOR")
+        if SQL_QUERY_MUTATOR:
+            sql = SQL_QUERY_MUTATOR(sql, user_name, security_manager, database)
+
+        sql = f"EXPLAIN (TYPE IO, FORMAT JSON) {sql}"
+
+        db_engine_spec.execute(cursor, sql)
+        polled = cursor.poll()
+        while polled:
+            time.sleep(0.2)
+            polled = cursor.poll()
+        data = db_engine_spec.fetch_data(cursor, 1)
+
+        first = data[0][0]
+        result = json.loads(first)
+        estimate = result["estimate"]
+
+        def humanize(value, suffix):
+            if value == "NaN":
+                return value
+
+            prefix = ""
+            symbols = ["K", "M", "G", "T", "P", "E", "Z", "Y"]
+            while value > 1000 and symbols:
+                prefix = symbols.pop(0)
+                value //= 1000
+
+            return f"{value} {prefix}{suffix}"
+
+        cost = {}
+        columns = [
 
 Review comment:
   What happens if the presto format changes? Will we need to make changes here? I know presto likes to add a bunch of breaking changes in new versions...

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org