Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor: fix: Include FetchRel when producing LogicalPlan from Sort #13862

Merged
merged 3 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 33 additions & 2 deletions datafusion/substrait/src/logical_plan/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,45 @@ pub fn to_substrait_rel(
.iter()
.map(|e| substrait_sort_field(state, e, sort.input.schema(), extensions))
.collect::<Result<Vec<_>>>()?;
Ok(Box::new(Rel {

let sort_rel = Box::new(Rel {
rel_type: Some(RelType::Sort(Box::new(SortRel {
common: None,
input: Some(input),
sorts: sort_fields,
advanced_extension: None,
}))),
}))
});

match sort.fetch {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Another pattern I have seen to avoid missing fields like this is

let Sort { input, offset } = sort ;
...

That way the compiler will tell you when you have not handled some field and you have to explicitly list it out

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Making sure I follow:

You mean on line 364 destructuring the plan like

LogicalPlan::Sort(Sort {expr, input, fetch}) => {...

I didn't see that pattern followed in that file for the other LogicalPlan inners. I think its a good idea though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Some(_) => {
let empty_schema = Arc::new(DFSchema::empty());
let count_mode = sort
.fetch
.map(|amount| {
Copy link
Contributor

Choose a reason for hiding this comment

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

given you have the match sort.fetch { Some(_) => above, you could store that "some" and then use it here

match sort.fetch {
   Some(amount) => { 
      ...
      let count_mode = to_substrait_rex(.., (amount as i64), ...)

Copy link
Contributor

Choose a reason for hiding this comment

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

also I think rather than using to_substrait_rex, you could create the substrait expr directly, that might be nicer here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ty for the suggestions! Heading to dinner now and I can address these tomorrow morning eastern time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Incorporated both suggestions. Thank you @Blizzara

to_substrait_rex(
state,
&Expr::Literal(ScalarValue::Int64(Some(amount as i64))),
&empty_schema,
0,
extensions,
)
})
.transpose()?
.map(Box::new)
.map(fetch_rel::CountMode::CountExpr);
Ok(Box::new(Rel {
rel_type: Some(RelType::Fetch(Box::new(FetchRel {
common: None,
input: Some(sort_rel),
offset_mode: None,
count_mode,
advanced_extension: None,
}))),
}))
}
None => Ok(sort_rel),
}
}
LogicalPlan::Aggregate(agg) => {
let input = to_substrait_rel(agg.input.as_ref(), state, extensions)?;
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 @@ -200,6 +200,11 @@ async fn select_with_filter() -> Result<()> {
roundtrip("SELECT * FROM data WHERE a > 1").await
}

#[tokio::test]
async fn select_with_filter_sort_limit() -> Result<()> {
roundtrip("SELECT * FROM data WHERE a > 1 ORDER BY b ASC LIMIT 2").await
alamb marked this conversation as resolved.
Show resolved Hide resolved
}

#[tokio::test]
async fn select_with_reused_functions() -> Result<()> {
let ctx = create_context().await?;
Expand Down
Loading