Skip to content

Commit 490e399

Browse files
committed
feat: ArrayType nested literal
1 parent 9dac47c commit 490e399

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

native/core/src/execution/planner.rs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ use datafusion::physical_expr::window::WindowExpr;
8484
use datafusion::physical_expr::LexOrdering;
8585

8686
use crate::parquet::parquet_exec::init_datasource_exec;
87-
use arrow::array::{new_empty_array, Array, ArrayRef, BinaryBuilder, BooleanArray, Date32Array, Decimal128Array, Float32Array, Float64Array, Int16Array, Int32Array, Int64Array, Int8Array, ListArray, NullArray, StringBuilder, TimestampMicrosecondArray};
87+
use arrow::array::{
88+
new_empty_array, Array, ArrayRef, BinaryBuilder, BooleanArray, Date32Array, Decimal128Array,
89+
Float32Array, Float64Array, Int16Array, Int32Array, Int64Array, Int8Array, ListArray,
90+
NullArray, StringBuilder, TimestampMicrosecondArray,
91+
};
8892
use arrow::buffer::{BooleanBuffer, NullBuffer, OffsetBuffer};
8993
use datafusion::common::utils::SingleRowListArrayBuilder;
9094
use datafusion::physical_plan::coalesce_batches::CoalesceBatchesExec;
@@ -480,7 +484,7 @@ impl PhysicalPlanner {
480484
}
481485
},
482486
Value::ListVal(values) => {
483-
if let DataType::List(ref f) = data_type {
487+
if let DataType::List(_) = data_type {
484488
SingleRowListArrayBuilder::new(literal_to_array_ref(data_type, values.clone())?).build_list_scalar()
485489
} else {
486490
return Err(GeneralError(format!(
@@ -2684,8 +2688,6 @@ fn literal_to_array_ref(
26842688
list_literal: ListLiteral,
26852689
) -> Result<ArrayRef, ExecutionError> {
26862690
let nulls = &list_literal.null_mask;
2687-
dbg!(&data_type);
2688-
dbg!(&list_literal);
26892691
match data_type {
26902692
DataType::Null => Ok(Arc::new(NullArray::new(nulls.len()))),
26912693
DataType::Boolean => Ok(Arc::new(BooleanArray::new(
@@ -2813,14 +2815,15 @@ fn literal_to_array_ref(
28132815
}
28142816
DataType::List(ref f) => {
28152817
let dt = f.data_type().clone();
2816-
2818+
28172819
// Build offsets and collect non-null child arrays
28182820
let mut offsets = Vec::with_capacity(list_literal.list_values.len() + 1);
28192821
offsets.push(0i32);
28202822
let mut child_arrays: Vec<ArrayRef> = Vec::new();
2821-
2823+
28222824
for (i, child_literal) in list_literal.list_values.iter().enumerate() {
2823-
if list_literal.null_mask[i] {
2825+
// Check if the current child literal is non-null and not the empty array
2826+
if list_literal.null_mask[i] && *child_literal != ListLiteral::default() {
28242827
// Non-null entry: process the child array
28252828
let child_array = literal_to_array_ref(dt.clone(), child_literal.clone())?;
28262829
let len = child_array.len() as i32;
@@ -3664,21 +3667,21 @@ mod tests {
36643667
#[tokio::test]
36653668
async fn test_literal_to_list() -> Result<(), DataFusionError> {
36663669
/*
3667-
[
3668-
[
3669-
[1, 2, 3],
3670-
[4, 5, 6],
3671-
[7, 8, 9, null],
3672-
[],
3673-
null
3674-
],
3675-
[
3676-
[10, null, 12]
3677-
],
3678-
null,
3679-
[]
3680-
]
3681-
*/
3670+
[
3671+
[
3672+
[1, 2, 3],
3673+
[4, 5, 6],
3674+
[7, 8, 9, null],
3675+
[],
3676+
null
3677+
],
3678+
[
3679+
[10, null, 12]
3680+
],
3681+
null,
3682+
[]
3683+
]
3684+
*/
36823685
let data = ListLiteral {
36833686
list_values: vec![
36843687
ListLiteral {
@@ -3750,22 +3753,17 @@ mod tests {
37503753

37513754
let array = literal_to_array_ref(nested_type, data)?;
37523755

3753-
dbg!(&array);
3754-
dbg!(&array.nulls());
3755-
37563756
// Top-level should be ListArray<ListArray<Int32>>
37573757
let list_outer = array.as_any().downcast_ref::<ListArray>().unwrap();
37583758
assert_eq!(list_outer.len(), 4);
37593759

37603760
// First outer element: ListArray<Int32>
37613761
let first_elem = list_outer.value(0);
3762-
dbg!(&first_elem);
37633762
let list_inner = first_elem.as_any().downcast_ref::<ListArray>().unwrap();
37643763
assert_eq!(list_inner.len(), 5);
37653764

37663765
// Inner values
37673766
let v0 = list_inner.value(0);
3768-
dbg!(&v0);
37693767
let vals0 = v0.as_any().downcast_ref::<Int32Array>().unwrap();
37703768
assert_eq!(vals0.values(), &[1, 2, 3]);
37713769

@@ -3786,8 +3784,6 @@ mod tests {
37863784
let vals3 = v3.as_any().downcast_ref::<Int32Array>().unwrap();
37873785
assert_eq!(vals3.values(), &[10, 0, 11]);
37883786

3789-
//println!("result 2 {:?}", build_array(&data));
3790-
37913787
Ok(())
37923788
}
37933789
}

spark/src/test/scala/org/apache/comet/exec/CometNativeReaderSuite.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,4 +557,12 @@ class CometNativeReaderSuite extends CometTestBase with AdaptiveSparkPlanHelper
557557
assert(sql("SELECT * FROM array_tbl where arr = ARRAY(1L)").count == 1)
558558
}
559559
}
560+
561+
test("native reader - support ARRAY literal ARRAY fields") {
562+
testSingleLineQuery(
563+
"""
564+
|select 1 a
565+
|""".stripMargin,
566+
"select array(array(1, 2, null), array(), array(10), null) from tbl")
567+
}
560568
}

0 commit comments

Comments
 (0)