Repository: duckdb/duckdb (language: python)

Issue:
DuckDB SEGV when running ill-formed CREATE VIEW statement
### What happens?

The latest version of the DuckDB (v1.1.3 19864453f7 and latest main: v1.1.3-dev165 19864453f7) crashes when running the following SQL statement: 

```sql
CREATE VIEW any_view_name AS ( SHOW ALL TABLES );
```

Here is the stack trace from v1.1.3-dev165 19864453f7:

```
(gdb) bt
#0  0x0000aaaadba322f0 in duckdb::Transformer::TransformPivotStatement (this=0xffffe4d4e090, select=...) at /home/duckdb/duckdb/src/parser/transform/statement/transform_pivot_stmt.cpp:138
#1  0x0000aaaadba37da4 in duckdb::Transformer::TransformSelectNodeInternal (this=this@entry=0xffffe4d4e090, select=..., is_select=is_select@entry=false) at /home/duckdb/duckdb/src/parser/transform/statement/transform_select.cpp:31
#2  0x0000aaaadba39e4c in duckdb::Transformer::TransformSelectStmt (this=this@entry=0xffffe4d4e090, select=..., is_select=is_select@entry=false) at ../../src/include/duckdb/common/unique_ptr.hpp:39
#3  0x0000aaaadba3a018 in duckdb::Transformer::TransformCreateView (this=this@entry=0xffffe4d4e090, stmt=...) at ../../src/include/duckdb/common/optional_ptr.hpp:42
#4  0x0000aaaadb210e8c in duckdb::Transformer::TransformStatementInternal (this=this@entry=0xffffe4d4e090, stmt=...) at /home/duckdb/duckdb/src/parser/transformer.cpp:153
#5  0x0000aaaadb211130 in duckdb::Transformer::TransformStatement (this=this@entry=0xffffe4d4e090, stmt=...) at /home/duckdb/duckdb/src/parser/transformer.cpp:59
#6  0x0000aaaadb211038 in duckdb::Transformer::TransformStatementInternal (this=this@entry=0xffffe4d4e090, stmt=...) at /home/duckdb/duckdb/src/parser/transformer.cpp:139
#7  0x0000aaaadb211130 in duckdb::Transformer::TransformStatement (this=this@entry=0xffffe4d4e090, stmt=...) at /home/duckdb/duckdb/src/parser/transformer.cpp:59
#8  0x0000aaaadb211224 in duckdb::Transformer::TransformParseTree (this=this@entry=0xffffe4d4e090, tree=<optimized out>, statements=...) at ../../src/include/duckdb/common/optional_ptr.hpp:42
#9  0x0000aaaadb215648 in duckdb::Parser::ParseQuery (this=this@entry=0xffffe4d4e3f8, query="CREATE VIEW any_view_name AS ( SHOW ALL TABLES );") at /home/duckdb/duckdb/src/parser/parser.cpp:219
#10 0x0000aaaadb1f4ecc in duckdb_shell_sqlite3_prepare_v2 (db=0xaaaae503c8c0, zSql=0xaaaae503a290 "CREATE VIEW any_view_name AS ( SHOW ALL TABLES );", nByte=<optimized out>, ppStmt=0xffffe4d4e620, pzTail=0xffffe4d4e628)
    at ../../tools/sqlite3_api_wrapper/sqlite3_api_wrapper.cpp:176
#11 0x0000aaaadb1e0f68 in shell_exec (pArg=0xffffe4d4e8b0, zSql=0xaaaae503a290 "CREATE VIEW any_view_name AS ( SHOW ALL TABLES );", pzErrMsg=<optimized out>) at ../../tools/shell/shell.c:12993
#12 0x0000aaaadb1e2a28 in runOneSqlLine (p=0xffffe4d4e8b0, zSql=0xaaaae503a290 "CREATE VIEW any_view_name AS ( SHOW ALL TABLES );", startline=<optimized out>, in=<optimized out>) at ../../tools/shell/shell.c:19273
#13 0x0000aaaadb1e9bc0 in process_input (p=0xffffe4d4e8b0) at ../../tools/shell/shell.c:19384
#14 0x0000aaaadb1bfb90 in main (argc=<optimized out>, argv=0xffffe4d4fc18) at ../../tools/shell/shell.c:20206
```

### To Reproduce

1. Clone the DuckDB Git from the official repo.
2. Checkout to either the latest main (19864453f7) or release version: 1.1.3 (19864453f7).
3. Compile the DuckDB binary by using either `make reldebug` or `make debug`.
4. Run the compiled DuckDB and input the following SQL:

```
CREATE VIEW any_view_name AS ( SHOW ALL TABLES );
```

### OS:

Ubuntu 20.04 LTS

### DuckDB Version:

1.1.3

### DuckDB Client:

cli

### Hardware:

_No response_

### Full Name:

Yu Liang

### Affiliation:

Pennsylvania State University

### 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/src/parser/transform/statement/transform_create_view.cpp b/src/parser/transform/statement/transform_create_view.cpp
index 8504ecfc9a91..24a26c022d0d 100644
--- a/src/parser/transform/statement/transform_create_view.cpp
+++ b/src/parser/transform/statement/transform_create_view.cpp
@@ -21,7 +21,7 @@ unique_ptr<CreateStatement> Transformer::TransformCreateView(duckdb_libpgquery::
 	}
 	info->on_conflict = TransformOnConflict(stmt.onconflict);
 
-	info->query = TransformSelectStmt(*PGPointerCast<duckdb_libpgquery::PGSelectStmt>(stmt.query), false);
+	info->query = TransformSelectStmt(*stmt.query, false);
 
 	PivotEntryCheck("view");
 


Test patch:
diff --git a/test/sql/pragma/test_show_tables.test b/test/sql/pragma/test_show_tables.test
index f8b01c5c1f4d..47bc30910fe5 100644
--- a/test/sql/pragma/test_show_tables.test
+++ b/test/sql/pragma/test_show_tables.test
@@ -120,3 +120,17 @@ query TTTITI
 DESCRIBE v1
 ----
 k	DATE	YES	NULL	NULL	NULL
+
+# view over show tables
+statement ok
+CREATE VIEW show_tables_view AS ( SHOW TABLES );
+
+query T
+SELECT * FROM show_tables_view ORDER BY ALL
+----
+integers
+select
+show_tables_view
+t2
+tbl
+v1