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

 ##########
 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:
   Sorry, I replied to your comment but I think I resolved the conversation. 
This is used not just for bytes, but also for cpu and network cost, so 1000 is 
the correct unit. Also, 1000 is the correct unit for the prefixes K, M, G, etc. 
For 1024 the prefixes are Ki, Mi, Gi.
   
   Eg, 1024 B = 1 KiB = 1.024 KB.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

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

Reply via email to