From 15a950245cda4a22db60db9f2193ee84b7b4f89b Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Wed, 3 Jul 2024 15:05:47 +0200 Subject: [PATCH] Add support for reducing PreparedStatement --- src/include/statement_simplifier.hpp | 2 ++ src/statement_simplifier.cpp | 8 ++++++++ test/sql/sql_reduce.test | 15 ++++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/include/statement_simplifier.hpp b/src/include/statement_simplifier.hpp index 0f23843..f27b0d6 100644 --- a/src/include/statement_simplifier.hpp +++ b/src/include/statement_simplifier.hpp @@ -16,6 +16,7 @@ class SQLStatement; class SelectStatement; class InsertStatement; class UpdateStatement; +class PrepareStatement; class DeleteStatement; class TableRef; class SelectNode; @@ -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(); diff --git a/src/statement_simplifier.cpp b/src/statement_simplifier.cpp index 66662f4..60f466f 100644 --- a/src/statement_simplifier.cpp +++ b/src/statement_simplifier.cpp @@ -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 @@ -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); @@ -445,6 +450,9 @@ void StatementSimplifier::Simplify(SQLStatement &stmt) { case StatementType::DELETE_STATEMENT: Simplify(stmt.Cast()); break; + case StatementType::PREPARE_STATEMENT: + Simplify(stmt.Cast()); + break; default: throw InvalidInputException("Expected a single SELECT, INSERT or UPDATE statement"); } diff --git a/test/sql/sql_reduce.test b/test/sql/sql_reduce.test index 1bfb3b1..428ae92 100644 --- a/test/sql/sql_reduce.test +++ b/test/sql/sql_reduce.test @@ -1,4 +1,4 @@ -# name: test/sql_reduce.test +# name: test/sql/sql_reduce.test # description: Test reduce SQL statement # group: [sqlsmith] @@ -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