-
Notifications
You must be signed in to change notification settings - Fork 144
Support BigQuery nested STRUCT fields in anomaly tests #1012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
d45a775
8c7b36e
e73e9c5
17c3574
db5061b
e791b3e
a5730b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,9 @@ | |
| {%- set timestamp_column = metric_properties.timestamp_column %} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. General note - unless very complicated, we need integration tests of column + dimension tests with struct fields. |
||
| {% set prefixed_dimensions = [] %} | ||
| {% for dimension_column in dimensions %} | ||
| {% do prefixed_dimensions.append("dimension_" ~ dimension_column) %} | ||
| {% do prefixed_dimensions.append( | ||
| "dimension_" ~ elementary.bq_safe_alias(dimension_column) | ||
| ) %} | ||
| {% endfor %} | ||
|
|
||
| {% set metric_types = [] %} | ||
|
|
@@ -53,7 +55,7 @@ | |
| ), | ||
| filtered_monitored_table as ( | ||
| select | ||
| {{ column_obj.quoted }}, | ||
| {{ column_obj.quoted }} as {{ column_obj.safe_alias }}, | ||
| {%- if dimensions -%} | ||
| {{ | ||
| elementary.select_dimensions_columns( | ||
|
|
@@ -78,7 +80,7 @@ | |
| {%- else %} | ||
| filtered_monitored_table as ( | ||
| select | ||
| {{ column_obj.quoted }}, | ||
| {{ column_obj.quoted }} as {{ column_obj.safe_alias }}, | ||
| {%- if dimensions -%} | ||
| {{ | ||
| elementary.select_dimensions_columns( | ||
|
|
@@ -94,7 +96,7 @@ | |
| column_metrics as ( | ||
|
|
||
| {%- if column_metrics %} | ||
| {%- set column = column_obj.quoted -%} | ||
| {%- set column = column_obj.safe_alias -%} | ||
| select | ||
| {%- if timestamp_column %} | ||
| edr_bucket_start as bucket_start, edr_bucket_end as bucket_end, | ||
|
|
@@ -341,17 +343,82 @@ | |
| {% endif %} | ||
| {% endmacro %} | ||
|
|
||
| {# Updated to segment-quote nested dimensions on BigQuery and sanitise the | ||
| alias suffix. Backward compatible for non-nested columns and non-BQ adapters. #} | ||
| {% macro select_dimensions_columns(dimension_columns, as_prefix="") %} | ||
| {% set select_statements %} | ||
| {%- for column in dimension_columns -%} | ||
| {{ column }} | ||
| {%- if as_prefix -%} | ||
| {{ " as " ~ as_prefix ~ "_" ~ column }} | ||
| {%- endif -%} | ||
| {%- if not loop.last -%} | ||
| {{ ", " }} | ||
| {%- set _is_nested_bq = (target.type == 'bigquery' and '.' in column) -%} | ||
| {%- set _source = elementary.bq_segment_quote(column) if _is_nested_bq else column -%} | ||
| {%- set _alias_suffix = elementary.bq_safe_alias(column) if _is_nested_bq else column -%} | ||
| {{ _source }}{{ " as " ~ as_prefix ~ "_" ~ _alias_suffix }} | ||
| {%- else -%} | ||
| {{ column }} | ||
| {%- endif -%} | ||
| {%- if not loop.last -%}{{ ", " }}{%- endif -%} | ||
| {%- endfor -%} | ||
| {% endset %} | ||
| {{ return(select_statements) }} | ||
| {% endmacro %} | ||
|
|
||
|
|
||
| {# ---------------------------------------------------------------------- #} | ||
| {# BigQuery STRUCT nested-field helpers. #} | ||
| {# ---------------------------------------------------------------------- #} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think all the utilities here should not be in this file, but in dedicated files |
||
|
|
||
| {# Segment-quote a (possibly dotted) identifier for BigQuery. | ||
| Returns `<seg1>`.`<seg2>`.`<seg3>` for dotted paths, `<name>` otherwise. | ||
| For non-BigQuery adapters, returns the name unchanged (preserves existing | ||
| behaviour at all callsites). #} | ||
| {% macro bq_segment_quote(name) %} | ||
| {%- if target.type == 'bigquery' -%} | ||
| {%- if '.' in name -%} | ||
| {%- set parts = [] -%} | ||
| {%- for seg in name.split('.') -%} | ||
| {%- do parts.append('`' ~ seg ~ '`') -%} | ||
| {%- endfor -%} | ||
| {{ parts | join('.') }} | ||
| {%- else -%} | ||
| `{{ name }}` | ||
| {%- endif -%} | ||
| {%- else -%} | ||
| {{ name }} | ||
| {%- endif -%} | ||
| {% endmacro %} | ||
|
|
||
| {# Convert a (possibly dotted) identifier into a dot-free alias safe to use | ||
| as a SQL identifier. No-op for names without dots. #} | ||
| {% macro bq_safe_alias(name) %} | ||
| {{- name | replace('.', '__') -}} | ||
| {% endmacro %} | ||
|
|
||
| {# Wrap a Column / BigQueryColumn with a dict carrying both the SQL identifier | ||
| representation (.quoted, segment-quoted for nested) and a CTE-projection-safe | ||
| alias (.safe_alias, dot-free). For non-nested columns and non-BigQuery | ||
| adapters the wrapper mirrors the original Column's values, so downstream | ||
| consumers (which use only attribute / subscript access on column_obj) see | ||
| no behavioural difference. #} | ||
| {% macro wrap_column_for_struct_support(column_obj) %} | ||
| {%- set name = column_obj.name -%} | ||
| {%- if target.type == 'bigquery' and '.' in name -%} | ||
| {%- set quoted_segments = [] -%} | ||
| {%- for seg in name.split('.') -%} | ||
| {%- do quoted_segments.append('`' ~ seg ~ '`') -%} | ||
| {%- endfor -%} | ||
| {%- set quoted = quoted_segments | join('.') -%} | ||
| {%- set safe_alias = name | replace('.', '__') -%} | ||
| {%- else -%} | ||
| {%- set quoted = column_obj.quoted -%} | ||
| {%- set safe_alias = column_obj.column -%} | ||
| {%- endif -%} | ||
| {{ return({ | ||
| 'name': name, | ||
| 'column': column_obj.column, | ||
| 'quoted': quoted, | ||
| 'safe_alias': safe_alias, | ||
| 'dtype': column_obj.dtype, | ||
| 'data_type': column_obj.data_type, | ||
| 'fields': column_obj.fields, | ||
| }) }} | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| {% endmacro %} | ||
Uh oh!
There was an error while loading. Please reload this page.