Skip to content

Commit

Permalink
fix: explain range query (#2664)
Browse files Browse the repository at this point in the history
* fix: explain range query

* chore: add comment
  • Loading branch information
Taylor-lagrange authored Oct 30, 2023
1 parent 000e147 commit d2f3793
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/query/src/range_select/plan_rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ use datafusion_common::tree_node::{TreeNode, TreeNodeRewriter, VisitRecursion};
use datafusion_common::{DFSchema, DataFusionError, Result as DFResult};
use datafusion_expr::expr::ScalarUDF;
use datafusion_expr::{
Aggregate, Expr, ExprSchemable, Extension, LogicalPlan, LogicalPlanBuilder, Projection,
Aggregate, Analyze, Explain, Expr, ExprSchemable, Extension, LogicalPlan, LogicalPlanBuilder,
Projection,
};
use datatypes::prelude::ConcreteDataType;
use promql_parser::util::parse_duration;
use snafu::{OptionExt, ResultExt};
use snafu::{ensure, OptionExt, ResultExt};
use table::table::adapter::DfTableProviderAdapter;

use super::plan::Fill;
Expand Down Expand Up @@ -265,9 +266,38 @@ impl RangePlanRewriter {
None => y.clone(),
})
.collect();
Ok(Some(
plan.with_new_inputs(&inputs).context(DataFusionSnafu)?,
))
// Due to the limitations of Datafusion, for `LogicalPlan::Analyze` and `LogicalPlan::Explain`,
// directly using the method `with_new_inputs` to rebuild a new `LogicalPlan` will cause an error,
// so here we directly use the `LogicalPlanBuilder` to build a new plan.
let plan = match plan {
LogicalPlan::Analyze(Analyze { verbose, .. }) => {
ensure!(
inputs.len() == 1,
RangeQuerySnafu {
msg: "Illegal subplan nums when rewrite Analyze logical plan",
}
);
LogicalPlanBuilder::from(inputs[0].clone())
.explain(*verbose, true)
.context(DataFusionSnafu)?
.build()
}
LogicalPlan::Explain(Explain { verbose, .. }) => {
ensure!(
inputs.len() == 1,
RangeQuerySnafu {
msg: "Illegal subplan nums when rewrite Explain logical plan",
}
);
LogicalPlanBuilder::from(inputs[0].clone())
.explain(*verbose, false)
.context(DataFusionSnafu)?
.build()
}
_ => plan.with_new_inputs(&inputs),
}
.context(DataFusionSnafu)?;
Ok(Some(plan))
} else {
Ok(None)
}
Expand Down
31 changes: 31 additions & 0 deletions tests/cases/standalone/common/range/nest.result
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,37 @@ SELECT ts, b, min(c) RANGE '5s' FROM (SELECT ts, host AS b, val AS c FROM host W
| 1970-01-01T00:00:20 | host1 | 2 |
+---------------------+-------+---------------------------+

-- Test EXPLAIN and ANALYZE
-- SQLNESS REPLACE (-+) -
-- SQLNESS REPLACE (\s\s+) _
-- SQLNESS REPLACE (peers.*) REDACTED
-- SQLNESS REPLACE (metrics.*) REDACTED
EXPLAIN SELECT ts, host, min(val) RANGE '5s' FROM host ALIGN '5s';

+-+-+
| plan_type_| plan_|
+-+-+
| logical_plan_| RangeSelect: range_exprs=[MIN(host.val) RANGE 5s FILL NULL], align=5s time_index=ts_|
|_|_MergeScan [is_placeholder=false]_|
| physical_plan | RangeSelectExec: range_expr=[RangeFnExec{ MIN(host.val), range: 5000}], align=5000, time_index=ts, by=[host@1] |
|_|_MergeScanExec: REDACTED
|_|_|
+-+-+

-- SQLNESS REPLACE (-+) -
-- SQLNESS REPLACE (\s\s+) _
-- SQLNESS REPLACE (peers.*) REDACTED
-- SQLNESS REPLACE (metrics.*) REDACTED
EXPLAIN ANALYZE SELECT ts, host, min(val) RANGE '5s' FROM host ALIGN '5s';

+-+-+
| plan_type_| plan_|
+-+-+
| Plan with Metrics | RangeSelectExec: range_expr=[RangeFnExec{ MIN(host.val), range: 5000}], align=5000, time_index=ts, by=[host@1], REDACTED
|_|_MergeScanExec: REDACTED
|_|_|
+-+-+

DROP TABLE host;

Affected Rows: 0
Expand Down
15 changes: 15 additions & 0 deletions tests/cases/standalone/common/range/nest.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,19 @@ SELECT ts, host, foo FROM (SELECT ts, host, min(val) RANGE '5s' AS foo FROM host

SELECT ts, b, min(c) RANGE '5s' FROM (SELECT ts, host AS b, val AS c FROM host WHERE host = 'host1') ALIGN '5s' BY (b) ORDER BY b, ts;


-- Test EXPLAIN and ANALYZE

-- SQLNESS REPLACE (-+) -
-- SQLNESS REPLACE (\s\s+) _
-- SQLNESS REPLACE (peers.*) REDACTED
-- SQLNESS REPLACE (metrics.*) REDACTED
EXPLAIN SELECT ts, host, min(val) RANGE '5s' FROM host ALIGN '5s';

-- SQLNESS REPLACE (-+) -
-- SQLNESS REPLACE (\s\s+) _
-- SQLNESS REPLACE (peers.*) REDACTED
-- SQLNESS REPLACE (metrics.*) REDACTED
EXPLAIN ANALYZE SELECT ts, host, min(val) RANGE '5s' FROM host ALIGN '5s';

DROP TABLE host;

0 comments on commit d2f3793

Please sign in to comment.