2021-08-26 13:19:52 +00:00
#!/usr/bin/env bash
CURDIR = $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " && pwd )
# shellcheck source=../shell_config.sh
. " $CURDIR " /../shell_config.sh
$CLICKHOUSE_CLIENT -q "drop table if exists dict_src;"
$CLICKHOUSE_CLIENT -q "drop dictionary if exists dict1;"
$CLICKHOUSE_CLIENT -q "drop dictionary if exists dict2;"
$CLICKHOUSE_CLIENT -q "drop table if exists join;"
$CLICKHOUSE_CLIENT -q "drop table if exists t;"
$CLICKHOUSE_CLIENT -q "create table dict_src (n int, m int, s String) engine=MergeTree order by n;"
$CLICKHOUSE_CLIENT -q " create dictionary dict1 (n int default 0, m int default 1, s String default 'qqq')
PRIMARY KEY n
SOURCE( CLICKHOUSE( HOST 'localhost' PORT tcpPort( ) USER 'default' TABLE 'dict_src' PASSWORD '' DB '$CLICKHOUSE_DATABASE' ) )
LIFETIME( MIN 1 MAX 10) LAYOUT( FLAT( ) ) ; "
$CLICKHOUSE_CLIENT -q " create table join(n int, m int default dictGet(' $CLICKHOUSE_DATABASE .dict1', 'm', 42::UInt64)) engine=Join(any, left, n); "
2021-11-02 14:30:22 +00:00
$CLICKHOUSE_CLIENT -q " create dictionary dict2 (n int default 0, m int DEFAULT 2)
2021-08-26 13:19:52 +00:00
PRIMARY KEY n
SOURCE( CLICKHOUSE( HOST 'localhost' PORT tcpPort( ) USER 'default' TABLE 'join' PASSWORD '' DB '$CLICKHOUSE_DATABASE' ) )
LIFETIME( MIN 1 MAX 10) LAYOUT( FLAT( ) ) ; "
2021-09-02 13:48:41 +00:00
$CLICKHOUSE_CLIENT -q " create table s (x default joinGet( $CLICKHOUSE_DATABASE .join, 'm', 42::int)) engine=Set "
2021-08-26 13:19:52 +00:00
$CLICKHOUSE_CLIENT -q " create table t (n int, m int default joinGet( $CLICKHOUSE_DATABASE .join, 'm', 42::int),
2021-11-02 14:30:22 +00:00
s String default dictGet( $CLICKHOUSE_DATABASE .dict1, 's' , 42::UInt64) , y default dictGet( $CLICKHOUSE_DATABASE .dict2, 'm' , 42::UInt64) ) engine = MergeTree order by n; "
$CLICKHOUSE_CLIENT -q " select table, arraySort(dependencies_table),
arraySort( loading_dependencies_table) , arraySort( loading_dependent_table) from system.tables where database = currentDatabase( ) order by table"
$CLICKHOUSE_CLIENT -q "select '====='"
$CLICKHOUSE_CLIENT -q " alter table t add column x int default in(1, $CLICKHOUSE_DATABASE .s), drop column y "
2021-08-26 13:19:52 +00:00
2024-02-08 03:03:12 +00:00
$CLICKHOUSE_CLIENT -q "create materialized view mv to s as select n as x from t where n in (select n from join)"
2021-09-24 10:22:22 +00:00
2021-11-01 18:53:07 +00:00
$CLICKHOUSE_CLIENT -q " select table, arraySort(dependencies_table),
arraySort( loading_dependencies_table) , arraySort( loading_dependent_table) from system.tables where database = currentDatabase( ) order by table"
2021-08-26 13:19:52 +00:00
CLICKHOUSE_CLIENT_DEFAULT_DB = $( echo ${ CLICKHOUSE_CLIENT } | sed 's/' " --database= ${ CLICKHOUSE_DATABASE } " '/--database=default/g' )
2021-08-31 08:53:48 +00:00
for _ in { 1..10} ; do
2021-08-26 13:19:52 +00:00
$CLICKHOUSE_CLIENT_DEFAULT_DB -q " detach database $CLICKHOUSE_DATABASE ; "
$CLICKHOUSE_CLIENT_DEFAULT_DB -q " attach database $CLICKHOUSE_DATABASE ; "
done
$CLICKHOUSE_CLIENT -q " show tables from $CLICKHOUSE_DATABASE ; "
2021-11-02 12:58:45 +00:00
$CLICKHOUSE_CLIENT -q "rename table join to join1" 2>& 1| grep -Fa "some tables depend on it" >/dev/null && echo "OK"
2021-11-01 18:53:07 +00:00
$CLICKHOUSE_CLIENT -q "drop table join" 2>& 1| grep -Fa "some tables depend on it" >/dev/null && echo "OK"
$CLICKHOUSE_CLIENT -q "detach dictionary dict1 permanently" 2>& 1| grep -Fa "some tables depend on it" >/dev/null && echo "OK"
$CLICKHOUSE_CLIENT -q " select table, arraySort(dependencies_table),
arraySort( loading_dependencies_table) , arraySort( loading_dependent_table) from system.tables where database = currentDatabase( ) order by table"
2021-11-02 12:58:45 +00:00
engine = ` $CLICKHOUSE_CLIENT -q " select engine from system.databases where name=' ${ CLICKHOUSE_DATABASE } ' " `
$CLICKHOUSE_CLIENT -q " drop database if exists ${ CLICKHOUSE_DATABASE } _1 "
if [ [ $engine = = "Atomic" ] ] ; then
$CLICKHOUSE_CLIENT -q " rename database ${ CLICKHOUSE_DATABASE } to ${ CLICKHOUSE_DATABASE } _1 " 2>& 1| grep -Fa "some tables depend on it" >/dev/null && echo "OK"
else
echo "OK"
fi
2021-11-03 15:06:19 +00:00
Fix table dependencies in case of failed RENAME TABLE
CI found this [1]
[ 382620 ] {} <Error> Application: Caught exception while loading metadata: Code: 60. DB::Exception: Table test_25.join doesn\'t exist: While processing _CAST(joinGet(test_25.join, \'m\', CAST(\'42\', \'int\')) AS m_tmp_alter1351896088559986425, \'Int32\') AS m: default expression and column type are incomp
[ 382620 ] {} <Error> Application: Code: 60. DB::Exception: Table test_25.join doesn\'t exist: While processing _CAST(joinGet(test_25.join, \'m\', CAST(\'42\', \'int\')) AS m_tmp_alter1351896088559986425, \'Int32\') AS m: default expression and column type are incompatible.: Cannot attach table `test_25`.`t
[1]: https://s3.amazonaws.com/clickhouse-test-reports/48670/d7f865037266ed87538cf4df7ec7e8165681871b/stress_test__asan_.html
The problem here is that after failed RENAME dependencies got lost:
[4e937f39d866] 2023.04.12 00:51:06.833624 [ 13419 ] {61429225-64cb-4fce-b60d-01e0dac6e52c} <Error> executeQuery: Code: 241. DB::Exception: Memory limit (total) exceeded: would use 34.65 GiB (attempt to allocate chunk of 2097419 bytes), maximum: 34.29 GiB. OvercommitTracker decision: Memory overcommit has freed not enough memory. (MEMORY_LIMIT_EXCEEDED) (version 23.4.1.1) (from [::1]:45710) (comment: 01160_table_dependencies.sh) (in query: create database test_25_1), Stack trace (when copying this message, always include the lines below):
[4e937f39d866] 2023.04.12 00:51:07.351914 [ 5151 ] {66d8bdd4-668e-4239-a8af-6b8f17bb5222} <Error> executeQuery: Code: 81. DB::Exception: Database test_25_1 doesn't exist. (UNKNOWN_DATABASE) (version 23.4.1.1) (from [::1]:45762) (comment: 01160_table_dependencies.sh) (in query: rename table t to test_25_1.t), Stack trace (when copying this message, always include the lines below):
And from the test output:
OK
OK
OK
OK
a [] [] []
data_02344 [] [] []
date_table [] [] []
dict1 [] ['dict_src'] ['join']
dict2 [] ['join'] []
dict_src [] [] ['dict1']
dist_02346 [] [] []
join [] ['dict1'] ['dict2','s']
^^^^^^^^^^^^^ no "t"
Signed-off-by: Azat Khuzhin <a.khuzhin@semrush.com>
2023-04-12 09:07:18 +00:00
$CLICKHOUSE_CLIENT -q " rename table t to ${ CLICKHOUSE_DATABASE } _2.t " | & grep -m1 -F -o UNKNOWN_DATABASE
$CLICKHOUSE_CLIENT -q " select table, arraySort(dependencies_table),
arraySort( loading_dependencies_table) , arraySort( loading_dependent_table) from system.tables where database in ( currentDatabase( ) , '$t_database' ) order by table"
2021-11-03 15:06:19 +00:00
$CLICKHOUSE_CLIENT -q "drop table mv"
2021-11-02 12:58:45 +00:00
$CLICKHOUSE_CLIENT -q " create database ${ CLICKHOUSE_DATABASE } _1 "
2021-11-03 15:06:19 +00:00
t_database = ${ CLICKHOUSE_DATABASE }
2021-11-02 12:58:45 +00:00
if [ [ $engine = = "Atomic" ] ] ; then
2021-11-03 15:06:19 +00:00
$CLICKHOUSE_CLIENT -q " rename table t to ${ CLICKHOUSE_DATABASE } _1.t "
$CLICKHOUSE_CLIENT -q " rename database ${ CLICKHOUSE_DATABASE } _1 to ${ CLICKHOUSE_DATABASE } _1_renamed "
t_database = " ${ CLICKHOUSE_DATABASE } _1_renamed "
2021-11-02 12:58:45 +00:00
fi
$CLICKHOUSE_CLIENT -q " select table, arraySort(dependencies_table),
2021-11-03 15:06:19 +00:00
arraySort( loading_dependencies_table) , arraySort( loading_dependent_table) from system.tables where database in ( currentDatabase( ) , '$t_database' ) order by table"
2021-11-02 12:58:45 +00:00
$CLICKHOUSE_CLIENT -q " drop table ${ t_database } .t; "
2021-11-01 18:53:07 +00:00
$CLICKHOUSE_CLIENT -q "drop table s;"
2021-08-26 13:19:52 +00:00
$CLICKHOUSE_CLIENT -q "drop dictionary dict2;"
2021-11-01 18:53:07 +00:00
$CLICKHOUSE_CLIENT -q "select '====='"
$CLICKHOUSE_CLIENT -q " select table, arraySort(dependencies_table),
arraySort( loading_dependencies_table) , arraySort( loading_dependent_table) from system.tables where database = currentDatabase( ) order by table"
2021-11-02 12:58:45 +00:00
if [ [ $engine != "Ordinary" ] ] ; then
$CLICKHOUSE_CLIENT -q "create or replace table dict_src (n int, m int, s String) engine=MergeTree order by (n, m);"
fi
2021-11-01 18:53:07 +00:00
2021-08-26 13:19:52 +00:00
$CLICKHOUSE_CLIENT -q "drop table join;"
2021-11-01 18:53:07 +00:00
$CLICKHOUSE_CLIENT -q "drop dictionary dict1;"
$CLICKHOUSE_CLIENT -q "drop table dict_src;"
2021-11-03 15:06:19 +00:00
if [ [ $t_database != " $CLICKHOUSE_DATABASE " ] ] ; then
$CLICKHOUSE_CLIENT -q " drop database if exists ${ t_database } "
fi
2021-11-01 18:53:07 +00:00
$CLICKHOUSE_CLIENT -q " drop database if exists ${ CLICKHOUSE_DATABASE } _1 "
$CLICKHOUSE_CLIENT -q " create database ${ CLICKHOUSE_DATABASE } _1 "
$CLICKHOUSE_CLIENT -q " create table ${ CLICKHOUSE_DATABASE } _1.xdict_src (n int, m int, s String) engine=MergeTree order by n; "
$CLICKHOUSE_CLIENT -q " create dictionary ${ CLICKHOUSE_DATABASE } _1.ydict1 (n int default 0, m int default 1, s String default 'qqq')
PRIMARY KEY n
SOURCE( CLICKHOUSE( HOST 'localhost' PORT tcpPort( ) USER 'default' TABLE 'xdict_src' PASSWORD '' DB '${CLICKHOUSE_DATABASE}_1' ) )
LIFETIME( MIN 1 MAX 10) LAYOUT( FLAT( ) ) ; "
$CLICKHOUSE_CLIENT -q " create table ${ CLICKHOUSE_DATABASE } _1.zjoin(n int, m int default dictGet(' ${ CLICKHOUSE_DATABASE } _1.ydict1', 'm', 42::UInt64)) engine=Join(any, left, n); "
$CLICKHOUSE_CLIENT -q " drop database ${ CLICKHOUSE_DATABASE } _1 "