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 filter expression in GroupConcat #948

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

caramdache
Copy link

@caramdache caramdache commented Sep 14, 2022

Fixes #947.

GroupConcat did not support fitler expression. This PR adds support based on Django Count aggregate.

https://github.com/adamchainz/django-mysql/blob/main/src/django_mysql/models/aggregates.py

Copy link
Owner

@adamchainz adamchainz left a comment

Choose a reason for hiding this comment

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

Nice work starting on this.

The tests are failing because the package requires test coverage - please add tests in the existing tests for group concat. It looks like we'll need at least two, depending on the value of distinct.

Also please update the documentation the changelog.

src/django_mysql/models/aggregates.py Outdated Show resolved Hide resolved
Comment on lines 57 to 64
if self.filter:
extra_context["distinct"] = "DISTINCT " if self.distinct else ""
copy = self.copy()
copy.filter = None
source_expressions = copy.get_source_expressions()
condition = When(self.filter, then=source_expressions[0])
copy.set_source_expressions([Case(condition)] + source_expressions[1:])
return super(Aggregate, copy).as_sql(compiler, connection, **extra_context)
Copy link
Owner

Choose a reason for hiding this comment

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

I haven't looked into it, but it's a bit odd that you're skipping the existing implementation and calling super(). The implemetnation below covers the SQL oddities in GROUP_CONCAT, I think we'd still want to use it, just tweak adding the filter there.

Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure we need keep the existing pathway in the long term. It's probably better to let Aggregate.as_sql() perform the work based on the template.

If you look at the code for Count, you'll see there are 2 paths, one when filter is set, the other when it's not. I think we should have a similar structure here, but avoid manual handcrafting as much as possible because this is likely fragile.

@caramdache
Copy link
Author

Also please update the documentation the changelog.

I've updated the history file. I'm not sure if this is the file you had in mind.

@adamchainz
Copy link
Owner

THat's the file, but you've done so in a separate PR: #949. Please do so in this PR, on the same branch.

caramdache and others added 2 commits September 15, 2022 09:30
due to the introduction of an auxiliary function.
@adamchainz
Copy link
Owner

Still waiting on docs, changelog note, and tests.

@jvacek
Copy link

jvacek commented Aug 21, 2024

@caramdache Any status updates? Wondering if I can help implement this somehow without having to start a whole new MR.

@adamchainz
Copy link
Owner

I think it's unlikely they'll return, please start a new PR. You can pull their changes with gh pr checkout 948 then create a new branch and squash rebase on top of the latest main before adding your own updates.

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.

GroupConcat missing filter parameter
3 participants