matrixorigin / matrixorigin/matrixone
MySQL Compatibility: ORDER BY/LIMIT not supported in recursive CTE anchor part
- Dominant language
- Go
- Stars
- 1.9k
- Forks
- 311
- Avg merge
- 1d 3h
- Merged PRs (30d)
- 768
Description
## Description
MatrixOne does not support `ORDER BY` or `LIMIT` clauses in the anchor (initial) part of recursive CTE (Common Table Expression) queries. When using ORDER BY or LIMIT in the anchor part of a recursive CTE, MatrixOne returns a syntax error.
## Error Messages
### ORDER BY Error:
```
ERROR 1064 (HY000) at line 1: SQL parser error: You have an error in your SQL syntax; check the manual that corresponds to your MatrixOne server version for the right syntax to use. syntax error at line 7 column 10 near "ORDER BY name"
```
### LIMIT Error:
```
ERROR 1064 (HY000) at line 1: SQL parser error: You have an error in your SQL syntax; check the manual that corresponds to your MatrixOne server version for the right syntax to use. syntax error at line 6 column 10 near "LIMIT 1"
```
## Affected Cases
This issue affects **2 test cases** from our recursive CTE compatibility testing on the `industry_radar_test` database.
## Related Table DDL
### Table: industry_domain
```sql
CREATE TABLE `industry_domain` (
`id` int NOT NULL,
`name` varchar(50) DEFAULT NULL,
`desc` varchar(512) DEFAULT NULL,
`father_id` int DEFAULT NULL,
`downstream_id` int DEFAULT NULL,
`is_root` smallint DEFAULT NULL,
`level` int DEFAULT NULL,
`update_time` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() COMMENT '最后修改时间',
`guobiao_category` varchar(128) DEFAULT NULL COMMENT '国标行业门类',
`guobiao_major_category` varchar(128) DEFAULT NULL COMMENT '国标行业大类',
`guobiao_medium_category` varchar(128) DEFAULT NULL COMMENT '国标行业中类',
`guobiao_small_category` varchar(128) DEFAULT NULL COMMENT '国标行业小类',
PRIMARY KEY (`id`),
UNIQUE KEY `unique_idx` (`name`,`father_id`,`level`)
)
```
## Failing SQL Statements
### Example 1: ORDER BY in anchor part
```sql
WITH RECURSIVE sorted_tree AS (
SELECT id, name, father_id, level, 0 as depth
FROM industry_domain
WHERE is_root = 1
ORDER BY name
LIMIT 1
UNION ALL
SELECT id.id, id.name, id.father_id, id.level, st.depth + 1
FROM industry_domain id
INNER JOIN sorted_tree st ON id.father_id = st.id
WHERE st.depth < 3
)
SELECT * FROM sorted_tree ORDER BY depth, name LIMIT 20;
```
**Error**: `ERROR 1064 (HY000) at line 1: SQL parser error: syntax error at line 7 column 10 near "ORDER BY name"`
### Example 2: LIMIT in anchor part
```sql
WITH RECURSIVE limited_tree AS (
SELECT id, name, father_id, level
FROM industry_domain
WHERE is_root = 1
LIMIT 1
UNION ALL
SELECT id.id, id.name, id.father_id, id.level
FROM industry_domain id
INNER JOIN limited_tree lt ON id.father_id = lt.id
WHERE (SELECT COUNT(*) FROM limited_tree) < 10
)
SELECT * FROM limited_tree LIMIT 10;
```
**Error**: `ERROR 1064 (HY000) at line 1: SQL parser error: syntax error at line 6 column 10 near "LIMIT 1"`
## Expected Behavior
In MySQL, `ORDER BY` and `LIMIT` are fully supported in the anchor part of recursive CTEs. This allows for:
- Selecting a specific starting point for recursion (using ORDER BY + LIMIT)
- Limiting the initial result set before recursion begins
- Controlling which rows are used as the base for recursive expansion
## Impact
- **MySQL Compatibility**: ORDER BY and LIMIT in recursive CTE anchor parts are standard MySQL features
- **Migration**: Applications using ORDER BY/LIMIT in recursive CTE anchor parts will fail when migrating to MatrixOne
- **Functionality**: Users cannot control the starting point or limit the initial set for recursion
- **Query Flexibility**: Cannot select specific root nodes or limit initial recursion base
## Test Context
This issue was discovered during recursive CTE compatibility testing using the `industry_radar_test` database, which contains:
- `industry_domain` table with ~1.5K records (hierarchical structure)
## Suggested Fix
1. **Parser Update**: Update the SQL parser to allow ORDER BY and LIMIT clauses in the anchor part of recursive CTEs
2. **Query Planning**: Ensure the query planner correctly handles ORDER BY/LIMIT in anchor parts before recursion begins
3. **Execution Engine**: Implement proper ordering and limiting of the anchor result set before recursive iteration
4. **Documentation**: Update documentation to reflect support for ORDER BY and LIMIT in recursive CTE anchor parts
Contributor guide
Assessment
This issue has not been assessed yet.