Skip to content

[Bug]: EXPLAIN fails for DISTINCT/GROUP BY sequence functions #30366

Closed

Description

@Ariznawlll

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_FUNCTION error.
  • Before each failed EXPLAIN, SELECT last_seq_num,is_called FROM s returned 1,0; afterward the real SELECT DISTINCT NEXTVAL('s') ... returned 1,2 and sequence state 2,1. No sequence value was consumed by the failed plan inspection.
  • EXPLAIN SELECT NEXTVAL('s') FROM t succeeds (Project -> Table Scan). EXPLAIN SELECT DISTINCT RAND() FROM t succeeds (Project -> Aggregate, Group Key: rand()).
  • UNION ALL with NEXTVAL in both branches also explains and executes normally; a zero-row branch consumes no sequence value.
  • SELECT DISTINCT NEXTVAL('missing') FROM t fails 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.md on the local isolated test host.
  • Existing planner UT TestGroupingSetDistinctMaterializesVolatileProjectionOnce passed on this commit. It checks that the query plan contains the expected number of volatile projections, but does not format that plan through EXPLAIN.

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

Activity

  1. added this to the MO-202610 milestone on Oct 16, 2026
  2. added
    kind/bug
    on Oct 16, 2026
  3. added theissue type on Oct 16, 2026
  4. self-assigned this
    on Oct 16, 2026
  5. added a commit that references this issue on Oct 16, 2026
    14a8d85
  6. XuPeng-SH commented on Oct 16, 2026

    @XuPeng-SH
    Contributor

    转测 @Ariznawlll。

    已在 main 提交 0723ae37b1183a03bac9eda16669dfc7e474dfb8 对关联修复 #30386 完成本 issue 对应的定向验证,所测场景通过。

    原始 DISTINCT/GROUP BY 序列函数 EXPLAIN 场景通过,确认 EXPLAIN 不消耗序列值,后续实际取值正确。

    测试入口:

    • TestExplainSequenceFunctionAsGroupKey

    请在目标验收版本复核后决定关闭;本次转测保留 issue 为 OPEN。

  7. Ariznawlll commented on Oct 16, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 官方 main 完整 SHA:47b9690d7caec56b589fdefaf0684177412cffb2;修复 #30386 已 MERGED,merge commit 14a8d854b0f9346065ff8432eb979e35f32f50d8 已确认进入该 SHA。
    • 2026-10-17,隔离本地 1 LogService / 1 TN / 2 CN,分别通过两个 CN 的 MySQL 协议入口验证;另以隔离 1 CN 服务运行 MOTR。

    原问题验证

    每个 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 覆盖

    1. EXPLAIN SELECT COUNT(*) FROM t GROUP BY NEXTVAL('s'),以及 DISTINCT SETVAL、CURRVAL、LASTVAL():2 CN × 3 轮共 24/24 个关联计划成功显示函数,且每次之后序列保持 (1,0)。
    2. 普通投影 EXPLAIN SELECT NEXTVAL('s') FROM t:6/6 成功,序列仍未消费。
    3. 原查询的实际 SELECT DISTINCT NEXTVAL('s') AS n FROM t ORDER BY n:6/6 返回 1,2(协议类型为字符串),随后序列状态为 (2,1)。

    Unhappy Path 覆盖

    1. SELECT DISTINCT NEXTVAL('missing') FROM t:6/6 返回预期 1146,有效序列保持 (2,1)。
    2. 错误后同连接 SELECT 1:6/6 返回 1。每轮测试库已清理;未见崩溃或卡死。

    白盒验证

    产品验收以两 CN 的 SQL 协议输出为准。隔离工作树中的临时诊断探针 TestCodexIssue30366SequenceExplainAcrossCNs -count=3 PASS;它没有作为 MatrixOne Go UT 提交。此问题非并发竞争,未执行 -race。

    回归测试

    测试证据

    • 原 SQL 协议计划实际输出 Group Key: nextval('s');EXPLAIN 前后状态 (1,0),执行原 SELECT 后得到 1,2、状态 (2,1)。两 CN 各三轮的诊断日志均记录原始和变体通过。
    • MOTR #247 文件与检查;本地 runner 汇总:total=9 success=9 failed=0 skipped=0 | stmts=201 ok=201 fail=0。

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions