Skip to content

Commit cb5163c

Browse files
authored
Merge pull request #67 from PostHog/bill-ph/cascade-drop
Switch drop cascade to drop restrict similar to dbt-duckdb
2 parents 743bc5e + bd38f38 commit cb5163c

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

transpiler/transform/ddl.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ func (t *DDLTransform) Transform(tree *pg_query.ParseResult, result *Result) (bo
5050
result.NoOpTag = "DROP INDEX"
5151
return true, nil
5252
}
53+
// DuckLake doesn't support CASCADE on DROP TABLE/VIEW.
54+
// Strip CASCADE by converting to RESTRICT (same approach as dbt-duckdb).
55+
// See: https://github.com/duckdb/dbt-duckdb/pull/557
56+
if n.DropStmt.Behavior == pg_query.DropBehavior_DROP_CASCADE {
57+
n.DropStmt.Behavior = pg_query.DropBehavior_DROP_RESTRICT
58+
changed = true
59+
}
5360
}
5461

5562
case *pg_query.Node_ReindexStmt:

transpiler/transpiler_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,64 @@ func TestTranspile_OnConflict_DuckLakeMode(t *testing.T) {
845845
}
846846
}
847847

848+
func TestTranspile_DropCascade_DuckLakeMode(t *testing.T) {
849+
// In DuckLake mode, CASCADE should be stripped from DROP statements
850+
// See: https://github.com/duckdb/dbt-duckdb/pull/557
851+
tests := []struct {
852+
name string
853+
input string
854+
notContains string
855+
}{
856+
{
857+
name: "DROP TABLE CASCADE becomes DROP TABLE",
858+
input: "DROP TABLE users CASCADE",
859+
notContains: "CASCADE",
860+
},
861+
{
862+
name: "DROP TABLE IF EXISTS CASCADE",
863+
input: "DROP TABLE IF EXISTS users CASCADE",
864+
notContains: "CASCADE",
865+
},
866+
{
867+
name: "DROP VIEW CASCADE",
868+
input: "DROP VIEW my_view CASCADE",
869+
notContains: "CASCADE",
870+
},
871+
{
872+
name: "DROP SCHEMA CASCADE",
873+
input: "DROP SCHEMA my_schema CASCADE",
874+
notContains: "CASCADE",
875+
},
876+
}
877+
878+
tr := New(Config{DuckLakeMode: true})
879+
880+
for _, tt := range tests {
881+
t.Run(tt.name, func(t *testing.T) {
882+
result, err := tr.Transpile(tt.input)
883+
if err != nil {
884+
t.Fatalf("Transpile(%q) error: %v", tt.input, err)
885+
}
886+
if strings.Contains(strings.ToUpper(result.SQL), tt.notContains) {
887+
t.Errorf("Transpile(%q) = %q, should NOT contain %q", tt.input, result.SQL, tt.notContains)
888+
}
889+
})
890+
}
891+
}
892+
893+
func TestTranspile_DropCascade_NonDuckLakeMode(t *testing.T) {
894+
// Outside DuckLake mode, CASCADE should be preserved
895+
tr := New(DefaultConfig())
896+
897+
result, err := tr.Transpile("DROP TABLE users CASCADE")
898+
if err != nil {
899+
t.Fatalf("Transpile error: %v", err)
900+
}
901+
if !strings.Contains(strings.ToUpper(result.SQL), "CASCADE") {
902+
t.Errorf("Non-DuckLake mode should preserve CASCADE, got: %q", result.SQL)
903+
}
904+
}
905+
848906
func TestTranspile_JSONOperators(t *testing.T) {
849907
// DuckDB supports -> and ->> operators
850908
tests := []struct {

0 commit comments

Comments
 (0)