|
| 1 | +"""Test parallel insert into iceberg table.""" |
| 2 | +import copy |
| 3 | +import os |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from dbt.artifacts.schemas.results import RunStatus |
| 8 | +from dbt.tests.util import check_relations_equal, run_dbt, run_dbt_and_capture |
| 9 | + |
| 10 | +PARALLELISM = 10 |
| 11 | + |
| 12 | +base_dbt_profile = { |
| 13 | + "type": "athena", |
| 14 | + "s3_staging_dir": os.getenv("DBT_TEST_ATHENA_S3_STAGING_DIR"), |
| 15 | + "s3_tmp_table_dir": os.getenv("DBT_TEST_ATHENA_S3_TMP_TABLE_DIR"), |
| 16 | + "schema": os.getenv("DBT_TEST_ATHENA_SCHEMA"), |
| 17 | + "database": os.getenv("DBT_TEST_ATHENA_DATABASE"), |
| 18 | + "region_name": os.getenv("DBT_TEST_ATHENA_REGION_NAME"), |
| 19 | + "threads": PARALLELISM, |
| 20 | + "poll_interval": float(os.getenv("DBT_TEST_ATHENA_POLL_INTERVAL", "1.0")), |
| 21 | + "num_retries": 0, |
| 22 | + "work_group": os.getenv("DBT_TEST_ATHENA_WORK_GROUP"), |
| 23 | + "aws_profile_name": os.getenv("DBT_TEST_ATHENA_AWS_PROFILE_NAME") or None, |
| 24 | +} |
| 25 | + |
| 26 | +models__target = """ |
| 27 | +{{ |
| 28 | + config( |
| 29 | + table_type='iceberg', |
| 30 | + materialized='table' |
| 31 | + ) |
| 32 | +}} |
| 33 | +
|
| 34 | +select * from ( |
| 35 | + values |
| 36 | + (1, -1) |
| 37 | +) as t (id, status) |
| 38 | +limit 0 |
| 39 | +
|
| 40 | +""" |
| 41 | + |
| 42 | +models__source = { |
| 43 | + f"model_{i}.sql": f""" |
| 44 | +{{{{ |
| 45 | + config( |
| 46 | + table_type='iceberg', |
| 47 | + materialized='table', |
| 48 | + tags=['src'], |
| 49 | + pre_hook='insert into target values ({i}, {i})' |
| 50 | + ) |
| 51 | +}}}} |
| 52 | +
|
| 53 | +select 1 as col |
| 54 | +""" |
| 55 | + for i in range(PARALLELISM) |
| 56 | +} |
| 57 | + |
| 58 | +seeds__expected_target_init = "id,status" |
| 59 | +seeds__expected_target_post = "id,status\n" + "\n".join([f"{i},{i}" for i in range(PARALLELISM)]) |
| 60 | + |
| 61 | + |
| 62 | +class TestIcebergRetriesDisabled: |
| 63 | + @pytest.fixture(scope="class") |
| 64 | + def dbt_profile_target(self): |
| 65 | + profile = copy.deepcopy(base_dbt_profile) |
| 66 | + profile["num_iceberg_retries"] = 0 |
| 67 | + return profile |
| 68 | + |
| 69 | + @pytest.fixture(scope="class") |
| 70 | + def models(self): |
| 71 | + return {**{"target.sql": models__target}, **models__source} |
| 72 | + |
| 73 | + @pytest.fixture(scope="class") |
| 74 | + def seeds(self): |
| 75 | + return { |
| 76 | + "expected_target_init.csv": seeds__expected_target_init, |
| 77 | + "expected_target_post.csv": seeds__expected_target_post, |
| 78 | + } |
| 79 | + |
| 80 | + def test__retries_iceberg(self, project): |
| 81 | + """Seed should match the model after run""" |
| 82 | + |
| 83 | + expected__init_seed_name = "expected_target_init" |
| 84 | + run_dbt(["seed", "--select", expected__init_seed_name, "--full-refresh"]) |
| 85 | + |
| 86 | + relation_name = "target" |
| 87 | + model_run = run_dbt(["run", "--select", relation_name]) |
| 88 | + model_run_result = model_run.results[0] |
| 89 | + assert model_run_result.status == RunStatus.Success |
| 90 | + check_relations_equal(project.adapter, [relation_name, expected__init_seed_name]) |
| 91 | + |
| 92 | + expected__post_seed_name = "expected_target_post" |
| 93 | + run_dbt(["seed", "--select", expected__post_seed_name, "--full-refresh"]) |
| 94 | + |
| 95 | + run, log = run_dbt_and_capture(["run", "--select", "tag:src"], expect_pass=False) |
| 96 | + assert any(model_run_result.status == RunStatus.Error for model_run_result in run.results) |
| 97 | + assert "ICEBERG_COMMIT_ERROR" in log |
| 98 | + |
| 99 | + |
| 100 | +class TestIcebergRetriesEnabled: |
| 101 | + @pytest.fixture(scope="class") |
| 102 | + def dbt_profile_target(self): |
| 103 | + profile = copy.deepcopy(base_dbt_profile) |
| 104 | + profile["num_iceberg_retries"] = 1 |
| 105 | + return profile |
| 106 | + |
| 107 | + @pytest.fixture(scope="class") |
| 108 | + def models(self): |
| 109 | + return {**{"target.sql": models__target}, **models__source} |
| 110 | + |
| 111 | + @pytest.fixture(scope="class") |
| 112 | + def seeds(self): |
| 113 | + return { |
| 114 | + "expected_target_init.csv": seeds__expected_target_init, |
| 115 | + "expected_target_post.csv": seeds__expected_target_post, |
| 116 | + } |
| 117 | + |
| 118 | + def test__retries_iceberg(self, project): |
| 119 | + """Seed should match the model after run""" |
| 120 | + |
| 121 | + expected__init_seed_name = "expected_target_init" |
| 122 | + run_dbt(["seed", "--select", expected__init_seed_name, "--full-refresh"]) |
| 123 | + |
| 124 | + relation_name = "target" |
| 125 | + model_run = run_dbt(["run", "--select", relation_name]) |
| 126 | + model_run_result = model_run.results[0] |
| 127 | + assert model_run_result.status == RunStatus.Success |
| 128 | + check_relations_equal(project.adapter, [relation_name, expected__init_seed_name]) |
| 129 | + |
| 130 | + expected__post_seed_name = "expected_target_post" |
| 131 | + run_dbt(["seed", "--select", expected__post_seed_name, "--full-refresh"]) |
| 132 | + |
| 133 | + run = run_dbt(["run", "--select", "tag:src"]) |
| 134 | + assert all([model_run_result.status == RunStatus.Success for model_run_result in run.results]) |
| 135 | + check_relations_equal(project.adapter, [relation_name, expected__post_seed_name]) |
0 commit comments