Cockroach: sql: the result column name for json_array_elements should be "value"

Created on 25 Oct 2019  路  9Comments  路  Source: cockroachdb/cockroach

Found by @drewdeally

 with a_table(jdata) as (
values (
    '{
        "uid":14105529,
        "o":[
            {"mid":6551,"ac":1913,"ip":"144.36.233.44","adw":5,"at":133000,"ad":151015,"aid":0},
            {"mid":7552,"ac":1913,"ip":"144.36.233.44","adw":5,"at":133000,"ad":151015,"aid":0},
            {"mid":7553,"ac":1913,"ip":"144.36.233.44","adw":5,"at":133000,"ad":151015,"aid":0}
        ] }'::jsonb
    )
)
select jdata->'uid' as uid, value
from a_table, jsonb_array_elements(jdata->'o')
where (value->>'mid')::int > 7000;

error:

pq: column "value" does not exist

This is because the result column for json_array_elements is not called "value" by default, unlike in pg.

Workaround is to use jsonb_array_elements(jdata->'o') as value

A-sql-builtins A-sql-optimizer A-sql-pgcompat C-bug E-easy good first issue

Most helpful comment

We'll probably want to backport the fix, ideally it would be a separate commit before the rest of the refactoring.

All 9 comments

Might be worthwhile to also audit other built-ins for mismatches from pg.

cc @RaduBerinde @asubiotto for triage

Looking at https://www.postgresql.org/docs/12/functions-json.html I see the json[b]_array_elements[_text] variations that return a column name that doesn't match the function.

cc @giorgosp -- If you can fix this easily as part of #41861 that would be great. Otherwise, I'll fix it after you merge that PR.

Yes @rytaft I will look into it

We'll probably want to backport the fix, ideally it would be a separate commit before the rest of the refactoring.

Basically, when a json function is in a SELECT, postgres uses the function's name as the column's label. But when the function is in a FROM , "value" is used as the column's label.
https://github.com/postgres/postgres/blob/5264d9154178d3aeaa0359b43a450298a7ce7281/src/test/regress/expected/json.out#L804

How do we want to handle this? Do we want to implement the same exact behavior?

Yeah we need to do the same (for compatibility).

Was this page helpful?
0 / 5 - 0 ratings