-
Notifications
You must be signed in to change notification settings - Fork 1k
Implemented RETURNING support for SQL
#3935
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
Open
egormanga
wants to merge
3
commits into
clockworklabs:master
Choose a base branch
from
egormanga:sql-returning
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
Some comments aren't visible on the classic Files Changed page.
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
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 |
|---|---|---|
|
|
@@ -58,22 +58,38 @@ impl DML { | |
| pub fn table_name(&self) -> &TableName { | ||
| &self.table_schema().table_name | ||
| } | ||
|
|
||
| /// Iterate over the projected column names and types | ||
| pub fn for_each_return_field(&self, f: impl FnMut(&str, &AlgebraicType)) { | ||
| match self { | ||
| Self::Insert(TableInsert { returning, .. }) | ||
| | Self::Update(TableUpdate { returning, .. }) | ||
| | Self::Delete(TableDelete { returning, .. }) => { | ||
| if let Some(returning) = returning { | ||
| returning.for_each_return_field(f); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub struct TableInsert { | ||
| pub table: Arc<TableOrViewSchema>, | ||
| pub rows: Box<[ProductValue]>, | ||
| pub returning: Option<ProjectList>, | ||
| } | ||
|
|
||
| pub struct TableDelete { | ||
| pub table: Arc<TableOrViewSchema>, | ||
| pub filter: Option<Expr>, | ||
| pub returning: Option<ProjectList>, | ||
| } | ||
|
|
||
| pub struct TableUpdate { | ||
| pub table: Arc<TableOrViewSchema>, | ||
| pub columns: Box<[(ColId, AlgebraicValue)]>, | ||
| pub filter: Option<Expr>, | ||
| pub returning: Option<ProjectList>, | ||
| } | ||
|
|
||
| pub struct SetVar { | ||
|
|
@@ -91,6 +107,7 @@ pub fn type_insert(insert: SqlInsert, tx: &impl SchemaView) -> TypingResult<Tabl | |
| table: SqlIdent(table_name), | ||
| fields, | ||
| values, | ||
| returning, | ||
| } = insert; | ||
|
|
||
| let schema = tx | ||
|
|
@@ -153,16 +170,30 @@ pub fn type_insert(insert: SqlInsert, tx: &impl SchemaView) -> TypingResult<Tabl | |
| } | ||
| rows.push(ProductValue::from(values)); | ||
| } | ||
| let into = schema; | ||
| let into = schema.clone(); | ||
| let rows = rows.into_boxed_slice(); | ||
| Ok(TableInsert { table: into, rows }) | ||
| let mut vars = Relvars::default(); | ||
| vars.insert(table_name.clone(), into.clone()); | ||
| let returning = returning | ||
| .map(|proj| type_proj(RelExpr::RelVar(Relvar { | ||
| schema: schema, | ||
| alias: ST_VAR_NAME.into(), | ||
| delta: None, | ||
| }), proj, &vars)) | ||
| .transpose()?; | ||
| Ok(TableInsert { | ||
| table: into, | ||
| rows, | ||
| returning, | ||
| }) | ||
| } | ||
|
|
||
| /// Type check a DELETE statement | ||
| pub fn type_delete(delete: SqlDelete, tx: &impl SchemaView) -> TypingResult<TableDelete> { | ||
| let SqlDelete { | ||
| table: SqlIdent(query_table_name), | ||
| filter, | ||
| returning, | ||
| } = delete; | ||
| let from = tx | ||
| .schema(&query_table_name) | ||
|
|
@@ -184,9 +215,17 @@ pub fn type_delete(delete: SqlDelete, tx: &impl SchemaView) -> TypingResult<Tabl | |
| let expr = filter | ||
| .map(|expr| type_expr(&vars, expr, Some(&AlgebraicType::Bool))) | ||
| .transpose()?; | ||
| let returning = returning | ||
| .map(|proj| type_proj(RelExpr::RelVar(Relvar { | ||
| schema: from.clone(), | ||
| alias: ST_VAR_NAME.into(), | ||
|
Contributor
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 should be the delete table. |
||
| delta: None, | ||
| }), proj, &vars)) | ||
| .transpose()?; | ||
| Ok(TableDelete { | ||
| table: from, | ||
| filter: expr, | ||
| returning, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -196,6 +235,7 @@ pub fn type_update(update: SqlUpdate, tx: &impl SchemaView) -> TypingResult<Tabl | |
| table: SqlIdent(query_table_name), | ||
| assignments, | ||
| filter, | ||
| returning, | ||
| } = update; | ||
| let schema = tx | ||
| .schema(&query_table_name) | ||
|
|
@@ -249,10 +289,18 @@ pub fn type_update(update: SqlUpdate, tx: &impl SchemaView) -> TypingResult<Tabl | |
| let filter = filter | ||
| .map(|expr| type_expr(&vars, expr, Some(&AlgebraicType::Bool))) | ||
| .transpose()?; | ||
| let returning = returning | ||
| .map(|proj| type_proj(RelExpr::RelVar(Relvar { | ||
| schema: schema.clone(), | ||
| alias: ST_VAR_NAME.into(), | ||
|
Contributor
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. Similarly this should be the update table. |
||
| delta: None, | ||
| }), proj, &vars)) | ||
| .transpose()?; | ||
| Ok(TableUpdate { | ||
| table: schema, | ||
| columns: values, | ||
| filter, | ||
| returning, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -306,6 +354,7 @@ pub fn type_and_rewrite_set(set: SqlSet, tx: &impl SchemaView) -> TypingResult<T | |
| Ok(TableInsert { | ||
| table, | ||
| rows: Box::new([ProductValue::from_iter([var_name, sum_value])]), | ||
| returning: None, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
This alias should be the insert table.