Skip to content

Commit 8938597

Browse files
author
Patryk Jatczak
committed
expected texts in top values sensor updated - postgresql, redshift
1 parent 55ccd5a commit 8938597

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

home/sensors/column/accepted_values/expected_texts_in_top_values_count/postgresql.sql.jinja2

+18-16
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ FROM
1818
{% if lib.time_series is not none -%}
1919
top_col_values.time_period as time_period,
2020
top_col_values.time_period_utc as time_period_utc,
21-
{%- endif -%}
21+
{% endif -%}
2222
RANK() OVER({{- render_data_grouping('top_col_values', indentation = ' ', partition_by_enabled=true) }}
23-
ORDER BY top_col_values.total_values DESC) as top_values_rank {{- render_data_grouping('top_col_values', indentation = ' ') }}
23+
ORDER BY top_col_values.total_values DESC) as top_values_rank {{- render_data_grouping('top_col_values', indentation = ' ') }}
2424
FROM
2525
(
2626
SELECT
@@ -31,30 +31,32 @@ FROM
3131
FROM
3232
{{ lib.render_target_table() }} AS analyzed_table
3333
{{- lib.render_where_clause(extra_filter = lib.render_target_column('analyzed_table') ~ ' IS NOT NULL', indentation = ' ') }}
34-
GROUP BY {{ render_grouping_columns() }} top_value
35-
ORDER BY {{ render_grouping_columns() }} total_values DESC
34+
GROUP BY {{ render_grouping_columns() -}} top_value
35+
ORDER BY {{ render_grouping_columns() -}} total_values DESC
3636
) AS top_col_values
3737
) AS top_values
3838
WHERE top_values_rank <= {{ parameters.top }}
3939
{%- endmacro -%}
4040

4141
{% macro render_grouping_columns() %}
42-
{%- if (lib.data_groupings is not none and (lib.data_groupings | length()) > 0) or lib.time_series is not none %}
43-
{{ lib.render_grouping_column_names() }} {{ ',' }}
42+
{%- if (lib.data_groupings is not none and (lib.data_groupings | length()) > 0) or lib.time_series is not none -%}
43+
{{ lib.render_grouping_column_names() }} {{- ', ' -}}
4444
{%- endif -%}
4545
{% endmacro %}
4646

4747
{%- macro render_data_grouping(table_alias_prefix = '', indentation = '', partition_by_enabled = false) -%}
4848

49-
{%- if lib.time_series is not none and partition_by_enabled == true -%}PARTITION BY top_col_values.time_period{%- endif -%}
49+
{%- if partition_by_enabled == true -%}PARTITION BY
50+
{%- if lib.time_series is not none -%}
51+
{{" "}}top_col_values.time_period
52+
{%- elif lib.data_groupings is none -%}
53+
{{" "}}NULL
54+
{%- endif -%}
55+
{%- endif -%}
5056

5157
{%- if lib.data_groupings is not none and (lib.data_groupings | length()) > 0 -%}
5258
{%- for attribute in lib.data_groupings -%}
53-
{% if lib.time_series is none and loop.index == 1 and partition_by_enabled == true -%}
54-
PARTITION BY
55-
{%- else -%}
56-
{{ ',' }}
57-
{%- endif -%}
59+
{{- "" if loop.first and lib.time_series is none and partition_by_enabled else "," -}}
5860
{%- with data_grouping_level = lib.data_groupings[attribute] -%}
5961
{%- if data_grouping_level.source == 'tag' -%}
6062
{{ indentation }}{{ lib.make_text_constant(data_grouping_level.tag) }}
@@ -73,17 +75,17 @@ SELECT
7375
{{- lib.render_data_grouping_projections('analyzed_table') }}
7476
{{- lib.render_time_dimension_projection('analyzed_table') }}
7577
FROM {{ lib.render_target_table() }} AS analyzed_table
76-
{%- else %}
78+
{%- else %}
7779
COUNT(DISTINCT
7880
CASE
7981
WHEN top_values.top_value IN ({{ extract_in_list(parameters.expected_values) }}) THEN top_values.top_value
8082
ELSE NULL
8183
END
8284
) AS actual_value,
8385
MAX({{ parameters.expected_values | length }}) AS expected_value
84-
{% if lib.time_series is not none -%}
85-
, top_values.time_period
86-
, top_values.time_period_utc
86+
{%- if lib.time_series is not none -%} {{- "," }}
87+
top_values.time_period,
88+
top_values.time_period_utc
8789
{%- endif -%}
8890
{{- render_data_grouping('top_values', indentation = lib.eol() ~ ' ') }}
8991
{{ render_from_subquery() }}

home/sensors/column/accepted_values/expected_texts_in_top_values_count/redshift.sql.jinja2

+18-16
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ FROM
1818
{% if lib.time_series is not none -%}
1919
top_col_values.time_period as time_period,
2020
top_col_values.time_period_utc as time_period_utc,
21-
{%- endif -%}
21+
{% endif -%}
2222
RANK() OVER({{- render_data_grouping('top_col_values', indentation = ' ', partition_by_enabled=true) }}
23-
ORDER BY top_col_values.total_values DESC) as top_values_rank {{- render_data_grouping('top_col_values', indentation = ' ') }}
23+
ORDER BY top_col_values.total_values DESC) as top_values_rank {{- render_data_grouping('top_col_values', indentation = ' ') }}
2424
FROM
2525
(
2626
SELECT
@@ -31,30 +31,32 @@ FROM
3131
FROM
3232
{{ lib.render_target_table() }} AS analyzed_table
3333
{{- lib.render_where_clause(extra_filter = lib.render_target_column('analyzed_table') ~ ' IS NOT NULL', indentation = ' ') }}
34-
GROUP BY {{ render_grouping_columns() }} top_value
35-
ORDER BY {{ render_grouping_columns() }} total_values DESC
34+
GROUP BY {{ render_grouping_columns() -}} top_value
35+
ORDER BY {{ render_grouping_columns() -}} total_values DESC
3636
) AS top_col_values
3737
) AS top_values
3838
WHERE top_values_rank <= {{ parameters.top }}
3939
{%- endmacro -%}
4040

4141
{% macro render_grouping_columns() %}
42-
{%- if (lib.data_groupings is not none and (lib.data_groupings | length()) > 0) or lib.time_series is not none %}
43-
{{ lib.render_grouping_column_names() }} {{ ',' }}
42+
{%- if (lib.data_groupings is not none and (lib.data_groupings | length()) > 0) or lib.time_series is not none -%}
43+
{{ lib.render_grouping_column_names() }} {{- ', ' -}}
4444
{%- endif -%}
4545
{% endmacro %}
4646

4747
{%- macro render_data_grouping(table_alias_prefix = '', indentation = '', partition_by_enabled = false) -%}
4848

49-
{%- if lib.time_series is not none and partition_by_enabled == true -%}PARTITION BY top_col_values.time_period{%- endif -%}
49+
{%- if partition_by_enabled == true -%}PARTITION BY
50+
{%- if lib.time_series is not none -%}
51+
{{" "}}top_col_values.time_period
52+
{%- elif lib.data_groupings is none -%}
53+
{{" "}}NULL
54+
{%- endif -%}
55+
{%- endif -%}
5056

5157
{%- if lib.data_groupings is not none and (lib.data_groupings | length()) > 0 -%}
5258
{%- for attribute in lib.data_groupings -%}
53-
{% if lib.time_series is none and loop.index == 1 and partition_by_enabled == true -%}
54-
PARTITION BY
55-
{%- else -%}
56-
{{ ',' }}
57-
{%- endif -%}
59+
{{- "" if loop.first and lib.time_series is none and partition_by_enabled else "," -}}
5860
{%- with data_grouping_level = lib.data_groupings[attribute] -%}
5961
{%- if data_grouping_level.source == 'tag' -%}
6062
{{ indentation }}{{ lib.make_text_constant(data_grouping_level.tag) }}
@@ -73,17 +75,17 @@ SELECT
7375
{{- lib.render_data_grouping_projections('analyzed_table') }}
7476
{{- lib.render_time_dimension_projection('analyzed_table') }}
7577
FROM {{ lib.render_target_table() }} AS analyzed_table
76-
{%- else %}
78+
{%- else %}
7779
COUNT(DISTINCT
7880
CASE
7981
WHEN top_values.top_value IN ({{ extract_in_list(parameters.expected_values) }}) THEN top_values.top_value
8082
ELSE NULL
8183
END
8284
) AS actual_value,
8385
MAX({{ parameters.expected_values | length }}) AS expected_value
84-
{% if lib.time_series is not none -%}
85-
, top_values.time_period
86-
, top_values.time_period_utc
86+
{%- if lib.time_series is not none -%} {{- "," }}
87+
top_values.time_period,
88+
top_values.time_period_utc
8789
{%- endif -%}
8890
{{- render_data_grouping('top_values', indentation = lib.eol() ~ ' ') }}
8991
{{ render_from_subquery() }}

0 commit comments

Comments
 (0)