You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "Rich-T-kid (via GitHub)" <gi...@apache.org> on 2026/06/30 18:02:03 UTC

[PR] bug: clear map contents after emit in GroupValuesColumns [datafusion]

Rich-T-kid opened a new pull request, #23262:
URL: https://github.com/apache/datafusion/pull/23262

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   - works towards  #22715
   
   ## Rationale for this change
   I was implementing dictionary support for this trait when I noticed that the `emit(emit_to::All)` path doesn't update the contents of the the hashtable/map
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   ## What changes are included in this PR?
   This PR adds test to catch future regressions as well as a simple `self.map.clear()` to clear the contents of the map on emit
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   ## Are these changes tested?
   yes, this PR adds two test
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   ## Are there any user-facing changes?
   no
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] bug: clear map contents after emit in GroupValuesColumns [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb closed pull request #23262: bug: clear map contents after emit in GroupValuesColumns
URL: https://github.com/apache/datafusion/pull/23262


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] bug: clear map contents after emit in GroupValuesColumns [datafusion]

Posted by "Rich-T-kid (via GitHub)" <gi...@apache.org>.
Rich-T-kid commented on code in PR #23262:
URL: https://github.com/apache/datafusion/pull/23262#discussion_r3508191354


##########
datafusion/physical-plan/src/aggregates/group_values/multi_group_by/mod.rs:
##########
@@ -1117,7 +1117,7 @@ impl<const STREAMING: bool> GroupValues for GroupValuesColumn<STREAMING> {
                 // a real Result rather than panicking.
                 let fresh = Self::build_group_columns(&self.schema)?;
                 let group_values = mem::replace(&mut self.group_values, fresh);
-
+                self.map.clear();

Review Comment:
   `EmitTo::All` gets called in two cases
   1. memory is filling up, leading to a spill. `EmitTo::All` is called [here](https://github.com/apache/datafusion/blob/9e8dd76d6deb6736c51962d9c97e04be4e3f1fc9/datafusion/physical-plan/src/aggregates/row_hash.rs#L1173) and then its written to disk. after `EmitTo::All` is called `GroupValues::clear_shrink()` gets [called](https://github.com/apache/datafusion/blob/9e8dd76d6deb6736c51962d9c97e04be4e3f1fc9/datafusion/physical-plan/src/aggregates/row_hash.rs#L1182) and thats when the map content gets cleared. iirc `EmitTo::All` can be called multiple times for spills. This wont matter as each call is paired with `GroupValues::clear_shrink()`
   
   2. all input has been seen, begin producing output. `EmitTo::all` is called [here](https://github.com/apache/datafusion/blob/9e8dd76d6deb6736c51962d9c97e04be4e3f1fc9/datafusion/physical-plan/src/aggregates/row_hash.rs#L1276) is called to produced all of the arrays and then gets repeatedly sliced. After this the `exec_state` is set to [producing output](https://github.com/apache/datafusion/blob/9e8dd76d6deb6736c51962d9c97e04be4e3f1fc9/datafusion/physical-plan/src/aggregates/row_hash.rs#L1279) so intern is never called again.
   
   Currently in both cases due to the ordering of surrounding code the state of `groupValuesColumns` is logically correct. 
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] bug: clear map contents after emit in GroupValuesColumns [datafusion]

Posted by "Rich-T-kid (via GitHub)" <gi...@apache.org>.
Rich-T-kid commented on code in PR #23262:
URL: https://github.com/apache/datafusion/pull/23262#discussion_r3508191354


##########
datafusion/physical-plan/src/aggregates/group_values/multi_group_by/mod.rs:
##########
@@ -1117,7 +1117,7 @@ impl<const STREAMING: bool> GroupValues for GroupValuesColumn<STREAMING> {
                 // a real Result rather than panicking.
                 let fresh = Self::build_group_columns(&self.schema)?;
                 let group_values = mem::replace(&mut self.group_values, fresh);
-
+                self.map.clear();

Review Comment:
   `EmitTo::All` gets called in two cases
   1. memory is filling up, leading to a spill. `EmitTo::All` is called [here](https://github.com/apache/datafusion/blob/9e8dd76d6deb6736c51962d9c97e04be4e3f1fc9/datafusion/physical-plan/src/aggregates/row_hash.rs#L1173) and then its written to disk. after `EmitTo::All` is called `GroupValues::clear_shrink()` gets [called](https://github.com/apache/datafusion/blob/9e8dd76d6deb6736c51962d9c97e04be4e3f1fc9/datafusion/physical-plan/src/aggregates/row_hash.rs#L1182) and thats when the map content gets cleared. iirc `EmitTo::All` can be called multiple times for spills. This wont matter as each call is paired with `GroupValues::clear_shrink()`
   
   2. all input has been seen, begin producing output. `EmitTo::all` is called [here](https://github.com/apache/datafusion/blob/9e8dd76d6deb6736c51962d9c97e04be4e3f1fc9/datafusion/physical-plan/src/aggregates/row_hash.rs#L1276) is called to produced all of the arrays and then gets repeatedly sliced. After this the `exec_state` is set to [producing output](https://github.com/apache/datafusion/blob/9e8dd76d6deb6736c51962d9c97e04be4e3f1fc9/datafusion/physical-plan/src/aggregates/row_hash.rs#L1279) so intern is never called again.
   
   Currently in both cases due to the ordering of surrounding code the state of `groupValuesColumns` is logically correct. 
   
   🤔 It may not be worth adding this `map.clear()` in this case. My thought was to try to prevent possible invariant violations, but if `clear_shrink(0)` isn't called, or if more input comes in after the state changes to `producing_output`, there may be larger logical inconsistencies.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] bug: clear map contents after emit in GroupValuesColumns [datafusion]

Posted by "Rich-T-kid (via GitHub)" <gi...@apache.org>.
Rich-T-kid commented on PR #23262:
URL: https://github.com/apache/datafusion/pull/23262#issuecomment-4929569869

   should I close this? @alamb 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] bug: clear map contents after emit in GroupValuesColumns [datafusion]

Posted by "Rich-T-kid (via GitHub)" <gi...@apache.org>.
Rich-T-kid commented on code in PR #23262:
URL: https://github.com/apache/datafusion/pull/23262#discussion_r3501179461


##########
datafusion/physical-plan/src/aggregates/group_values/multi_group_by/mod.rs:
##########
@@ -1117,7 +1117,7 @@ impl<const STREAMING: bool> GroupValues for GroupValuesColumn<STREAMING> {
                 // a real Result rather than panicking.
                 let fresh = Self::build_group_columns(&self.schema)?;
                 let group_values = mem::replace(&mut self.group_values, fresh);
-
+                self.map.clear();

Review Comment:
   Not exactly,the [map's capacity](https://docs.rs/hashbrown/latest/hashbrown/struct.HashMap.html#method.clear) is preserved. This gives the caller a correctness guarantee: emit will never leave GroupValuesColumn in an intermediate or inconsistent state.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] bug: clear map contents after emit in GroupValuesColumns [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #23262:
URL: https://github.com/apache/datafusion/pull/23262#issuecomment-4929768932

   > should I close this? @alamb
   
   Yeah, I think that would be best @Rich-T-kid  --  I think it makes the code more complicated and harder to understad (we are also in the midst of refactoring, so this change would likely get lost anways)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] bug: clear map contents after emit in GroupValuesColumns [datafusion]

Posted by "Rich-T-kid (via GitHub)" <gi...@apache.org>.
Rich-T-kid commented on PR #23262:
URL: https://github.com/apache/datafusion/pull/23262#issuecomment-4846623950

   From my understanding [spilling causes](https://vscode.dev/github/Rich-T-kid/datafusion/blob/rich-T-kid/fix-map-clear/datafusion/physical-plan/src/aggregates/row_hash.rs#L962) `emitTo::All` followed by `clear_shrink()`. But I think its worth making this explicit, to make `emit`  self-consistent.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] bug: clear map contents after emit in GroupValuesColumns [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #23262:
URL: https://github.com/apache/datafusion/pull/23262#discussion_r3501795120


##########
datafusion/physical-plan/src/aggregates/group_values/multi_group_by/mod.rs:
##########
@@ -1117,7 +1117,7 @@ impl<const STREAMING: bool> GroupValues for GroupValuesColumn<STREAMING> {
                 // a real Result rather than panicking.
                 let fresh = Self::build_group_columns(&self.schema)?;
                 let group_values = mem::replace(&mut self.group_values, fresh);
-
+                self.map.clear();

Review Comment:
   Can emit ever be called more than once with `EmitTo::All` ? That seems like it would be a bug right there



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] bug: clear map contents after emit in GroupValuesColumns [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #23262:
URL: https://github.com/apache/datafusion/pull/23262#discussion_r3501060070


##########
datafusion/physical-plan/src/aggregates/group_values/multi_group_by/mod.rs:
##########
@@ -1117,7 +1117,7 @@ impl<const STREAMING: bool> GroupValues for GroupValuesColumn<STREAMING> {
                 // a real Result rather than panicking.
                 let fresh = Self::build_group_columns(&self.schema)?;
                 let group_values = mem::replace(&mut self.group_values, fresh);
-
+                self.map.clear();

Review Comment:
   Can you please add a comment here?
   
   What is the end user  implication of this change? less peak memory usage?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org