Skip to content

Commit

Permalink
support decimal scalar value
Browse files Browse the repository at this point in the history
  • Loading branch information
liukun4515 committed Dec 3, 2021
1 parent e4dcfc2 commit ce0150f
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 90 deletions.
27 changes: 0 additions & 27 deletions datafusion/src/execution/context.rs
Expand Up @@ -1914,33 +1914,6 @@ mod tests {
Ok(())
}

#[tokio::test]
async fn aggregate_min_decimal() -> Result<()> {
let mut ctx = ExecutionContext::new();
// schema with data
let schema = Arc::new(Schema::new(vec![
Field::new("c1", DataType::Decimal(10, 6), false),
Field::new("c2", DataType::Float64, false),
Field::new("c3", DataType::Boolean, false),
]));

ctx.register_csv(
"aggregate_simple",
"tests/aggregate_simple.csv",
CsvReadOptions::new().schema(&schema),
)
.await?;

// decimal query
let result = plan_and_collect(&mut ctx, "select min(c1) from aggregate_simple")
.await
.unwrap();

println!("{:?}", result);

Ok(())
}

#[tokio::test]
async fn aggregate_min() -> Result<()> {
let results = execute("SELECT MIN(c1), MIN(c2) FROM test", 4).await?;
Expand Down
63 changes: 0 additions & 63 deletions datafusion/src/physical_plan/expressions/min_max.rs
Expand Up @@ -208,20 +208,6 @@ fn max_batch(values: &ArrayRef) -> Result<ScalarValue> {
_ => min_max_batch!(values, max),
})
}
macro_rules! typed_min_max_decimal {
($VALUE:expr, $DELTA:expr, $PRECISION:expr, $SCALE:expr, $SCALAR:ident, $OP:ident) => {{
ScalarValue::$SCALAR(
match ($VALUE, $DELTA) {
(None, None) => None,
(Some(a), None) => Some(a.clone()),
(None, Some(b)) => Some(b.clone()),
(Some(a), Some(b)) => Some((*a).$OP(*b)),
},
$PRECISION.clone(),
$SCALE.clone(),
)
}};
}

// min/max of two non-string scalar values.
macro_rules! typed_min_max {
Expand Down Expand Up @@ -251,16 +237,6 @@ macro_rules! typed_min_max_string {
macro_rules! min_max {
($VALUE:expr, $DELTA:expr, $OP:ident) => {{
Ok(match ($VALUE, $DELTA) {
(ScalarValue::Decimal128(lhsv,lhsp,lhss), ScalarValue::Decimal128(rhsv,rhsp,rhss)) => {
if lhsp.eq(rhsp) && lhss.eq(rhss) {
typed_min_max_decimal!(lhsv, rhsv, lhsp, lhss, Decimal128, $OP)
} else {
return Err(DataFusionError::Internal(format!(
"MIN/MAX is not expected to receive scalars of incompatible types {:?}",
(ScalarValue::Decimal128(*lhsv,*lhsp,*lhss),ScalarValue::Decimal128(*rhsv,*rhsp,*rhss))
)));
}
}
(ScalarValue::Float64(lhs), ScalarValue::Float64(rhs)) => {
typed_min_max!(lhs, rhs, Float64, $OP)
}
Expand Down Expand Up @@ -508,48 +484,9 @@ mod tests {
use crate::physical_plan::expressions::col;
use crate::physical_plan::expressions::tests::aggregate;
use crate::{error::Result, generic_test_op};
use arrow::array::{DecimalArray, DecimalBuilder};
use arrow::datatypes::*;
use arrow::record_batch::RecordBatch;

#[test]
fn min_decimal() -> Result<()> {
let mut decimal_builder = DecimalBuilder::new(5, 10, 0);
for i in 1..6 {
decimal_builder.append_value(i as i128)?;
}
let array: ArrayRef = Arc::new(decimal_builder.finish());

// generic_test_op!(
// array,
// DataType::Decimal(10,0),
// Min,
// ScalarValue::from(5i64),
// DataType::Decimal(10,0)
// )

let left = ScalarValue::Decimal128(Some(123), Some(10), Some(2));
let right = ScalarValue::Decimal128(Some(124), Some(10), Some(2));
let result = min(&left, &right)?;
assert_eq!(result, left);

Ok(())
}

#[test]
fn max_decimal() -> Result<()> {
let left = ScalarValue::Decimal128(Some(123), Some(10), Some(2));
let right = ScalarValue::Decimal128(Some(124), Some(10), Some(2));
let result = max(&left, &right)?;
assert_eq!(result, right);

let right = ScalarValue::Decimal128(Some(124), Some(10), Some(3));
let result = max(&left, &right);
let expect = "Internal error: MIN/MAX is not expected to receive scalars of incompatible types (Decimal128(Some(123),Some(10),Some(2))";
assert_eq!(expect.to_string(), result.unwrap_err().to_string());
Ok(())
}

#[test]
fn max_i32() -> Result<()> {
let a: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5]));
Expand Down

0 comments on commit ce0150f

Please sign in to comment.