risingwave_expr_impl/aggregate/approx_count_distinct/append_only.rs
1// Copyright 2025 RisingWave 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
15use risingwave_common::bail;
16use risingwave_common_estimate_size::EstimateSize;
17use risingwave_expr::Result;
18
19use super::Bucket;
20
21#[derive(Clone, Copy, Default, Debug, EstimateSize)]
22pub struct AppendOnlyBucket(pub u8);
23
24impl Bucket for AppendOnlyBucket {
25 fn update(&mut self, index: u8, retract: bool) -> Result<()> {
26 if index > 64 || index == 0 {
27 bail!("HyperLogLog: Invalid bucket index");
28 }
29 if retract {
30 bail!("HyperLogLog: Deletion in append-only bucket");
31 }
32 if index > self.0 {
33 self.0 = index;
34 }
35 Ok(())
36 }
37
38 fn max(&self) -> u8 {
39 self.0
40 }
41}