Skip to content

Commit

Permalink
Fix incorrect time issue in server-side-execution and inconsisten res…
Browse files Browse the repository at this point in the history
…ults problem
  • Loading branch information
gauravk-in committed Dec 16, 2024
1 parent 251cf4d commit 26fdd17
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,29 @@ def remove_with_ordinality(sql_str):
return sql_str


def get_result(cur, is_dml: bool, has_order_by: bool, has_limit: bool):
def get_result_for_consistency_check(cur, is_dml: bool, has_order_by: bool, has_limit: bool):
if is_dml:
return cur.rowcount, f"{cur.rowcount} updates"

# if there is a limit without order by we can't validate results
str_result = []
cardinality = 0
if has_limit and not has_order_by:
str_result = ["LIMIT_WITHOUT_ORDER_BY"]
else:
result = cur.fetchall()

for row in result:
cardinality += 1
for column_value in row:
str_result.append(f"{str(column_value)}")

if not has_order_by:
str_result.sort()

return cardinality, ''.join(str_result)

def get_result(cur, is_dml: bool):
if is_dml:
return cur.rowcount, f"{cur.rowcount} updates"

Expand All @@ -48,13 +70,6 @@ def get_result(cur, is_dml: bool, has_order_by: bool, has_limit: bool):
for column_value in row:
str_result.append(f"{str(column_value)}")

if not has_order_by:
str_result.sort()

# if there is a limit without order by we can't validate results
if has_limit and not has_order_by:
str_result = ["LIMIT_WITHOUT_ORDER_BY"]

return cardinality, ''.join(str_result)


Expand All @@ -70,7 +85,7 @@ def calculate_avg_execution_time(cur,
query_str_lower = query_str.lower() if query_str is not None else None

has_order_by = query.has_order_by
has_limit = True if "limit" in query_str_lower else False
has_limit = True if " limit " in query_str_lower else False

with_analyze = query_with_analyze(query_str_lower)
is_dml = query_is_dml(query_str_lower)
Expand Down Expand Up @@ -98,14 +113,14 @@ def calculate_avg_execution_time(cur,
# using first iteration as a result collecting step
# even if EXPLAIN ANALYZE is explain query
query.parameters = evaluate_sql(cur, query.get_query())
cardinality, result = get_result(cur, is_dml, has_order_by, has_limit)
cardinality, result = get_result_for_consistency_check(cur, is_dml, has_order_by, has_limit)

query.result_cardinality = cardinality
query.result_hash = get_md5(result)
else:
if iteration < num_warmup:
query.parameters = evaluate_sql(cur, query_str)
_, result = get_result(cur, is_dml, has_order_by, has_limit)
_, result = get_result(cur, is_dml)
else:
if not execution_plan_collected:
collect_execution_plan(cur, connection, query, sut_database)
Expand All @@ -118,7 +133,7 @@ def calculate_avg_execution_time(cur,

evaluate_sql(cur, query_str)
config.logger.debug("SQL >> Getting results")
_, result = get_result(cur, is_dml, has_order_by, has_limit)
_, result = get_result(cur, is_dml)

if with_analyze:
execution_times.append(extract_execution_time_from_analyze(result))
Expand Down

0 comments on commit 26fdd17

Please sign in to comment.