Skip to content

refactor(types): type dispatch design (#10181), oidscan, DML dispatch fixes, traits table - #33338

Merged
fengttt merged 4 commits into
matrixorigin:mainfrom
fengttt:design/type-vft
Nov 26, 2026
Merged

fengttt merged 4 commits into
matrixorigin:mainfrom
fengttt:design/type-vft

Conversation

@fengttt

@fengttt fengttt commented Nov 26, 2026 •

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:

issue #10181

What this PR does / why we need it:

Design plus steps 1–3 of the plan in docs/design/20261126-type-vft.md.

Design doc. Measures the type-id switches in pkg/ and proposes an Oid-indexed type table (types.Traits) and per-package operation tables with completeness tests. The switches that stay are covered by extending the existing TestTypeSwitchFamilies check (more families, plus an "all types" rule for dispatch switches). The table is indexed by Oid rather than stored as a pointer in types.Type, because Type has a fixed 16-byte stored layout.

Step 1: optools/oidscan. A standalone, standard-library-only module that reports switch counts, a case-count histogram, the switches inside loops, and which of those have a type that is fixed for the loop. It is a report, not a check. go run . -root ../.. -loops -invariant gives the worklist for step 6.

Step 2: dispatch out of row loops in DML.

  • multi_update and preinsert each had a copy of the zero-temporal check with a type switch per row. Both now call colexec.CheckZeroTemporalVector, which switches once per vector. Error codes, messages and row numbers are unchanged.
  • postdml decides how to quote the primary key once per batch instead of once per row.
  • deletion copied each deleted key through getNonNullValue + AppendAny: boxed, with two type switches per row. Its getNonNullValue copy panicked on key types it didn't list (the low-precision floats, year, most vector types). The key is now copied with UnionOne, and that copy of getNonNullValue is removed.
  • typeswitch_baseline.txt was regenerated. Lines were only removed: the two deletion entries, plus 12 entries already fixed upstream.

Step 3: one table of per-type facts in types. traits_table.go has one row per T_ constant: family flags, TypeLen, FixedLength, the default type TryToType returns, and the String/OidString names. These methods and the T.IsXxx() predicates (plus Type.IsInt/IsUInt/IsFloat/IsDecimal) now read the table instead of each switching on the type id.

  • No change in behavior. The old switches are kept in traits_legacy_test.go as an oracle, and TestTraitsMatchLegacy compares every method on all 256 T values, including the existing panics for int128, uint128, Objectid and star. I confirmed the test fails when a row is changed.
  • A field left out of a row means "absent" (panic, false or "unknown"), so a forgotten size fails loudly instead of returning 0.
  • TestTraitsCoverEveryOid fails when a T_ constant has no row. TestTraitsFlagFamilies ties the float and vector flags to the TestTypeSwitchFamilies families.
  • Benchmarks: the table is as fast as or faster than the switches (one pass over all types: FixedLength 106 vs 124 ns, IsFloat 31 vs 39 ns, IsDateRelate 31 vs 33 ns).
  • Two baseline entries removed; both were switches in types.go that are now table rows.

Tests

  • New: TestCheckZeroTemporalVector covers nulls, const and const-null vectors, the timestamp lower bound, and error row numbers. TestCollectBatchInfoCopiesKeys covers int64, out-of-line varchar, decimal128 and float16 keys, checks grouping by block, and checks the mpool is back to zero afterwards.
  • oidscan has fixture tests.
  • Step 3: the full types suite passes, and so do the container/vector, container/batch, sql/plan/function and vm/engine/tae/containers suites. All of ./pkg/... builds.
  • The full test suites of colexec, multi_update, preinsert, postdml and deletion, and TestTypeSwitchFamilies, pass.
  • molint and golangci-lint report 0 issues on the changed packages, pkg/container/types included.

🤖 Generated with Claude Code

Design and phased plan for issue matrixorigin#10181. Measures the 949 type-id
switches in pkg/, identifies the ones that run per row, and proposes an
Oid-indexed traits table in types, per-package operation tables with
completeness tests, and the exhaustive linter for the switches that stay.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fengttt
fengttt requested a review from XuPeng-SH as a code owner November 26, 2026 19:50
@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 →

@mergify mergify Bot added the kind/documentation Improvements or additions to documentation label Nov 26, 2026
@matrix-meow matrix-meow added the size/XS Denotes a PR that changes [1, 9] lines label Nov 26, 2026
@mergify mergify Bot added the kind/refactor Code refactor label Nov 26, 2026
@mergify

mergify Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@XuPeng-SH

XuPeng-SH commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

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

Commit: 822dc7b14250e13c70e5c55b28095af813890ce9

Review belongs to an older commit; it is not a review of the current head.

fengttt and others added 2 commits November 26, 2026 12:02
…eSwitchFamilies

Step 1 of docs/design/20261126-type-vft.md (matrixorigin#10181). optools/oidscan is a
standalone, standard-library-only module that scans pkg/ for switch
statements over types.T and reports their count, case-count histogram, the
ones inside a loop, and which of those have a loop-invariant type (the
worklist for moving dispatch out of row loops). It is a report, not a check.

The design doc now builds on the existing TestTypeSwitchFamilies check
(families, typeswitch:partial tags, shrinking baseline) instead of proposing
the exhaustive linter, and records steps 1 and 2 as done.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Step 2 of docs/design/20261126-type-vft.md (matrixorigin#10181).

- multi_update and preinsert each had a copy of the zero-temporal check
  that switched on the column type for every row. Both now call
  colexec.CheckZeroTemporalVector, which switches once per vector and runs
  a typed row loop. Error codes, messages and row numbers are unchanged.
- postdml decides how to quote the primary key once per batch instead of
  per row.
- deletion copied each deleted row's key by boxing it (getNonNullValue)
  and unboxing it again (AppendAny): two type switches per row, and the
  getNonNullValue copy panicked on key types it did not list (the
  low-precision floats, year, most vector types). The key is now copied
  with UnionOne, and that copy of getNonNullValue is removed.
- typeswitch_baseline.txt regenerated: removals only (the two deletion
  entries plus 12 entries already fixed upstream).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fengttt fengttt changed the title docs(design): type dispatch tables to consolidate switch typ.Oid refactor(types): type dispatch design (#10181), oidscan tool, and first DML dispatch fixes Nov 26, 2026
@matrix-meow matrix-meow added size/XL Denotes a PR that changes [1000, 1999] lines and removed size/XS Denotes a PR that changes [1, 9] lines labels Nov 26, 2026
Step 3 of docs/design/20261126-type-vft.md (matrixorigin#10181).

traits_table.go holds one row per T_ constant: family flags, TypeLen,
FixedLength, the default type TryToType returns, and the String and
OidString names. String, OidString, TypeLen, FixedLength, TryToType, the
T.IsXxx predicates and Type.IsInt/IsUInt/IsFloat/IsDecimal read the table
instead of each switching on the type id. AllOids() lists the T_ constants
for other packages' completeness tests.

No change in behavior: the switch implementations are kept in
traits_legacy_test.go and TestTraitsMatchLegacy compares every method on
all 256 T values, including the panics for types with no size. A field
left out of a row means "absent" (panic, false or "unknown"), so a
forgotten size fails loudly. TestTraitsCoverEveryOid fails when a T_
constant has no row; TestTraitsFlagFamilies ties the float and vector
flags to the TestTypeSwitchFamilies families. Benchmarks: the table is
as fast as or faster than the switches (FixedLength 106 vs 124 ns,
IsFloat 31 vs 39 ns per pass over all types).

typeswitch_baseline.txt: two entries removed, both switches in types.go
that are now table rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fengttt fengttt changed the title refactor(types): type dispatch design (#10181), oidscan tool, and first DML dispatch fixes refactor(types): type dispatch design (#10181), oidscan, DML dispatch fixes, traits table Nov 26, 2026
@matrix-meow matrix-meow added size/XXL Denotes a PR that changes 2000+ lines and removed size/XL Denotes a PR that changes [1000, 1999] lines labels Nov 26, 2026
@fengttt
fengttt merged commit 9531e59 into matrixorigin:main Nov 26, 2026
33 checks passed
fengttt added a commit that referenced this pull request Nov 26, 2026
…#10181 steps 4, 4b) (#33339)

## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [x] Code Refactoring

## Which issue(s) this PR fixes:

issue #10181

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

**Stacked on #33338.** This branch contains #33338's commits. Review
only the last three: `cea83426fa` (oidscan `-list`/`-dir`), `d96e48d87d`
(vector type-ops table, step 4) and `2ef75a7532` (step 4b). Once #33338
merges, this PR's diff shrinks to those three.

This is steps 4 and 4b of `docs/design/20261126-type-vft.md`.

**`pkg/container/vector/type_ops.go`**
- One row per type id, giving the Go element type that stores it
(`fixedOps[T]()` or `varlenOps()`).
- The 7 type ids no operation here supports (`any`, `star`, `int128`,
`uint128`, `interval`, `Objectid`, `tuple`) are listed explicitly.

**Moved onto the table:** `GetAny`, `AppendAny`, `Shrink`,
`ShrinkByMask`, `Shuffle` and `shuffleWithBufData`.
- Each used to switch over 47 type ids. I extracted their switches
mechanically and all six had the same type-to-Go-type mapping.
- Each keeps its own behavior for an unsupported type: `GetAny` returns
nil, `AppendAny` does nothing, and the others panic with their own
message.

**No change in behavior**
- The six switch implementations are kept in `type_ops_legacy_test.go`.
- `TestTypeOpsMatchLegacy` runs old and new on random vectors of every
type id, with NULLs and with varlenas both inline and in the area. It
compares returned values, resulting vectors, errors and panics, and
checks the mpool is back to zero afterwards.
- I checked the test catches a wrong row: `int16` for `date` (wrong
size), and `uint32` for `date` (right size, wrong Go type).
- `TestTypeOpsCoverEveryOid` fails when a `T_` constant is neither a row
nor listed as unsupported.
- `TestTypeOpsRows` checks each row's element size against
`types.T.TypeLen` and its varlen flag against the type.

**Step 4b: five more on the table**
- `getUnionAllFunction`: 853 lines of hand-written per-type closures
become `unionAllFixed[T]` and `unionAllVarlen`. `T_any` keeps its own
closure, and the local `unionNsp` helper moves to package level
unchanged.
- `MakeAppendBytesFunc` and `NewFunctionResultWrapper` read the
fixed-size case from the table. Variable-length types are still handled
first, as before.
- `canonicalVectorTypeSize` and `isVarlenaMarshalType` read the table
instead of listing the type ids again.
- `TestTypeOpsConstructorsMatchLegacy` compares old and new on every
type id: union from plain, const and const-null sources into a vector
with a different data area; append-bytes; function result; and canonical
size, with right and wrong sizes. I checked it fails when a varlena type
is given the fixed-size union. My first version missed that, because it
appended into a copy of the source, whose identical area hid the bug.
- Union benchmarks are within ±1%.

**`getConstSetFunction` stays a switch.** Its closures run once per
probe row per column in `SetJoinBatchValues`. A generic version measured
about 1 ns (8%) slower per call (12.0 → 13.0 ns), because Go compiles
one body per memory layout with type lookups at run time, while the
hand-written closures are fully specialized. Instead,
`TestConstSetCoversTypeOps` ties the switch to the table: it must return
a function for every supported type id and panic for every other one
except `T_any`. A new type therefore can't be forgotten there either.
The design doc records this as a general rule for switches on per-row
paths.

**What stays in `vector`:** 12 switches that encode meaning rather than
storage. They are formatting (`String`, `RowToString`), ordering
(`GetMinMaxValue`, `compareVectorRows`, the sort functions,
`supportsInplaceSort`), summing with widening (`GetSumValue`), and small
helpers.

**Step 4 benchmarks** (6 runs each): within noise.

| | table | switch |
|---|---|---|
| `GetAny` int64 | 10.2 ns | 10.1 ns |
| `GetAny` varchar | 19.6 ns | 19.3 ns |
| `GetAny` Blockid | 13.1 ns | 13.2 ns |
| `Shrink` (Dup + Shrink + Free) | unchanged | |

**Also:** `optools/oidscan` gains `-list` and `-dir`, which print one
package's switches. That's how the six were picked.

**Tests**
- The full `container/vector` suite, all of `sql/colexec/...`,
`sql/plan/function`, `container/batch`, `container/pSpool` and
`vm/engine/tae/containers` pass, after both 4 and 4b.
- All of `./pkg/...` builds.
- `molint` and `golangci-lint` report 0 issues on `container/vector`.
- `typeswitch_baseline.txt` is unchanged (the six switches were
complete).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
fengttt added a commit to fengttt/matrixone that referenced this pull request Nov 27, 2026
Upstream squash-merged the commits this branch stacked on (matrixorigin#33338,
matrixorigin#33339, matrixorigin#33342). The merged tree is upstream/main plus this branch's
step 5 change; conflict in containers/utils.go UpdateValue resolved to
keep main's const and read-only checks (SetNull/UnsetNull) on top of the
SupportsAny/SetAny dispatch. value_access_test uses Vector.GetValue,
since containers.GetValue was removed by matrixorigin#33342.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

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

kind/documentation Improvements or additions to documentation kind/enhancement kind/refactor Code refactor size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants