Skip to content

Fix GH-20122: getColumnMeta() for JSON-column in MySQL - #20143

Closed
ndossche wants to merge 2 commits into
php:PHP-8.3from
ndossche:fix-20122
Closed

ndossche wants to merge 2 commits into
php:PHP-8.3from
ndossche:fix-20122

Conversation

@ndossche

Copy link
Copy Markdown
Member

While at it, also add VECTOR.

@ndossche ndossche linked an issue Oct 11, 2025 that may be closed by this pull request
$stmt = $db->query('SELECT * from test');
$meta = $stmt->getColumnMeta(0);

// Note: JSON is an alias for LONGTEXT on MariaDB!

@mvorisek mvorisek Oct 11, 2025 •

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Member Author

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

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Member Author

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]> 

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay got it, thanks for the info

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation of the string change should still only be done on master. So I'll make a reminder issue to implement sending the flag + incorporating the string MariaDB sends us.

@mvorisek mvorisek Oct 15, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is going to be changed for MySQL, I do not see any reason to not change it for MariaDB. It is more or less transport protocol detail.

I consider this as a bug as JSON type is present almost in every DB since about y2020 and the type info is important to handle the data correctly, as reproduced above.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSON type was absent on both MariaDB & MySQL.
Other types may change if we send the flag and use the string the server sends back. This part is the BC break.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In PHP it shouldn't replace the old information, it should be presented as additional information too. If it is to replace the native type then it should be behind a user facing option and disabled by default.

@ndossche
ndossche marked this pull request as ready for review October 11, 2025 23:44
@kamil-tekiela

Copy link
Copy Markdown
Member

As far as I know, there is no JSON datatype in MySQL protocol. Has something changed recently?

@ndossche

Copy link
Copy Markdown
Member Author

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.

@kamil-tekiela

Copy link
Copy Markdown
Member

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 mysqli_fetch_field_types.phpt:

        MYSQLI_TYPE_JSON => array('JSON', '[]'),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

::getColumnMeta() for JSON-column in MySQL

4 participants