Skip to content

Commit

Permalink
merge pingcap/release-3.1 until (tikv#7762)
Browse files Browse the repository at this point in the history
txn: only wake up waiters when locks are indeed released (tikv#7379) (tikv#7585)

Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>

txn: don't protect rollback for BatchRollback (tikv#7605) (tikv#7608)

Signed-off-by: youjiali1995 <zlwgx1023@gmail.com>

tidb_query: add is true/false keep null ScalarFuncSig (tikv#7532) (tikv#7566)

Signed-off-by: zhongzc <zhongzc_arch@outlook.com>

tidb_query: fix the logical behavior of floats (tikv#7342) (tikv#7582)

Signed-off-by: zhongzc <zhongzc_arch@outlook.com>

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

Signed-off-by: zhongzc <zhongzc_arch@outlook.com>

raftstore: change the condition of proposing rollback merge (tikv#6584) (tikv#7762)

Signed-off-by: Liqi Geng <gengliqiii@gmail.com>
Signed-off-by: Tong Zhigao <tongzhigao@pingcap.com>
  • Loading branch information
solotzg committed May 14, 2020
1 parent 47a0d07 commit a1192bd
Show file tree
Hide file tree
Showing 21 changed files with 837 additions and 242 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions benches/misc/storage/key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.

use rand::{self, Rng, RngCore};
use tikv::coprocessor::codec::{datum, table, Datum};
use tikv::storage::Key;

#[inline]
fn gen_rand_str(len: usize) -> Vec<u8> {
let mut rand_str = vec![0; len];
rand::thread_rng().fill_bytes(&mut rand_str);
rand_str
}

#[bench]
fn bench_row_key_gen_hash(b: &mut test::Bencher) {
let id: i64 = rand::thread_rng().gen();
let row_key = Key::from_raw(&table::encode_row_key(id, id));
b.iter(|| {
test::black_box(row_key.gen_hash());
});
}

#[bench]
fn bench_index_key_gen_hash(b: &mut test::Bencher) {
let id: i64 = rand::thread_rng().gen();
let encoded_index_val = datum::encode_key(&[Datum::Bytes(gen_rand_str(64))]).unwrap();
let index_key = Key::from_raw(&table::encode_index_seek_key(id, id, &encoded_index_val));
b.iter(|| {
test::black_box(index_key.gen_hash());
});
}
1 change: 1 addition & 0 deletions benches/misc/storage/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0.

mod key;
mod scan;
8 changes: 6 additions & 2 deletions components/test_raftstore/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,11 +793,15 @@ impl<T: Simulator> Cluster<T> {
}

pub fn truncated_state(&self, region_id: u64, store_id: u64) -> RaftTruncatedState {
self.apply_state(region_id, store_id).take_truncated_state()
}

pub fn apply_state(&self, region_id: u64, store_id: u64) -> RaftApplyState {
let key = keys::apply_state_key(region_id);
self.get_engine(store_id)
.get_msg_cf::<RaftApplyState>(engine::CF_RAFT, &keys::apply_state_key(region_id))
.get_msg_cf::<RaftApplyState>(engine::CF_RAFT, &key)
.unwrap()
.unwrap()
.take_truncated_state()
}

pub fn transfer_leader(&mut self, region_id: u64, leader: metapb::Peer) {
Expand Down
93 changes: 91 additions & 2 deletions src/coprocessor/codec/data_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ 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)
}
}

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,92 @@ 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_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,),
}
}
}
}
14 changes: 7 additions & 7 deletions src/coprocessor/codec/datum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ impl Datum {
let b = match self {
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::F64(f) => Some(f != 0f64),
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),
Datum::Dec(d) => Some(!d.is_zero()),
Datum::Null => None,
_ => return Err(invalid_type!("can't convert {:?} to bool", self)),
};
Expand Down Expand Up @@ -1698,13 +1698,13 @@ mod tests {
(Datum::U64(0), Some(false)),
(Datum::U64(1), Some(true)),
(Datum::F64(0f64), Some(false)),
(Datum::F64(0.4), Some(false)),
(Datum::F64(0.4), Some(true)),
(Datum::F64(0.5), Some(true)),
(Datum::F64(-0.5), Some(true)),
(Datum::F64(-0.4), Some(false)),
(Datum::F64(-0.4), Some(true)),
(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 All @@ -1720,7 +1720,7 @@ mod tests {
),
(
Datum::Dec(Decimal::from_f64(0.1415926).unwrap()),
Some(false),
Some(true),
),
(Datum::Dec(0u64.into()), Some(false)),
];
Expand Down
3 changes: 1 addition & 2 deletions src/coprocessor/codec/mysql/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1790,8 +1790,7 @@ impl Display for Decimal {
impl crate::coprocessor::codec::data_type::AsMySQLBool for Decimal {
#[inline]
fn as_mysql_bool(&self, _context: &mut EvalContext) -> crate::coprocessor::Result<bool> {
// Note: as_f64() may be never fail?
Ok(self.as_f64()?.round() != 0f64)
Ok(!self.is_zero())
}
}

Expand Down
Loading

0 comments on commit a1192bd

Please sign in to comment.