-
Notifications
You must be signed in to change notification settings - Fork 8
/
get_columns_in_relation_by_column_prefix.sql
34 lines (27 loc) · 1.32 KB
/
get_columns_in_relation_by_column_prefix.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{#
Copyright (c) 2021-present Snowplow Analytics Ltd. All rights reserved.
This program is licensed to you under the Snowplow Personal and Academic License Version 1.0,
and you may not use this file except in compliance with the Snowplow Personal and Academic License Version 1.0.
You may obtain a copy of the Snowplow Personal and Academic License Version 1.0 at https://docs.snowplow.io/personal-and-academic-license-1.0/
#}
{% macro get_columns_in_relation_by_column_prefix(relation, column_prefix) %}
{# Prevent introspective queries during parsing #}
{%- if not execute -%}
{{ return('') }}
{% endif %}
{%- set columns = adapter.get_columns_in_relation(relation) -%}
{# get_columns_in_relation returns uppercase cols for snowflake so uppercase column_prefix #}
{%- set column_prefix = column_prefix.upper() if target.type == 'snowflake' else column_prefix -%}
{%- set matched_columns = [] -%}
{# add columns with matching prefix to matched_columns #}
{% for column in columns %}
{% if column.name.startswith(column_prefix) %}
{% do matched_columns.append(column) %}
{% endif %}
{% endfor %}
{% if matched_columns|length %}
{{ return(matched_columns) }}
{% else %}
{{ exceptions.raise_compiler_error("Snowplow: No columns found with prefix "~column_prefix) }}
{% endif %}
{% endmacro %}