|
| 1 | +import json |
| 2 | +import pytest |
| 3 | +from dbt.tests.util import run_dbt, run_dbt_and_capture, get_connection |
| 4 | + |
| 5 | +model = """ |
| 6 | +{{ |
| 7 | + config( |
| 8 | + remote_config={'cluster': 'test_shard', 'local_db_prefix': '_', 'local_suffix': '_local'}, |
| 9 | + sharding_key='key1', |
| 10 | + ) |
| 11 | +}} |
| 12 | +select toUInt64(number) as key1, toInt64(-number) as key2 from numbers(10) |
| 13 | +""" |
| 14 | + |
| 15 | + |
| 16 | +class TestRemoteTableRemoteConfig: |
| 17 | + @pytest.fixture(scope="class") |
| 18 | + def models(self): |
| 19 | + return { |
| 20 | + "remote_table.sql": model, |
| 21 | + } |
| 22 | + |
| 23 | + @pytest.fixture(scope="class") |
| 24 | + def init_local_table(self, project): |
| 25 | + project.run_sql(f"create database if not exists _{project.test_schema} on cluster test_shard") |
| 26 | + project.run_sql(f""" |
| 27 | + create table _{project.test_schema}.remote_table_local on cluster test_shard |
| 28 | + (key1 UInt64, key2 Int64) |
| 29 | + engine=MergeTree order by key1 |
| 30 | + """) |
| 31 | + return |
| 32 | + |
| 33 | + def test_with_remote_configuration(self, project, init_local_table): |
| 34 | + # the created distributed table should point to a local table as defined in the model's `remote_config` |
| 35 | + materialized_var = {"materialized": "remote_table"} |
| 36 | + run_dbt(["run", "--vars", json.dumps(materialized_var)]) |
| 37 | + |
| 38 | + project.run_sql(f""" |
| 39 | + insert into {project.test_schema}.remote_table |
| 40 | + select toUInt64(number) as key1, toInt64(-number) as key2 from numbers(10) |
| 41 | + """) |
| 42 | + |
| 43 | + self._assert_is_distributed_table(project) |
| 44 | + self._assert_correct_engine(project) |
| 45 | + self._assert_correct_data(project) |
| 46 | + |
| 47 | + # rerun (should be no-op) |
| 48 | + _, log_output = run_dbt_and_capture(["run", "--vars", json.dumps(materialized_var)]) |
| 49 | + assert "no-op run" in log_output |
| 50 | + |
| 51 | + @staticmethod |
| 52 | + def _assert_is_distributed_table(project): |
| 53 | + # check correct table creation on current host |
| 54 | + result = project.run_sql( |
| 55 | + f"select engine from system.tables where name='remote_table'", |
| 56 | + fetch="one" |
| 57 | + ) |
| 58 | + assert result is not None |
| 59 | + assert result[0] == "Distributed" |
| 60 | + |
| 61 | + @staticmethod |
| 62 | + def _assert_correct_engine(project): |
| 63 | + # assert correct engine parameters |
| 64 | + result = project.run_sql(f"select create_table_query from system.tables where name='remote_table'", fetch="one") |
| 65 | + assert f"Distributed('test_shard', '_{project.test_schema}', 'remote_table_local', key1)" in result[0] |
| 66 | + |
| 67 | + @staticmethod |
| 68 | + def _assert_correct_data(project): |
| 69 | + # query remote data from distributed table |
| 70 | + result = project.run_sql("select count(*) as num_rows from remote_table", fetch="one") |
| 71 | + assert result[0] == 10 |
| 72 | + |
| 73 | + |
| 74 | +class TestRemoteTableRemoteConfigReplicatedDB(TestRemoteTableRemoteConfig): |
| 75 | + @pytest.fixture(scope="class") |
| 76 | + def test_config(self, test_config): |
| 77 | + test_config["db_engine"] = "Replicated('/clickhouse/databases/{uuid}', '{shard}', '{replica}')" |
| 78 | + return test_config |
| 79 | + |
| 80 | + @pytest.fixture(scope="class") |
| 81 | + def init_local_table(self, project): |
| 82 | + schema_name = f"_{project.test_schema}" |
| 83 | + with get_connection(project.adapter): |
| 84 | + relation = project.adapter.Relation.create(database=project.database, schema=schema_name) |
| 85 | + project.adapter.create_schema(relation) |
| 86 | + project.created_schemas.append(schema_name) |
| 87 | + |
| 88 | + project.run_sql(f""" |
| 89 | + create table _{project.test_schema}.remote_table_local |
| 90 | + (key1 UInt64, key2 Int64) |
| 91 | + engine=MergeTree order by key1 |
| 92 | + """) |
0 commit comments