Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
ygf11 committed Sep 26, 2021
1 parent 7057863 commit 78f0048
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 53 deletions.
4 changes: 2 additions & 2 deletions common/functions/src/scalars/function_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ use crate::scalars::Function;
use crate::scalars::HashesFunction;
use crate::scalars::LogicFunction;
use crate::scalars::NullableFunction;
use crate::scalars::OtherFunction;
use crate::scalars::StringFunction;
use crate::scalars::ToCastFunction;
use crate::scalars::UdfFunction;
use crate::scalars::OtherFunction;

pub struct FunctionFactory;
pub type FactoryFunc = fn(name: &str) -> Result<Box<dyn Function>>;
Expand All @@ -53,7 +53,7 @@ lazy_static! {
ToCastFunction::register(map.clone()).unwrap();
ConditionalFunction::register(map.clone()).unwrap();
DateFunction::register(map.clone()).unwrap();
OtherFunction::register(map.clone()).unwrap();
OtherFunction::register(map.clone()).unwrap();
map
};
}
Expand Down
8 changes: 4 additions & 4 deletions common/functions/src/scalars/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#[cfg(test)]
mod function_column_test;
mod arithmetics;
mod comparisons;
mod conditionals;
Expand All @@ -22,14 +20,16 @@ mod expressions;
mod function;
mod function_alias;
mod function_column;
#[cfg(test)]
mod function_column_test;
mod function_factory;
mod function_literal;
mod hashes;
mod logics;
mod nullables;
mod others;
mod strings;
mod udfs;
mod others;

pub use arithmetics::*;
pub use comparisons::*;
Expand All @@ -45,6 +45,6 @@ pub use function_literal::LiteralFunction;
pub use hashes::*;
pub use logics::*;
pub use nullables::*;
pub use others::*;
pub use strings::*;
pub use udfs::*;
pub use others::*;
2 changes: 1 addition & 1 deletion common/functions/src/scalars/others/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ mod running_difference_function_test;
mod other;
mod running_difference_function;
pub use other::OtherFunction;
pub use running_difference_function::RunningDifferenceFunction;
pub use running_difference_function::RunningDifferenceFunction;
70 changes: 30 additions & 40 deletions common/functions/src/scalars/others/running_difference_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ impl Function for RunningDifferenceFunction {
match args[0] {
DataType::Int8 | DataType::UInt8 => Ok(DataType::Int16),
DataType::Int16 | DataType::UInt16 | DataType::Date16 => Ok(DataType::Int32),
DataType::Int32 | DataType::UInt32 | DataType::Int64 | DataType::UInt64 | DataType::Date32 | DataType::DateTime32(_) => Ok(DataType::Int64),
_ => Result::Err(ErrorCode::IllegalDataType(format!(
"Illegal type {:?} of argument of function {}.Should be a date16/data32 or a dateTime32",
"",
self.name()))),
DataType::Int32
| DataType::UInt32
| DataType::Int64
| DataType::UInt64
| DataType::Date32
| DataType::DateTime32(_) => Ok(DataType::Int64),
_ => Result::Err(ErrorCode::IllegalDataType(
"Argument for function runningDifference must have numeric type",
)),
}
}

Expand All @@ -60,36 +64,23 @@ impl Function for RunningDifferenceFunction {
}

fn eval(&self, columns: &DataColumnsWithField, input_rows: usize) -> Result<DataColumn> {
match columns[0].data_type() {
DataType::Int8 => {
compute_i8(columns[0].column(), input_rows)
},
DataType::UInt8 => {
compute_u8(columns[0].column(), input_rows)
},
DataType::Int16 => {
compute_i16(columns[0].column(), input_rows)
},
DataType::UInt16 | DataType::Date16 => {
compute_u16(columns[0].column(), input_rows)
},
DataType::Int32 => {
compute_i32(columns[0].column(), input_rows)
},
DataType::UInt32 | DataType::Date32 | DataType::DateTime32(_)=> {
match columns[0].data_type() {
DataType::Int8 => compute_i8(columns[0].column(), input_rows),
DataType::UInt8 => compute_u8(columns[0].column(), input_rows),
DataType::Int16 => compute_i16(columns[0].column(), input_rows),
DataType::UInt16 | DataType::Date16 => compute_u16(columns[0].column(), input_rows),
DataType::Int32 => compute_i32(columns[0].column(), input_rows),
DataType::UInt32 | DataType::Date32 | DataType::DateTime32(_) => {
compute_u32(columns[0].column(), input_rows)
},
DataType::Int64 => {
compute_i64(columns[0].column(), input_rows)
},
DataType::UInt64 => {
compute_u64(columns[0].column(), input_rows)
},
other => Result::Err(ErrorCode::IllegalDataType(format!(
"Illegal type {:?} of argument of function {}.Should be a date16/data32 or a dateTime32",
other,
self.name()))),
}
DataType::Int64 => compute_i64(columns[0].column(), input_rows),
DataType::UInt64 => compute_u64(columns[0].column(), input_rows),
_ => Result::Err(ErrorCode::IllegalDataType(
format!(
"Argument for function runningDifference must have numeric type.: While processing runningDifference({})",
columns[0].field().name(),
))),
}
}

fn is_deterministic(&self) -> bool {
Expand All @@ -103,10 +94,8 @@ 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> {
if let DataColumn::Constant(_, _) = column{
fn $func(column: &DataColumn, input_rows: usize) -> Result<DataColumn> {
if let DataColumn::Constant(_, _) = column {
Ok(DataColumn::Constant(
DataValue::$result_type(Some(0i8 as $target_type)),
input_rows,
Expand All @@ -125,7 +114,8 @@ macro_rules! run_difference_compute {
} 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 $target_type
- array.value(index - 1) as $target_type;
result_vec.push(Some(diff))
}
}
Expand All @@ -135,14 +125,14 @@ macro_rules! run_difference_compute {
Ok(Series::new(result_vec).into())
}
}
}
};
}

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!(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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ macro_rules! run_difference_first_not_null_test {
None,
Some(6_i8 as $result_type),
]);
actual.iter().for_each(|a| println!("{:?}", a));

assert_eq!(&expected, actual);

// result type
Expand Down Expand Up @@ -177,7 +177,7 @@ macro_rules! run_difference_first_null_test {
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)]);
actual.iter().for_each(|a| println!("{:?}", a));

assert_eq!(&expected, actual);

// result type
Expand Down Expand Up @@ -303,17 +303,14 @@ fn test_running_difference_datetime32_first_not_null() -> Result<()> {

// eval
let result = run_difference_function.eval(&columns, block.num_rows())?;
println!("result:{:?}", result);
let actual_ref = result.get_array_ref().unwrap();
let actual = actual_ref.as_any().downcast_ref::<Int64Array>().unwrap();
let expected = Int64Array::from([Some(0i64), Some(1), None, None, Some(6)]);
actual.iter().for_each(|a| println!("{:?}", a));
assert_eq!(&expected, actual);

// result type
let args_type_array = [DataType::DateTime32(None); 1];
let result_type = run_difference_function.return_type(&args_type_array[..])?;
println!("result_type:{:?}", result_type);
assert_eq!(result_type, DataType::Int64);
}

Expand Down Expand Up @@ -345,7 +342,7 @@ fn test_running_difference_datetime32_first_null() -> Result<()> {
let actual_ref = result.get_array_ref().unwrap();
let actual = actual_ref.as_any().downcast_ref::<Int64Array>().unwrap();
let expected = Int64Array::from([Some(0), Some(1_i64), None, None, Some(6_i64)]);
actual.iter().for_each(|a| println!("{:?}", a));

assert_eq!(&expected, actual);

// result type
Expand Down

0 comments on commit 78f0048

Please sign in to comment.