Repository navigation
Description
Description
EXPLAIN rejects a valid query when a sequence function is used as a DISTINCT or GROUP BY key. The query itself runs correctly. This is a plan-inspection failure, not a wrong-result claim.
Environment
- Branch:
main - Commit:
48dbe277b6d20216c1c513d03c5cbd681380a8a2 - Deployment: isolated local two-CN MatrixOne cluster (
127.0.0.1:16041,:16042),version() = 8.0.30-MatrixOne-v1.3.0,git_version() = 48dbe277b6 - Date: 2026-10-16
Steps to reproduce
CREATE DATABASE seq_explain_repro;
USE seq_explain_repro;
CREATE SEQUENCE s INCREMENT BY 1 START WITH 1;
CREATE TABLE t(k INT PRIMARY KEY);
INSERT INTO t VALUES (1), (2);
EXPLAIN SELECT DISTINCT NEXTVAL('s') AS n FROM t;Actual behavior
ERROR 20301 (HY000): invalid input: explain contains UNKNOW_KIND_FUNCTION
The same error occurs for EXPLAIN SELECT COUNT(*) FROM t GROUP BY NEXTVAL('s'), and for DISTINCT SETVAL('s', 10) or DISTINCT CURRVAL('s') in place of NEXTVAL.
Expected behavior
EXPLAIN should produce a plan for the supported query without evaluating or changing the sequence. SELECT DISTINCT NEXTVAL('s') AS n FROM t ORDER BY n itself returns 1, 2.
Stability and controls
- Reproducer: 3/3 in three independently created databases; runs on both CNs. Each returned the exact
UNKNOW_KIND_FUNCTIONerror. - Before each failed
EXPLAIN,SELECT last_seq_num,is_called FROM sreturned1,0; afterward the realSELECT DISTINCT NEXTVAL('s') ...returned1,2and sequence state2,1. No sequence value was consumed by the failed plan inspection. EXPLAIN SELECT NEXTVAL('s') FROM tsucceeds (Project -> Table Scan).EXPLAIN SELECT DISTINCT RAND() FROM tsucceeds (Project -> Aggregate,Group Key: rand()).UNION ALLwithNEXTVALin both branches also explains and executes normally; a zero-row branch consumes no sequence value.SELECT DISTINCT NEXTVAL('missing') FROM tfails with 1146, leaves the valid sequence unchanged, and a later valid query succeeds.
Evidence
- Reproducer and exact outputs are preserved in
/private/tmp/mo-explore-run-20261016/checkpoint.mdon the local isolated test host. - Existing planner UT
TestGroupingSetDistinctMaterializesVolatileProjectionOncepassed on this commit. It checks that the query plan contains the expected number of volatile projections, but does not format that plan throughEXPLAIN.
Code analysis
pkg/sql/plan/function/list_builtIn.go declares NEXTVAL, SETVAL, and CURRVAL with layout: UNKNOW_KIND_FUNCTION around lines 15802, 15836, and 15894. pkg/sql/plan/explain/explain_expr.go:672-673 unconditionally returns this error when formatting an expression with that layout. DISTINCT / GROUP BY exposes the sequence function as an aggregate group key that the formatter visits; a plain projection does not print it in the same way. This is the likely direct cause, not a runtime sequence-function failure.
Regression coverage
After a fix, add a focused EXPLAIN regression for sequence functions as DISTINCT/GROUP BY keys, asserting a plan is returned and sequence state is unchanged. No repository test or PR was added during this exploration.
Related
- [Bug]: concurrent NEXTVAL returns context canceled instead of unique values #28246 tests sequence state and UNION ALL behavior, not EXPLAIN formatting.
- [Bug]: sequence ownership and state must survive DML, DDL, and branch merge #30359 and [Bug]: sequence-backed table default resolves against caller database in qualified INSERT #30360 are distinct sequence/branch and table-default binding issues.
Activity
fix(plan): let EXPLAIN format sequence functions in group keys (#30386)
XuPeng-SH commentedon Oct 16, 2026
转测 @Ariznawlll。
已在 main 提交
0723ae37b1183a03bac9eda16669dfc7e474dfb8对关联修复 #30386 完成本 issue 对应的定向验证,所测场景通过。原始 DISTINCT/GROUP BY 序列函数 EXPLAIN 场景通过,确认 EXPLAIN 不消耗序列值,后续实际取值正确。
测试入口:
TestExplainSequenceFunctionAsGroupKey请在目标验收版本复核后决定关闭;本次转测保留 issue 为 OPEN。
Ariznawlll commentedon Oct 16, 2026
测试版本
main完整 SHA:47b9690d7caec56b589fdefaf0684177412cffb2;修复 #30386 已 MERGED,merge commit14a8d854b0f9346065ff8432eb979e35f32f50d8已确认进入该 SHA。原问题验证
每个 CN 独立建起始值为 1 的 sequence
s和两行表t(1),(2)。原始EXPLAIN SELECT DISTINCT NEXTVAL('s') AS n FROM t在 2 CN × 3 轮共 6/6 返回计划,含Group Key: nextval('s');没有原20301/HY000: explain contains UNKNOW_KIND_FUNCTION。每次 EXPLAIN 前后last_seq_num,is_called均为(1,0),未消费序列值。Happy Path 覆盖
EXPLAIN SELECT COUNT(*) FROM t GROUP BY NEXTVAL('s'),以及 DISTINCTSETVAL、CURRVAL、LASTVAL():2 CN × 3 轮共 24/24 个关联计划成功显示函数,且每次之后序列保持(1,0)。EXPLAIN SELECT NEXTVAL('s') FROM t:6/6 成功,序列仍未消费。SELECT DISTINCT NEXTVAL('s') AS n FROM t ORDER BY n:6/6 返回1,2(协议类型为字符串),随后序列状态为(2,1)。Unhappy Path 覆盖
SELECT DISTINCT NEXTVAL('missing') FROM t:6/6 返回预期 1146,有效序列保持(2,1)。SELECT 1:6/6 返回 1。每轮测试库已清理;未见崩溃或卡死。白盒验证
产品验收以两 CN 的 SQL 协议输出为准。隔离工作树中的临时诊断探针
TestCodexIssue30366SequenceExplainAcrossCNs -count=3PASS;它没有作为 MatrixOne Go UT 提交。此问题非并发竞争,未执行-race。回归测试
178c15125cb37130cd89c29f6a1c82794e6829c5,场景文件suites/scenarios/14_issue_regression/issue_30366_sequence_explain.py和对应out/issue_30366_sequence_explain.out;没有修改 (AOE) incompatible bug fix #237。覆盖原 DISTINCT/GROUP BY、四种序列函数、非消费状态、实际取值和错误后恢复。--repeat 3:3/3 success;与 [Bug]: CTAS with NEXTVAL projection or target default fails in internal INSERT #30375 CTAS sequence、[Bug]: Qualified sequence-backed view resolves NEXTVAL in the session database #27581 跨库 view sequence 的相关子集共 9/9、201/201 语句通过。select without from clause is not supported #247 尚未合入;完整 suite 与集成 CI 未执行,后续跟踪。此次未启动共享或昂贵 CI。测试证据
Group Key: nextval('s');EXPLAIN 前后状态(1,0),执行原 SELECT 后得到1,2、状态(2,1)。两 CN 各三轮的诊断日志均记录原始和变体通过。total=9 success=9 failed=0 skipped=0 | stmts=201 ok=201 fail=0。测试结论
测试通过。