Skip to content

Commit

Permalink
feat: alter/drop AUTO_INCREMENT column (#298)
Browse files Browse the repository at this point in the history
* feat: alter/drop AUTO_INCREMENT column
* Enable more supported AUTO_INCREMENT DDL test
  • Loading branch information
fanyang01 authored Dec 18, 2024
1 parent d8ed972 commit c2cce1a
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 100 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/clients-compatibility.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ jobs:
npm install mysql
sudo cpanm --notest DBD::mysql
pip3 install mysql-connector-python
sudo apt-get install -y libblas-dev
sudo R -e "install.packages('RMySQL', repos='http://cran.r-project.org')"
sudo gem install mysql2
# R is unstable on GitHub Actions, so we disable it for now
# sudo apt-get install -y libblas-dev
# sudo R -e "install.packages('RMySQL', repos='http://cran.r-project.org')"
- name: Build
run: go build -v

Expand Down Expand Up @@ -111,10 +113,12 @@ jobs:
sudo cpanm --notest DBD::Pg
pip3 install "psycopg[binary]" pandas pyarrow polars
sudo apt-get install -y libblas-dev
# TODO: Speed up the installation of RPostgres
sudo R -e "install.packages('RPostgres', repos='http://cran.r-project.org')"
sudo gem install pg
# R is unstable on GitHub Actions, so we disable it for now
# TODO: Speed up the installation of RPostgres
# sudo R -e "install.packages('RPostgres', repos='http://cran.r-project.org')"
- name: Build
run: go build -v

Expand Down
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 c2cce1a

Please sign in to comment.