Skip to content

Commit df6f4e6

Browse files
evenyagavantgardnerio
authored andcommitted
fix: rewrite fetch, skip of the Limit node in correct order (apache#14496) v46
* fix: rewrite fetch, skip of the Limit node in correct order * style: fix clippy
1 parent d90a102 commit df6f4e6

1 file changed

Lines changed: 46 additions & 18 deletions

File tree

  • datafusion/expr/src/logical_plan

datafusion/expr/src/logical_plan/plan.rs

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -953,8 +953,9 @@ impl LogicalPlan {
953953
expr.len()
954954
);
955955
}
956-
let new_skip = skip.as_ref().and_then(|_| expr.pop());
956+
// `LogicalPlan::expressions()` returns in [skip, fetch] order, so we can pop from the end.
957957
let new_fetch = fetch.as_ref().and_then(|_| expr.pop());
958+
let new_skip = skip.as_ref().and_then(|_| expr.pop());
958959
let input = self.only_input(inputs)?;
959960
Ok(LogicalPlan::Limit(Limit {
960961
skip: new_skip.map(Box::new),
@@ -4163,23 +4164,50 @@ digraph {
41634164

41644165
#[test]
41654166
fn test_limit_with_new_children() {
4166-
let limit = LogicalPlan::Limit(Limit {
4167-
skip: None,
4168-
fetch: Some(Box::new(Expr::Literal(
4169-
ScalarValue::new_ten(&DataType::UInt32).unwrap(),
4170-
))),
4171-
input: Arc::new(LogicalPlan::Values(Values {
4172-
schema: Arc::new(DFSchema::empty()),
4173-
values: vec![vec![]],
4174-
})),
4175-
});
4176-
let new_limit = limit
4177-
.with_new_exprs(
4178-
limit.expressions(),
4179-
limit.inputs().into_iter().cloned().collect(),
4180-
)
4181-
.unwrap();
4182-
assert_eq!(limit, new_limit);
4167+
let input = Arc::new(LogicalPlan::Values(Values {
4168+
schema: Arc::new(DFSchema::empty()),
4169+
values: vec![vec![]],
4170+
}));
4171+
let cases = [
4172+
LogicalPlan::Limit(Limit {
4173+
skip: None,
4174+
fetch: None,
4175+
input: Arc::clone(&input),
4176+
}),
4177+
LogicalPlan::Limit(Limit {
4178+
skip: None,
4179+
fetch: Some(Box::new(Expr::Literal(
4180+
ScalarValue::new_ten(&DataType::UInt32).unwrap(),
4181+
))),
4182+
input: Arc::clone(&input),
4183+
}),
4184+
LogicalPlan::Limit(Limit {
4185+
skip: Some(Box::new(Expr::Literal(
4186+
ScalarValue::new_ten(&DataType::UInt32).unwrap(),
4187+
))),
4188+
fetch: None,
4189+
input: Arc::clone(&input),
4190+
}),
4191+
LogicalPlan::Limit(Limit {
4192+
skip: Some(Box::new(Expr::Literal(
4193+
ScalarValue::new_one(&DataType::UInt32).unwrap(),
4194+
))),
4195+
fetch: Some(Box::new(Expr::Literal(
4196+
ScalarValue::new_ten(&DataType::UInt32).unwrap(),
4197+
))),
4198+
input,
4199+
}),
4200+
];
4201+
4202+
for limit in cases {
4203+
let new_limit = limit
4204+
.with_new_exprs(
4205+
limit.expressions(),
4206+
limit.inputs().into_iter().cloned().collect(),
4207+
)
4208+
.unwrap();
4209+
assert_eq!(limit, new_limit);
4210+
}
41834211
}
41844212

41854213
#[test]

0 commit comments

Comments
 (0)