From 6c6faaa05d343bcd4f1aab362a295ada1c299bc5 Mon Sep 17 00:00:00 2001 From: Vitaly Baranov Date: Fri, 12 Apr 2024 15:30:25 +0200 Subject: [PATCH] Add test. --- .../test_backup_restore_new/test.py | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/tests/integration/test_backup_restore_new/test.py b/tests/integration/test_backup_restore_new/test.py index 34ffdf7a8df..4b6ccd24e4f 100644 --- a/tests/integration/test_backup_restore_new/test.py +++ b/tests/integration/test_backup_restore_new/test.py @@ -1279,6 +1279,93 @@ def test_projection(): ) +def test_restore_table_not_evaluate_table_defaults(): + instance.query("CREATE DATABASE test") + instance.query( + "CREATE TABLE test.src(key Int64, value Int64) ENGINE=MergeTree ORDER BY key" + ) + instance.query( + "INSERT INTO test.src SELECT number as key, number * number AS value FROM numbers(1, 3)" + ) + instance.query( + "INSERT INTO test.src SELECT number as key, number * number AS value FROM numbers(6, 3)" + ) + instance.query("CREATE USER u1") + instance.query("GRANT SELECT ON test.src TO u1") + instance.query( + "CREATE DICTIONARY test.dict(key Int64, value Int64 DEFAULT -1) PRIMARY KEY key SOURCE(CLICKHOUSE(HOST 'localhost' PORT 9000 DB 'test' TABLE 'src' USER u1)) LIFETIME(0) LAYOUT(FLAT())" + ) + instance.query( + "CREATE TABLE test.tbl(a Int64, b Int64 DEFAULT 0, c Int64 DEFAULT dictGet(test.dict, 'value', b)) ENGINE=MergeTree ORDER BY a" + ) + instance.query( + "INSERT INTO test.tbl (a, b) SELECT number, number + 1 FROM numbers(5)" + ) + + backup_name = new_backup_name() + instance.query(f"BACKUP TABLE system.users, DATABASE test TO {backup_name}") + + instance.query("DROP USER u1") + + instance.query( + f"RESTORE TABLE system.users, DATABASE test AS test2 FROM {backup_name}" + ) + + # RESTORE should not try to load dictionary `test2.dict` + assert instance.query("SELECT * FROM test2.tbl ORDER BY a") == TSV( + [[0, 1, 1], [1, 2, 4], [2, 3, 9], [3, 4, -1], [4, 5, -1]] + ) + + assert ( + instance.query( + "SELECT status FROM system.dictionaries WHERE name = 'dict' AND database = 'test2'" + ) + == "NOT_LOADED\n" + ) + + # INSERT needs dictionary `test2.dict` and it will cause loading it. + error = "necessary to have the grant SELECT(key, value) ON test2.src" # User `u1` has no privileges for reading `test2.src` + assert error in instance.query_and_get_error( + "INSERT INTO test2.tbl (a, b) SELECT number, number + 1 FROM numbers(5, 5)" + ) + + assert ( + instance.query( + "SELECT status FROM system.dictionaries WHERE name = 'dict' AND database = 'test2'" + ) + == "FAILED\n" + ) + + instance.query("GRANT SELECT ON test2.src TO u1") + instance.query("SYSTEM RELOAD DICTIONARY test2.dict") + + assert ( + instance.query( + "SELECT status FROM system.dictionaries WHERE name = 'dict' AND database = 'test2'" + ) + == "LOADED\n" + ) + + instance.query( + "INSERT INTO test2.tbl (a, b) SELECT number, number + 1 FROM numbers(5, 5)" + ) + + assert instance.query("SELECT * FROM test2.tbl ORDER BY a") == TSV( + [ + [0, 1, 1], + [1, 2, 4], + [2, 3, 9], + [3, 4, -1], + [4, 5, -1], + [5, 6, 36], + [6, 7, 49], + [7, 8, 64], + [8, 9, -1], + [9, 10, -1], + ] + ) + + def test_system_functions(): instance.query("CREATE FUNCTION linear_equation AS (x, k, b) -> k*x + b;")