Skip to content

Commit dece00e

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 dece00e

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
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: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,36 @@ 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+
columns = self.engine_adapter.columns(table_name=mapped_table).items()
297+
298+
if self.project_dialect == "bigquery":
299+
# dbt.adapters.bigquery.column.BigQueryColumn has a different constructor signature
300+
# We need to use BigQueryColumn.create_from_field() to create the column instead
301+
if (
302+
hasattr(self.column_type, "create_from_field")
303+
and callable(getattr(self.column_type, "create_from_field"))
304+
and hasattr(self.engine_adapter, "get_bq_schema")
305+
and callable(getattr(self.engine_adapter, "get_bq_schema"))
306+
):
307+
return [
308+
self.column_type.create_from_field(field) # type: ignore
309+
for field in self.engine_adapter.get_bq_schema(mapped_table) # type: ignore
310+
]
311+
from dbt.adapters.base.column import Column
312+
313+
return [
314+
Column.from_description(
315+
name=name, raw_data_type=dtype.sql(dialect=self.project_dialect)
316+
)
317+
for name, dtype in columns
318+
]
297319
return [
298-
Column.from_description(
320+
self.column_type.from_description(
299321
name=name, raw_data_type=dtype.sql(dialect=self.project_dialect)
300322
)
301-
for name, dtype in self.engine_adapter.columns(table_name=mapped_table).items()
323+
for name, dtype in columns
302324
]
303325

304326
def get_missing_columns(

0 commit comments

Comments
 (0)