- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.7k
          Support convert_to_state for AVG accumulator
          #11734
        
          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
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      f3bedc0
              
                Support `convert_to_state` for `AVG` accumulator
              
              
                alamb 92decac
              
                Update datafusion/physical-expr-common/src/aggregate/groups_accumulat…
              
              
                alamb 149406b
              
                fix documentation
              
              
                alamb e725150
              
                Merge remote-tracking branch 'apache/main' into alamb/support_avg
              
              
                alamb 6186171
              
                Fix after merge
              
              
                alamb ba3f00c
              
                Merge remote-tracking branch 'apache/main' into alamb/support_avg
              
              
                alamb 8d4ea5b
              
                fix for change in location
              
              
                alamb File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -20,6 +20,7 @@ | |
|  | ||
| pub mod accumulate; | ||
| pub mod bool_op; | ||
| pub mod nulls; | ||
| pub mod prim_op; | ||
|  | ||
| use arrow::{ | ||
|  | ||
        
          
          
            93 changes: 93 additions & 0 deletions
          
          93 
        
  datafusion/functions-aggregate-common/src/aggregate/groups_accumulator/nulls.rs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|  | ||
| //! [`set_nulls`], and [`filtered_null_mask`], utilities for working with nulls | ||
|  | ||
| use arrow::array::{Array, ArrowNumericType, BooleanArray, PrimitiveArray}; | ||
| use arrow::buffer::NullBuffer; | ||
|  | ||
| /// Sets the validity mask for a `PrimitiveArray` to `nulls` | ||
| /// replacing any existing null mask | ||
| pub fn set_nulls<T: ArrowNumericType + Send>( | ||
| array: PrimitiveArray<T>, | ||
| nulls: Option<NullBuffer>, | ||
| ) -> PrimitiveArray<T> { | ||
| let (dt, values, _old_nulls) = array.into_parts(); | ||
| PrimitiveArray::<T>::new(values, nulls).with_data_type(dt) | ||
| } | ||
|  | ||
| /// Converts a `BooleanBuffer` representing a filter to a `NullBuffer. | ||
| /// | ||
| /// The `NullBuffer` is | ||
| /// * `true` (representing valid) for values that were `true` in filter | ||
| /// * `false` (representing null) for values that were `false` or `null` in filter | ||
| fn filter_to_nulls(filter: &BooleanArray) -> Option<NullBuffer> { | ||
| let (filter_bools, filter_nulls) = filter.clone().into_parts(); | ||
| let filter_bools = NullBuffer::from(filter_bools); | ||
| NullBuffer::union(Some(&filter_bools), filter_nulls.as_ref()) | ||
| } | ||
|  | ||
| /// Compute an output validity mask for an array that has been filtered | ||
| /// | ||
| /// This can be used to compute nulls for the output of | ||
| /// [`GroupsAccumulator::convert_to_state`], which quickly applies an optional | ||
| /// filter to the input rows by setting any filtered rows to NULL in the output. | ||
| /// Subsequent applications of aggregate functions that ignore NULLs (most of | ||
| /// them) will thus ignore the filtered rows as well. | ||
| /// | ||
| /// # Output element is `true` (and thus output is non-null) | ||
| /// | ||
| /// A `true` in the output represents non null output for all values that were *both*: | ||
| /// | ||
| /// * `true` in any `opt_filter` (aka values that passed the filter) | ||
| /// | ||
| /// * `non null` in `input` | ||
| /// | ||
| /// # Output element is `false` (and thus output is null) | ||
| /// | ||
| /// A `false` in the output represents an input that was *either*: | ||
| /// | ||
| /// * `null` | ||
| /// | ||
| /// * filtered (aka the value was `false` or `null` in the filter) | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ```text | ||
| /// ┌─────┐ ┌─────┐ ┌─────┐ | ||
| /// │true │ │NULL │ │false│ | ||
| /// │true │ │ │true │ │true │ | ||
| /// │true │ ───┼─── │false│ ────────▶ │false│ filtered_nulls | ||
| /// │false│ │ │NULL │ │false│ | ||
| /// │false│ │true │ │false│ | ||
| /// └─────┘ └─────┘ └─────┘ | ||
| /// array opt_filter output | ||
| /// .nulls() | ||
| /// | ||
| /// false = NULL true = pass false = NULL Meanings | ||
| /// true = valid false = filter true = valid | ||
| /// NULL = filter | ||
| /// ``` | ||
| /// | ||
| /// [`GroupsAccumulator::convert_to_state`]: datafusion_expr_common::groups_accumulator::GroupsAccumulator | ||
| pub fn filtered_null_mask( | ||
| opt_filter: Option<&BooleanArray>, | ||
| input: &dyn Array, | ||
| ) -> Option<NullBuffer> { | ||
| let opt_filter = opt_filter.and_then(filter_to_nulls); | ||
| NullBuffer::union(opt_filter.as_ref(), input.nulls()) | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just wondering why the test is only for tinyint / floats?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea was that
AVGthe accumulator is already tested elsewhere -- this test is only to exercise the partial aggregate skipping logic