|
| 1 | +// Copyright 2021 Datafuse Labs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/// An indicator to limit the size of datablocks transferred between processor |
| 16 | +#[derive(Debug, Copy, Clone)] |
| 17 | +pub struct DataBlockLimit { |
| 18 | + /// The maximum number of rows allowed in a data block |
| 19 | + pub max_rows: usize, |
| 20 | + /// The maximum size in bytes allowed for a data block |
| 21 | + pub max_bytes: usize, |
| 22 | +} |
| 23 | + |
| 24 | +impl DataBlockLimit { |
| 25 | + pub fn new(mut max_rows: usize, mut max_bytes: usize) -> Self { |
| 26 | + // If max_rows or max_bytes is set to 0, we treat it as no limit |
| 27 | + if max_rows == 0 { |
| 28 | + max_rows = usize::MAX; |
| 29 | + } |
| 30 | + if max_bytes == 0 { |
| 31 | + max_bytes = usize::MAX; |
| 32 | + } |
| 33 | + |
| 34 | + Self { |
| 35 | + max_rows, |
| 36 | + max_bytes, |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// Calculate the number of rows to take based on both row and byte limits |
| 41 | + pub fn rows_to_take(&self, total_rows: usize, total_bytes: usize) -> usize { |
| 42 | + debug_assert!(total_rows > 0); |
| 43 | + // check row limit |
| 44 | + let rows_by_limit = self.max_rows.min(total_rows); |
| 45 | + |
| 46 | + // check byte limit |
| 47 | + let average_bytes_per_row = total_bytes / total_rows; |
| 48 | + let rows_by_bytes = if average_bytes_per_row > 0 { |
| 49 | + // TODO: this 1 may be not suitable |
| 50 | + (self.max_bytes / average_bytes_per_row).max(1) |
| 51 | + } else { |
| 52 | + // Avoid division by zero |
| 53 | + total_rows |
| 54 | + }; |
| 55 | + |
| 56 | + // the minimum of the two limits |
| 57 | + rows_by_limit.min(rows_by_bytes) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[cfg(test)] |
| 62 | +mod tests { |
| 63 | + use super::*; |
| 64 | + |
| 65 | + #[test] |
| 66 | + fn test_rows_to_take_basic() { |
| 67 | + let limit = DataBlockLimit::new(1000, 1_000_000); |
| 68 | + |
| 69 | + // Case 1: Both limits not exceeded |
| 70 | + let rows = limit.rows_to_take(500, 500_000); |
| 71 | + assert_eq!(rows, 500); |
| 72 | + |
| 73 | + // Case 2: Row limit exceeded |
| 74 | + let rows = limit.rows_to_take(2000, 500_000); |
| 75 | + assert_eq!(rows, 1000); |
| 76 | + |
| 77 | + // Case 3: Byte limit exceeded |
| 78 | + // 2_000_000 bytes / 1000 rows = 2000 bytes per row |
| 79 | + // 1_000_000 bytes / 2000 bytes per row = 500 rows |
| 80 | + let rows = limit.rows_to_take(1000, 2_000_000); |
| 81 | + assert_eq!(rows, 500); |
| 82 | + |
| 83 | + let limit = DataBlockLimit::new(1000, 1_000_000); |
| 84 | + |
| 85 | + // Both limits exceeded, byte limit is more restrictive |
| 86 | + // 4_000_000 bytes / 2000 rows = 2000 bytes per row |
| 87 | + // 1_000_000 bytes / 2000 bytes per row = 500 rows |
| 88 | + let rows = limit.rows_to_take(2000, 4_000_000); |
| 89 | + assert_eq!(rows, 500); |
| 90 | + |
| 91 | + // Both limits exceeded, row limit is more restrictive |
| 92 | + // 1_500_000 bytes / 2000 rows = 750 bytes per row |
| 93 | + // 1_000_000 bytes / 750 bytes per row = 1333 rows |
| 94 | + // But row limit is 1000, so we take 1000 |
| 95 | + let rows = limit.rows_to_take(2000, 1_500_000); |
| 96 | + assert_eq!(rows, 1000); |
| 97 | + } |
| 98 | +} |
0 commit comments