Skip to content

Commit

Permalink
add constant path tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ygf11 committed Sep 26, 2021
1 parent 78f0048 commit 5107963
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 49 deletions.
1 change: 1 addition & 0 deletions common/functions/src/scalars/function_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ lazy_static! {
ConditionalFunction::register(map.clone()).unwrap();
DateFunction::register(map.clone()).unwrap();
OtherFunction::register(map.clone()).unwrap();

map
};
}
Expand Down
30 changes: 15 additions & 15 deletions common/functions/src/scalars/others/running_difference_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,29 @@ impl Function for RunningDifferenceFunction {
}

macro_rules! run_difference_compute {
($method:ident, $result_type:ident, $target_type:ty, $func: ident) => {
fn $func(column: &DataColumn, input_rows: usize) -> Result<DataColumn> {
($method_name:ident, $to_df_array:ident, $result_logic_type:ident, $result_primitive_type:ty) => {
fn $method_name(column: &DataColumn, input_rows: usize) -> Result<DataColumn> {
if let DataColumn::Constant(_, _) = column {
Ok(DataColumn::Constant(
DataValue::$result_type(Some(0i8 as $target_type)),
DataValue::$result_logic_type(Some(0_i8 as $result_primitive_type)),
input_rows,
))
} else {
let series = column.to_array()?;
let array = series.$method()?.inner();
let array = series.$to_df_array()?.inner();

let mut result_vec = Vec::with_capacity(array.len());
for index in 0..array.len() {
match array.is_null(index) {
true => result_vec.push(None),
false => {
if index == 0 {
result_vec.push(Some(0i8 as $target_type))
result_vec.push(Some(0_i8 as $result_primitive_type))
} else if array.is_null(index - 1) {
result_vec.push(None)
} else {
let diff = array.value(index) as $target_type
- array.value(index - 1) as $target_type;
let diff = array.value(index) as $result_primitive_type
- array.value(index - 1) as $result_primitive_type;
result_vec.push(Some(diff))
}
}
Expand All @@ -128,14 +128,14 @@ macro_rules! run_difference_compute {
};
}

run_difference_compute!(i8, Int16, i16, compute_i8);
run_difference_compute!(u8, Int16, i16, compute_u8);
run_difference_compute!(i16, Int32, i32, compute_i16);
run_difference_compute!(u16, Int32, i32, compute_u16);
run_difference_compute!(i32, Int64, i64, compute_i32);
run_difference_compute!(u32, Int64, i64, compute_u32);
run_difference_compute!(i64, Int64, i64, compute_i64);
run_difference_compute!(u64, Int64, i64, compute_u64);
run_difference_compute!(compute_i8, i8, Int16, i16);
run_difference_compute!(compute_u8, u8, Int16, i16);
run_difference_compute!(compute_i16, i16, Int32, i32);
run_difference_compute!(compute_u16, u16, Int32, i32);
run_difference_compute!(compute_i32, i32, Int64, i64);
run_difference_compute!(compute_u32, u32, Int64, i64);
run_difference_compute!(compute_i64, i64, Int64, i64);
run_difference_compute!(compute_u64, u64, Int64, i64);

impl fmt::Display for RunningDifferenceFunction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
163 changes: 136 additions & 27 deletions common/functions/src/scalars/others/running_difference_function_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,123 @@ use common_datablocks::*;
use common_datavalues::prelude::*;
use common_exception::Result;

use crate::scalars::*;
use crate::scalars::RunningDifferenceFunction;

macro_rules! run_difference_constant_test {
($method_name:ident, $primitive_type:ty, $logic_type:ident, $result_primitive_type:ty, $result_logic_type:ident, $array_type:ident) => {
#[test]
fn $method_name() -> Result<()> {
let schema =
DataSchemaRefExt::create(vec![DataField::new("a", DataType::$logic_type, false)]);
let block = DataBlock::create(schema.clone(), vec![DataColumn::Constant(
DataValue::$logic_type(Some(0_i8 as $primitive_type)),
5,
)]);

// Ok.
{
let run_difference_function = RunningDifferenceFunction::try_create("a")?;
let columns = vec![DataColumnWithField::new(
block.try_column_by_name("a")?.clone(),
schema.field_with_name("a")?.clone(),
)];

// eval
let result = run_difference_function.eval(&columns, block.num_rows())?;
let actual_ref = result.get_array_ref().unwrap();
let actual = actual_ref.as_any().downcast_ref::<$array_type>().unwrap();
let expected = $array_type::from_slice([0i8 as $result_primitive_type; 5]);

assert_eq!(&expected, actual);

// result type
let args_type_array = [DataType::$logic_type; 1];
let result_type = run_difference_function.return_type(&args_type_array[..])?;
assert_eq!(result_type, DataType::$result_logic_type);
}

Ok(())
}
};
}

run_difference_constant_test!(
test_running_difference_constant_i8,
i8,
Int8,
i16,
Int16,
Int16Array
);
run_difference_constant_test!(
test_running_difference_constant_u8,
u8,
UInt8,
i16,
Int16,
Int16Array
);
run_difference_constant_test!(
test_running_difference_constant_i16,
i16,
Int16,
i32,
Int32,
Int32Array
);
run_difference_constant_test!(
test_running_difference_constant_u16,
u16,
UInt16,
i32,
Int32,
Int32Array
);
run_difference_constant_test!(
test_running_difference_constant_i32,
i32,
Int32,
i64,
Int64,
Int64Array
);
run_difference_constant_test!(
test_running_difference_constant_u32,
u32,
UInt32,
i64,
Int64,
Int64Array
);
run_difference_constant_test!(
test_running_difference_constant_i64,
i64,
Int64,
i64,
Int64,
Int64Array
);
run_difference_constant_test!(
test_running_difference_constant_u64,
u64,
UInt64,
i64,
Int64,
Int64Array
);

macro_rules! run_difference_first_not_null_test {
($method:ident, $primitive_type:ty, $data_type:ident, $result_type:ty, $result_primitive_type:ident, $array_type:ident) => {
($method_name:ident, $primitive_type:ty, $logic_type:ident, $result_primitive_type:ty, $result_logic_type:ident, $array_type:ident) => {
#[test]
fn $method() -> Result<()> {
fn $method_name() -> Result<()> {
let schema =
DataSchemaRefExt::create(vec![DataField::new("a", DataType::$data_type, true)]);
DataSchemaRefExt::create(vec![DataField::new("a", DataType::$logic_type, true)]);
let block = DataBlock::create_by_array(schema.clone(), vec![Series::new(vec![
Some(2_i8 as $primitive_type),
Some(3_i8 as $primitive_type),
Some(3),
None,
Some(4_i8 as $primitive_type),
Some(10_i8 as $primitive_type),
Some(4),
Some(10),
])]);

// Ok.
Expand All @@ -48,19 +151,20 @@ macro_rules! run_difference_first_not_null_test {
let actual_ref = result.get_array_ref().unwrap();
let actual = actual_ref.as_any().downcast_ref::<$array_type>().unwrap();
let expected = $array_type::from([
Some(0),
Some(1_i8 as $result_type),
Some(0_i8 as $result_primitive_type),
Some(1),
None,
None,
Some(6_i8 as $result_type),
Some(6),
]);

assert_eq!(&expected, actual);

// result type
let args_type_array = [DataType::$data_type; 1];
let args_type_array = [DataType::$logic_type; 1];
let result_type = run_difference_function.return_type(&args_type_array[..])?;
assert_eq!(result_type, DataType::$result_primitive_type);

assert_eq!(result_type, DataType::$result_logic_type);
}

Ok(())
Expand Down Expand Up @@ -150,17 +254,17 @@ run_difference_first_not_null_test!(
);

macro_rules! run_difference_first_null_test {
($method:ident, $primitive_type:ty, $data_type:ident, $result_type:ty, $result_primitive_type:ident, $array_type:ident) => {
($method_name:ident, $primitive_type:ty, $logic_type:ident, $result_primitive_type:ty, $result_logic_type:ident, $array_type:ident) => {
#[test]
fn $method() -> Result<()> {
fn $method_name() -> Result<()> {
let schema =
DataSchemaRefExt::create(vec![DataField::new("a", DataType::$data_type, true)]);
DataSchemaRefExt::create(vec![DataField::new("a", DataType::$logic_type, true)]);
let block = DataBlock::create_by_array(schema.clone(), vec![Series::new(vec![
None,
Some(1_i8 as $primitive_type),
None,
Some(3_i8 as $primitive_type),
Some(7_i8 as $primitive_type),
Some(3),
Some(7),
])]);

// Ok.
Expand All @@ -175,15 +279,20 @@ macro_rules! run_difference_first_null_test {
let result = run_difference_function.eval(&columns, block.num_rows())?;
let actual_ref = result.get_array_ref().unwrap();
let actual = actual_ref.as_any().downcast_ref::<$array_type>().unwrap();
let expected =
$array_type::from([None, None, None, None, Some(4_i8 as $result_type)]);
let expected = $array_type::from([
None,
None,
None,
None,
Some(4_i8 as $result_primitive_type),
]);

assert_eq!(&expected, actual);

// result type
let args_type_array = [DataType::$data_type; 1];
let args_type_array = [DataType::$logic_type; 1];
let result_type = run_difference_function.return_type(&args_type_array[..])?;
assert_eq!(result_type, DataType::$result_primitive_type);
assert_eq!(result_type, DataType::$result_logic_type);
}

Ok(())
Expand Down Expand Up @@ -287,10 +396,10 @@ fn test_running_difference_datetime32_first_not_null() -> Result<()> {
DataSchemaRefExt::create(vec![DataField::new("a", DataType::DateTime32(None), true)]);
let block = DataBlock::create_by_array(schema.clone(), vec![Series::new(vec![
Some(2_u32),
Some(3_u32),
Some(3),
None,
Some(4_u32),
Some(10_u32),
Some(4),
Some(10),
])]);

// Ok.
Expand Down Expand Up @@ -323,10 +432,10 @@ fn test_running_difference_datetime32_first_null() -> Result<()> {
DataSchemaRefExt::create(vec![DataField::new("a", DataType::DateTime32(None), true)]);
let block = DataBlock::create_by_array(schema.clone(), vec![Series::new(vec![
Some(2_u32),
Some(3_u32),
Some(3),
None,
Some(4_u32),
Some(10_u32),
Some(4),
Some(10),
])]);

// Ok.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
1 0 1 0 1 0
3 2 3 2 3 2
5 2 5 2 5 2
10 5 10 5 10 5
1 0 1 0 1 0 0
3 2 3 2 3 2 0
5 2 5 2 5 2 0
10 5 10 5 10 5 0
ERROR 1105 (HY000) at line 7: Code: 7, displayText = Argument for function runningDifference must have numeric type.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
drop table if exists runing_difference_test;

create table runing_difference_test (a Int8, b Int32, c Int64) engine=Memory;
insert into runing_difference_test values (1, 1, 1),(3, 3, 3),(5, 5, 5),(10, 10, 10);
create table runing_difference_test (a Int8, b Int32, c Int64, d varchar) engine=Memory;
insert into runing_difference_test values (1, 1, 1, 'a'),(3, 3, 3, 'b'),(5, 5, 5, 'c'),(10, 10, 10, 'd');

select a, runningDifference(a), b, runningDifference(b), c, runningDifference(c) from runing_difference_test;
select a, runningDifference(a), b, runningDifference(b), c, runningDifference(c), runningDifference(10) from runing_difference_test;
select d, runningDifference(d) from runing_difference_test;

DROP TABLE IF EXISTS runing_difference_test;

0 comments on commit 5107963

Please sign in to comment.