bug: Multi-table LATERAL JOIN reconstructed query returns empty result while single-table query returns 3 rows
Search before asking
- I had searched in the issues and found no similar issues.
Version
- Databend version: v1.2.925-patch-11-ebcd374c34
- Build toolchain: rust-1.94.0-nightly-2026-08-26
What's Wrong?
The single-table query returns 3 rows, but the multi-table reconstructed query using JOIN LATERAL returns an empty result. The single-table query plan directly scans the source table and filters on c0boolean, returning 3 rows. In the multi-table query plan, part_l is a MEMORY table with 9 actual rows, but the plan shows partitions total: 0, partitions scanned: 0, causing the probe side to receive no input and ultimately return 0 rows.
How to Reproduce?
- Start the Databend query service and connect using a MySQL client.
- Execute the following SQL script:
DROP DATABASE IF EXISTS repro_databend912_db7_min;
CREATE DATABASE repro_databend912_db7_min;
USE repro_databend912_db7_min;
CREATE TABLE source (
vp_rowid BIGINT NOT NULL,
c0boolean BOOLEAN,
c1int BIGINT
) ENGINE=FUSE;
INSERT INTO source VALUES
(1, false, -1),
(2, false, -2),
(3, false, -3),
(4, true, -4),
(5, true, -5),
(6, true, -6),
(7, true, -7),
(8, true, -8),
(9, true, -9);
CREATE TABLE part_l (
vp_rowid BIGINT NOT NULL,
c1int BIGINT
) ENGINE=MEMORY;
CREATE TABLE part_r (
vp_rowid BIGINT NOT NULL,
c0boolean BOOLEAN
) ENGINE=FUSE;
INSERT INTO part_l
SELECT vp_rowid, c1int FROM source;
INSERT INTO part_r
SELECT vp_rowid, c0boolean FROM source;
-- Single-table query: returns 3 rows
SELECT
SUM(c1int),
vp_rowid,
c1int
FROM source
WHERE c0boolean
GROUP BY vp_rowid, c1int
ORDER BY vp_rowid, c1int
LIMIT 3;
-- Multi-table reconstructed query: returns empty
SELECT
SUM(s.c1int),
s.vp_rowid,
s.c1int
FROM (
SELECT
l.vp_rowid,
r.c0boolean,
l.c1int
FROM part_l l
JOIN LATERAL (
SELECT *
FROM part_r rr
WHERE rr.vp_rowid = l.vp_rowid
) r ON TRUE
) s
WHERE s.c0boolean
GROUP BY s.vp_rowid, s.c1int
ORDER BY s.vp_rowid, s.c1int
LIMIT 3;Expected Result
The multi-table reconstructed query should return the same 3 rows as the single-table query:
-4 4 -4
-5 5 -5
-6 6 -6Actual Result
- The single-table query returns 3 rows:
-4 4 -4
-5 5 -5
-6 6 -6- The multi-table reconstructed query returns an empty result.
COUNT(*)verification:
single_count = 3
split_count = 0Additional Context
Single-table query plan:
Limit
└── Sort(Single)
└── AggregateFinal
└── AggregatePartial
└── TableScan source
output columns: [vp_rowid, c1int]
filters: [is_true(source.c0boolean)]
read rows: 9
partitions total: 1
partitions scanned: 1Key point: the single-table path directly scans source, filters on c0boolean, and then aggregates, actually returning 3 rows.
LATERAL split query plan:
Limit
└── Sort(Single)
└── EvalScalar
└── AggregateFinal
└── AggregatePartial
└── HashJoin
join type: INNER
build keys: [vp_rowid]
probe keys: [vp_rowid]
├── TableScan(Build) part_r
│ output columns: [vp_rowid]
│ filters: [is_true(part_r.c0boolean)]
│ read rows: 9
│ partitions total: 1
│ partitions scanned: 1
│
└── AggregateFinal(Probe)
└── AggregatePartial
└── TableScan part_l
output columns: [vp_rowid, c1int]
read rows: 9
partitions total: 0
partitions scanned: 0
filters: []
apply join filters: [#0]The most suspicious difference is:
part_l: partitions total: 0, partitions scanned: 0But part_l is a MEMORY table with 9 actual rows. The plan estimate still shows an expected output of 3 rows, but the actual split query returns 0 rows.
Measured results:
single_count = 3
split_count = 0Possible cause: the outer aggregation is pushed down to the TableScan of part_l, and since part_l is a MEMORY table with empty partition metadata, partitions total: 0, partitions scanned: 0 results in an empty probe-side input.
Are you willing to submit PR?
- Yes I am willing to submit a PR!
Source: databendlabs/databend