mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-26 12:44:10 +00:00
* test(query): rewrite deep UNION ALL plans on a small stack Run `RangePlanRewriter::rewrite` on a 2 MiB thread, the stack size of a release tokio worker, over two plans built directly: 400 `SELECT 1 AS a` joined by `UNION ALL`, and a range query over 400 `UNION ALL` branches that scan `test`. The rewrite recurses once per plan level, so the test aborts with a stack overflow (#9356). Signed-off-by: dayan02dev <pawarudayan92@gmail.com> * fix(query): rewrite range plans without recursion `RangePlanRewriter` runs on every SQL plan, and `rewrite_logical_plan` recursed through `#[async_recursion]` once per plan level, polling one boxed future per level on the same stack. A chain of `UNION ALL` plans to nested `Union` nodes, so a few hundred branches overflowed a 2 MiB release worker stack and aborted the server (#9356). Visit the nodes in post-order with explicit stacks instead. The per-node rewrite moves unchanged into `rewrite_plan_node`, and nodes are still rewritten inputs first, left to right. Signed-off-by: dayan02dev <pawarudayan92@gmail.com> * test(query): plan deep UNION ALL queries through the SQL planner Plan 400-branch `UNION ALL` chains, with and without a range query on top, through `DfLogicalPlanner` on an 8 MiB thread, the stack of a debug-build tokio worker. In a debug build the recursive range rewrite overflowed that stack from about 355 branches. With the iterative walk the first overflow moves to about 470 branches, in the statement clone of `TryFrom<&Statement> for DfStatement`. Signed-off-by: dayan02dev <pawarudayan92@gmail.com> * test(sqlness): cover queries over long UNION ALL chains Run `count(*)` over 256 `SELECT 1 AS a` joined by `UNION ALL`, and a range query over 256 `UNION ALL` branches that scan one table. Both chains plan to nested unions that every SQL plan walks through the range rewrite. The chains come from `SQLNESS TEMPLATE` loops. Signed-off-by: dayan02dev <pawarudayan92@gmail.com> * test(sqlness): keep the deep UNION ALL range query fast The range query in range/deep_union scanned `host` in all 256 branches. In CI's debug builds the case took 19-36s, nearly all of it in the distributed planner analyzer for this query, so the query can cross the 30s slow query threshold. The frontend then creates `greptime_private.slow_queries`, and its region shows up in the unfiltered `information_schema.region_peers` queries of common/partition, which fails with one extra row per query. Keep the 256-branch chain but let only the first branch scan rows; the other branches are `WHERE false`. The query result is unchanged. Signed-off-by: dayan02dev <pawarudayan92@gmail.com> * test(frontend): run a long UNION ALL chain through do_query `Instance::do_query_inner` clones the parsed statement for its error log, the slow query timer clones it again, and `TryFrom<&Statement> for DfStatement` clones the query once more before planning. The derived `Clone` of sqlparser's left-deep `SetExpr` recurses once per branch, so a 550-branch `UNION ALL` aborts on an 8 MiB thread, the stack size of a debug-build worker. Also check that the log line of a failed statement keeps its credentials masked. Signed-off-by: dayan02dev <pawarudayan92@gmail.com> * fix: plan SQL statements without cloning them A SQL query was deep-cloned three times on its way to the planner: `Instance::do_query_inner` kept a copy for its error log, the slow query timer kept another, and `TryFrom<&Statement> for DfStatement` cloned the query before handing it to `SqlToRel`. sqlparser's `SetExpr` derives `Clone`, which recurses once per `UNION ALL` branch, so a long left-deep chain overflowed the stack in these clones (#9356). Move the statement instead: - `TryFrom<Statement> for DfStatement` replaces the `&Statement` impl and moves the parsed query out. - `LogicalPlanner::plan` and `StatementExecutor::plan` take the `QueryStatement` by value, and `DfLogicalPlanner::plan_sql` and `explain_to_plan` consume the statement. - `do_query_inner` renders the statement once and uses that text for the process list, the slow query timer and the error log. The text comes from the statement's `Display`, so credentials stay masked. - `plan_and_exec_sql` copies the statement for `pre_execute` only when a `SqlQueryInterceptor` is registered. In a debug build, the longest `SELECT 1 AS a UNION ALL ...` chain that runs through `do_query_inner` grows from 433 to 722 branches on an 8 MiB stack and from 70 to 128 on a 2 MiB stack. The next limit is the recursive Substrait encoding of the plan in the distributed planner. Signed-off-by: dayan02dev <pawarudayan92@gmail.com> * refactor(catalog): drop the unused QueryStatement::Sql variant Nothing constructs process_manager::QueryStatement::Sql anymore; the frontend renders SQL text through the Plan variant. Remove the variant, its Display and slow-query-event arms, and the now-unused Statement import, and correct the Plan doc comment. Signed-off-by: dayan02dev <pawarudayan92@gmail.com> * revert: move the statement ownership change to a separate PR This reverts commits20a4509866,5e861e1deaandcd1462e8f3: planning SQL statements by value instead of cloning them, its frontend tests, and the removal of the catalog `QueryStatement::Sql` variant that followed from it. As discussed in #10386, that refactor goes to a separate PR, so this one only changes the range plan rewrite. Signed-off-by: dayan02dev <pawarudayan92@gmail.com> --------- Signed-off-by: dayan02dev <pawarudayan92@gmail.com>
58 lines
1.8 KiB
Plaintext
58 lines
1.8 KiB
Plaintext
CREATE TABLE host (
|
|
ts timestamp(3) time index,
|
|
host STRING PRIMARY KEY,
|
|
val BIGINT,
|
|
);
|
|
|
|
Affected Rows: 0
|
|
|
|
INSERT INTO TABLE host VALUES
|
|
(0, 'host1', 0),
|
|
(5000, 'host1', null),
|
|
(10000, 'host1', 1),
|
|
(15000, 'host1', null),
|
|
(20000, 'host1', 2),
|
|
(0, 'host2', 3),
|
|
(5000, 'host2', null),
|
|
(10000, 'host2', 4),
|
|
(15000, 'host2', null),
|
|
(20000, 'host2', 5);
|
|
|
|
Affected Rows: 10
|
|
|
|
-- Test long UNION ALL chains, which plan to deeply nested unions
|
|
-- SQLNESS TEMPLATE
|
|
SELECT count(*) FROM ({% for i in range(256) %}{% if not loop.first %} UNION ALL {% endif %}SELECT 1 AS a{% endfor %});
|
|
|
|
+----------+
|
|
| count(*) |
|
|
+----------+
|
|
| 256 |
|
|
+----------+
|
|
|
|
-- Only the first branch returns rows. 256 branches that all scan `host` can
|
|
-- take over 30s in a debug build, which records a slow query and creates the
|
|
-- `greptime_private.slow_queries` table.
|
|
-- SQLNESS TEMPLATE
|
|
SELECT ts, host, min(val) RANGE '5s' FROM (SELECT ts, host, val FROM host{% for i in range(255) %} UNION ALL SELECT ts, host, val FROM host WHERE false{% endfor %}) ALIGN '5s' BY (host) ORDER BY host, ts;
|
|
|
|
+---------------------+-------+-------------------+
|
|
| ts | host | min(val) RANGE 5s |
|
|
+---------------------+-------+-------------------+
|
|
| 1970-01-01T00:00:00 | host1 | 0 |
|
|
| 1970-01-01T00:00:05 | host1 | |
|
|
| 1970-01-01T00:00:10 | host1 | 1 |
|
|
| 1970-01-01T00:00:15 | host1 | |
|
|
| 1970-01-01T00:00:20 | host1 | 2 |
|
|
| 1970-01-01T00:00:00 | host2 | 3 |
|
|
| 1970-01-01T00:00:05 | host2 | |
|
|
| 1970-01-01T00:00:10 | host2 | 4 |
|
|
| 1970-01-01T00:00:15 | host2 | |
|
|
| 1970-01-01T00:00:20 | host2 | 5 |
|
|
+---------------------+-------+-------------------+
|
|
|
|
DROP TABLE host;
|
|
|
|
Affected Rows: 0
|
|
|