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

sql: jsonb comparator operators are not implemented #49144

Closed
timgraham opened this issue May 16, 2020 · 11 comments · Fixed by #104293
Closed

sql: jsonb comparator operators are not implemented #49144

timgraham opened this issue May 16, 2020 · 11 comments · Fixed by #104293
Labels
A-sql-typing SQLtype inference, typing rules, type compatibility. A-tools-prisma C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. docs-done docs-known-limitation O-community Originated from the community T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) X-blathers-triaged blathers was able to find an owner

Comments

@timgraham
Copy link
Contributor

timgraham commented May 16, 2020

Describe the problem

A clause like ("jsontest"."value" -> 'c') > '2' crashes (but works on PostgreSQL).

To Reproduce

CREATE TABLE jsontest (
    value jsonb
);
INSERT INTO jsontest VALUES ('{"c": 2}');
INSERT INTO jsontest VALUES ('{"c": 2.5}');
INSERT INTO jsontest VALUES ('{"c": 3}');
SELECT * FROM jsontest WHERE ("jsontest"."value" -> 'c') > '2';
SELECT * FROM jsontest WHERE ("jsontest"."value" -> 'c') > '2.33';

Expected behavior

Expected result (for both select queries):

{"c": 2.5}
{"c": 3}

Actual results:

ERROR: unsupported comparison operator: <jsonb> > <string>
SQLSTATE: 22023

Environment:

  • CockroachDB version 20.1.0

Django 3.1 (to be released in August 2020) adds support for JSONField and this issue came up in a failing test.

Jira issue: CRDB-4253

@blathers-crl
Copy link

blathers-crl bot commented May 16, 2020

Hello, I am Blathers. I am here to help you get the issue triaged.

Hoot - a bug! Though bugs are the bane of my existence, rest assured the wretched thing will get the best of care here.

I have CC'd a few people who may be able to assist you:

If we have not gotten back to your issue within a few business days, you can try the following:

  • Join our community slack channel and ask on #cockroachdb.
  • Try find someone from here if you know they worked closely on the area and CC them.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan.

@blathers-crl blathers-crl bot added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-community Originated from the community X-blathers-triaged blathers was able to find an owner labels May 16, 2020
@jordanlewis jordanlewis changed the title sql: json field extract comparison crashes: "unsupported comparison operator: <jsonb> > <string>" sql: json field extract comparison return error: "unsupported comparison operator: <jsonb> > <string>" Jun 3, 2020
@jordanlewis
Copy link
Member

This would be fixed by moving toward a Postgres-style implicit cast model. We could also add a quick fix for it. @timgraham do you think this is a really useful thing, or is it one of those things that comes up just in tests?

@timgraham
Copy link
Contributor Author

I don't think this case deserves special priority over similar issues (cockroachdb/django-cockroachdb#19, cockroachdb/django-cockroachdb#20, etc) that may be fixed by the more general solution you described.

@otan
Copy link
Contributor

otan commented Aug 7, 2020

EDIT: nvm, misread this one!

@otan otan closed this as completed Aug 7, 2020
@otan otan reopened this Aug 7, 2020
@rafiss rafiss added the A-sql-typing SQLtype inference, typing rules, type compatibility. label Mar 15, 2021
@rafiss rafiss added the T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) label May 12, 2021
@otan
Copy link
Contributor

otan commented Oct 12, 2021

ah this is slightly more than i expected: we don't support jsonb comparison!

[email protected]:5436/json_path_multi_filtering_pg_json> select '{"a": 1}'::jsonb < '{"b": 2}'::jsonb;
ERROR: unsupported comparison operator: <jsonb> < <jsonb>
SQLSTATE: 22023

Prisma relies on this too, but I am skipping those tests.

@otan otan changed the title sql: json field extract comparison return error: "unsupported comparison operator: <jsonb> > <string>" sql: jsonb comparator operators are not implemented Oct 12, 2021
@pbiggar
Copy link

pbiggar commented Jan 20, 2022

I'm evaluating cockroachDB as a new DB for darklang. We use jsonb to store all user data, and then compile users' queries -- written in darklang -- to SQL. (Blog post on that here: https://blog.darklang.com/compiling-dark-to-sql/, and the implementation is here https://github.com/darklang/dark/blob/3e6fd9959e0ec9e87bce1f6033386e9c44d81c53/fsharp-backend/src/LibBackend/SqlCompiler.fs).

To be able to switch over, we would need to support < and > operators in jsonb, as numerical comparisons are a pretty common use case in queries.

@saurik
Copy link

saurik commented Jan 23, 2022

@pbiggar I would expect you are extracting a specific field and comparing that--to perform a "numerical comparison"--not comparing the JSON object values themselves, and so you should not need this feature?

@pbiggar
Copy link

pbiggar commented Jan 24, 2022

I would expect you are extracting a specific field and comparing that--to perform a "numerical comparison"--not comparing the JSON object values themselves, and so you should not need this feature?

We need to make the sort of query listed in this issues description:

SELECT * FROM jsontest WHERE ("jsontest"."value" -> 'c') > '2';

We also allow this sort of query:

SELECT * FROM jsontest WHERE ("jsontest"."value" -> 'c') > ("jsontest"."value" -> 'd');

We do not support (or intend to support) this sort of query:

select '{"a": 1}'::jsonb < '{"b": 2}'::jsonb;

@saurik
Copy link

saurik commented Jan 26, 2022

@pbiggar But you said you wanted to do a "numerical comparison". The reason those queries don't work is because, from a type perspective, jsontest.value->'c' might return anything, including an object or an array, and due to this issue there is no way to compare arbitrary jsonb values.

But, if you want to just extract fields whose type you know, this works fine (though it would be preferable, to me at least, to be able to directly cast a jsonb to a decimal instead of having to first get its value as a string, but that's a different issue from being unable to compare jsonb values):

xxx@:26257/defaultdb> SELECT * FROM jsontest WHERE ("jsontest"."value" ->> 'c')::decimal > 2;
    value
--------------
  {"c": 2.5}
  {"c": 3}
(2 rows)

@pbiggar
Copy link

pbiggar commented Jan 26, 2022

@saurik Ah, I see! Yes, that's exactly what I need. Sounds like it's already supported. I always know the types and the sql I generate already has appropriate type-casts. Thanks!

@timgraham
Copy link
Contributor Author

The case in the issue description works in CockroachDB 23.2 nightly. (Not sure if it was fixed in a duplicate issue or as a side effect of another change, possibly 5cdb625.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-typing SQLtype inference, typing rules, type compatibility. A-tools-prisma C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. docs-done docs-known-limitation O-community Originated from the community T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) X-blathers-triaged blathers was able to find an owner
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants