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

Fix(optimizer): column qualify not applying in an order by inside within group when it also the alias #4780

Merged
merged 2 commits into from
Feb 23, 2025

Conversation

ran-lakeway
Copy link
Contributor

example test code

query = """
    SELECT group_name, GROUP_CONCAT(name, ', ') WITHIN GROUP (ORDER BY sub_group_name NULLS LAST) AS sub_group_name
    FROM department 
    GROUP BY group_name
"""

parsed_query = parse_one(query)
qualified_query = qualify(parsed_query, schema={"department": {"group_name": "string", "sub_group_name": "string", "name": "string"}})

print(qualified_query.sql(pretty=True))

current output

SELECT
  "department"."group_name" AS "group_name",
  GROUP_CONCAT("department"."name", ', ') WITHIN GROUP (ORDER BY
    "sub_group_name" NULLS LAST) AS "sub_group_name"
FROM "department" AS "department"
GROUP BY
  "department"."group_name"

after fix

SELECT
  "department"."group_name" AS "group_name",
  GROUP_CONCAT("department"."name", ', ') WITHIN GROUP (ORDER BY
    "department"."sub_group_name" NULLS LAST) AS "sub_group_name"
FROM "department" AS "department"
GROUP BY
  "department"."group_name"

…hin group when it also the alias

for example "GROUP_CONCAT(name, ', ') WITHIN GROUP (ORDER BY sub_group_name NULLS LAST) AS sub_group_name"
@georgesittas
Copy link
Collaborator

Hi @ran-lakeway, can you please add test(s) for this? Thanks for the contribution!

Copy link
Collaborator

@georgesittas georgesittas left a comment

Choose a reason for hiding this comment

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

I can add a test for this.

@georgesittas georgesittas merged commit 674d884 into tobymao:main Feb 23, 2025
7 checks passed
georgesittas added a commit that referenced this pull request Feb 23, 2025
…thin group when it also the alias (#4780)

* Fix(optimizer): column qualify not applying in an order by inside within group when it also the alias

for example "GROUP_CONCAT(name, ', ') WITHIN GROUP (ORDER BY sub_group_name NULLS LAST) AS sub_group_name"

* Update sqlglot/optimizer/scope.py

---------

Co-authored-by: Jo <46752250+georgesittas@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants