|
1 | | -from typing import Optional |
| 1 | +from typing import ClassVar, Optional |
2 | 2 |
|
3 | | -from typing_extensions import Self |
| 3 | +from dbt.adapters.contracts.relation import RelationConfig |
| 4 | +from dbt.adapters.relation_configs.config_base import RelationResults |
| 5 | +from dbt_common.exceptions import DbtRuntimeError |
4 | 6 |
|
5 | | -from dbt.adapters.databricks.logging import logger |
6 | 7 | from dbt.adapters.databricks.relation_configs.base import ( |
7 | | - DatabricksRelationChangeSet, |
| 8 | + DatabricksComponentConfig, |
| 9 | + DatabricksComponentProcessor, |
8 | 10 | DatabricksRelationConfigBase, |
9 | 11 | ) |
10 | | -from dbt.adapters.databricks.relation_configs.column_comments import ColumnCommentsProcessor |
11 | | -from dbt.adapters.databricks.relation_configs.column_tags import ColumnTagsProcessor |
12 | | -from dbt.adapters.databricks.relation_configs.comment import CommentProcessor |
13 | | -from dbt.adapters.databricks.relation_configs.query import QueryProcessor |
14 | 12 | from dbt.adapters.databricks.relation_configs.tags import TagsProcessor |
15 | 13 | from dbt.adapters.databricks.relation_configs.tblproperties import TblPropertiesProcessor |
16 | 14 |
|
17 | 15 |
|
| 16 | +class MetricViewQueryConfig(DatabricksComponentConfig): |
| 17 | + """Component encapsulating the YAML definition of a metric view.""" |
| 18 | + |
| 19 | + query: str |
| 20 | + |
| 21 | + def get_diff(self, other: "MetricViewQueryConfig") -> Optional["MetricViewQueryConfig"]: |
| 22 | + # Normalize whitespace for comparison |
| 23 | + self_normalized = " ".join(self.query.split()) |
| 24 | + other_normalized = " ".join(other.query.split()) |
| 25 | + if self_normalized != other_normalized: |
| 26 | + return self |
| 27 | + return None |
| 28 | + |
| 29 | + |
| 30 | +class MetricViewQueryProcessor(DatabricksComponentProcessor[MetricViewQueryConfig]): |
| 31 | + """Processor for metric view YAML definitions. |
| 32 | +
|
| 33 | + Metric views store their YAML definitions in information_schema.views, but wrapped |
| 34 | + in $$ delimiters. This processor extracts and compares the YAML content. |
| 35 | + """ |
| 36 | + |
| 37 | + name: ClassVar[str] = "query" |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def from_relation_results(cls, result: RelationResults) -> MetricViewQueryConfig: |
| 41 | + from dbt.adapters.databricks.logging import logger |
| 42 | + |
| 43 | + # Get the view text from DESCRIBE EXTENDED output |
| 44 | + describe_extended = result.get("describe_extended") |
| 45 | + if not describe_extended: |
| 46 | + raise DbtRuntimeError( |
| 47 | + f"Cannot find metric view description. Result keys: {list(result.keys())}" |
| 48 | + ) |
| 49 | + |
| 50 | + # Find the "View Text" row in DESCRIBE EXTENDED output |
| 51 | + view_definition = None |
| 52 | + for row in describe_extended: |
| 53 | + if row[0] == "View Text": |
| 54 | + view_definition = row[1] |
| 55 | + break |
| 56 | + |
| 57 | + logger.debug( |
| 58 | + f"MetricViewQueryProcessor: view_definition = " |
| 59 | + f"{view_definition[:200] if view_definition else 'None'}" |
| 60 | + ) |
| 61 | + |
| 62 | + if not view_definition: |
| 63 | + raise DbtRuntimeError("Metric view has no 'View Text' in DESCRIBE EXTENDED output") |
| 64 | + |
| 65 | + view_definition = view_definition.strip() |
| 66 | + |
| 67 | + # Extract YAML content from $$ delimiters if present |
| 68 | + # Format: $$ yaml_content $$ |
| 69 | + if "$$" in view_definition: |
| 70 | + parts = view_definition.split("$$") |
| 71 | + if len(parts) >= 2: |
| 72 | + # The YAML is between the first and second $$ markers |
| 73 | + view_definition = parts[1].strip() |
| 74 | + |
| 75 | + return MetricViewQueryConfig(query=view_definition) |
| 76 | + |
| 77 | + @classmethod |
| 78 | + def from_relation_config(cls, relation_config: RelationConfig) -> MetricViewQueryConfig: |
| 79 | + query = relation_config.compiled_code |
| 80 | + |
| 81 | + if query: |
| 82 | + return MetricViewQueryConfig(query=query.strip()) |
| 83 | + else: |
| 84 | + raise DbtRuntimeError( |
| 85 | + f"Cannot compile metric view {relation_config.identifier} with no YAML definition" |
| 86 | + ) |
| 87 | + |
| 88 | + |
18 | 89 | class MetricViewConfig(DatabricksRelationConfigBase): |
| 90 | + """Config for metric views. |
| 91 | +
|
| 92 | + Metric views use YAML definitions stored in information_schema.views wrapped in $$ delimiters. |
| 93 | + Changes to the YAML definition can be applied via ALTER VIEW AS. |
| 94 | + Tags and tblproperties can also be altered incrementally. |
| 95 | + """ |
| 96 | + |
19 | 97 | config_components = [ |
20 | 98 | TagsProcessor, |
21 | 99 | TblPropertiesProcessor, |
22 | | - QueryProcessor, |
23 | | - CommentProcessor, |
24 | | - ColumnCommentsProcessor, |
25 | | - ColumnTagsProcessor, |
| 100 | + MetricViewQueryProcessor, |
26 | 101 | ] |
27 | | - |
28 | | - def get_changeset(self, existing: Self) -> Optional[DatabricksRelationChangeSet]: |
29 | | - changeset = super().get_changeset(existing) |
30 | | - if changeset: |
31 | | - # Metric views: query changes require full refresh (can't ALTER YAML definition) |
32 | | - if "query" in changeset.changes: |
33 | | - logger.debug( |
34 | | - "Metric view YAML definition changed, requiring replace, as there is" |
35 | | - " no API to update the YAML specification via ALTER." |
36 | | - ) |
37 | | - changeset.requires_full_refresh = True |
38 | | - # Comment changes also require full refresh for metric views |
39 | | - if "comment" in changeset.changes: |
40 | | - logger.debug( |
41 | | - "Metric view description changed, requiring replace, as there is" |
42 | | - " no API yet to update comments." |
43 | | - ) |
44 | | - changeset.requires_full_refresh = True |
45 | | - # Column comment changes require full refresh for metric views |
46 | | - if "column_comments" in changeset.changes: |
47 | | - logger.debug( |
48 | | - "Metric view column comments changed, requiring replace, as there is" |
49 | | - " no API to update column comments for metric views." |
50 | | - ) |
51 | | - changeset.requires_full_refresh = True |
52 | | - return changeset |
|
0 commit comments