Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion datafusion/physical-plan/src/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ impl LimitStream {

match &poll {
Poll::Ready(Some(Ok(batch))) => {
if batch.num_rows() > 0 && self.skip == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the rationale that this condition is redundant because any batch with zero rows will also have self.skip equal to zero?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. To my understanding these two conditions are identical. So I removed one to make it simpler

if batch.num_rows() > 0 {
break poll;
} else {
// continue to poll input stream
Expand Down
9 changes: 7 additions & 2 deletions datafusion/substrait/src/logical_plan/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,13 @@ pub async fn from_substrait_rel(
from_substrait_rel(ctx, input, extensions).await?,
);
let offset = fetch.offset as usize;
let count = fetch.count as usize;
input.limit(offset, Some(count))?.build()
// Since protobuf can't directly distinguish `None` vs `0` `None` is encoded as `MAX`
let count = if fetch.count as usize == usize::MAX {
None
} else {
Some(fetch.count as usize)
};
input.limit(offset, count)?.build()
} else {
not_impl_err!("Fetch without an input is not valid")
}
Expand Down
3 changes: 2 additions & 1 deletion datafusion/substrait/src/logical_plan/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ pub fn to_substrait_rel(
}
LogicalPlan::Limit(limit) => {
let input = to_substrait_rel(limit.input.as_ref(), ctx, extension_info)?;
let limit_fetch = limit.fetch.unwrap_or(0);
// Since protobuf can't directly distinguish `None` vs `0` encode `None` as `MAX`
let limit_fetch = limit.fetch.unwrap_or(usize::MAX);
Ok(Box::new(Rel {
rel_type: Some(RelType::Fetch(Box::new(FetchRel {
common: None,
Expand Down
5 changes: 5 additions & 0 deletions datafusion/substrait/tests/cases/roundtrip_logical_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ async fn select_with_limit() -> Result<()> {
roundtrip_fill_na("SELECT * FROM data LIMIT 100").await
}

#[tokio::test]
async fn select_without_limit() -> Result<()> {
roundtrip_fill_na("SELECT * FROM data OFFSET 10").await
}

#[tokio::test]
async fn select_with_limit_offset() -> Result<()> {
roundtrip("SELECT * FROM data LIMIT 200 OFFSET 10").await
Expand Down