diff --git a/dbt/include/clickhouse/macros/utils/utils.sql b/dbt/include/clickhouse/macros/utils/utils.sql index d2f7a614..f4c4c74c 100644 --- a/dbt/include/clickhouse/macros/utils/utils.sql +++ b/dbt/include/clickhouse/macros/utils/utils.sql @@ -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) -%} diff --git a/tests/integration/adapter/utils/test_listagg.py b/tests/integration/adapter/utils/test_listagg.py index eb60a177..aad9bc3e 100644 --- a/tests/integration/adapter/utils/test_listagg.py +++ b/tests/integration/adapter/utils/test_listagg.py @@ -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, @@ -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)