-
Notifications
You must be signed in to change notification settings - Fork 477
feat: Support the DELETE SQL statement #942
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
+484
−17
Merged
Changes from 37 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
e3f5b83
[WIP]:delete sql
jun0315 e8da65e
[fix]:time parser bug
jun0315 56448e0
Merge branch 'develop' into xqj_delete
jun0315 f96c345
[fix]:resolve conflict
jun0315 76581cc
[fmt]:cargo fmt
jun0315 3782bb8
[fix]:remove unless log
jun0315 821df85
[fix]:test
jun0315 42ea2a7
[feat]:add error parse
c1307a0
Merge branch 'develop' into xqj_delete
d7beacb
[fix]:resolve conflict
c01d77c
[fix]:remove unless code
e2f872b
[fix]:remove unless code
b32f62a
[test]:add IT
5be930f
[fix]:add license
452b0e1
[fix]:ci
83f5fe3
[fix]:ci
43b73bf
[fix]:ci
81c8bd9
[fix]:remove
b082769
[fix]:ci
f679429
[feat]:add sql
2e1dceb
[fix]:modify sql
0c7a9cd
[feat]:refactor parser_expr
6c36f38
[feat]:rm backtrace
jun0315 5fb29df
Merge branch 'develop' into xqj_delete
jun0315 c28d3a3
[fix]:ci
jun0315 d5eebe4
[fix]: conversation
jun0315 558a3f6
[fix]: conversation
jun0315 f70f3cd
feat:refactor delete
jun0315 3afdb2e
feat:refactor delete
jun0315 6f0ad69
fix:resolve conversation
jun0315 86789ca
fix:ut
jun0315 83b6f5c
Merge branch 'develop' into xqj_delete
jun0315 593555a
fix:ut
jun0315 d38d87c
fix:conversation
jun0315 9f0efaa
Merge branch 'develop' into xqj_delete
jun0315 83038e5
Merge branch 'develop' into xqj_delete
jun0315 d339ce1
fix:conversation
jun0315 6e8f17a
fix:conservation
jun0315 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
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 |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| // Copyright 2023 Greptime Team | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use std::collections::HashMap; | ||
| use std::sync::Arc; | ||
|
|
||
| use common_query::Output; | ||
| use datatypes::data_type::DataType; | ||
| use datatypes::prelude::VectorRef; | ||
| use datatypes::vectors::StringVector; | ||
| use session::context::QueryContextRef; | ||
| use snafu::ResultExt; | ||
| use sql::ast::{BinaryOperator, Expr, Value}; | ||
| use sql::statements::delete::Delete; | ||
| use sql::statements::sql_value_to_value; | ||
| use table::engine::TableReference; | ||
| use table::requests::DeleteRequest; | ||
| use table::TableRef; | ||
|
|
||
| use crate::error::{ColumnNotFoundSnafu, DeleteSnafu, InvalidSqlSnafu, NotSupportSqlSnafu, Result}; | ||
| use crate::instance::sql::table_idents_to_full_name; | ||
| use crate::sql::SqlHandler; | ||
|
|
||
| impl SqlHandler { | ||
| pub(crate) async fn delete(&self, query_ctx: QueryContextRef, stmt: Delete) -> Result<Output> { | ||
| let (catalog_name, schema_name, table_name) = | ||
| table_idents_to_full_name(stmt.table_name(), query_ctx)?; | ||
| let table_ref = TableReference { | ||
| catalog: &catalog_name.to_string(), | ||
| schema: &schema_name.to_string(), | ||
| table: &table_name.to_string(), | ||
| }; | ||
|
|
||
| let table = self.get_table(&table_ref)?; | ||
|
|
||
| let req = DeleteRequest { | ||
| key_column_values: parse_selection(stmt.selection(), &table)?, | ||
| }; | ||
|
|
||
| let affected_rows = table.delete(req).await.with_context(|_| DeleteSnafu { | ||
| table_name: table_ref.to_string(), | ||
| })?; | ||
|
|
||
| Ok(Output::AffectedRows(affected_rows)) | ||
| } | ||
| } | ||
|
|
||
| /// parse selection, currently supported format is `tagkey1 = 'tagvalue1' and 'ts' = 'value'`. | ||
| /// (only uses =, and in the where clause and provides all columns needed by the key.) | ||
| fn parse_selection( | ||
| selection: &Option<Expr>, | ||
| table: &TableRef, | ||
| ) -> Result<HashMap<String, VectorRef>> { | ||
| let mut key_column_values = HashMap::new(); | ||
| if let Some(expr) = selection { | ||
| parse_expr(expr, &mut key_column_values, table)?; | ||
| } | ||
| Ok(key_column_values) | ||
| } | ||
|
|
||
| fn parse_expr( | ||
| expr: &Expr, | ||
| key_column_values: &mut HashMap<String, VectorRef>, | ||
| table: &TableRef, | ||
| ) -> Result<()> { | ||
| // match BinaryOp | ||
| if let Expr::BinaryOp { left, op, right } = expr { | ||
| match (&**left, op, &**right) { | ||
| // match And operator | ||
| (Expr::BinaryOp { .. }, BinaryOperator::And, Expr::BinaryOp { .. }) => { | ||
| parse_expr(left, key_column_values, table)?; | ||
| parse_expr(right, key_column_values, table)?; | ||
| return Ok(()); | ||
| } | ||
| // match Eq operator | ||
| (Expr::Identifier(column_name), BinaryOperator::Eq, Expr::Value(value)) => { | ||
| key_column_values.insert( | ||
| column_name.to_string(), | ||
| value_to_vector(&column_name.to_string(), value, table)?, | ||
| ); | ||
| return Ok(()); | ||
| } | ||
| (Expr::Identifier(column_name), BinaryOperator::Eq, Expr::Identifier(value)) => { | ||
| key_column_values.insert( | ||
| column_name.to_string(), | ||
| Arc::new(StringVector::from(vec![value.to_string()])), | ||
| ); | ||
| return Ok(()); | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
| NotSupportSqlSnafu { | ||
| msg: format!( | ||
| "Not support sql expr:{expr},correct format is tagkey1 = tagvalue1 and ts = value" | ||
| ), | ||
| } | ||
| .fail() | ||
| } | ||
|
|
||
| /// parse value to vector | ||
| fn value_to_vector(column_name: &String, sql_value: &Value, table: &TableRef) -> Result<VectorRef> { | ||
| let data_type = table.schema().column_type_by_name(column_name); | ||
| match data_type { | ||
| Some(data_type) => { | ||
| let value = sql_value_to_value(column_name, &data_type, sql_value); | ||
| match value { | ||
| Ok(value) => { | ||
| let mut vec = data_type.create_mutable_vector(1); | ||
| if vec.push_value_ref(value.as_value_ref()).is_err() { | ||
| return InvalidSqlSnafu { | ||
| msg: format!( | ||
| "invalid sql, column name is {column_name}, value is {sql_value}", | ||
| ), | ||
| } | ||
| .fail(); | ||
| } | ||
| Ok(vec.to_vector()) | ||
| } | ||
| _ => { | ||
| InvalidSqlSnafu { | ||
| msg: format!( | ||
| "invalid sql, column name is {column_name}, value is {sql_value}", | ||
| ), | ||
| } | ||
| .fail() | ||
| } | ||
| } | ||
| } | ||
| None => ColumnNotFoundSnafu { | ||
| column_name, | ||
| table_name: table.table_info().name.clone(), | ||
| } | ||
| .fail(), | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.