-
Notifications
You must be signed in to change notification settings - Fork 596
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
fix: support string to number type in JSONB_POPULATE_RECORD #19937
Conversation
Is decimal excluded here by design? |
src/common/src/types/jsonb.rs
Outdated
if let Some(s) = self.0.as_str() { | ||
F64::from_str(s).map_err(|e| format!("{e}")) | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep as_number
at its current behavior and let its caller (JSONB_POPULATE_RECORD
) to do more work.
Besides JSONB_POPULATE_RECORD
, or actually before JSONB_POPULATE_RECORD
is supported, this as_number
is intended to support CAST
. In PostgreSQL:
test=# select ('{"f1":4}'::jsonb -> 'f1')::int;
int4
------
4
(1 row)
test=# select ('{"f1":"4"}'::jsonb -> 'f1')::int;
ERROR: cannot cast jsonb string to type integer
That is, a JSON string shall NOT be CAST
ed into integer. But it can be POPULATE
ed into integer:
test=# select jsonb_populate_record(row(null::int), '{"f1":4}');
jsonb_populate_record
-----------------------
(4)
(1 row)
test=# select jsonb_populate_record(row(null::int), '{"f1":"4"}');
jsonb_populate_record
-----------------------
(4)
(1 row)
This is the PostgreSQL doc on JSONB_POPULATE_RECORD
:
https://www.postgresql.org/docs/17/functions-json.html#id-1.5.8.22.8.13.2.2.9.1.2.1
To convert a JSON value to the SQL type of an output column, the following rules are applied in sequence:
- A JSON null value is converted to an SQL null in all cases.
- If the output column is of type
json
orjsonb
, the JSON value is just reproduced exactly.- If the output column is a composite (row) type, and the JSON value is a JSON object, the fields of the object are converted to columns of the output row type by recursive application of these rules.
- Likewise, if the output column is an array type and the JSON value is a JSON array, the elements of the JSON array are converted to elements of the output array by recursive application of these rules.
- Otherwise, if the JSON value is a string, the contents of the string are fed to the input conversion function for the column's data type.
- Otherwise, the ordinary text representation of the JSON value is fed to the input conversion function for the column's data type.
That is, only null
/jsonb
/object
/array
get special treatment. Other cases (bool
/number
) are always force_str
(function defined below) first and then from_text
(defined here as the input conversion function).
No. According to the PostgreSQL doc I just shared above, all data types having an input conversion function can be supported. |
I hereby agree to the terms of the RisingWave Labs, Inc. Contributor License Agreement.
What's changed and what's your intention?
close #19918
Checklist
Documentation
Release note