Skip to content

feat: support customizing column default values for inserting#8283

Merged
alamb merged 11 commits intoapache:mainfrom
jonahgao:column_default
Nov 22, 2023
Merged

feat: support customizing column default values for inserting#8283
alamb merged 11 commits intoapache:mainfrom
jonahgao:column_default

Conversation

@jonahgao
Copy link
Copy Markdown
Member

@jonahgao jonahgao commented Nov 20, 2023

Which issue does this PR close?

This is a continuation of #8146.

Rationale for this change

Currently, we only support NULL as the default value for columns.
This pr will enable users to customize the default value while creating a table.

For example:

DataFusion CLI v33.0.0

❯ create table t(a int, b int default 100);
0 rows in set. Query took 0.004 seconds.

❯ insert into t(a) values(1);
+-------+
| count |
+-------+
| 1     |
+-------+
1 row in set. Query took 0.007 seconds.

❯ select * from t;
+---+-----+
| a | b   |
+---+-----+
| 1 | 100 |
+---+-----+
1 row in set. Query took 0.006 seconds.

What changes are included in this PR?

  • Parsing column default values when constructing the CreateMemoryTable plan.
  • The default value of a column can be obtained from the TableSource.
  • Fill default value if a column value is not specified during inserting.
  • Implement it on MemTable for verification.

Are these changes tested?

Yes

Are there any user-facing changes?

Yes.

  • New methods for TableSource and TableProvider, but provided default implementations.
  • New field column_defaults in the struct CreateMemoryTable.

@github-actions github-actions Bot added sql SQL Planner logical-expr Logical plan and expressions core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) labels Nov 20, 2023
Comment thread datafusion/sqllogictest/test_files/insert.slt
Comment thread datafusion/sql/src/planner.rs Outdated
Ok(Schema::new(fields))
}

pub(super) fn build_column_defaults(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to explain in comments what exactly gets returned in the tuple for future readers, like <column name, expr>
I'm even thinking should be that be a map instead of Vec<>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment has been added for this.

The main reason is that Hashmap has not implemented the Hash trait, which is required by CreateMemoryTable.

Comment thread datafusion/sql/src/planner.rs Outdated
@comphead
Copy link
Copy Markdown
Contributor

Thanks @jonahgao I think we're very close

jonahgao and others added 3 commits November 21, 2023 14:43
Co-authored-by: comphead <comphead@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@comphead comphead left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm thanks @jonahgao for supporting custom default values

@jonahgao
Copy link
Copy Markdown
Member Author

Thank you for reviewing @comphead ♥️.

@alamb alamb merged commit 3dbda1e into apache:main Nov 22, 2023
@alamb
Copy link
Copy Markdown
Contributor

alamb commented Nov 22, 2023

Thank you @jonahgao and @comphead for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate logical-expr Logical plan and expressions sql SQL Planner sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants