← Back to Pull requests

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

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 22, 2026 10:39 UTC
Author

  • The Jira issue number for this PR is: MDEV-42268
  • 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_database when 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:

    -- before
    `c` varbinary(4) GENERATED ALWAYS AS (char(`a` using latin1)) VIRTUAL
    -- after
    `c` varbinary(4) GENERATED ALWAYS AS (mariadb_schema.chr(`a`,_binary'latin1_bin')) VIRTUAL
    

    EXPLAIN EXTENDED notes 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:

  • Bare chr(x): the collation is rebound when the definition is reparsed. Generated columns, DEFAULT and CHECK use 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 skips NULL.

  • Adding COLLATE: it changes the coercibility to 0.

  • CONVERT or CAST: they validate bytes differently. For example, byte FF under utf8mb4 returns 3F instead of an empty string or NULL.

  • IF or NULLIF wrappers: they evaluate the argument twice. CHR(NEXT VALUE FOR s) would consume two sequence values.

  • *Version-gated comments such as /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

  • Objects created before the upgrade: they still contain char(x using cs) and keep the old behaviour. The original CHR cannot be recovered from that text, so these objects must be re-created from their original DDL. This patch does not reinterpret existing CHAR(...) definitions, because they may have been written as CHAR on purpose.

  • Servers without this fix: they report ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT for the new form. This affects downgrades, loading a mariadb-dump file made on a patched server, and row-based replication of CREATE TABLE ... SELECT to an older replica. The failure is loud: no object is created with wrong values.
  • Release Notes

    CHR() in views, generated columns, DEFAULT and CHECK constraints now returns NULL for a NULL argument and keeps the collation it had at creation time. Such definitions are now stored and shown by SHOW CREATE as mariadb_schema.chr(x,_binary'collation'). Servers without this fix cannot load them, including from mariadb-dump output. Objects created before upgrading keep the old behaviour until they are re-created.

    How can this PR be tested?

    ./mtr main.func_str
    

    The new test case in main.func_str covers:

  • a generated column and a view created in another database (latin1_bin) over a ucs2 connection, then reopened after FLUSH TABLES: NULL stays NULL, byte E9 survives, and the collation stays latin1_bin

  • the SHOW CREATE TABLE output

  • ORDER BY with a different CHR collation (a, B)

  • single evaluation with a sequence

  • a utf8mb4_uca1400_ai_ci database, including an invalid byte

  • error handling for a NULL, integer, or unknown collation argument, and for unqualified chr(x, 'collation')
  • I ran these locally on an 11.4.15 Linux debug build (WSL):

  • mtr: 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.other and others all passed.

  • Dump and restore: I made a fresh mariadb-dump from DDL that uses plain CHR, 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, HEX values, collation, and coercibility 4. Restoring the same file on an unpatched 11.4.15 or 13.3.0 server failed with ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT, and no objects were created.

  • Matching direct CHR(): stored CHR matched direct CHR() for an invalid byte in both strict and permissive mode, for DEFAULT and CHECK, for sorting through a view, and for a sequence argument.
  • Basing the PR against the correct MariaDB version


  • This is a new feature and the PR is based against the latest MariaDB development branch.

  • This is a bug fix and the PR is based against the earliest maintained branch in which the bug can be reproduced.*
  • PR quality check


  • I checked the CODING_STANDARDS.md file and my PR conforms to this where appropriate.

  • For any trivial modifications to the PR, I am ok with the reviewer making the changes themselves.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: gkodinov Review duration: 12697m
    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
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.7% · Safe to merge
    27.7%
    Repository