Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ext/pdo_mysql/mysql_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,12 @@ static char *type_to_name_native(int type) /* {{{ */
PDO_MYSQL_NATIVE_TYPE_NAME(DATE)
#ifdef FIELD_TYPE_NEWDATE
PDO_MYSQL_NATIVE_TYPE_NAME(NEWDATE)
#endif
#ifdef FIELD_TYPE_VECTOR
PDO_MYSQL_NATIVE_TYPE_NAME(VECTOR)
#endif
#ifdef FIELD_TYPE_JSON
PDO_MYSQL_NATIVE_TYPE_NAME(JSON)
#endif
PDO_MYSQL_NATIVE_TYPE_NAME(TIME)
PDO_MYSQL_NATIVE_TYPE_NAME(DATETIME)
Expand Down
33 changes: 33 additions & 0 deletions ext/pdo_mysql/tests/gh20122.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
GH-20122 (getColumnMeta() for JSON-column in MySQL)
--EXTENSIONS--
pdo
pdo_mysql
--SKIPIF--
<?php
require __DIR__ . '/config.inc';
require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
PDOTest::skip();
?>
--FILE--
<?php
require __DIR__ . '/config.inc';
require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
$db = PDOTest::test_factory(__DIR__ . '/common.phpt');

$db->exec('CREATE TABLE test (bar JSON)');
$db->exec('INSERT INTO test VALUES("[]")');

$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

echo $meta['native_type'], "\n";
?>
--CLEAN--
<?php
require __DIR__ . '/mysql_pdo_test.inc';
MySQLPDOTest::dropTestTable();
?>
--EXPECTF--
%r(JSON|LONGTEXT)%r
Loading