forked from apache/horaedb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrite.rs
801 lines (713 loc) · 26.5 KB
/
write.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
// Copyright 2022-2023 CeresDB Project Authors. Licensed under Apache-2.0.
//! Write logic of instance
use std::sync::Arc;
use ceresdbproto::{schema as schema_pb, table_requests};
use common_types::{
bytes::ByteVec,
row::{RowGroup, RowGroupSlicer},
schema::{IndexInWriterSchema, Schema},
};
use common_util::{codec::row, define_result};
use log::{debug, error, info, trace, warn};
use smallvec::SmallVec;
use snafu::{ensure, Backtrace, ResultExt, Snafu};
use table_engine::table::WriteRequest;
use tokio::sync::oneshot;
use wal::{
kv_encoder::LogBatchEncoder,
manager::{SequenceNumber, WalLocation, WriteContext},
};
use crate::{
instance,
instance::{
flush_compaction::TableFlushOptions,
write_worker,
write_worker::{BackgroundStatus, WorkerLocal, WriteTableCommand},
Instance,
},
memtable::{key::KeySequence, PutContext},
payload::WritePayload,
space::{SpaceAndTable, SpaceRef},
table::{
data::{TableData, TableDataRef},
version::MemTableForWrite,
},
};
#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display(
"Failed to encode payloads, table:{}, wal_location:{:?}, err:{}",
table,
wal_location,
source
))]
EncodePayloads {
table: String,
wal_location: WalLocation,
source: wal::manager::Error,
},
#[snafu(display("Failed to write to wal, table:{}, err:{}", table, source))]
WriteLogBatch {
table: String,
source: wal::manager::Error,
},
#[snafu(display("Failed to write to memtable, table:{}, err:{}", table, source))]
WriteMemTable {
table: String,
source: crate::table::version::Error,
},
#[snafu(display("Try to write to a dropped table, table:{}", table))]
WriteDroppedTable { table: String },
#[snafu(display(
"Too many rows to write (more than {}), table:{}, rows:{}.\nBacktrace:\n{}",
MAX_ROWS_TO_WRITE,
table,
rows,
backtrace,
))]
TooManyRows {
table: String,
rows: usize,
backtrace: Backtrace,
},
#[snafu(display("Failed to find mutable memtable, table:{}, err:{}", table, source))]
FindMutableMemTable {
table: String,
source: crate::table::data::Error,
},
#[snafu(display("Failed to write table, source:{}", source,))]
Write { source: write_worker::Error },
#[snafu(display("Failed to flush table, table:{}, err:{}", table, source))]
FlushTable {
table: String,
source: crate::instance::flush_compaction::Error,
},
#[snafu(display(
"Background flush failed, cannot write more data, err:{}.\nBacktrace:\n{}",
msg,
backtrace
))]
BackgroundFlushFailed { msg: String, backtrace: Backtrace },
#[snafu(display("Schema of request is incompatible with table, err:{}", source))]
IncompatSchema {
source: common_types::schema::CompatError,
},
#[snafu(display("Failed to encode row group, err:{}", source))]
EncodeRowGroup {
source: common_util::codec::row::Error,
},
#[snafu(display("Failed to update sequence of memtable, err:{}", source))]
UpdateMemTableSequence { source: crate::memtable::Error },
}
define_result!(Error);
/// Max rows in a write request, must less than [u32::MAX]
const MAX_ROWS_TO_WRITE: usize = 10_000_000;
pub(crate) struct EncodeContext {
pub row_group: RowGroup,
pub index_in_writer: IndexInWriterSchema,
pub encoded_rows: Vec<ByteVec>,
}
impl EncodeContext {
pub fn new(row_group: RowGroup) -> Self {
Self {
row_group,
index_in_writer: IndexInWriterSchema::default(),
encoded_rows: Vec::new(),
}
}
pub fn encode_rows(&mut self, table_schema: &Schema) -> Result<()> {
row::encode_row_group_for_wal(
&self.row_group,
table_schema,
&self.index_in_writer,
&mut self.encoded_rows,
)
.context(EncodeRowGroup)?;
assert_eq!(self.row_group.num_rows(), self.encoded_rows.len());
Ok(())
}
}
/// Split the write request into multiple batches whose size is determined by
/// the `max_bytes_per_batch`.
struct WriteRowGroupSplitter {
/// Max bytes per batch. Actually, the size of a batch is not exactly
/// ensured less than this `max_bytes_per_batch`, but it is guaranteed that
/// the batch contains at most one more row when its size exceeds this
/// `max_bytes_per_batch`.
max_bytes_per_batch: usize,
}
enum SplitResult<'a> {
Splitted {
encoded_batches: Vec<Vec<ByteVec>>,
row_group_batches: Vec<RowGroupSlicer<'a>>,
},
Integrate {
encoded_rows: Vec<ByteVec>,
row_group: RowGroupSlicer<'a>,
},
}
impl WriteRowGroupSplitter {
pub fn new(max_bytes_per_batch: usize) -> Self {
Self {
max_bytes_per_batch,
}
}
/// Split the write request into multiple batches.
///
/// NOTE: The length of the `encoded_rows` should be the same as the number
/// of rows in the `row_group`.
pub fn split<'a>(
&'_ self,
encoded_rows: Vec<ByteVec>,
row_group: &'a RowGroup,
) -> SplitResult<'a> {
let end_row_indexes = self.compute_batches(&encoded_rows);
if end_row_indexes.len() <= 1 {
// No need to split.
return SplitResult::Integrate {
encoded_rows,
row_group: RowGroupSlicer::from(row_group),
};
}
let mut prev_end_row_index = 0;
let mut encoded_batches = Vec::with_capacity(end_row_indexes.len());
let mut row_group_batches = Vec::with_capacity(end_row_indexes.len());
for end_row_index in &end_row_indexes {
let end_row_index = *end_row_index;
let curr_batch = Vec::with_capacity(end_row_index - prev_end_row_index);
encoded_batches.push(curr_batch);
let row_group_slicer =
RowGroupSlicer::new(prev_end_row_index..end_row_index, row_group);
row_group_batches.push(row_group_slicer);
prev_end_row_index = end_row_index;
}
let mut current_batch_idx = 0;
for (row_idx, encoded_row) in encoded_rows.into_iter().enumerate() {
if row_idx >= end_row_indexes[current_batch_idx] {
current_batch_idx += 1;
}
encoded_batches[current_batch_idx].push(encoded_row);
}
SplitResult::Splitted {
encoded_batches,
row_group_batches,
}
}
/// Compute the end row indexes in the original `encoded_rows` of each
/// batch.
fn compute_batches(&self, encoded_rows: &[ByteVec]) -> Vec<usize> {
let mut current_batch_size = 0;
let mut end_row_indexes = Vec::new();
for (row_idx, encoded_row) in encoded_rows.iter().enumerate() {
let row_size = encoded_row.len();
current_batch_size += row_size;
// If the current batch size exceeds the `max_bytes_per_batch`, freeze this
// batch by recording its end row index.
// Note that such check may cause the batch size exceeds the
// `max_bytes_per_batch`.
if current_batch_size >= self.max_bytes_per_batch {
current_batch_size = 0;
end_row_indexes.push(row_idx + 1)
}
}
if current_batch_size > 0 {
end_row_indexes.push(encoded_rows.len());
}
end_row_indexes
}
}
impl Instance {
/// Write data to the table under give space.
pub async fn write_to_table(
&self,
space_table: &SpaceAndTable,
request: WriteRequest,
) -> Result<usize> {
// Collect metrics.
space_table.table_data().metrics.on_write_request_begin();
self.validate_before_write(space_table, &request)?;
// Create a oneshot channel to send/receive write result.
let (tx, rx) = oneshot::channel();
let cmd = WriteTableCommand {
space: space_table.space().clone(),
table_data: space_table.table_data().clone(),
request,
tx,
};
// Send write request to write worker, actual works done in
// Self::process_write_table_command().
write_worker::process_command_in_write_worker(
cmd.into_command(),
space_table.table_data(),
rx,
)
.await
.context(Write)
}
/// Do the actual write, must called by write worker in write thread
/// sequentially.
pub(crate) async fn process_write_table_command(
self: &Arc<Self>,
worker_local: &mut WorkerLocal,
space: &SpaceRef,
table_data: &TableDataRef,
request: WriteRequest,
) -> Result<usize> {
let mut encode_ctx = EncodeContext::new(request.row_group);
self.preprocess_write(worker_local, space, table_data, &mut encode_ctx)
.await?;
{
let _timer = table_data.metrics.start_table_write_encode_timer();
let schema = table_data.schema();
encode_ctx.encode_rows(&schema)?;
}
let EncodeContext {
row_group,
index_in_writer,
encoded_rows,
} = encode_ctx;
match self.maybe_split_write_request(encoded_rows, &row_group) {
SplitResult::Integrate {
encoded_rows,
row_group,
} => {
self.write_table_row_group(
worker_local,
table_data,
row_group,
index_in_writer,
encoded_rows,
)
.await?;
}
SplitResult::Splitted {
encoded_batches,
row_group_batches,
} => {
for (encoded_rows, row_group) in encoded_batches.into_iter().zip(row_group_batches)
{
self.write_table_row_group(
worker_local,
table_data,
row_group,
index_in_writer.clone(),
encoded_rows,
)
.await?;
}
}
}
Ok(row_group.num_rows())
}
fn maybe_split_write_request(
self: &Arc<Self>,
encoded_rows: Vec<ByteVec>,
row_group: &RowGroup,
) -> SplitResult {
if self.max_bytes_per_write_batch.is_none() {
return SplitResult::Integrate {
encoded_rows,
row_group: RowGroupSlicer::from(row_group),
};
}
let splitter = WriteRowGroupSplitter::new(self.max_bytes_per_write_batch.unwrap());
splitter.split(encoded_rows, row_group)
}
async fn write_table_row_group(
self: &Arc<Self>,
worker_local: &mut WorkerLocal,
table_data: &TableDataRef,
row_group: RowGroupSlicer<'_>,
index_in_writer: IndexInWriterSchema,
encoded_rows: Vec<ByteVec>,
) -> Result<()> {
let sequence = self
.write_to_wal(worker_local, table_data, encoded_rows)
.await?;
Self::write_to_memtable(
worker_local,
table_data,
sequence,
&row_group,
index_in_writer,
)
.map_err(|e| {
error!(
"Failed to write to memtable, table:{}, table_id:{}, err:{}",
table_data.name, table_data.id, e
);
e
})?;
// Failure of writing memtable may cause inconsecutive sequence.
if table_data.last_sequence() + 1 != sequence {
warn!(
"Sequence must be consecutive, table:{}, table_id:{}, last_sequence:{}, wal_sequence:{}",
table_data.name,table_data.id,
table_data.last_sequence(),
sequence
);
}
debug!(
"Instance write finished, update sequence, table:{}, table_id:{} last_sequence:{}",
table_data.name, table_data.id, sequence
);
table_data.set_last_sequence(sequence);
// Collect metrics.
table_data
.metrics
.on_write_request_done(row_group.num_rows());
Ok(())
}
/// Return Ok if the request is valid, this is done before entering the
/// write thread.
fn validate_before_write(
&self,
space_table: &SpaceAndTable,
request: &WriteRequest,
) -> Result<()> {
ensure!(
request.row_group.num_rows() < MAX_ROWS_TO_WRITE,
TooManyRows {
table: &space_table.table_data().name,
rows: request.row_group.num_rows(),
}
);
Ok(())
}
/// Preprocess before write, check:
/// - whether table is dropped
/// - memtable capacity and maybe trigger flush
///
/// Fills [common_types::schema::IndexInWriterSchema] in [EncodeContext]
async fn preprocess_write(
self: &Arc<Self>,
worker_local: &mut WorkerLocal,
// space_table: &SpaceAndTable,
space: &SpaceRef,
table_data: &TableDataRef,
encode_ctx: &mut EncodeContext,
) -> Result<()> {
let _timer = table_data.metrics.start_table_write_preprocess_timer();
ensure!(
!table_data.is_dropped(),
WriteDroppedTable {
table: &table_data.name,
}
);
let worker_id = worker_local.worker_id();
worker_local
.ensure_permission(
&table_data.name,
table_data.id.as_u64() as usize,
self.write_group_worker_num,
)
.context(Write)?;
// Checks schema compatibility.
table_data
.schema()
.compatible_for_write(
encode_ctx.row_group.schema(),
&mut encode_ctx.index_in_writer,
)
.context(IncompatSchema)?;
// TODO(yingwen): Allow write and retry flush.
// Check background status, if background error occurred, not allow to write
// again.
match &*worker_local.background_status() {
// Compaction error is ignored now.
BackgroundStatus::Ok | BackgroundStatus::CompactionFailed(_) => (),
BackgroundStatus::FlushFailed(e) => {
return BackgroundFlushFailed { msg: e.to_string() }.fail();
}
}
if self.should_flush_instance() {
if let Some(space) = self.space_store.find_maximum_memory_usage_space() {
if let Some(table) = space.find_maximum_memory_usage_table(worker_id) {
info!("Trying to flush table {} bytes {} in space {} because engine total memtable memory usage exceeds db_write_buffer_size {}.",
table.name,
table.memtable_memory_usage(),
space.id,
self.db_write_buffer_size,
);
let _timer = table_data
.metrics
.start_table_write_instance_flush_wait_timer();
self.handle_memtable_flush(worker_local, &table).await?;
}
}
}
if space.should_flush_space() {
if let Some(table) = space.find_maximum_memory_usage_table(worker_id) {
info!("Trying to flush table {} bytes {} in space {} because space total memtable memory usage exceeds space_write_buffer_size {}.",
table.name,
table.memtable_memory_usage() ,
space.id,
space.write_buffer_size,
);
let _timer = table_data
.metrics
.start_table_write_space_flush_wait_timer();
self.handle_memtable_flush(worker_local, &table).await?;
}
}
if table_data.should_flush_table(worker_local) {
let _timer = table_data.metrics.start_table_write_flush_wait_timer();
self.handle_memtable_flush(worker_local, table_data).await?;
}
Ok(())
}
/// Write log_batch into wal, return the sequence number of log_batch.
async fn write_to_wal(
&self,
worker_local: &WorkerLocal,
table_data: &TableData,
encoded_rows: Vec<ByteVec>,
) -> Result<SequenceNumber> {
let _timer = table_data.metrics.start_table_write_wal_timer();
worker_local
.ensure_permission(
&table_data.name,
table_data.id.as_u64() as usize,
self.write_group_worker_num,
)
.context(Write)?;
// Convert into pb
let write_req_pb = table_requests::WriteRequest {
// FIXME: Shall we avoid the magic number here?
version: 0,
// Use the table schema instead of the schema in request to avoid schema
// mismatch during replaying
schema: Some(schema_pb::TableSchema::from(&table_data.schema())),
rows: encoded_rows,
};
// Encode payload
let payload = WritePayload::Write(&write_req_pb);
let table_location = table_data.table_location();
let wal_location =
instance::create_wal_location(table_location.id, table_location.shard_info);
let log_batch_encoder = LogBatchEncoder::create(wal_location);
let log_batch = log_batch_encoder.encode(&payload).context(EncodePayloads {
table: &table_data.name,
wal_location,
})?;
// Write to wal manager
let write_ctx = WriteContext::default();
let sequence = self
.space_store
.wal_manager
.write(&write_ctx, &log_batch)
.await
.context(WriteLogBatch {
table: &table_data.name,
})?;
Ok(sequence)
}
// TODO(yingwen): How to trigger flush if we found memtables are full during
// inserting memtable? RocksDB checks memtable size in MemTableInserter
/// Write data into memtable.
///
/// The data in `encoded_rows` will be moved to memtable.
///
/// The len of `row_group` and `encoded_rows` must be equal.
pub(crate) fn write_to_memtable(
worker_local: &WorkerLocal,
table_data: &TableDataRef,
sequence: SequenceNumber,
row_group: &RowGroupSlicer,
index_in_writer: IndexInWriterSchema,
) -> Result<()> {
let _timer = table_data.metrics.start_table_write_memtable_timer();
if row_group.is_empty() {
return Ok(());
}
let schema = row_group.schema();
// Store all memtables we wrote and update their last sequence later.
let mut wrote_memtables: SmallVec<[_; 4]> = SmallVec::new();
let mut last_mutable_mem: Option<MemTableForWrite> = None;
let mut ctx = PutContext::new(index_in_writer);
for (row_idx, row) in row_group.iter().enumerate() {
// TODO(yingwen): Add RowWithSchema and take RowWithSchema as input, then remove
// this unwrap()
let timestamp = row.timestamp(schema).unwrap();
// skip expired row
if table_data.is_expired(timestamp) {
trace!("Skip expired row when write to memtable, row:{:?}", row);
continue;
}
if last_mutable_mem.is_none()
|| !last_mutable_mem
.as_ref()
.unwrap()
.accept_timestamp(timestamp)
{
// The time range is not processed by current memtable, find next one.
let mutable_mem = table_data
.find_or_create_mutable(worker_local, timestamp, schema)
.context(FindMutableMemTable {
table: &table_data.name,
})?;
wrote_memtables.push(mutable_mem.clone());
last_mutable_mem = Some(mutable_mem);
}
// We have check the row num is less than `MAX_ROWS_TO_WRITE`, it is safe to
// cast it to u32 here
let key_seq = KeySequence::new(sequence, row_idx as u32);
// TODO(yingwen): Batch sample timestamp in sampling phase.
last_mutable_mem
.as_ref()
.unwrap()
.put(&mut ctx, key_seq, row, schema, timestamp)
.context(WriteMemTable {
table: &table_data.name,
})?;
}
// Update last sequence of memtable.
for mem_wrote in wrote_memtables {
mem_wrote
.set_last_sequence(sequence)
.context(UpdateMemTableSequence)?;
}
Ok(())
}
/// Flush memtables of table in background.
///
/// Only flush mutable memtables, assuming all immutable memtables are
/// flushing.
async fn handle_memtable_flush(
self: &Arc<Self>,
worker_local: &mut WorkerLocal,
table_data: &TableDataRef,
) -> Result<()> {
let opts = TableFlushOptions::default();
// Set `block_on_write_thread` to false and let flush do in background.
self.flush_table_in_worker(worker_local, table_data, opts)
.await
.context(FlushTable {
table: &table_data.name,
})
}
}
#[cfg(test)]
mod tests {
use common_types::{
column_schema::Builder as ColumnSchemaBuilder,
datum::{Datum, DatumKind},
row::{Row, RowGroupBuilder},
schema::Builder as SchemaBuilder,
time::Timestamp,
};
use super::*;
fn generate_rows_for_test(sizes: Vec<usize>) -> (Vec<ByteVec>, RowGroup) {
let encoded_rows: Vec<_> = sizes.iter().map(|size| vec![0; *size]).collect();
let rows: Vec<_> = sizes
.iter()
.map(|size| {
let datum = Datum::Timestamp(Timestamp::new(*size as i64));
Row::from_datums(vec![datum])
})
.collect();
let column_schema = ColumnSchemaBuilder::new("ts".to_string(), DatumKind::Timestamp)
.build()
.unwrap();
let schema = SchemaBuilder::new()
.add_key_column(column_schema)
.unwrap()
.build()
.unwrap();
let row_group = RowGroupBuilder::with_rows(schema, rows).unwrap().build();
(encoded_rows, row_group)
}
#[test]
fn test_write_split_compute_batches() {
let cases = vec![
(2, vec![1, 2, 3, 4, 5], vec![2, 3, 4, 5]),
(100, vec![50, 50, 100, 10], vec![2, 3, 4]),
(1000, vec![50, 50, 100, 10], vec![4]),
(2, vec![10, 10, 0, 10], vec![1, 2, 4]),
(0, vec![10, 10, 0, 10], vec![1, 2, 3, 4]),
(0, vec![0, 0], vec![1, 2]),
(10, vec![], vec![]),
];
for (batch_size, sizes, expected_batch_indexes) in cases {
let (encoded_rows, _) = generate_rows_for_test(sizes);
let write_row_group_splitter = WriteRowGroupSplitter::new(batch_size);
let batch_indexes = write_row_group_splitter.compute_batches(&encoded_rows);
assert_eq!(batch_indexes, expected_batch_indexes);
}
}
#[test]
fn test_write_split_row_group() {
let cases = vec![
(
2,
vec![1, 2, 3, 4, 5],
vec![vec![1, 2], vec![3], vec![4], vec![5]],
),
(
100,
vec![50, 50, 100, 10],
vec![vec![50, 50], vec![100], vec![10]],
),
(1000, vec![50, 50, 100, 10], vec![vec![50, 50, 100, 10]]),
(
2,
vec![10, 10, 0, 10],
vec![vec![10], vec![10], vec![0, 10]],
),
(
0,
vec![10, 10, 0, 10],
vec![vec![10], vec![10], vec![0], vec![10]],
),
(0, vec![0, 0], vec![vec![0], vec![0]]),
(10, vec![], vec![]),
];
let check_encoded_rows = |encoded_rows: &[ByteVec], expected_row_sizes: &[usize]| {
assert_eq!(encoded_rows.len(), expected_row_sizes.len());
for (encoded_row, expected_row_size) in
encoded_rows.iter().zip(expected_row_sizes.iter())
{
assert_eq!(encoded_row.len(), *expected_row_size);
}
};
for (batch_size, sizes, expected_batches) in cases {
let (encoded_rows, row_group) = generate_rows_for_test(sizes.clone());
let write_row_group_splitter = WriteRowGroupSplitter::new(batch_size);
let split_res = write_row_group_splitter.split(encoded_rows, &row_group);
if expected_batches.is_empty() {
assert!(matches!(split_res, SplitResult::Integrate { .. }));
} else if expected_batches.len() == 1 {
assert!(matches!(split_res, SplitResult::Integrate { .. }));
if let SplitResult::Integrate {
encoded_rows,
row_group,
} = split_res
{
check_encoded_rows(&encoded_rows, &expected_batches[0]);
assert_eq!(row_group.num_rows(), expected_batches[0].len());
}
} else {
assert!(matches!(split_res, SplitResult::Splitted { .. }));
if let SplitResult::Splitted {
encoded_batches,
row_group_batches,
} = split_res
{
assert_eq!(encoded_batches.len(), row_group_batches.len());
assert_eq!(encoded_batches.len(), expected_batches.len());
let mut batch_start_index = 0;
for ((encoded_batch, row_group_batch), expected_batch) in encoded_batches
.iter()
.zip(row_group_batches.iter())
.zip(expected_batches.iter())
{
check_encoded_rows(encoded_batch, expected_batch);
assert_eq!(row_group_batch.num_rows(), expected_batch.len());
assert_eq!(row_group_batch.slice_range().start, batch_start_index);
batch_start_index += expected_batch.len();
}
}
}
}
}
}