Skip to content
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
50 changes: 48 additions & 2 deletions datafusion/spark/src/function/bitmap/bitmap_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use arrow::array::{
use arrow::datatypes::DataType::{
Binary, BinaryView, Dictionary, FixedSizeBinary, LargeBinary,
};
use arrow::datatypes::{DataType, Int16Type, Int32Type, Int64Type, Int8Type};
use arrow::datatypes::{DataType, FieldRef, Int16Type, Int32Type, Int64Type, Int8Type};
use datafusion_common::utils::take_function_args;
use datafusion_common::{internal_err, Result};
use datafusion_expr::{
Expand Down Expand Up @@ -71,7 +71,20 @@ impl ScalarUDFImpl for BitmapCount {
}

fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
Ok(DataType::Int64)
internal_err!("return_field_from_args should be used instead")
}

fn return_field_from_args(
&self,
args: datafusion_expr::ReturnFieldArgs,
) -> Result<FieldRef> {
use arrow::datatypes::Field;
// bitmap_count returns Int64 with the same nullability as the input
Ok(Arc::new(Field::new(
args.arg_fields[0].name(),
DataType::Int64,
args.arg_fields[0].is_nullable(),
)))
}

fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
Expand Down Expand Up @@ -224,4 +237,37 @@ mod tests {
assert_eq!(*actual.into_array(1)?, *expect.into_array(1)?);
Ok(())
}

#[test]
fn test_bitmap_count_nullability() -> Result<()> {
use datafusion_expr::ReturnFieldArgs;

let bitmap_count = BitmapCount::new();

// Test with non-nullable binary field
let non_nullable_field = Arc::new(Field::new("bin", DataType::Binary, false));

let result = bitmap_count.return_field_from_args(ReturnFieldArgs {
arg_fields: &[Arc::clone(&non_nullable_field)],
scalar_arguments: &[None],
})?;

// The result should not be nullable (same as input)
assert!(!result.is_nullable());
assert_eq!(result.data_type(), &Int64);

// Test with nullable binary field
let nullable_field = Arc::new(Field::new("bin", DataType::Binary, true));

let result = bitmap_count.return_field_from_args(ReturnFieldArgs {
arg_fields: &[Arc::clone(&nullable_field)],
scalar_arguments: &[None],
})?;

// The result should be nullable (same as input)
assert!(result.is_nullable());
assert_eq!(result.data_type(), &Int64);

Ok(())
}
}