-
-
Notifications
You must be signed in to change notification settings - Fork 88
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
fix(query_row_optional): do not treat rows with NULL as missing rows #6014
Conversation
dd1e9ed
to
56fd0d9
Compare
Primary reason for this was a bug where NULL in
While trying to make a test for this one discovered another bug: #6021 |
a5778e5
to
aab29a1
Compare
aab29a1
to
e26c419
Compare
f81b531
to
637a3f6
Compare
e26c419
to
23e8df6
Compare
9a3e779
to
5af3c26
Compare
5af3c26
to
5708bba
Compare
I added one test for ephemeral timer so this can be considered a fix, but otherwise this is mostly a refactoring to prevent more bugs similar to the one fixed in #6023. |
@@ -558,15 +558,13 @@ impl Sql { | |||
self.call(move |conn| match conn.query_row(sql.as_ref(), params, f) { | |||
Ok(res) => Ok(Some(res)), | |||
Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None), | |||
Err(rusqlite::Error::InvalidColumnType(_, _, rusqlite::types::Type::Null)) => Ok(None), |
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 is the main part of the PR.
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.
LGTM, I have 2 non-blocking thoughts
"SELECT MAX(timestamp) FROM msgs WHERE chat_id=? AND state!=?", | ||
"SELECT MAX(timestamp) | ||
FROM msgs | ||
WHERE chat_id=? AND state!=? | ||
HAVING COUNT(*) > 0", |
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.
Just FTR: It would also be possible to expect an Option<i64>
in the Rust code, and then handle it within Rust instead of SQL:
@@ -1402,7 +1402,7 @@ pub(crate) async fn calc_sort_timestamp(
) -> Result<i64> {
let mut sort_timestamp = cmp::min(message_timestamp, smeared_time(context));
- let last_msg_time: Option<i64> = if always_sort_to_bottom {
+ let last_msg_time: Option<Option<i64>> = if always_sort_to_bottom {
// get newest message for this chat
// Let hidden messages also be ordered with protection messages because hidden messages
@@ -1413,8 +1413,7 @@ pub(crate) async fn calc_sort_timestamp(
.query_get_value(
"SELECT MAX(timestamp)
FROM msgs
- WHERE chat_id=? AND state!=?
- HAVING COUNT(*) > 0",
+ WHERE chat_id=? AND state!=?",
(self, MessageState::OutDraft),
)
.await?
@@ -1432,8 +1431,7 @@ pub(crate) async fn calc_sort_timestamp(
.query_get_value(
"SELECT MAX(timestamp)
FROM msgs
- WHERE chat_id=? AND hidden=0 AND state>?
- HAVING COUNT(*) > 0",
+ WHERE chat_id=? AND hidden=0 AND state>?",
(self, MessageState::InFresh),
)
.await?
@@ -1441,7 +1439,7 @@ pub(crate) async fn calc_sort_timestamp(
None
};
- if let Some(last_msg_time) = last_msg_time {
+ if let Some(Some(last_msg_time)) = last_msg_time {
if last_msg_time > sort_timestamp {
sort_timestamp = last_msg_time;
}
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.
Option::flatten()
can also be used to make the code more brief
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 just kept HAVING
here, devs have to understand interaction between MIN
, MAX
and HAVING
anyway and it is then easier to read than trying to think about Result<Option<Option<...
.
Instead of treating NULL type error as absence of the row, handle NULL values with SQL. Previously we sometimes accidentally treated a single column being NULL as the lack of the whole row.
6ac053a
to
e8c239b
Compare
Instead of treating NULL type error
as absence of the row,
handle NULL values with SQL.
Previously we sometimes
accidentally treated a single column
being NULL as the lack of the whole row.
Fixes #5994