Skip to content

fix(function): parse numeric prefixes in CONV and BIN - #28430

Merged
XuPeng-SH merged 7 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/issue-28392-conv-bin-prefix
Sep 9, 2026
Merged

XuPeng-SH merged 7 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/issue-28392-conv-bin-prefix

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #28392.

Make CONV() and BIN() accept MySQL-compatible numeric prefixes for character inputs. A valid prefix is consumed until the first byte that is not a digit in the selected source base; an input with no usable prefix evaluates to zero, while NULL and the empty string retain their NULL behavior.

Root cause

  • CONV() used an all-or-nothing string parser and rejected otherwise valid prefixes such as 123xyz.
  • BIN() exposed only numeric overloads, so character inputs went through strict string-to-integer casting instead of the same prefix conversion semantics as CONV(..., 10, 2).

Implementation

  • Replaced strict character parsing for CONV() with a byte-oriented, single-pass prefix parser.
  • Added a character overload for BIN() while keeping the existing numeric overloads and their dispatch unchanged.
  • Preserved signs, source/destination base validation, signed output-base behavior, and MySQL-compatible overflow behavior.
  • Skips only ASCII SQL whitespace before the prefix. Non-ASCII bytes, NUL, and binary payload bytes are not silently reclassified as whitespace.
  • Uses a fixed-width uint64 accumulator with checked multiplication/addition. It stops at the first invalid digit and saturates/clamps at the first provable overflow, without allocating a big.Int or copying each varlen input into a new string.
  • Keeps the BIN() type matcher allocation-free at planning time when reserving the string overload; numeric inputs continue to use the original typed fast paths.

Correctness and unhappy paths covered

  • valid prefixes with trailing text;
  • no usable prefix, whitespace-only input, sign-only input, NULL, and empty strings;
  • positive and negative values, including unsigned overflow beyond uint64;
  • signed source bases and signed output bases;
  • bases 2 through 36, invalid digits, and invalid bases;
  • CHAR/VARCHAR/TEXT plus BINARY/VARBINARY/BLOB inputs;
  • binary values containing non-ASCII bytes or NUL before a digit;
  • masked rows in vector execution are short-circuited before input decoding;
  • numeric BIN()/CONV() overloads remain covered;
  • issue-level scalar and batch execution through an embedded MatrixOne cluster.

Validation

Passed locally on macOS with the repository CGo/native-library setup:

go test -mod=readonly -count=1 -timeout=240s ./pkg/sql/plan/function
go test -mod=readonly -count=20 -timeout=240s ./pkg/sql/plan/function -run 'Test(Conv|Bin|ParseBase)'
go test -race -mod=readonly -count=1 -timeout=240s ./pkg/sql/plan/function -run 'Test(Conv|Bin|ParseBase)'
go test -mod=readonly -count=1 -timeout=6m ./pkg/tests/issues -run '^TestIssue28392ConvBinNumericPrefixes$'
golangci-lint run -c .golangci.yml ./pkg/sql/plan/function ./pkg/tests/issues
go vet -mod=readonly -vettool=/Users/xupeng/code/bin/molint ./pkg/sql/plan/function ./pkg/tests/issues
license-eye -c .licenserc.yml header check
git diff --check

All listed local checks passed. MySQL 8.4 controls also matched the documented prefix, sign, overflow, binary-whitespace, NUL, and base-36 cases.

The failures on the previous PR head were unrelated to this function change: one Compose BVT had an existing dtype/text_blob.sql failure, the multi-CN BVT could not establish a CN connection during startup, and Coverage was a chained failure because the required BVT producer did not publish a profile. The updated head will run the repository CI again.

@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 →

@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Sep 8, 2026
@XuPeng-SH
XuPeng-SH force-pushed the codex/issue-28392-conv-bin-prefix branch from 435d38d to 1f210d7 Compare September 8, 2026 15:50
@matrix-meow matrix-meow added size/L Denotes a PR that changes [500,999] lines and removed size/M Denotes a PR that changes [100,499] lines labels Sep 8, 2026

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deep review of exact head ac8bdf0 against base 84def69: no confirmed blocking regression found. COMMENT only, as requested for this self-authored PR.

Reviewed all 15 changed files, including the binder literal distinction, overload matcher, prefix parser/string executors, focused unit tests, embedded SQL test and changed BVT expectations.

The byte parser correctly separates empty input from nonempty/no-prefix input, accepts only ASCII leading whitespace and base-valid digits, stops at suffix/NUL/non-ASCII bytes, and performs checked fixed-width accumulation. I checked signed-source limits, negative unsigned wrap versus overflow-to-zero, positive saturation, output-base signed formatting, and invalid/NULL base handling. The selected overflow and literal-domain rules were cross-checked against MySQL 8.4 Item_func_conv::val_str. Existing numeric CONV dispatch and constant-base restrictions are unchanged; this review does not certify broader pre-existing numeric-CONV equivalence to MySQL.

BIN reserves the new overload for MySQL string families/unresolved parameters, while retaining the original numeric overload IDs and excluding the string candidate from numeric fallback matching. The binder explicitly distinguishes direct HEX/BIT numeric literals from ordinary binary-introduced strings before overload selection. Prepared values/NULLs still come through the existing parameter vector path; the added matcher test checks unresolved-parameter typing, but there is no new prepared-client execution test and I did not independently run that protocol matrix.

Both changed string loops short-circuit masked rows before decoding values, propagate NULLs and result-append allocation errors, and retain no borrowed input bytes. The parser has constant auxiliary storage, stops at provable overflow, and no longer allocates a big.Int for negative strings; leading whitespace/zero runs remain linear in supplied input size. Output is bounded by the 64-bit representation. No new goroutine, lock, retry, external I/O or long-lived cache is introduced. The issue test reuses the existing cluster fixture and closes rows/connection/database handles with an independent bounded database-cleanup context.

Independent validation: extracted the exact new parser/helpers into a temporary standard-library-only Go harness and checked 3,780 combinations across bases 2–36, both signs, signed/unsigned source bases and boundaries around 2^31/32/62/63/64/65/128 against an independent big.Int arithmetic oracle. Seven additional empty/sign/ASCII-whitespace/NUL/Unicode/100,000-zero cases passed. This proves the tested parser arithmetic and termination properties, not a full package or live MySQL differential run.

Exact-head MatrixOne ALL CI run 34295759779 completed successfully. The author also reports package tests, repeated/focused race tests and embedded SQL controls; those are distinguished from the independently run parser probe. No repository source changes and no CI polling.

@XuPeng-SH
XuPeng-SH merged commit cfcfa7a into matrixorigin:main Sep 9, 2026
18 of 20 checks passed
@XuPeng-SH
XuPeng-SH deleted the codex/issue-28392-conv-bin-prefix branch September 9, 2026 01:44
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…8430)

## Summary

Closes matrixorigin#28392.

Make `CONV()` and `BIN()` accept MySQL-compatible numeric prefixes for
character inputs. A valid prefix is consumed until the first byte that
is not a digit in the selected source base; an input with no usable
prefix evaluates to zero, while `NULL` and the empty string retain their
`NULL` behavior.

## Root cause

- `CONV()` used an all-or-nothing string parser and rejected otherwise
valid prefixes such as `123xyz`.
- `BIN()` exposed only numeric overloads, so character inputs went
through strict string-to-integer casting instead of the same prefix
conversion semantics as `CONV(..., 10, 2)`.

## Implementation

- Replaced strict character parsing for `CONV()` with a byte-oriented,
single-pass prefix parser.
- Added a character overload for `BIN()` while keeping the existing
numeric overloads and their dispatch unchanged.
- Preserved signs, source/destination base validation, signed
output-base behavior, and MySQL-compatible overflow behavior.
- Skips only ASCII SQL whitespace before the prefix. Non-ASCII bytes,
NUL, and binary payload bytes are not silently reclassified as
whitespace.
- Uses a fixed-width `uint64` accumulator with checked
multiplication/addition. It stops at the first invalid digit and
saturates/clamps at the first provable overflow, without allocating a
`big.Int` or copying each varlen input into a new string.
- Keeps the `BIN()` type matcher allocation-free at planning time when
reserving the string overload; numeric inputs continue to use the
original typed fast paths.

## Correctness and unhappy paths covered

- valid prefixes with trailing text;
- no usable prefix, whitespace-only input, sign-only input, `NULL`, and
empty strings;
- positive and negative values, including unsigned overflow beyond
`uint64`;
- signed source bases and signed output bases;
- bases 2 through 36, invalid digits, and invalid bases;
- `CHAR`/`VARCHAR`/`TEXT` plus `BINARY`/`VARBINARY`/`BLOB` inputs;
- binary values containing non-ASCII bytes or NUL before a digit;
- masked rows in vector execution are short-circuited before input
decoding;
- numeric `BIN()`/`CONV()` overloads remain covered;
- issue-level scalar and batch execution through an embedded MatrixOne
cluster.

## Validation

Passed locally on macOS with the repository CGo/native-library setup:

```text
go test -mod=readonly -count=1 -timeout=240s ./pkg/sql/plan/function
go test -mod=readonly -count=20 -timeout=240s ./pkg/sql/plan/function -run 'Test(Conv|Bin|ParseBase)'
go test -race -mod=readonly -count=1 -timeout=240s ./pkg/sql/plan/function -run 'Test(Conv|Bin|ParseBase)'
go test -mod=readonly -count=1 -timeout=6m ./pkg/tests/issues -run '^TestIssue28392ConvBinNumericPrefixes$'
golangci-lint run -c .golangci.yml ./pkg/sql/plan/function ./pkg/tests/issues
go vet -mod=readonly -vettool=/Users/xupeng/code/bin/molint ./pkg/sql/plan/function ./pkg/tests/issues
license-eye -c .licenserc.yml header check
git diff --check
```

All listed local checks passed. MySQL 8.4 controls also matched the
documented prefix, sign, overflow, binary-whitespace, NUL, and base-36
cases.

The failures on the previous PR head were unrelated to this function
change: one Compose BVT had an existing `dtype/text_blob.sql` failure,
the multi-CN BVT could not establish a CN connection during startup, and
Coverage was a chained failure because the required BVT producer did not
publish a profile. The updated head will run the repository CI again.
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…8430)

## Summary

Closes matrixorigin#28392.

Make `CONV()` and `BIN()` accept MySQL-compatible numeric prefixes for
character inputs. A valid prefix is consumed until the first byte that
is not a digit in the selected source base; an input with no usable
prefix evaluates to zero, while `NULL` and the empty string retain their
`NULL` behavior.

## Root cause

- `CONV()` used an all-or-nothing string parser and rejected otherwise
valid prefixes such as `123xyz`.
- `BIN()` exposed only numeric overloads, so character inputs went
through strict string-to-integer casting instead of the same prefix
conversion semantics as `CONV(..., 10, 2)`.

## Implementation

- Replaced strict character parsing for `CONV()` with a byte-oriented,
single-pass prefix parser.
- Added a character overload for `BIN()` while keeping the existing
numeric overloads and their dispatch unchanged.
- Preserved signs, source/destination base validation, signed
output-base behavior, and MySQL-compatible overflow behavior.
- Skips only ASCII SQL whitespace before the prefix. Non-ASCII bytes,
NUL, and binary payload bytes are not silently reclassified as
whitespace.
- Uses a fixed-width `uint64` accumulator with checked
multiplication/addition. It stops at the first invalid digit and
saturates/clamps at the first provable overflow, without allocating a
`big.Int` or copying each varlen input into a new string.
- Keeps the `BIN()` type matcher allocation-free at planning time when
reserving the string overload; numeric inputs continue to use the
original typed fast paths.

## Correctness and unhappy paths covered

- valid prefixes with trailing text;
- no usable prefix, whitespace-only input, sign-only input, `NULL`, and
empty strings;
- positive and negative values, including unsigned overflow beyond
`uint64`;
- signed source bases and signed output bases;
- bases 2 through 36, invalid digits, and invalid bases;
- `CHAR`/`VARCHAR`/`TEXT` plus `BINARY`/`VARBINARY`/`BLOB` inputs;
- binary values containing non-ASCII bytes or NUL before a digit;
- masked rows in vector execution are short-circuited before input
decoding;
- numeric `BIN()`/`CONV()` overloads remain covered;
- issue-level scalar and batch execution through an embedded MatrixOne
cluster.

## Validation

Passed locally on macOS with the repository CGo/native-library setup:

```text
go test -mod=readonly -count=1 -timeout=240s ./pkg/sql/plan/function
go test -mod=readonly -count=20 -timeout=240s ./pkg/sql/plan/function -run 'Test(Conv|Bin|ParseBase)'
go test -race -mod=readonly -count=1 -timeout=240s ./pkg/sql/plan/function -run 'Test(Conv|Bin|ParseBase)'
go test -mod=readonly -count=1 -timeout=6m ./pkg/tests/issues -run '^TestIssue28392ConvBinNumericPrefixes$'
golangci-lint run -c .golangci.yml ./pkg/sql/plan/function ./pkg/tests/issues
go vet -mod=readonly -vettool=/Users/xupeng/code/bin/molint ./pkg/sql/plan/function ./pkg/tests/issues
license-eye -c .licenserc.yml header check
git diff --check
```

All listed local checks passed. MySQL 8.4 controls also matched the
documented prefix, sign, overflow, binary-whitespace, NUL, and base-36
cases.

The failures on the previous PR head were unrelated to this function
change: one Compose BVT had an existing `dtype/text_blob.sql` failure,
the multi-CN BVT could not establish a CN connection during startup, and
Coverage was a chained failure because the required BVT producer did not
publish a profile. The updated head will run the repository CI again.
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…8430)

## Summary

Closes matrixorigin#28392.

Make `CONV()` and `BIN()` accept MySQL-compatible numeric prefixes for
character inputs. A valid prefix is consumed until the first byte that
is not a digit in the selected source base; an input with no usable
prefix evaluates to zero, while `NULL` and the empty string retain their
`NULL` behavior.

## Root cause

- `CONV()` used an all-or-nothing string parser and rejected otherwise
valid prefixes such as `123xyz`.
- `BIN()` exposed only numeric overloads, so character inputs went
through strict string-to-integer casting instead of the same prefix
conversion semantics as `CONV(..., 10, 2)`.

## Implementation

- Replaced strict character parsing for `CONV()` with a byte-oriented,
single-pass prefix parser.
- Added a character overload for `BIN()` while keeping the existing
numeric overloads and their dispatch unchanged.
- Preserved signs, source/destination base validation, signed
output-base behavior, and MySQL-compatible overflow behavior.
- Skips only ASCII SQL whitespace before the prefix. Non-ASCII bytes,
NUL, and binary payload bytes are not silently reclassified as
whitespace.
- Uses a fixed-width `uint64` accumulator with checked
multiplication/addition. It stops at the first invalid digit and
saturates/clamps at the first provable overflow, without allocating a
`big.Int` or copying each varlen input into a new string.
- Keeps the `BIN()` type matcher allocation-free at planning time when
reserving the string overload; numeric inputs continue to use the
original typed fast paths.

## Correctness and unhappy paths covered

- valid prefixes with trailing text;
- no usable prefix, whitespace-only input, sign-only input, `NULL`, and
empty strings;
- positive and negative values, including unsigned overflow beyond
`uint64`;
- signed source bases and signed output bases;
- bases 2 through 36, invalid digits, and invalid bases;
- `CHAR`/`VARCHAR`/`TEXT` plus `BINARY`/`VARBINARY`/`BLOB` inputs;
- binary values containing non-ASCII bytes or NUL before a digit;
- masked rows in vector execution are short-circuited before input
decoding;
- numeric `BIN()`/`CONV()` overloads remain covered;
- issue-level scalar and batch execution through an embedded MatrixOne
cluster.

## Validation

Passed locally on macOS with the repository CGo/native-library setup:

```text
go test -mod=readonly -count=1 -timeout=240s ./pkg/sql/plan/function
go test -mod=readonly -count=20 -timeout=240s ./pkg/sql/plan/function -run 'Test(Conv|Bin|ParseBase)'
go test -race -mod=readonly -count=1 -timeout=240s ./pkg/sql/plan/function -run 'Test(Conv|Bin|ParseBase)'
go test -mod=readonly -count=1 -timeout=6m ./pkg/tests/issues -run '^TestIssue28392ConvBinNumericPrefixes$'
golangci-lint run -c .golangci.yml ./pkg/sql/plan/function ./pkg/tests/issues
go vet -mod=readonly -vettool=/Users/xupeng/code/bin/molint ./pkg/sql/plan/function ./pkg/tests/issues
license-eye -c .licenserc.yml header check
git diff --check
```

All listed local checks passed. MySQL 8.4 controls also matched the
documented prefix, sign, overflow, binary-whitespace, NUL, and base-36
cases.

The failures on the previous PR head were unrelated to this function
change: one Compose BVT had an existing `dtype/text_blob.sql` failure,
the multi-CN BVT could not establish a CN connection during startup, and
Coverage was a chained failure because the required BVT producer did not
publish a profile. The updated head will run the repository CI again.

This branch was previously deployed

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

Labels

size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Compatibility]: CONV and BIN reject strings with trailing text instead of using numeric prefixes

2 participants