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

tidb_query: fix converting bytes to bool (#7486) #7547

Merged
merged 6 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion src/coprocessor/codec/data_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl AsMySQLBool for Bytes {
#[inline]
fn as_mysql_bool(&self, context: &mut EvalContext) -> Result<bool> {
Ok(!self.is_empty()
&& crate::coprocessor::codec::convert::bytes_to_int(context, self)? != 0)
&& crate::coprocessor::codec::convert::bytes_to_f64(context, self)? != 0f64)
}
}

Expand Down Expand Up @@ -114,3 +114,64 @@ impl_evaluable_type! { Bytes }
impl_evaluable_type! { DateTime }
impl_evaluable_type! { Duration }
impl_evaluable_type! { Json }

#[cfg(test)]
mod tests {
use super::*;
use std::f64;

#[test]
fn test_bytes_to_bool() {
let tests: Vec<(&'static [u8], Option<bool>)> = vec![
(b"", Some(false)),
(b" 23", Some(true)),
(b"-1", Some(true)),
(b"1.11", Some(true)),
(b"1.11.00", None),
(b"xx", None),
(b"0x00", None),
(b"11.xx", None),
(b"xx.11", None),
(
b".0000000000000000000000000000000000000000000000000000001",
Some(true),
),
];

let mut ctx = EvalContext::default();
for (i, (v, expect)) in tests.into_iter().enumerate() {
let rb: Result<bool> = v.to_vec().as_mysql_bool(&mut ctx);
match expect {
Some(val) => {
assert_eq!(rb.unwrap(), val);
}
None => {
assert!(
rb.is_err(),
"index: {}, {:?} should not be converted, but got: {:?}",
i,
v,
rb
);
}
}
}

// test overflow
let mut ctx = EvalContext::default();
let val: Result<bool> = f64::INFINITY
.to_string()
.as_bytes()
.to_vec()
.as_mysql_bool(&mut ctx);
assert!(val.is_err());

let mut ctx = EvalContext::default();
let val: Result<bool> = f64::NEG_INFINITY
.to_string()
.as_bytes()
.to_vec()
.as_mysql_bool(&mut ctx);
assert!(val.is_err());
}
}
4 changes: 2 additions & 2 deletions src/coprocessor/codec/datum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ impl Datum {
Datum::I64(i) => Some(i != 0),
Datum::U64(u) => Some(u != 0),
Datum::F64(f) => Some(f.round() != 0f64),
Datum::Bytes(ref bs) => Some(!bs.is_empty() && convert::bytes_to_int(ctx, bs)? != 0),
Datum::Bytes(ref bs) => Some(!bs.is_empty() && convert::bytes_to_f64(ctx, bs)? != 0f64),
Datum::Time(t) => Some(!t.is_zero()),
Datum::Dur(d) => Some(!d.is_zero()),
Datum::Dec(d) => Some(d.as_f64()?.round() != 0f64),
Expand Down Expand Up @@ -1704,7 +1704,7 @@ mod tests {
(Datum::F64(-0.4), Some(false)),
(Datum::Null, None),
(b"".as_ref().into(), Some(false)),
(b"0.5".as_ref().into(), Some(false)),
(b"0.5".as_ref().into(), Some(true)),
(b"0".as_ref().into(), Some(false)),
(b"2".as_ref().into(), Some(true)),
(b"abc".as_ref().into(), Some(false)),
Expand Down