* 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 commits 20a4509866,
5e861e1dea and
cd1462e8f3: 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>
* change dep
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
* feat: adapt to arrow's interval array
* chore: fix compile errors in datatypes crate
* chore: fix api crate compiler errors
* chore: fix compiler errors in common-grpc
* chore: fix common-datasource errors
* chore: fix deprecated code in common-datasource
* fix promql and physical plan related
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
* wip: upgrading network deps
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
* block on updating `sqlparser`
* upgrade sqlparser
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
* adapt new df's trait requirements
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
* chore: fix compiler errors in mito2
* chore: fix common-function crate errors
* chore: fix catalog errors
* change import path
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
* chore: fix some errors in query crate
* chore: fix some errors in query crate
* aggr expr and some other tiny fixes
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
* chore: fix expr related errors in query crate
* chore: fix query serializer and admin command
* chore: fix grpc services
* feat: axum serve
* chore: fix http server
* remove handle_error handler
* refactor timeout layer
* serve axum
* chore: fix flow aggr functions
* chore: fix flow
* feat: fix errors in meta-srv
* boxed()
* use TokioIo
* feat!: Remove script crate and python feature (#5321)
* feat: exclude script crate
* chore: simplify feature
* feat: remove the script crate
* chore: remove python feature and some comments
* chore: fix warning
* chore: fix servers tests compiler errors
* feat: fix tests-integration errors
* chore: fix unused
* test: fix catalog test
* chore: fix compiler errors for crates using common-meta
testing feature is enabled when check with --workspace
* test: use display for logical plan test
* test: implement rewrite for ScanHintRule
* fix: http server build panic
* test: fix mito test
* fix: sql parser type alias error
* test: fix TestClient not listen
* test: some flow tests
* test(flow): more fix
* fix: test_otlp_logs
* test: fix promql test that using deprecated method fun()
* fix: sql type replace supports Int8 ~ Int64, UInt8 ~ UInt64
* test: fix infer schema test case
* test: fix tests related to plan display
* chore: fix last flow test
* test: fix function format related assertion
* test: use larger port range for tests
* fix: test_otlp_traces
* fix: test_otlp_metrics
* fix range query and dist plan
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
* fix: flow handle distinct use deprecated field
* fix: can't pass Join plan expressions to LogicalPlan::with_new_exprs
* test: fix deserialize test
* test: reduce split key case num
* tests: lower case aggr func name
* test: fix some sqlness tests
* tests: more sqlness fix
* tests: fixed sqlness test
* commit non-bug changes
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
* fix: make our udf correct
* fix: implement empty methods of ContextProvider for DfContextProviderAdapter
* test: update sqlness test result
* chore: remove unused
* fix: provide alias name for AggregateExprBuilder in range plan
* test: update range query result
* fix: implement missing ContextProvider methods for DfContextProviderAdapter
* test: update timestamps, cte result
* fix: supports empty projection in mito
* test: update comment for cte test
* fix: support projection for numbers
* test: update test cases after projection fix
* fix: fix range select first_value/last_value
* fix: handle CAST and time index conflict
* fix: handle order by correctly in range first_value/last_value
* test: update sqlness result
* test: update view test result
* test: update decimal test
wait for https://github.com/apache/datafusion/pull/14126 to fix this
* feat: remove redundant physical optimization
todo(ruihang): Check if we can remove this.
* test: update sqlness test result
* chore: range select default sort use nulls_first = false
* test: update filter push down test result
* test: comment deciaml test to avoid different panic message
* test: update some distributed test result
* test: update test for distributed count and filter push down
* test: update subqueries test
* fix: SessionState may overwrite our UDFs
* chore: fix compiler errors after merging main
* fix: fix elasticsearch and dashboard router panic
* chore: fix common-functions tests
* chore: update sqlness result
* test: fix id keyword and update sqlness result
* test: fix flow_null test
* fix: enlarge thread size in debug mode to avoid overflow
* chore: fix warnings in common-function
* chore: fix warning in flow
* chore: fix warnings in query crate
* chore: remove unused warnings
* chore: fix deprecated warnings for parquet
* chore: fix deprecated warning in servers crate
* style: fix clippy
* test: enlarge mito cache tttl test ttl time
* chore: fix typo
* style: fmt toml
* refactor: reimplement PartialOrd for RangeSelect
* chore: remove script crate files introduced by merge
* fix: return error if sql option is not kv
* chore: do not use ..default::default()
* chore: per review
* chore: update error message in BuildAdminFunctionArgsSnafu
Co-authored-by: jeremyhi <jiachun_feng@proton.me>
* refactor: typed precision
* update sqlness view case
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
* chore: flow per review
* chore: add example in comment
* chore: warn if parquet stats of timestamp is not INT64
* style: add a newline before derive to make the comment more clear
* test: update sqlness result
* fix: flow from substrait
* chore: change update_range_context log to debug level
* chore: move axum-extra axum-macros to workspace
---------
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Co-authored-by: Ruihang Xia <waynestxia@gmail.com>
Co-authored-by: luofucong <luofc@foxmail.com>
Co-authored-by: discord9 <discord9@163.com>
Co-authored-by: shuiyisong <xixing.sys@gmail.com>
Co-authored-by: jeremyhi <jiachun_feng@proton.me>
* feat: refine logs for scan
* feat: improve build parts and unordered scan metrics
* feat: change to debug log
* fix: release lock before reading part
* test: replace region id
* test: fix sqlness
* chore: add todo
Co-authored-by: dennis zhuang <killme2008@gmail.com>
---------
Co-authored-by: dennis zhuang <killme2008@gmail.com>
* refactor: RangeBase
* feat: memtable range
* feat: scanner use mem range
* feat: remove base from mem range context
* feat: impl ranges for memtables
* chore: fix warnings
* refactor: make predicate cheap to clone
* refactor: MemRange -> MemtableRange
* feat: pub empty memtable to fix warnings
* test: fix sqlness result
* feat: use fixed error message for unknown error
* feat: return fixed message for internal error as well
* chore: include status code in error message
* test: update tests for asserts of error message
* feat: change status code of some datafusion error
* fix: make CollectRecordbatch an query error
* test: update sqlness results
* feat: eable range expr nest
* fix: change range expr rewrite format
* chore: organize range query tests
* chore: change range expr name(e.g. MAX(v) RANGE 5s FILL 6)
* chore: add range query test
* chore: fix code advice
* chore: fix ca