Skip to content

Conversation

@berkaysynnada
Copy link
Contributor

Which issue does this PR close?

Closes #6272 .

Rationale for this change

Union get meet can only consider sort option and corresponding vector index during comparison. No need to check for PhysicalExpr equality.

What changes are included in this PR?

If the PhysicalSortExpr's can be downcasted to Column, compare the Column's indexes and sort options. Otherwise, the existing comparison is sustained.

Are these changes tested?

Existing tests are sufficient.

Are there any user-facing changes?

@github-actions github-actions bot added the core Core DataFusion crate label May 7, 2023
@mingmwang
Copy link
Contributor

Nice optimization

@mingmwang
Copy link
Contributor

@berkaysynnada

Can you extend the optimization to non-column cases?

For example

select a + b from table_2 order by a + b
union all
select x + y from table_2  order by x + y

@berkaysynnada
Copy link
Contributor Author

berkaysynnada commented May 8, 2023

@berkaysynnada

Can you extend the optimization to non-column cases?

For example

select a + b from table_2 order by a + b
union all
select x + y from table_2  order by x + y

My last commit can succeed in the cases of your example. However, for the cases like a+b+c (having two or more depth), the build order of the binary expression is important. We can get false negative results. Actually, I think this is a general problem with binary expressions in Datafusion.

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Is there and SQL / plan level test that could be written that would cover this code? It seems like a neat optimization but I don't fully understand how to map it to how it would affect queries

@github-actions github-actions bot added the sqllogictest SQL Logic Tests (.slt) label May 9, 2023
@berkaysynnada
Copy link
Contributor Author

berkaysynnada commented May 9, 2023

Is there and SQL / plan level test that could be written that would cover this code? It seems like a neat optimization but I don't fully understand how to map it to how it would affect queries

c1 and c1a columns are provided as ascending, and this PR optimizes the plan such:

query TT
explain
SELECT c1 FROM(
(   
    SELECT c1 FROM t1
)  
UNION ALL
(   
    SELECT c1a FROM t2
))
ORDER BY c1
----
logical_plan
  Sort: t1.c1 ASC NULLS LAST
    Union
      TableScan: t1 projection=[c1]
      Projection: t2.c1a AS t1.c1
        TableScan: t2 projection=[c1a]
physical_plan
  SortPreservingMergeExec: [c1@0 ASC NULLS LAST]
    UnionExec
      CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c1], output_ordering=[c1@0 ASC NULLS LAST], has_header=true
        ProjectionExec: expr=[c1a@0 as t1.c1]
      CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c1a], output_ordering=[c1a@0 ASC NULLS LAST], has_header=true

However, the main version's result is:

logical_plan
  Sort: t1.c1 ASC NULLS LAST
    Union
      TableScan: t1 projection=[c1]
      Projection: t2.c1a AS t1.c1
        TableScan: t2 projection=[c1a]
physical_plan
  SortPreservingMergeExec: [c1@0 ASC NULLS LAST]
    UnionExec
      CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c1], output_ordering=[c1@0 ASC NULLS LAST], has_header=true
      SortExec: expr=[c1@0 ASC NULLS LAST]
        ProjectionExec: expr=[c1a@0 as t1.c1]
          CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c1a], output_ordering=[c1a@0 ASC NULLS LAST], has_header=true

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

This looks like a good improvement to me. Thank you @berkaysynnada

@ozankabak and/ or @mustafasrepo do you have time to review this PR (as I think you authored the code originally and most recently modified it): https://github.com/apache/arrow-datafusion/blame/894beb1d3b2b42ecc58c582f98e17800ac2b9527/datafusion/core/src/physical_plan/common.rs#L333

Copy link
Contributor

@ozankabak ozankabak left a comment

Choose a reason for hiding this comment

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

I went over the code and checked in a commit simplifying it a little bit.

It looks good to me at this point, feel free to merge when you like.

@alamb
Copy link
Contributor

alamb commented May 9, 2023

Thanks @berkaysynnada and @ozankabak

@alamb
Copy link
Contributor

alamb commented May 9, 2023

For some reason github claims this PR has conflicts that must be resolved 🤔 since I can't push changes to your branch to resolve this myself, @ozankabak or @berkaysynnada can you please resolve the problem? I will then merge it

Screenshot 2023-05-09 at 7 45 10 PM

@ozankabak
Copy link
Contributor

Done, thank you

Copy link
Contributor

@mingmwang mingmwang left a comment

Choose a reason for hiding this comment

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

LGTM

@alamb alamb merged commit b1af8de into apache:main May 10, 2023
@berkaysynnada berkaysynnada deleted the feature/union-get-meet branch May 10, 2023 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove the PhysicalSortExpr restriction on union get meet

5 participants