2018-07-12 11:15:09 +00:00
#!/usr/bin/env bash
CURDIR = $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " && pwd )
2020-12-28 11:46:53 +00:00
# shellcheck source=../shell_config.sh
2020-08-01 00:51:12 +00:00
. " $CURDIR " /../shell_config.sh
2018-07-12 11:15:09 +00:00
2020-12-28 11:46:53 +00:00
# shellcheck source=./mergetree_mutations.lib
2020-08-01 00:52:41 +00:00
. " $CURDIR " /mergetree_mutations.lib
2018-08-01 19:01:58 +00:00
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "DROP TABLE IF EXISTS mutations"
2018-07-12 11:15:09 +00:00
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "CREATE TABLE mutations(d Date, x UInt32, s String, a UInt32 ALIAS x + 1, m MATERIALIZED x + 2) ENGINE MergeTree(d, intDiv(x, 10), 8192)"
2018-07-12 11:15:09 +00:00
# Test a mutation on empty table
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations DELETE WHERE x = 1"
2018-07-12 11:15:09 +00:00
# Insert some data
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= " INSERT INTO mutations(d, x, s) VALUES \
2018-07-12 11:15:09 +00:00
( '2000-01-01' , 1, 'a' ) "
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= " INSERT INTO mutations(d, x, s) VALUES \
2018-07-12 11:15:09 +00:00
( '2000-01-01' , 2, 'b' ) , ( '2000-01-01' , 3, 'c' ) , ( '2000-01-01' , 4, 'd' ) \
( '2000-02-01' , 2, 'b' ) , ( '2000-02-01' , 3, 'c' ) , ( '2000-02-01' , 4, 'd' ) "
# Try some malformed queries that should fail validation.
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations DELETE WHERE nonexistent = 0" 2>/dev/null || echo "Query should fail 1"
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations DELETE WHERE d = '11'" 2>/dev/null || echo "Query should fail 2"
2018-07-12 11:15:09 +00:00
# TODO: Queries involving alias columns are not supported yet and should fail on submission.
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations UPDATE s = s || '' WHERE a = 0" 2>/dev/null || echo "Query involving aliases should fail on submission"
2018-07-12 11:15:09 +00:00
# Delete some values
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations DELETE WHERE x % 2 = 1"
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations DELETE WHERE s = 'd'"
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations DELETE WHERE m = 3"
2018-07-12 11:15:09 +00:00
# Insert more data
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= " INSERT INTO mutations(d, x, s) VALUES \
2018-07-12 11:15:09 +00:00
( '2000-01-01' , 5, 'e' ) , ( '2000-02-01' , 5, 'e' ) "
2018-08-01 19:01:58 +00:00
# Wait until the last mutation is done.
2018-08-22 12:42:18 +00:00
wait_for_mutation "mutations" "mutation_7.txt"
2018-07-12 11:15:09 +00:00
# Check that the table contains only the data that should not be deleted.
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "SELECT d, x, s, m FROM mutations ORDER BY d, x"
2018-07-12 11:15:09 +00:00
# Check the contents of the system.mutations table.
${ CLICKHOUSE_CLIENT } --query= " SELECT mutation_id, command, block_numbers.partition_id, block_numbers.number, parts_to_do, is_done \
2021-08-07 10:02:30 +00:00
FROM system.mutations WHERE database = '$CLICKHOUSE_DATABASE' and table = 'mutations' ORDER BY mutation_id"
2018-07-12 11:15:09 +00:00
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "DROP TABLE mutations"
2018-09-06 10:49:36 +00:00
2018-08-01 19:01:58 +00:00
${ CLICKHOUSE_CLIENT } --query= "SELECT '*** Test mutations cleaner ***'"
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "DROP TABLE IF EXISTS mutations_cleaner"
2018-08-01 19:01:58 +00:00
# Create a table with finished_mutations_to_keep = 2
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "CREATE TABLE mutations_cleaner(x UInt32) ENGINE MergeTree ORDER BY x SETTINGS finished_mutations_to_keep = 2"
2018-08-01 19:01:58 +00:00
# Insert some data
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "INSERT INTO mutations_cleaner(x) VALUES (1), (2), (3), (4)"
2018-08-01 19:01:58 +00:00
# Add some mutations and wait for their execution
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations_cleaner DELETE WHERE x = 1"
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations_cleaner DELETE WHERE x = 2"
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations_cleaner DELETE WHERE x = 3"
2018-08-01 19:01:58 +00:00
wait_for_mutation "mutations_cleaner" "mutation_4.txt"
# Sleep and then do an INSERT to wakeup the background task that will clean up the old mutations
sleep 1
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "INSERT INTO mutations_cleaner(x) VALUES (4)"
2018-08-01 19:01:58 +00:00
sleep 0.1
# Check that the first mutation is cleaned
2021-08-07 10:02:30 +00:00
${ CLICKHOUSE_CLIENT } --query= " SELECT mutation_id, command, is_done FROM system.mutations WHERE database = ' $CLICKHOUSE_DATABASE ' and table = 'mutations_cleaner' ORDER BY mutation_id "
2018-08-01 19:01:58 +00:00
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "DROP TABLE mutations_cleaner"