|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use crate::physical_expr::EmitTo; |
| 19 | + |
| 20 | +/// Tracks grouping state when the data is ordered entirely by its |
| 21 | +/// group keys |
| 22 | +/// |
| 23 | +/// When the group values are sorted, as soon as we see group `n+1` we |
| 24 | +/// know we will never see any rows for group `n again and thus they |
| 25 | +/// can be emitted. |
| 26 | +/// |
| 27 | +/// ```text |
| 28 | +/// SUM(amt) GROUP BY id |
| 29 | +/// |
| 30 | +/// The input is sorted by id |
| 31 | +/// |
| 32 | +/// |
| 33 | +/// ┌─────┐ ┌──────────────────┐ |
| 34 | +/// │┌───┐│ │ ┌──────────────┐ │ ┏━━━━━━━━━━━━━━┓ |
| 35 | +/// ││ 0 ││ │ │ 123 │ │ ┌─────┃ 13 ┃ |
| 36 | +/// │└───┘│ │ └──────────────┘ │ │ ┗━━━━━━━━━━━━━━┛ |
| 37 | +/// │ ... │ │ ... │ │ |
| 38 | +/// │┌───┐│ │ ┌──────────────┐ │ │ current |
| 39 | +/// ││12 ││ │ │ 234 │ │ │ |
| 40 | +/// │├───┤│ │ ├──────────────┤ │ │ |
| 41 | +/// ││12 ││ │ │ 234 │ │ │ |
| 42 | +/// │├───┤│ │ ├──────────────┤ │ │ |
| 43 | +/// ││13 ││ │ │ 456 │◀┼───┘ |
| 44 | +/// │└───┘│ │ └──────────────┘ │ |
| 45 | +/// └─────┘ └──────────────────┘ |
| 46 | +/// |
| 47 | +/// group indices group_values current tracks the most |
| 48 | +/// (in group value recent group index |
| 49 | +/// order) |
| 50 | +/// ``` |
| 51 | +/// |
| 52 | +/// In the above diagram the current group is `13` groups `0..12` can |
| 53 | +/// be emitted. Group `13` can not be emitted because it may have more |
| 54 | +/// values in the next batch. |
| 55 | +#[derive(Debug)] |
| 56 | +pub(crate) struct GroupOrderingFull { |
| 57 | + state: State, |
| 58 | + /// Hash values for groups in 0..completed |
| 59 | + hashes: Vec<u64>, |
| 60 | +} |
| 61 | + |
| 62 | +#[derive(Debug)] |
| 63 | +enum State { |
| 64 | + /// Have seen no input yet |
| 65 | + Start, |
| 66 | + |
| 67 | + /// Have seen all groups with indexes less than `completed_index` |
| 68 | + InProgress { |
| 69 | + /// index of the current group for which values are being |
| 70 | + /// generated (can emit current - 1) |
| 71 | + current: usize, |
| 72 | + }, |
| 73 | + |
| 74 | + /// Seen end of input, all groups can be emitted |
| 75 | + Complete, |
| 76 | +} |
| 77 | + |
| 78 | +impl GroupOrderingFull { |
| 79 | + pub fn new() -> Self { |
| 80 | + Self { |
| 81 | + state: State::Start, |
| 82 | + hashes: vec![], |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // How far can data be emitted? Returns None if no data can be |
| 87 | + // emitted |
| 88 | + pub fn emit_to(&self) -> Option<EmitTo> { |
| 89 | + match &self.state { |
| 90 | + State::Start => None, |
| 91 | + State::InProgress { current, .. } => { |
| 92 | + // Can not emit if we are still on the first row, |
| 93 | + // otherwise emit all rows prior to the current group |
| 94 | + if *current == 0 { |
| 95 | + None |
| 96 | + } else { |
| 97 | + Some(EmitTo::First(*current - 1)) |
| 98 | + } |
| 99 | + } |
| 100 | + State::Complete { .. } => Some(EmitTo::All), |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// removes the first n groups from this ordering, shifting all |
| 105 | + /// existing indexes down by N and returns a reference to the |
| 106 | + /// updated hashes |
| 107 | + pub fn remove_groups(&mut self, n: usize) -> &[u64] { |
| 108 | + println!("remove_groups n:{n}, self: {self:?}"); |
| 109 | + match &mut self.state { |
| 110 | + State::Start => panic!("invalid state: start"), |
| 111 | + State::InProgress { current } => { |
| 112 | + // shift down by n |
| 113 | + assert!(*current >= n); |
| 114 | + *current = *current - n; |
| 115 | + self.hashes.drain(0..n); |
| 116 | + } |
| 117 | + State::Complete { .. } => panic!("invalid state: complete"), |
| 118 | + }; |
| 119 | + &self.hashes |
| 120 | + } |
| 121 | + |
| 122 | + /// Note that the input is complete so any outstanding groups are done as well |
| 123 | + pub fn input_done(&mut self) { |
| 124 | + println!("input done"); |
| 125 | + self.state = match self.state { |
| 126 | + State::Start => State::Complete, |
| 127 | + State::InProgress { .. } => State::Complete, |
| 128 | + State::Complete => State::Complete, |
| 129 | + }; |
| 130 | + } |
| 131 | + |
| 132 | + /// Note that we saw a new distinct group |
| 133 | + pub fn new_group(&mut self, group_index: usize, hash: u64) { |
| 134 | + println!("new group: group_index: {group_index}"); |
| 135 | + self.state = match self.state { |
| 136 | + State::Start => { |
| 137 | + assert_eq!(group_index, 0); |
| 138 | + self.hashes.push(hash); |
| 139 | + State::InProgress { |
| 140 | + current: group_index, |
| 141 | + } |
| 142 | + } |
| 143 | + State::InProgress { current } => { |
| 144 | + // expect to see group_index the next after this |
| 145 | + assert_eq!(group_index, self.hashes.len()); |
| 146 | + assert_eq!(group_index, current + 1); |
| 147 | + self.hashes.push(hash); |
| 148 | + State::InProgress { |
| 149 | + current: group_index, |
| 150 | + } |
| 151 | + } |
| 152 | + State::Complete { .. } => { |
| 153 | + panic!("Saw new group after input was complete"); |
| 154 | + } |
| 155 | + }; |
| 156 | + } |
| 157 | +} |
0 commit comments