Files
greptimedb/tests/cases/standalone/common/subquery/table.result
T
discord9 9e9a8cac20 fix(query): insert MergeScan into nested scalar subqueries (#9261)
* fix(query): insert MergeScan into nested scalar subqueries

DataFusion 55 keeps uncorrelated scalar subqueries as expression
subqueries (enable_physical_uncorrelated_scalar_subquery, default true)
instead of decorrelating them into joins, and executes them via the new
physical ScalarSubqueryExec. DistPlannerAnalyzer::try_push_down walked
the plan with a plain TreeNode transform that does not descend into
expression subqueries, so MergeScan was only inserted for depth-1
subqueries. A scalar subquery nested inside another scalar subquery kept
a bare frontend DistTable TableScan and failed at execution with
"Unsupported operation: get stream from a distributed table".

Use the subquery-aware transform so handle_subquery (PlanRewriter /
MergeScan insertion) runs for subquery plans at every nesting depth.

Fixes #9260.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

* test(query): strengthen nested scalar subquery regression coverage

Address review on #9261:
- Replace the ineffective 'no bare TableScan' string check with a real
  subquery-aware plan walk (apply_with_subqueries); MergeScan hides its
  remote input from traversal, so any TableScan the walk reaches was
  genuinely left unwrapped.
- Add a distributed regression case on a range-partitioned table so the
  nested inner aggregate must merge partial results across regions
  (global AVG feeding an outer SUM filter).

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>

---------

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
2026-09-21 03:58:38 +00:00

248 lines
5.7 KiB
Plaintext

-- aliasing, from:
-- https://github.com/duckdb/duckdb/blob/9196dd9b0a163e6c8aada26218803d04be30c562/test/sql/subquery/table/test_aliasing.test
CREATE TABLE a(ts TIMESTAMP TIME INDEX, i INTEGER);
Affected Rows: 0
insert into a values (1, 42);
Affected Rows: 1
SELECT * FROM (SELECT i AS j FROM a GROUP BY j) WHERE j = 42;
+----+
| j |
+----+
| 42 |
+----+
SELECT * FROM (SELECT i AS j FROM a GROUP BY i) WHERE j = 42;
+----+
| j |
+----+
| 42 |
+----+
DROP TABLE a;
Affected Rows: 0
-- nested table subquery, from:
-- https://github.com/duckdb/duckdb/blob/2e4e2913266ddc46c7281d1b992228cb0095954b/test/sql/subquery/table/test_nested_table_subquery.test_slow
CREATE TABLE test (ts TIMESTAMP TIME INDEX, i INTEGER, j INTEGER);
Affected Rows: 0
INSERT INTO test VALUES (0, 3, 4), (1, 4, 5), (2, 5, 6);
Affected Rows: 3
SELECT * FROM (SELECT i, j FROM (SELECT j AS i, i AS j FROM (SELECT j AS i, i AS j FROM test) AS a) AS a) AS a, (SELECT i+1 AS r,j FROM test) AS b, test WHERE a.i=b.r AND test.j=a.i ORDER BY 1;
+---+---+---+---+-------------------------+---+---+
| i | j | r | j | ts | i | j |
+---+---+---+---+-------------------------+---+---+
| 4 | 5 | 4 | 4 | 1970-01-01T00:00:00 | 3 | 4 |
| 5 | 6 | 5 | 5 | 1970-01-01T00:00:00.001 | 4 | 5 |
+---+---+---+---+-------------------------+---+---+
SELECT i FROM (SELECT i + 1 AS i FROM (SELECT i + 1 AS i FROM (SELECT i + 1 AS i FROM test)));
+---+
| i |
+---+
| 6 |
| 7 |
| 8 |
+---+
DROP TABLE test;
Affected Rows: 0
-- subquery union, from:
-- https://github.com/duckdb/duckdb/blob/9196dd9b0a163e6c8aada26218803d04be30c562/test/sql/subquery/table/test_subquery_union.test
SELECT * FROM (SELECT 42) UNION ALL SELECT * FROM (SELECT 43) ORDER BY 1;
+-----------+
| Int64(42) |
+-----------+
| 42 |
| 43 |
+-----------+
-- table subquery, from:
-- https://github.com/duckdb/duckdb/blob/8704c7d0807d6ce1e2ebcdf6398e1b6cc050e507/test/sql/subquery/table/test_table_subquery.test
CREATE TABLE test (ts TIMESTAMP TIME INDEX, i INTEGER, j INTEGER);
Affected Rows: 0
INSERT INTO test VALUES (0, 3, 4), (1, 4, 5), (2, 5, 6);
Affected Rows: 3
SELECT * FROM (SELECT i, j AS d FROM test ORDER BY i) AS b;
+---+---+
| i | d |
+---+---+
| 3 | 4 |
| 4 | 5 |
| 5 | 6 |
+---+---+
SELECT b.d FROM (SELECT i * 2 + j AS d FROM test) AS b;
+----+
| d |
+----+
| 10 |
| 13 |
| 16 |
+----+
SELECT a.i,a.j,b.r,b.j FROM (SELECT i, j FROM test) AS a INNER JOIN (SELECT i+1 AS r,j FROM test) AS b ON a.i=b.r ORDER BY 1;
+---+---+---+---+
| i | j | r | j |
+---+---+---+---+
| 4 | 5 | 4 | 4 |
| 5 | 6 | 5 | 5 |
+---+---+---+---+
SELECT * FROM (SELECT i, j FROM test) AS a, (SELECT i+1 AS r,j FROM test) AS b, test WHERE a.i=b.r AND test.j=a.i ORDER BY 1;
+---+---+---+---+-------------------------+---+---+
| i | j | r | j | ts | i | j |
+---+---+---+---+-------------------------+---+---+
| 4 | 5 | 4 | 4 | 1970-01-01T00:00:00 | 3 | 4 |
| 5 | 6 | 5 | 5 | 1970-01-01T00:00:00.001 | 4 | 5 |
+---+---+---+---+-------------------------+---+---+
SELECT sum(x) FROM (SELECT i AS x FROM test GROUP BY i) sq;
+-----------+
| sum(sq.x) |
+-----------+
| 12 |
+-----------+
SELECT sum(x) FROM (SELECT i+1 AS x FROM test GROUP BY x) sq;
+-----------+
| sum(sq.x) |
+-----------+
| 15 |
+-----------+
DROP TABLE test;
Affected Rows: 0
-- test unamed subquery, from:
-- https://github.com/duckdb/duckdb/blob/00a605270719941ca0412ad5d0a14b1bdfbf9eb5/test/sql/subquery/table/test_unnamed_subquery.test
SELECT a FROM (SELECT 42 a);
+----+
| a |
+----+
| 42 |
+----+
SELECT * FROM (SELECT 42 a), (SELECT 43 b);
+----+----+
| a | b |
+----+----+
| 42 | 43 |
+----+----+
SELECT * FROM (VALUES (42, 43));
+---------+---------+
| column1 | column2 |
+---------+---------+
| 42 | 43 |
+---------+---------+
SELECT * FROM (SELECT 42 a), (SELECT 43 b), (SELECT 44 c), (SELECT 45 d);
+----+----+----+----+
| a | b | c | d |
+----+----+----+----+
| 42 | 43 | 44 | 45 |
+----+----+----+----+
SELECT * FROM (SELECT * FROM (SELECT 42 a), (SELECT 43 b)) JOIN (SELECT 44 c) ON (true) JOIN (SELECT 45 d) ON (true);
+----+----+----+----+
| a | b | c | d |
+----+----+----+----+
| 42 | 43 | 44 | 45 |
+----+----+----+----+
-- skipped, unsupported feature: unnamed_subquery, see also:
-- https://github.com/GreptimeTeam/greptimedb/issues/5012
-- SELECT * FROM (SELECT unnamed_subquery.a FROM (SELECT 42 a)), (SELECT unnamed_subquery.b FROM (SELECT 43 b));
-- SELECT unnamed_subquery.a, unnamed_subquery2.b FROM (SELECT 42 a), (SELECT 43 b);
-- Regression test for https://github.com/GreptimeTeam/greptimedb/issues/9260:
-- nested scalar subqueries in the projection must not hit
-- "Unsupported operation: get stream from a distributed table".
CREATE TABLE nested_scalar (k INT, v BIGINT, ts TIMESTAMP TIME INDEX);
Affected Rows: 0
INSERT INTO nested_scalar VALUES (1, 10, '2024-01-01 00:00:00'), (2, NULL, '2024-01-01 00:00:01');
Affected Rows: 2
-- control group: single-level scalar subqueries
SELECT (SELECT MAX(v) FROM nested_scalar) AS m FROM nested_scalar LIMIT 1;
+----+
| m |
+----+
| 10 |
+----+
SELECT (SELECT MAX(v) FROM nested_scalar WHERE k = 1) AS m FROM nested_scalar LIMIT 1;
+----+
| m |
+----+
| 10 |
+----+
-- scalar subquery nested inside another scalar subquery's filter
SELECT (SELECT MAX(v) FROM nested_scalar WHERE v > (SELECT MAX(v) FROM nested_scalar)) AS m FROM nested_scalar LIMIT 1;
+---+
| m |
+---+
| |
+---+
-- scalar subquery nested one level deeper
SELECT (SELECT (SELECT MAX(v) FROM nested_scalar)) AS m FROM nested_scalar LIMIT 1;
+----+
| m |
+----+
| 10 |
+----+
-- three levels deep
SELECT (SELECT (SELECT (SELECT MAX(v) FROM nested_scalar))) AS m FROM nested_scalar LIMIT 1;
+----+
| m |
+----+
| 10 |
+----+
DROP TABLE nested_scalar;
Affected Rows: 0