Skip to content

(aoe): add ut for common/ - #723

Merged
yingfeng merged 1 commit into
matrixorigin:mainfrom
zzl200012:common-ut
Sep 15, 2021
Merged

yingfeng merged 1 commit into
matrixorigin:mainfrom
zzl200012:common-ut

Conversation

@zzl200012

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:

#681
currently, the coverage of ./common:
file.go 100%
id.go 100%
linklist.go 100%
mempool.go 93.3%
mvcc.go 100%
refs.go 100%

What this PR does / why we need it:

Not Available

Special notes for your reviewer:

Not Available

Additional documentation (e.g. design docs, usage docs, etc.):

Not Available

@zzl200012 zzl200012 added this to the v0.1.0 milestone Sep 15, 2021
@zzl200012
zzl200012 requested a review from XuPeng-SH September 15, 2021 08:50
@zzl200012 zzl200012 self-assigned this Sep 15, 2021
@XuPeng-SH XuPeng-SH added the kind/refactor Code refactor label Sep 15, 2021

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

lgtm

@yingfeng
yingfeng merged commit c80b16a into matrixorigin:main Sep 15, 2021
@zzl200012
zzl200012 deleted the common-ut branch September 22, 2021 14:04
fengttt pushed a commit to fengttt/matrixone that referenced this pull request May 7, 2026
XuPeng-SH added a commit that referenced this pull request Oct 9, 2026
## What type of PR is this?

- [x] BUG
- [x] Improvement
- [x] Documentation

## Which issue(s) this PR fixes:

Fixes #28251.
Partially addresses #28382; documents the intentional native UUID/v7
contract in #28380.
Assesses the UUID_SHORT portion of #25107; it remains deferred pending
an explicit distributed uniqueness contract. #27800 P0 is already
delivered by #28955; its storage-format P1 remains separate.

## What this PR does / why we need it:

Current main (`d2a0055ebe`) reproduces UUID/untyped-NULL bind errors and
branch-order-dependent UNION truncation to eight characters. Codec NULL
flags propagate NULL and nonnumeric flags error. Mixed UUID/text
LEAST/GREATEST is rejected.

### Design document

#### 1. Scope and product contract

This PR owns the UUID SQL compatibility design and implementation.

| Issue | Decision |
|---|---|
| #28251 | Correct codec swap flags: NULL/nonnumeric text means false;
decimal prefixes use the existing numeric policy. |
| #28382 | Correct mixed UUID/character value expressions and
truncation. Preserve native CTAS and strict native predicates as
explicit differences. |
| #28380 | Preserve UUIDv7/native UUID and correct misleading
documentation; UUID_V1() remains available explicitly. |
| #25107, UUID_SHORT | Defer the feature until cluster/restart/clone
uniqueness and allocator availability semantics are chosen. |
| #27800 | Existing P0 implementation is already merged in #28955. No
duplicate probe implementation or persisted-format change. |

`UUID()` / `UUID_V7()` continue to generate native 16-byte UUIDv7
values. They render as 36 canonical ASCII characters. `UUID(datetime)`
is a deterministic timestamp boundary, not a fresh unique identifier.
Native comparisons validate textual operands as UUID values.
Applications requiring arbitrary character semantics use an explicit
cast; version 1 is available through `UUID_V1()`.

#### 2. Root causes and first owners

- **Common result domain:** `textStringCommonType` excludes UUID, while
the generic COALESCE fallback reconstructs cast targets with different
widths. UNION selects that common target from COALESCE, allowing a short
first text branch to truncate the formatted UUID. The existing
`formattedScalarStringType` already owns its 36-character bound.
- **Mixed LEAST/GREATEST:** its resolver rejects UUID mixtures before
reaching the existing character executor. The correction must share the
value-domain owner and preserve all-UUID fast paths.
- **Codec policy:** the private flag getter uses strict floating
parsing, and both codecs propagate a NULL flag. This differs from the
intended flag policy. A nullable vector can retain nonzero physical
payload, so simply ignoring its NULL bit is unsafe.
- **NULL neutrality:** the existing comparison table omits symmetric
ANY/UUID entries even though its common NULL handling already supports
preserving the peer type. Completing those entries preserves the native
comparison domain without a separate state or execution path.

#### 3. Correction and integration

1. Extend `textStringCommonType` to accept UUID as a cast-requiring
branch, but require a real CHAR/VARCHAR/TEXT branch before selecting
text. UUID/NULL-only expressions remain native.
2. Extend `commonConditionalStringType` with UUID's existing formatted
width. Preserve TEXT-family bounds, unbounded TEXT and existing charset
merge ownership.
3. CASE/IF and COALESCE already use these owners. UNION inherits
complete aligned targets from COALESCE; its production planner/executor
remains unchanged.
4. For mixed LEAST/GREATEST, reuse the same common character owner only
when UUID is present, after the same-type fast path. UUID with
numeric/binary/JSON/temporal types remains rejected. Existing character
ordering semantics are retained.
5. Keep typed codec flag accessors. Normalize NULL with `swapFlag &&
!null2`. For strings, scan the existing decimal prefix, then use
existing MySQL numeric parsing; no prefix means false. This avoids
unintentionally accepting native hexadecimal/NaN/Inf extensions.
6. Preserve first-argument NULL handling, malformed UUID/binary errors
and explicit swap byte permutation for every UUID version. Matching
flags must be used for encode/decode; default/zero is appropriate for
v7.
7. Complete the two existing ANY/UUID comparison table entries. Valid
UUID/text key comparisons continue casting the operand into UUID rather
than formatting an indexed UUID column.

#### 4. Invariants and unhappy paths

- Every mixed character result/cast can hold every branch, independent
of branch order; UUID requires 36 characters, not its storage size.
- Native UUID-only expressions, generation, comparison and CTAS keep
their type and representation.
- Untyped NULL is neutral during comparison resolution; SQL UNKNOWN and
NULL-safe equality retain their existing semantics.
- A NULL flag means false even when its raw payload is nonzero. NULL
UUID/binary input remains NULL; invalid non-NULL input still errors.
- Prepared execution survives valid → NULL → valid parameters and an
error followed by healthy execution on the same connection.
- No new owned buffer, goroutine, lock, cache, retry state or storage
protocol is introduced. Existing executor ownership handles result
cleanup and query cancellation.

#### 5. Alternatives, cost and compatibility

Changing UUID() globally to VARCHAR/v1 would alter schema inference and
native generator use. Formatting UUID columns for every comparison would
add per-row work and compromise native index opportunities. Separate
CASE/IF/UNION patches would duplicate the same result-domain
calculation. Reusing the current owners avoids those costs.

Common-domain work remains bind-time O(argument count). Mixed character
expressions already require formatting; no additional per-row formatting
pass or retained state is introduced. Typed codec flags retain their
accessors; textual parsing is O(flag length). Overall throughput
improvement is not claimed.

Persistent UUID bytes, indexes, function IDs and protocol layouts remain
unchanged. Existing materialized schemas retain their types; newly
evaluated mixed expressions receive safe widths. During rolling
upgrades, old executors retain old expression semantics until upgraded;
instant cluster-wide semantic consistency is not claimed.

UUID_SHORT cannot be safely approximated with a local counter, CN hash
or truncated UUID. A future cluster-scoped implementation could reuse
`proc.GetHaKeeper().AllocateID(ctx)`, but must first define allocator
failure/cancellation, restart, clone and namespace boundaries. Exact
MySQL server/startup layout is a separate contract.

#### 6. Validation and delivery decisions

Typed registry tests assert complete widths/types, both branch orders,
CHAR/VARCHAR/TEXT, NULL neutrality and rejected neighboring domains.
Actual SQL tests assert full UUID values, prepared reuse, CTAS width,
driver type, native CTAS and error recovery. Existing version tests
protect generation and extraction. A shared two-CN fixture is reused;
the test does not claim explicit placement on both CNs.

Existing UUID codec and LEAST/GREATEST BVT expectations are migrated to
the documented contract. Their negative controls remain, and the codec
roundtrip now includes existing nullable flag and nullable UUID rows.
Both complete cases pass normal mo-tester comparison twice on the same
test-owned two-CN fixture, with table teardown checked before
repetition.

Production delta: **+15 lines net**. Implementation, tests and
documentation were examined separately. Existing strict swap tests are
migrated rather than duplicated. The official reference correction is a
companion documentation PR.

### Validation

Go 1.27.1, matching CPU CGo libraries built with `make cgo`, controlled
disk TMPDIR, GOMAXPROCS=4, package parallelism=2.

- Complete `pkg/sql/plan/function` normal tests: PASS (6.032s).
- Complete function package `-race`: PASS (13.005s); later test-only
additions also pass targeted race (1.135s).
- Twelve relevant existing planner consumers `-race`: PASS (2.310s).
- Public UUID SQL/prepared/materialization tests normal and race: PASS;
final metadata/error-boundary race run 10.307s.
- Exact strings, CTAS width36, driver VARCHAR type, native CTAS UUID,
NULL-safe predicates, valid→NULL→valid prepared reuse, invalid UUID and
15/17-byte binary errors followed by healthy reuse covered.
- Incremental SCA: `go vet` PASS; configured golangci-lint v2.14.0 zero
new issues; configured molint has zero new findings. Its two unchanged
unsafe-allowlist diagnostics are reproduced identically against baseline
source; they are not reported as a globally clean scan.
- Existing codec BVT: 61/61 PASS; existing LEAST/GREATEST BVT: 160/160
PASS. Both complete cases pass twice in normal comparison mode, zero
ignores/failures; all fixture tables are absent after each run. Local
two-CN direct SQL proof; final Compose/proxy CI remains pending.

Design and independent final QA: gpt-6.1-sol / xhigh. Accepted design
revision3, SHA256
`f5ed07141b60f768cd175dfd9ef9cee1c484f46d3f01cf78324ec03b3eb770f8`; QA
receipts recorded with exact revisions.

### Limits

No overall throughput improvement is claimed. Type changes are
bind-time; native generation/comparison/storage execution stays
unchanged. Mixed text expressions already require formatting. Existing
shared text ordering semantics are retained; full MySQL collation or
warning parity is not claimed. UUID_SHORT is not approximated with a
counter or truncated UUID.

Official UUID documentation: [matrixorigin.io
#723](matrixorigin/matrixorigin.io#723).


CI follow-up: stale codec flag and mixed UUID/text golden expectations
are corrected in four BVT files (+28/-16). No production changes or
additional queries/fixtures are introduced. The existing invalid-input
SQL test now requires a server MySQL error and uses a nullable receiver,
so unexpected SQL NULL cannot masquerade as an expected rejection.
Complete function package race passes locally (18.095s), and the
strengthened public `TestUUIDSemantics` regression passes race
(11.375s); unchanged function-code CI SCA is reused, and local dml vet,
molint and configured incremental golangci-lint pass with zero new
issues after strengthening the test.

Follow-up design and final overall review: gpt-6.1-sol / xhigh; final
correction review APPROVE at `164a20fb33`. Strengthened SQL regression
normal PASS (9.579s), race PASS (11.375s). Final Compose/proxy CI is
pending.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/refactor Code refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants