[MDEV-25151] JSON_TABLE: Unexpectedly padded values in a PATH column Created: 2021-03-15  Updated: 2021-04-21  Resolved: 2021-03-18

Status: Closed
Project: MariaDB Server
Component/s: JSON
Affects Version/s: N/A
Fix Version/s: 10.6.0

Type: Bug Priority: Major
Reporter: Elena Stepanova Assignee: Alexey Botchkov
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-17399 Add support for JSON_TABLE Closed

 Description   

bb-10.6-mdev17399-psergey2 8b533cc1d5

MariaDB [test]> select a, b from json_table('["foo","bar"]','$[*]' columns (a char(8) path '$', b for ordinality)) t;
+--------------+------+
| a            | b    |
+--------------+------+
| foo         |    1 |
| bar         |    2 |
+--------------+------+
2 rows in set (0.001 sec)

The result already looks damaged. Looking into it further,

MariaDB [test]> select hex(a), b from json_table('["foo","bar"]','$[*]' columns (a char(8) path '$', b for ordinality)) t;
+--------------------------+------+
| hex(a)                   | b    |
+--------------------------+------+
| 666F6F202020202001000000 |    1 |
| 626172202020202002000000 |    2 |
+--------------------------+------+
2 rows in set (0.001 sec)

I consider it totally unexpected.

SQL mode doesn't enforce any padding (and even if it did, it should have been different):

MariaDB [test]> select @@sql_mode;
+-------------------------------------------------------------------------------------------+
| @@sql_mode                                                                                |
+-------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------+
1 row in set (0.000 sec)

MySQL doesn't do anything like this:

MySQL 8.0.23

MySQL [test]> select a, b from json_table('["foo","bar"]','$[*]' columns (a char(8) path '$', b for ordinality)) t;
+------+------+
| a    | b    |
+------+------+
| foo  |    1 |
| bar  |    2 |
+------+------+
2 rows in set (0.001 sec)
 
MySQL [test]> select hex(a), b from json_table('["foo","bar"]','$[*]' columns (a char(8) path '$', b for ordinality)) t;
+--------+------+
| hex(a) | b    |
+--------+------+
| 666F6F |    1 |
| 626172 |    2 |
+--------+------+
2 rows in set (0.001 sec)

Also, it doesn't happen on MariaDB either, if there is no 2nd column:

bb-10.6-mdev17399-psergey2 8b533cc1d5

MariaDB [test]> select a from json_table('["foo","bar"]','$[*]' columns (a char(8) path '$')) t;
+------+
| a    |
+------+
| foo  |
| bar  |
+------+
2 rows in set (0.001 sec)
 
MariaDB [test]> select hex(a) from json_table('["foo","bar"]','$[*]' columns (a char(8) path '$')) t;
+--------+
| hex(a) |
+--------+
| 666F6F |
| 626172 |
+--------+
2 rows in set (0.001 sec)



 Comments   
Comment by Elena Stepanova [ 2021-03-17 ]

This is an MTR version:

SET NAMES utf8;
select hex(a), b from json_table('["foo","bar"]','$[*]' columns (a char(8) path '$', b for ordinality)) t;

Generated at Thu Feb 08 09:35:32 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.