-
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
sql: ordering of keys in JSON objects is not identical to Postgres #96507
Comments
This is tough. Postgres is allowed to order the fields however it wants. I don't know why it orders it the way it does, but the values are technically equal according to postgres. This one is annoying.
|
You can create a recursive UDF in Postgres that mimics our JSON->TEXT cast behavior: CREATE FUNCTION jsonb_to_text(j JSONB) RETURNS TEXT LANGUAGE SQL AS $$
SELECT CASE jsonb_typeof(j)
WHEN 'object' THEN (
SELECT '{' || (
SELECT string_agg('"' || key || '": ' || jsonb_to_text(value), ', ')
FROM (SELECT key, value FROM jsonb_each(j) ORDER BY key) tmp
) || '}'
)
ELSE j::TEXT
END
$$;
SELECT jsonb_to_text('{"it is": true, "that": ["this","json"],"is":"in","a weird":"order"}'::JSONB);
-- {"a weird": "order", "is": "in", "it is": true, "that": ["this", "json"]}
SELECT jsonb_to_text('{"it is": true, "that": ["this","json"],"is": {"z": 1, "a": 0}, "a weird":"order"}'::JSONB);
-- {"a weird": "order", "is": {"a": 0, "z": 1}, "it is": true, "that": ["this", "json"]} You can't create a UDF in CRDB that mimics PG's JSON->TEXT cast because we don't yet support recursive UDFs. |
It seems like udacity/pgverify#18 has worked around this issue, so we will not change anything for now. |
this sort of relates to #95434 - in the sense that PG remembers more about the spacing/ordering inside of a JSON object than CRDB does. |
@rafiss It seems like that's only the case for
|
JSON spec says: https://www.json.org/json-en.html We happen to order our keys (because we need stable sort over golang native maps[] -- but note, new parse json implementation allows one to request unsorted cockroach/pkg/util/json/config.go Line 25 in 0d3209c
There is an argument to be made that applications that rely on any order (PG ordering or CRDB ordering) are somewhat broken. if application requires ordering, it should order keys explicitly. Not sure how hard it is to do -- but @mgartner suggestion seems quite nice. |
I agree with this. Key/value pairs in JSON objects have no guaranteed ordering - any observed ordering is an artifact of the specific JSON implementation. An interesting observation is that Postgres orders keys within a JSON object differently than it compares the keys of two objects:
Within the object, the key I think we should close this issue as a "won't fix" or "not a bug". |
I agree - thanks for digging into the details! |
Run this in Postgres and CRDB:
The ordering of the results are different:
This is affecting tools like: https://github.com/udacity/pgverify
Jira issue: CRDB-24156
The text was updated successfully, but these errors were encountered: