-- { echoOn } SYSTEM STOP MERGES tbl; -- simple test case create table if not exists replacing_mt (x String) engine=ReplacingMergeTree() ORDER BY x; insert into replacing_mt values ('abc'); insert into replacing_mt values ('abc'); -- expected output is 2 because final is turned off select count() from replacing_mt; 2 set final = 1; -- expected output is 1 because final is turned on select count() from replacing_mt; 1 -- JOIN test cases 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; insert into lhs values ('abc'); insert into lhs values ('abc'); insert into rhs values ('abc'); insert into rhs values ('abc'); set final = 0; -- expected output is 4 because select_final == 0 select count() from lhs inner join rhs on lhs.x = rhs.x; 4 set final = 1; -- expected output is 1 because final == 1 select count() from lhs inner join rhs on lhs.x = rhs.x; 1 -- regular non final table set final = 1; create table if not exists regular_mt_table (x String) engine=MergeTree() ORDER BY x; insert into regular_mt_table values ('abc'); insert into regular_mt_table values ('abc'); -- expected output is 1, it should silently ignore final modifier select count() from regular_mt_table; 2 -- 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; set final=1; select count() from nv_regular_mt_table; 2 -- 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; 1 c a c 1 c b c 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; SELECT `_--left_table.id` AS `left_table.id`, val_left, val_middle, val_right FROM ( SELECT val_left, id AS `_--left_table.id`, val_middle, middle_table.id AS `_--middle_table.id` FROM left_table FINAL ALL INNER JOIN ( SELECT id, val_middle FROM middle_table ) AS middle_table ON `_--left_table.id` = `_--middle_table.id` ) AS `--.s` ALL INNER JOIN ( SELECT val_right, id AS `_--right_table.id` FROM right_table FINAL ) AS `--.t` ON `_--middle_table.id` = `_--right_table.id` ORDER BY `_--left_table.id` ASC, val_left ASC, val_middle ASC, val_right ASC -- 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; 1 c a c 1 c b c -- 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; 1 c 1 a 1 c 1 c 1 b 1 c 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; 1 c 1 a 1 c 1 c 1 b 1 c -- 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; 1 c 2 a 2 b 3 c