Allow union with empty array

This is a fairly annoying interaction that I’ve been running into quite a bit:

Union(
  Map(
    Select(
      "data",
      Paginate(Match("index_1"))
    ),
    Lambda(
      "ref",
      Match("index_2", Var("ref"))
    )
  )
)

This works great unless index_1 is empty, which means I always has to come with a Let and an If:

Let(
  {
    elems: ...
  },
  If(
    IsEmpty(Var("elems")),
    Var("elems"),
    Union(Var("elems"))
  )
)

I’d propose that Union should just work with an empty array. I do see a problem with determining whether to return an array, set or page, but it would be a great QOL improvement.

Union does work with an empty array:

> Union([1, 2, 3], [4, 5, 6])
[ 1, 2, 3, 4, 5, 6 ]
> Union([], [4, 5, 6])
[ 4, 5, 6 ]
> Union([1, 2, 3], [])
[ 1, 2, 3 ]

What does the result of your Map look like?

Sorry, maybe my wording was unclear. Indeed, Union does take empty arrays as operands for a union operation.

With Map being the only parameter, it works great when index_1 is not empty:

Union([
  Var("lambda_result_1"),
  Var("lambda_result_2"),
  ...
])

However, this effectively turns into a call with no operands when index_1 is empty:

Union([])

This will fail with Non-empty array expected.

You could try using this:

Union([], Map(...))

Since this works:

> Union([], [])
[]

That would be incorrect if there is data:

> Union([], [[1,2],[3,4]])
[[1,2],[3,4]]

The correct result should be:

> Union([[1,2],[3,4]])
[1,2,3,4]

Excellent point.

Your suggestion is a good one, and your Let(..., If()) handling seems like a good workaround for now.