Fix expression defaults being quoted as string literals#695
Merged
Conversation
…as string literals The schema differ was incorrectly quoting expression defaults like json_object() and json_array() as string literals (e.g. DEFAULT 'json_object()') instead of wrapping them in parentheses (DEFAULT (json_object())). This caused MySQL to reject the generated ALTER with errno 1101: "BLOB, TEXT, GEOMETRY or JSON column can't have a default value". Root cause: the Column struct had no way to distinguish expression defaults from literal defaults. The needsQuotes() function treated all unrecognized values (including function calls) as strings that needed quoting. The fix adds a DefaultIsExpr field to Column, set during parsing by inspecting the TiDB AST. Expression defaults are now formatted as DEFAULT (expr) while literal defaults continue to be quoted as DEFAULT 'value'. Also fixes parseExpression stripping trailing () from all zero-arg functions -- now only strips for timestamp-family functions (current_timestamp, now, etc.) where MySQL's canonical form omits the parens. Fixes #542 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Armand Parajon <armand@squareup.com>
…ing table Simulates the real-world scenario where a JSON column with DEFAULT (json_object()) is added to a table that already has multiple columns including other JSON columns. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Armand Parajon <armand@squareup.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Armand Parajon <armand@squareup.com>
morgo
approved these changes
Apr 20, 2026
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.
What's this?
Fixes expression defaults like
json_object(),json_array(), anduuid()being incorrectly quoted as string literals in generated ALTER statements.Before:
ALTER TABLE t1 ADD COLUMN metadata json NOT NULL DEFAULT 'json_object()'(errno 1101)After:
ALTER TABLE t1 ADD COLUMN metadata json NOT NULL DEFAULT (json_object())How it works
DefaultIsExprbool to theColumnstruct to track whether a default is an expression vs literalFuncCallExprnodes (json_object, json_array, uuid, etc.) are flagged as expression defaultsformatColumnDefinitionusesDEFAULT (expr)for expression defaults instead ofDEFAULT 'expr'parseExpressionstripping()from all zero-arg functions — now only strips for timestamp-family functions where MySQL's canonical form omits parensMost of this was written by Claude Code — I just provided direction.