2022-10-25 13:06:15 +00:00
|
|
|
-- { echoOn }
|
2022-11-25 16:31:08 +00:00
|
|
|
SYSTEM STOP MERGES tbl;
|
|
|
|
|
2022-10-25 13:06:15 +00:00
|
|
|
-- simple test case
|
2022-11-25 16:31:08 +00:00
|
|
|
create table if not exists replacing_mt (x String) engine=ReplacingMergeTree() ORDER BY x;
|
2022-10-25 13:06:15 +00:00
|
|
|
|
|
|
|
insert into replacing_mt values ('abc');
|
|
|
|
insert into replacing_mt values ('abc');
|
|
|
|
|
2023-01-31 18:21:00 +00:00
|
|
|
-- expected output is 2 because final is turned off
|
2022-11-25 16:31:08 +00:00
|
|
|
select count() from replacing_mt;
|
|
|
|
|
2023-01-31 18:21:00 +00:00
|
|
|
set final = 1;
|
|
|
|
-- expected output is 1 because final is turned on
|
2022-10-25 13:06:15 +00:00
|
|
|
select count() from replacing_mt;
|
|
|
|
|
|
|
|
-- JOIN test cases
|
2022-11-25 16:31:08 +00:00
|
|
|
create table if not exists lhs (x String) engine=ReplacingMergeTree() ORDER BY x;
|
|
|
|
create table if not exists rhs (x String) engine=ReplacingMergeTree() ORDER BY x;
|
2022-10-25 13:06:15 +00:00
|
|
|
|
|
|
|
insert into lhs values ('abc');
|
|
|
|
insert into lhs values ('abc');
|
|
|
|
|
|
|
|
insert into rhs values ('abc');
|
|
|
|
insert into rhs values ('abc');
|
|
|
|
|
2023-01-31 18:21:00 +00:00
|
|
|
set final = 0;
|
2022-11-25 16:31:08 +00:00
|
|
|
-- expected output is 4 because select_final == 0
|
2022-10-25 13:06:15 +00:00
|
|
|
select count() from lhs inner join rhs on lhs.x = rhs.x;
|
|
|
|
|
2023-01-31 18:21:00 +00:00
|
|
|
set final = 1;
|
|
|
|
-- expected output is 1 because final == 1
|
2022-10-25 13:06:15 +00:00
|
|
|
select count() from lhs inner join rhs on lhs.x = rhs.x;
|
2022-12-05 13:18:44 +00:00
|
|
|
|
|
|
|
-- regular non final table
|
2023-01-31 18:21:00 +00:00
|
|
|
set final = 1;
|
2022-12-05 13:18:44 +00:00
|
|
|
create table if not exists regular_mt_table (x String) engine=MergeTree() ORDER BY x;
|
|
|
|
insert into regular_mt_table values ('abc');
|
2022-12-05 16:21:50 +00:00
|
|
|
insert into regular_mt_table values ('abc');
|
2022-12-05 13:18:44 +00:00
|
|
|
-- expected output is 1, it should silently ignore final modifier
|
|
|
|
select count() from regular_mt_table;
|
2022-12-05 16:21:50 +00:00
|
|
|
|
|
|
|
-- view test
|
|
|
|
create materialized VIEW mv_regular_mt_table TO regular_mt_table AS SELECT * FROM regular_mt_table;
|
|
|
|
create view nv_regular_mt_table AS SELECT * FROM mv_regular_mt_table;
|
|
|
|
|
2023-01-31 18:21:00 +00:00
|
|
|
set final=1;
|
2022-12-05 16:21:50 +00:00
|
|
|
select count() from nv_regular_mt_table;
|
2023-01-30 23:33:18 +00:00
|
|
|
|
2023-01-31 15:32:26 +00:00
|
|
|
-- join on mix of tables that support / do not support select final with explain
|
|
|
|
create table if not exists left_table (id UInt64, val_left String) engine=ReplacingMergeTree() ORDER BY id;
|
|
|
|
create table if not exists middle_table (id UInt64, val_middle String) engine=MergeTree() ORDER BY id;
|
|
|
|
create table if not exists right_table (id UInt64, val_right String) engine=ReplacingMergeTree() ORDER BY id;
|
|
|
|
insert into left_table values (1,'a');
|
|
|
|
insert into left_table values (1,'b');
|
|
|
|
insert into left_table values (1,'c');
|
|
|
|
insert into middle_table values (1,'a');
|
|
|
|
insert into middle_table values (1,'b');
|
|
|
|
insert into right_table values (1,'a');
|
|
|
|
insert into right_table values (1,'b');
|
|
|
|
insert into right_table values (1,'c');
|
|
|
|
-- expected output
|
|
|
|
-- 1 c a c
|
|
|
|
-- 1 c b c
|
|
|
|
select left_table.id,val_left, val_middle, val_right from left_table
|
|
|
|
inner join middle_table on left_table.id = middle_table.id
|
|
|
|
inner join right_table on middle_table.id = right_table.id
|
|
|
|
ORDER BY left_table.id, val_left, val_middle, val_right;
|
|
|
|
|
|
|
|
explain syntax select left_table.id,val_left, val_middle, val_right from left_table
|
|
|
|
inner join middle_table on left_table.id = middle_table.id
|
|
|
|
inner join right_table on middle_table.id = right_table.id
|
|
|
|
ORDER BY left_table.id, val_left, val_middle, val_right;
|
|
|
|
|
|
|
|
-- extra: same with subquery
|
|
|
|
select left_table.id,val_left, val_middle, val_right from left_table
|
|
|
|
inner join middle_table on left_table.id = middle_table.id
|
|
|
|
inner join (SELECT * FROM right_table WHERE id = 1) r on middle_table.id = r.id
|
|
|
|
ORDER BY left_table.id, val_left, val_middle, val_right;
|
|
|
|
|
|
|
|
-- distributed tables
|
|
|
|
drop table if exists left_table;
|
|
|
|
drop table if exists middle_table;
|
|
|
|
drop table if exists right_table;
|
|
|
|
create table if not exists left_table (id UInt64, val_left String) engine=ReplacingMergeTree() ORDER BY id;
|
|
|
|
create table if not exists middle_table (id UInt64, val_middle String) engine=MergeTree() ORDER BY id;
|
|
|
|
create table if not exists right_table_local (id UInt64, val_right String) engine=ReplacingMergeTree() ORDER BY id;
|
|
|
|
create table if not exists right_table engine=Distributed('test_shard_localhost', currentDatabase(), right_table_local) AS right_table_local;
|
|
|
|
insert into left_table values (1,'a');
|
|
|
|
insert into left_table values (1,'b');
|
|
|
|
insert into left_table values (1,'c');
|
|
|
|
insert into middle_table values (1,'a');
|
|
|
|
insert into middle_table values (1,'b');
|
|
|
|
insert into right_table_local values (1,'a');
|
|
|
|
insert into right_table_local values (1,'b');
|
|
|
|
insert into right_table_local values (1,'c');
|
|
|
|
SET prefer_localhost_replica=0;
|
|
|
|
-- expected output:
|
|
|
|
-- 1 c 1 a 1 c
|
|
|
|
-- 1 c 1 b 1 c
|
|
|
|
select left_table.*,middle_table.*, right_table.* from left_table
|
|
|
|
inner join middle_table on left_table.id = middle_table.id
|
|
|
|
inner join right_table on middle_table.id = right_table.id
|
|
|
|
ORDER BY left_table.id, val_left, val_middle, val_right;
|
|
|
|
|
|
|
|
SET prefer_localhost_replica=1;
|
|
|
|
-- expected output:
|
|
|
|
-- 1 c 1 a 1 c
|
|
|
|
-- 1 c 1 b 1 c
|
|
|
|
select left_table.*,middle_table.*, right_table.* from left_table
|
|
|
|
inner join middle_table on left_table.id = middle_table.id
|
|
|
|
inner join right_table on middle_table.id = right_table.id
|
|
|
|
ORDER BY left_table.id, val_left, val_middle, val_right;
|
|
|
|
|
|
|
|
-- Quite exotic with Merge engine
|
|
|
|
DROP TABLE IF EXISTS table_to_merge_a;
|
|
|
|
DROP TABLE IF EXISTS table_to_merge_b;
|
|
|
|
DROP TABLE IF EXISTS table_to_merge_c;
|
|
|
|
DROP TABLE IF EXISTS merge_table;
|
|
|
|
|
|
|
|
create table if not exists table_to_merge_a (id UInt64, val String) engine=ReplacingMergeTree() ORDER BY id;
|
|
|
|
create table if not exists table_to_merge_b (id UInt64, val String) engine=MergeTree() ORDER BY id;
|
|
|
|
create table if not exists table_to_merge_c (id UInt64, val String) engine=ReplacingMergeTree() ORDER BY id;
|
|
|
|
CREATE TABLE merge_table Engine=Merge(currentDatabase(), '^(table_to_merge_[a-z])$') AS table_to_merge_a;
|
|
|
|
|
|
|
|
insert into table_to_merge_a values (1,'a');
|
|
|
|
insert into table_to_merge_a values (1,'b');
|
|
|
|
insert into table_to_merge_a values (1,'c');
|
|
|
|
insert into table_to_merge_b values (2,'a');
|
|
|
|
insert into table_to_merge_b values (2,'b');
|
|
|
|
insert into table_to_merge_c values (3,'a');
|
|
|
|
insert into table_to_merge_c values (3,'b');
|
|
|
|
insert into table_to_merge_c values (3,'c');
|
|
|
|
|
|
|
|
-- expected output:
|
|
|
|
-- 1 c, 2 a, 2 b, 3 c
|
|
|
|
SELECT * FROM merge_table ORDER BY id, val;
|