Skip to content

fix(plan): support LENGTH/OCTET_LENGTH/CHAR_LENGTH on BIT columns - #32103

Merged
XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-31183-main-64bff638
Nov 3, 2026
Merged

XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-31183-main-64bff638

Conversation

@gouhongshen

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Related to #31183

What this PR does / why we need it:

Root cause

LENGTH/OCTET_LENGTH are backed by the LENGTH built-in and CHAR_LENGTH/CHARACTER_LENGTH by LENGTH_UTF8 (pkg/sql/plan/function/function_id.go). Both were registered with checkFn: stringDomainFixedTypeMatch, which never admits types.T_bit (BIT's implicit-cast rule row only targets INT64/UINT64), so every call on a BIT column failed function binding with ERROR 20203 invalid argument .... BIT literals worked because they are binary-string constants and never reach that type-match path.

Change

  • pkg/sql/plan/function/type_check.go: new lengthTypeMatch — a single BIT argument binds directly with no implicit cast, so the declared width M is preserved; all other inputs keep the previous string-domain behavior. BIT's global implicit-cast rules are untouched.
  • pkg/sql/plan/function/list_builtIn.go: LENGTH and LENGTH_UTF8 use lengthTypeMatch and each gains a BIT overload returning INT64, evaluated by the new executor.
  • pkg/sql/plan/function/func_unary.go: new LengthBit executor returns (width+7)/8, i.e. ceil(M/8), for declared widths 1..64. This matches MySQL 8.0 fixed storage width (Field_bit::pack_length()); BIT values stringify with the binary charset, so the character count equals the byte count. NULL stays NULL. Out-of-range width metadata (0, negative, >64 — no supported SQL producer) falls back to a per-value minimal byte count instead of failing.
  • Tests: new UT pkg/sql/plan/function/length_bit_type_match_test.go; BIT sections appended to BVT cases func_string_length, func_string_octet_length, func_string_char_length.

Behavior after the fix

LENGTH, OCTET_LENGTH, CHAR_LENGTH and CHARACTER_LENGTH on a BIT(M) column return ceil(M/8) for every non-NULL value (value-independent fixed width, leading zero bytes preserved) and NULL for NULL, matching MySQL 8.0.45 (verified against mysql-server sql/field.cc, sql/field.h, sql/item_func.cc).

Validation

  • mo-cgo-test -run 'TestLengthFamily' ./pkg/sql/plan/function — new UTs pass: binding for all four function names x widths 1/8/9/16/64 (cast-free INT64 via the BIT overload), fixed-width results for zero/leading-zero/max values, NULL propagation, invalid-width fallback, and unchanged non-BIT binding (varchar/char/text/blob/varbinary, INT64). The same binding test fails on the base commit with the issue's ERROR 20203 (red/green proof).
  • mo-cgo-test ./pkg/sql/plan/function — whole package green.
  • go vet -mod=readonly ./pkg/sql/plan/function with the repository CGo environment — clean; gofmt clean.
  • mo-tester run-mode for the three touched BVT case files against a local instance built from this branch: 204/204 statements pass. The BIT sections cover the issue's exact reproduction (LENGTH on a BIT(8) column holding b'11111111'), widths 1/8/9/64, zero and leading-zero values, NULL rows, MAX/SUM aggregates, and BIT-literal controls.
  • Live SQL check on a local instance: LENGTH/OCTET_LENGTH/CHAR_LENGTH over BIT(1)/BIT(8)/BIT(16)/BIT(64) columns return 1/1/2/8 for every non-NULL value and NULL for NULL; SUM(LENGTH(b64)) = 24 — identical to MySQL 8.0.45.

Residual risks / limits

  • Invalid BIT width metadata uses the documented minimal-byte fallback; no supported SQL path produces it (noted as a P3 follow-up in review).
  • Binding errors keep MatrixOne's existing moerr wording (invalid argument %s, bad value %s); MySQL's exact error text is not mirrored.
  • Full-repo make static-check and the complete BVT suite are left to CI; the changed-package closure above is green.

LENGTH and LENGTH_UTF8 were registered with checkFn
stringDomainFixedTypeMatch, which never admits T_bit (BIT's implicit
cast rule only targets INT64/UINT64), so LENGTH/OCTET_LENGTH/
CHAR_LENGTH/CHARACTER_LENGTH on a BIT column failed binding with
ERROR 20203 while BIT literals (binary-string constants) worked.

Bind a single BIT argument directly with no implicit cast, preserving
the declared width, and evaluate the length family on BIT(M) as
ceil(M/8), the fixed storage width MySQL 8.0 defines in
Field_bit::pack_length(); the binary charset makes character length
equal byte length. NULL stays NULL. Out-of-range width metadata falls
back to a per-value minimal byte count.

Adds focused UTs (binding for all four names x widths 1/8/9/16/64,
fixed-width semantics incl. NULL, fallback, non-BIT preservation) and
BVT coverage in func_string_length/func_string_octet_length/
func_string_char_length.

Related to matrixorigin#31183
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH

XuPeng-SH commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 2f5967dc3ee5951e120423b3e781dd6c0e7c8369

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: 2f5967dc3ee5951e120423b3e781dd6c0e7c8369

Assessment: approve

The native BIT overloads preserve declared width and reuse existing scalar execution for NULLs and selections. Function-local matching preserves existing casts, aliases and overload identities. No actionable findings remain.

Model: gpt-6.1-sol / max

Session: 01a1a004-90c3-73b1-acc9-3d4636452003 · slot 0 · generation 0

Validation:

  • Reviewed all ten changed files, both discussion entries and linked issue context; completed discussion-assessment.json.

  • Verified the fixed-width representation against MySQL 8.0.45 field handling and its packed-width definition.

  • Reused reported focused/full-package tests, vet/gofmt and 204/204 statements across the three changed BVT cases.

  • Frozen CI records successful Ubuntu UT, arm64 SCA and proxy/pessimistic multi-CN BVT checks.

  • Local source, overload-preservation, case/result and receipt audits passed. Source remained unchanged.

Limitations:

  • Go, BVT and static-analysis execution results were reused from frozen author/CI evidence; no local rerun is claimed.

  • Reference MySQL semantics were checked in pinned source rather than a fresh MySQL instance.

@mergify mergify Bot added the queued label Nov 3, 2026
@mergify

mergify Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

🚫 Left the queue · pull request dequeued

1 h 6 min in the queue, with no time running CI.

So far

  • ✅ Entered the queue Tue 3 Nov, 20:11 · rule main · triggered by rule Automatic queue on approval for main
  • ⏹️ Testing stopped · in place
  • 🚫 Left the queue 21:17 · bf62197

Reason

Pull request #32103 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

To get back in the queue

  • Push a change if one is needed: the pull request is queued again once its queue conditions match.
  • If no change is needed, tick the box below to queue it again as it is.

Merge conditions not met when it left the queue

  • any of · required by GitHub branch protection:
    • check-neutral = CI Required
    • check-skipped = CI Required
    • check-success = CI Required

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request
2 merge conditions met
  • #review-threads-unresolved = 0 · required by GitHub branch protection
  • github-review-approved · required by GitHub branch protection

Times in UTC · dashboard · how the merge queue works

This branch was previously deployed

1 inactive deployment
ci — bf621970 Deployed Nov 3, 2026 by mergify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dequeued kind/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants