Skip to content

Commit 9e663dc

Browse files
committed
feat: Add basic output_hints to SortExec
1 parent d92a1d8 commit 9e663dc

File tree

1 file changed

+29
-11
lines changed
  • datafusion/src/physical_plan

1 file changed

+29
-11
lines changed

datafusion/src/physical_plan/sort.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
//! Defines the SORT plan
1919
20-
use super::{RecordBatchStream, SendableRecordBatchStream};
20+
use crate::physical_plan::{OptimizerHints, RecordBatchStream, SendableRecordBatchStream};
2121
use crate::cube_ext;
2222
use crate::error::{DataFusionError, Result};
23-
use crate::physical_plan::expressions::PhysicalSortExpr;
23+
use crate::physical_plan::expressions::{Column, PhysicalSortExpr};
2424
use crate::physical_plan::{
2525
common, DisplayFormatType, Distribution, ExecutionPlan, Partitioning, SQLMetric,
2626
};
@@ -186,15 +186,33 @@ impl ExecutionPlan for SortExec {
186186
metrics
187187
}
188188

189-
// TODO
190-
// fn output_sort_order(&self) -> Result<Option<Vec<usize>>> {
191-
// let mut order = Vec::with_capacity(self.expr.len());
192-
// for s in &self.expr {
193-
// let col = s.expr.as_any().downcast_ref::<Column>()?;
194-
// order.push(self.schema().index_of(col.name())?);
195-
// }
196-
// Ok(Some(order))
197-
// }
189+
fn output_hints(&self) -> OptimizerHints {
190+
let mut order = Vec::with_capacity(self.expr.len());
191+
// let mut sort_order_truncated = false;
192+
for s in &self.expr {
193+
let column = s.expr.as_any().downcast_ref::<Column>();
194+
if column.is_none() {
195+
// sort_order_truncated = true;
196+
break;
197+
}
198+
let column = column.unwrap();
199+
200+
let index: usize = match self.schema().index_of(column.name()) {
201+
Ok(ix) => ix,
202+
Err(_) => return OptimizerHints::default()
203+
};
204+
order.push(index);
205+
}
206+
207+
let input_hints = self.input.output_hints();
208+
// TODO: If sort_order_truncated is false, we can combine input_hints.sort_order. Do this.
209+
210+
OptimizerHints{
211+
sort_order: Some(order),
212+
single_value_columns: input_hints.single_value_columns.clone(),
213+
}
214+
}
215+
198216
}
199217

200218
#[tracing::instrument(level = "trace", skip(batch, schema, expr))]

0 commit comments

Comments
 (0)