alan910127 opened a new pull request, #14737:
URL: https://github.com/apache/datafusion/pull/14737

   ## 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.
   -->
   
   - Closes #14451.
   
   ## 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 change ensures that array functions handle `NULL` and incorrect 
argument types gracefully, preventing unexpected failures or internal errors 
showing up to the user.
   
   ## 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.
   -->
   
   Added `ArrayFunctionArgument::DataType(data_type)` variant for array 
functions to define an argument of a specific type.
   
   ### `array_sort`
   
   - Returns NULL when either `desc` or `nulls_first` is NULL.
   - There's no incorrect input type since every type can be coerced into `Utf8`
   
   **NULL inputs:**
   ```
   > select array_sort([1,2,3]);
   +----------------------------------------------------+
   | array_sort(make_array(Int64(1),Int64(2),Int64(3))) |
   +----------------------------------------------------+
   | [1, 2, 3]                                          |
   +----------------------------------------------------+
   1 row(s) fetched.
   Elapsed 0.008 seconds.
   
   > select array_sort([1,2,3], NULL);
   +---------------------------------------------------------+
   | array_sort(make_array(Int64(1),Int64(2),Int64(3)),NULL) |
   +---------------------------------------------------------+
   | NULL                                                    |
   +---------------------------------------------------------+
   1 row(s) fetched.
   Elapsed 0.007 seconds.
   
   > select array_sort([1,2,3], NULL, 'NULLS FIRST');
   
+-----------------------------------------------------------------------------+
   | array_sort(make_array(Int64(1),Int64(2),Int64(3)),NULL,Utf8("NULLS 
FIRST")) |
   
+-----------------------------------------------------------------------------+
   | NULL                                                                       
 |
   
+-----------------------------------------------------------------------------+
   1 row(s) fetched.
   Elapsed 0.005 seconds.
   
   > select array_sort([1,2,3], 'DESC', NULL);
   +----------------------------------------------------------------------+
   | array_sort(make_array(Int64(1),Int64(2),Int64(3)),Utf8("DESC"),NULL) |
   +----------------------------------------------------------------------+
   | NULL                                                                 |
   +----------------------------------------------------------------------+
   1 row(s) fetched.
   Elapsed 0.007 seconds.
   
   > select array_sort([1,2,3], NULL, NULL);
   +--------------------------------------------------------------+
   | array_sort(make_array(Int64(1),Int64(2),Int64(3)),NULL,NULL) |
   +--------------------------------------------------------------+
   | NULL                                                         |
   +--------------------------------------------------------------+
   1 row(s) fetched.
   Elapsed 0.008 seconds.
   ```
   
   **Incorrect type of inputs:**
   N/A
   
   ### `array_replace`, `array_replace_n`, `array_replace_all`
   
   - Treat NULLs as elements.
   - Internal coercion errors are not handled yet. Don't know if it should be 
handled in this PR
   
   **NULL inputs:**
   ```
   > select array_replace([1,2,3,NULL],NULL,4);
   +--------------------------------------------------------------------------+
   | array_replace(make_array(Int64(1),Int64(2),Int64(3),NULL),NULL,Int64(4)) |
   +--------------------------------------------------------------------------+
   | [1, 2, 3, 4]                                                             |
   +--------------------------------------------------------------------------+
   1 row(s) fetched.
   Elapsed 0.008 seconds.
   
   > select array_replace([1,2,3,NULL],2,NULL);
   +--------------------------------------------------------------------------+
   | array_replace(make_array(Int64(1),Int64(2),Int64(3),NULL),Int64(2),NULL) |
   +--------------------------------------------------------------------------+
   | [1, NULL, 3, NULL]                                                       |
   +--------------------------------------------------------------------------+
   1 row(s) fetched.
   Elapsed 0.008 seconds.
   ```
   
   **incorrect type of inputs:**
   ```
   > select array_replace([1,2,3], 1, true);
   Error during planning: Internal error: Function 'array_replace' does not 
support coercion from Int64 to Boolean.
   This was likely caused by a bug in DataFusion's code and we would welcome 
that you file an bug report in our issue tracker No function matches the given 
name and argument types 'array_replace(List(Field { name: "item", data_type: 
Int64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), 
Int64, Boolean)'. You might need to add explicit type casts.
           Candidate functions:
           array_replace(array, element, element)
   ```
   
   ### `array_resize`
   
   - If `size` is NULL, returns an empty array. If `value` is NULL, fill the 
new slots to be NULL.
   - Incorrect types of arguments returns plan errors instead of internal 
errors.
   
   **NULL inputs:**
   ```
   > select array_resize([1,2,3], NULL);
   +-----------------------------------------------------------+
   | array_resize(make_array(Int64(1),Int64(2),Int64(3)),NULL) |
   +-----------------------------------------------------------+
   | []                                                        |
   +-----------------------------------------------------------+
   1 row(s) fetched.
   Elapsed 0.005 seconds.
   
   > select array_resize([1,2,3], NULL, NULL);
   +----------------------------------------------------------------+
   | array_resize(make_array(Int64(1),Int64(2),Int64(3)),NULL,NULL) |
   +----------------------------------------------------------------+
   | []                                                             |
   +----------------------------------------------------------------+
   1 row(s) fetched.
   Elapsed 0.003 seconds.
   
   > select array_resize([1,2,3], 5, NULL);
   +--------------------------------------------------------------------+
   | array_resize(make_array(Int64(1),Int64(2),Int64(3)),Int64(5),NULL) |
   +--------------------------------------------------------------------+
   | [1, 2, 3, NULL, NULL]                                              |
   +--------------------------------------------------------------------+
   1 row(s) fetched.
   Elapsed 0.006 seconds.
   ```
   
   **incorrect type of inputs:**
   ```
   > select array_resize([1,2,3], 'invalid', NULL);
   Error during planning: Failed to coerce arguments to satisfy a call to 
'array_resize' function: coercion from [List(Field { name: "item", data_type: 
Int64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), 
Utf8, Null] to the signature OneOf([ArraySignature(Array { arguments: [Array, 
Index], array_coercion: Some(FixedSizedListToList) }), ArraySignature(Array { 
arguments: [Array, Index, Element], array_coercion: Some(FixedSizedListToList) 
})]) failed No function matches the given name and argument types 
'array_resize(List(Field { name: "item", data_type: Int64, nullable: true, 
dict_id: 0, dict_is_ordered: false, metadata: {} }), Utf8, Null)'. You might 
need to add explicit type casts.
           Candidate functions:
           array_resize(array, index)
           array_resize(array, index, element)
   > select array_resize([1,2,3], 5, true);
   Error during planning: Failed to coerce arguments to satisfy a call to 
'array_resize' function: coercion from [List(Field { name: "item", data_type: 
Int64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), 
Int64, Boolean] to the signature OneOf([ArraySignature(Array { arguments: 
[Array, Index], array_coercion: Some(FixedSizedListToList) }), 
ArraySignature(Array { arguments: [Array, Index, Element], array_coercion: 
Some(FixedSizedListToList) })]) failed No function matches the given name and 
argument types 'array_resize(List(Field { name: "item", data_type: Int64, 
nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), Int64, 
Boolean)'. You might need to add explicit type casts.
           Candidate functions:
           array_resize(array, index)
           array_resize(array, index, element)
   ```
   
   ## 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
   5. 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)?
   -->
   
   Tests for `array_sort` with NULL and incorrect inputs are added in 
`array.slt`. Other functions are already tested via `arrays_values` in 
`sqllogictest`. If additional tests are needed, please let me know.
   
   ## Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be 
updated before approving the PR.
   -->
   No.
   
   <!--
   If there are any breaking changes to public APIs, please add the `api 
change` label.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to