-
Notifications
You must be signed in to change notification settings - Fork 54
feat(binder): add create table binding #16
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,55 @@ | ||
| use super::Binder; | ||
| use crate::binder::{lower_case_name, split_name}; | ||
| use crate::catalog::{Column, ColumnDesc}; | ||
| use crate::planner::logical_create_table_plan::LogicalCreateTablePlan; | ||
| use crate::planner::LogicalPlan; | ||
| use crate::types::ColumnId; | ||
| use anyhow::Result; | ||
| use sqlparser::ast::{ColumnDef, ObjectName}; | ||
| use std::collections::HashSet; | ||
|
|
||
| impl Binder { | ||
| pub(super) fn bind_create_table( | ||
| &mut self, | ||
| name: ObjectName, | ||
| columns: &[ColumnDef], | ||
| ) -> Result<LogicalCreateTablePlan> { | ||
| let name = lower_case_name(&name); | ||
|
|
||
| let (_, table_name) = split_name(&name)?; | ||
|
|
||
| let table = self | ||
| .context | ||
| .catalog | ||
| .get_table_by_name(table_name) | ||
| .ok_or_else(|| { | ||
| anyhow::Error::msg(format!("table {} not found", table_name.to_string())) | ||
| })?; | ||
|
|
||
| // check duplicated column names | ||
| let mut set = HashSet::new(); | ||
| for col in columns.iter() { | ||
| if !set.insert(col.name.value.clone()) { | ||
| return Err(anyhow::Error::msg(format!( | ||
| "bind duplicated column {}", | ||
| col.name.value.clone() | ||
| ))); | ||
| } | ||
| } | ||
|
|
||
| let mut columns: Vec<Column> = columns | ||
| .iter() | ||
| .enumerate() | ||
| .map(|(_, col)| Column::from(col)) | ||
| .collect(); | ||
|
|
||
| let plan = LogicalCreateTablePlan { | ||
| table_name: table_name.to_string(), | ||
| columns: columns | ||
| .into_iter() | ||
| .map(|col| (col.name.to_string(), col.desc.clone())) | ||
| .collect(), | ||
| }; | ||
| Ok(plan) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| use crate::types::{ColumnId, DataType, IdGenerator}; | ||
| use sqlparser::ast::{ColumnDef, ColumnOption}; | ||
|
|
||
| #[derive(Clone)] | ||
| pub struct Column { | ||
|
|
@@ -8,10 +9,7 @@ pub struct Column { | |
| } | ||
|
|
||
| impl Column { | ||
| pub(crate) fn new( | ||
| column_name: String, | ||
| column_desc: ColumnDesc, | ||
| ) -> Column { | ||
| pub(crate) fn new(column_name: String, column_desc: ColumnDesc) -> Column { | ||
| Column { | ||
| id: IdGenerator::build(), | ||
| name: column_name, | ||
|
|
@@ -63,6 +61,28 @@ impl ColumnDesc { | |
| } | ||
| } | ||
|
|
||
| impl From<&ColumnDef> for Column { | ||
| fn from(cdef: &ColumnDef) -> Self { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This variable name definition is too easy to confuse! |
||
| let mut is_nullable = true; | ||
| let mut is_primary_ = false; | ||
| for opt in &cdef.options { | ||
| match opt.option { | ||
| ColumnOption::Null => is_nullable = true, | ||
| ColumnOption::NotNull => is_nullable = false, | ||
| ColumnOption::Unique { is_primary } => is_primary_ = is_primary, | ||
| _ => todo!("column options"), | ||
| } | ||
| } | ||
| Column::new( | ||
| cdef.name.value.clone(), | ||
| ColumnDesc::new( | ||
| DataType::new(cdef.data_type.clone(), is_nullable), | ||
| is_primary_, | ||
| ), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The provided code patch appears to be an implementation of the
bind_create_tablemethod in aBinderstruct. Here are some observations and suggestions for improvement:It seems that the code is missing necessary import statements at the beginning. Make sure to include the required imports, such as
super,Binder,lower_case_name,split_name, etc.Consider adding proper documentation or comments to describe the purpose and functionality of the
bind_create_tablemethod. This will make it easier for other developers (including yourself) to understand the code in the future.It's generally a good practice to handle potential errors gracefully. For example, when retrieving a table by name (
self.context.catalog.get_table_by_name(table_name)), consider using theResulttype along with proper error handling instead of directly calling.ok_or_else()and returning anError. This will provide more information about the specific error that occurred during table retrieval.The code checks for duplicated column names using a
HashSet. This is a valid approach, but it would be helpful to provide a more specific error message indicating which column names are duplicated.When mapping the columns from
ColumnDeftoColumn, the linelet mut col = Column::from(col);seems unnecessary since theColumn::fromfunction can directly return a new instance without needing to mutate it.Check if there are any additional validations or constraints that need to be implemented. For example, you may want to verify the data types or enforce certain properties on the columns being created.
Consider adding tests to cover different scenarios, such as creating a table with no columns, creating a table with duplicate columns, etc. This will ensure the correctness of the implementation and help catch any potential issues.
Remember that these suggestions are general guidelines, and your specific project requirements may vary.