Skip to content
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

feat: implement listagg macro #290

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions dbt/include/clickhouse/macros/utils/utils.sql
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,15 @@
{% endmacro %}


{% macro clickhouse__listagg(measure, delimiter_text, order_by_clause, limit_num) %}
{{ exceptions.raise_compiler_error(
'ClickHouse does not support the listagg function. See the groupArray function instead')
}}
{% endmacro %}
{% macro clickhouse__listagg(measure, delimiter_text, order_by_clause, limit_num) -%}
{% set arr = "array_agg({})".format(measure) %}
{% set arr = "arraySort({}, {})".format(arr, order_by_clause) if order_by_clause else arr %}
BentsiLeviav marked this conversation as resolved.
Show resolved Hide resolved
{% if limit_num -%}
arrayStringConcat('{{delimiter_text}}', arraySlice({{ arr }}, 1, {{ limit_num }}))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on the docs it seems that you need to pass the column name/array first, and only then the delimiter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, thanks a lot!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on docs singlequote also not needed. fixed that too

{% else -%}
arrayStringConcat('{{delimiter_text}}', {{ arr }})
{%- endif %}
{%- endmacro %}


{% macro clickhouse__array_construct(inputs, data_type) -%}
Expand Down
8 changes: 2 additions & 6 deletions tests/integration/adapter/utils/test_listagg.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from dbt.exceptions import CompilationError
from dbt.tests.adapter.utils.fixture_listagg import (
models__test_listagg_yml,
seeds__data_listagg_csv,
Expand Down Expand Up @@ -29,8 +28,5 @@ def models(self):
"test_listagg.sql": models__test_listagg_sql,
}

def test_listagg_exception(self, project):
try:
run_dbt(["build"], False)
except CompilationError as e:
assert 'does not support' in e.msg
def test_listagg_run(self, project):
run_dbt(["build"], False)