datafusion icon indicating copy to clipboard operation
datafusion copied to clipboard

unexpected output for `concat` for arrays

Open comphead opened this issue 1 year ago • 17 comments

Describe the bug

concat builtin in function should concat arrays like array_concat, currently the output is wrong

> select concat(make_array(1, 2, 3), make_array(4, 5));
+------------------------------------------------------------------------------+
| concat(make_array(Int64(1),Int64(2),Int64(3)),make_array(Int64(4),Int64(5))) |
+------------------------------------------------------------------------------+
| [1, 2, 3][4, 5]                                                              |
+------------------------------------------------------------------------------+
1 row(s) fetched. 
Elapsed 0.012 seconds.

> select array_concat(make_array(1, 2, 3), make_array(4, 5));
+------------------------------------------------------------------------------------+
| array_concat(make_array(Int64(1),Int64(2),Int64(3)),make_array(Int64(4),Int64(5))) |
+------------------------------------------------------------------------------------+
| [1, 2, 3, 4, 5]                                                                    |
+------------------------------------------------------------------------------------+
1 row(s) fetched. 
Elapsed 0.014 seconds.

concat for array should return same as array_concat

To Reproduce

No response

Expected behavior

No response

Additional context

No response

comphead avatar Oct 12 '25 03:10 comphead

take

petern48 avatar Oct 12 '25 06:10 petern48

I see that the string concat function is being called instead of function-nested array_concat one. Adding a concat alias to array_concat technically solves the problem. Is that what we want? Seems reasonable to me, but I just didn't see any signs that we want / expected this to be already be an alias.

petern48 avatar Oct 12 '25 06:10 petern48

Thanks @petern48 for looking into this issue. I'm not sure how would be easy to make an alias depending on the signature. in the end of the day we need to have concat with arrays working properly and this is current behavior in DuckDB.

Naive solution is to replace concat function to array_concat in the planner depending on the input

comphead avatar Oct 12 '25 18:10 comphead

I see, so it sounds like we want to use the current concat implementation only for strings and then use array_concat implementation for the other data types?

I tried finding an approach where we didn't have to replace the function call in the planner. I tried adding concat as an alias to array_concat, hoping that concat.rs would be called for strings and array_concat would work for everything else, but array_concat seemed to just override the concat.rs udf entirely (saying there's no valid kernel for Utf8 args), so the existing concat tests failed.

Anyways, given my current bandwidth, I'd rather leave this for someone else to debug / propose a solution. Could you unassign me @comphead? Doesn't seem like I can do it myself.

petern48 avatar Oct 15 '25 05:10 petern48

We have to use array_concat for concatenating arrays because concat is used for strings, and array_concat is used for concatenating arrays.

divyanshmrawal avatar Oct 16 '25 18:10 divyanshmrawal

I'd like to take a shot at this issue. After examining the codebase, I think there's a clean solution that doesn't require planner changes or function aliases.

The key insight is that we can modify the existing concat function to detect when array types are passed and delegate to the array_concat logic internally, while preserving all existing string concatenation behavior.

Proposed approach:

  1. Extend the concat function signature to accept array types (List, LargeList, FixedSizeList) in addition to string types
  2. Add type detection logic in invoke_with_args - if any argument is an array type, delegate to array_concat_inner
  3. Keep all existing string logic unchanged for backward compatibility

The implementation would be contained within /datafusion/functions/src/string/concat.rs with minimal changes to the existing logic flow.

@comphead Would this approach work?

EeshanBembi avatar Oct 16 '25 18:10 EeshanBembi

Thanks @EeshanBembi it might actually work, would be nice if you create a draft PR and explore how that would work.

comphead avatar Oct 16 '25 19:10 comphead

I played with replacement in the planner, but somehow I dont feel like this is a good idea. @EeshanBembi I have attached a PR just for reference, please feel free to take tests from the PR, tests for both DF concat and Spark should pass(they are slightly different)

comphead avatar Oct 17 '25 00:10 comphead

@comphead Thanks for the reference PR! After reviewing #18105, I believe the function-level approach is better:

  1. Cleaner Architecture: Logic stays within the function rather than spreading across the planner
  2. No Planner Complexity: Avoids conditional function replacement during planning
  3. Better Performance: No overhead during function resolution
  4. Simpler Testing: All concat behavior testable in one place

The implementation is complete with full test coverage and 100% backward compatibility. Creating PR now with the working solution.

EeshanBembi avatar Oct 17 '25 17:10 EeshanBembi

I agree, please tag me in the PR once it has passed the CI and ready for review

comphead avatar Oct 18 '25 17:10 comphead

  • The concat builtin function is currently producing incorrect output when used with arrays.
  • Instead of merging arrays element-wise (like array_concat does), it is simply concatenating their string representations. Example select concat(make_array(1, 2, 3), make_array(4, 5)); output [1, 2, 3][4, 5] correct behavior select array_concat(make_array(1, 2, 3), make_array(4, 5)); output [1, 2, 3, 4, 5]
  • The concat builtin is treating arrays as strings rather than array objects.
  • It performs string concatenation ("[1,2,3]" + "[4,5]") instead of array concatenation.

ruthvikgowda814-bot avatar Nov 30 '25 08:11 ruthvikgowda814-bot

Hey @ruthvikgowda814-bot This is fixed in PR #18137 (branch EeshanBembi:feature/concat-array-support) Just tested and concat now returns [1, 2, 3, 4, 5] correctly. Did you test using this branch?

EeshanBembi avatar Nov 30 '25 16:11 EeshanBembi

Hi! I’d really like to work on this issue. From what I understand, the problem is that when concat() receives array arguments, it currently concatenates their string representations instead of behaving like array_concat() and returning a properly merged flat array.

I’m planning to review the concat function’s dispatch logic and add a check to ensure array-type inputs are handled the right way.

Could I please be assigned this issue? Thanks!

KARTIK64-rgb avatar Feb 21 '26 18:02 KARTIK64-rgb

Perhaps can check with @EeshanBembi if they still plan to continue with the current PR for this issue

  • #18137

I would suggest taking a look at it as there are things we encountered that make this non-trivial to fix

Jefffrey avatar Feb 23 '26 02:02 Jefffrey

sure

KARTIK64-rgb avatar Feb 23 '26 10:02 KARTIK64-rgb

take

1fanwang avatar Sep 24 '26 17:09 1fanwang

untake

1fanwang avatar Sep 24 '26 18:09 1fanwang