Skip to content

Conversation

nielsdos
Copy link
Member

While at it, also add VECTOR.

@nielsdos nielsdos 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!
Copy link
Contributor

@mvorisek mvorisek Oct 11, 2025

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

Choose a reason for hiding this comment

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

Copy link
Member Author

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

Copy link
Contributor

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?

Copy link
Contributor

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:

  1. in MariaDB, when format=json is set (if and only if the type is JSON internally), the type is JSON in reality
  2. in MySQL and MariaDB JSON is always exported as string (encoded JSON)
  3. JSON type is supported by both MySQL and MariaDB

@nielsdos nielsdos marked this pull request as ready for review October 11, 2025 23:44
@kamil-tekiela
Copy link
Member

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

@nielsdos
Copy link
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
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

3 participants