-
Notifications
You must be signed in to change notification settings - Fork 8k
Fix GH-20122: getColumnMeta() for JSON-column in MySQL #20143
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
base: PHP-8.3
Are you sure you want to change the base?
Conversation
While at it, also add VECTOR.
$stmt = $db->query('SELECT * from test'); | ||
$meta = $stmt->getColumnMeta(0); | ||
|
||
// Note: JSON is an alias for LONGTEXT on MariaDB! |
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.
While this is true, the behaviour is not 100% the same, for example encoding JSON field as JSON does not add quotes when the value is not a string.
repro: https://dbfiddle.uk/YilhsCEk
JSON type must be preserved
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.
That's not possible because mariadb protocol does not make the distinction in its metadata
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.
@vuvova do you have an idea if the distinction of a JSON field is somehow possible?
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.
You can even try this in a MariaDB CLI session to see it's even stored like LONGTEXT:
MariaDB [(none)]> create database testdb;
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> use testdb;
Database changed
MariaDB [testdb]> create table test(a JSON);
Query OK, 0 rows affected (0.032 sec)
MariaDB [testdb]> describe test;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| a | longtext | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
1 row in set (0.002 sec)
MariaDB [testdb]>
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.
MariaDB internally checks the JSON type like in https://github.com/MariaDB/server/blob/mariadb-12.1.1/sql/item_jsonfunc.cc#L1848.
Then I found https://github.com/MariaDB/server/blob/mariadb-12.1.1/sql/sql_type_json.h#L86 - is this really sent in the protocol?
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.
While it's true that the format is sent over as "json", it sends LONGTEXT in its type field, making an exception to this and transforming this to JSON is lying. See also https://mariadb.com/docs/server/reference/data-types/string-data-types/json
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.
In atk4/data we need to detect the type is JSON - can https://github.com/MariaDB/server/blob/mariadb-12.1.1/sql/sql_type_json.h#L86 be accessed right now or can it be make accessible either by returning JSON here or via some other extended field?
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.
I would say "lying" would be correct here:
- in MariaDB, when
format=json
is set (if and only if the type is JSON internally), the type is JSON in reality - in MySQL and MariaDB JSON is always exported as string (encoded JSON)
- JSON type is supported by both MySQL and MariaDB
As far as I know, there is no JSON datatype in MySQL protocol. Has something changed recently? |
The JSON type was introduced in Mysql 5.7.8: https://dev.mysql.com/doc/refman/5.7/en/json.html but it's mainly treated as a string. |
I cannot set up PDO_MYSQL testing environment for some reason, but I looked into this topic more and it seems I was confused before. The JSON type seems to be present in the protocol and so it can be reported as you showed. I think I confused it with ENUM. This line can also be added in
|
While at it, also add VECTOR.