Skip to content

Remove Sort expression (Expr::Sort) - #12177

Merged
crepererum merged 11 commits into
apache:mainfrom
findepi:findepi/sort-is-not-expression
Aug 29, 2024
Merged

Remove Sort expression (Expr::Sort)#12177
crepererum merged 11 commits into
apache:mainfrom
findepi:findepi/sort-is-not-expression

Conversation

@findepi

@findepifindepi commented Aug 26, 2024

Copy link
Copy Markdown
Member

Remove sort as an expression, i.e. remove Expr::Sort from Expr enum.
Use expr::Sort directly when sorting.

The sort expression was used in context of ordering (sort, topk, create
table, file sorting). Those places require their sort expression to be
of type Sort anyway and no other expression was allowed, so this change
improves static typing. Sort as an expression was illegal in other
contexts.

For #1468 (comment)
Fixes#12193

@findepi
findepi marked this pull request as draft August 26, 2024 15:27
@github-actionsgithub-actionsBot added sql SQL Planner logical-expr Logical plan and expressions optimizer Optimizer rules core Core DataFusion crate proto Related to proto crate functions Changes to functions implementation labels Aug 26, 2024
@findepi

Copy link
Copy Markdown
MemberAuthor

Not ready for review. Just for FYI.

Comment threaddatafusion/sql/src/unparser/rewrite.rs Outdated
Comment threaddatafusion/core/src/datasource/listing/table.rs
Comment threaddatafusion/core/tests/expr_api/mod.rs
Comment threaddatafusion/optimizer/src/eliminate_limit.rs
@findepi
findepiforce-pushed the findepi/sort-is-not-expression branch from c29d147 to cdea34eCompareAugust 27, 2024 08:50
@github-actionsgithub-actionsBot added the documentation Improvements or additions to documentation label Aug 27, 2024
@findepifindepi changed the title Remove Expr::SortRemove Sort expression (Expr::Sort)Aug 27, 2024
@findepi

Copy link
Copy Markdown
MemberAuthor

@findepifindepi mentioned this pull request Aug 27, 2024
@findepi
findepiforce-pushed the findepi/sort-is-not-expression branch from cdea34e to 1c3f194CompareAugust 27, 2024 13:28
@findepi
findepi marked this pull request as ready for review August 27, 2024 13:31
@findepi

Copy link
Copy Markdown
MemberAuthor

Rebased, should be ready for initial review pass.

I split the change into couple commits, but bulk of it is still one big commit.
Should i continue along this pattern?
Should i extract some commits into own PRs
let me know what's best from review perspective.

cc @alamb@comphead@jonahgao@jayzhan211@andygrove@crepererum

@crepererumcrepererum left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking at all the invalid states (panics and "unimplemented" errors) that are removed in this PR, this is a clear improvement. I also agree with the overall premise that "sorts" are not really a subtype/variant of Expr.

This should probably get a 2nd ACK by another maintainer.

@findepi

Copy link
Copy Markdown
MemberAuthor

there is a new conflict (perhaps due to #12196), let me rebase.

Part of effort to remove `Expr::Sort`.
Part of effort to remove `Expr::Sort`.
Take `expr::Sort` in `LogicalPlanBuilder.sort`.
Accept any `Expr` in new function, `LogicalPlanBuilder.sort_by` which
apply default sort ordering.
Part of effort to remove `Expr::Sort`.
Part of effort to remove `Expr::Sort`.
Remove sort as an expression, i.e. remove `Expr::Sort` from `Expr` enum.
Use `expr::Sort` directly when sorting.
The sort expression was used in context of ordering (sort, topk, create
table, file sorting). Those places require their sort expression to be
of type Sort anyway and no other expression was allowed, so this change
improves static typing. Sort as an expression was illegal in other
contexts.
@findepi
findepiforce-pushed the findepi/sort-is-not-expression branch from 1c3f194 to 9840b8fCompareAugust 27, 2024 20:11
}))
}

pub fn replace_sort_expressions(sorts: Vec<Sort>, new_expr: Vec<Expr>) -> Vec<Sort> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe returns with Result<Vec<Sort>>?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Good question. This is used in two places. In one we know the list of expressions is exact size.
the other is LogicalPlan.with_new_exprs which does assert! on provided new expr length. so it looks we don't need to be permissive here?

Comment threaddatafusion/expr/src/tree_node.rs Outdated
Comment threaddatafusion/optimizer/src/eliminate_duplicated_expr.rs Outdated
.build()?;
// Do nothing
let expected = "Aggregate: groupBy=[[test.c]], aggr=[[sum(test.a) ORDER BY [test.a], count(DISTINCT test.b)]] [c:UInt32, sum(test.a) ORDER BY [test.a]:UInt64;N, count(DISTINCT test.b):Int64]\
let expected = "Aggregate: groupBy=[[test.c]], aggr=[[sum(test.a) ORDER BY [test.a ASC NULLS LAST], count(DISTINCT test.b)]] [c:UInt32, sum(test.a) ORDER BY [test.a ASC NULLS LAST]:UInt64;N, count(DISTINCT test.b):Int64]\

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this the same as previous?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

the default ASC/DESC NULLS FIRST/LAST behavior seems to come from

let asc = asc.unwrap_or(true);
expr_vec.push(Expr::Sort(Sort::new(
Box::new(expr),
asc,
// when asc is true, by default nulls last to be consistent with postgres
// postgres rule: https://www.postgresql.org/docs/current/queries-order.html
nulls_first.unwrap_or(!asc),

if i read this correctly, ASC NULLS LAST is the default

@findepifindepi mentioned this pull request Aug 28, 2024
@findepi
findepiforce-pushed the findepi/sort-is-not-expression branch from 2f0ea05 to 39877e4CompareAugust 28, 2024 07:26
@findepi

Copy link
Copy Markdown
MemberAuthor

thank you @jayzhan211 for your review! would you mind taking another look?

this commit is longer than advised in the review comment, but after
squashing the diff will be smaller

@jayzhan211jayzhan211 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍

@alamb

Copy link
Copy Markdown
Contributor

EPIC!

@alambalamb added the api change Changes the API exposed to users of the crate label Aug 28, 2024
@findepi

Copy link
Copy Markdown
MemberAuthor

thank you @jayzhan211@crepererum@alamb for all your time spent reviewing this!

@crepererum
crepererum merged commit 85adb6c into apache:mainAug 29, 2024
@findepi
findepi deleted the findepi/sort-is-not-expression branch August 29, 2024 15:46
@findepi

Copy link
Copy Markdown
MemberAuthor

❤️ thank you for the (brave) merge, @crepererum !

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api changeChanges the API exposed to users of the cratecoreCore DataFusion cratedocumentationImprovements or additions to documentationfunctionsChanges to functions implementationlogical-exprLogical plan and expressionsoptimizerOptimizer rulesprotoRelated to proto cratesqlSQL PlannersubstraitChanges to the substrait crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove Sort from Expr

4 participants

@findepi@alamb@crepererum@jayzhan211