Skip to content

Commit

Permalink
cherry pick tikv#7342 to release-4.0
Browse files Browse the repository at this point in the history
Signed-off-by: sre-bot <sre-bot@pingcap.com>
  • Loading branch information
zhongzc authored and sre-bot committed Apr 21, 2020
1 parent 82f37e5 commit b668678
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
94 changes: 93 additions & 1 deletion components/tidb_query/src/codec/data_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl AsMySQLBool for Int {
impl AsMySQLBool for Real {
#[inline]
fn as_mysql_bool(&self, _context: &mut EvalContext) -> Result<bool> {
Ok(self.round() != 0f64)
Ok(self.into_inner() != 0f64)
}
}

Expand Down Expand Up @@ -125,3 +125,95 @@ impl_evaluable_type! { Bytes }
impl_evaluable_type! { DateTime }
impl_evaluable_type! { Duration }
impl_evaluable_type! { Json }
<<<<<<< HEAD:components/tidb_query/src/codec/data_type/mod.rs
=======

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

#[test]
fn test_bytes_as_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());
}

#[test]
fn test_real_as_bool() {
let tests: Vec<(f64, Option<bool>)> = vec![
(0.0, Some(false)),
(1.3, Some(true)),
(-1.234, Some(true)),
(0.000000000000000000000000000000001, Some(true)),
(-0.00000000000000000000000000000001, Some(true)),
(f64::MAX, Some(true)),
(f64::MIN, Some(true)),
(f64::MIN_POSITIVE, Some(true)),
(f64::INFINITY, Some(true)),
(f64::NEG_INFINITY, Some(true)),
(f64::NAN, None),
];

let mut ctx = EvalContext::default();
for (f, expected) in tests {
match Real::new(f) {
Ok(b) => {
let r = b.as_mysql_bool(&mut ctx).unwrap();
assert_eq!(r, expected.unwrap());
}
Err(_) => assert!(expected.is_none(), "{} to bool should fail", f,),
}
}
}
}
>>>>>>> 6b60d57... tidb_query: fix the logical behavior of floats (#7342):components/tidb_query_datatype/src/codec/data_type/mod.rs
5 changes: 5 additions & 0 deletions components/tidb_query/src/codec/mysql/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1926,8 +1926,13 @@ impl Display for Decimal {

impl crate::codec::data_type::AsMySQLBool for Decimal {
#[inline]
<<<<<<< HEAD:components/tidb_query/src/codec/mysql/decimal.rs
fn as_mysql_bool(&self, ctx: &mut EvalContext) -> crate::Result<bool> {
Ok(ConvertTo::<f64>::convert(self, ctx)?.round() != 0f64)
=======
fn as_mysql_bool(&self, _ctx: &mut EvalContext) -> tidb_query_common::error::Result<bool> {
Ok(!self.is_zero())
>>>>>>> 6b60d57... tidb_query: fix the logical behavior of floats (#7342):components/tidb_query_datatype/src/codec/mysql/decimal.rs
}
}

Expand Down

0 comments on commit b668678

Please sign in to comment.