You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Optimizer] TPCH Q15 executes the same multi-reference CTE twice #26131
The canonical TPCH Q15 shape defines q15_revenue0 once but references it both in the supplier join and in the scalar MAX(total_revenue) subquery. MatrixOne inlines and fully executes the CTE twice, including two full scans and two identical grouped aggregates over lineitem.
Reproduction
Environment: local standalone MatrixOne at commit 9c2e84ad5c, TPCH SF10.
with q15_revenue0 as (
select l_suppkey as supplier_no,
sum(l_extendedprice * (1- l_discount)) as total_revenue
from lineitem
where l_shipdate >=date'1995-12-01'and l_shipdate <date'1995-12-01'+ interval '3' month
group by l_suppkey
)
select s_suppkey, s_name, s_address, s_phone, total_revenue
from supplier, q15_revenue0
where s_suppkey = supplier_no
and total_revenue = (selectmax(total_revenue) from q15_revenue0)
order by s_suppkey;
EXPLAIN ANALYZE contains two independent copies of the same subtree. Each copy:
scans all 59,986,052 lineitem rows / 7,323 blocks
applies the same three-month date filter and outputs 2,265,119 rows
groups by l_suppkey and outputs the same 100,000 groups
consumes about 1.34 GiB input and 83.81 MiB scan memory
The second copy exists only to calculate MAX(total_revenue) from the same CTE result.
Expected behavior
For a deterministic CTE referenced multiple times, the optimizer should cost alternatives and reuse/materialize the result when that is cheaper than recomputation. Q15 should calculate the 100,000-row revenue relation once and feed both consumers from it.
Possible implementations include shared CTE materialization, common-subplan reuse, or a Q15-equivalent rewrite that computes the maximum from the already-produced revenue stream.
Please add a plan-shape regression test that verifies the base lineitem aggregate is not duplicated for this multi-reference CTE.
PR #26426 merged as b2aec782dc16717ee38c53d2c5961481781b2882 from final PR head 24f17f687e78e2a0b765cfca462b90fe9f442bdd.
QA Decision
required: yes
reason: this changes user-visible multi-reference CTE planning and distributed execution, including multi-CN remote producer topology, query cancellation/cleanup, and bounded materialized spill resources.
production_entrypoint: TPCH SF10 Q15 on a multi-CN deployment, where one eligible CTE producer is consumed by the supplier join and the scalar MAX(total_revenue) subquery.
automated_terminal_coverage: final-head MatrixOne CI passed UT, SCA, coverage, Proxy/Pessimistic BVT, and the exact-head TKE check-in run passed TPCH10G, SSB, SYSBENCH, and TPCC. Focused planner/compile/materialized tests cover one producer, two readers, per-CN producer grouping, cancellation, spill admission, resource cleanup, and reuse.
remaining_validation: verify the final merged main build through both CN entry points, including plan shape, cancellation/reuse, spill limits, and stable-host performance.
Existing local evidence on the earlier PR head d782957e37a5b46af957609b7edce03640c2379a:
stock 1 Log + 1 TN + 2 CN topology, tested independently through both CN ports
both entries returned supplier 33987, revenue 2296670.4443
cardinalities: lineitem 59,986,052, date filter 2,265,119, grouped suppliers 100,000
EXPLAIN ANALYZE showed one scan (59,986,052 -> 2,265,119), one grouped SUM (2,265,119 -> 100,000), and two Sink Scan consumers of the same producer
120 ms client cancellation disappeared from the other CN's process list within 1 second; the immediate rerun succeeded
all clusters exited cleanly and released their ports
The final head additionally includes f26b7629d74211ce916206d23af34bc1536d5ee0, which bounds materialized CTE spill memory, disk, and file-descriptor resources. The earlier local A/B/A therefore does not replace final-head QA.
QA scenarios on merged main:
Run TPCH SF10 Q15 through CN1 and CN2 independently; both must return supplier 33987 and revenue 2296670.4443.
Confirm EXPLAIN/EXPLAIN ANALYZE has exactly one lineitem scan/filter/grouped-SUM producer and two Sink Scan consumers of that producer; the second consumer must feed MAX(total_revenue).
Cancel Q15 while running, inspect it from the other CN, and verify it disappears promptly with no residual pipeline; rerun immediately and verify the correct result.
Exercise a constrained spill limit so materialized overflow either succeeds within the admitted budget or fails cleanly before unbounded allocation/file growth; after completion or failure, memory, spill bytes, and file-descriptor charges must be released.
On a stable or dedicated host, compare the merged implementation against the inline-CTE baseline. Report correctness and plan shape regardless of timing; publish a performance percentage only when the clean A1/A2 stability gate is within 10%.
Pass: both CN entries are correct, only one producer executes, cancellation/reuse leaves no residual query, spill resources remain bounded and are released, and no material performance regression is observed on a stable host.
Fail: wrong result, duplicate producer execution, remote-pipeline error, cleanup timeout/residual query, spill budget/resource leak, or a stable-host performance regression.
with q15_revenue0 as (
select
l_suppkey as supplier_no,
sum(l_extendedprice * (1- l_discount)) as total_revenue
from
lineitem
where
l_shipdate >=date'1995-12-01'and l_shipdate <date'1995-12-01'+ interval '3' month
group by
l_suppkey
)
select
s_suppkey,
s_name,
s_address,
s_phone,
total_revenue
from
supplier,
q15_revenue0
where
s_suppkey = supplier_no
and total_revenue = (
selectmax(total_revenue)
from
q15_revenue0
)
order by
s_suppkey;
TPCH_10:
This time: sum(cost) = 12094
Last two times: 11610, 13101
Result: not 25.0% higher than the last one
TPCH_100:
This time: sum(cost) = 104849
Last two times: 94841, 98131
Result: not 25.0% higher than the last one
TPCH_1000:
This time: sum(cost) = 1041933
Last two times: 1042567
Result: not 25.0% higher than the last one
当前覆盖范围
本次证据可以支持:
多 CN TKE 环境下 TPCH Q15 返回正确结果。
Q15 原始多引用 CTE SQL 被实际执行。
EXPLAIN 中 lineitem revenue 聚合只作为一个 Plan 0 producer 出现。
Activity
perf(plan): reuse eligible multi-reference CTEs (#26331)
fix(compile): group materialized CTE producer scopes by CN (#26426)
VioletQwQ-0 commentedon Jul 31, 2026
PR #26426 merged as
b2aec782dc16717ee38c53d2c5961481781b2882from final PR head24f17f687e78e2a0b765cfca462b90fe9f442bdd.QA Decision
MAX(total_revenue)subquery.Existing local evidence on the earlier PR head
d782957e37a5b46af957609b7edce03640c2379a:33987, revenue2296670.444359,986,052, date filter2,265,119, grouped suppliers100,000EXPLAIN ANALYZEshowed one scan (59,986,052 -> 2,265,119), one grouped SUM (2,265,119 -> 100,000), and twoSink Scanconsumers of the same producerThe final head additionally includes
f26b7629d74211ce916206d23af34bc1536d5ee0, which bounds materialized CTE spill memory, disk, and file-descriptor resources. The earlier local A/B/A therefore does not replace final-head QA.QA scenarios on merged main:
33987and revenue2296670.4443.EXPLAIN/EXPLAIN ANALYZEhas exactly one lineitem scan/filter/grouped-SUM producer and twoSink Scanconsumers of that producer; the second consumer must feedMAX(total_revenue).Pass: both CN entries are correct, only one producer executes, cancellation/reuse leaves no residual query, spill resources remain bounded and are released, and no material performance regression is observed on a stable host.
Fail: wrong result, duplicate producer execution, remote-pipeline error, cleanup timeout/residual query, spill budget/resource leak, or a stable-host performance regression.
Final-head CI: https://github.com/matrixorigin/matrixone/actions/runs/30544294369
Final-head TKE check-in: https://github.com/matrixorigin/matrixone/actions/runs/30546239273
Ariznawlll commentedon Aug 6, 2026
TPCH 多 CN 回归验证证据
针对 issue #26131,本次先补充 TPCH 多 CN 维度的验证证据。该评论只覆盖 TPCH 正确性、Q15 多引用 CTE 计划形态和 TPCH 性能门禁;取消清理与受限 spill 资源边界仍需单独补测。
测试版本
MO Main Nightly Regression On TKEMain Nightly Regression Test / TPCH BENCHMARK TEST92390629023successmainab28d1b4ac41d0bef6b0800a2c6c455aaacd2fdab2aec782dc16717ee38c53d2c5961481781b2882为ahead_by=152, behind_by=0测试环境
mo-main-commit-ab28d1b4a-20260805Running,RESTARTS=0Running,RESTARTS=0Running,RESTARTS=0Running,RESTARTS=0cn_count=3,cn_limits_memory=55Gi,cn_memory_cache_size=12Gi,cn_gomemlimit=25000MiB启动时 pod 摘要:
原问题 SQL
mo-tpch/queries/q15.sql使用的是原问题的多引用 CTE 形态:q15_revenue0先聚合lineitem,随后同时被 supplier join 和select max(total_revenue) from q15_revenue0标量子查询消费。正确性验证
TPCH job 中
TPCH_1、TPCH_10、TPCH_100、TPCH_1000均为PASS。SF10 的 Q15 结果与 golden 对比通过,golden 为:
Q15 执行记录:
Q15 计划形态
SF10
EXPLAIN显示 Q15 使用AP QUERY PLAN ON MULTICN(14 core)。计划中只有一个lineitemscan/filter/group producer,即Plan 0;supplier join 和MAX(total_revenue)两个 consumer 都通过Sink Scan读取同一个DataSource: Plan 0。SF100 和 SF1000 的 Q15 explain 也显示相同复用形态:一个
Plan 0producer,两个Sink Scanconsumer,且两个 consumer 均为DataSource: Plan 0。SF100 关键片段:
SF1000 关键片段:
该计划形态覆盖了 issue 中要求的核心 TPCH 检查点:
lineitemrevenue relation 只生产一次,join consumer 和 scalarMAX(total_revenue)consumer 复用同一个 materialized CTE producer。TPCH 性能门禁
TPCH 汇总结果:
性能阈值检查:
当前覆盖范围
本次证据可以支持:
EXPLAIN中lineitemrevenue 聚合只作为一个Plan 0producer 出现。MAX(total_revenue)两个消费者均通过Sink Scan读取DataSource: Plan 0。仍未由本 run 覆盖:
SHOW PROCESSLIST观察无残留 query/pipeline,并且立即重跑成功。因此,该 run 可作为 #26131 的 TPCH 多 CN 正确性、计划形态和性能门禁证据;最终完整验证仍需要补充取消清理与 spill 资源边界两项证据。
Ariznawlll commentedon Aug 13, 2026
测试版本
main1c292cf20e113b296a3db2cb6a65e00af2ea2327原问题验证
重新验证 TPCH Q15 的多引用 CTE 形态:同一个 revenue CTE 同时供 supplier join 和标量
MAX(total_revenue)消费。最新 main 的计划只有一个 CTE producer,两个消费者均通过Sink Scan读取该 producer;查询返回唯一预期供应商及正确 revenue,没有重复行或漏行。同时在修复前对照版本
9c0f901c2执行相同计划检查,计划中出现两个独立的lineitemTable Scan,Sink Scan=0;最新 main 不再出现该重复执行形态。Happy Path 覆盖
Sink Scan,并精确校验唯一供应商42、名称supplier-42、非空 revenue 和无额外结果行。Sink Scan;结果精确正确。13,162,036bytes,查询完成后资源计费归零;从两个 CN 入口执行及恢复查询均正常。Unhappy Path 覆盖
conn_id/statement 仍可保持COM_QUERY超过 5 秒。该现象是“取消传播/后台查询清理”缺陷,与本 issue 的“同一多引用 CTE 被重复执行”具有不同触发条件和验收标准,不作为 [Optimizer] TPCH Q15 executes the same multi-reference CTE twice #26131 的成功基线;应独立立项并在修复后增加 multi-CN 回归。3015 resource exhausted或20101 internal error;资源释放和后续恢复均正常,但错误分类一致性应作为独立问题处理。白盒验证
count=10通过。-race测试通过。回归测试
Sink Scanconsumer 及精确结果。测试证据
b2aec782dc16717ee38c53d2c5961481781b2882。2 Table Scan / 0 Sink Scan;最新 main1 producer / 2 Sink Scan;spillreserve=release=13,162,036 bytes;超限拒绝后跨 CN 立即恢复成功。测试结论
测试通过。
Ariznawlll commentedon Aug 13, 2026
补充验证中确认的两个派生问题已分别建 Bug,避免与 #26131 原始 CTE 复用/取消问题混在一起:
3015 resource exhausted或20101 internal error。EXPLAIN ANALYZE仍未报告正数SpillSize。两项均已在最新 main
1d7b6311ce91df0968da435bb405f3d68137c595的 2-CN 隔离环境复现,并包含 Happy/Unhappy、无部分结果及恢复性对照。perf(plan): reuse eligible multi-reference CTEs (matrixorigin#26331)
fix(compile): group materialized CTE producer scopes by CN (matrixori…
perf(plan): reuse eligible multi-reference CTEs (matrixorigin#26331)
fix(compile): group materialized CTE producer scopes by CN (matrixori…
perf(plan): reuse eligible multi-reference CTEs (matrixorigin#26331)
fix(compile): group materialized CTE producer scopes by CN (matrixori…