Skip to content

Commit

Permalink
Add fallback for simple CTAS
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyang01 committed Jan 8, 2025
1 parent 472cbd6 commit c80adc8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 34 deletions.
9 changes: 8 additions & 1 deletion backend/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,19 @@ func (b *DuckBuilder) Build(ctx *sql.Context, root sql.Node, r sql.Row) (sql.Row
}
return b.base.Build(ctx, root, r)
// ResolvedTable is for `SELECT * FROM table` and `TABLE table`
// SubqueryAlias is for `SELECT * FROM view``
// SubqueryAlias is for `SELECT * FROM view`
case *plan.ResolvedTable, *plan.SubqueryAlias, *plan.TableAlias:
return b.executeQuery(ctx, node, conn)
case *plan.Distinct, *plan.OrderedDistinct:
return b.executeQuery(ctx, node, conn)
case *plan.TableCopier:
// We preserve the table schema in a best-effort manner.
// For simple `CREATE TABLE t AS SELECT * FROM t`,
// we fall back to the framework to create the table and copy the data.
// For more complex cases, we directly execute the CTAS statement in DuckDB.
if _, ok := node.Source.(*plan.ResolvedTable); ok {
return b.base.Build(ctx, root, r)
}
return b.executeDML(ctx, node, conn)
case sql.Expressioner:
return b.executeExpressioner(ctx, node, conn)
Expand Down
22 changes: 22 additions & 0 deletions catalog/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,25 @@ func (d *Database) GetCollation(ctx *sql.Context) sql.CollationID {
func (d *Database) SetCollation(ctx *sql.Context, collation sql.CollationID) error {
return nil
}

// CopyTableData implements sql.TableCopierDatabase interface.
func (d *Database) CopyTableData(ctx *sql.Context, sourceTable string, destinationTable string) (uint64, error) {
d.mu.Lock()
defer d.mu.Unlock()

// Use INSERT INTO ... SELECT to copy data
sql := `INSERT INTO ` + FullTableName(d.catalog, d.name, destinationTable) + ` FROM ` + FullTableName(d.catalog, d.name, sourceTable)

res, err := adapter.Exec(ctx, sql)
if err != nil {
return 0, ErrDuckDB.New(err)
}

// Get count of affected rows
count, err := res.RowsAffected()
if err != nil {
return 0, ErrDuckDB.New(err)
}

return uint64(count), nil
}
36 changes: 3 additions & 33 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1110,45 +1110,15 @@ func TestCreateTable(t *testing.T) {
"create_table_t1_(i_int_primary_key,_b1_blob,_b2_blob,_unique_index(b1(123),_b2(456)))",
"create_table_t1_(i_int_primary_key,_b1_blob,_b2_blob,_index(b1(10)),_index(b2(20)),_index(b1(123),_b2(456)))",
"create_table_t1_(i_int_primary_key,_b1_blob,_b2_blob,_index(b1(10)),_index(b2(20)),_index(b1(123),_b2(456)))",
// "CREATE_TABLE_t1_as_select_*_from_mytable",
// "CREATE_TABLE_t1_as_select_*_from_mytable",
// "CREATE_TABLE_t1_as_select_*_from_mytable#01",
// "CREATE_TABLE_t1_as_select_*_from_mytable",
// "CREATE_TABLE_t1_as_select_s,_i_from_mytable",
// "CREATE_TABLE_t1_as_select_s,_i_from_mytable",
// "CREATE_TABLE_t1_as_select_distinct_s,_i_from_mytable",
// "CREATE_TABLE_t1_as_select_distinct_s,_i_from_mytable",
// "CREATE_TABLE_t1_as_select_s,_i_from_mytable_order_by_s",
// "CREATE_TABLE_t1_as_select_s,_i_from_mytable_order_by_s",
// "CREATE_TABLE_t1_as_select_s,_sum(i)_from_mytable_group_by_s",
// "CREATE_TABLE_t1_as_select_s,_sum(i)_from_mytable_group_by_s",
// "CREATE_TABLE_t1_as_select_s,_sum(i)_from_mytable_group_by_s_having_sum(i)_>_2",
// "CREATE_TABLE_t1_as_select_s,_sum(i)_from_mytable_group_by_s_having_sum(i)_>_2",
// "CREATE_TABLE_t1_as_select_s,_i_from_mytable_order_by_s_limit_1",
// "CREATE_TABLE_t1_as_select_s,_i_from_mytable_order_by_s_limit_1",
// "CREATE_TABLE_t1_as_select_concat(\"new\",_s),_i_from_mytable",
// "CREATE_TABLE_t1_as_select_concat(\"new\",_s),_i_from_mytable",
// SUM(VARCHAR) is not supported by DuckDB
"CREATE_TABLE_t1_as_select_s,_sum(i)_from_mytable_group_by_s",
"CREATE_TABLE_t1_as_select_s,_sum(i)_from_mytable_group_by_s_having_sum(i)_>_2",
"display_width_for_numeric_types",
"SHOW_FULL_FIELDS_FROM_numericDisplayWidthTest;",
"datetime_precision",
"CREATE_TABLE_tt_(pk_int_primary_key,_d_datetime(6)_default_current_timestamp(6))",
"Identifier_lengths",
"table_charset_options",
// "show_create_table_t3",
// "show_create_table_t4",
// "create_table_with_select_preserves_default",
// "create_table_t1_select_*_from_a;",
// "create_table_t2_select_j_from_a;",
// "create_table_t3_select_j_as_i_from_a;",
// "create_table_t4_select_j_+_1_from_a;",
// "create_table_t5_select_a.j_from_a;",
// "create_table_t6_select_sqa.j_from_(select_i,_j_from_a)_sqa;",
// "show_create_table_t7;",
// "create_table_t8_select_*_from_(select_*_from_a)_a_join_(select_*_from_b)_b;",
// "show_create_table_t9;",
// "create_table_t11_select_sum(j)_over()_as_jj_from_a;",
// "create_table_t12_select_j_from_a_group_by_j;",
// "create_table_t13_select_*_from_c;",
"event_contains_CREATE_TABLE_AS",
"CREATE_EVENT_foo_ON_SCHEDULE_EVERY_1_YEAR_DO_CREATE_TABLE_bar_AS_SELECT_1;",
"trigger_contains_CREATE_TABLE_AS",
Expand Down

0 comments on commit c80adc8

Please sign in to comment.