def aggregate_explain(query_plan): # For plans of the aggregate type: SortAggregate, HashAggregate, PlainAggregate strategy = query_plan["Strategy"] if strategy == "Sorted": result = "The rows are sorted based on their keys." if "Group Key" in query_plan: result += f" They are {bold_string('aggregated')} by the following keys: " for key in query_plan["Group Key"]: result += bold_string(key) + "," result = result[:-1] result += "." if "Filter" in query_plan: result += " They are filtered by " + bold_string( query_plan["Filter"].replace("::text", "")) result += "." return result elif strategy == "Hashed": result = "It hashes all rows based on the following key(s): " for key in query_plan["Group Key"]: result += bold_string(key.replace("::text", "")) + ", " result += f"which are then {bold_string('aggregated')} into bucket given by the hashed key." return result elif strategy == "Plain": return f"Result is simply {bold_string('aggregated')} as normal." else: raise ValueError("Aggregate_explain does not work for strategy: " + strategy)
def sort_explain(query_plan): result = f"The result is {bold_string('sorted')} using the attribute " if "DESC" in query_plan["Sort Key"]: result += ( bold_string(str(query_plan["Sort Key"].replace("DESC", ""))) + " in descending order") elif "INC" in query_plan["Sort Key"]: result += ( bold_string(str(query_plan["Sort Key"].replace("INC", ""))) + " in increasing order") else: result += bold_string(str(query_plan["Sort Key"])) result += "." return result
def cte_explain(query_plan): result = ( f"A {bold_string('CTE scan')} is performed on the table " + bold_string(str(query_plan["CTE Name"])) + " which is stored in memory " ) if "Index Cond" in query_plan: result += " with condition(s) " + bold_string( query_plan["Index Cond"].replace("::text", "") ) if "Filter" in query_plan: result += " and then filtered by " + bold_string( query_plan["Filter"].replace("::text", "") ) result += "." return result
def group_explain(query_plan): result = f"The result from the previous operation is {bold_string('grouped')} together using the following keys: " for i, key in enumerate(query_plan["Group Key"]): result += bold_string(key.replace("::text", "")) if i == len(query_plan["Group Key"]) - 1: result += "." else: result += ", " return result
def merge_join_explain(query_plan): result = f"The results from sub-operations are joined using {bold_string('Merge Join')}" if "Merge Cond" in query_plan: result += " with condition " + bold_string( query_plan["Merge Cond"].replace("::text", "")) if "Join Type" == "Semi": result += " but only the row from the left relation is returned" result += "." return result
def seq_scan_explain(query_plan): sentence = f"It does a {bold_string('sequential scan')} on relation " if "Relation Name" in query_plan: sentence += bold_string(query_plan["Relation Name"]) if "Alias" in query_plan: if query_plan["Relation Name"] != query_plan["Alias"]: sentence += " with an alias of {}".format(query_plan["Alias"]) if "Filter" in query_plan: sentence += " and filtered with the condition {}".format( query_plan["Filter"].replace("::text", "")) sentence += "." return sentence
def default_explain(query_plan): return "The {} is performed.".format(bold_string(query_plan["Node Type"]))