Skip to content

Commit

Permalink
feat: alter/drop AUTO_INCREMENT column
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyang01 committed Dec 18, 2024
1 parent 708d026 commit 28f8228
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 52 deletions.
11 changes: 11 additions & 0 deletions backend/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ func (b *DuckBuilder) Build(ctx *sql.Context, root sql.Node, r sql.Row) (sql.Row
return b.base.Build(ctx, root, r)
case *plan.InsertInto:
insert := n.(*plan.InsertInto)

// For AUTO_INCREMENT column, we fallback to the framework if the column is specified.
if dst, err := plan.GetInsertable(insert.Destination); err == nil && dst.Schema().HasAutoIncrement() {
if len(insert.ColumnNames) == 0 || len(insert.ColumnNames) == len(dst.Schema()) {
return b.base.Build(ctx, root, r)
}
}

src := insert.Source
if proj, ok := src.(*plan.Project); ok {
src = proj.Child
Expand Down Expand Up @@ -189,6 +197,9 @@ func (b *DuckBuilder) executeDML(ctx *sql.Context, n sql.Node, conn *stdsql.Conn
// Execute the DuckDB query
result, err := conn.ExecContext(ctx.Context, duckSQL)
if err != nil {
if yes, column := catalog.IsDuckDBNotNullConstraintViolationError(err); yes {
return nil, sql.ErrInsertIntoNonNullableProvidedNull.New(column)
}
return nil, err
}

Expand Down
4 changes: 3 additions & 1 deletion catalog/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ func (d *Database) tablesInsensitive(ctx *sql.Context, pattern string) ([]*Table
return nil, err
}
for _, t := range tables {
t.withSchema(ctx)
if err := t.withSchema(ctx); err != nil {
return nil, err
}
}
return tables, nil
}
Expand Down
10 changes: 10 additions & 0 deletions catalog/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,13 @@ func IsDuckDBIndexAlreadyExistsError(err error) bool {
func IsDuckDBUniqueConstraintViolationError(err error) bool {
return strings.Contains(err.Error(), "Constraint Error: Data contains duplicates on indexed column(s)")
}

// Constraint Error: NOT NULL constraint failed: <column>
func IsDuckDBNotNullConstraintViolationError(err error) (bool, string) {
msg := err.Error()
pattern := "Constraint Error: NOT NULL constraint failed: "
if idx := strings.Index(msg, pattern); idx >= 0 {
return true, msg[idx+len(pattern):]
}
return false, ""
}
Loading

0 comments on commit 28f8228

Please sign in to comment.