|
| 1 | +import pytest |
| 2 | +from dbt.tests.util import run_dbt, write_file |
| 3 | + |
| 4 | + |
| 5 | +_MODEL_ICEBERG_BASE = """ |
| 6 | +{{ |
| 7 | + config( |
| 8 | + materialized="incremental", |
| 9 | + table_format="iceberg", |
| 10 | + external_volume="s3_iceberg_snow", |
| 11 | + on_schema_change="append_new_columns" |
| 12 | + ) |
| 13 | +}} |
| 14 | +
|
| 15 | +select 1 as id, |
| 16 | +cast('John' as varchar) as first_name |
| 17 | +""" |
| 18 | + |
| 19 | +_MODEL_ICEBERG_ADDED_COLUMN = """ |
| 20 | +{{ |
| 21 | + config( |
| 22 | + materialized="incremental", |
| 23 | + table_format="iceberg", |
| 24 | + external_volume="s3_iceberg_snow", |
| 25 | + on_schema_change="append_new_columns" |
| 26 | + ) |
| 27 | +}} |
| 28 | +
|
| 29 | +select 1 as id, |
| 30 | +cast('John' as varchar) as first_name, |
| 31 | +cast('Smith' as varchar) as last_name |
| 32 | +""" |
| 33 | + |
| 34 | +_MODEL_ICEBERG_ADDED_STRING_COLUMN = """ |
| 35 | +{{ |
| 36 | + config( |
| 37 | + materialized="incremental", |
| 38 | + table_format="iceberg", |
| 39 | + external_volume="s3_iceberg_snow", |
| 40 | + on_schema_change="append_new_columns" |
| 41 | + ) |
| 42 | +}} |
| 43 | +
|
| 44 | +select 1 as id, |
| 45 | +cast('John' as varchar) as first_name, |
| 46 | +cast('Smith' as string) as last_name |
| 47 | +""" |
| 48 | + |
| 49 | +_MODEL_ICEBERG_ADDED_SIZED_VARCHAR_COLUMN = """ |
| 50 | +{{ |
| 51 | + config( |
| 52 | + materialized="incremental", |
| 53 | + table_format="iceberg", |
| 54 | + external_volume="ICEBERG_SANDBOX", |
| 55 | + catalog="SNOWFLAKE", |
| 56 | + on_schema_change="append_new_columns" |
| 57 | + ) |
| 58 | +}} |
| 59 | +
|
| 60 | +select 1 as id, |
| 61 | +cast('John' as varchar) as first_name, |
| 62 | +cast('Smith' as varchar(134217728)) as last_name |
| 63 | +""" |
| 64 | + |
| 65 | + |
| 66 | +class TestIcebergSchemaChange: |
| 67 | + """ |
| 68 | + Test schema changes with Iceberg tables to ensure VARCHAR columns work correctly. |
| 69 | +
|
| 70 | + This tests the fix for the bug where adding VARCHAR columns to Iceberg tables |
| 71 | + fails because dbt generates VARCHAR(16777216) which is not supported by Snowflake |
| 72 | + Iceberg tables. The fix should use STRING instead for Iceberg tables. |
| 73 | + """ |
| 74 | + |
| 75 | + @pytest.fixture(scope="class") |
| 76 | + def models(self): |
| 77 | + return { |
| 78 | + "test_iceberg_base.sql": _MODEL_ICEBERG_BASE, |
| 79 | + } |
| 80 | + |
| 81 | + def test_iceberg_varchar_column_addition(self, project): |
| 82 | + """Test that adding VARCHAR columns to Iceberg tables works correctly.""" |
| 83 | + |
| 84 | + # First, create the initial table |
| 85 | + run_dbt(["run", "--select", "test_iceberg_base"]) |
| 86 | + |
| 87 | + # Verify the table was created successfully |
| 88 | + results = run_dbt(["run", "--select", "test_iceberg_base"]) |
| 89 | + assert len(results) == 1 |
| 90 | + |
| 91 | + # Now add a VARCHAR column by updating the model |
| 92 | + write_file(_MODEL_ICEBERG_ADDED_COLUMN, "models", "test_iceberg_base.sql") |
| 93 | + |
| 94 | + # This should not fail with the varchar size error |
| 95 | + results = run_dbt(["run", "--select", "test_iceberg_base"]) |
| 96 | + assert len(results) == 1 |
| 97 | + assert results[0].status == "success" |
| 98 | + |
| 99 | + def test_iceberg_string_column_addition(self, project): |
| 100 | + """Test that adding STRING columns to Iceberg tables works correctly.""" |
| 101 | + |
| 102 | + # First, create the initial table |
| 103 | + run_dbt(["run", "--select", "test_iceberg_base"]) |
| 104 | + |
| 105 | + # Now add a STRING column by updating the model |
| 106 | + write_file(_MODEL_ICEBERG_ADDED_STRING_COLUMN, "models", "test_iceberg_base.sql") |
| 107 | + |
| 108 | + # This should work fine |
| 109 | + results = run_dbt(["run", "--select", "test_iceberg_base"]) |
| 110 | + assert len(results) == 1 |
| 111 | + assert results[0].status == "success" |
| 112 | + |
| 113 | + def test_iceberg_max_varchar_column_addition(self, project): |
| 114 | + """Test that adding VARCHAR with max size to Iceberg tables works correctly.""" |
| 115 | + |
| 116 | + # First, create the initial table |
| 117 | + run_dbt(["run", "--select", "test_iceberg_base"]) |
| 118 | + |
| 119 | + # Now add a VARCHAR column with max size by updating the model |
| 120 | + write_file(_MODEL_ICEBERG_ADDED_SIZED_VARCHAR_COLUMN, "models", "test_iceberg_base.sql") |
| 121 | + |
| 122 | + # This should work fine |
| 123 | + results = run_dbt(["run", "--select", "test_iceberg_base"]) |
| 124 | + assert len(results) == 1 |
| 125 | + assert results[0].status == "success" |
| 126 | + |
| 127 | + |
| 128 | +class TestIcebergSchemaChangeIntegration: |
| 129 | + |
| 130 | + @pytest.fixture(scope="class") |
| 131 | + def models(self): |
| 132 | + return { |
| 133 | + "test_iceberg.sql": _MODEL_ICEBERG_BASE, |
| 134 | + } |
| 135 | + |
| 136 | + def test_reproduce_and_fix_bug(self, project): |
| 137 | + |
| 138 | + # Step 1: Create the initial incremental iceberg table |
| 139 | + results = run_dbt(["run"]) |
| 140 | + assert len(results) == 1 |
| 141 | + assert results[0].status == "success" |
| 142 | + |
| 143 | + # Step 2: Modify the model to add new column (this used to fail) |
| 144 | + write_file(_MODEL_ICEBERG_ADDED_COLUMN, "models", "test_iceberg.sql") |
| 145 | + |
| 146 | + # Step 3: Run dbt build again - this should now work with our fix |
| 147 | + results = run_dbt(["run"]) |
| 148 | + assert len(results) == 1 |
| 149 | + assert results[0].status == "success" |
0 commit comments