-
-
Notifications
You must be signed in to change notification settings - Fork 114
Add support for filter expression in GroupConcat #948
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
537d035
452d3c8
24c0e35
97ea273
65b301c
b3e4a2c
451597b
96e7c9a
8c5c5e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ class GroupConcat(Aggregate): | |
def __init__( | ||
self, | ||
expression: Expression, | ||
filter=None, | ||
distinct: bool = False, | ||
separator: str | None = None, | ||
ordering: str | None = None, | ||
|
@@ -38,7 +39,7 @@ def __init__( | |
# This can/will be improved to SetTextField or ListTextField | ||
extra["output_field"] = CharField() | ||
|
||
super().__init__(expression, **extra) | ||
super().__init__(expression, filter=filter, **extra) | ||
|
||
self.distinct = distinct | ||
self.separator = separator | ||
|
@@ -53,6 +54,15 @@ def as_sql( | |
connection: BaseDatabaseWrapper, | ||
**extra_context: Any, | ||
) -> tuple[str, tuple[Any, ...]]: | ||
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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 If you look at the code for |
||
|
||
connection.ops.check_expression_support(self) | ||
sql = ["GROUP_CONCAT("] | ||
if self.distinct: | ||
|
Uh oh!
There was an error while loading. Please reload this page.