Skip to content

MySQL Compatibility: GROUP BY not supported in recursive CTE #23077

Closed

Description

@dengn

Description

MatrixOne does not support GROUP BY clauses within recursive CTE (Common Table Expression) queries. When using GROUP BY in either the anchor part or the recursive part of a recursive CTE, MatrixOne returns a syntax error indicating that GROUP BY is not supported in recursive CTE.

Error Message

ERROR 1064 (HY000) at line 1: SQL parser error: not support group by in recursive cte: 'group by id.level'

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

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`)
)

Table: company

CREATE TABLE `company` (
  `id` int NOT NULL,
  `full_name` varchar(200) DEFAULT NULL,
  `short_name` varchar(128) DEFAULT NULL,
  `update_time` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() COMMENT '最后修改时间',
  `address` varchar(255) DEFAULT NULL,
  `board_id` int DEFAULT NULL,
  `business_scope` text DEFAULT NULL,
  `city` varchar(32) DEFAULT NULL,
  `company_profile` text DEFAULT NULL,
  `company_type_tags` varchar(255) DEFAULT NULL,
  `country` varchar(20) DEFAULT NULL,
  `directory_label` varchar(512) DEFAULT NULL,
  `district` varchar(32) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `english_name` varchar(255) DEFAULT NULL,
  `enterprises_type` varchar(50) DEFAULT NULL,
  `founded_time` datetime(6) DEFAULT NULL,
  `industry` varchar(32) DEFAULT NULL,
  `industry_paqu` varchar(1000) DEFAULT NULL,
  `insured_num` varchar(32) DEFAULT NULL,
  `product_label` varchar(512) DEFAULT NULL,
  `province` varchar(32) DEFAULT NULL,
  `registered_capital` varchar(50) DEFAULT NULL,
  `representative` varchar(32) DEFAULT NULL,
  `taxpayer_num` varchar(50) DEFAULT NULL,
  `telephone` varchar(255) DEFAULT NULL,
  `website` varchar(500) DEFAULT NULL,
  `industry_web_label` varchar(512) DEFAULT NULL COMMENT '国标行业标签',
  `industry_domain_label` varchar(512) DEFAULT NULL COMMENT '素问产业链标签',
  `scale` varchar(255) DEFAULT NULL COMMENT '规模标签',
  `policy_type` varchar(512) DEFAULT NULL COMMENT '政策类型标签',
  PRIMARY KEY (`id`),
  UNIQUE KEY `company_full_name_idx` (`full_name`),
  UNIQUE KEY `company_short_name_idx` (`short_name`),
  KEY `company_board_id_idx` (`board_id`),
  KEY `company_city_idx` (`city`),
  KEY `company_province_idx` (`province`)
)

Failing SQL Statements

Example 1: GROUP BY in recursive part

WITH RECURSIVE level_stats AS (
    SELECT level, COUNT(*) as domain_count, 1 as iter
    FROM industry_domain
    WHERE level = 1
    GROUP BY level
    UNION ALL
    SELECT id.level, COUNT(*) as domain_count, ls.iter + 1
    FROM industry_domain id
    INNER JOIN level_stats ls ON id.level = ls.level + 1
    WHERE ls.iter < 3
    GROUP BY id.level
)
SELECT * FROM level_stats ORDER BY level;

Error: ERROR 1064 (HY000) at line 1: SQL parser error: not support group by in recursive cte: 'group by id.level'

Example 2: GROUP BY in anchor part

WITH RECURSIVE province_companies AS (
    SELECT province, COUNT(*) as cnt, 1 as level
    FROM company
    WHERE province IS NOT NULL
    GROUP BY province
    HAVING COUNT(*) > 1000
    LIMIT 1
    UNION ALL
    SELECT c.province, COUNT(*) as cnt, pc.level + 1
    FROM company c
    INNER JOIN province_companies pc ON c.province = pc.province
    WHERE pc.level < 2
    GROUP BY c.province
)
SELECT * FROM province_companies LIMIT 10;

Error: ERROR 1064 (HY000) at line 1: SQL parser error: syntax error near "UNION ALL" (GROUP BY in anchor part causes syntax error)

Expected Behavior

In MySQL, GROUP BY is fully supported in both the anchor part and the recursive part of recursive CTEs. This allows for aggregating data at each level of recursion, which is essential for hierarchical data analysis and statistical computations.

Impact

  • MySQL Compatibility: GROUP BY in recursive CTEs is a standard MySQL feature
  • Migration: Applications using GROUP BY in recursive CTEs will fail when migrating to MatrixOne
  • Functionality: Users cannot perform aggregations within recursive CTEs, limiting analytical capabilities
  • Hierarchical Analysis: Cannot compute statistics at each level of a hierarchy using recursive CTEs

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)
  • company table with ~766K records

Suggested Fix

  1. Parser Update: Update the SQL parser to allow GROUP BY clauses in recursive CTE anchor and recursive parts
  2. Query Planning: Ensure the query planner can handle GROUP BY within recursive CTE execution
  3. Execution Engine: Implement proper aggregation handling during recursive CTE iteration
  4. Documentation: Update documentation to reflect support for GROUP BY in recursive CTEs

Activity

  1. added
    ai-easy
    on Oct 19, 2026
  2. XuPeng-SH commented on Oct 19, 2026

    @XuPeng-SH
    Contributor

    本轮功能核验(2026-10-19 UTC):PR #30082 的 recursive CTE query-block 修复在最新 main 8e63625c4e9baeb1d28c1095c07732f056c04895 上通过实测。Linux/amd64、Go 1.27.1、隔离嵌入式单 CN + 真实 MySQL TCP;本轮没有验证 4.2-dev,因为该 PR 合入 main。

    Race planner 测试 TestRecursiveCteAggregateQueryBlockScope exit 0(14 个正反场景通过)。另用真实 SQL 集成测试覆盖:锚点 GROUP BY/HAVING;递归成员中的分组标量投影、分组/HAVING 标量谓词、相关分组与嵌套标量;NULL/空分组边界;同一 SQL PREPARE 以 2、4、3 连续重绑定;多行标量、嵌套递归源及直接递归聚合/GROUP BY 的错误边界;失败后同一连接 SELECT 77 成功。SQL race 集成测试 exit 0,临时数据库已同步删除。

    范围澄清:issue 原示例要求在递归 SELECT 本身直接 GROUP BY/聚合。MySQL 8.0 参考手册明确禁止递归 SELECT 使用聚合函数、GROUP BY、窗口函数等;这些限制不适用于非递归 SELECT。故直接递归成员 GROUP BY 继续被拒绝符合 MySQL 行为;本 PR 实测通过的是锚点分组和递归成员内独立标量子查询的分组语义。

    命令与原始日志:

    将现有问题转 QA 核对兼容范围和原示例预期;保留当前开发指派。

  3. Ariznawlll commented on Oct 19, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:官方 main
    • commit:fed3bf59700158212a9135d0b619c3849034bb65(执行时核对远端 refs/heads/main)
    • 部署方式:本机隔离嵌入式 1 CN / 1 TN / 1 LogService,真实 MySQL TCP;另运行 planner Go race 测试。未使用共享集群。
    • 测试日期:2026-10-20(北京时间)
    • 修复关联:fix(plan): allow GROUP BY and HAVING in recursive CTE scalar subqueries #30082 已 MERGED,merge commit 75ff736835c3d00d40dc00a0a1d3b11129388e5b 已进入上述官方 main。

    原问题验证

    在隔离实例建立仅含所需列的 industry_domain、company 后,按 issue 正文原样执行两条 CTE SQL,重复 3/3:例 1 在递归 SELECT 直接 GROUP BY id.level,实际返回 1064 (42000): not support group by in recursive cte;例 2 在锚点 LIMIT 1 后、UNION ALL 前发生语法错误。不能把这两条原 SQL 声称为执行成功。

    这里需纠正原预期:MySQL 8.0 官方 WITH 文档明确禁止递归 SELECT 本身使用聚合与 GROUP BY,限制不作用于非递归锚点。#30082 修复的是合法的锚点分组,以及递归成员内部独立子查询的 GROUP BY / HAVING,并非开放直接递归分组。

    Happy Path 覆盖

    1. 锚点 GROUP BY 后递归传播:真实 SQL 3/3,按序得到 (level,n)=(1,2),(2,2),(3,2)。
    2. 递归成员内相关标量子查询独立分组:真实 SQL 3/3,按序得到 (n,total)=(1,0),(2,2),(3,1),(4,1);与未分组锚点及普通查询对照一致。
    3. 现有 BVT 包含锚点 GROUP BY/HAVING、递归成员内分组标量投影/谓词、相关/嵌套子查询、预处理语句重绑定,以及空组、NULL 等值/类型边界;本轮未重跑整个 BVT suite,不把代码覆盖等同于本轮执行结果。

    Unhappy Path 覆盖

    1. 原例 1 及最小化直接递归 GROUP BY 均 3/3 按 MySQL 兼容约束拒绝;直接递归 MAX() 同样 3/3 拒绝。
    2. 原例 2 按原文 3/3 在 UNION ALL 附近语法拒绝;本轮不把它当作已成功支持的 SQL。
    3. 每轮拒绝后,同一连接 SELECT 77 实际返回 77;临时数据库与嵌入式服务由测试清理,无残留业务数据。

    白盒验证

    在同一完整 SHA 上,TestRecursiveCteAggregateQueryBlockScope 使用 -race -count=3,3/3 PASS;覆盖合法锚点/子查询作用域及非法直接递归分组、聚合等规划边界。它不代替上面的真实 TCP SQL 验证。

    回归测试

    测试证据

    • 本轮隔离测试命令:mo-cgo-test -p 1 -count=3 -run '^TestIssue23077CurrentMainValidation$' ./pkg/tests/issues/isolated,exit 0;原例 1 实际 1064 (42000),原例 2 实际 1064 (42000),上述准确结果由测试逐项断言。验证专用临时代码已在本地运行,未提交为产品回归。
    • Planner 命令:mo-cgo-test -race -p 1 -count=3 -run '^TestRecursiveCteAggregateQueryBlockScope$' ./pkg/sql/plan,exit 0。
    • 可复核的持久测试文件及修复记录:PR #30082。

    在 MySQL 8.0 的实际递归 CTE 规则下,合法的锚点和独立子查询分组已通过;原 issue 所要求的“递归 SELECT 直接 GROUP BY/聚合”并非 MySQL 支持的能力,保持拒绝是正确兼容行为。该问题作为 MySQL 兼容性缺陷的预期已核正。

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

ai-easy

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions