mustafasrepo opened a new pull request, #9597:
URL: https://github.com/apache/arrow-datafusion/pull/9597

   ## 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.
   -->
   
   Closes #.
   
   ## Rationale for this change
   Background:  In `Linear` mode of the `BoundedWindowAggExec` none of the 
partition by expressions are ordered. In these cases, we can generate result 
for a partition as long as a new row with same partition is received. 
Otherwise, result cannot be generated for the partition. As an example consider 
the table, 
   
   sn | hash
   -- | --
   0 | 2
   1 | 2
   2 | 2
   3 | 2
   4 | 1
   5 | 1
   6 | 1
   7 | 1
   8 | 0
   9 | 0
   
   Assume following query is executed on this table
   ```  
   SELECT *, COUNT(*) OVER(PARTITION BY hash ORDER BY sn RANGE BETWEEN CURRENT 
ROW AND 1 FOLLOWING) as count
   FROM test;
   ```
   datafusion will generate following plan
   ```  
   "ProjectionExec: expr=[sn@0 as sn,  hash@1 as hash, COUNT([Column { name: 
\"sn\", index: 0 }]) PARTITION BY: [[Column { name: \"hash\", index: 1 }]], 
ORDER BY: [[PhysicalSortExpr { expr: Column { name: \"hash\", index: 1 }, 
options: SortOptions { descending: false, nulls_first: true } }]]@2 as count]",
   "  BoundedWindowAggExec: wdw=[COUNT([Column { name: \"sn\", index: 0 }]) 
PARTITION BY: [[Column { name: \"hash\", index: 1 }]], ORDER BY: 
[[PhysicalSortExpr { expr: Column { name: \"duplicated_hash\", index: 3 }, 
options: SortOptions { descending: false, nulls_first: true } }]]: Ok(Field { 
name: \"COUNT([Column { name: \\\"sn\\\", index: 0 }]) PARTITION BY: [[Column { 
name: \\\"duplicated_hash\\\", index: 1 }]], ORDER BY: [[PhysicalSortExpr { 
expr: Column { name: \\\"hash\\\", index: 1 }, options: SortOptions { 
descending: false, nulls_first: true } }]]\", data_type: Int64, nullable: true, 
dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { 
units: Range, start_bound: CurrentRow, end_bound: Following(UInt64(1)) }], 
mode=[Linear]",
   "    SourceExec: partition_sizes=1, projection=[sn, hash], 
output_ordering=[sn@0 ASC NULLS LAST]",
   ```  
   where `BoundedWindowAggExec`   is in `Linear` mode.  Partition with `hash=2` 
receives following section from the input table
   sn | hash
   -- | --
   0 | 2
   1 | 2
   2 | 2
   3 | 2
   
   query above can generate following result for the section above
   sn | hash | count
   -- | -- | --
   0 | 2 | 2
   1 | 2 | 2
   2 | 2 | <not yet>
   3 | 2 | <not yet>
   
   Since in query we have `range between unbounded preceding and 1 following` 
for `sn=2` we cannot generate result until `sn=4` is received (where it is 
guaranteed end range for the `sn=3`). Same thing applies to other partitions 
with different hash values. However, from the input data, it can be seen that 
for the partition: `hash=2` possible future rows cannot have `sn=3`,`sn=4`, 
etc. (where most recent data the input is `sn=9`). If we can use this 
information, we can generate early results for different partitions. Also this 
enables us to use less memory. With this information we can generate the 
following
   result 
   sn | hash
   -- | --
   0 | 2
   1 | 2
   2 | 2
   3 | 2
   4 | 1
   5 | 1
   6 | 1
   7 | 1
   8 | <not yet>
   9 | <not yet>
   
   instead of current behaviour with result
   sn | hash
   -- | --
   0 | 2
   1 | 2
   2 | <not yet>
   3 | <not yet>
   4 | 1
   5 | 1
   6 | <not yet>
   7 | <not yet>
   8 | <not yet>
   9 | <not yet>
   
   This enables to use less memory when cardinality is high for partition by 
expressions, and window frame query is either `RANGE` or `GROUPS` query (For 
`ROWS` queries we need the new row that belong to same partition anyway.).
   <!--
    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?
   
   <!--
   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
   <!--
   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?
   
   <!--
   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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to