-
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: allow cast from jsonb to numbers and some jsonb type coercion #39495
Comments
I like this idea! We should continue to investigate here. cc @RaduBerinde |
Does Postgres permit this? Such a change is not too difficult. |
@awoods187 please check for us if you don't mind :) |
Something to consider is that providing auto-casts will fool users into believing this is something they should do, whereas neither the CBO nor the execution machinery optimize well for JSONB scalars. I personally like the fact that currently a CockroachDB user can reliably assume that if a cast is needed, chances are the query execution will be slow, and that instead they should seek to modify their SQL schema to ensure casts are not necessary. |
Agreed. We can agree surely that implementing this as an explicit cast would be a good place to start! I'm generally happy with our current philosophy of providing implicit casts only when resolving constants - I don't think we need to consider changing that right now. |
One can argue that extracting fields from a JSON is analogous to untyped constants - you don't know the type you're getting, so it wouldn't be unreasonable to assume/coerce it to be what the context of its usage implies.
You're referring to the fact that there's no support for joining the |
I think @knz is pointing out that introducing implicit casts would mean we can't use an inverted index. For example, consider this hypothetical case (doesn't work today since
If we implicitly convert the That said, I think it'd be theoretically possible to cast the
Notice that there would be no way to return both rows at once using an equality condition, because we're purposefully preserving the data types of the CC @justinj |
The support for explicit cast is covered by #41367 |
This is done now |
We allow casting string to int, but not jsonb to int. I think we should allow it (and error when the jsonb is not a trivial int).
This seems important to me because it takes away the need to understand the difference between the
->
and->>
operators (the first returns jsonb, the former string). As things stand, I need to know about the->>
operator whenever I want to then further cast the value. And casting is needed if I'm ever going to join a number to some other column.Which brings us to the the next peeve. It seems unfortunate that, if I want to get a number out of a jsonb, I need a cast at all. Perhaps our typing rules could allow comparisons of a jsonb with a numeric type (by doing an implicit cast). Is that feasible? I know we have some type coercion rules; would it be feasible to expand them in this way?
Once we start supporting JSON Schema for typing our JSON columns, presumably some of this becomes easier.
As a motivating example, here's a simple JSON and some hoops I've had to jump through to join it with a numeric column. Notice the
->>
operator and the::int
cast. Together, they made JSONB a bit unfriendly.cc @knz @andy-kimball
Jira issue: CRDB-5574
The text was updated successfully, but these errors were encountered: