From 2399002565c0c9393f0055f1d2a95a162eed1961 Mon Sep 17 00:00:00 2001 From: Ben Cassell <98852248+benc-db@users.noreply.github.com> Date: Thu, 25 Jul 2024 14:18:31 -0700 Subject: [PATCH] Backport close cursor code to 1.7 (#746) --- CHANGELOG.md | 6 ++++++ dbt/adapters/databricks/__version__.py | 2 +- dbt/adapters/databricks/column.py | 5 +++++ dbt/adapters/databricks/impl.py | 16 ++++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 179230965..5accc1c5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## dbt-databricks 1.7.17 (July 25, 2024) + +### Fixes + +- Backport warning prevention from 1.9 + ## dbt-databricks 1.7.16 (May 21, 2024) ### Fixes diff --git a/dbt/adapters/databricks/__version__.py b/dbt/adapters/databricks/__version__.py index d6ebbbd6f..733092a36 100644 --- a/dbt/adapters/databricks/__version__.py +++ b/dbt/adapters/databricks/__version__.py @@ -1 +1 @@ -version: str = "1.7.16" +version: str = "1.7.17" diff --git a/dbt/adapters/databricks/column.py b/dbt/adapters/databricks/column.py index c0e4367c9..b79735fc4 100644 --- a/dbt/adapters/databricks/column.py +++ b/dbt/adapters/databricks/column.py @@ -17,6 +17,11 @@ class DatabricksColumn(SparkColumn): def translate_type(cls, dtype: str) -> str: return super(SparkColumn, cls).translate_type(dtype).lower() + @classmethod + def create(cls, name: str, label_or_dtype: str) -> "DatabricksColumn": + column_type = cls.translate_type(label_or_dtype) + return cls(name, column_type) + @property def data_type(self) -> str: return self.translate_type(self.dtype) diff --git a/dbt/adapters/databricks/impl.py b/dbt/adapters/databricks/impl.py index 6b21a1c98..a46837496 100644 --- a/dbt/adapters/databricks/impl.py +++ b/dbt/adapters/databricks/impl.py @@ -389,6 +389,22 @@ def _type_from_names( else: return DatabricksRelationType.Unknown + @available.parse(lambda *a, **k: []) + def get_column_schema_from_query(self, sql: str) -> List[DatabricksColumn]: + """Get a list of the Columns with names and data types from the given sql.""" + _, cursor = self.connections.add_select_query(sql) + try: + columns: List[DatabricksColumn] = [ + self.Column.create( + column_name, self.connections.data_type_code_to_name(column_type_code) + ) + # https://peps.python.org/pep-0249/#description + for column_name, column_type_code, *_ in cursor.description + ] + finally: + cursor.close() + return columns + def get_relation( self, database: Optional[str],