Skip to content

[Bug]: LENGTH/CHAR_LENGTH/OCTET_LENGTH error on BIT columns (BIT literal works) #31183

Closed

Description

@Ariznawlll

Description

LENGTH(), CHAR_LENGTH(), and OCTET_LENGTH() error out on a BIT column with invalid argument function length, bad value [BIT], while MySQL returns the byte/char count of the BIT value. Notably the same functions work on a BIT literal, and other functions (HEX, arithmetic) work on the column — so the column's type is not routed through the string-coercion path that literals and other types use.

Environment

  • MatrixOne dev build (local multi-CN, main)
  • MySQL 8.0.45 control

Reproduction

CREATE TABLE bt2 (b BIT(8));
INSERT INTO bt2 VALUES (b'11111111');
Expression MO MySQL 8.0.45
SELECT LENGTH(b) FROM bt2 ERROR 20203 invalid argument function length, bad value [BIT] 1
SELECT CHAR_LENGTH(b) FROM bt2 ERROR 20203 invalid argument function char_length, bad value [BIT] 1
SELECT OCTET_LENGTH(b) FROM bt2 ERROR 20203 invalid argument function octet_length, bad value [BIT] 1
SELECT HEX(b) FROM bt2 FF ✓ FF
SELECT b+0 FROM bt2 255 ✓ 255

The inconsistency: literals work, columns don't

SELECT LENGTH(b'101');   -- MO: 1  ✓ (same as MySQL)
SELECT LENGTH(b);        -- via column: ERROR

So LENGTH accepts a BIT literal but rejects a BIT column. Whatever coercion path the literal takes is not applied when the argument is a typed column reference — which suggests the function's type-matching only consults the literal's inferred type, not the BIT column type.

Impact

  • Schema-introspection / data-dictionary queries fail outright. It is common to size a column with MAX(LENGTH(col)) or SUM(LENGTH(col)); over a BIT column (flags, permission bitmasks, legacy boolean packs) that query errors and cannot be worked around without casting every column name.
  • Generic tooling breaks. ORMs and BI tools that measure string width use LENGTH/CHAR_LENGTH from information_schema metadata; a BIT column makes the whole profiling query fail rather than returning one column's value.
  • CAST(b AS CHAR) returns a raw byte, not a readable string: SELECT CAST(b AS CHAR) FROM bt2 yields a single unprintable 0xFF byte rather than MySQL's behaviour, so the intended workaround also produces unusable output.
  • Because the same query works on MySQL, a migrated schema with BIT columns turns previously-working health-check and profiling SQL into hard errors.

Expected behavior

LENGTH/CHAR_LENGTH/OCTET_LENGTH on a BIT column should coerce the value to its binary string form and return its width, matching MySQL and matching the already-working literal path:

SELECT LENGTH(b) FROM bt2;        -- 1
SELECT CHAR_LENGTH(b) FROM bt2;   -- 1
SELECT OCTET_LENGTH(b) FROM bt2;  -- 1

More generally, BIT columns should participate in the same implicit string/numeric coercion matrix as other types, rather than raising a hard "bad value [BIT]" error in string functions.

Activity

  1. added
    kind/bug
    on Oct 26, 2026
  2. added this to the MO-202610 milestone on Oct 26, 2026
  3. added theissue type on Oct 26, 2026
  4. added
    severity/s0
    ai-easy
    and removed on Oct 26, 2026
  5. gouhongshen commented on Nov 3, 2026

    @gouhongshen
    Contributor

    PR #32103 is trying to fix this issue.

  6. gouhongshen commented on Nov 3, 2026

    @gouhongshen
    Contributor

    PR #32103 has merged and is awaiting manual validation. The issue remains open.

  7. XuPeng-SH commented on Nov 4, 2026

    @XuPeng-SH
    Contributor

    QA handoff after #32103 merged into main (a7aed8b70ff5ee012d7c631f94b1d47f594876e7). Native BIT overloads for LENGTH/OCTET_LENGTH/CHAR_LENGTH/CHARACTER_LENGTH return ceil(M/8), preserve declared width and propagate NULL.

    Author reports the complete function package and 204/204 changed BVT statements passing; the final reviewer reused author/CI evidence rather than executing the SQL suite locally. This handoff run performed only remote review and no local tests.

    Please verify BIT widths 1/8/9/16/64, zero/leading-zero/max values, NULLs, aggregates, literals and non-BIT controls. Scope is the reported LENGTH-family behavior, not all BIT coercions. The unsupported invalid-width metadata fallback has no demonstrated supported SQL producer and is not asserted covered. QA acceptance remains pending.

  8. Ariznawlll commented on Nov 5, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试通过。LENGTH 系列函数接受 BIT 列,并按声明宽度返回字节数。

    关键证据

    • 官方 main@80224b0314c6008ecb4de82d1e37b019d5e355df,隔离本地 2 CN / 1 TN / 1 LogService / Proxy;三个入口的 git_version() 均为该提交。
    • 对 BIT(1/8/9/16/64) 的非零、零和 NULL 值核对 LENGTH、CHAR_LENGTH、OCTET_LENGTH、CHARACTER_LENGTH,结果分别为 1/1/2/2/8 或 NULL;BIT(64) 最大值、BIT literal、普通字符串以及 SUM/MAX 聚合对照均正确。失败批量写入后源表不变、连接可用;三个入口各重复 3 轮通过。
    • 主库相关函数 BVT 与类型匹配测试已随 #32103 合入,见 length_bit_type_match_test.go;相关 Go 测试在 arm64 race 模式重复 3 次通过。

    待解决点

    无。本次验证 LENGTH 系列,不将结论扩展到全部 BIT 字符串转换。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-easykind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions