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

filter not supported for listagg #19869

Closed
danielyahn opened this issue Nov 23, 2023 · 0 comments · Fixed by #19871
Closed

filter not supported for listagg #19869

danielyahn opened this issue Nov 23, 2023 · 0 comments · Fixed by #19871
Labels
enhancement New feature or request

Comments

@danielyahn
Copy link
Member

This is an example for the listagg function without filter. It works as expected:

SELECT
    id,
    listagg(value, ',') WITHIN GROUP (ORDER BY value) csv_value
FROM (VALUES
   ROW('a', 1, 'filter-me'),
   ROW('c', 1, 'not-me'),
   ROW('b', 2, 'not-me')
    ) t(value, id, attr)
group by id

However, I tried placing filter in couple places with listagg, but Trino return SQL grammar errors.

-- mismatched input 'FILTER'. Expecting: 'WITHIN'
SELECT id,
       listagg(value, ',')
           FILTER (WHERE attr <> 'filter-me')
           WITHIN GROUP
           (ORDER BY value)
           csv_value
FROM (VALUES
    ROW('a', 1, 'filter-me'),
    ROW('c', 1, 'not-me'),
    ROW('b', 2, 'not-me')
    ) t(value, id, attr)
group by id;

-- mismatched input 'FILTER'. Expecting: '('
SELECT id,
       listagg(value, ',')
           WITHIN GROUP
           FILTER (WHERE attr <> 'filter-me')
           (ORDER BY value)
           csv_value
FROM (VALUES
    ROW('a', 1, 'filter-me'),
    ROW('c', 1, 'not-me'),
    ROW('b', 2, 'not-me')
    ) t(value, id, attr)
group by id;

-- mismatched input 'FILTER'. Expecting: '%', '(', ')', '*', '+', ',', '-', '->', '.', '/', 'AND', 'ASC', 'AT', 'DESC', 'NULLS', 'OR', 'OVER', '[', '||', <predicate>, <string>
SELECT id,
       listagg(value, ',')
           WITHIN GROUP
           (ORDER BY value FILTER (WHERE attr <> 'filter-me'))
           csv_value
FROM (VALUES
    ROW('a', 1, 'filter-me'),
    ROW('c', 1, 'not-me'),
    ROW('b', 2, 'not-me')
    ) t(value, id, attr)
group by id;

-- mismatched input '('. Expecting: ',', 'EXCEPT', 'FETCH', 'FROM', 'GROUP', 'HAVING', 'INTERSECT', 'LIMIT', 'OFFSET', 'ORDER', 'UNION', 'WHERE', 'WINDOW', <EOF>
SELECT id,
       listagg(value, ',')
           WITHIN GROUP
           (ORDER BY value)
           FILTER (WHERE attr <> 'filter-me')
           csv_value
FROM (VALUES
    ROW('a', 1, 'filter-me'),
    ROW('c', 1, 'not-me'),
    ROW('b', 2, 'not-me')
    ) t(value, id, attr)
group by id;

This brings back the expected result, but this is a workaround using array_join, array_sort, and array_agg.

SELECT
    id,
    array_join(array_sort(array_agg(value) FILTER (WHERE attr <> 'filter-me')), ',')
FROM (VALUES
          ROW('a', 1, 'filter-me'),
          ROW('c', 1, 'not-me'),
          ROW('b', 2, 'not-me')
     ) t(value, id, attr)
group by id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging a pull request may close this issue.

2 participants