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

feat: support customizing column default values for inserting #8283

Merged
merged 11 commits into from
Nov 22, 2023

Conversation

jonahgao
Copy link
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
@@ -230,6 +231,41 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
Ok(Schema::new(fields))
}

pub(super) fn build_column_defaults(
Copy link
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
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.

@comphead
Copy link
Contributor

Thanks @jonahgao I think we're very close

Copy link
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
Member Author

Thank you for reviewing @comphead ♥️.

@alamb alamb merged commit 3dbda1e into apache:main Nov 22, 2023
22 checks passed
@alamb
Copy link
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