File: 214_get_unique_attributes.sql

package info (click to toggle)
sqlfmt 0.29.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,580 kB
  • sloc: python: 10,007; sql: 5,626; makefile: 39
file content (54 lines) | stat: -rw-r--r-- 2,027 bytes parent folder | download
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{# Source: https://github.com/tconbeer/sqlfmt/issues/326 #}
{% macro get_unique_attributes(source_table, node_col) %}

{% set attribute_query %} 
    select
        distinct x.key as attributes
    from {{ source_table }} x
    where startswith(x.key, '@')  -- attributes get parsed as keys that start with '@'
        and length(x.key) > 1 -- but keys of just '@' designate the node itself
        and regexp_count(x.path, '\\[') > 1 -- we don't need the attributes from the xml root node
        and not startswith(x.key, '@xmlns') -- we don't need attributed data about xml namespaces
{% endset %}

{% set results = run_query(attribute_query) %}

{% if execute %}
    {% set results_list = results.columns[0].values() %}
{% else %}
    {% set results_list = [] %}
{% endif %}

{% for attribute in results_list %}
    , get({{ node_col }}, '{{ attribute }}')::varchar(256) as attribute_{{ dbt_utils.slugify(attribute) | replace("@", "") }}
{% endfor %}

{% endmacro %}
)))))__SQLFMT_OUTPUT__(((((
{# Source: https://github.com/tconbeer/sqlfmt/issues/326 #}
{% macro get_unique_attributes(source_table, node_col) %}

    {% set attribute_query %}
    select
        distinct x.key as attributes
    from {{ source_table }} x
    where startswith(x.key, '@')  -- attributes get parsed as keys that start with '@'
        and length(x.key) > 1 -- but keys of just '@' designate the node itself
        and regexp_count(x.path, '\\[') > 1 -- we don't need the attributes from the xml root node
        and not startswith(x.key, '@xmlns') -- we don't need attributed data about xml namespaces
    {% endset %}

    {% set results = run_query(attribute_query) %}

    {% if execute %} {% set results_list = results.columns[0].values() %}
    {% else %} {% set results_list = [] %}
    {% endif %}

    {% for attribute in results_list %}
        ,
        get({{ node_col }}, '{{ attribute }}')::varchar(
            256
        ) as attribute_{{ dbt_utils.slugify(attribute) | replace("@", "") }}
    {% endfor %}

{% endmacro %}