cockroachdb / cockroachdb/cockroach

sql: distSQLSpecExecFactory.ConstructVectorSearch not using InitAllowingExternalRowData

Open
#146,125 2 comments 0 reactions 0 assignees View on GitHub
A-vector-index branch-master branch-release-25.2 C-bug O-qa T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

In [this slack conversation](https://cockroachlabs.slack.com/archives/CV581CE78/p1746469163185869?thread_ts=1746286049.818619&cid=CV581CE78), @andy-kimball pointed out that `sql.(*distSQLSpecExecFactory).ConstructVectorSearch` is using `span.(*Builder).Init` instead of `span.(*Builder).InitAllowingExternalRowData` and so will get an internal error when used on external row data.

This is pretty tough to hit. It requires `SET experimental_distsql_planning = always;` + vector search + external row data. Here's a repro using `cockroach demo`:

```sql
-- 1. create a table with a vector index

SET CLUSTER SETTING feature.vector_index.enabled = on;

CREATE TABLE abc (
a INT PRIMARY KEY,
b INT,
c VECTOR(3),
VECTOR INDEX (b, c)
);

INSERT INTO abc VALUES (1, 2, '[1, 2, 3]');
ANALYZE abc;
SELECT * FROM abc WHERE b = 2 ORDER BY c <-> '[1, 2, 3]' LIMIT 1;

-- 2. connect to the system tenant and start replication to a new tenant

\demo ls
\connect ''

CREATE VIRTUAL CLUSTER standby FROM REPLICATION OF demoapp ON 'demo://system' WITH READ VIRTUAL CLUSTER;
-- wait until the standby-readonly tenant data_state is ready
SHOW VIRTUAL CLUSTERS;

-- 3. connect to the standby-readonly tenant and try querying the table

\connect ''

SHOW TABLES;
SET experimental_distsql_planning = always;
SELECT * FROM abc WHERE b = 2 ORDER BY c <-> '[1, 2, 3]' LIMIT 1;
```

The internal error looks like this:

```
demo@[local:/Users/michae2/.cockroach-demo]:26257/standby-readonly/defaultdb> SELECT * FROM abc WHERE b = 2 ORDER BY c <-> '[1, 2, 3]' LIMIT 1;
ERROR: internal error: abc uses external row data
SQLSTATE: XX000
DETAIL: stack trace:
pkg/sql/span/span_builder.go:49: Init()
pkg/sql/distsql_spec_exec_factory.go:1471: ConstructVectorSearch()
bazel-out/darwin_arm64-fastbuild/bin/pkg/sql/opt/exec/explain/plan_gist_factory.og.go:1223: ConstructVectorSearch()
bazel-out/darwin_arm64-fastbuild/bin/pkg/sql/opt/exec/explain/explain_factory.og.go:2108: ConstructVectorSearch()
pkg/sql/opt/exec/execbuilder/relational.go:3979: buildVectorSearch()
pkg/sql/opt/exec/execbuilder/relational.go:273: buildRelational()
pkg/sql/opt/exec/execbuilder/relational.go:2733: buildLookupJoin()
pkg/sql/opt/exec/execbuilder/relational.go:228: buildRelational()
pkg/sql/opt/exec/execbuilder/relational.go:1152: buildProject()
pkg/sql/opt/exec/execbuilder/relational.go:203: buildRelational()
pkg/sql/opt/exec/execbuilder/relational.go:2153: buildTopK()
pkg/sql/opt/exec/execbuilder/relational.go:213: buildRelational()
pkg/sql/opt/exec/execbuilder/builder.go:380: build()
pkg/sql/opt/exec/execbuilder/builder.go:298: Build()
pkg/sql/plan_opt.go:936: runExecBuilder()
pkg/sql/plan_opt.go:285: runExecBuild()
pkg/sql/plan_opt.go:268: makeOptimizerPlan()
pkg/sql/conn_executor_exec.go:3308: makeExecPlan()
pkg/sql/conn_executor_exec.go:2855: dispatchToExecutionEngine()
pkg/sql/conn_executor_exec.go:1080: execStmtInOpenState()
pkg/sql/conn_executor_exec.go:172: func2()
pkg/sql/conn_executor_exec.go:4477: execWithProfiling()
pkg/sql/conn_executor_exec.go:171: execStmt()
pkg/sql/conn_executor.go:2342: func1()
pkg/sql/conn_executor.go:2347: execCmd()
pkg/sql/conn_executor.go:2264: run()
pkg/sql/conn_executor.go:1048: ServeConn()
pkg/sql/pgwire/conn.go:252: processCommands()
pkg/sql/pgwire/server.go:1197: func4()
src/runtime/asm_arm64.s:1223: goexit()

HINT: You have encountered an unexpected error.

Please check the public issue tracker to check whether this problem is
already tracked. If you cannot find it there, please report the error
with details by creating a new issue.

If you would rather not post publicly, please contact us directly
using the support form.

We appreciate your feedback.
```

Jira issue: CRDB-50399

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.