8000 Add support for altering struct columns (adding fields, dropping fields, renaming fields) by Mytherin · Pull Request #17003 · duckdb/duckdb · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add support for altering struct columns (adding fields, dropping fields, renaming fields) #17003

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

Merged
merged 5 commits into from
Apr 7, 2025

Conversation

Mytherin
Copy link
Collaborator
@Mytherin Mytherin commented Apr 5, 2025

This PR adds support for altering struct columns to the SQL dialect. The syntax follows the regular ALTER syntax for columns - but just identifies struct fields through qualifiers.

Syntax:

CREATE TABLE test(s STRUCT(i INTEGER, j INTEGER));
INSERT INTO test VALUES (ROW(1, 1)), (ROW(2, 2));

Adding a field

-- add field "k INTEGER" to struct "s" in table "test"
ALTER TABLE test ADD COLUMN s.k INTEGER;
FROM test;
┌─────────────────────────────────────────┐
│                    s                    │
│ struct(i integer, j integer, k integer) │
├─────────────────────────────────────────┤
│ {'i': 1, 'j': 1, 'k': NULL}             │
│ {'i': 2, 'j': 2, 'k': NULL}             │
└─────────────────────────────────────────┘

Dropping a field

-- drop field "i" from struct "s" in table "test"
ALTER TABLE test DROP COLUMN s.i;
FROM test;
┌──────────────────────────────┐
│              s               │
│ struct(j integer, k integer) │
├──────────────────────────────┤
│ {'j': 1, 'k': NULL}          │
│ {'j': 2, 'k': NULL}          │
└──────────────────────────────┘

Renaming a field

-- renaming field "j" of struct "s" to "v1" in table test"
ALTER TABLE test RENAME s.j TO v1;
FROM test;
┌───────────────────────────────┐
│               s               │
│ struct(v1 integer, k integer) │
├───────────────────────────────┤
│ {'v1': 1, 'k': NULL}          │
│ {'v1': 2, 'k': NULL}          │
└───────────────────────────────┘

Implementation

The actual alter commands are currently implemented through a rewrite to a ChangeColumnType using a struct_remap expression. While this works - it has the drawback that existing (unchanged) fields are also rewritten. In the future, we can work on optimizing this to work similarly to adding/dropping/renaming columns which do not touch the other columns of a table.

@Mytherin Mytherin added the Needs Documentation Use for issues or PRs that require changes in the documentation label Apr 5, 2025
@Mytherin Mytherin merged commit ba0c2a7 into duckdb:main Apr 7, 2025
51 of 52 checks passed
8000
Mytherin added a commit that referenced this pull request May 15, 2025
…ST` and `MAP` columns. (#17462)

This PR extends #17003

Using `element` to target the child of a `LIST`, or `key`/`value` to
target the key/value of a `MAP` respectively.

### Examples

#### `MAP`
```sql
s MAP(
	STRUCT(n INTEGER, m INTEGER),
	STRUCT(i INTEGER, j INTEGER)
)
```
`ALTER TABLE test ADD COLUMN s.key.k INTEGER`

Turns the schema into:
```sql
s MAP(
	STRUCT(n INTEGER, m INTEGER, k INTEGER),
	STRUCT(i INTEGER, j INTEGER)
)
```

#### `LIST`
```sql
s STRUCT(i INTEGER, j INTEGER)[]
```
`ALTER TABLE test ADD COLUMN s.element.k INTEGER`

Turns the schema into:
```sql
s STRUCT(i INTEGER, j INTEGER, k INTEGER)[]
```
krlmlr added a commit to duckdb/duckdb-r that referenced this pull request May 15, 2025
Add support for altering struct columns (adding fields, dropping fields, renaming fields) (duckdb/duckdb#17003)
Clean up format script, gather all files then run concurrently instead of running concurrently per directory (duckdb/duckdb#16988)
krlmlr added a commit to duckdb/duckdb-r that referenced this pull request May 15, 2025
Add support for altering struct columns (adding fields, dropping fields, renaming fields) (duckdb/duckdb#17003)
Clean up format script, gather all files then run concurrently instead of running concurrently per directory (duckdb/duckdb#16988)
krlmlr added a commit to duckdb/duckdb-r that referenced this pull request May 16, 2025
Add support for altering struct columns (adding fields, dropping fields, renaming fields) (duckdb/duckdb#17003)
Clean up format script, gather all files then run concurrently instead of running concurrently per directory (duckdb/duckdb#16988)
krlmlr added a commit to duckdb/duckdb-r that referenced this pull request May 16, 2025
Add support for altering struct columns (adding fields, dropping fields, renaming fields) (duckdb/duckdb#17003)
Clean up format script, gather all files then run concurrently instead of running concurrently per directory (duckdb/duckdb#16988)
krlmlr added a commit to duckdb/duckdb-r that referenced this pull request May 17, 2025
Add support for altering struct columns (adding fields, dropping fields, renaming fields) (duckdb/duckdb#17003)
Clean up format script, gather all files then run concurrently instead of running concurrently per directory (duckdb/duckdb#16988)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Documentation Use for issues or PRs that require changes in the documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0