Repository: duckdb/duckdb (language: python)

Issue:
json_deserialize_sql(): Error: Attempted to dereference unique_ptr that is NULL
### What happens?

Playing around with `json_deserialize_sql()` with some malformed JSON leads to this error:

```
duckdb.duckdb.InvalidInputException: Invalid Input Error: Attempting to execute an unsuccessful or closed pending query result
Error: INTERNAL Error: Attempted to dereference unique_ptr that is NULL!
This error signals an assertion failure within DuckDB. This usually occurs due to unexpected conditions or errors in the program's logic.
For more information, see https://duckdb.org/docs/dev/internal_errors
```


I know that the JSON that I've provided isn't actually a valid serialized statement, I was just testing the capabilities if it would the `json_deserialize_sql()` would deserialize a part of a statement.



### To Reproduce

```python
import duckdb
import json

con = duckdb.connect(":memory:")

filter_json = {
    "expression_class": "BOUND_COMPARISON",
    "type": "COMPARE_EQUAL",
    "left": {
        "expression_class": "BOUND_COLUMN_REF",
        "type": "BOUND_COLUMN_REF",
        "alias": "r",
        "return_type": {"id": "VARCHAR"},
    },
    "right": {
        "expression_class": "BOUND_CONSTANT",
        "type": "VALUE_CONSTANT",
        "value": {
            "type": {"id": "VARCHAR"},
            "is_null": False,
            "value": "u",
        },
    },
}

result = con.execute(
    "select json_deserialize_sql(?)", [json.dumps({"statements": [filter_json]})]
).fetchall()
```

### OS:

Mac OS X

### DuckDB Version:

v1.0.0 1f98600c2c

### DuckDB Client:

Python

### Full Name:

Rusty Conover

### Affiliation:

self

### What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.

I have tested with a stable release

### Did you include all relevant data sets for reproducing the issue?

Yes

### 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/extension/json/json_functions/json_serialize_sql.cpp b/extension/json/json_functions/json_serialize_sql.cpp
index 17fd333647a3..2873eba31bec 100644
--- a/extension/json/json_functions/json_serialize_sql.cpp
+++ b/extension/json/json_functions/json_serialize_sql.cpp
@@ -196,7 +196,11 @@ static unique_ptr<SelectStatement> DeserializeSelectStatement(string_t input, yy
 	}
 	auto stmt_json = yyjson_arr_get(statements, 0);
 	JsonDeserializer deserializer(stmt_json, doc);
-	return SelectStatement::Deserialize(deserializer);
+	auto stmt = SelectStatement::Deserialize(deserializer);
+	if (!stmt->node) {
+		throw ParserException("Error parsing json: no select node found in json");
+	}
+	return stmt;
 }
 
 //----------------------------------------------------------------------


Test patch:
diff --git a/test/sql/json/test_json_serialize_sql.test b/test/sql/json/test_json_serialize_sql.test
index d870c0c34c80..07c29f2db0d9 100644
--- a/test/sql/json/test_json_serialize_sql.test
+++ b/test/sql/json/test_json_serialize_sql.test
@@ -90,3 +90,14 @@ query II
 SELECT * FROM json_execute_serialized_sql(json_serialize_sql('WITH a(i) as (SELECT 1) SELECT a1.i as i1, a2.i as i2 FROM a as a1, a as a2'));
 ----
 1	1
+
+# Missing select nodes should throw an error
+statement error
+SELECT json_deserialize_sql('{ "statements": [ {"expression_class": "BOUND_COMPARISON"}]}');
+----
+Parser Error: Error parsing json: no select node found in json
+
+statement error
+SELECT * FROM json_execute_serialized_sql('{ "statements": [ {"expression_class": "BOUND_COMPARISON"}]}');
+----
+Parser Error: Error parsing json: no select node found in json
\ No newline at end of file