|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) |
| 4 | +# shellcheck source=../shell_config.sh |
| 5 | +. "$CURDIR"/../shell_config.sh |
| 6 | + |
| 7 | +SYNC_USER="${CLICKHOUSE_DATABASE}_sync_user" |
| 8 | +ASYNC_USER="${CLICKHOUSE_DATABASE}_async_user" |
| 9 | + |
| 10 | +${CLICKHOUSE_CLIENT} --multiquery <<EOF |
| 11 | +DROP TABLE IF EXISTS source_table, target_table, target_table_remote_sync, target_table_remote_async, async_insert_mv, sync_insert_mv; |
| 12 | +DROP USER IF EXISTS ${SYNC_USER}, ${ASYNC_USER}; |
| 13 | +
|
| 14 | +CREATE USER ${SYNC_USER} HOST ANY IDENTIFIED WITH NO_PASSWORD SETTINGS async_insert = 0; |
| 15 | +CREATE USER ${ASYNC_USER} HOST ANY IDENTIFIED WITH NO_PASSWORD SETTINGS async_insert = 1; |
| 16 | +GRANT ALL ON target_table TO ${SYNC_USER}; |
| 17 | +GRANT ALL ON target_table TO ${ASYNC_USER}; |
| 18 | +
|
| 19 | +CREATE TABLE source_table ( |
| 20 | + id UInt64, |
| 21 | + data String |
| 22 | +) |
| 23 | +ENGINE=MergeTree() |
| 24 | +ORDER BY id; |
| 25 | +
|
| 26 | +CREATE TABLE target_table ( |
| 27 | + id UInt64, |
| 28 | + data String |
| 29 | +) |
| 30 | +ENGINE=MergeTree() |
| 31 | +ORDER BY id; |
| 32 | +
|
| 33 | +CREATE TABLE target_table_remote_sync ( |
| 34 | + id UInt64, |
| 35 | + data String |
| 36 | +) |
| 37 | +AS remote('127.0.0.2', currentDatabase(), 'target_table', '${SYNC_USER}'); |
| 38 | +
|
| 39 | +CREATE TABLE target_table_remote_async ( |
| 40 | + id UInt64, |
| 41 | + data String |
| 42 | +) |
| 43 | +AS remote('127.0.0.2', currentDatabase(), 'target_table', '${ASYNC_USER}'); |
| 44 | +
|
| 45 | +CREATE MATERIALIZED VIEW async_insert_mv TO target_table_remote_async AS |
| 46 | +SELECT * FROM source_table; |
| 47 | +
|
| 48 | +CREATE MATERIALIZED VIEW sync_insert_mv TO target_table_remote_sync AS |
| 49 | +SELECT * FROM source_table; |
| 50 | +
|
| 51 | +-- Default setting, unset async_insert, so its default is 0 |
| 52 | +SET async_insert = DEFAULT; |
| 53 | +-- One type of inserts each |
| 54 | +INSERT INTO source_table (id, data) VALUES (1, 'test1'), (2, 'test2'), (3, 'test3'); |
| 55 | +SET async_insert = 1; |
| 56 | +-- One type of inserts each |
| 57 | +INSERT INTO source_table (id, data) VALUES (4, 'test4'), (5, 'test5'), (6, 'test6'); |
| 58 | +SET async_insert = 0; |
| 59 | +-- This time both inserts have async_insert = 0, so 2 async and 4 sync inserts in total |
| 60 | +INSERT INTO source_table (id, data) VALUES (7, 'test7'), (8, 'test8'), (9, 'test9'); |
| 61 | +
|
| 62 | +SYSTEM FLUSH LOGS query_log; |
| 63 | +SELECT count() FROM system.query_log |
| 64 | +WHERE query_kind = 'Insert' AND type = 'QueryFinish' |
| 65 | + AND user IN ('${SYNC_USER}', '${ASYNC_USER}') |
| 66 | +-- AND current_database = currentDatabase() -- to silent strange style check warning |
| 67 | + AND tables = [currentDatabase() || '.target_table'] |
| 68 | +GROUP BY Settings['async_insert'] |
| 69 | +ORDER BY count() ASC; |
| 70 | +
|
| 71 | +SELECT * FROM target_table ORDER BY id; |
| 72 | +EOF |
0 commit comments