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

fix(substrait): Do not add implicit groupBy expressions when building logical plans from Substrait #14860

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

anlinc
Copy link

@anlinc anlinc commented Feb 24, 2025

Which issue does this PR close?

Closes #14348

Rationale for this change

Substrait plans are intended to be interpreted literally. When you see plan nodes like:

"project": {
  "common": {
    "emit": {
      "outputMapping": [0, 3]
    }
  },
...
}

The output mapping (e.g. [0, 3]) contains ordinals representing the offset of the target expression(s) within the [input, output] list. If the DataFusion LogicalPlanBuilder is introducing additional input expressions, this violates the plan's intent and will produce the incorrect output mappings. Please see the issue for a concrete example.

What changes are included in this PR?

  • Introduce new add_implicit_group_by_exprs option to the logical plan builder. It is disabled by default.
    • The option is enabled only in the DataFrame and SQL paths.

Are these changes tested?

Added a multilayer aggregation Substrait example. The first aggregation produces a unique column with a functional dependency. Despite this, the second aggregation must not introduce any additional grouping expressions.

There should be no changes in the non-Substrait paths.

Are there any user-facing changes?

No.

Appendix

This is a continuation of #14553.

@github-actions github-actions bot added sql SQL Planner logical-expr Logical plan and expressions core Core DataFusion crate substrait labels Feb 24, 2025
@anlinc anlinc marked this pull request as ready for review February 24, 2025 21:14
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.

Thank you @anlinc

I think this looks great to me

cc @Blizzara @vbarua

@alamb
Copy link
Contributor

alamb commented Feb 24, 2025

Hi @anlinc

It looks like there is a small clippy failure with this PR:

https://github.com/apache/datafusion/actions/runs/13507870042/job/37742937287?pr=14860

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate logical-expr Logical plan and expressions sql SQL Planner substrait
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[substrait] Synthetically added grouping expressions in Aggregates can cause mismatched output columns
2 participants