|
| 1 | +-- Reproduce the Hybrid currentDatabase() metadata bug without a server restart. |
| 2 | +-- Detaching the table and re-attaching it while a different database is current |
| 3 | +-- mimics the missing session-database context that startup ATTACH has. Before the |
| 4 | +-- fix the stored metadata keeps currentDatabase(), which then resolves to the wrong |
| 5 | +-- database and the segment fails to attach with UNKNOWN_TABLE. |
| 6 | + |
| 7 | +SET allow_experimental_hybrid_table = 1; |
| 8 | + |
| 9 | +CREATE TABLE {CLICKHOUSE_DATABASE:Identifier}.local_hot (ts DateTime, value UInt64) ENGINE = MergeTree ORDER BY ts; |
| 10 | +CREATE TABLE {CLICKHOUSE_DATABASE:Identifier}.local_cold (ts DateTime, value UInt64) ENGINE = MergeTree ORDER BY ts; |
| 11 | +INSERT INTO {CLICKHOUSE_DATABASE:Identifier}.local_hot VALUES ('2025-10-15', 1), ('2025-11-01', 2); |
| 12 | +INSERT INTO {CLICKHOUSE_DATABASE:Identifier}.local_cold VALUES ('2025-08-01', 3), ('2025-06-15', 4); |
| 13 | + |
| 14 | +-- Create with the test database as current so currentDatabase() resolves to it. |
| 15 | +USE {CLICKHOUSE_DATABASE:Identifier}; |
| 16 | +CREATE TABLE {CLICKHOUSE_DATABASE:Identifier}.hybrid_t (ts DateTime, value UInt64) |
| 17 | +ENGINE = Hybrid( |
| 18 | + remote('localhost:9000', {CLICKHOUSE_DATABASE:String}, 'local_hot'), ts > '2025-09-01', |
| 19 | + remote('localhost:9000', currentDatabase(), 'local_cold'), ts <= '2025-09-01' |
| 20 | +); |
| 21 | + |
| 22 | +SELECT count() FROM {CLICKHOUSE_DATABASE:Identifier}.hybrid_t; |
| 23 | + |
| 24 | +-- Same table through the DATABASE() alias of currentDatabase(): the metadata rewrite |
| 25 | +-- has to canonicalize the function name instead of comparing it literally. |
| 26 | +CREATE TABLE {CLICKHOUSE_DATABASE:Identifier}.hybrid_alias_t (ts DateTime, value UInt64) |
| 27 | +ENGINE = Hybrid( |
| 28 | + remote('localhost:9000', {CLICKHOUSE_DATABASE:String}, 'local_hot'), ts > '2025-09-01', |
| 29 | + remote('localhost:9000', DATABASE(), 'local_cold'), ts <= '2025-09-01' |
| 30 | +); |
| 31 | + |
| 32 | +SELECT count() FROM {CLICKHOUSE_DATABASE:Identifier}.hybrid_alias_t; |
| 33 | + |
| 34 | +-- Detach, switch the current database, then re-attach. currentDatabase() in the |
| 35 | +-- stored metadata now resolves against the other database, whose local_cold does |
| 36 | +-- not exist. With the fix the metadata holds the resolved name, so this succeeds. |
| 37 | +DETACH TABLE {CLICKHOUSE_DATABASE:Identifier}.hybrid_t; |
| 38 | +DETACH TABLE {CLICKHOUSE_DATABASE:Identifier}.hybrid_alias_t; |
| 39 | +CREATE DATABASE IF NOT EXISTS {CLICKHOUSE_DATABASE_1:Identifier}; |
| 40 | +USE {CLICKHOUSE_DATABASE_1:Identifier}; |
| 41 | +ATTACH TABLE {CLICKHOUSE_DATABASE:Identifier}.hybrid_t; |
| 42 | +ATTACH TABLE {CLICKHOUSE_DATABASE:Identifier}.hybrid_alias_t; |
| 43 | + |
| 44 | +SELECT count() FROM {CLICKHOUSE_DATABASE:Identifier}.hybrid_t; |
| 45 | +SELECT count() FROM {CLICKHOUSE_DATABASE:Identifier}.hybrid_alias_t; |
| 46 | + |
| 47 | +USE {CLICKHOUSE_DATABASE:Identifier}; |
| 48 | +DROP DATABASE {CLICKHOUSE_DATABASE_1:Identifier}; |
0 commit comments