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

add support for the within group keyword #205

Closed
tconbeer opened this issue Jul 14, 2022 · 0 comments · Fixed by #228
Closed

add support for the within group keyword #205

tconbeer opened this issue Jul 14, 2022 · 0 comments · Fixed by #228
Labels
bug Something isn't working good first issue Good for newcomers
Milestone

Comments

@tconbeer
Copy link
Owner

Describe the bug
add support for the within group keyword, which seems analagous to over, e.g.,

array_agg(distinct my_field) within group (order by another_field)

This is necessary for proper formatting when this spills onto multiple lines, as below.

To Reproduce
from gitlab's project

, services_by_marketing_contact_id AS (

    SELECT
      marketing_contact_order.dim_marketing_contact_id                                           AS dim_marketing_contact_id,
      COUNT(*)                                                                                   AS pql_nbr_integrations_installed,
      ARRAY_AGG(DISTINCT services.service_type) WITHIN GROUP (ORDER BY services.service_type)    AS pql_integrations_installed
    FROM services
    LEFT JOIN project
      ON services.project_id = project.dim_project_id
    LEFT JOIN marketing_contact_order
      ON marketing_contact_order.dim_namespace_id = project.dim_namespace_id
    GROUP BY 1

), users_role_by_marketing_contact_id AS (

    SELECT
      marketing_contact_order.dim_marketing_contact_id,
      ARRAY_AGG(DISTINCT marketing_contact.job_title) WITHIN GROUP (ORDER BY marketing_contact.job_title) AS pql_namespace_creator_job_description
    FROM marketing_contact_order
    ...

Expected behavior

services_by_marketing_contact_id as (
    select
        marketing_contact_order.dim_marketing_contact_id as dim_marketing_contact_id,
        count(*) as pql_nbr_integrations_installed,
        array_agg(distinct services.service_type) within group (
            order by services.service_type
        ) as pql_integrations_installed
    from services
    left join project on services.project_id = project.dim_project_id
    left join
        marketing_contact_order
        on marketing_contact_order.dim_namespace_id = project.dim_namespace_id
    group by 1
),
users_role_by_marketing_contact_id as (

    select
        marketing_contact_order.dim_marketing_contact_id,
        array_agg(distinct marketing_contact.job_title) within group (
            order by marketing_contact.job_title
        ) as pql_namespace_creator_job_description
    from marketing_contact_order
    ...

Actual behavior

services_by_marketing_contact_id as (
    select
        marketing_contact_order.dim_marketing_contact_id as dim_marketing_contact_id,
        count(*) as pql_nbr_integrations_installed,
        array_agg(
            distinct services.service_type) within group(order by services.service_type
        ) as pql_integrations_installed
    from services
    left join project on services.project_id = project.dim_project_id
    left join
        marketing_contact_order
        on marketing_contact_order.dim_namespace_id = project.dim_namespace_id
    group by 1
),
users_role_by_marketing_contact_id as (

    select
        marketing_contact_order.dim_marketing_contact_id,
        array_agg(
            distinct marketing_contact.job_title
        ) within group(order by marketing_contact.job_title
        ) as pql_namespace_creator_job_description
    from marketing_contact_order
    ...

Additional context
What is the output of sqlfmt --version?

@tconbeer tconbeer added the bug Something isn't working label Jul 14, 2022
@tconbeer tconbeer added this to the v0.10.1 milestone Aug 2, 2022
@tconbeer tconbeer added the good first issue Good for newcomers label Aug 2, 2022
tconbeer added a commit that referenced this issue Aug 2, 2022
* fix #205: support within group and filter keywords

* chore: update primer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant