Skip to content

Commit 010dbc2

Browse files
committed
Fix!: sqlmesh.dbt.adapter.RuntimeAdapter.get_columns_in_relation()
returns columns in the proper Column subclass type. fixes #1874
1 parent 8fbf26c commit 010dbc2

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ filterwarnings = [
272272
]
273273
retry_delay = 10
274274

275+
[tool.ruff]
276+
line-length = 100
277+
275278
[tool.ruff.lint]
276279
select = [
277280
"F401",

sqlmesh/core/engine_adapter/bigquery.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,13 @@ def create_schema(
266266
raise
267267
logger.warning("Failed to create schema '%s': %s", schema_name, e)
268268

269+
def get_bq_schema(self, table_name: TableName) -> t.List[bigquery.SchemaField]:
270+
table = exp.to_table(table_name)
271+
if len(table.parts) == 3 and "." in table.name:
272+
self.execute(exp.select("*").from_(table).limit(0))
273+
return self._query_job._query_results.schema
274+
return self._get_table(table).schema
275+
269276
def columns(
270277
self, table_name: TableName, include_pseudo_columns: bool = False
271278
) -> t.Dict[str, exp.DataType]:

sqlmesh/dbt/adapter.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,31 @@ def list_relations_without_caching(self, schema_relation: BaseRelation) -> t.Lis
291291
return relations
292292

293293
def get_columns_in_relation(self, relation: BaseRelation) -> t.List[Column]:
294-
from dbt.adapters.base.column import Column
295-
296294
mapped_table = self._map_table_name(self._normalize(self._relation_to_table(relation)))
295+
296+
if self.project_dialect == "bigquery":
297+
# dbt.adapters.bigquery.column.BigQueryColumn has a different constructor signature
298+
# We need to use BigQueryColumn.create_from_field() to create the column instead
299+
if (
300+
hasattr(self.column_type, "create_from_field")
301+
and callable(getattr(self.column_type, "create_from_field"))
302+
and hasattr(self.engine_adapter, "get_bq_schema")
303+
and callable(getattr(self.engine_adapter, "get_bq_schema"))
304+
):
305+
return [
306+
self.column_type.create_from_field(field) # type: ignore
307+
for field in self.engine_adapter.get_bq_schema(mapped_table) # type: ignore
308+
]
309+
from dbt.adapters.base.column import Column
310+
311+
return [
312+
Column.from_description(
313+
name=name, raw_data_type=dtype.sql(dialect=self.project_dialect)
314+
)
315+
for name, dtype in self.engine_adapter.columns(table_name=mapped_table).items()
316+
]
297317
return [
298-
Column.from_description(
318+
self.column_type.from_description(
299319
name=name, raw_data_type=dtype.sql(dialect=self.project_dialect)
300320
)
301321
for name, dtype in self.engine_adapter.columns(table_name=mapped_table).items()

0 commit comments

Comments
 (0)