-
-
Notifications
You must be signed in to change notification settings - Fork 111
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
base: main
Are you sure you want to change the base?
Conversation
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
for more information, see https://pre-commit.ci
There was a problem hiding this 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.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Co-authored-by: Adam Johnson <[email protected]>
for more information, see https://pre-commit.ci
I missed this in the first commit
for more information, see https://pre-commit.ci
I've updated the history file. I'm not sure if this is the file you had in mind. |
THat's the file, but you've done so in a separate PR: #949. Please do so in this PR, on the same branch. |
due to the introduction of an auxiliary function.
for more information, see https://pre-commit.ci
Still waiting on docs, changelog note, and tests. |
@caramdache Any status updates? Wondering if I can help implement this somehow without having to start a whole new MR. |
I think it's unlikely they'll return, please start a new PR. You can pull their changes with |
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