From 09100c8db5b9c9f8ab52042d522e2a27a1367d3b Mon Sep 17 00:00:00 2001 From: Takuya UESHIN Date: Mon, 14 Nov 2022 16:19:21 -0800 Subject: [PATCH] Support new error messages. (#226) ### Description Supports new error messages. In `SparkAdapter.get_columns_in_relation`, it checks the error message when the specified table or view doesn't exist: https://github.com/dbt-labs/dbt-spark/blob/c87b6b2c48bcefb0ce52cd64984d3129d6f14ea0/dbt/adapters/spark/impl.py#L223 but, Spark will change the error message in the future release (apache/spark#37887), which causes the function to raise the `dbt.exceptions.RuntimeException` instead of returning an empty list. The function should also check whether the error message contains `[TABLE_OR_VIEW_NOT_FOUND]` or not. This will be reverted once dbt-labs/dbt-spark#515 is resolved. --- dbt/adapters/databricks/impl.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/dbt/adapters/databricks/impl.py b/dbt/adapters/databricks/impl.py index 62affa9ca..b2fce460b 100644 --- a/dbt/adapters/databricks/impl.py +++ b/dbt/adapters/databricks/impl.py @@ -12,6 +12,7 @@ from dbt.adapters.base.relation import BaseRelation from dbt.adapters.spark.impl import ( SparkAdapter, + GET_COLUMNS_IN_RELATION_RAW_MACRO_NAME, KEY_TABLE_OWNER, KEY_TABLE_STATISTICS, LIST_RELATIONS_MACRO_NAME, @@ -180,6 +181,34 @@ def parse_describe_extended( # type: ignore[override] for idx, column in enumerate(rows) ] + def get_columns_in_relation( # type: ignore[override] + self, relation: DatabricksRelation + ) -> List[DatabricksColumn]: + columns = [] + try: + rows: List[Row] = self.execute_macro( + GET_COLUMNS_IN_RELATION_RAW_MACRO_NAME, kwargs={"relation": relation} + ) + columns = self.parse_describe_extended(relation, rows) + except dbt.exceptions.RuntimeException as e: + # spark would throw error when table doesn't exist, where other + # CDW would just return and empty list, normalizing the behavior here + errmsg = getattr(e, "msg", "") + if any( + msg in errmsg + for msg in ( + "[TABLE_OR_VIEW_NOT_FOUND]", + "Table or view not found", + "NoSuchTableException", + ) + ): + pass + else: + raise e + + # strip hudi metadata columns. + return [x for x in columns if x.name not in self.HUDI_METADATA_COLUMNS] + def parse_columns_from_information( # type: ignore[override] self, relation: DatabricksRelation ) -> List[DatabricksColumn]: