Skip to content

fix: correct temporal numeric coercion for bit consumers - #32642

Merged
mergify[bot] merged 6 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-31904-main-b9a9160b
Nov 17, 2026
Merged

mergify[bot] merged 6 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-31904-main-b9a9160b

Conversation

@gouhongshen

@gouhongshen gouhongshen commented Nov 12, 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:

Related to #31904.

What this PR does / why we need it:

Temporal values reached integer consumers through string/provisional domains. As a result, &, |, and ^ could return 255-byte binary strings for DATE/DATETIME/TIME instead of the numeric results returned by MySQL. BIT_COUNT and SEC_TO_TIME also lacked complete native temporal conversion and prepared rebinding behavior.

Root causes

  • Bitwise overload selection did not consistently route native temporal operands through temporal-to-integer conversion.
  • BIT_COUNT lacked native temporal overload/rebinding paths, and SEC_TO_TIME used text-oriented conversion that lost native source scale.
  • After a prepared numeric latch, generic strict CAST handling rejected later string executions instead of applying numeric-prefix conversion and warning 1292.
  • A remembered BIT_COUNT numeric target was also applied before native temporal binding, so fractional TIME was truncated and DATETIME rounding/calendar carry was lost.

Changes

  • Add temporal-only integer argument classification and binding for &, |, ^, <<, >>, and BIT_COUNT while preserving existing numeric, text, plain-hexadecimal, and explicit-binary behavior.
  • Bind native DATE/DATETIME/TIME/TIMESTAMP through the existing temporal integer CAST, add source-scale SEC_TO_TIME handling, and rebind prepared native temporal parameters without mutating the prepared template.
  • Add binder-only prepared BIT_COUNT string conversion for signed/unsigned/DOUBLE/DECIMAL latches, including numeric prefixes, NULL handling, saturation, and row-scoped warning 1292 diagnostics.
  • Keep native temporal parameters on the temporal conversion path after numeric latches. Document the two intentionally partial type switches; their canonical latch targets cannot include other float-family members.
  • Keep BIT_COUNT's dedicated prepared fallback positions out of generic numeric metadata, and include them in native temporal runtime decoding after initial prepare and re-prepare.
  • Add focused planner UTs and SQL regressions for literals, columns, NULL/zero values, fractional and negative TIME, calendar carry, TIMESTAMP zones, result scale, warnings, prepared reuse, and text/binary counterexamples.

The original row now produces d & 3 = 1, dt | 1 = 20240101100001, tm ^ 1 = 10202, and d << 1 = 40480202. On one prepared cursor, DOUBLE 64.0, native TIME 00:00:00.500000, and native DATETIME 2024-02-29 23:59:59.500000 now produce BIT_COUNT values 1, 1, and 20; later text 2024-01-01 produces 7 with warning 1292, and NULL produces NULL without a warning.

Issue-to-test proof

  • On assigned base 0c8bd5e5f3be4ce5582201513161181ddca37d6d, the focused temporal UT failed all nine primary witnesses. The same coverage passes after the change. The prepared mixed-text regression also failed on base and passes after the prepared binding repair.
  • The later DOUBLE-latch regression failed before the final repair with TIME 0 instead of 1 and DATETIME 25 instead of 20; the identical UT and native-protocol probe pass after the repair.
  • TestTypeSwitchFamilies initially identified the two new partial switches and passes after concrete typeswitch:partial annotations, without expanding its baseline.
  • Independent black-box challenge run 43222436235 passed all 12 valid cases. The final numeric-to-temporal prepared sequence has additional focused UT and canonical native-protocol proof.
  • On PR head c4081ad46b595a64eba59c78aeefe90d8642606a, the UT Coverage regression expected generic numeric positions [0] but observed [0,1]. The identical frontend test now passes with generic positions [0], dedicated BIT_COUNT positions [1], and temporal runtime positions [0,1]; eight related frontend tests and the complete planner package also pass.
  • SQL BVTs passed: temporal_numeric_consumer 57/57 twice, func_bit_count 210/210, func_datetime_sec_to_time 33/33, mysql_compat_temporal_numeric_cast 30/30, temporal_numeric_zero_default_29721 18/18, func_temporal_consistency_28851 458/458, integer_parameter_coercion 118/118, bit_integer_parameter_coercion 47/47, and mysql_compat_bitwise_expr 25/25.

Validation commands

.agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=180s -run '^TestPreparedBitCountStateAdvancesAlongsideOtherNumericOverloads$' -o <turn-temp>/frontend.test ./pkg/frontend
.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=20m -o <turn-temp>/plan.test ./pkg/sql/plan
.agents/skills/mo-dev/scripts/mo-cgo-test -o <turn-temp>/plan-r4.test ./pkg/sql/plan -run '^TestPreparedExecutionPlanConsumerDomains/bit_count_latched_double_' -count=1
.agents/skills/mo-dev/scripts/mo-cgo-test -o <turn-temp>/plan-r4.test ./pkg/sql/plan -run '^TestPreparedExecutionPlanConsumerDomains$' -count=1 -v
.agents/skills/mo-dev/scripts/mo-cgo-test -o <turn-temp>/plan-base-refresh.test ./pkg/sql/plan -run '^(TestTemporalNumericConsumers|TestTemporalNumericConsumerColumns|TestPreparedTemporalNumericConsumers|TestSecToTimeTemporalColumns|TestPreparedExecutionPlanConsumerDomains)$' -count=1 -v -timeout=120s
.agents/skills/mo-dev/scripts/mo-cgo-test -o <turn-temp>/plan-base-refresh.test ./pkg/sql/plan -run '^(TestCRC32JSONTextWithExistingFunctionIdentity|TestCRC32UntypedPreparedRebindingPreservesDomains|TestCRC32PreparedAndLegacyBinding)$' -count=1 -v -timeout=120s
.agents/skills/mo-dev/scripts/mo-cgo-test -o <turn-temp>/plan-r4.test ./pkg/sql/plan -count=1
.agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=20m -o <turn-temp>/function-package-final.test ./pkg/sql/plan/function
.agents/skills/mo-dev/scripts/mo-cgo-test ./pkg/container/types -run '^TestTypeSwitchFamilies$' -count=1

go vet -mod=readonly ./pkg/sql/plan ./pkg/sql/plan/function ./pkg/frontend
golangci-lint run -c .golangci.yml --new-from-rev 0c8bd5e5f3be4ce5582201513161181ddca37d6d ./pkg/sql/plan ./pkg/sql/plan/function
make build

# Run from mo-tester with the isolated validation configuration; substitute each listed case path.
bash run.sh -p <case-path> -m run -g -n

# Canonical pure Connector/Python prepared native-protocol sequence.
MO_PORTS=46001 PYTHONPATH=<pure_client_site> PYTHONNOUSERSITE=1 PYTHONDONTWRITEBYTECODE=1 python3 canonical_bit_count_numeric_latch.py

The final complete planner package passed in 176.570s, the function package passed in 49.541s, the build and both static checks exited 0, and incremental lint reported 0 issues.

The CI metadata repair passed its exact frontend regression, eight related frontend tests, the complete planner package in 99.596s, and go vet for the planner and frontend packages.

Base refresh merge 16f7b4c94044c3752471b2c1257768b820d9e6df incorporates main through f491aaab4519b29313c5747d3ce3b3f84e865c71. Both focused planner selections above passed at that merge head, including the prepared-CRC32 controls sharing ResetParamRefRule; the PR diff remains scoped to the same 17 files and git diff --check passes.

Residual risks and limitations

  • Repository-wide SCA and the full BVT suite remain for CI. Focused and related packages/BVTs listed above passed locally.
  • Native Connector/Python datetime.timedelta verification uses the pure encoder (use_pure=True or a pure-only installation); the optional 9.4.0 C encoder emits malformed prepared TIME payloads.
  • Existing prepared BIT_COUNT DOUBLE extreme-value differences from MySQL are unchanged and outside this temporal-conversion repair.

Convert DATE, DATETIME, TIME, and TIMESTAMP through their numeric
semantics for bitwise operators, BIT_COUNT, and SEC_TO_TIME. Preserve
native temporal conversion across prepared numeric latches while keeping
string and binary behavior unchanged.

Add planner, SQL, and native-protocol regressions for literals, columns,
fractions, warnings, prepared reuse, and NULL handling.

Related to matrixorigin#31904.
Keep BIT_COUNT's dedicated prepared fallback marker out of the generic
numeric collector. Include the dedicated positions in native temporal
packet decoding, using current metadata when a prepared plan is rebuilt.
@gouhongshen

gouhongshen commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

CI triage for MatrixOne ALL CI run 43869576010, attempt 2, head bd572a949f4cb9b0c5382f8f2519ecd303d19680:

The UT blocker remains unresolved. Attempt 2 was the single permitted rerun, so no further rerun was requested. No PR code or tests were changed for this disposition.

@gouhongshen
gouhongshen marked this pull request as ready for review November 16, 2026 12:24
@mergify

mergify Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

✅ Merged into main as e765c5f at 00:05 UTC

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

So far

  • ✅ Entered the queue Tue 17 Nov, 00:04 · rule main · triggered by rule Automatic queue on approval for main
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged 00:05 · e765c5f · squash
3 merge conditions met
  • #review-threads-unresolved = 0 · required by GitHub branch protection
  • github-review-approved · required by GitHub branch protection
  • any of · required by GitHub branch protection:
    • check-success = CI Required
    • check-neutral = CI Required
    • check-skipped = CI Required

Times in UTC · dashboard · how the merge queue works

@mergify mergify Bot removed the queued label Nov 17, 2026
XuPeng-SH pushed a commit that referenced this pull request Nov 20, 2026
## What type of PR is this?

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

## Which issue(s) this PR fixes:

issue #32873

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

Unary bitwise NOT (`~`) rejected native DATE, TIME, DATETIME and
TIMESTAMP operands:

```sql
SELECT ~CAST('2024-06-18' AS DATE);
-- ERROR 20203 (HY000): invalid argument operator unary_tilde, bad value [DATE]
```

### Root cause

#32642 made the native temporal-to-integer conversion a per-parameter
contract (`integerParameters` with `temporalOnlyIntegerParameter`). That
declaration drives both argument-type normalization
(`checkArgumentTypes`) and the binder's
`appendSourceDependentIntegerArgument`, which inserts
`TemporalIntegerArgumentCastOverload`. It was added to `&`, `|`, `^`,
`<<`, `>>` and `BIT_COUNT`, but not to `UNARY_TILDE`. For `~`,
`unaryTildeTypeMatch` only adds an implicit cast for CHAR/VARCHAR/TEXT,
so a temporal operand fell through to direct overload matching and was
rejected.

### Fix

Declare `integerParameters: {{position: 0, mode:
temporalOnlyIntegerParameter}}` on `UNARY_TILDE`. No new mechanism is
needed. For any non-temporal source, `sourceTarget` reports "not
applicable", so integer, text, binary and BIT behavior is unchanged.

### Validation

- The new planner UTs fail on the base commit with `invalid argument
operator unary_tilde, bad value [DATE|DATETIME|TIME|TIMESTAMP]` and pass
with the fix. They cover literals, columns (NULL, zero value, negative
and fractional TIME, TIMESTAMP in UTC), a prepared `~?` with native
temporal parameters and template reuse, and integer/text controls.
- BVT `temporal_numeric_consumer` is extended with `~` cases, including
the equivalence from the issue (`~(d & -1) = ~CAST(d AS SIGNED) =
18446744073689310997`). `.result` was generated by mo-tester against a
locally built standalone service. The case passes 60/60. Related cases
also pass: `func_bit_count` 210/210,
`mysql_compat_temporal_numeric_cast` 30/30,
`bit_integer_parameter_coercion` 47/47, `integer_parameter_coercion`
118/118, `mysql_compat_bitwise_expr` 25/25.
- Go packages `./pkg/sql/plan` and `./pkg/sql/plan/function` pass in
full, and so does `TestTypeSwitchFamilies`. `go vet` and incremental
`golangci-lint` report no issues.
- Not run locally: the full BVT suite and repository-wide SCA, which are
left to CI.

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

This branch was previously deployed

1 inactive deployment
ci — 16f7b4c9 Deployed Nov 16, 2026 by gouhongshen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants