Skip to content

Commit

Permalink
support cast/try_cast for decimal: signed numeric to decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
liukun4515 committed Dec 14, 2021
1 parent 4b454d0 commit ca259e0
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 2 deletions.
6 changes: 4 additions & 2 deletions datafusion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ avro = ["avro-rs", "num-traits"]
[dependencies]
ahash = "0.7"
hashbrown = { version = "0.11", features = ["raw"] }
arrow = { version = "6.3.0", features = ["prettyprint"] }
parquet = { version = "6.3.0", features = ["arrow"] }
#arrow = { version = "6.3.0", features = ["prettyprint"] }
#parquet = { version = "6.3.0", features = ["arrow"] }
arrow = { path = "/Users/kliu3/Documents/github/arrow-rs/arrow", features = ["prettyprint"] }
parquet = { path = "/Users/kliu3/Documents/github/arrow-rs/parquet", features = ["arrow"] }
sqlparser = "0.13"
paste = "^1.0"
num_cpus = "1.13.0"
Expand Down
92 changes: 92 additions & 0 deletions datafusion/src/physical_plan/expressions/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ pub fn cast_with_options(
cast_type: DataType,
cast_options: CastOptions,
) -> Result<Arc<dyn PhysicalExpr>> {
// TODO
// support numeric data type to decimal
// support one type decimal to another type decimal
let expr_type = expr.data_type(input_schema)?;
if expr_type == cast_type {
Ok(expr.clone())
Expand Down Expand Up @@ -217,6 +220,95 @@ mod tests {
}};
}

#[test]
fn test_cast_numeric_to_decimal() -> Result<()> {
// int32
generic_test_cast!(
Int32Array,
DataType::Int32,
vec![1, 2, 3, 4, 5],
// TODO
UInt32Array,
DataType::UInt32,
vec![
Some(1_u32),
Some(2_u32),
Some(3_u32),
Some(4_u32),
Some(5_u32)
],
DEFAULT_DATAFUSION_CAST_OPTIONS
);
// int64
generic_test_cast!(
Int32Array,
DataType::Int64,
vec![1, 2, 3, 4, 5],
// TODO
UInt32Array,
DataType::UInt32,
vec![
Some(1_u32),
Some(2_u32),
Some(3_u32),
Some(4_u32),
Some(5_u32)
],
DEFAULT_DATAFUSION_CAST_OPTIONS
);
// float32
generic_test_cast!(
Int32Array,
DataType::Float32,
vec![1, 2, 3, 4, 5],
// TODO
UInt32Array,
DataType::UInt32,
vec![
Some(1_u32),
Some(2_u32),
Some(3_u32),
Some(4_u32),
Some(5_u32)
],
DEFAULT_DATAFUSION_CAST_OPTIONS
);
// float64
generic_test_cast!(
Int32Array,
DataType::Float64,
vec![1, 2, 3, 4, 5],
// TODO
UInt32Array,
DataType::UInt32,
vec![
Some(1_u32),
Some(2_u32),
Some(3_u32),
Some(4_u32),
Some(5_u32)
],
DEFAULT_DATAFUSION_CAST_OPTIONS
);
generic_test_cast!(
Int32Array,
DataType::Decimal(10, 4),
vec![1, 2, 3, 4, 5],
// TODO
UInt32Array,
DataType::UInt32,
vec![
Some(1_u32),
Some(2_u32),
Some(3_u32),
Some(4_u32),
Some(5_u32)
],
DEFAULT_DATAFUSION_CAST_OPTIONS
);
Ok(())
}

#[test]
fn test_cast_i32_u32() -> Result<()> {
generic_test_cast!(
Expand Down
92 changes: 92 additions & 0 deletions datafusion/src/physical_plan/expressions/try_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ pub fn try_cast(
input_schema: &Schema,
cast_type: DataType,
) -> Result<Arc<dyn PhysicalExpr>> {
// TODO
// support numeric data type to decimal
// support one type decimal to another type decimal
let expr_type = expr.data_type(input_schema)?;
if expr_type == cast_type {
Ok(expr.clone())
Expand Down Expand Up @@ -175,6 +178,95 @@ mod tests {
}};
}

#[test]
fn test_try_cast_numeric_to_decimal() -> Result<()> {
// int32
generic_test_cast!(
Int32Array,
DataType::Int32,
vec![1, 2, 3, 4, 5],
// TODO
UInt32Array,
DataType::UInt32,
vec![
Some(1_u32),
Some(2_u32),
Some(3_u32),
Some(4_u32),
Some(5_u32)
],
DEFAULT_DATAFUSION_CAST_OPTIONS
);
// int64
generic_test_cast!(
Int32Array,
DataType::Int64,
vec![1, 2, 3, 4, 5],
// TODO
UInt32Array,
DataType::UInt32,
vec![
Some(1_u32),
Some(2_u32),
Some(3_u32),
Some(4_u32),
Some(5_u32)
],
DEFAULT_DATAFUSION_CAST_OPTIONS
);
// float32
generic_test_cast!(
Int32Array,
DataType::Float32,
vec![1, 2, 3, 4, 5],
// TODO
UInt32Array,
DataType::UInt32,
vec![
Some(1_u32),
Some(2_u32),
Some(3_u32),
Some(4_u32),
Some(5_u32)
],
DEFAULT_DATAFUSION_CAST_OPTIONS
);
// float64
generic_test_cast!(
Int32Array,
DataType::Float64,
vec![1, 2, 3, 4, 5],
// TODO
UInt32Array,
DataType::UInt32,
vec![
Some(1_u32),
Some(2_u32),
Some(3_u32),
Some(4_u32),
Some(5_u32)
],
DEFAULT_DATAFUSION_CAST_OPTIONS
);
generic_test_cast!(
Int32Array,
DataType::Decimal(10, 4),
vec![1, 2, 3, 4, 5],
// TODO
UInt32Array,
DataType::UInt32,
vec![
Some(1_u32),
Some(2_u32),
Some(3_u32),
Some(4_u32),
Some(5_u32)
],
DEFAULT_DATAFUSION_CAST_OPTIONS
);
Ok(())
}

#[test]
fn test_cast_i32_u32() -> Result<()> {
generic_test_cast!(
Expand Down

0 comments on commit ca259e0

Please sign in to comment.