Skip to content

[Bug]: EXISTS(A) OR EXISTS(B) correlated subqueries inflate outer rows (semi-join semantics broken) #24219

Closed

Description

@Ariznawlll

Summary

When the WHERE clause contains EXISTS(corr_sub_A) OR EXISTS(corr_sub_B) (two correlated EXISTS joined by OR), MatrixOne incorrectly multiplies outer rows by the number of matches in the inner table(s) instead of doing a semi-join. A single outer row that should be returned once becomes N rows.

This is reproducible on TPC-DS 1G data and breaks at least query10 and query35 (and likely many real BI workloads with "(EXISTS A OR EXISTS B)" filters).

Environment

  • MatrixOne version: 8.0.30 (standalone)
  • Reference: MySQL 8.4.2 (correct)

Minimal repro (single outer row, 1 customer)

-- single EXISTS works correctly: returns 0 and 1 respectively
select count(*) from customer c
where c.c_customer_sk = 12829
  and exists (select 1 from web_sales ws,date_dim
              where c.c_customer_sk = ws.ws_bill_customer_sk
                and ws.ws_sold_date_sk = date_dim.d_date_sk
                and d_year=2001 and d_moy between 3 and 6);
-- MO: 0   MySQL: 0   ✅

select count(*) from customer c
where c.c_customer_sk = 12829
  and exists (select 1 from catalog_sales cs,date_dim
              where c.c_customer_sk = cs.cs_ship_customer_sk
                and cs.cs_sold_date_sk = date_dim.d_date_sk
                and d_year=2001 and d_moy between 3 and 6);
-- MO: 1   MySQL: 1   ✅

-- BUG: EXISTS(A) OR EXISTS(B) — outer row count should be 1
select count(*) from customer c
where c.c_customer_sk = 12829
  and ( exists (select 1 from web_sales ws,date_dim
                where c.c_customer_sk = ws.ws_bill_customer_sk
                  and ws.ws_sold_date_sk = date_dim.d_date_sk
                  and d_year=2001 and d_moy between 3 and 6)
        or exists (select 1 from catalog_sales cs,date_dim
                   where c.c_customer_sk = cs.cs_ship_customer_sk
                     and cs.cs_sold_date_sk = date_dim.d_date_sk
                     and d_year=2001 and d_moy between 3 and 6) );
-- MO: 20    ❌
-- MySQL: 1  ✅

The result 20 equals the number of matching rows in catalog_sales for that customer in 2001-Q2 — i.e. MO is producing one outer-row copy per inner match instead of de-duplicating.

Real-query impact

TPC-DS query10

On the 1G data set:

  • MO: 1 result row but cnt1=cnt2=cnt3=cnt4=cnt5=cnt6=28
  • PG / expected: 1 result row with cnt1=1

The count(*) is inflated by ~28× because the single matching customer is duplicated 28 times before the GROUP BY.

TPC-DS query35

On the 1G data set:

  • MO row example: state F D 4 60 4 0 240 0 60 0 0 0 4 60 4 0 240 — cnt1 = 60
  • PG row example: state AK F D 4 1 4 ... 4 — cnt1 = 1

Every customer's row count is multiplied by the number of qualifying sales rows, completely distorting all avg/sum/stddev_samp aggregates. Of the 100 result rows, 0 of 100 match PG after normalization; the data is unusable for analytics.

Both queries share the same SQL pattern:

exists (... where outer.x = inner.x)        -- correlated EXISTS A
AND
( exists (... where outer.x = inner2.x)
  OR
  exists (... where outer.x = inner3.x) )   -- OR-of-correlated-EXISTS

Expected behavior

EXISTS A OR EXISTS B is a boolean predicate. For each outer row it should evaluate to TRUE/FALSE; the outer row must appear at most once in the result (semi-join semantics, like IN).

Equivalent rewrites that work correctly in MO:

-- works in MO: produces 1
select count(*) from customer c
where c.c_customer_sk = 12829
  and ( c.c_customer_sk in (select ws_bill_customer_sk ...)
        or c.c_customer_sk in (select cs_ship_customer_sk ...) );

Suspected root cause

The query rewriter likely turns EXISTS A OR EXISTS B into LEFT JOIN A union LEFT JOIN B (or an APPLY) without inserting a DISTINCT / aggregation on the outer side, so each inner match yields a duplicate outer row.

Severity

P0 — silently wrong results, breaks any BI dashboard or analytical report that uses "did the user buy in any of these channels" filters. Affects count, sum, avg, stddev and downstream rankings.

Repro data

TPC-DS scale 1G, table customer, customer_address, customer_demographics, store_sales, web_sales, catalog_sales, date_dim — standard load.

Activity

  1. added
    kind/bug
    severity/s0
    on Apr 27, 2026
  2. added theissue type on Apr 27, 2026
  3. added this to the MO-202604 milestone on Apr 27, 2026
  4. self-assigned this
    on May 6, 2026
  5. gouhongshen commented on May 6, 2026

    @gouhongshen
    Contributor

    fixed

  6. Ariznawlll commented on May 15, 2026

    @Ariznawlll
    CollaboratorAuthor

    Verification

    Tested on main branch, commit 20346cae7 (2026-05-15).

    Minimal repro:

    SELECT count(*) FROM customer c
    WHERE c.c_customer_sk = 12829
      AND (EXISTS (SELECT 1 FROM web_sales ws, date_dim
                   WHERE c.c_customer_sk = ws.ws_bill_customer_sk
                     AND ws.ws_sold_date_sk = date_dim.d_date_sk
                     AND d_year=2001 AND d_moy BETWEEN 3 AND 6)
           OR EXISTS (SELECT 1 FROM catalog_sales cs, date_dim
                      WHERE c.c_customer_sk = cs.cs_ship_customer_sk
                        AND cs.cs_sold_date_sk = date_dim.d_date_sk
                        AND d_year=2001 AND d_moy BETWEEN 3 AND 6));

    Result: 1 (correct, semi-join semantics preserved). Previously returned 20 (row duplication by catalog_sales match count).

    Cannot reproduce. Fixed by #24223. Closing.

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions