-
-
Notifications
You must be signed in to change notification settings - Fork 129
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
feat(pg): support ordering with NULLS FIRST / NULLS LAST #332
feat(pg): support ordering with NULLS FIRST / NULLS LAST #332
Conversation
This allows plugins to solve #331 |
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.
You're on fire, @mattbretl! This looks good to me; we need a test for it before I can merge it though.
? sql.fragment` NULLS FIRST` | ||
: nullsFirst === false | ||
? sql.fragment` NULLS LAST` | ||
: null |
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 had to actually read the source to make sure null
was okay here! It is ✅
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.
Yeah, I find that really handy.. don't change it! 😄
Given how the tests are structured, I can add a new integration test in postgraphile-core/__tests__/integration/schema, appending a small plugin to expose additional orderBy enums that use |
Or.. I could add an |
I think the latter makes sense and requires minimal effort. I don't really care so long as it is tested and can be show to work both ways. Basically what you're saying is "treat nulls as if they are smaller than everything" - i.e. they're first when ascending and last when descending? Can you flag that this behaviour will become the default in v5? I always want it so that when you reverse the order (ASC -> DESC) the rows return in the exact opposite order (which is not the case currently, if there are nulls). Thanks Matt! |
I wonder if there's a performance impact of adding the NULLS FIRST/LAST clause... |
Actually, no. This is about using NULLS LAST for both ASC and DESC. As a simple example, consider a If 5 is high-priority and 1 is low-priority:
If 1 is high-priority and 5 is low-priority:
It works that way currently. Records are returned in the exact opposite order, with nulls treated as greater than all non-nulls. So there should be no need to change the default in v5. |
Hmmm. Add --order-nulls-last flag then I guess. |
I wasn't sure if you wanted to add yet-another global config option, so I left it at the graphileBuildOptions level for now. Let me know what you think. |
From the PG docs..
So unless you explicitly create a NULLS LAST index, using DESC NULLS LAST could have a performance impact. |
Ohhhh. Thanks for sharing. Reviewing now 👍 |
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 going to merge this. A small follow-up PR that adds | null
to the relevant places would be welcome but TBH I'm happy to cross this bridge when we get to it.
@@ -25,7 +25,7 @@ export default class QueryBuilder { | |||
public where(exprGen: SQLGen): void; | |||
public whereBound(exprGen: SQLGen, isLower: boolean): void; | |||
public setOrderIsUnique(): void; | |||
public orderBy(exprGen: SQLGen, ascending: boolean): void; | |||
public orderBy(exprGen: SQLGen, ascending: boolean, nullsFirst: boolean): void; |
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.
Technically this should be nullsFirst?: boolean
because it doesn't have to be specified
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.
You know what... Lets have it required.
@@ -236,9 +236,9 @@ class QueryBuilder { | |||
setOrderIsUnique() { | |||
this.data.orderIsUnique = true; | |||
} | |||
orderBy(exprGen: SQLGen, ascending: boolean = true) { | |||
orderBy(exprGen: SQLGen, ascending: boolean = true, nullsFirst: boolean) { |
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.
Technically this should also be nullsFirst?: boolean
because it doesn't have to be specified. Huh, TypeScript and Flow use the same syntax for this 👍
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.
Actually it should be nullsFirst: boolean | null = null
I think.
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.
You know what... Lets have it required. It should still be | null
technically...
No description provided.