Skip to content

Add support for filter expression in GroupConcat#948

Open
caramdache wants to merge 9 commits into
adamchainz:mainfrom
caramdache:patch-1
Open

Add support for filter expression in GroupConcat#948
caramdache wants to merge 9 commits into
adamchainz:mainfrom
caramdache:patch-1

Conversation

@caramdache

@caramdache caramdache commented Sep 14, 2022

Copy link
Copy Markdown

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

@adamchainz adamchainz left a comment

Copy link
Copy Markdown
Owner

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.

Comment thread src/django_mysql/models/aggregates.py Outdated
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Owner

Still waiting on docs, changelog note, and tests.

@jvacek

jvacek commented Aug 21, 2024

Copy link
Copy Markdown

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

@adamchainz

Copy link
Copy Markdown
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