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: test2 #13806

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 2 additions & 22 deletions src/query/sql/src/planner/optimizer/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,31 +127,14 @@ pub fn optimize(
Ok(Plan::CopyIntoTable(plan))
}
Plan::MergeInto(plan) => {
// optimize source :fix issue #13733
// reason: if there is subquery,windowfunc exprs etc. see
// src/planner/semantic/lowering.rs `as_raw_expr()`, we will
// get dummy index. So we need to use optimizer to solve this.
let right_source = optimize_query(
ctx.clone(),
opt_ctx.clone(),
plan.meta_data.clone(),
plan.input.child(1)?.clone(),
)?;
// replace right source
let mut join_sexpr = plan.input.clone();
join_sexpr = Box::new(join_sexpr.replace_children(vec![
Arc::new(join_sexpr.child(0)?.clone()),
Arc::new(right_source),
]));

// try to optimize distributed join
if opt_ctx.config.enable_distributed_optimization
&& ctx.get_settings().get_enable_distributed_merge_into()?
{
// Todo(JackTan25): We should use optimizer to make a decision to use
// left join and right join.
// input is a Join_SExpr
let merge_into_join_sexpr = optimize_distributed_query(ctx.clone(), &join_sexpr)?;
let merge_into_join_sexpr = optimize_distributed_query(ctx.clone(), &plan.input)?;

let merge_source_optimizer = MergeSourceOptimizer::create();
let optimized_distributed_merge_into_join_sexpr =
Expand All @@ -161,10 +144,7 @@ pub fn optimize(
..*plan
})))
} else {
Ok(Plan::MergeInto(Box::new(MergeInto {
input: join_sexpr,
..*plan
})))
Ok(Plan::MergeInto(plan))
}
}
// Passthrough statements.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -776,102 +776,6 @@ select count(*) from t11;
----
0

## test issue #13732
statement ok
CREATE TABLE orders CLUSTER BY (to_yyyymmddhh(created_at), user_id) AS SELECT
number % 5000 AS order_id,
number % 10000 AS user_id,
CASE WHEN (rand() * 10)::int % 2 = 0 THEN 'buy'
ELSE 'sell'
END AS order_type,
CASE WHEN (rand() * 10)::int % 3 = 0 THEN 'BTC'
WHEN (rand() * 10)::int % 3 = 1 THEN 'ETH'
ELSE 'XRP'
END AS asset_type,
(rand() * 100)::decimal(18, 8) AS quantity,
(rand() * 1000)::decimal(18, 8) AS price,
CASE WHEN (rand() * 10)::int % 3 = 0 THEN 'completed'
WHEN (rand() * 10)::int % 3 = 1 THEN 'pending'
ELSE 'cancelled'
END AS status,
date_add('day', floor(rand() * 10 % 365)::int, '2021-01-01') AS created_at,
date_add('day', floor(rand() * 10 % 365)::int, '2021-01-01') AS updated_at
FROM numbers(5000);

statement ok
MERGE INTO orders USING
(
SELECT
number % 5000 AS order_id,
number % 100000 AS user_id,
CASE WHEN (rand() * 10)::int % 2 = 0 THEN 'buy'
ELSE 'sell'
END AS order_type,
CASE WHEN (rand() * 10)::int % 3 = 0 THEN 'BTC'
WHEN (rand() * 10)::int % 3 = 1 THEN 'ETH'
ELSE 'XRP'
END AS asset_type,
(rand() * 100)::decimal(18, 8) AS quantity,
(rand() * 1000)::decimal(18, 8) AS price,
CASE WHEN (rand() * 10)::int % 3 = 0 THEN 'completed'
WHEN (rand() * 10)::int % 3 = 1 THEN 'pending'
ELSE 'cancelled'
END AS status,
date_add('day', floor(rand() * 10 % 365)::int, '2021-01-01') AS created_at,
date_add('day', floor(rand() * 10 % 365)::int, '2021-01-01') AS updated_at
FROM numbers(5000)
) AS source
ON orders.order_id = source.order_id
WHEN MATCHED THEN
UPDATE SET
orders.user_id = source.user_id,
orders.order_type = source.order_type,
orders.asset_type = source.asset_type,
orders.quantity = source.quantity,
orders.price = source.price,
orders.status = source.status,
orders.created_at = source.created_at,
orders.updated_at = source.updated_at
WHEN NOT MATCHED THEN
INSERT (order_id, user_id, order_type, asset_type, quantity, price, status, created_at, updated_at)
VALUES (source.order_id, source.user_id, source.order_type, source.asset_type, source.quantity, source.price, source.status, source.created_at, source.updated_at);

## test issue #13733
statement ok
MERGE INTO orders AS tt USING
(
SELECT
CASE
WHEN number % 2 = 0 THEN (number / 2) % 250000
ELSE (SELECT MAX(order_id) FROM orders) + number + 1
END AS order_id,
number % 100000 AS user_id,
CASE WHEN (rand() * 10)::int % 2 = 0 THEN 'buy'
ELSE 'sell'
END AS order_type,
CASE WHEN (rand() * 10)::int % 3 = 0 THEN 'BTC'
WHEN (rand() * 10)::int % 3 = 1 THEN 'ETH'
ELSE 'XRP'
END AS asset_type,
(rand() * 100)::decimal(18, 8) AS quantity,
(rand() * 1000)::decimal(18, 8) AS price,
CASE WHEN (rand() * 10)::int % 3 = 0 THEN 'completed'
WHEN (rand() * 10)::int % 3 = 1 THEN 'pending'
ELSE 'cancelled'
END AS status,
date_add('day', floor(rand() * 10 % 365)::int, '2021-01-01') AS created_at,
date_add('day', floor(rand() * 10 % 365)::int, '2021-01-01') AS updated_at,
CASE WHEN number % 2 = 0 THEN false ELSE true END AS is_delete
FROM numbers(5000)
) AS ss
ON (tt.user_id = ss.user_id AND tt.asset_type = ss.asset_type)
WHEN MATCHED AND ss.is_delete = true THEN
DELETE
WHEN MATCHED AND ss.is_delete = false THEN
UPDATE * WHEN NOT MATCHED THEN
INSERT *;


statement ok
set enable_distributed_merge_into = 0;

Expand Down
Loading