Repository: duckdb/duckdb (language: python)

Issue:
SET/RESET not able to be used in prepared statements
### What happens?

```python
import duckdb
duckdb.execute("SET s3_region=?",["us-west-1"])
```

doesn't work

### To Reproduce

^^

### OS:

mac

### DuckDB Version:

1

### DuckDB Client:

python

### Full Name:

Andrew Grosser

### Affiliation:

Sourcetable

### What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.

I have not tested with any build

### Did you include all relevant data sets for reproducing the issue?

No - Other reason (please specify in the issue body)

### Did you include all code required to reproduce the issue?

- [X] Yes, I have

### Did you include all relevant configuration (e.g., CPU architecture, Python version, Linux distribution) to reproduce the issue?

- [X] Yes, I have
SET/RESET not able to be used in prepared statements
### What happens?

```python
import duckdb
duckdb.execute("SET s3_region=?",["us-west-1"])
```

doesn't work

### To Reproduce

^^

### OS:

mac

### DuckDB Version:

1

### DuckDB Client:

python

### Full Name:

Andrew Grosser

### Affiliation:

Sourcetable

### What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.

I have not tested with any build

### Did you include all relevant data sets for reproducing the issue?

No - Other reason (please specify in the issue body)

### Did you include all code required to reproduce the issue?

- [X] Yes, I have

### Did you include all relevant configuration (e.g., CPU architecture, Python version, Linux distribution) to reproduce the issue?

- [X] Yes, I have


Discussion / hints:

Fix patch:
diff --git a/src/main/client_context.cpp b/src/main/client_context.cpp
index d7dc9a9a1525..31389c69fbfc 100644
--- a/src/main/client_context.cpp
+++ b/src/main/client_context.cpp
@@ -450,6 +450,7 @@ void ClientContext::RebindPreparedStatement(ClientContextLock &lock, const strin
 	auto new_prepared =
 	    CreatePreparedStatement(lock, query, prepared->unbound_statement->Copy(), parameters.parameters);
 	D_ASSERT(new_prepared->properties.bound_all_parameters);
+	new_prepared->properties.parameter_count = prepared->properties.parameter_count;
 	prepared = std::move(new_prepared);
 	prepared->properties.bound_all_parameters = false;
 }


Test patch:
diff --git a/test/api/test_prepared_api.cpp b/test/api/test_prepared_api.cpp
index 98724ca4ab30..1d8c45b6dc91 100644
--- a/test/api/test_prepared_api.cpp
+++ b/test/api/test_prepared_api.cpp
@@ -476,3 +476,23 @@ TEST_CASE("Test ambiguous prepared statement parameter types", "[api]") {
 	result = prep->Execute("hello");
 	REQUIRE(CHECK_COLUMN(result, 0, {"hello"}));
 }
+
+TEST_CASE("Test prepared statements with SET", "[api]") {
+	duckdb::unique_ptr<QueryResult> result;
+	DuckDB db(nullptr);
+	Connection con(db);
+	con.EnableQueryVerification();
+
+	// create a prepared statement and use it to query
+	auto prepare = con.Prepare("SET default_null_order=$1");
+	REQUIRE(prepare->success);
+
+	// too many parameters
+	REQUIRE_FAIL(prepare->Execute("xxx", "yyy"));
+	// too few parameters
+	REQUIRE_FAIL(prepare->Execute());
+	// unsupported setting
+	REQUIRE_FAIL(prepare->Execute("unsupported_mode"));
+	// this works
+	REQUIRE_NO_FAIL(prepare->Execute("NULLS FIRST"));
+}