MDEV-42268 CHR(NULL) returns an empty string instead of NULL in a view or a generated column, because CHR() is printed as CHAR(... USING ...) #6192
All Vouch review confidence checks passed
Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #6192"
general
weight: 0.35
gkodinov
Description
Item_func_chr inherited Item_func_char::print(), so the stored
definitions of views, generated columns, DEFAULT and CHECK turned
CHR(x) into CHAR(x USING cs). CHAR skips NULL arguments and returns
an empty string, while CHR returns NULL. The printed form also kept
only the character set, not the collation that CHR takes from
@@collation_databasewhen the expression is created.Print CHR as mariadb_schema.chr(x,_binary'collation'). The parser
recognizes this qualified two-argument form and builds the native
Item_func_chr with that exact collation. A reopened definition thus
keeps the NULL handling, byte validation, collation, coercibility and
single evaluation of the argument. Unqualified CHR still takes
exactly one argument.
Item_func_chr::eq() also compares collations, so the optimizer no
longer reuses a CHR expression with a different collation, for
example from the select list in ORDER BY.
Servers without this fix cannot parse the new stored form. They
report ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT when loading such a
definition, for example from a mariadb-dump file.
Printed output that changes:
EXPLAIN EXTENDEDnotes show the new form too.Why a new stored form
I could not find any text that an unpatched server parses into exactly
CHR's behaviour:chr(x): the collation is rebound when the definition is reparsed. Generated columns,DEFAULTandCHECKuse whichever database is current in the session that opens the table. Views use the view's database, which can differ from the database that was current at creation.CHAR(x USING cs): it keeps only the character set's primary collation, and it skipsNULL.COLLATE: it changes the coercibility to 0.CONVERTorCAST: they validate bytes differently. For example, byteFFunder utf8mb4 returns3Finstead of an empty string orNULL.IForNULLIFwrappers: they evaluate the argument twice.CHR(NEXT VALUE FOR s)would consume two sequence values./M!110415 ... /: they cannot tell a patched build from an unpatched build with the same version number.This follows the approach of MDEV-27744, which prints
mariadb_schema-qualified names in stored definitions.Compatibility limitations
char(x using cs)and keep the old behaviour. The originalCHRcannot be recovered from that text, so these objects must be re-created from their original DDL. This patch does not reinterpret existingCHAR(...)definitions, because they may have been written asCHARon purpose.ER_WRONG_PARAMCOUNT_TO_NATIVE_FCTfor the new form. This affects downgrades, loading amariadb-dumpfile made on a patched server, and row-based replication ofCREATE TABLE ... SELECTto an older replica. The failure is loud: no object is created with wrong values.Release Notes
CHR()in views, generated columns,DEFAULTandCHECKconstraints now returnsNULLfor aNULLargument and keeps the collation it had at creation time. Such definitions are now stored and shown bySHOW CREATEasmariadb_schema.chr(x,_binary'collation'). Servers without this fix cannot load them, including frommariadb-dumpoutput. Objects created before upgrading keep the old behaviour until they are re-created.How can this PR be tested?
The new test case in
main.func_strcovers:latin1_bin) over aucs2connection, then reopened afterFLUSH TABLES:NULLstaysNULL, byteE9survives, and the collation stayslatin1_binSHOW CREATE TABLEoutputORDER BYwith a differentCHRcollation (a, B)utf8mb4_uca1400_ai_cidatabase, including an invalid byteNULL, integer, or unknown collation argument, and for unqualifiedchr(x, 'collation')I ran these locally on an 11.4.15 Linux debug build (WSL):
main.func_str,main.view,main.mysqldump,main.ctype_ucs,main.ctype_uca,main.ctype_utf8,main.ctype_utf8mb4,main.ctype_latin1,main.check_constraint,main.default,vcol.vcol_misc,vcol.vcol_syntax,compat/oracle.func_misc,rpl.rpl_charset,sql_sequence.otherand others all passed.mariadb-dumpfrom DDL that uses plainCHR, with generated columns and views created from both the same and another database. Restored on the patched server, it read back the same as the original:NULL,HEXvalues, collation, and coercibility 4. Restoring the same file on an unpatched 11.4.15 or 13.3.0 server failed withER_WRONG_PARAMCOUNT_TO_NATIVE_FCT, and no objects were created.CHR(): storedCHRmatched directCHR()for an invalid byte in both strict and permissive mode, forDEFAULTandCHECK, for sorting through a view, and for a sequence argument.Basing the PR against the correct MariaDB version
PR quality check