Skip to content

Commit a50ed34

Browse files
authored
Minor: fix: Include FetchRel when producing LogicalPlan from Sort (#13862)
* include FetchRel when producing LogicalPlan from Sort * add suggested test * address review feedback
1 parent b99400e commit a50ed34

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

datafusion/substrait/src/logical_plan/producer.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,21 +361,45 @@ pub fn to_substrait_rel(
361361
}))),
362362
}))
363363
}
364-
LogicalPlan::Sort(sort) => {
365-
let input = to_substrait_rel(sort.input.as_ref(), state, extensions)?;
366-
let sort_fields = sort
367-
.expr
364+
LogicalPlan::Sort(datafusion::logical_expr::Sort { expr, input, fetch }) => {
365+
let sort_fields = expr
368366
.iter()
369-
.map(|e| substrait_sort_field(state, e, sort.input.schema(), extensions))
367+
.map(|e| substrait_sort_field(state, e, input.schema(), extensions))
370368
.collect::<Result<Vec<_>>>()?;
371-
Ok(Box::new(Rel {
369+
370+
let input = to_substrait_rel(input.as_ref(), state, extensions)?;
371+
372+
let sort_rel = Box::new(Rel {
372373
rel_type: Some(RelType::Sort(Box::new(SortRel {
373374
common: None,
374375
input: Some(input),
375376
sorts: sort_fields,
376377
advanced_extension: None,
377378
}))),
378-
}))
379+
});
380+
381+
match fetch {
382+
Some(amount) => {
383+
let count_mode =
384+
Some(fetch_rel::CountMode::CountExpr(Box::new(Expression {
385+
rex_type: Some(RexType::Literal(Literal {
386+
nullable: false,
387+
type_variation_reference: DEFAULT_TYPE_VARIATION_REF,
388+
literal_type: Some(LiteralType::I64(*amount as i64)),
389+
})),
390+
})));
391+
Ok(Box::new(Rel {
392+
rel_type: Some(RelType::Fetch(Box::new(FetchRel {
393+
common: None,
394+
input: Some(sort_rel),
395+
offset_mode: None,
396+
count_mode,
397+
advanced_extension: None,
398+
}))),
399+
}))
400+
}
401+
None => Ok(sort_rel),
402+
}
379403
}
380404
LogicalPlan::Aggregate(agg) => {
381405
let input = to_substrait_rel(agg.input.as_ref(), state, extensions)?;

datafusion/substrait/tests/cases/roundtrip_logical_plan.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,16 @@ async fn select_with_filter() -> Result<()> {
199199
roundtrip("SELECT * FROM data WHERE a > 1").await
200200
}
201201

202+
#[tokio::test]
203+
async fn select_with_filter_sort_limit() -> Result<()> {
204+
roundtrip("SELECT * FROM data WHERE a > 1 ORDER BY b ASC LIMIT 2").await
205+
}
206+
207+
#[tokio::test]
208+
async fn select_with_filter_sort_limit_offset() -> Result<()> {
209+
roundtrip("SELECT * FROM data WHERE a > 1 ORDER BY b ASC LIMIT 2 OFFSET 1").await
210+
}
211+
202212
#[tokio::test]
203213
async fn select_with_reused_functions() -> Result<()> {
204214
let ctx = create_context().await?;

0 commit comments

Comments
 (0)