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

Add support for maintain_order param in joins #17698

Open
wants to merge 28 commits into
base: branch-25.02
Choose a base branch
from

Conversation

Matt711
Copy link
Contributor

@Matt711 Matt711 commented Jan 8, 2025

Description

Closes #17696

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@Matt711 Matt711 added feature request New feature or request non-breaking Non-breaking change labels Jan 8, 2025
Copy link

copy-pr-bot bot commented Jan 8, 2025

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@github-actions github-actions bot added Python Affects Python cuDF API. cudf.polars Issues specific to cudf.polars labels Jan 8, 2025
@Matt711 Matt711 marked this pull request as ready for review January 9, 2025 15:39
@Matt711 Matt711 requested a review from a team as a code owner January 9, 2025 15:39
@Matt711 Matt711 requested a review from wence- January 9, 2025 15:39
Copy link
Contributor

@wence- wence- left a comment

Choose a reason for hiding this comment

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

Thanks @Matt711, I think we are doing too much work in the "none" case.

python/cudf_polars/cudf_polars/dsl/ir.py Outdated Show resolved Hide resolved
python/cudf_polars/cudf_polars/dsl/ir.py Outdated Show resolved Hide resolved
python/cudf_polars/cudf_polars/dsl/ir.py Outdated Show resolved Hide resolved
python/cudf_polars/cudf_polars/dsl/ir.py Show resolved Hide resolved
)
if maintain_order == "left":
sort_keys = left_order.columns()
elif maintain_order == "right":
Copy link
Contributor Author

@Matt711 Matt711 Jan 9, 2025

Choose a reason for hiding this comment

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

For the reviewer: This PR needs more work, but I'm opening it up for review so I can get some help handling a special case: full joins (where we maintain the order of the right table). Specifically, the case where the test fails is when there are unmatched keys in the left dataframe. Any advice on how to handle this?

Example:

left = pl.LazyFrame(
    {
        "a": [1, 2, 3, 1, None],
        "b": [1, 2, 3, 4, 5],
        "c": [2, 3, 4, 5, 6],
    }
)
right = pl.LazyFrame(
    {
        "a": [1, 4, 3, 7, None, None, 1],
        "c": [2, 3, 4, 5, 6, 7, 8],
        "d": [6, None, 7, 8, -1, 2, 4],
    }
)
q = left.join(right, on=pl.col("a"), how="full", maintain_order="right")
q.collect(engine="gpu")

The dataframe differ at column "a"

AssertionError: DataFrames are different (value mismatch for column 'a')
[left]:  [1, 1, None, 3, None, None, None, 1, 1, **None, 2]**
[right]: [1, 1, None, 3, None, None, None, 1, 1, **2, None]**

The a=2 entry is unmatched in the right dataframe, so it should be appended to the end of the result, not included with the other matches.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is expected. Because those last two rows have the same sort key in the right table column, there's no disambiguator to decide which order the left column result comes in.

e.g.

(No GPU engine involved):

In [18]: q = left.join(right, on=pl.col("a"), how="full", maintain_order="right")

In [19]: q.collect(engine="cpu")
Out[19]: 
shape: (11, 2)
┌──────┬─────────┐
│ a    ┆ a_right │
│ ---  ┆ ---     │
│ i64  ┆ i64     │
╞══════╪═════════╡
│ 1    ┆ 1       │
│ 1    ┆ 1       │
│ null ┆ 4       │
│ 3    ┆ 3       │
│ null ┆ 7       │
│ …    ┆ …       │
│ null ┆ null    │
│ 1    ┆ 1       │
│ 1    ┆ 1       │
│ null ┆ null    │
│ 2    ┆ null    │
└──────┴─────────┘

In [20]: q.collect(engine="cpu")
Out[20]: 
shape: (11, 2)
┌──────┬─────────┐
│ a    ┆ a_right │
│ ---  ┆ ---     │
│ i64  ┆ i64     │
╞══════╪═════════╡
│ 1    ┆ 1       │
│ 1    ┆ 1       │
│ null ┆ 4       │
│ 3    ┆ 3       │
│ null ┆ 7       │
│ …    ┆ …       │
│ null ┆ null    │
│ 1    ┆ 1       │
│ 1    ┆ 1       │
│ 2    ┆ null    │
│ null ┆ null    │
└──────┴─────────┘

Notice how the last two rows flip around between the two runs.

Comment on lines +184 to +189
"tests/unit/operations/test_join.py::test_update": 'LazyFrame.update doesn\'t maintain order of either table (ie. maintain_order="none")',
"tests/unit/operations/test_join.py::test_join_preserve_order_left": 'The test includes a join w/ maintain_order="none"',
"tests/unit/streaming/test_streaming.py::test_streaming_generic_left_and_inner_join_from_disk": 'The test includes a join w/ maintain_order="none"',
"tests/unit/streaming/test_streaming_join.py::test_join_null_matches[False]": 'The test includes a join w/ maintain_order="none"',
"tests/unit/streaming/test_streaming_join.py::test_join_null_matches_multiple_keys[False]": 'The test includes a join w/ maintain_order="none"',
"tests/unit/test_cse.py::test_cse_rename_cross_join_5405": 'The test includes a join w/ maintain_order="none"',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I open an issue in polars to add maintain_order="left_right" or some other value than none? That way we wouldn't have to xfail some of these tests. WDYT?
cc. @wence-

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh maybe, yes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tested this PR with Polars 1.19 and these tests now pass

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the problem is related to left joins preserving the order of the left table by default. There are two options I see

  1. If I'm right about left joins, we can try handling that case in this PR (ie. how="left" and maintain_order=None => preserve the left table order)
  2. Keep the PR as is and do a polars version bump
    Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's bump the version I think. I would like to be aligned with the most recent polars release before we go into burndown anyway

@Matt711 Matt711 requested a review from wence- January 17, 2025 18:21
@vyasr
Copy link
Contributor

vyasr commented Jan 25, 2025

Is this PR gated on #17771 going in? If there are Polars version-specific behaviors that we care about?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cudf.polars Issues specific to cudf.polars feature request New feature or request non-breaking Non-breaking change Python Affects Python cuDF API.
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

[FEA] Add support for maintain_order param in joins
3 participants