8000 Support CREATE TABLE AS ... WITH NO DATA by hannes · Pull Request #16586 · duckdb/duckdb · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support CREATE TABLE AS ... WITH NO DATA #16586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/parser/transform/statement/transform_create_table_as.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "duckdb/parser/statement/create_statement.hpp"
#include "duckdb/parser/expression/constant_expression.hpp"
#include "duckdb/parser/parsed_data/create_table_info.hpp"
#include "duckdb/parser/query_node/select_node.hpp"
#include "duckdb/parser/tableref/subqueryref.hpp"
#include "duckdb/parser/transformer.hpp"

namespace duckdb {
Expand All @@ -20,6 +23,19 @@ unique_ptr<CreateStatement> Transformer::TransformCreateTableAs(duckdb_libpgquer
auto qname = TransformQualifiedName(*stmt.into->rel);
auto query = TransformSelectStmt(*stmt.query, false);

// push a LIMIT 0 if 'WITH NO DATA' is specified
if (stmt.into->skipData) {
auto limit_modifier = make_uniq<LimitModifier>();
limit_modifier->limit = make_uniq<ConstantExpression>(Value::BIGINT(0));
auto limit_node = make_uniq<SelectNode>();
limit_node->modifiers.push_back(std::move(limit_modifier));
limit_node->from_table = make_uniq<SubqueryRef>(std::move(query));
limit_node->select_list.push_back(make_uniq<StarExpression>());
auto limit_stmt = make_uniq<SelectStatement>();
limit_stmt->node = std::move(limit_node);
query = std::move(limit_stmt);
}

if (stmt.into->colNames) {
auto cols = TransformStringList(stmt.into->colNames);
for (idx_t i = 0; i < cols.size(); i++) {
Expand Down
19 changes: 18 additions & 1 deletion test/sql/create/create_as.test
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,21 @@ SELECT * FROM tbl6;
statement error
CREATE TABLE tbl7(col1, col2) AS SELECT 5;
----
Binder Error: Target table has more colum names than query result.
Binder Error: Target table has more colum names than query result.

# WITH NO DATA / WITH DATA
statement ok
CREATE TABLE tbl8 AS SELECT 42 WITH NO DATA

query I
SELECT COUNT(*) FROM tbl8;
----
0

statement ok
CREATE TABLE tbl9 AS SELECT 42 WITH DATA

query I
SELECT COUNT(*) FROM tbl9;
----
1
Loading
0