This is an automated email from the ASF dual-hosted git repository.

github-bot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new f7a9f2449f fix: spark array return type mismatch when inner data type 
is LargeList (#18485)
f7a9f2449f is described below

commit f7a9f2449f7e45bf4dbff49979089e5c619b1faf
Author: jizezhang <[email protected]>
AuthorDate: Wed Nov 5 00:06:31 2025 -0800

    fix: spark array return type mismatch when inner data type is LargeList 
(#18485)
    
    ## Which issue does this PR close?
    
    <!--
    We generally require a GitHub issue to be filed for all bug fixes and
    enhancements and this helps us generate change logs for our releases.
    You can link an issue to this PR using the GitHub syntax. For example
    `Closes #123` indicates that this PR will close issue #123.
    -->
    
    - This PR came up as part of #17964.
    
    ## Rationale for this change
    
    <!--
    Why are you proposing this change? If this is already explained clearly
    in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand
    your changes and offer better suggestions for fixes.
    -->
    
    This PR is intended to fix return type mismatch of spark `array` when
    inner data type is `LargeList`, e.g.
    ```
    query error
    SELECT array(arrow_cast(array(1), 'LargeList(Int64)'))
    ----
    DataFusion error: Internal error: Function 'array' returned value of type 
'LargeList(Field { name: "element", data_type: LargeList(Field { data_type: 
Int64, nullable: true }), nullable: true })' while the following type was 
promised at planning time and expected: 'List(Field { name: "element", 
data_type: LargeList(Field { data_type: Int64, nullable: true }), nullable: 
true })'.
    This issue was likely caused by a bug in DataFusion's code. Please help us 
to resolve this by filing a bug report in our issue tracker: 
https://github.com/apache/datafusion/issues
    ```
    
    ## What changes are included in this PR?
    
    <!--
    There is no need to duplicate the description in the issue here but it
    is sometimes worth providing a summary of the individual changes in this
    PR.
    -->
    
    - Return `List` regardless of whether inner data type is `LargeList` or
    not. This aligns with the behavior of datafusion `make_array` function.
    - Remove `return_field_from_args` as `return_type` is already defined
    and is invoked internally.
    
    ## Are these changes tested?
    
    <!--
    We typically require tests for all PRs in order to:
    1. Prevent the code from being accidentally broken by subsequent changes
    2. Serve as another way to document the expected behavior of the code
    
    If tests are not included in your PR, please explain why (for example,
    are they covered by existing tests)?
    -->
    
    Yes
    
    ## Are there any user-facing changes?
    
    <!--
    If there are user-facing changes then we may require documentation to be
    updated before approving the PR.
    -->
    
    <!--
    If there are any breaking changes to public APIs, please add the `api
    change` label.
    -->
    
    No.
    
    ---------
    
    Co-authored-by: Michael Kleen <[email protected]>
    Co-authored-by: Sergey Zhukov <[email protected]>
    Co-authored-by: Sergey Zhukov <[email protected]>
    Co-authored-by: Andrew Lamb <[email protected]>
    Co-authored-by: bubulalabu <[email protected]>
    Co-authored-by: Vegard Stikbakke <[email protected]>
---
 datafusion/spark/src/function/array/spark_array.rs | 31 +++++++++++-----------
 .../sqllogictest/test_files/spark/array/array.slt  | 15 +++++++++++
 2 files changed, 31 insertions(+), 15 deletions(-)

diff --git a/datafusion/spark/src/function/array/spark_array.rs 
b/datafusion/spark/src/function/array/spark_array.rs
index bf5842cb5a..bb9665613d 100644
--- a/datafusion/spark/src/function/array/spark_array.rs
+++ b/datafusion/spark/src/function/array/spark_array.rs
@@ -24,7 +24,7 @@ use arrow::array::{
 use arrow::buffer::OffsetBuffer;
 use arrow::datatypes::{DataType, Field, FieldRef};
 use datafusion_common::utils::SingleRowListArrayBuilder;
-use datafusion_common::{plan_datafusion_err, plan_err, Result};
+use datafusion_common::{internal_err, plan_datafusion_err, plan_err, Result};
 use datafusion_expr::type_coercion::binary::comparison_coercion;
 use datafusion_expr::{
     ColumnarValue, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDFImpl, 
Signature,
@@ -72,9 +72,20 @@ impl ScalarUDFImpl for SparkArray {
         &self.signature
     }
 
-    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
+    fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
+        internal_err!("return_field_from_args should be used instead")
+    }
+
+    fn return_field_from_args(&self, args: ReturnFieldArgs) -> 
Result<FieldRef> {
+        let data_types = args
+            .arg_fields
+            .iter()
+            .map(|f| f.data_type())
+            .cloned()
+            .collect::<Vec<_>>();
+
         let mut expr_type = DataType::Null;
-        for arg_type in arg_types {
+        for arg_type in &data_types {
             if !arg_type.equals_datatype(&DataType::Null) {
                 expr_type = arg_type.clone();
                 break;
@@ -85,21 +96,12 @@ impl ScalarUDFImpl for SparkArray {
             expr_type = DataType::Int32;
         }
 
-        Ok(DataType::List(Arc::new(Field::new(
+        let return_type = DataType::List(Arc::new(Field::new(
             ARRAY_FIELD_DEFAULT_NAME,
             expr_type,
             true,
-        ))))
-    }
+        )));
 
-    fn return_field_from_args(&self, args: ReturnFieldArgs) -> 
Result<FieldRef> {
-        let data_types = args
-            .arg_fields
-            .iter()
-            .map(|f| f.data_type())
-            .cloned()
-            .collect::<Vec<_>>();
-        let return_type = self.return_type(&data_types)?;
         Ok(Arc::new(Field::new(
             "this_field_name_is_irrelevant",
             return_type,
@@ -166,7 +168,6 @@ pub fn make_array_inner(arrays: &[ArrayRef]) -> 
Result<ArrayRef> {
                     .build_list_array(),
             ))
         }
-        DataType::LargeList(..) => array_array::<i64>(arrays, data_type),
         _ => array_array::<i32>(arrays, data_type),
     }
 }
diff --git a/datafusion/sqllogictest/test_files/spark/array/array.slt 
b/datafusion/sqllogictest/test_files/spark/array/array.slt
index 09821e6d58..79dca1c10a 100644
--- a/datafusion/sqllogictest/test_files/spark/array/array.slt
+++ b/datafusion/sqllogictest/test_files/spark/array/array.slt
@@ -70,3 +70,18 @@ query ?
 SELECT array(array(1,2));
 ----
 [[1, 2]]
+
+query ?
+SELECT array(arrow_cast(array(1), 'LargeList(Int64)'));
+----
+[[1]]
+
+query ?
+SELECT array(arrow_cast(array(1), 'LargeList(Int64)'), arrow_cast(array(), 
'LargeList(Int64)'));
+----
+[[1], []]
+
+query ?
+SELECT array(arrow_cast(array(1,2), 'LargeList(Int64)'), array(3));
+----
+[[1, 2], [3]]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to