[MDEV-12995] JSON_EXTRACT returning string for integer Created: 2017-06-05 Updated: 2017-06-05 Resolved: 2017-06-05 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | JSON |
| Affects Version/s: | 10.2.6 |
| Fix Version/s: | N/A |
| Type: | Bug | Priority: | Major |
| Reporter: | Richard Heisterberg | Assignee: | Sergei Golubchik |
| Resolution: | Not a Bug | Votes: | 0 |
| Labels: | None | ||
| Environment: |
MacOS |
||
| Description |
|
I am trying to understand the correct way to use the JSON_EXTRACT function. It seems that JSON_EXTRACT is returning a string for a json integer. When I execute a SQL query from java (Hibernate 4) the result of JSON_EXTRACT is returned as a java String.
JSON_TYPE correctly returns INTEGER as json type
I tried casting the result of JSON_EXTRACT as an unsigned, and that works;
but when I tried a simple case statement to cast the result of JSON_EXTRACT based on JSON_TYPE, the cast doesn't work and I'm getting the wrong result;
For some reason this works;
Any help would be greatly appreciated, thanks. |
| Comments |
| Comment by Sergei Golubchik [ 2017-06-05 ] |
|
Unfortunately, this is the only way it can possibly work. JSON or not, SELECT statement still returns a table — a collection of rows and columns. Every column has some specific metadata, such a data type. You cannot have a column have one type in one row and a different type in the next row. That's why your trick with CASE...WHEN didn't work. This also explains why the first query didn't return a number. Without looking inside JSON, the server can not know whether the result will be a number. And in that case, it can be different for different rows, so the server has to use the most generic type that will be able to fit any value from JSON_EXTRACT. |