Skip to content
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

Merged
merged 12 commits into from
Jan 8, 2022
Merged

Conversation

qyihua
Copy link
Contributor

@qyihua qyihua commented Dec 20, 2021

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

Copy link
Member

@billy1624 billy1624 left a 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/types.rs Outdated Show resolved Hide resolved
Comment on lines 640 to 644
match order_expr.nulls_last {
Some(true) => write!(sql, " NULLS LAST").unwrap(),
Some(false) => write!(sql, " NULLS FISRT").unwrap(),
_ => (),
}
Copy link
Member

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.

Comment on lines 150 to 157
/// 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,
Copy link
Member

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?

Comment on lines 169 to 176
/// 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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

@qyihua
Copy link
Contributor Author

qyihua commented Dec 30, 2021

@billy1624 Thank you for the good suggestions.
I need convert the pr to draft and go on

@qyihua qyihua marked this pull request as draft December 30, 2021 01:26
@qyihua qyihua marked this pull request as ready for review December 31, 2021 09:03
@qyihua
Copy link
Contributor Author

qyihua commented Dec 31, 2021

Hi @billy1624 ,
I make some change, it's ready for review now. Thank you!

@billy1624
Copy link
Member

Btw... just found something interesting. We could actually implement NULLS FIRST and NULLS LAST for MySQL and MariaDB.

SELECT ... FROM ... ORDER BY reuestId IS NULL DESC, reuestId DESC

Adapted from https://stackoverflow.com/a/9307657/7059723

@qyihua
Copy link
Contributor Author

qyihua commented Dec 31, 2021

Btw... just found something interesting. We could actually implement NULLS FIRST and NULLS LAST for MySQL and MariaDB.

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

@billy1624
Copy link
Member

Thanks!!

@qyihua
Copy link
Contributor Author

qyihua commented Dec 31, 2021

Btw... just found something interesting. We could actually implement NULLS FIRST and NULLS LAST for MySQL and MariaDB.

SELECT ... FROM ... ORDER BY reuestId IS NULL DESC, reuestId DESC

Adapted from https://stackoverflow.com/a/9307657/7059723

Nulls order support for Mysql backend is ok now :)
I test in http://sqlfiddle.com/#!9/286c76/4

@billy1624
Copy link
Member

I have added some more test cases on billy1624@e43245b
You can cherrypick it to this PR

@qyihua
Copy link
Contributor Author

qyihua commented Dec 31, 2021

I have added some more test cases on billy1624@e43245b You can cherrypick it to this PR

I have cherrypick it to this PR, and all test is passed.
Thanks for your help!

@billy1624 billy1624 requested a review from tyt2y3 December 31, 2021 10:57
Copy link
Member

@tyt2y3 tyt2y3 left a 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 {
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 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 ?
  2. I also think NullOrdering is better than Nulls

@qyihua
Copy link
Contributor Author

qyihua commented Jan 3, 2022

Sorry for the delay but there is now several conflicts. Can help resolving?

Yeah, but something is wrong in src/driver/sqlx_postgres.rs

} else if value.is_array() {
    query.bind(value.as_ref_array())

@billy1624
Copy link
Member

Let me check

@billy1624
Copy link
Member

Should be fixed

@qyihua
Copy link
Contributor Author

qyihua commented Jan 5, 2022

Should be fixed

Yes! It's fixed now. Thanks :)

@tyt2y3 tyt2y3 linked an issue Jan 8, 2022 that may be closed by this pull request
@tyt2y3 tyt2y3 merged commit 9264434 into SeaQL:master Jan 8, 2022
@tyt2y3
Copy link
Member

tyt2y3 commented Jan 8, 2022

Thank you for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Support NULLS LAST and NULLS FIRST
3 participants