Skip to content
Draft
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion datafusion/common/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ use crate::{downcast_value, Result};
use arrow::array::{
BinaryViewArray, DurationMicrosecondArray, DurationMillisecondArray,
DurationNanosecondArray, DurationSecondArray, Float16Array, Int16Array, Int8Array,
LargeBinaryArray, LargeStringArray, StringViewArray, UInt16Array,
LargeBinaryArray, LargeListViewArray, LargeStringArray, ListViewArray,
StringViewArray, UInt16Array,
};
use arrow::{
array::{
Expand Down Expand Up @@ -147,6 +148,16 @@ pub fn as_list_array(array: &dyn Array) -> Result<&ListArray> {
Ok(downcast_value!(array, ListArray))
}

// Downcast Array to ListViewArray
pub fn as_list_view_array(array: &dyn Array) -> Result<&ListViewArray> {
Ok(downcast_value!(array, ListViewArray))
}

// Downcast Array to LargeListViewArray
pub fn as_large_list_view_array(array: &dyn Array) -> Result<&LargeListViewArray> {
Ok(downcast_value!(array, LargeListViewArray))
}
Comment on lines +151 to +159
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstrings need triple quotes! ... Although I see that the ones around this also don't have docstrings.

Suggested change
// Downcast Array to ListViewArray
pub fn as_list_view_array(array: &dyn Array) -> Result<&ListViewArray> {
Ok(downcast_value!(array, ListViewArray))
}
// Downcast Array to LargeListViewArray
pub fn as_large_list_view_array(array: &dyn Array) -> Result<&LargeListViewArray> {
Ok(downcast_value!(array, LargeListViewArray))
}
/// Downcast Array to ListViewArray
pub fn as_list_view_array(array: &dyn Array) -> Result<&ListViewArray> {
Ok(downcast_value!(array, ListViewArray))
}
/// Downcast Array to LargeListViewArray
pub fn as_large_list_view_array(array: &dyn Array) -> Result<&LargeListViewArray> {
Ok(downcast_value!(array, LargeListViewArray))
}


// Downcast Array to DictionaryArray
pub fn as_dictionary_array<T: ArrowDictionaryKeyType>(
array: &dyn Array,
Expand Down
46 changes: 41 additions & 5 deletions datafusion/core/tests/dataframe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ mod dataframe_functions;
mod describe;

use arrow::array::{
record_batch, Array, ArrayRef, BooleanArray, DictionaryArray, FixedSizeListArray,
FixedSizeListBuilder, Float32Array, Float64Array, Int32Array, Int32Builder,
Int8Array, LargeListArray, ListArray, ListBuilder, RecordBatch, StringArray,
StringBuilder, StructBuilder, UInt32Array, UInt32Builder, UnionArray,
as_list_array, record_batch, Array, ArrayRef, BooleanArray, DictionaryArray,
FixedSizeListArray, FixedSizeListBuilder, Float32Array, Float64Array, Int32Array,
Int32Builder, Int8Array, LargeListArray, ListArray, ListBuilder, RecordBatch,
StringArray, StringBuilder, StructBuilder, UInt32Array, UInt32Builder, UnionArray,
};
use arrow::buffer::ScalarBuffer;
use arrow::buffer::{NullBuffer, OffsetBuffer, ScalarBuffer};
use arrow::datatypes::{
DataType, Field, Float32Type, Int32Type, Schema, SchemaRef, UInt64Type, UnionFields,
UnionMode,
Expand Down Expand Up @@ -79,6 +79,7 @@ use datafusion_expr::{
LogicalPlanBuilder, ScalarFunctionImplementation, SortExpr, WindowFrame,
WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
};
use datafusion_functions_nested::transform::array_transform_udf;
use datafusion_physical_expr::expressions::Column;
use datafusion_physical_expr::Partitioning;
use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
Expand Down Expand Up @@ -6245,3 +6246,38 @@ async fn test_copy_to_preserves_order() -> Result<()> {
);
Ok(())
}

#[tokio::test]
async fn test_function_array_transform() -> Result<()> {
let values = Arc::new(Int32Array::from(vec![1, -2, 3, -4, 5, -6, 7, -8]));
let field = Arc::new(Field::new("a", DataType::Int32, false));
let offsets: OffsetBuffer<i32> = OffsetBuffer::from_lengths([3, 2, 3]);

let outer = Arc::new(ListArray::try_new(field, offsets, values, None)?);

let df = DataFrame::from_columns(vec![("a", outer)])?;

let udf = array_transform_udf(datafusion_functions::math::abs(), 0);

let df = df.select([col("a"), udf.call(vec![col("a")]).alias("abs(a[])")])?;

let results = df.collect().await?;
let result_column = as_list_array(results[0].column(1));
assert_eq!(result_column.len(), 3);

let expected_values = Arc::new(Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8]));
let expected_field = Arc::new(Field::new("abs", DataType::Int32, true));
let expected_offsets: OffsetBuffer<i32> = OffsetBuffer::from_lengths([3, 2, 3]);
let expected_nulls = NullBuffer::new_valid(3);

let expected = Arc::new(ListArray::try_new(
expected_field,
expected_offsets,
expected_values,
Some(expected_nulls),
)?) as ArrayRef;

assert_eq!(results[0].column(1), &expected);

Ok(())
}
1 change: 1 addition & 0 deletions datafusion/functions-nested/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub mod reverse;
pub mod set_ops;
pub mod sort;
pub mod string;
pub mod transform;
pub mod utils;

use datafusion_common::Result;
Expand Down
Loading