Skip to content

Commit

Permalink
Merge pull request #9 from Mytherin/expandreducescript
Browse files Browse the repository at this point in the history
Expand reduce script and add simplification of case statements with multiple WHEN/THEN branches
  • Loading branch information
Mytherin authored Aug 27, 2024
2 parents 54a7c8d + f9e6e55 commit 7b00307
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
28 changes: 25 additions & 3 deletions scripts/reduce_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
SELECT * FROM reduce_sql_statement('${QUERY}');
'''

verbose = False

class MultiStatementManager:
delimiter = ';'
Expand All @@ -33,7 +34,8 @@ def __init__(self, multi_statement):
self.statements.append(stmt.strip() + ";")

def is_multi_statement(sql_statement):
if len(sql_statement.split(';')) > 1:
splits = [x for x in sql_statement.split(';') if len(x.strip()) > 0]
if len(splits) > 1:
return True
return False

Expand All @@ -45,10 +47,12 @@ def sanitize_error(err):
err = re.sub(r'Error: near line \d+: ', '', err)
err = err.replace(os.getcwd() + '/', '')
err = err.replace(os.getcwd(), '')
err = re.sub(r'LINE \d+:.*\n', '', err)
err = re.sub(r' *\^ *', '', err)
if 'AddressSanitizer' in err:
match = re.search(r'[ \t]+[#]0 ([A-Za-z0-9]+) ([^\n]+)', err).groups()[1]
err = 'AddressSanitizer error ' + match
return err
return err.strip()


def run_shell_command(shell, cmd):
Expand All @@ -69,7 +73,13 @@ def get_reduced_sql(shell, sql_query):
raise Exception("Failed to reduce query")
reduce_candidates = []
for line in stdout.split('\n'):
reduce_candidates.append(line.strip('"').replace('""', '"'))
if len(line) <= 2:
continue
if line[0] == '"':
line = line[1:]
if line[len(line) - 1] == '"':
line = line[:len(line) - 1]
reduce_candidates.append(line.replace('""', '"'))
return reduce_candidates[1:]


Expand All @@ -95,6 +105,15 @@ def reduce(sql_query, data_load, shell, error_msg, max_time_seconds=300):
print(sql_query)
print("=======================")
break
elif verbose:
print("Failed to reduce query")
print("=======================")
print(reduce_candidate)
print("=====Target error======")
print(error_msg)
print("=====Actual error======")
print(new_error)
print("=======================")
if not found_new_candidate:
break
return sql_query
Expand Down Expand Up @@ -271,13 +290,16 @@ def reduce_query_log(queries, shell, data_load=[], max_time_seconds=300):
parser.add_argument(
'--max-time', dest='max_time', action='store', help='Maximum time in seconds to run the reducer', default=300
)
parser.add_argument(
'--verbose', dest='verbose', action='store_true', help='Verbose output')

args = parser.parse_args()
print("Starting reduce process")

shell = args.shell
data_load = open(args.load).read()
sql_query = open(args.exec).read()
verbose = args.verbose
(stdout, stderr, returncode) = run_shell_command(shell, data_load + sql_query)
expected_error = sanitize_error(stderr).strip()
if len(expected_error) == 0:
Expand Down
1 change: 1 addition & 0 deletions src/statement_simplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ void StatementSimplifier::SimplifyExpression(duckdb::unique_ptr<ParsedExpression
SimplifyChildExpression(expr, case_check.then_expr);
SimplifyChildExpression(expr, case_check.when_expr);
}
SimplifyList(op.case_checks, false);
break;
}
case ExpressionClass::CAST: {
Expand Down

0 comments on commit 7b00307

Please sign in to comment.