This function calculates beginning/ending indices for the frame of the current row.
(
window_frame: &Arc<WindowFrame>,
length: usize,
idx: usize,
)
| 189 | |
| 190 | /// This function calculates beginning/ending indices for the frame of the current row. |
| 191 | fn calculate_range_rows( |
| 192 | window_frame: &Arc<WindowFrame>, |
| 193 | length: usize, |
| 194 | idx: usize, |
| 195 | ) -> Result<Range<usize>> { |
| 196 | let start = match window_frame.start_bound { |
| 197 | // UNBOUNDED PRECEDING |
| 198 | WindowFrameBound::Preceding(ScalarValue::UInt64(None)) => 0, |
| 199 | WindowFrameBound::Preceding(ScalarValue::UInt64(Some(n))) => { |
| 200 | idx.saturating_sub(n as usize) |
| 201 | } |
| 202 | WindowFrameBound::CurrentRow => idx, |
| 203 | // UNBOUNDED FOLLOWING |
| 204 | WindowFrameBound::Following(ScalarValue::UInt64(None)) => { |
| 205 | return internal_err!( |
| 206 | "Frame start cannot be UNBOUNDED FOLLOWING '{window_frame:?}'" |
| 207 | ); |
| 208 | } |
| 209 | WindowFrameBound::Following(ScalarValue::UInt64(Some(n))) => { |
| 210 | std::cmp::min(idx + n as usize, length) |
| 211 | } |
| 212 | // ERRONEOUS FRAMES |
| 213 | WindowFrameBound::Preceding(_) | WindowFrameBound::Following(_) => { |
| 214 | return internal_err!("Rows should be UInt64"); |
| 215 | } |
| 216 | }; |
| 217 | let end = match window_frame.end_bound { |
| 218 | // UNBOUNDED PRECEDING |
| 219 | WindowFrameBound::Preceding(ScalarValue::UInt64(None)) => { |
| 220 | return internal_err!( |
| 221 | "Frame end cannot be UNBOUNDED PRECEDING '{window_frame:?}'" |
| 222 | ); |
| 223 | } |
| 224 | WindowFrameBound::Preceding(ScalarValue::UInt64(Some(n))) => { |
| 225 | if idx >= n as usize { |
| 226 | idx - n as usize + 1 |
| 227 | } else { |
| 228 | 0 |
| 229 | } |
| 230 | } |
| 231 | WindowFrameBound::CurrentRow => idx + 1, |
| 232 | // UNBOUNDED FOLLOWING |
| 233 | WindowFrameBound::Following(ScalarValue::UInt64(None)) => length, |
| 234 | WindowFrameBound::Following(ScalarValue::UInt64(Some(n))) => { |
| 235 | std::cmp::min(idx + n as usize + 1, length) |
| 236 | } |
| 237 | // ERRONEOUS FRAMES |
| 238 | WindowFrameBound::Preceding(_) | WindowFrameBound::Following(_) => { |
| 239 | return internal_err!("Rows should be UInt64"); |
| 240 | } |
| 241 | }; |
| 242 | Ok(Range { start, end }) |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /// State for each unique partition determined according to PARTITION BY column(s) |