fix MySQL JSON append args for MariaDB#4495
Open
errdefer wants to merge 1 commit intoent:masterfrom
Open
Conversation
Replace CAST(? AS JSON) with JSON_EXTRACT(?, '$') when appending marshaled JSON values in the MySQL sqljson dialect. MariaDB treats JSON as an alias for LONGTEXT and does not support CAST(... AS JSON), causing sqljson.Append on non-primitive values to fail with a syntax error. JSON_EXTRACT(?, '$') parses and returns the root JSON value, preserving the existing object/array append semantics while working on both MySQL and MariaDB.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Replace
CAST(? AS JSON)withJSON_EXTRACT(?, '$')when appending marshaled JSON values in the MySQLsqljsondialect.MariaDB treats
JSONas an alias forLONGTEXTand does not supportCAST(... AS JSON). As a result,sqljson.Appendfails on MariaDB when appending non-primitive values such as objects, maps, structs, or arrays.Using
JSON_EXTRACT(?, '$')parses the argument as JSON and returns the root JSON value, preserving the intended append semantics for marshaled values while working on both MySQL and MariaDB.Problem
Given a schema field that stores a slice of objects:
Appending a non-primitive element on MariaDB:
can generate an update containing
CAST(? AS JSON)and fail with:The failing SQL is generated by the MySQL
sqljsondialect for non-primitive append arguments:This works for MySQL, but not for MariaDB.
Fix
In
dialect/sql/sqljson/dialect.go, update the non-primitive branch of(*mysql).appendArgto emit:JSON_EXTRACT(?, '$')instead of:
CAST(? AS JSON)resulting in:
Before:
After: