-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Behavior: Get column info from information_schema Part I (#808)
- Loading branch information
Showing
14 changed files
with
271 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import List | ||
from dbt.adapters.sql import SQLAdapter | ||
from dbt.adapters.databricks.column import DatabricksColumn | ||
from dbt.adapters.databricks.relation import DatabricksRelation | ||
from dbt.adapters.databricks.utils import handle_missing_objects | ||
from dbt_common.utils.dict import AttrDict | ||
|
||
GET_COLUMNS_COMMENTS_MACRO_NAME = "get_columns_comments" | ||
|
||
|
||
class GetColumnsBehavior(ABC): | ||
@classmethod | ||
@abstractmethod | ||
def get_columns_in_relation( | ||
cls, adapter: SQLAdapter, relation: DatabricksRelation | ||
) -> List[DatabricksColumn]: | ||
pass | ||
|
||
@staticmethod | ||
def _get_columns_with_comments( | ||
adapter: SQLAdapter, relation: DatabricksRelation, macro_name: str | ||
) -> List[AttrDict]: | ||
return list( | ||
handle_missing_objects( | ||
lambda: adapter.execute_macro(macro_name, kwargs={"relation": relation}), | ||
AttrDict(), | ||
) | ||
) | ||
|
||
|
||
class GetColumnsByDescribe(GetColumnsBehavior): | ||
@classmethod | ||
def get_columns_in_relation( | ||
cls, adapter: SQLAdapter, relation: DatabricksRelation | ||
) -> List[DatabricksColumn]: | ||
rows = cls._get_columns_with_comments(adapter, relation, "get_columns_comments") | ||
return cls._parse_columns(rows) | ||
|
||
@classmethod | ||
def _parse_columns(cls, rows: List[AttrDict]) -> List[DatabricksColumn]: | ||
columns = [] | ||
|
||
for row in rows: | ||
if row["col_name"].startswith("#"): | ||
break | ||
columns.append( | ||
DatabricksColumn( | ||
column=row["col_name"], dtype=row["data_type"], comment=row["comment"] | ||
) | ||
) | ||
|
||
return columns | ||
|
||
|
||
class GetColumnsByInformationSchema(GetColumnsByDescribe): | ||
@classmethod | ||
def get_columns_in_relation( | ||
cls, adapter: SQLAdapter, relation: DatabricksRelation | ||
) -> List[DatabricksColumn]: | ||
if relation.is_hive_metastore() or relation.type == DatabricksRelation.View: | ||
return super().get_columns_in_relation(adapter, relation) | ||
|
||
rows = cls._get_columns_with_comments( | ||
adapter, relation, "get_columns_comments_via_information_schema" | ||
) | ||
return cls._parse_columns(rows) | ||
|
||
@classmethod | ||
def _parse_columns(cls, rows: List[AttrDict]) -> List[DatabricksColumn]: | ||
return [DatabricksColumn(column=row[0], dtype=row[1], comment=row[2]) for row in rows] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,4 @@ types-requests | |
types-mock | ||
pre-commit | ||
|
||
dbt-tests-adapter~=1.8.0 | ||
dbt-tests-adapter>=1.8.0, <2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
databricks-sql-connector>=3.4.0, <3.5.0 | ||
dbt-spark~=1.8.0 | ||
dbt-core>=1.8.0, <2.0 | ||
dbt-adapters>=1.3.0, <2.0 | ||
dbt-core>=1.8.7, <2.0 | ||
dbt-common>=1.10.0, <2.0 | ||
dbt-adapters>=1.7.0, <2.0 | ||
databricks-sdk==0.17.0 | ||
keyring>=23.13.0 | ||
protobuf<5.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
base_model = """ | ||
select struct('a', 1, 'b', 'b', 'c', ARRAY(1,2,3)) as struct_col, 'hello' as str_col | ||
""" | ||
|
||
schema = """ | ||
version: 2 | ||
models: | ||
- name: base_model | ||
config: | ||
materialized: table | ||
columns: | ||
- name: struct_col | ||
- name: str_col | ||
""" | ||
|
||
view_schema = """ | ||
version: 2 | ||
models: | ||
- name: base_model | ||
config: | ||
materialized: view | ||
columns: | ||
- name: struct_col | ||
- name: str_col | ||
""" |
Oops, something went wrong.