Skip to content

[Subtask]: Hoist invariant decimal cast metadata and scale proofs out of row loops #30839

Open

Description

@aunjgr

Parent Issue

#30838

Detail of Subtask

Validation update — 2026-11-02

Implementation is delivered; current-main focused decimal tests and public SQL BVT pass. Current optimized cast proof calls are outside row loops, and Q1/Q15 selected-kernel/CPU diagnostics have been collected. Overall performance acceptance remains OPEN for the missing SF100/mixed-I/O and other applicable gates. Server129 validation reuses its existing loaded data in place, with no image copy or reload. Queued work is not a measured pass. See the validation follow-up comment for exact source, executable and scoped evidence.

Evidence reassessment — 2026-10-26

Current delivery/performance status: PARTIAL DELIVERY. #30880 introduced the invariant hoist; merged correction #30981 addresses empty/NULL/setup costs and reports isolated local SF10 acceptance. The original all-NULL regression is historical, not an unresolved finding against current main. SF100/mixed-I/O and current-baseline completion evidence remain required.

Observed: Historical Q1 exclusive user leaves in type/scale proof helpers total about 18.62% at SF10 and 20.10% at SF100. Pinned source and optimized assembly place batch-invariant proof calls inside non-NULL cast loops. This established redundant work in that revision, not a remaining 20% opportunity after the merged fixes.

Not yet established: How much redundant proof/cast work remains on current main, and which entrypoints still own it. A hot type helper can serve multiple callers; its whole sample share is not the savings of one cast change.

Next discriminating experiment: Reprofile the current cast paths after #30981. Attribute each remaining metadata/proof call to its cast entrypoint and report calls/instructions per non-NULL value plus absolute query CPU. First confirm the proof has not already moved outside the loop.

Measured target: Unchanged Q1 and equivalent decimal cast workloads; show reduced remaining proof work and target latency/CPU after the current baseline.

Mandatory controls and total cost boundary: Empty and flat all-NULL batches; constants; widening/non-widening scales; same-scale paths; rejected values; scalar/tiny/tail batches. Include setup and broadcast cost.

Performance acceptance and rejection

  • Freeze current-main/candidate sources, executables, real caller shapes and effective settings. Use the unchanged qualification SQL and complete result consumption; exclude cold runs and keep profiles separate from latency scoring.
  • Demonstrate the named work/CPU reduction and an attributable uninstrumented target-query improvement. Include the controls below and full Q1-Q22 comparison; component wins, source changes, functional tests, SCA, merging and profile percentages are separate evidence.
  • A confirmed affected/control slowdown fails; unresolved slower signals are inconclusive. Confirm with an independent longer balanced run. No gain elsewhere, favorable average or self-approved tolerance waives a regression.
  • Complete applicable SF10/SF100 and memory/mixed-I/O acceptance before issue closure under the umbrella protocol. Report uncertainty, absolute CPU/instructions/work, allocations/copies and the same whole-application budget, effective cache, I/O/spill/swap/pressure evidence. Benchmark scripts do not verify input/table/schema/row counts/reference results/checksums.

Semantics and lifecycle requirements retained

  • Type/scale decisions that are constant for one batch are absent from its non-NULL row loop; optimized code or per-row instruction evidence demonstrates the removal.
  • Focused tests cover empty/constant/nullable vectors, widening and non-widening scales, overflow, rounding and error behavior without changing SQL metadata.
  • Report warmed Q1 CPU/instructions and uninstrumented latency before/after at the same binary settings, image, CPU and memory budget; exclude cold runs.
  • Report the measured gain without extrapolating the 20.10% sample share to a 16× speedup: eliminating that entire fraction alone has a conditional user-CPU ceiling near 1.25×.

Describe implementation you've considered

Keep the current batch-level proof when it is already adequate. Hoist only genuinely invariant remaining work; retain value-dependent precision, scaling, rounding and overflow checks. Empty/all-NULL/constant routes must avoid unnecessary setup.

Coordination: Coordinate domain inference with #30840; do not count the earlier hoist again as a new result.

Additional information

Pinned implementation / plan references

Activity

  1. self-assigned this
    on Oct 22, 2026
  2. added
    severity/s0
    on Oct 22, 2026
  3. XuPeng-SH commented on Oct 22, 2026

    @XuPeng-SH
    Contributor

    转测:修复已合入(性能子任务),本地只能做微基准,压测验收交 QA

    • 修复 PR:#30880 perf(function): hoist decimal cast invariants(已合入),无其他 OPEN PR。
    • 本地可验证的部分:decimal 乘法/除法内核在多精度组合下的结果与标量路径一致(单包/微基准级别)。
    • 无法验证的部分:本子任务所属 epic(#30838 TPC-H SF10/SF100)要求的端到端性能收益。

    转测:assignee 从实现者改为 QA(Ariznawlll),保持单一 owner;验收请在具备 SF10/SF100 环境的机器上做收益对比。

  4. aunjgr commented on Nov 2, 2026

    @aunjgr
    ContributorAuthor

    Validation follow-up — 2026-11-02: implementation present; issue stays OPEN pending remaining performance acceptance.

    The original invariant-hoist problem is absent from all five owned cast paths in the current optimized executable. Each has exactly one canWidenDecimalScale call in setup; source AST inspection finds zero GetType/ToType/proof calls inside their row loops. An independent control-flow audit of optimized disassembly also places all type/scale proof calls outside instruction cycles, excluding only Go's pre-execution stack-growth restart:

    Cast function Optimized proof-call address In a row-loop cycle?
    decimal64ToDecimal64 0x3808548 No
    decimal64ToDecimal256Array 0x3809508 No
    decimal128ToDecimal128 0x380a1c8 No
    decimal128ToDecimal256 0x380a688 No
    decimal256ToDecimal256 0x380b148 No

    The current cast implementation retains early empty/NULL handling, batch proofs and value-dependent checked fallbacks. Passing precision/scale/NULL tests cover the relevant semantics. The current Q1/Q15 profiles have no sampled leaves in canWidenDecimalScale or types.T.ToType; sampling alone is not proof of zero calls, so the source/assembly proof above is retained.

    Reuse, without rerunning, the separately isolated SF10 Q1-Q22 campaign for merged #30981: Q1 medians 1.646204 -> 1.467484 s; median paired change -9.86%, ratio interval [0.8908, 0.9084]. This is the historical isolated correction result, not a new current-main comparison or a gain to add to #30880.

    Current-main validation snapshot: be8eb40ad8bb7b35cbd3c1f50a5a6d23e1f6da1e. Normal release build, Go 1.27.1, GOAMD64=v3, GOEXPERIMENT=simd, no source overlays/PGO. Executable SHA256: 0f4f07c1d39b32e08dcd0c100531cb50843500d74ae6380bd3704a7596647aae. This is current-code correctness/kernel evidence, not a before/after latency comparison of all changes merged since the original fixes.

    Focused function/planner/Substrait decimal tests pass (483 named top-level tests in the selected output, including producer-domain, legacy carrier, scale/rounding, prepared-execution and precision controls). SQL BVT passes 450/450 distinct statements on each of two same-instance runs: decimal intermediate precision 58, decimal multiply/subtract 18, casts 374; zero failures/ignores. The owned service stopped cleanly with SIGINT. These are semantic tests; the benchmark worker performs execution/draining only, with no input/table/schema/row/reference/result/checksum verification.

    Separate warm CPU/PMU diagnostics, 16 completed calls/query after the selected cold call was excluded:

    Query Whole-application CPU seconds/call User instructions/call Conservative whole-application peak, GiB
    Q1 6.746071 155,514,649,774 7.448
    Q15 1.672838 23,689,671,853 6.891

    Both local SF10 profiles satisfy the existing all-memory gates: eight physical application CPUs, 16 GiB effective CN SHARED cache, 32 GiB whole-application budget including native image/pinned software, zero measured physical reads/writes, spill, swap, OOM, major faults, direct reclaim and measured memory-pressure stalls. Profiling is separate from scored latency. Raw PMU counts include the measured application process scope; sample percentages are user-space diagnostics, not savings estimates.

    Server129 already retains the loaded SF100 database. The revised runner reuses it in place, sequentially, with no image/data transfer, copy or reload. It uses all 64 CPUs, a 200 GiB application / 192 GiB CN cache all-memory context and a 32 GiB / 16 GiB warm disk-backed context; actual I/O is recorded. A warm run is accepted as all-memory only if measured physical I/O/spill/swap are zero. Tiny configuration and diagnostic-output files are separate from the database. PSI availability limitations remain explicit.

    The queued SF100 work is currently waiting for a quiet resource window; no benchmark service has launched. These queued jobs supplement the isolated #30981/#30983 Q1/Q15 evidence and current-main kernel/plan evidence. They do not claim that #31034's SF100 effect, every full-suite/context requirement or this issue's complete performance acceptance has already passed. A resource-rejected arm is retained and is not silently staged or relabeled.

  5. Ariznawlll commented on Nov 16, 2026

    @Ariznawlll
    Collaborator

    QA 验证补充:cast invariant 提升的功能与白盒核对通过(性能验收门仍未完成,issue 保持 OPEN)

    测试版本

    白盒核对(当前源码)

    对 pkg/sql/plan/function/func_cast.go 逐处核对 canWidenDecimalScale proof 调用位置:

    Cast 路径 proof 调用行 行循环起始行 proof 在循环外
    decimal64ToDecimal64 6696 6704 ✓
    decimal64ToDecimal128Array 6743 6769 ✓
    decimal64ToDecimal256Array 6913 6924 ✓
    decimal128ToDecimal128 7101 7110 ✓
    decimal128ToDecimal256 7167 7178 ✓
    decimal256ToDecimal256 7331 7340 ✓

    全部 6 处批不变 proof 调用均位于行循环之前的 setup 段,与交付声明一致。该核对为源码结构确认,未独立复现优化后二进制的反汇编地址证据(沿用交接记录)。

    功能验证

    1. 跨精度 cast 值保留:decimal(18,4)→decimal(30,8)、decimal(30,8)→decimal(40,12)、同精度 256→256 均逐位精确;缩窄 decimal(30,8)→decimal(18,4) 按 scale 正确舍入(12345678.12345678 → 12345678.1235)。
    2. 零值、NULL、混合 scale 的 widening 保持值与 NULL 契约。
    3. 交接契约中的域推断边界(price * (1 - discount) 列操作数 → decimal(37,4))在 SQL 元数据上逐字复核通过(详见 [Subtask]: Preserve sound decimal domains and avoid unnecessary wide arithmetic #30840 的验证 comment)。

    Go 单测

    TestDecimal|TestArithDecimal(pkg/sql/plan/function)本地 ok。

    回归测试

    cast 功能契约已并入 matrixorigin/motr#349 的 14_issue_regression/issue_30840_decimal_domain(37 语句)。PR 4 项 CI 全部成功,用例标记 @target-branch: main。

    验证边界(保持 OPEN 的原因)

    本子任务的核心验收是 warmed Q1/Q15 的 CPU/指令数与未插桩延迟的前后对比(冻结源、同机同预算、排除冷启动),以及 full Q1–Q22 对照和分配/拷贝预算——本地不具备该基准条件,未执行也不宣称任何性能收益数字。原 20.10% 样本份额不可外推为整体加速比(交接记录已注明条件上限约 1.25×)。功能与白盒核对通过不等于性能验收通过,issue 维持 OPEN,待基准环境数据补齐。

  6. Ariznawlll commented on Nov 18, 2026

    @Ariznawlll
    Collaborator

    Note

    QA 性能面补充验证:大数据量真机基准已实测(main 517e169538),功能面此前已通过。本 subtask 的可测性能证据已交付;最终"目标倍数"裁定权在研发/负责人,QA 证据供其引用。保持 OPEN 至父项策略裁定。

    QA 补充:大数据量真机基准(#30839 cast invariant hoist / #30840 decimal 域)

    此前功能面验证(2026-11-16)已通过。本.comment 补性能面证据。

    测试环境

    • main 隔离实例 517e1695388cbaf1135238d5ab0c19a4556fb551(git_version() 实测确认),1 LOG / 1 TN / 1 CN,Darwin arm64 8 核 16GB。
    • 数据集:qa_big.lineitem 5,498,998 行(1M 订单、DECIMAL(15,2) 价量),partsupp 800,000 行 ⋈ part 200,000 行。

    大数据量计时(warm 1 + 3 轮,取 best)

    工作负载 触及修复路径 结果
    5.5M 行 l_extendedprice * l_quantity 乘法 + 全量 GROUP SUM(三组,组值达 4.06e12,超 DECIMAL(15,2) 单行域宽——域拓宽分派正确) #30840 sound domains / #30839 cast hoist best=0.131s,三组值与真值一致(A 1.157e12 / B 1.159e12 / C 4.055e12)
    1M 组 shuffle 聚合 group by l_orderkey + sum(price*qty) 同上 best=0.139s
    白盒:pkg/sql/plan 定向 219 项 Decimal/Cast 单测 #30839/#30840 代码路径 全部 PASS(ok 1.75s,0 FAIL)
    白盒:aggexec(聚合执行核心)全包 #30840 PASS(3.17s)

    结论

    • 大数据量(550 万行)下 decimal 乘法+聚合、百万组 shuffle 聚合均在亚秒级完成,结果精确;此前功能面已证明精确推断/保守回退契约。性能面证据:Load 下无法对照修复前版本(修复 PR 早早合入且父项要求 SF10/SF100 专机对照),本基准作为当前 head 的实测性能记录,供研发对照其历史 SF10 数字评估是否达成目标。
    • 无回归、无 panic、无精度丢失(4e12 量级总值仍精确表示)。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/subtaskseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions