matrixorigin / matrixorigin/matrixone

[Compatibility]: correlated scalar LIMIT rejects non-equality, outer ORDER BY, and dynamic bounds

Open
#28,296 0 comments 0 reactions 1 assignee Claimed by @aunjgr View on GitHub
kind/bug severity/s0
Dominant language
Go
Stars
1.9k
Forks
311
Avg merge
1d 3h
Merged PRs (30d)
768

Description

## Description

The per-correlation-key LIMIT rewrite added after #26816 works for literal LIMIT/OFFSET with equality correlation, but MatrixOne still rejects three valid MySQL correlated scalar-subquery forms: non-equality correlation, an ORDER BY expression that references the outer row, and a prepared LIMIT parameter.

## Environment

- Branch: `main`
- Commit: `74668fc075c965c462b30f10ae24a1c19c289cfd`
- Deployment: local single-CN `etc/launch/launch.toml`
- Comparison: MySQL 8.0.45
- Date: 2026-09-07

## Steps to reproduce

```sql
CREATE DATABASE corr_limit_repro;
USE corr_limit_repro;

CREATE TABLE o(id INT PRIMARY KEY, grp INT, val INT);
CREATE TABLE i(id INT PRIMARY KEY, grp INT, val INT);
INSERT INTO o VALUES (1,1,5),(2,1,NULL),(3,2,10),(4,3,7);
INSERT INTO i VALUES (10,1,5),(11,1,NULL),(12,2,8),(13,2,10),(14,4,NULL);

-- Non-equality correlation.
SELECT o.id,
(SELECT i.val FROM i WHERE i.grp <= o.grp ORDER BY i.id LIMIT 1) AS v
FROM o ORDER BY o.id;

-- Per-outer-row ordering expression.
SELECT o.id,
(SELECT i.val FROM i WHERE i.grp=o.grp
ORDER BY ABS(COALESCE(i.val,0)-COALESCE(o.val,0)),i.id LIMIT 1) AS v
FROM o ORDER BY o.id;

-- Dynamic LIMIT in a prepared correlated subquery.
PREPARE ps FROM
'SELECT o.id,(SELECT i.val FROM i WHERE i.grp=o.grp ORDER BY i.id LIMIT ?) FROM o ORDER BY o.id';
SET @lim=1;
EXECUTE ps USING @lim;
```

## Actual behavior

MatrixOne rejects the three forms respectively with:

```text
ERROR 20102: correlated LIMIT with non-equality predicates is not yet implemented
ERROR 20102: correlated columns in ORDER BY with LIMIT is not yet implemented
ERROR 20102: dynamic LIMIT in correlated subquery is not yet implemented
```

## Expected behavior

MySQL 8.0.45 executes all three with per-outer-row pagination. With the fixture above:

- non-equality correlation returns `5,5,5,5`;
- correlated ordering returns `5,NULL,10,NULL`;
- prepared `LIMIT 1` returns `5,5,8,NULL`.

## Stability and controls

- All three rejection paths reproduced 3/3; MySQL results were stable 3/3.
- Literal `ORDER BY i.id LIMIT 1` with equality correlation returns `5,5,8,NULL` on both systems.
- Literal `LIMIT 1 OFFSET 1` returns `NULL,NULL,10,NULL` on both systems.
- A prepared filter parameter with fixed literal LIMIT works on MatrixOne and matches MySQL, separating parameter binding in the predicate from a parameter used as the pagination boundary.
- Read-only statements; failures leave the connection usable.

## Code analysis

`rewriteCorrelatedPagination` correctly removes the old unsafe global-LIMIT plan and partitions equality-correlated inputs. It currently has explicit NYI guards when LIMIT/OFFSET is non-literal, an ORDER BY expression still contains a correlated column, or the pulled predicate cannot be represented by equality partition keys. These guards exactly produce the three black-box errors above.

## Regression coverage

After implementation, extend the correlated-pagination planner tests and SQL regression with non-equality predicates, outer-dependent ordering, prepared LIMIT/OFFSET values, multiple outer groups, empty groups, NULL values, tie breaking, and invalid/overflow bounds.

## Related

- #26816: closed wrong-result issue for equality-correlated LIMIT being applied globally. The equality and OFFSET controls now work; this issue covers still-rejected valid forms.

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.