Skip to content

Commit

Permalink
Merge pull request #8 from Mytherin/reduceprepare
Browse files Browse the repository at this point in the history
Add support for reducing PreparedStatement
  • Loading branch information
Mytherin authored Aug 27, 2024
2 parents cadca1b + 15a9502 commit 54a7c8d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
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

0 comments on commit 54a7c8d

Please sign in to comment.