Skip to content

Commit 21a209f

Browse files
authored
fix: skip replacing exprs of the DistinctOn node (#5823)
* fix: handle distinct on specially * chore: update comment
1 parent 917510f commit 21a209f

File tree

6 files changed

+42
-9
lines changed

6 files changed

+42
-9
lines changed

src/query/src/dist_plan/analyzer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ impl AnalyzerRule for DistPlannerAnalyzer {
6666

6767
impl DistPlannerAnalyzer {
6868
fn inspect_plan_with_subquery(plan: LogicalPlan) -> DfResult<Transformed<LogicalPlan>> {
69-
// Workaround for https://github.com/GreptimeTeam/greptimedb/issues/5469
70-
// FIXME(yingwen): Remove this once we update DataFusion.
71-
if let LogicalPlan::Limit(_) = &plan {
69+
// Workaround for https://github.com/GreptimeTeam/greptimedb/issues/5469 and https://github.com/GreptimeTeam/greptimedb/issues/5799
70+
// FIXME(yingwen): Remove the `Limit` plan once we update DataFusion.
71+
if let LogicalPlan::Limit(_) | LogicalPlan::Distinct(_) = &plan {
7272
return Ok(Transformed::no(plan));
7373
}
7474

src/query/src/optimizer/string_normalization.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ impl AnalyzerRule for StringNormalizationRule {
4646
| LogicalPlan::Values(_)
4747
| LogicalPlan::Analyze(_)
4848
| LogicalPlan::Extension(_)
49-
| LogicalPlan::Distinct(_)
5049
| LogicalPlan::Dml(_)
5150
| LogicalPlan::Copy(_)
5251
| LogicalPlan::RecursiveQuery(_) => {
@@ -63,7 +62,8 @@ impl AnalyzerRule for StringNormalizationRule {
6362
Ok(Transformed::no(plan))
6463
}
6564
}
66-
LogicalPlan::Limit(_)
65+
LogicalPlan::Distinct(_)
66+
| LogicalPlan::Limit(_)
6767
| LogicalPlan::Explain(_)
6868
| LogicalPlan::Unnest(_)
6969
| LogicalPlan::Ddl(_)

src/query/src/optimizer/type_conversion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ impl ExtensionAnalyzerRule for TypeConversionRule {
8888
| LogicalPlan::Sort { .. }
8989
| LogicalPlan::Union { .. }
9090
| LogicalPlan::Join { .. }
91-
| LogicalPlan::Distinct { .. }
9291
| LogicalPlan::Values { .. }
9392
| LogicalPlan::Analyze { .. } => {
9493
let mut converter = TypeConverter {
@@ -105,7 +104,8 @@ impl ExtensionAnalyzerRule for TypeConversionRule {
105104
plan.with_new_exprs(expr, inputs).map(Transformed::yes)
106105
}
107106

108-
LogicalPlan::Limit { .. }
107+
LogicalPlan::Distinct { .. }
108+
| LogicalPlan::Limit { .. }
109109
| LogicalPlan::Subquery { .. }
110110
| LogicalPlan::Explain { .. }
111111
| LogicalPlan::SubqueryAlias { .. }

src/query/src/range_select/plan_rewrite.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ use datafusion_expr::execution_props::ExecutionProps;
3232
use datafusion_expr::expr::WildcardOptions;
3333
use datafusion_expr::simplify::SimplifyContext;
3434
use datafusion_expr::{
35-
Aggregate, Analyze, Cast, Explain, Expr, ExprSchemable, Extension, LogicalPlan,
36-
LogicalPlanBuilder, Projection,
35+
Aggregate, Analyze, Cast, Distinct, DistinctOn, Explain, Expr, ExprSchemable, Extension,
36+
LogicalPlan, LogicalPlanBuilder, Projection,
3737
};
3838
use datafusion_optimizer::simplify_expressions::ExprSimplifier;
3939
use datatypes::prelude::ConcreteDataType;
@@ -453,6 +453,28 @@ impl RangePlanRewriter {
453453
.context(DataFusionSnafu)?
454454
.build()
455455
}
456+
LogicalPlan::Distinct(Distinct::On(DistinctOn {
457+
on_expr,
458+
select_expr,
459+
sort_expr,
460+
..
461+
})) => {
462+
ensure!(
463+
inputs.len() == 1,
464+
RangeQuerySnafu {
465+
msg:
466+
"Illegal subplan nums when rewrite DistinctOn logical plan",
467+
}
468+
);
469+
LogicalPlanBuilder::from(inputs[0].clone())
470+
.distinct_on(
471+
on_expr.clone(),
472+
select_expr.clone(),
473+
sort_expr.clone(),
474+
)
475+
.context(DataFusionSnafu)?
476+
.build()
477+
}
456478
_ => plan.with_new_exprs(plan.expressions_consider_join(), inputs),
457479
}
458480
.context(DataFusionSnafu)?;

tests/cases/standalone/common/aggregate/distinct.result

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ SELECT DISTINCT CASE WHEN a > 11 THEN 11 ELSE a END FROM test;
6969
| 11 |
7070
+-------------------------------------------------------------+
7171

72+
SELECT DISTINCT ON (a) * FROM test ORDER BY a, t DESC;
73+
74+
+----+----+-------------------------+
75+
| a | b | t |
76+
+----+----+-------------------------+
77+
| 11 | 22 | 1970-01-01T00:00:00.004 |
78+
| 13 | 22 | 1970-01-01T00:00:00.002 |
79+
+----+----+-------------------------+
80+
7281
DROP TABLE test;
7382

7483
Affected Rows: 0

tests/cases/standalone/common/aggregate/distinct.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ SELECT DISTINCT MAX(b) FROM test GROUP BY a;
1616

1717
SELECT DISTINCT CASE WHEN a > 11 THEN 11 ELSE a END FROM test;
1818

19+
SELECT DISTINCT ON (a) * FROM test ORDER BY a, t DESC;
20+
1921
DROP TABLE test;

0 commit comments

Comments
 (0)