Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate more optimal combined array lambda function emulations #16597

Open
lukaseder opened this issue Apr 25, 2024 · 0 comments
Open

Generate more optimal combined array lambda function emulations #16597

lukaseder opened this issue Apr 25, 2024 · 0 comments

Comments

@lukaseder
Copy link
Member

jOOQ 3.20 added support for array lambda functions:

They can be emulated using correlated subqueries that UNNEST and ARRAY_AGG the array in order to use ordinary SQL to MAP (SELECT), FILTER (WHERE), etc.

When combining such array lambda functions, such as ARRAY_MAP(ARRAY_FILTER(...)), the current emulation isn't aware of the combination and produces such beasts (using kotlin):

select(array("1", "2", "3", "4").map { s -> s.cast(INTEGER) } .anyMatch {  i -> i.mod(2).ne(0) })

Produces this monster in PostgreSQL:

select (case
  when (
    select coalesce(
      array_agg(cast(e as int)),
      case
        when array['1', '2', '3', '4'] is not null then cast(array[] as int[])
      end
    )
    from unnest(array['1', '2', '3', '4']) as t (e)
  ) is not null then exists (
    select 1 as "one"
    from unnest((
      select coalesce(
        array_agg(cast(e as int)),
        case
          when array['1', '2', '3', '4'] is not null then cast(array[] as int[])
        end
      )
      from unnest(array['1', '2', '3', '4']) as t (e)
    )) as t (e)
    where mod(e, 2) <> '0'
  )
end)

When it should instead generate this:

select (case
  when array['1', '2', '3', '4'] is not null then exists (
    select 1 as "one"
    from unnest(array['1', '2', '3', '4']) as t (e)
    where mod(cast(e as int), 2) <> 0
  )
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant