Skip to content
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

ALTER TABLE support for adding constraints #57

Open
Mytherin opened this issue Dec 6, 2018 · 13 comments
Open

ALTER TABLE support for adding constraints #57

Mytherin opened this issue Dec 6, 2018 · 13 comments

Comments

@Mytherin
Copy link
Collaborator

Mytherin commented Dec 6, 2018

Constraints should be able to be dropped/created separately from the CREATE TABLE statement (i.e. as separate ALTER TABLE ADD CONSTRAINT statements). Constraints also need names for this.

@Mytherin Mytherin changed the title (Catalog) ALTER TABLE support for adding constraints ALTER TABLE support for adding constraints Dec 7, 2018
@Mytherin Mytherin added this to To do in First Release Dec 7, 2018
@hannes hannes added this to the First Release milestone Mar 22, 2019
@hannes hannes removed this from the First Release milestone Apr 21, 2021
Mytherin pushed a commit that referenced this issue Apr 29, 2021
Mytherin pushed a commit that referenced this issue Feb 10, 2022
@taniabogatsch
Copy link
Contributor

For example support the following SQL statements.

  • ALTER TABLE tbl ADD CONSTRAINT constraint_name UNIQUE (column1, column2, ... column_n);
  • ALTER TABLE tbl ADD PRIMARY KEY (id);
  • ALTER TABLE tbl DROP CONSTRAINT constraint_name;
  • ALTER TABLE tbl RENAME CONSTRAINT constraint_name TO new_constraint_name;

@taniabogatsch taniabogatsch added the good first issue Good for newcomers label Jan 2, 2023
@papparapa
Copy link
Contributor

I'm interested in working on this.
At first, constraints have to be named.
Is it a good way to implement ConstraintCatalogEntry that inherits StandardEntry?
Inheriting StandardEntry should lead to constraints having name and also oid.

@taniabogatsch
Copy link
Contributor

Hi @papparapa. Nice to hear that you are interested in working on this. Yes, that makes sense; we could add constraints to the catalog like we currently add indexes (duck_index_entry.hpp/index_catalog_entry.hpp).

Since all these constraints create an index in the background, I'd suggest reusing the existing code for the CREATE UNIQUE INDEX idx_name ON table (expression) statement as much as possible for this issue. Make sure to add the constraints to both the table catalog entry and its storage; have a look at PhysicalCreateIndex::Finalize (physical_create_index.hpp).

And let me know if you have more specific questions or anything. :)

@papparapa
Copy link
Contributor

Thanks @taniabogatsch for some hints!
I will read relevant codes.

@coffepowered
Copy link

Hello @papparapa , I am interested in this functionality as well. Did you make any progress?

@papparapa
Copy link
Contributor

@coffepowered No progress yet, so feel free to work on it.

@lnkuiper
Copy link
Contributor

lnkuiper commented Jun 6, 2023

@coffepowered Have you made any progress on this? If not, I would like to assign this task:

ALTER TABLE tbl RENAME CONSTRAINT constraint_name TO new_constraint_name;

to @kriti-sc

Edit: this is assigned to @kriti-sc until further notice.

kriti-sc added a commit to kriti-sc/duckdb that referenced this issue Jun 25, 2023
kriti-sc added a commit to kriti-sc/duckdb that referenced this issue Jun 26, 2023
kriti-sc added a commit to kriti-sc/duckdb that referenced this issue Jun 26, 2023
@chienguo
Copy link

Hi @taniabogatsch. There exists a TableCatalogEntry that already includes constraints, so do we need another ConstraintCatalogEntry to support this alter table operation, instead of just modifying the constraint in that table entry?

Copy link

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@loicalleyne
Copy link

Is adding this feature still on the roadmap? I'm currently having to implement an table schema auto-patcher workflow for when a data source's schema evolves and new columns need to be added and this is complicating things for tables that need a primary key. The ability to specify a primary key in a CREATE TABLE t1 AS SELECT * FROM read_json(...) || read_parquet(...) would be a helpful alternative solution.

@taniabogatsch
Copy link
Contributor

@loicalleyne, on the roadmap, yes. But I can't say when we'll have this. Afaik, there have been two people working on it, but we never got a PR.

@Ammar-Azman
Copy link

Still not supported?

@frapa
Copy link

frapa commented Apr 18, 2024

Hi, I would like to give this a go. I know there were a couple of people that previously tried implementing this, but it's almost a year that there is no activity, so I believe I wouldn't be stepping on anybody's toes. @lnkuiper @kriti-sc please shout if you are still on it!

My plan would be to start with

ALTER TABLE table ADD PRIMARY KEY (column);

because this can be implemented without the constraint naming capabilities. It would then be possible to add a primary key but not remove it.

If I understood correctly, adding a PRIMARY KEY is the same as adding a NOT NULL and UNIQUE constraint. I didn't figure out yet if I need to explicitly create the ART index for the unique constraint or if adding the constraints in DuckTableEntry is sufficient.

Let me know if you find this plan reasonable.

I also have some additional questions on the expected API:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
First Release
  
To do
Development

No branches or pull requests

10 participants