Skip to content

Commit

Permalink
Merge pull request #290 from igorvoltaic/listagg
Browse files Browse the repository at this point in the history
feat: implement listagg macro
  • Loading branch information
BentsiLeviav authored Jun 25, 2024
2 parents 3a81f09 + 08a3860 commit 03d7a18
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
18 changes: 13 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,19 @@
{% 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) -%}
{% if order_by_clause -%}
{% set arr = "arrayMap(x -> x.1, arraySort(x -> x.2, arrayZip(array_agg({}), array_agg({}))))".format(arr, order_by_clause) %}
{% else -%}
{% set arr = "array_agg({})".format(measure) %}
{%- endif %}

{% if limit_num -%}
arrayStringConcat(arraySlice({{ arr }}, 1, {{ limit_num }}), {{delimiter_text}})
{% else -%}
arrayStringConcat({{ arr }}, {{delimiter_text}})
{%- 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)

0 comments on commit 03d7a18

Please sign in to comment.