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: Remove memory reservation in JoinLeftData used in HashJoin #13751

Merged
merged 2 commits into from
Dec 13, 2024

Conversation

jayzhan-synnada
Copy link
Contributor

Which issue does this PR close?

Closes #.

Rationale for this change

memory reservation is not used outside of collect_left_input, I don't think there is any good reason to add it in JoinLeftData. Remove this so we don't need to create dummy value for reservation when we create JoinLeftData

What changes are included in this PR?

Are these changes tested?

Also, Debug and Clone for HashJoinStreamState

Are there any user-facing changes?

…atchState enums

This commit enhances the HashJoinStreamState and ProcessProbeBatchState structures by implementing the Debug and Clone traits, allowing for easier debugging and cloning of these state representations in the hash join implementation.
@comphead
Copy link
Contributor

Good catch. Some day ago it was intention to add spilling to HJ #12952
but even for that case the mem reservation on left side is not needed

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.

LGTM

@berkaysynnada berkaysynnada merged commit c030bfe into apache:main Dec 13, 2024
25 checks passed
@@ -90,9 +90,6 @@ struct JoinLeftData {
/// Counter of running probe-threads, potentially
/// able to update `visited_indices_bitmap`
probe_threads_counter: AtomicUsize,
/// Memory reservation that tracks memory used by `hash_map` hash table
/// `batch`. Cleared on drop.
_reservation: MemoryReservation,
Copy link
Contributor

@korowa korowa Dec 14, 2024

Choose a reason for hiding this comment

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

In case reservation is not stored in JoinLeftData, it'll be dropped right after collect_left_input due to being unneeded anymore. And due to MemoryReservation calls free on its drop, it looks like that with this patch, during the whole join execution build side data will be untracked by memory pool.

So the initial idea of this attribute was to make reservation to live as long as JoinLeftData exists.

Copy link
Contributor

Choose a reason for hiding this comment

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

UPD: some "logs" (additional prints 😞 ) just to check join execution before/after this patch:

// Before
---- joins::hash_join::tests::join_inner_one_no_shared_column_names stdout ----
Waiting build side
Reserving 324 bytes
Reserving 124 bytes
Fetching probe batch
Processing probe batch
Fetching probe batch
Processing unmatched build side
Freeing 448 bytes

// After
---- joins::hash_join::tests::join_inner_one_no_shared_column_names stdout ----
Waiting build side
Reserving 324 bytes
Reserving 124 bytes
Freeing 448 bytes
Fetching probe batch
Processing probe batch
Fetching probe batch
Processing unmatched build side

so yes, the memory is "freed" before join completes its execution, which doesn't seem to be an expected behavior.

Copy link
Contributor

@jayzhan211 jayzhan211 Dec 14, 2024

Choose a reason for hiding this comment

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

The reason I think this is not an issue is because it only matters when we run 'collect_left_join', we track the memory and free after the build side is done. Although, the memory is clean while the hash map is not freed. BUT, since we don't have expected memory change in the probe stage, the memory is not helpful for the probe stage. That's why I think this will not cause any issues if we drop the reservation early.

Copy link
Contributor

Choose a reason for hiding this comment

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

Although, the memory is clean while the hash map is not freed

That actually is the issue, because there is single memory pool for a RuntimeEnv, and it's intended to be used by multiple sessions or at least by the multiple operators within the same query. Even in case of single query like select * from a join b order by a.field untracked build side may cause OOM due to SortExec will consider MemoryPool as being empty, while there is a build side data in memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
physical-expr Physical Expressions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants