Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support date32 and date 64 in inlist node #3413

Merged
merged 1 commit into from
Sep 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 164 additions & 2 deletions datafusion/physical-expr/src/expressions/in_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ use crate::PhysicalExpr;
use arrow::array::*;
use datafusion_common::ScalarValue;
use datafusion_common::ScalarValue::{
Binary, Boolean, Decimal128, Int16, Int32, Int64, Int8, LargeBinary, LargeUtf8,
UInt16, UInt32, UInt64, UInt8, Utf8,
Binary, Boolean, Date32, Date64, Decimal128, Int16, Int32, Int64, Int8, LargeBinary,
LargeUtf8, UInt16, UInt32, UInt64, UInt8, Utf8,
};
use datafusion_common::{DataFusionError, Result};
use datafusion_expr::ColumnarValue;
Expand Down Expand Up @@ -619,6 +619,26 @@ impl PhysicalExpr for InListExpr {
u64
))
}
DataType::Date32 => {
let array = array.as_any().downcast_ref::<Date32Array>().unwrap();
Ok(set_contains_for_primitive!(
array,
set,
Date32,
self.negated,
i32
))
}
DataType::Date64 => {
let array = array.as_any().downcast_ref::<Date64Array>().unwrap();
Ok(set_contains_for_primitive!(
array,
set,
Date64,
self.negated,
i64
))
}
DataType::Float32 => {
let array = array.as_any().downcast_ref::<Float32Array>().unwrap();
Ok(set_contains_for_float!(
Expand Down Expand Up @@ -779,6 +799,24 @@ impl PhysicalExpr for InListExpr {
UInt8Array
)
}
DataType::Date32 => {
make_contains_primitive!(
array,
list_values,
self.negated,
Date32,
Date32Array
)
}
DataType::Date64 => {
make_contains_primitive!(
array,
list_values,
self.negated,
Date64,
Date64Array
)
}
DataType::Boolean => Ok(make_contains!(
array,
list_values,
Expand Down Expand Up @@ -1131,6 +1169,130 @@ mod tests {
Ok(())
}

#[test]
fn in_list_date64() -> Result<()> {
let schema = Schema::new(vec![Field::new("a", DataType::Date64, true)]);
let a = Date64Array::from(vec![Some(0), Some(2), None]);
let col_a = col("a", &schema)?;
let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(a)])?;

// expression: "a in (0, 1)"
let list = vec![lit(Date64(Some(0))), lit(Date64(Some(1)))];
in_list!(
batch,
list,
&false,
vec![Some(true), Some(false), None],
col_a.clone(),
&schema
);

// expression: "a not in (0, 1)"
let list = vec![lit(Date64(Some(0))), lit(Date64(Some(1)))];
in_list!(
batch,
list,
&true,
vec![Some(false), Some(true), None],
col_a.clone(),
&schema
);

// expression: "a in (0, 1, NULL)"
let list = vec![
lit(Date64(Some(0))),
lit(Date64(Some(1))),
lit(ScalarValue::Null),
];
in_list!(
batch,
list,
&false,
vec![Some(true), None, None],
col_a.clone(),
&schema
);

// expression: "a not in (0, 1, NULL)"
let list = vec![
lit(Date64(Some(0))),
lit(Date64(Some(1))),
lit(ScalarValue::Null),
];
in_list!(
batch,
list,
&true,
vec![Some(false), None, None],
col_a.clone(),
&schema
);

Ok(())
}

#[test]
fn in_list_date32() -> Result<()> {
let schema = Schema::new(vec![Field::new("a", DataType::Date32, true)]);
let a = Date32Array::from(vec![Some(0), Some(2), None]);
let col_a = col("a", &schema)?;
let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(a)])?;

// expression: "a in (0, 1)"
let list = vec![lit(Date32(Some(0))), lit(Date32(Some(1)))];
in_list!(
batch,
list,
&false,
vec![Some(true), Some(false), None],
col_a.clone(),
&schema
);

// expression: "a not in (0, 1)"
let list = vec![lit(Date32(Some(0))), lit(Date32(Some(1)))];
in_list!(
batch,
list,
&true,
vec![Some(false), Some(true), None],
col_a.clone(),
&schema
);

// expression: "a in (0, 1, NULL)"
let list = vec![
lit(Date32(Some(0))),
lit(Date32(Some(1))),
lit(ScalarValue::Null),
];
in_list!(
batch,
list,
&false,
vec![Some(true), None, None],
col_a.clone(),
&schema
);

// expression: "a not in (0, 1, NULL)"
let list = vec![
lit(Date32(Some(0))),
lit(Date32(Some(1))),
lit(ScalarValue::Null),
];
in_list!(
batch,
list,
&true,
vec![Some(false), None, None],
col_a.clone(),
&schema
);

Ok(())
}

#[test]
fn in_list_decimal() -> Result<()> {
// Now, we can check the NULL type
Expand Down