risingwave_common/test_utils/
rand_stream_chunk.rs1use rand::{Rng, SeedableRng};
16
17use crate::array::stream_chunk::Op;
18use crate::array::{ArrayBuilder, ArrayImpl, I64ArrayBuilder};
19use crate::bitmap::Bitmap;
20
21pub fn gen_legal_stream_chunk(
22 bitmap: &Bitmap,
23 chunk_size: usize,
24 append_only: bool,
25 seed: u64,
26) -> (Vec<Op>, ArrayImpl) {
27 let mut data_builder = I64ArrayBuilder::new(chunk_size);
28 let mut ops: Vec<Op> = vec![];
29 let mut cur_data: Vec<i64> = vec![];
30 let mut rng = rand::rngs::StdRng::seed_from_u64(seed);
31 for i in 0..chunk_size {
32 if unsafe { bitmap.is_set_unchecked(i) } {
34 let op = if append_only || cur_data.is_empty() || rng.random() {
35 Op::Insert
36 } else {
37 Op::Delete
38 };
39 ops.push(op);
40 if op == Op::Insert {
41 let value = rng.random::<i32>() as i64;
42 data_builder.append(Some(value));
43 cur_data.push(value);
44 } else {
45 let idx = rng.random_range(0..cur_data.len());
46 data_builder.append(Some(cur_data[idx]));
47 cur_data.remove(idx);
48 }
49 } else {
50 ops.push(Op::Insert);
51 data_builder.append(Some(1234567890));
52 }
53 }
54 (ops, data_builder.finish().into())
55}