Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into make-fuzzer-ci-use-…
Browse files Browse the repository at this point in the history
…reusable-wf
  • Loading branch information
hmeriann committed Aug 28, 2024
2 parents a937e1f + 7b00307 commit 83c353d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 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
2 changes: 2 additions & 0 deletions src/include/statement_simplifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class SQLStatement;
class SelectStatement;
class InsertStatement;
class UpdateStatement;
class PrepareStatement;
class DeleteStatement;
class TableRef;
class SelectNode;
Expand All @@ -41,6 +42,7 @@ class StatementSimplifier {
void Simplify(SelectStatement &stmt);
void Simplify(InsertStatement &stmt);
void Simplify(UpdateStatement &stmt);
void Simplify(PrepareStatement &stmt);
void Simplify(DeleteStatement &stmt);

void Simplification();
Expand Down
9 changes: 9 additions & 0 deletions src/statement_simplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "duckdb/parser/expression/list.hpp"
#include "duckdb/parser/statement/delete_statement.hpp"
#include "duckdb/parser/statement/insert_statement.hpp"
#include "duckdb/parser/statement/prepare_statement.hpp"
#include "duckdb/parser/statement/update_statement.hpp"
#include "duckdb/parser/statement/select_statement.hpp"
#endif
Expand Down Expand Up @@ -308,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 Expand Up @@ -423,6 +425,10 @@ void StatementSimplifier::Simplify(UpdateSetInfo &info) {
}
}

void StatementSimplifier::Simplify(PrepareStatement &stmt) {
Simplify(*stmt.statement);
}

void StatementSimplifier::Simplify(UpdateStatement &stmt) {
Simplify(stmt.cte_map);
SimplifyOptional(stmt.from_table);
Expand All @@ -445,6 +451,9 @@ void StatementSimplifier::Simplify(SQLStatement &stmt) {
case StatementType::DELETE_STATEMENT:
Simplify(stmt.Cast<DeleteStatement>());
break;
case StatementType::PREPARE_STATEMENT:
Simplify(stmt.Cast<PrepareStatement>());
break;
default:
throw InvalidInputException("Expected a single SELECT, INSERT or UPDATE statement");
}
Expand Down
15 changes: 14 additions & 1 deletion test/sql/sql_reduce.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# name: test/sql_reduce.test
# name: test/sql/sql_reduce.test
# description: Test reduce SQL statement
# group: [sqlsmith]

Expand Down Expand Up @@ -77,3 +77,16 @@ DELETE FROM a WHERE (i < 5000)
DELETE FROM a WHERE (i >= 2000)
DELETE FROM a WHERE (i AND (i < 5000))
DELETE FROM a WHERE NULL

query I
SELECT * FROM reduce_sql_statement('PREPARE v1 AS SELECT a, b FROM tbl') ORDER BY 1
----
PREPARE v1 AS SELECT NULL, b FROM tbl
PREPARE v1 AS SELECT NULL, b FROM tbl
PREPARE v1 AS SELECT a FROM tbl
PREPARE v1 AS SELECT a, NULL FROM tbl
PREPARE v1 AS SELECT a, NULL FROM tbl
PREPARE v1 AS SELECT a, b
PREPARE v1 AS SELECT a, b FROM tbl
PREPARE v1 AS SELECT a, b FROM tbl
PREPARE v1 AS SELECT b FROM tbl

0 comments on commit 83c353d

Please sign in to comment.