Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for reducing PreparedStatement #8

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 8 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 @@ -423,6 +424,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 +450,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
Loading