-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
GROUP BY support for jsonb array values #35848
Comments
I just confirmed that this does not work on master either. The inserts above didn't work for me until I modified them: INSERT
INTO
computed_pipelines (labels)
VALUES
(
'[{"key": "group", "value": "a"}, {"key": "group", "value": "b"}]'
); INSERT
INTO
computed_pipelines (labels)
VALUES
(
'[{"key": "group", "value": "a"}, {"key": "group", "value": "c"}]'
); INSERT
INTO
computed_pipelines (labels)
VALUES
(
'[{"key": "group", "value": "a"}, {"key": "group", "value": "b"}]'
); What's happening here @justinj ? |
Oh sorry, I didn't test the insert query, just did them from the top of my head. I removed the incorrect braces. But can probably be merged into the following: INSERT INTO
computed_pipelines (labels)
VALUES
(
'[{"key": "group", "value": "a"}, {"key": "group", "value": "b"}]',
'[{"key": "group", "value": "a"}, {"key": "group", "value": "c"}]',
'[{"key": "group", "value": "a"}, {"key": "group", "value": "b"}]'
); But the inserts themselves are not really the issue here, just included them to illustrate my point :) |
The problem is what @knz describes in the linked issue which is more about the implicit lateral syntax than it is correlated subqueries. Here's a somewhat gnarly query that I think does what you want:
You're right that #28059 is also a blocker to making this query a bit nicer. |
Here is the |
Cool the nasty query works! For making sure i'm actually unnesting an array I need to check the jsonb type, so the most inner select query becomes: (SELECT id, jsonb_array_elements(labels) AS l FROM computed_pipelines where jsonb_typeof(labels) = 'array') Should we keep this open until the dependencies are implemented? Or close it so it doesn't bloat your backlog? |
I think at the very least we should integrate this query into the QA / unit test suite when the dependencies are implemented. I'm not sure how to best do this -- should the issue remain open so that the future implementor can look at it? @jordanlewis please advise. |
We have marked this issue as stale because it has been inactive for |
The limitation here has been lifted. The OP's query still doesn't work, but that's because you're not allowed to reference an aliased group by column in a |
I'm trying to do a group by query using values in a JSONB array. What I'm trying to figure out what features still need to be implemented to do what's show below and what alternative to use in the meantime.
Describe the solution you'd like
I'd like the select query above to return the following result:
Instead it returns
no data source matches prefix: c
which is due to correlated subqueries not being supported yet, see #24676Describe alternatives you've considered
Just retrieving the values from the array without grouping works fine with
As soon as I try
It returns
column "key" does not exist
. I think (partially) due to the fact that using aliases in group by, etc isn't supported yet. See #28059.And if I try the following to work around this
It fails with
jsonb_array_elements(): generator functions are not allowed in GROUP BY
. On this topic I found closed issue #10520.An alternative I though about as well is to store the result of the non-grouped query in a temporary table, but for that #5807 needs to be implemented.
Are there any other features that need to be implemented before the original select query is possible? And what are the alternatives currently? Right now I don't group, count and sort in my query, but have to do it in code instead.
The text was updated successfully, but these errors were encountered: