Skip to content

[Bug]: GROUP BY query rejects correlated subquery referencing grouped column #25352

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

I have checked the existing issues.

Existing issue #25327 covers ONLY_FULL_GROUP_BY single-value and functional-dependency cases. This issue is different: the outer column referenced by the correlated subquery is itself present in the GROUP BY list, but MatrixOne still rejects it.

Branch Name

main

Commit ID

0e8602b78

Other Environment Information

  • Local MatrixOne built from latest origin/main
  • MySQL oracle: MySQL 8.0.45

Actual Behavior

MatrixOne rejects a grouped query when a scalar correlated subquery references the grouped outer column:

ERROR 1149 (HY000): SQL syntax error: column "t.status" must appear in the GROUP BY clause or be used in an aggregate function

But t.status is already in the outer GROUP BY list.

Expected Behavior

MySQL accepts the query and evaluates the correlated subquery per grouped row.

Expected result:

status    c
active    2
archived  1
pending   1

Steps to Reproduce

DROP DATABASE IF EXISTS mysql_compat_group_subquery_min;
CREATE DATABASE mysql_compat_group_subquery_min;
USE mysql_compat_group_subquery_min;

CREATE TABLE t (
  id int primary key,
  status varchar(16),
  payload int,
  index idx_status(status,id)
);

INSERT INTO t VALUES
  (1,'active',10),
  (2,'active',20),
  (3,'archived',30),
  (4,'pending',40);

SELECT status,
       (SELECT COUNT(*) FROM t t2 WHERE t2.status = t.status) AS c
FROM t
GROUP BY status
ORDER BY status;

Additional information

MySQL 8.0.45 result:

status    c
active    2
archived  1
pending   1

MatrixOne latest main at 0e8602b78 rejects the query with:

ERROR 1149 (HY000): SQL syntax error: column "t.status" must appear in the GROUP BY clause or be used in an aggregate function

This was found while backfilling aggregate / GROUP BY compatibility coverage with indexed tables.

Activity

  1. added
    kind/bug
    severity/s1
    on Jul 2, 2026
  2. added
    severity/s0
    severity/s1
    and removed
    severity/s1
    severity/s0
    on Jul 2, 2026
  3. added this to the MO-202607 milestone on Jul 2, 2026
  4. added
    severity/s0
    and removed
    severity/s1
    on Jul 2, 2026
  5. added theissue type on Jul 2, 2026
  6. added and removed
    severity/s0
    on Jul 2, 2026
  7. added
    severity/s0
    ai-medium
    and removed on Jul 3, 2026
  8. LeftHandCold commented on Jul 8, 2026

    @LeftHandCold
    Collaborator

    fixed

  9. Ariznawlll commented on Jul 8, 2026

    @Ariznawlll
    CollaboratorAuthor

    Verified on latest main at bd2497354.

    Result: fixed. The grouped outer column can now be referenced by scalar correlated subqueries when it is already present in the outer GROUP BY list.

    Covered:

    select status,
           (select count(*) from t t2 where t2.status = t.status) as c,
           sum(payload) as s
    from t
    group by status
    order by status;

    Actual result:

    active    3  80
    archived  1  30
    pending   1  40
    

    Also added extensions for correlated MAX(...) in HAVING and correlated GROUP_CONCAT(...) over the grouped key.

    Added motr regression:

    • suites/14_issue_regression/t/issue_25352_grouped_correlated_subquery.test
    • suites/14_issue_regression/r/issue_25352_grouped_correlated_subquery.result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-mediumkind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions