-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
Used JSON_OBJECT database function on PostgreSQL 16+. #17664
Conversation
class ArgJoiner: | ||
def join(self, args): | ||
args = [" VALUE ".join(arg) for arg in zip(args[::2], args[1::2])] | ||
return ", ".join(args) | ||
pairs = zip(args[::2], args[1::2], strict=True) |
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.
Adding the strict flag here avoids silently dropping the last argument if an odd number of arguments are provided to JSONObject()
.
@ngnpope Thanks 👍 |
args = [" VALUE ".join(arg) for arg in zip(args[::2], args[1::2])] | ||
return ", ".join(args) | ||
pairs = zip(args[::2], args[1::2], strict=True) | ||
return ", ".join([" VALUE ".join(pair) for pair in pairs]) |
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 late to the party, but something that I usually recommend is to avoid creating a whole list in memory before calling join
, since it handles generators very nicely and it uses less memory:
return ", ".join([" VALUE ".join(pair) for pair in pairs]) | |
return ", ".join(" VALUE ".join(pair) for pair in pairs) |
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.
A list comprehension is preferable here as str.join()
converts to list internally anyway. It is better performance to provide a list up front.
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.
Thank you! Now that you mention it, I recall this was already discussed in another Django PR. I need to re-learn this! 📚
Updates
JSONObject()
to use the standard SQL functionJSON_OBJECT
on PostgreSQL 16+.This makes Oracle and PostgreSQL 16+ share the same implementation.