-
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
Open
nielsdos
wants to merge
1
commit into
php:PHP-8.3
Choose a base branch
from
nielsdos:fix-20122
base: PHP-8.3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+39
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! | ||
echo $meta['native_type'], "\n"; | ||
?> | ||
--CLEAN-- | ||
<?php | ||
require __DIR__ . '/mysql_pdo_test.inc'; | ||
MySQLPDOTest::dropTestTable(); | ||
?> | ||
--EXPECTF-- | ||
%r(JSON|LONGTEXT)%r |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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:
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:
format=json
is set (if and only if the type is JSON internally), the type is JSON in reality