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

Support concatenate (||) for jsonb arrays #12143

Closed
ly9chee opened this issue Sep 7, 2023 · 5 comments
Closed

Support concatenate (||) for jsonb arrays #12143

ly9chee opened this issue Sep 7, 2023 · 5 comments

Comments

@ly9chee
Copy link
Contributor

ly9chee commented Sep 7, 2023

Is your feature request related to a problem? Please describe.

select '["1","2","3"]'::jsonb::varchar[];
this sql will cause a bind error

Describe the solution you'd like

No response

Describe alternatives you've considered

No response

Additional context

No response

@github-actions github-actions bot added this to the release-1.2 milestone Sep 7, 2023
@xxchan
Copy link
Member

xxchan commented Sep 8, 2023

You may try

select array_agg(x) from jsonb_array_elements_text( '["1","2","3"]'::jsonb) as t(x);

@ly9chee
Copy link
Contributor Author

ly9chee commented Sep 8, 2023

@xxchan Hi, thanks for your reply! My actual needs is to concatenate two json array into a single array, just like bellow:

SELECT x || y as concated
FROM (
    VALUES ('["1","2","3"]'::jsonb, '["4","5","6"]'::jsonb)
) AS t (x, y);

the expected result is:
["1","2","3","4","5","6"]
but I found currently only normal array can do the concatenation (through || operator) so I'm wondering if there is a more convenient approach to doing this.

according to the example you provided above, the sql should change to:

select array_agg(u.a) || array_agg(u.b) as concated from (
  select jsonb_array_elements_text(x) as a, jsonb_array_elements_text(y) as b
    from (
        values ('["1","2","3"]'::jsonb, '["4","5","6"]'::jsonb)
    ) as t (x, y)
) as u;

it looks like more verbose(and maybe it is not performant? I think).
it is very common to do a concat with two json array in business logic, so is there a better way to accomplish this?

@xxchan
Copy link
Member

xxchan commented Sep 8, 2023

We can support the || operator (#12180), which concatenates 2 jsonb values to a new jsonb.

-- in Postgres
xxchan@/tmp:postgres> select  '["a", "b"]'::jsonb || '["c", "d"]'::jsonb;

+----------------------+
| ?column?             |
|----------------------|
| ["a", "b", "c", "d"] |
+----------------------+

Does it fit your needs?

@ly9chee
Copy link
Contributor Author

ly9chee commented Sep 8, 2023

exactly! I would be appreciate if this || operator could be supported

@xxchan xxchan changed the title Support converting jsonb array to varchar[] Support concatenate (||) for jsonb Sep 8, 2023
@xxchan xxchan changed the title Support concatenate (||) for jsonb Support concatenate (||) for jsonb arrays Sep 8, 2023
@xiangjinwu
Copy link
Contributor

will be tracked by #12180

@xiangjinwu xiangjinwu closed this as not planned Won't fix, can't repro, duplicate, stale Sep 11, 2023
@fuyufjh fuyufjh modified the milestones: release-1.2, release-1.3 Sep 11, 2023
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

4 participants