Wrong results / internal error: a window function + correlated scalar subquery corrupts CREATE TABLE AS (persists 0/NULL) and crashes INSERT .. SELECT
- Dominant language
- Go
- Stars
- 24.4k
- Forks
- 873
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 120
Description
### What happens
Per #11478, when a window function and a **correlated** scalar subquery appear in the **same** `SELECT`,
the correlated subquery column collapses to its empty-match value (`COUNT(*)` → `0`, `SUM(...)` → `NULL`)
for every row. When such a `SELECT` drives a write, that corruption is either persisted or turns into an
error:
* `CREATE TABLE ... AS SELECT ...` materializes the wrong values (`0`/`NULL`) permanently;
* `INSERT INTO ... SELECT ...` aborts with an internal error instead of inserting.
Both diverge from MySQL 8.0, which materializes / inserts the correct correlated values.
### To Reproduce
```sql
CREATE DATABASE IF NOT EXISTS dolt_ctas_bug;
USE dolt_ctas_bug;
DROP TABLE IF EXISTS c; DROP TABLE IF EXISTS r; DROP TABLE IF EXISTS m0;
CREATE TABLE m0(id INT PRIMARY KEY, c0 INT); INSERT INTO m0 VALUES (1,10),(2,20);
-- CTAS: silently materializes c = (0,0) instead of (1,1)
CREATE TABLE c AS
SELECT id, ROW_NUMBER() OVER (ORDER BY id) AS rn,
(SELECT COUNT(*) FROM m0 x WHERE x.c0 = m0.c0) AS c
FROM m0;
SELECT id, c FROM c ORDER BY id;
-- INSERT ... SELECT: internal error instead of inserting
CREATE TABLE r(id INT PRIMARY KEY, rn INT, c INT);
INSERT INTO r SELECT id, ROW_NUMBER() OVER (ORDER BY id),
(SELECT COUNT(*) FROM m0 x WHERE x.c0 = m0.c0)
FROM m0;
```
### Actual (Dolt 2.2.3) vs Expected (MySQL 8.0)
| # | Statement | Dolt 2.2.3 (actual) | MySQL 8.0 (expected) | verdict |
|---|---|---|---|---|
| CTAS | `CREATE TABLE c AS SELECT id, ROW_NUMBER() OVER(...), (correlated COUNT)` | persists `c = (1,0),(2,0)` | `c = (1,1),(2,1)` | **WRONG (wrong data persisted)** |
| INSERT | `INSERT INTO r SELECT id, ROW_NUMBER() OVER(...), (correlated COUNT)` | `ERROR 1105 (HY000): unable to find field with index 5 in row of 3 columns. This is a bug.` | inserts `(1,1,1),(2,2,1)` | **WRONG (internal error)** |
The CTAS case is the write-path persistence of #11478 (the correlated column collapses to `0`); the
`INSERT ... SELECT` case additionally hits an internal planner error (Dolt self-reports "This is a bug").
### Version
Dolt 2.2.3, go-mysql-server. `SELECT dolt_version()` → `2.2.3`; `SELECT version()` → `8.0.31`.
Default configuration, macOS arm64.
---
*Related: write-path counterpart of #11478, which covers the SELECT. Re-verified on Dolt 2.2.3
(`dolt sql-server`); the CTAS values and the INSERT error above were cross-checked the same day against a
live MySQL 8.0 server (Dolt's stated compatibility target) on identical data, and reproduce
deterministically.*
Contributor guide
No contributing guide indexed for this repository
Research direction
Start the dolt sql-server entry point and run the supplied CTAS and INSERT ... SELECT reproductions against the stated expected MySQL results. Read related issue #11478 for the SELECT-side behavior. Done means CTAS persists the correct correlated counts and INSERT ... SELECT succeeds with the expected rows, without the internal planner error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, mysql, sql
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100