Skip to content

[Bug]: HNSW Top-K query panics in SQL and Starlark stored procedures #29147

Closed

Description

@Ariznawlll

Description

An HNSW Top-K query works when executed directly, but the same query triggers a recovered planner panic when executed from either a SQL stored procedure or Starlark mo.sql.

The SQL procedure path returns the panic as an internal error. The Starlark path converts the same planner panic into the mo.sql error value. The CN remains reachable, but the indexed query cannot be used through either procedure execution path.

Environment

  • Branch: main
  • Commit: 56f7d1eae392b5f3de33e1ea43c916956c50b833
  • Deployment: isolated local one-LOG/one-TN/two-CN cluster
  • SQL endpoints: both CNs were exercised independently

Steps to reproduce

set experimental_hnsw_index = 1;

create database hnsw_proc_repro;
use hnsw_proc_repro;

create table vecs(id bigint primary key, v vecf32(4));
insert into vecs values
  (1,'[0,0,0,0]'),
  (2,'[1,1,1,1]'),
  (3,'[5,5,5,5]'),
  (4,'[2,2,2,2]');

create index h_vecs using hnsw on vecs(v)
  op_type 'vector_l2_ops'
  m=4 ef_construction=16 ef_search=16;

-- Control: succeeds and returns 2, 1.
select id
from vecs
order by l2_distance(v,'[0.9,0.9,0.9,0.9]')
limit 2;

create procedure p_vec()
'begin
   select id
   from vecs
   order by l2_distance(v,''[0.9,0.9,0.9,0.9]'')
   limit 2;
 end';

call p_vec();

The equivalent Starlark path also reaches the same failure:

create procedure slk_vec(out o varchar) language 'starlark'
$$
rows, err = mo.sql("select id from vecs order by l2_distance(v,'[0.9,0.9,0.9,0.9]') limit 2")
out_o = err if err != None else str(rows)
$$;

call slk_vec(@o);
select @o;

Actual behavior

call p_vec() returns an internal error beginning with:

internal error: panic interface conversion: interface {} is nil, not int64

The stack starts at:

pkg/sql/plan.(*QueryBuilder).prepareHnswIndexContext
    pkg/sql/plan/apply_indices_hnsw.go:131

The Starlark mo.sql call returns the same panic text in err.

Expected behavior

The procedure execution paths should build the same valid HNSW plan as the direct statement and return IDs 2, 1. Planner context differences must not cause a type assertion panic.

Stability and controls

  • Direct HNSW query: correct Top-K result on every control run.
  • SQL procedure: reproduced 3/3 on the second CN; connection remained usable after every error.
  • Starlark mo.sql: reproduced and returned the same planner panic as its structured error value.
  • Fulltext, data-branch and snapshot reads in the same procedure test matrix completed normally, which narrows the failure to the HNSW planning path.

Code analysis

The failure occurs in prepareHnswIndexContext while planning the statement through the background execution context used by stored procedures. A context value read there is nil, but the code asserts it to int64. Direct frontend execution populates the required value, so the same query succeeds outside a procedure.

Regression coverage

After the fix, add a procedure regression that creates a small HNSW index, verifies the direct Top-K control, then runs the same query through both SQL Procedure and Starlark mo.sql and compares the ordered IDs. This is a small deterministic case; it belongs in MOTR or the existing procedure/vector regression rather than big-data.

Activity

  1. added this to the MO-202609 milestone on Sep 20, 2026
  2. added
    kind/bug
    on Sep 20, 2026
  3. added theissue type on Sep 20, 2026
  4. self-assigned this
    on Sep 20, 2026
  5. added 4 commits that reference this issue on Oct 11, 2026
    dbbbadf
    113333e
    4e959d7
    73149a3
  6. Ariznawlll commented on Oct 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main;commit:02a2823c2007d5e4fb4ada148e93743df337551a;日期:2026-10-12。
    • 部署:本地独立单 CN / 单 TN / 单 LogService;experimental_hnsw_index=1。

    原问题验证

    依照 issue 建四行 VECF32(4) 表及 HNSW 索引,以 l2_distance(...,'[0.9,0.9,0.9,0.9]') LIMIT 2 查询。直接 SQL、SQL 存储过程 CALL p_vec()、Starlark mo.sql 均连续 3 轮返回顺序 2,1,未出现原先的 interface {} is nil, not int64 planner panic。

    Happy Path 覆盖

    直接 Top-K 查询结果作为对照,SQL procedure 逐行与其比较;Starlark 的结果字符串也核对 ID 2 在 ID 1 之前。三条路径都走同一已建 HNSW 索引的原查询。

    Unhappy Path 覆盖

    重点覆盖原问题中“直接查询成功而存储过程报 panic”的执行上下文差异;连续 3 轮调用两个过程,均未返回 panic,后续调用和同连接查询保持可用。没有把未建索引的普通全表扫描当作本问题的修复证据。

    白盒验证

    该测试走 SQL procedure 和 Starlark mo.sql 两种后台规划上下文,对照前台直接查询,以检查 HNSW 规划所需上下文值不再缺失。本次未测百万向量 ANN 召回率或性能。

    回归测试

    已加入 MOTR PR #229 的定向过程回归;单次 3 轮,compare 3/3。四行可稳定触发原 planner 缺陷,不需要 big-data。

    测试证据

    • 本地原 SQL:3/3 直接、SQL procedure、Starlark 结果均为 2,1。
    • MOTR 脚本和 golden:compare 3/3。

    测试结论

    测试通过。

  7. Ariznawlll commented on Oct 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    回归归档:本 issue 在 MatrixOne main 02a2823c2007d5e4fb4ada148e93743df337551a 上的原问题、正常与边界/异常路径已有上方详细验证记录,MOTR 对应用例本地 compare 3/3 通过。回归 PR https://github.com/matrixorigin/motr/pull/229 已合入(merge commit 0ed4301b164aa42d0affa0dc27e17f7435b7bf04)。据此关闭本 issue;本次关闭未增加先前评论未覆盖的验收范围。

    测试通过。

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