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/13 05:48:23 UTC

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

villebro 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_r324043052
 
 

 ##########
 File path: superset/db_engine_specs/presto.py
 ##########
 @@ -373,6 +380,79 @@ def select_star(
             presto_cols,
         )
 
+    @classmethod
+    def estimate_statement_cost(
+        cls, statement: str, database, cursor, user_name: str
+    ) -> Dict[str, str]:
+        """
+        Generate a SQL query that estimates the cost of a given statement.
+
+        :param statement: A single SQL statement
+        :param database: Database instance
+        :param cursor: Cursor instance
+        :param username: Effective username
+        """
+        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()
+
+        # the output from Presto is a single column and a single row containing
+        # JSON:
+        #
+        #   {
+        #     ...
+        #     "estimate" : {
+        #       "outputRowCount" : 8.73265878E8,
+        #       "outputSizeInBytes" : 3.41425774958E11,
+        #       "cpuCost" : 3.41425774958E11,
+        #       "maxMemory" : 0.0,
+        #       "networkCost" : 3.41425774958E11
+        #     }
+        #   }
+        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
+
+            prefixes = ["K", "M", "G", "T", "P", "E", "Z", "Y"]
+            prefix = ""
+            to_next_prefix = 1000
 
 Review comment:
   I had the same concern as @etr2460 , learned something new here (had to google to double check) 👍 

----------------------------------------------------------------
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