Skip to content

Commit

Permalink
support float and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ygf11 committed Sep 26, 2021
1 parent 5107963 commit 8812ab8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 18 deletions.
4 changes: 1 addition & 3 deletions Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl Function for RunningDifferenceFunction {
| DataType::UInt64
| DataType::Date32
| DataType::DateTime32(_) => Ok(DataType::Int64),
DataType::Float32 | DataType::Float64 => Ok(DataType::Float64),
_ => Result::Err(ErrorCode::IllegalDataType(
"Argument for function runningDifference must have numeric type",
)),
Expand All @@ -75,6 +76,8 @@ impl Function for RunningDifferenceFunction {
}
DataType::Int64 => compute_i64(columns[0].column(), input_rows),
DataType::UInt64 => compute_u64(columns[0].column(), input_rows),
DataType::Float32 => compute_f32(columns[0].column(), input_rows),
DataType::Float64 => compute_f64(columns[0].column(), input_rows),
_ => Result::Err(ErrorCode::IllegalDataType(
format!(
"Argument for function runningDifference must have numeric type.: While processing runningDifference({})",
Expand Down Expand Up @@ -136,6 +139,8 @@ 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);
run_difference_compute!(compute_f32, f32, Float64, f64);
run_difference_compute!(compute_f64, f64, Float64, f64);

impl fmt::Display for RunningDifferenceFunction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use common_arrow::arrow::array::Float64Array;
use common_arrow::arrow::array::Int16Array;
use common_arrow::arrow::array::Int32Array;
use common_arrow::arrow::array::Int64Array;
Expand All @@ -28,7 +29,7 @@ macro_rules! run_difference_constant_test {
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)),
DataValue::$logic_type(Some(10_i8 as $primitive_type)),
5,
)]);

Expand Down Expand Up @@ -123,6 +124,22 @@ run_difference_constant_test!(
Int64,
Int64Array
);
run_difference_constant_test!(
test_running_difference_constant_f32,
f32,
Float32,
f64,
Float64,
Float64Array
);
run_difference_constant_test!(
test_running_difference_constant_f64,
f64,
Float64,
f64,
Float64,
Float64Array
);

macro_rules! run_difference_first_not_null_test {
($method_name:ident, $primitive_type:ty, $logic_type:ident, $result_primitive_type:ty, $result_logic_type:ident, $array_type:ident) => {
Expand All @@ -132,10 +149,10 @@ macro_rules! run_difference_first_not_null_test {
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),
Some(3_i8 as $primitive_type),
None,
Some(4),
Some(10),
Some(4_i8 as $primitive_type),
Some(10_i8 as $primitive_type),
])]);

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

assert_eq!(&expected, actual);
Expand Down Expand Up @@ -252,6 +269,22 @@ run_difference_first_not_null_test!(
Int64,
Int64Array
);
run_difference_first_not_null_test!(
test_running_difference_f32_first_not_null,
f32,
Float32,
f64,
Float64,
Float64Array
);
run_difference_first_not_null_test!(
test_running_difference_f64_first_not_null,
f64,
Float64,
f64,
Float64,
Float64Array
);

macro_rules! run_difference_first_null_test {
($method_name:ident, $primitive_type:ty, $logic_type:ident, $result_primitive_type:ty, $result_logic_type:ident, $array_type:ident) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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.
0 0 0 0 0
2 2 2 2 0
2 2 2 2 0
5 5 5 5 0
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
drop table if exists runing_difference_test;

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');
create table runing_difference_test (a Int8, b Int32, c Int64, d varchar, e Float32, f Float64) engine=Memory;
insert into runing_difference_test values (1, 1, 1, 'a', 1, 1),(3, 3, 3, 'b', 3, 3),(5, 5, 5, 'c', 5, 5),(10, 10, 10, 'd', 10, 10);

select a, runningDifference(a), b, runningDifference(b), c, runningDifference(c), runningDifference(10) from runing_difference_test;
select d, runningDifference(d) from runing_difference_test;
select runningDifference(a), runningDifference(b), runningDifference(c), runningDifference(e), runningDifference(10) from runing_difference_test;
select d, runningDifference(d) from runing_difference_test; -- {ErrorCode 7}

DROP TABLE IF EXISTS runing_difference_test;

0 comments on commit 8812ab8

Please sign in to comment.