-
-
Notifications
You must be signed in to change notification settings - Fork 195
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
Support 'NULLS LAST' and 'NULLS FIRST' #210
Conversation
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.
Hey @qyihua, thanks for the contribution!
I'm thinking is there any better name for these order_by_xxx_nulls_last
methods?
src/backend/query_builder.rs
Outdated
match order_expr.nulls_last { | ||
Some(true) => write!(sql, " NULLS LAST").unwrap(), | ||
Some(false) => write!(sql, " NULLS FISRT").unwrap(), | ||
_ => (), | ||
} |
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.
This should be placed inside src/backend/postgres/query.rs
. So, by default all other backend won't write order by nulls when preparing the select statement.
src/query/ordered.rs
Outdated
/// Order by custom string with nulls order option. | ||
fn order_by_customs_nulls_last<T>( | ||
&mut self, | ||
cols: Vec<(T, Order)>, | ||
nulls_last: bool, | ||
) -> &mut Self | ||
where | ||
T: ToString, |
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.
Shouldn't this nills_last
be in the cols
tuple?
src/query/ordered.rs
Outdated
/// Order by vector of columns with nulls order option. | ||
fn order_by_columns_nulls_last<T>( | ||
&mut self, | ||
cols: Vec<(T, Order)>, | ||
nulls_last: bool, | ||
) -> &mut Self | ||
where | ||
T: IntoColumnRef, |
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.
Same here
@billy1624 Thank you for the good suggestions. |
Hi @billy1624 , |
Btw... just found something interesting. We could actually implement SELECT ... FROM ... ORDER BY reuestId IS NULL DESC, reuestId DESC Adapted from https://stackoverflow.com/a/9307657/7059723 |
It's great! Let me have a try |
Thanks!! |
Nulls order support for Mysql backend is ok now :) |
I have added some more test cases on billy1624@e43245b |
I have cherrypick it to this PR, and all test is passed. |
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.
Sorry for the delay but there is now several conflicts. Can help resolving?
src/types.rs
Outdated
@@ -148,11 +148,19 @@ pub enum JoinType { | |||
RightJoin, | |||
} | |||
|
|||
/// Nulls order | |||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | |||
pub enum Nulls { |
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 think we don't need to pub this ? We can keep this as pub(crate)
?
If not, we need to think of a better name than Nulls
.
I am thinking may be NullOrdering
or something
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.
- If this keep as pub(crate), how do you think users should use these methods order_by_xxxs_with_nulls?
I think it need pass bool as nulls order argument ? - I also think
NullOrdering
is better thanNulls
Yeah, but something is wrong in } else if value.is_array() {
query.bind(value.as_ref_array()) |
Let me check |
Should be fixed |
Yes! It's fixed now. Thanks :) |
Thank you for your contribution! |
Hi,
In PostgreSQL, by default, null values sort as if larger than any non-null value
ref: https://www.postgresql.org/docs/current/queries-order.html
SQLite considers NULL values to be smaller than any other values for sorting purposes
ref: https://sqlite.org/lang_select.html
Mysql seems doesn't support the syntax to sort nulls last in.
#209