|
| 1 | +import pytest |
| 2 | +from dbt.tests import util |
| 3 | + |
| 4 | +from dbt.adapters.databricks.column import DatabricksColumn |
| 5 | +from dbt.adapters.databricks.relation import DatabricksRelation, DatabricksRelationType |
| 6 | +from tests.functional.adapter.columns import fixtures |
| 7 | +from tests.functional.adapter.fixtures import ( |
| 8 | + RequiresDescribeAsJsonCapabilityMixin, |
| 9 | + fail_if_get_columns_as_json_called_macros, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +# The HMS cluster profile uses hive_metastore, where get_columns_in_relation already |
| 14 | +# takes the legacy path via is_hive_metastore() — before the foreign-table check |
| 15 | +# matters. This test only validates the fix on UC profiles where JSON column |
| 16 | +# metadata would otherwise be preferred. |
| 17 | +@pytest.mark.skip_profile("databricks_cluster") |
| 18 | +class TestForeignTableGetColumns(RequiresDescribeAsJsonCapabilityMixin): |
| 19 | + """Foreign tables must fetch columns without attempting DESCRIBE AS JSON.""" |
| 20 | + |
| 21 | + @pytest.fixture(scope="class") |
| 22 | + def models(self): |
| 23 | + # Lakehouse Federation foreign tables aren't available in CI, so we |
| 24 | + # materialize a normal UC table and assert column fetch works when the |
| 25 | + # relation is typed as Foreign (see test method below). |
| 26 | + return { |
| 27 | + "foreign_table_source.sql": fixtures.foreign_table_source_model, |
| 28 | + "schema.yml": fixtures.foreign_table_source_schema, |
| 29 | + } |
| 30 | + |
| 31 | + @pytest.fixture(scope="class") |
| 32 | + def macros(self): |
| 33 | + return {"fail_if_get_columns_as_json_called.sql": fail_if_get_columns_as_json_called_macros} |
| 34 | + |
| 35 | + @pytest.fixture(scope="class", autouse=True) |
| 36 | + def setup(self, project): |
| 37 | + util.run_dbt(["debug", "--connection"]) |
| 38 | + util.run_dbt(["run"]) |
| 39 | + |
| 40 | + @pytest.fixture(scope="class") |
| 41 | + def expected_columns(self): |
| 42 | + return [ |
| 43 | + DatabricksColumn(column="id", dtype="bigint"), |
| 44 | + DatabricksColumn(column="name", dtype="string"), |
| 45 | + ] |
| 46 | + |
| 47 | + def test_foreign_table_get_columns_returns_expected_columns(self, project, expected_columns): |
| 48 | + # No real federated table in CI — reuse the managed table above but mark |
| 49 | + # the relation Foreign so get_columns_in_relation exercises that code path. |
| 50 | + foreign_relation = DatabricksRelation.create( |
| 51 | + database=project.database, |
| 52 | + schema=project.test_schema, |
| 53 | + identifier="foreign_table_source", |
| 54 | + type=DatabricksRelationType.Foreign, |
| 55 | + ) |
| 56 | + |
| 57 | + with project.adapter.connection_named("_test"): |
| 58 | + actual_columns = project.adapter.get_columns_in_relation(foreign_relation) |
| 59 | + |
| 60 | + assert actual_columns == expected_columns |
0 commit comments