Skip to content

Commit

Permalink
Simplify some code using field type converts. (#4649)
Browse files Browse the repository at this point in the history
Signed-off-by: Breezewish <breezewish@pingcap.com>
  • Loading branch information
breezewish committed May 8, 2019
1 parent 0609d44 commit 73c5495
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 207 deletions.
21 changes: 7 additions & 14 deletions src/coprocessor/codec/batch/lazy_column.rs
Expand Up @@ -287,9 +287,6 @@ mod tests {
fn test_lazy_batch_column_clone() {
use cop_datatype::FieldTypeTp;

let mut ft = FieldType::new();
ft.as_mut_accessor().set_tp(FieldTypeTp::Long);

let mut col = LazyBatchColumn::raw_with_capacity(5);
assert!(col.is_raw());
assert_eq!(col.len(), 0);
Expand All @@ -306,7 +303,7 @@ mod tests {
{
// Empty raw to empty decoded.
let mut col = col.clone();
col.decode(&Tz::utc(), &ft).unwrap();
col.decode(&Tz::utc(), &FieldTypeTp::Long.into()).unwrap();
assert!(col.is_decoded());
assert_eq!(col.len(), 0);
assert_eq!(col.capacity(), 5);
Expand Down Expand Up @@ -346,7 +343,7 @@ mod tests {
assert_eq!(col.raw()[1].as_slice(), datum_raw_2.as_slice());
}
// Non-empty raw to non-empty decoded.
col.decode(&Tz::utc(), &ft).unwrap();
col.decode(&Tz::utc(), &FieldTypeTp::Long.into()).unwrap();
assert!(col.is_decoded());
assert_eq!(col.len(), 2);
assert_eq!(col.capacity(), 5);
Expand Down Expand Up @@ -471,11 +468,9 @@ mod benches {
column.push_raw(datum_raw.as_slice());
}

let mut ft = tipb::expression::FieldType::new();
ft.as_mut_accessor().set_tp(FieldTypeTp::LongLong);
let tz = Tz::utc();

column.decode(&tz, &ft).unwrap();
column
.decode(&Tz::utc(), &FieldTypeTp::LongLong.into())
.unwrap();

b.iter(|| {
test::black_box(test::black_box(&column).clone());
Expand All @@ -499,8 +494,7 @@ mod benches {
column.push_raw(datum_raw.as_slice());
}

let mut ft = tipb::expression::FieldType::new();
ft.as_mut_accessor().set_tp(FieldTypeTp::LongLong);
let ft = FieldTypeTp::LongLong.into();
let tz = Tz::utc();

b.iter(|| {
Expand Down Expand Up @@ -528,8 +522,7 @@ mod benches {
column.push_raw(datum_raw.as_slice());
}

let mut ft = tipb::expression::FieldType::new();
ft.as_mut_accessor().set_tp(FieldTypeTp::LongLong);
let ft = FieldTypeTp::LongLong.into();
let tz = Tz::utc();

column.decode(&tz, &ft).unwrap();
Expand Down
40 changes: 5 additions & 35 deletions src/coprocessor/codec/batch/lazy_column_vec.rs
Expand Up @@ -215,7 +215,7 @@ impl std::ops::DerefMut for LazyBatchColumnVec {
mod tests {
use super::*;

use cop_datatype::{EvalType, FieldTypeAccessor};
use cop_datatype::EvalType;

use crate::coprocessor::codec::datum::{Datum, DatumEncoder};

Expand Down Expand Up @@ -268,26 +268,10 @@ mod tests {

for comparable in &[true, false] {
let schema = [
{
// Column 1: Long
let mut ft = FieldType::new();
ft.as_mut_accessor().set_tp(FieldTypeTp::Long);
ft
},
{
// Column 2: Double
let mut ft = FieldType::new();
ft.as_mut_accessor().set_tp(FieldTypeTp::Double);
ft
},
{
// Column 3: VarChar
let mut ft = FieldType::new();
ft.as_mut_accessor().set_tp(FieldTypeTp::VarChar);
ft
},
FieldTypeTp::Long.into(),
FieldTypeTp::Double.into(),
FieldTypeTp::VarChar.into(),
];

let values = vec![
vec![Datum::U64(1), Datum::F64(1.0), Datum::Null],
vec![Datum::Null, Datum::Null, Datum::Bytes(vec![0u8, 2u8])],
Expand Down Expand Up @@ -355,21 +339,7 @@ mod tests {
fn test_retain_rows_by_index() {
use cop_datatype::FieldTypeTp;

let schema = [
{
// Column 1: Long
let mut ft = FieldType::new();
ft.as_mut_accessor().set_tp(FieldTypeTp::Long);
ft
},
{
// Column 2: Double
let mut ft = FieldType::new();
ft.as_mut_accessor().set_tp(FieldTypeTp::Double);
ft
},
];

let schema = [FieldTypeTp::Long.into(), FieldTypeTp::Double.into()];
let mut columns = LazyBatchColumnVec::with_raw_columns(2);
assert_eq!(columns.rows_len(), 0);
assert_eq!(columns.columns_len(), 2);
Expand Down
3 changes: 1 addition & 2 deletions src/coprocessor/codec/data_type/vector.rs
Expand Up @@ -957,8 +957,7 @@ mod benches {
let mut datum_raw: Vec<u8> = Vec::new();
DatumEncoder::encode(&mut datum_raw, &[Datum::U64(0xDEADBEEF)], true).unwrap();

let mut field_type = tipb::expression::FieldType::new();
field_type.as_mut_accessor().set_tp(FieldTypeTp::LongLong);
let field_type = FieldTypeTp::LongLong.into();
let tz = Tz::utc();

b.iter(move || {
Expand Down
19 changes: 3 additions & 16 deletions src/coprocessor/dag/batch/executors/index_scan_executor.rs
Expand Up @@ -247,7 +247,6 @@ mod tests {

use cop_datatype::{FieldTypeAccessor, FieldTypeTp};
use kvproto::coprocessor::KeyRange;
use tipb::expression::FieldType;
use tipb::schema::ColumnInfo;

use crate::coprocessor::codec::mysql::Tz;
Expand Down Expand Up @@ -293,21 +292,9 @@ mod tests {

// The schema of these columns. Used to check executor output.
let schema = vec![
{
let mut ft = FieldType::new();
ft.as_mut_accessor().set_tp(FieldTypeTp::LongLong);
ft
},
{
let mut ft = FieldType::new();
ft.as_mut_accessor().set_tp(FieldTypeTp::Double);
ft
},
{
let mut ft = FieldType::new();
ft.as_mut_accessor().set_tp(FieldTypeTp::LongLong);
ft
},
FieldTypeTp::LongLong.into(),
FieldTypeTp::Double.into(),
FieldTypeTp::LongLong.into(),
];

// Case 1. Normal index.
Expand Down
Expand Up @@ -66,7 +66,7 @@ mod tests {
use crate::coprocessor::codec::data_type::VectorValue;
use crate::coprocessor::dag::exec_summary::*;
use crate::coprocessor::dag::expr::EvalConfig;
use cop_datatype::{EvalType, FieldTypeAccessor, FieldTypeTp};
use cop_datatype::{EvalType, FieldTypeTp};
use tipb::expression::FieldType;

struct MockExecutor {
Expand All @@ -77,12 +77,6 @@ mod tests {
cfg: EvalConfig,
}

fn field_type(ft: FieldTypeTp) -> FieldType {
let mut f = FieldType::new();
f.as_mut_accessor().set_tp(ft);
f
}

impl MockExecutor {
/// create the MockExecutor with fixed schema and data.
fn new() -> MockExecutor {
Expand All @@ -93,11 +87,10 @@ mod tests {
(5, None, Some(0.1)),
(6, None, Some(4.5)),
];

let field_types = vec![
field_type(FieldTypeTp::LongLong),
field_type(FieldTypeTp::LongLong),
field_type(FieldTypeTp::Double),
FieldTypeTp::LongLong.into(),
FieldTypeTp::LongLong.into(),
FieldTypeTp::Double.into(),
];

MockExecutor {
Expand Down
4 changes: 2 additions & 2 deletions src/coprocessor/dag/batch/executors/mod.rs
@@ -1,12 +1,12 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

mod index_scan_executor;
mod limit;
mod limit_executor;
mod selection_executor;
mod table_scan_executor;
mod util;

pub use self::index_scan_executor::BatchIndexScanExecutor;
pub use self::limit::BatchLimitExecutor;
pub use self::limit_executor::BatchLimitExecutor;
pub use self::selection_executor::BatchSelectionExecutor;
pub use self::table_scan_executor::BatchTableScanExecutor;
45 changes: 7 additions & 38 deletions src/coprocessor/dag/batch/executors/table_scan_executor.rs
Expand Up @@ -326,12 +326,6 @@ mod tests {
use crate::coprocessor::util::convert_to_prefix_next;
use crate::storage::{FixtureStore, Key};

fn field_type(ft: FieldTypeTp) -> FieldType {
let mut f = FieldType::new();
f.as_mut_accessor().set_tp(ft);
f
}

/// Test Helper for normal test with fixed schema and data.
/// Table Schema: ID (INT, PK), Foo (INT), Bar (FLOAT, Default 4.5)
/// Column id: 1, 2, 4
Expand Down Expand Up @@ -429,9 +423,9 @@ mod tests {
];

let field_types = vec![
field_type(FieldTypeTp::LongLong),
field_type(FieldTypeTp::LongLong),
field_type(FieldTypeTp::Double),
FieldTypeTp::LongLong.into(),
FieldTypeTp::LongLong.into(),
FieldTypeTp::Double.into(),
];

let store = {
Expand Down Expand Up @@ -725,23 +719,10 @@ mod tests {
ci
},
];

let schema = vec![
{
let mut ft = FieldType::new();
ft.as_mut_accessor().set_tp(FieldTypeTp::LongLong);
ft
},
{
let mut ft = FieldType::new();
ft.as_mut_accessor().set_tp(FieldTypeTp::LongLong);
ft
},
{
let mut ft = FieldType::new();
ft.as_mut_accessor().set_tp(FieldTypeTp::LongLong);
ft
},
FieldTypeTp::LongLong.into(),
FieldTypeTp::LongLong.into(),
FieldTypeTp::LongLong.into(),
];

let mut kv = vec![];
Expand Down Expand Up @@ -843,19 +824,7 @@ mod tests {
ci
},
];

let schema = vec![
{
let mut ft = FieldType::new();
ft.as_mut_accessor().set_tp(FieldTypeTp::LongLong);
ft
},
{
let mut ft = FieldType::new();
ft.as_mut_accessor().set_tp(FieldTypeTp::LongLong);
ft
},
];
let schema = vec![FieldTypeTp::LongLong.into(), FieldTypeTp::LongLong.into()];

let mut kv = vec![];
{
Expand Down

0 comments on commit 73c5495

Please sign in to comment.