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

fix: add visit for merge into/update/replace/insert/delete/copy statement #13848

Merged
merged 44 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
101f1e8
add rewrite rule
JackTan25 Nov 29, 2023
e6b209a
add tests
JackTan25 Nov 29, 2023
3d1d96a
fix test
JackTan25 Nov 29, 2023
006a068
Merge branch 'main' into fix_bug
JackTan25 Nov 29, 2023
e8c8760
add more exprs
JackTan25 Nov 29, 2023
fd91178
Merge branch 'fix_bug' of https://github.com/JackTan25/databend into …
JackTan25 Nov 29, 2023
499d1ce
Merge branch 'main' into fix_bug
JackTan25 Nov 29, 2023
ed3de21
add merge into visit comments
JackTan25 Nov 30, 2023
02ce375
Merge branch 'fix_bug' of https://github.com/JackTan25/databend into …
JackTan25 Nov 30, 2023
4bb134b
fix visit and source for merge,insert,delete,update,replace,copy
JackTan25 Nov 30, 2023
4605958
fix lint
JackTan25 Nov 30, 2023
38802fd
fix lint
JackTan25 Nov 30, 2023
b57799a
Merge branch 'main' into fix_bug
JackTan25 Nov 30, 2023
30a7da3
Merge branch 'main' into fix_bug
JackTan25 Nov 30, 2023
5791538
modify support
JackTan25 Nov 30, 2023
45f61d1
Merge branch 'fix_bug' of https://github.com/JackTan25/databend into …
JackTan25 Nov 30, 2023
814d799
modify
JackTan25 Nov 30, 2023
42966df
Merge branch 'main' of https://github.com/datafuselabs/databend into …
JackTan25 Nov 30, 2023
40650fd
add create table visit
JackTan25 Nov 30, 2023
38f36b6
Merge branch 'main' into fix_bug
JackTan25 Nov 30, 2023
6b24f51
fix test
JackTan25 Nov 30, 2023
391b426
Merge branch 'fix_bug' of https://github.com/JackTan25/databend into …
JackTan25 Nov 30, 2023
2a8e5db
fix test
JackTan25 Nov 30, 2023
b426243
Merge branch 'main' into fix_bug
JackTan25 Nov 30, 2023
3eea3fd
fix
JackTan25 Nov 30, 2023
8972f8a
Merge branch 'main' into fix_bug
JackTan25 Nov 30, 2023
78c0f6f
revert copy
JackTan25 Nov 30, 2023
775d2b4
revert copy
JackTan25 Nov 30, 2023
e7a1625
Merge branch 'fix_bug' of https://github.com/JackTan25/databend into …
JackTan25 Nov 30, 2023
4baa751
fix
JackTan25 Nov 30, 2023
235f1b5
revert test
JackTan25 Dec 1, 2023
a27a2b2
fix
JackTan25 Dec 1, 2023
96312e5
fix comments
JackTan25 Dec 1, 2023
c5077cf
fix
JackTan25 Dec 1, 2023
cb1233a
Merge branch 'main' into fix_bug
JackTan25 Dec 1, 2023
150007f
fix
JackTan25 Dec 1, 2023
a72d8ce
Merge branch 'fix_bug' of https://github.com/JackTan25/databend into …
JackTan25 Dec 1, 2023
7d4e5a8
fix lint
JackTan25 Dec 1, 2023
50dd7ce
fix lint
JackTan25 Dec 1, 2023
164d841
Merge branch 'main' into fix_bug
JackTan25 Dec 1, 2023
bb01af7
fix lint
JackTan25 Dec 1, 2023
8e5e024
Merge branch 'fix_bug' of https://github.com/JackTan25/databend into …
JackTan25 Dec 1, 2023
3bcc1f9
fix
JackTan25 Dec 1, 2023
c519374
Merge branch 'main' into fix_bug
JackTan25 Dec 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/query/ast/src/visitors/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,34 @@ pub trait Visitor<'ast>: Sized {

fn visit_insert(&mut self, _insert: &'ast InsertStmt) {}
fn visit_replace(&mut self, _replace: &'ast ReplaceStmt) {}
fn visit_merge_into(&mut self, _merge_into: &'ast MergeIntoStmt) {}
fn visit_merge_into(&mut self, merge_into: &'ast MergeIntoStmt) {
if let MergeSource::Select { query, .. } = &merge_into.source {
self.visit_query(query)
}
self.visit_expr(&merge_into.join_expr);
for operation in &merge_into.merge_options {
match operation {
MergeOption::Match(match_operation) => {
if let Some(expr) = &match_operation.selection {
self.visit_expr(expr)
}
if let MatchOperation::Update { update_list, .. } = &match_operation.operation {
for update in update_list {
self.visit_expr(&update.expr)
}
}
}
MergeOption::Unmatch(unmatch_operation) => {
if let Some(expr) = &unmatch_operation.selection {
self.visit_expr(expr)
}
for expr in &unmatch_operation.insert_operation.values {
self.visit_expr(expr)
}
}
}
}
}
fn visit_insert_source(&mut self, _insert_source: &'ast InsertSource) {}

fn visit_delete(&mut self, _delete: &'ast DeleteStmt) {}
Expand Down
31 changes: 30 additions & 1 deletion src/query/ast/src/visitors/visitor_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,36 @@ pub trait VisitorMut: Sized {

fn visit_insert(&mut self, _insert: &mut InsertStmt) {}
fn visit_replace(&mut self, _replace: &mut ReplaceStmt) {}
fn visit_merge_into(&mut self, _merge_into: &mut MergeIntoStmt) {}
fn visit_merge_into(&mut self, merge_into: &mut MergeIntoStmt) {
JackTan25 marked this conversation as resolved.
Show resolved Hide resolved
if let MergeSource::Select { query, .. } = &mut merge_into.source {
self.visit_query(query)
}
self.visit_expr(&mut merge_into.join_expr);
for operation in &mut merge_into.merge_options {
match operation {
MergeOption::Match(match_operation) => {
if let Some(expr) = &mut match_operation.selection {
self.visit_expr(expr)
}
if let MatchOperation::Update { update_list, .. } =
&mut match_operation.operation
{
for update in update_list {
self.visit_expr(&mut update.expr)
}
}
}
MergeOption::Unmatch(unmatch_operation) => {
if let Some(expr) = &mut unmatch_operation.selection {
self.visit_expr(expr)
}
for expr in &mut unmatch_operation.insert_operation.values {
self.visit_expr(expr)
}
}
}
}
}
fn visit_insert_source(&mut self, _insert_source: &mut InsertSource) {}

fn visit_delete(&mut self, _delete: &mut DeleteStmt) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,57 @@ WHEN NOT MATCHED THEN
INSERT (order_id, user_id, order_type, asset_type, quantity, price, status, created_at, updated_at)
VALUES ((SELECT MAX(order_id) FROM orders) + 1, synthetic_orders.user_id, synthetic_orders.synthetic_order_type, synthetic_orders.asset_type, synthetic_orders.total_quantity, 0, 'pending', synthetic_orders.synthetic_date, synthetic_orders.synthetic_date);

## issue #13810: rewrite rule test
statement ok
DROP TABLE IF EXISTS orders;

statement ok
CREATE TABLE orders (
order_id INT NOT NULL,
user_id INT NOT NULL,
order_type VARCHAR NOT NULL,
asset_type VARCHAR NOT NULL,
quantity DECIMAL(18,8) NOT NULL,
price DECIMAL(18,8) NOT NULL,
status VARCHAR NOT NULL,
created_at DATE NOT NULL,
updated_at DATE NOT NULL
) row_per_block=5113;

statement ok
insert into orders values(200007,7,'buy','BTC',4.81412194,48.14121943,'completed',to_date('2021-01-01'),to_date('2021-01-01')),
(200015,15,'buy','BTC',3.78463552,37.84635523,'completed',to_date('2021-01-01'),to_date('2021-01-01')),
(200019,19,'buy','BTC',1.61186913,16.11869132,'completed',to_date('2021-01-01'),to_date('2021-01-01')),
(200031,31,'buy','BTC',3.99013730,39.90137297,'completed',to_date('2021-01-01'),to_date('2021-01-01')),
(200047,47,'buy','BTC',0.98841829,9.88418289,'completed',to_date('2021-01-01'),to_date('2021-01-01')),
(200077,77,'buy','BTC',2.07360391,20.73603908,'completed',to_date('2021-01-01'),to_date('2021-01-01')),
(200087,87,'sell','ETH',9.64567442,96.45674419,'pending',to_date('2021-01-01'),to_date('2021-01-01')),
(200095,95,'buy','BTC',2.26686563,22.66865634,'completed',to_date('2021-01-01'),to_date('2021-01-01')),
(200098,98,'buy','BTC',1.37252960,13.72529599,'completed',to_date('2021-01-01'),to_date('2021-01-01')),
(200102,102,'buy','BTC',1.53596481,15.35964815,'completed',to_date('2021-01-01'),to_date('2021-01-01'));

statement ok
MERGE INTO orders USING (
SELECT o.order_id, o.user_id, o.order_type, o.asset_type, o.quantity + a.avg_quantity AS new_quantity, o.price, o.status, o.created_at, o.updated_at
FROM orders o
INNER JOIN (
SELECT user_id, asset_type, sum(quantity) AS avg_quantity
FROM orders
GROUP BY user_id, asset_type
) a ON o.user_id = a.user_id AND o.asset_type = a.asset_type
) AS joined_data ON orders.order_id = joined_data.order_id
WHEN MATCHED THEN
UPDATE SET orders.quantity = joined_data.new_quantity;

query TTTT
SELECT SUM(quantity) AS total_quantity,
AVG(quantity) AS average_quantity,
MIN(quantity) AS min_quantity,
MAX(quantity) AS max_quantity
FROM orders;
----
64.16764110 6.416764110000 1.97683658 19.29134884

statement ok
set enable_distributed_merge_into = 0;

Expand Down
Loading