fluss-rs 1.0.0

The official rust client of Apache Fluss
Documentation
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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

use crate::TableId;
use crate::client::broadcast::{BatchWriteResult, BroadcastOnce};
use crate::client::{Record, ResultHandle, WriteRecord};
use crate::error::{Error, Result};
use crate::metadata::{KvFormat, PhysicalTablePath};
use crate::record::kv::KvRecordBatchBuilder;
use crate::record::{ArrowBatchConfig, MemoryLogRecordsArrowBuilder};
use crate::record::{NO_BATCH_SEQUENCE, NO_WRITER_ID};
use bytes::Bytes;
use std::cmp::max;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicI32, Ordering};

pub struct InnerWriteBatch {
    batch_id: i64,
    physical_table_path: Arc<PhysicalTablePath>,
    table_id: TableId,
    create_ms: i64,
    results: BroadcastOnce<BatchWriteResult>,
    completed: AtomicBool,
    attempts: AtomicI32,
    drained_ms: i64,
    batch_sequence: i32,
    writer_id: i64,
    last_acked_sequence_at_send: i32,
}

impl InnerWriteBatch {
    fn new(
        batch_id: i64,
        physical_table_path: Arc<PhysicalTablePath>,
        table_id: TableId,
        create_ms: i64,
    ) -> Self {
        InnerWriteBatch {
            batch_id,
            physical_table_path,
            table_id,
            create_ms,
            results: Default::default(),
            completed: AtomicBool::new(false),
            attempts: AtomicI32::new(0),
            drained_ms: -1,
            batch_sequence: NO_BATCH_SEQUENCE,
            writer_id: NO_WRITER_ID,
            last_acked_sequence_at_send: -1,
        }
    }

    pub fn batch_sequence(&self) -> i32 {
        self.batch_sequence
    }

    pub fn writer_id(&self) -> i64 {
        self.writer_id
    }

    pub fn has_batch_sequence(&self) -> bool {
        self.batch_sequence != NO_BATCH_SEQUENCE
    }

    fn waited_time_ms(&self, now: i64) -> i64 {
        max(0i64, now - self.create_ms)
    }

    /// Time the batch spent queued in the accumulator, from creation until it
    /// was drained for sending. Returns 0 if the batch has not been drained
    /// yet (`drained_ms == -1`). Distinct from [`Self::waited_time_ms`], which
    /// measures the batch's current age for timeout purposes.
    fn queue_time_ms(&self) -> i64 {
        max(0i64, self.drained_ms - self.create_ms)
    }

    fn complete(&self, write_result: BatchWriteResult) -> bool {
        if self
            .completed
            .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
            .is_err()
        {
            return false;
        }
        self.results.broadcast(write_result);
        true
    }

    fn drained(&mut self, now_ms: i64) {
        self.drained_ms = max(self.drained_ms, now_ms);
    }

    fn physical_table_path(&self) -> &Arc<PhysicalTablePath> {
        &self.physical_table_path
    }

    fn table_id(&self) -> TableId {
        self.table_id
    }

    fn attempts(&self) -> i32 {
        self.attempts.load(Ordering::Acquire)
    }

    fn re_enqueued(&self) {
        self.attempts.fetch_add(1, Ordering::AcqRel);
    }

    fn is_done(&self) -> bool {
        self.completed.load(Ordering::Acquire)
    }
}

pub enum WriteBatch {
    ArrowLog(ArrowLogWriteBatch),
    Kv(KvWriteBatch),
}

impl WriteBatch {
    pub fn inner_batch(&self) -> &InnerWriteBatch {
        match self {
            WriteBatch::ArrowLog(batch) => &batch.write_batch,
            WriteBatch::Kv(batch) => &batch.write_batch,
        }
    }

    pub fn inner_batch_mut(&mut self) -> &mut InnerWriteBatch {
        match self {
            WriteBatch::ArrowLog(batch) => &mut batch.write_batch,
            WriteBatch::Kv(batch) => &mut batch.write_batch,
        }
    }

    pub fn try_append(&mut self, write_record: &WriteRecord) -> Result<Option<ResultHandle>> {
        match self {
            WriteBatch::ArrowLog(batch) => batch.try_append(write_record),
            WriteBatch::Kv(batch) => batch.try_append(write_record),
        }
    }

    pub fn waited_time_ms(&self, now: i64) -> i64 {
        self.inner_batch().waited_time_ms(now)
    }

    /// Time the batch spent queued in the accumulator before being drained
    /// (`drained_ms - create_ms`). See [`InnerWriteBatch::queue_time_ms`].
    pub(crate) fn queue_time_ms(&self) -> i64 {
        self.inner_batch().queue_time_ms()
    }

    /// Number of records currently in this batch. Used for writer metrics.
    pub(crate) fn record_count(&self) -> i32 {
        match self {
            WriteBatch::ArrowLog(batch) => batch.arrow_builder.records_count(),
            WriteBatch::Kv(batch) => batch.record_count(),
        }
    }

    pub fn close(&mut self) -> Result<()> {
        match self {
            WriteBatch::ArrowLog(batch) => {
                batch.close();
                Ok(())
            }
            WriteBatch::Kv(batch) => batch.close(),
        }
    }

    pub fn estimated_size_in_bytes(&self) -> usize {
        match self {
            WriteBatch::ArrowLog(batch) => batch.estimated_size_in_bytes(),
            WriteBatch::Kv(batch) => batch.estimated_size_in_bytes(),
        }
    }

    pub fn is_closed(&self) -> bool {
        match self {
            WriteBatch::ArrowLog(batch) => batch.is_closed(),
            WriteBatch::Kv(batch) => batch.is_closed(),
        }
    }

    pub fn drained(&mut self, now_ms: i64) {
        self.inner_batch_mut().drained(now_ms);
    }

    pub fn build(&mut self) -> Result<Bytes> {
        match self {
            WriteBatch::ArrowLog(batch) => batch.build(),
            WriteBatch::Kv(batch) => batch.build(),
        }
    }

    pub fn complete(&self, write_result: BatchWriteResult) -> bool {
        self.inner_batch().complete(write_result)
    }

    pub fn batch_id(&self) -> i64 {
        self.inner_batch().batch_id
    }

    pub fn physical_table_path(&self) -> &Arc<PhysicalTablePath> {
        self.inner_batch().physical_table_path()
    }

    pub fn table_id(&self) -> TableId {
        self.inner_batch().table_id()
    }

    pub fn attempts(&self) -> i32 {
        self.inner_batch().attempts()
    }

    pub fn re_enqueued(&self) {
        self.inner_batch().re_enqueued();
    }

    pub fn is_done(&self) -> bool {
        self.inner_batch().is_done()
    }

    pub fn batch_sequence(&self) -> i32 {
        self.inner_batch().batch_sequence()
    }

    pub fn writer_id(&self) -> i64 {
        self.inner_batch().writer_id()
    }

    pub fn has_batch_sequence(&self) -> bool {
        self.inner_batch().has_batch_sequence()
    }

    /// Last acknowledged sequence for this bucket when the current attempt was sent.
    pub(crate) fn last_acked_sequence_at_send(&self) -> i32 {
        self.inner_batch().last_acked_sequence_at_send
    }

    /// Refreshes the acknowledged-sequence snapshot on every send, including retries.
    pub(crate) fn set_last_acked_sequence_at_send(&mut self, sequence: i32) {
        self.inner_batch_mut().last_acked_sequence_at_send = sequence;
    }

    pub fn set_writer_state(&mut self, writer_id: i64, batch_base_sequence: i32) {
        match self {
            WriteBatch::ArrowLog(batch) => batch.set_writer_state(writer_id, batch_base_sequence),
            WriteBatch::Kv(batch) => batch.set_writer_state(writer_id, batch_base_sequence),
        }
    }
}

pub struct ArrowLogWriteBatch {
    pub write_batch: InnerWriteBatch,
    pub(crate) arrow_builder: MemoryLogRecordsArrowBuilder,
    built_records: Option<Bytes>,
}

impl ArrowLogWriteBatch {
    pub(crate) fn new(
        batch_id: i64,
        physical_table_path: Arc<PhysicalTablePath>,
        table_id: TableId,
        config: ArrowBatchConfig,
        create_ms: i64,
        to_append_record_batch: bool,
    ) -> Result<Self> {
        let base = InnerWriteBatch::new(batch_id, physical_table_path, table_id, create_ms);
        Ok(Self {
            write_batch: base,
            arrow_builder: MemoryLogRecordsArrowBuilder::new(config, to_append_record_batch)?,
            built_records: None,
        })
    }

    pub fn batch_id(&self) -> i64 {
        self.write_batch.batch_id
    }

    pub fn try_append(&mut self, write_record: &WriteRecord) -> Result<Option<ResultHandle>> {
        if self.arrow_builder.is_closed() || self.arrow_builder.is_full() {
            Ok(None)
        } else {
            // append successfully
            if self.arrow_builder.append(write_record)? {
                Ok(Some(ResultHandle::new(self.write_batch.results.receiver())))
            } else {
                // append fail
                Ok(None)
            }
        }
    }

    pub fn set_writer_state(&mut self, writer_id: i64, batch_base_sequence: i32) {
        self.arrow_builder
            .set_writer_state(writer_id, batch_base_sequence);
        self.write_batch.batch_sequence = batch_base_sequence;
        self.write_batch.writer_id = writer_id;
        self.built_records = None;
    }

    pub fn build(&mut self) -> Result<Bytes> {
        if let Some(bytes) = &self.built_records {
            return Ok(bytes.clone());
        }
        let bytes = Bytes::from(self.arrow_builder.build()?);
        self.built_records = Some(bytes.clone());
        Ok(bytes)
    }

    pub fn is_closed(&self) -> bool {
        self.arrow_builder.is_closed()
    }

    pub fn close(&mut self) {
        self.arrow_builder.close()
    }

    /// Get an estimate of the number of bytes written to the underlying buffer.
    /// The returned value is exactly correct if the batch has been built.
    pub fn estimated_size_in_bytes(&self) -> usize {
        if let Some(ref bytes) = self.built_records {
            // Return actual size if already built
            bytes.len()
        } else {
            // Delegate to arrow builder for estimated size
            self.arrow_builder.estimated_size_in_bytes()
        }
    }
}

pub struct KvWriteBatch {
    write_batch: InnerWriteBatch,
    kv_batch_builder: KvRecordBatchBuilder,
    target_columns: Option<Arc<Vec<usize>>>,
    schema_id: i32,
}

impl KvWriteBatch {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        batch_id: i64,
        physical_table_path: Arc<PhysicalTablePath>,
        table_id: TableId,
        schema_id: i32,
        write_limit: usize,
        kv_format: KvFormat,
        target_columns: Option<Arc<Vec<usize>>>,
        create_ms: i64,
    ) -> Self {
        let base = InnerWriteBatch::new(batch_id, physical_table_path, table_id, create_ms);
        Self {
            write_batch: base,
            kv_batch_builder: KvRecordBatchBuilder::new(schema_id, write_limit, kv_format),
            target_columns,
            schema_id,
        }
    }

    pub fn try_append(&mut self, write_record: &WriteRecord) -> Result<Option<ResultHandle>> {
        let kv_write_record = match &write_record.record {
            Record::Kv(record) => record,
            _ => {
                return Err(Error::UnsupportedOperation {
                    message: "Only KvRecord to append to KvWriteBatch ".to_string(),
                });
            }
        };

        let key = kv_write_record.key.as_ref();

        if self.schema_id != write_record.schema_id {
            return Err(Error::UnexpectedError {
                message: format!(
                    "schema id {} of the write record to append is not the same as the current schema id {} in the batch.",
                    write_record.schema_id, self.schema_id
                ),
                source: None,
            });
        };

        if self.target_columns != kv_write_record.target_columns {
            return Err(Error::UnexpectedError {
                message: format!(
                    "target columns {:?} of the write record to append are not the same as the current target columns {:?} in the batch.",
                    kv_write_record.target_columns,
                    self.target_columns.as_deref()
                ),
                source: None,
            });
        }

        let row_bytes = kv_write_record.row_bytes();

        if self.is_closed() || !self.kv_batch_builder.has_room_for_row(key, row_bytes) {
            Ok(None)
        } else {
            // append successfully
            self.kv_batch_builder
                .append_row(key, row_bytes)
                .map_err(|e| Error::UnexpectedError {
                    message: "Failed to append row to KvWriteBatch".to_string(),
                    source: Some(Box::new(e)),
                })?;
            Ok(Some(ResultHandle::new(self.write_batch.results.receiver())))
        }
    }

    pub fn build(&mut self) -> Result<Bytes> {
        self.kv_batch_builder.build()
    }

    pub fn is_closed(&self) -> bool {
        self.kv_batch_builder.is_closed()
    }

    pub fn close(&mut self) -> Result<()> {
        self.kv_batch_builder.close()
    }

    pub fn set_writer_state(&mut self, writer_id: i64, batch_base_sequence: i32) {
        self.kv_batch_builder
            .set_writer_state(writer_id, batch_base_sequence);
        self.write_batch.batch_sequence = batch_base_sequence;
        self.write_batch.writer_id = writer_id;
    }

    pub fn target_columns(&self) -> Option<&Arc<Vec<usize>>> {
        self.target_columns.as_ref()
    }

    /// Get an estimate of the number of bytes written to the underlying buffer.
    /// This returns the current size including header and all appended records.
    pub fn estimated_size_in_bytes(&self) -> usize {
        self.kv_batch_builder.get_size_in_bytes()
    }

    /// Number of records appended to this KV batch. Used for writer metrics.
    pub(crate) fn record_count(&self) -> i32 {
        self.kv_batch_builder.record_count()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::client::{RowBytes, WriteFormat};
    use crate::metadata::{RowType, TablePath};
    use crate::test_utils::build_table_info;

    /// An uncompressed [`ArrowBatchConfig`] for schema 1.
    fn uncompressed_config(row_type: &RowType, write_limit: usize) -> ArrowBatchConfig {
        crate::test_utils::uncompressed_arrow_batch_config(1, row_type, write_limit)
    }

    #[test]
    fn complete_only_once() {
        let table_path = TablePath::new("db".to_string(), "tbl".to_string());
        let physical_path = PhysicalTablePath::of(Arc::new(table_path));
        let batch = InnerWriteBatch::new(1, Arc::new(physical_path), 1, 0);
        assert!(batch.complete(Ok(())));
        assert!(!batch.complete(Err(crate::client::broadcast::Error::Dropped)));
    }

    #[test]
    fn attempts_increment_on_reenqueue() {
        let table_path = TablePath::new("db".to_string(), "tbl".to_string());
        let physical_path = PhysicalTablePath::of(Arc::new(table_path));
        let batch = InnerWriteBatch::new(1, Arc::new(physical_path), 1, 0);
        assert_eq!(batch.attempts(), 0);
        batch.re_enqueued();
        assert_eq!(batch.attempts(), 1);
    }

    #[test]
    fn queue_time_ms_is_drained_minus_create() {
        let table_path = TablePath::new("db".to_string(), "tbl".to_string());
        let physical_path = PhysicalTablePath::of(Arc::new(table_path));
        let mut batch = InnerWriteBatch::new(1, Arc::new(physical_path), 1, 1_000);
        // Not drained yet -> 0 (drained_ms == -1).
        assert_eq!(batch.queue_time_ms(), 0);
        batch.drained(1_150);
        assert_eq!(batch.queue_time_ms(), 150);
        // drained() keeps the max; an earlier timestamp does not shrink it.
        batch.drained(1_120);
        assert_eq!(batch.queue_time_ms(), 150);
    }

    #[test]
    fn record_count_reflects_appended_rows() {
        use crate::client::WriteRecord;
        use crate::metadata::{DataField, DataTypes, RowType};
        use crate::row::GenericRow;

        let row_type = RowType::new(vec![DataField::new(
            "id".to_string(),
            DataTypes::int(),
            None,
        )]);
        let table_path = TablePath::new("db".to_string(), "tbl".to_string());
        let table_info = Arc::new(build_table_info(table_path.clone(), 1, 1));
        let physical_table_path = Arc::new(PhysicalTablePath::of(Arc::new(table_path)));

        let arrow_batch = ArrowLogWriteBatch::new(
            1,
            Arc::clone(&physical_table_path),
            1,
            uncompressed_config(&row_type, 2 * 1024 * 1024),
            0,
            false,
        )
        .unwrap();
        let mut batch = WriteBatch::ArrowLog(arrow_batch);

        assert_eq!(batch.record_count(), 0);
        for _ in 0..5 {
            let mut row = GenericRow::new(1);
            row.set_field(0, 1_i32);
            let record = WriteRecord::for_append(
                Arc::clone(&table_info),
                Arc::clone(&physical_table_path),
                1,
                &row,
            );
            batch.try_append(&record).unwrap();
        }
        assert_eq!(batch.record_count(), 5);
    }

    #[test]
    fn kv_record_count_reflects_appended_records() {
        use crate::client::WriteRecord;
        use crate::metadata::KvFormat;

        let table_path = TablePath::new("db".to_string(), "tbl".to_string());
        let table_info = Arc::new(build_table_info(table_path.clone(), 1, 1));
        let physical_path = Arc::new(PhysicalTablePath::of(Arc::new(table_path)));

        let mut batch = WriteBatch::Kv(KvWriteBatch::new(
            1,
            Arc::clone(&physical_path),
            1,
            1,
            64 * 1024,
            KvFormat::COMPACTED,
            None,
            0,
        ));

        assert_eq!(batch.record_count(), 0);
        for _ in 0..3 {
            let record = WriteRecord::for_upsert(
                Arc::clone(&table_info),
                Arc::clone(&physical_path),
                1,
                Bytes::from(vec![1_u8, 2_u8, 3_u8]),
                None,
                WriteFormat::CompactedKv,
                None,
                Some(RowBytes::Owned(Bytes::from(vec![1_u8, 2_u8, 3_u8]))),
            );
            batch.try_append(&record).unwrap();
        }
        assert_eq!(batch.record_count(), 3);
    }

    #[test]
    fn test_arrow_log_write_batch_estimated_size() {
        use crate::client::WriteRecord;
        use crate::metadata::{DataField, DataTypes, RowType};
        use crate::row::GenericRow;
        use arrow::array::{Int32Array, RecordBatch, StringArray};
        use std::sync::Arc;

        let row_type = RowType::new(vec![
            DataField::new("id".to_string(), DataTypes::int(), None),
            DataField::new("name".to_string(), DataTypes::string(), None),
        ]);
        let table_path = TablePath::new("db".to_string(), "tbl".to_string());
        let table_info = Arc::new(build_table_info(table_path.clone(), 1, 1));
        let physical_table_path = Arc::new(PhysicalTablePath::of(Arc::new(table_path)));

        // Test 1: RowAppendRecordBatchBuilder (to_append_record_batch=false)
        {
            let mut batch = ArrowLogWriteBatch::new(
                1,
                Arc::clone(&physical_table_path),
                1,
                uncompressed_config(&row_type, 2 * 1024 * 1024),
                0,
                false,
            )
            .unwrap();

            // Append rows
            for _ in 0..200 {
                let mut row = GenericRow::new(2);
                row.set_field(0, 1_i32);
                row.set_field(1, "hello");
                let record = WriteRecord::for_append(
                    Arc::clone(&table_info),
                    Arc::clone(&physical_table_path),
                    1,
                    &row,
                );
                batch.try_append(&record).unwrap();
            }

            let estimated_size = batch.estimated_size_in_bytes();
            assert!(estimated_size > 0);

            let built_data = batch.build().unwrap();
            let actual_size = built_data.len();

            let diff = actual_size.abs_diff(estimated_size);
            let threshold = actual_size / 10; // 10% tolerance
            assert!(
                diff <= threshold,
                "RowAppend: estimated_size {estimated_size} and actual_size {actual_size} differ by more than 10%"
            );
        }

        // Test 2: PrebuiltRecordBatchBuilder (to_append_record_batch=true)
        {
            let mut batch = ArrowLogWriteBatch::new(
                1,
                physical_table_path.clone(),
                1,
                uncompressed_config(&row_type, 2 * 1024 * 1024),
                0,
                true,
            )
            .unwrap();

            // Create a pre-built RecordBatch
            let schema = crate::record::to_arrow_schema(&row_type).unwrap();
            let ids: Vec<i32> = (0..200).collect();
            let names: Vec<&str> = (0..200).map(|_| "hello").collect();
            let record_batch = RecordBatch::try_new(
                schema,
                vec![
                    Arc::new(Int32Array::from(ids)),
                    Arc::new(StringArray::from(names)),
                ],
            )
            .unwrap();

            let record = WriteRecord::for_append_record_batch(
                Arc::clone(&table_info),
                Arc::clone(&physical_table_path),
                1,
                record_batch,
            );
            batch.try_append(&record).unwrap();

            let estimated_size = batch.estimated_size_in_bytes();
            assert!(estimated_size > 0);

            let built_data = batch.build().unwrap();
            let actual_size = built_data.len();

            let diff = actual_size.abs_diff(estimated_size);
            let threshold = actual_size / 10; // 10% tolerance
            assert!(
                diff <= threshold,
                "Prebuilt: estimated_size {estimated_size} and actual_size {actual_size} differ by more than 10%"
            );
        }
    }

    #[test]
    fn test_kv_write_batch_estimated_size() {
        use crate::metadata::KvFormat;

        let table_path = TablePath::new("db".to_string(), "tbl".to_string());
        let table_info = Arc::new(build_table_info(table_path.clone(), 1, 1));
        let physical_path = Arc::new(PhysicalTablePath::of(Arc::new(table_path)));

        let mut batch = KvWriteBatch::new(
            1,
            Arc::clone(&physical_path),
            1,
            1,
            256,
            KvFormat::COMPACTED,
            None,
            0,
        );

        for _ in 0..200 {
            let record = WriteRecord::for_upsert(
                Arc::clone(&table_info),
                Arc::clone(&physical_path),
                1,
                Bytes::from(vec![1_u8, 2_u8, 3_u8]),
                None,
                WriteFormat::CompactedKv,
                None,
                Some(RowBytes::Owned(Bytes::from(vec![1_u8, 2_u8, 3_u8]))),
            );
            batch.try_append(&record).unwrap();
        }

        let estimated_size = batch.estimated_size_in_bytes();
        let actual_size = batch.build().unwrap().len();

        assert_eq!(
            actual_size, estimated_size,
            "estimated size {estimated_size} is not equal to actual size"
        );
    }

    /// Verifies byte-size-based fullness:
    /// 1. Actual built size stays within the configured limit (no compression).
    /// 2. Old 256-record cap is gone — large batches accept >256 small rows.
    /// 3. Compression feedback loop: shared estimator updates after build(),
    ///    second batch with same estimator accepts more records.
    #[test]
    fn test_arrow_batch_byte_size_fullness() {
        use crate::client::WriteRecord;
        use crate::compression::{
            ArrowCompressionInfo, ArrowCompressionRatioEstimator, ArrowCompressionType,
        };
        use crate::metadata::{DataField, DataTypes, RowType};
        use crate::row::GenericRow;
        use std::sync::Arc;

        let row_type = RowType::new(vec![
            DataField::new("id".to_string(), DataTypes::int(), None),
            DataField::new("name".to_string(), DataTypes::string(), None),
        ]);
        let table_path = TablePath::new("db".to_string(), "tbl".to_string());
        let table_info = Arc::new(build_table_info(table_path.clone(), 1, 1));
        let physical_table_path = Arc::new(PhysicalTablePath::of(Arc::new(table_path)));

        // --- Part 1: actual built size stays within limit (uncompressed) ---
        let write_limit: usize = 16 * 1024;
        let mut batch = ArrowLogWriteBatch::new(
            1,
            Arc::clone(&physical_table_path),
            1,
            uncompressed_config(&row_type, write_limit),
            0,
            false,
        )
        .unwrap();

        let mut appended = 0;
        for i in 0..100_000 {
            let mut row = GenericRow::new(2);
            row.set_field(0, i);
            row.set_field(1, "hello_world");
            let record = WriteRecord::for_append(
                Arc::clone(&table_info),
                Arc::clone(&physical_table_path),
                1,
                &row,
            );
            match batch.try_append(&record).unwrap() {
                Some(_) => appended += 1,
                None => break,
            }
        }

        assert!(
            appended > 0 && appended < 100_000,
            "batch should have filled, appended: {appended}"
        );
        let built = batch.build().unwrap();
        assert!(
            built.len() <= write_limit * 120 / 100,
            "actual size {} exceeds write_limit {write_limit} by more than 20%",
            built.len()
        );

        // --- Part 2: old 256-record cap is gone ---
        let row_type_small = RowType::new(vec![DataField::new(
            "id".to_string(),
            DataTypes::int(),
            None,
        )]);
        let mut batch = ArrowLogWriteBatch::new(
            2,
            Arc::clone(&physical_table_path),
            1,
            uncompressed_config(&row_type_small, 2 * 1024 * 1024),
            0,
            false,
        )
        .unwrap();

        let mut appended = 0;
        for i in 0..1000 {
            let mut row = GenericRow::new(1);
            row.set_field(0, i);
            let record = WriteRecord::for_append(
                Arc::clone(&table_info),
                Arc::clone(&physical_table_path),
                1,
                &row,
            );
            match batch.try_append(&record).unwrap() {
                Some(_) => appended += 1,
                None => break,
            }
        }
        assert_eq!(appended, 1000, "2MB batch should fit 1000 tiny rows");

        // --- Part 3: compression feedback loop ---
        let estimator = Arc::new(ArrowCompressionRatioEstimator::default());
        assert_eq!(estimator.estimation(), 1.0);

        let write_limit = 64 * 1024;
        let compression = ArrowCompressionInfo {
            compression_type: ArrowCompressionType::Zstd,
            compression_level: 3,
        };

        // First batch: fill and build with ZSTD.
        let mut batch1 = ArrowLogWriteBatch::new(
            3,
            Arc::clone(&physical_table_path),
            1,
            ArrowBatchConfig {
                schema_id: 1,
                row_type: row_type.clone(),
                stats_index_mapping: None,
                compression: compression.clone(),
                write_limit,
                compression_ratio_estimator: Arc::clone(&estimator),
            },
            0,
            false,
        )
        .unwrap();

        for i in 0..500 {
            let mut row = GenericRow::new(2);
            row.set_field(0, i);
            row.set_field(1, "aaaaaaaaaaaaaaaa");
            let record = WriteRecord::for_append(
                Arc::clone(&table_info),
                Arc::clone(&physical_table_path),
                1,
                &row,
            );
            if batch1.try_append(&record).unwrap().is_none() {
                break;
            }
        }
        batch1.build().unwrap();

        // Estimator should have decreased (ZSTD compresses repeated data well).
        assert!(
            estimator.estimation() < 1.0,
            "ratio should decrease after compressed build, got: {}",
            estimator.estimation()
        );

        // Second batch: same estimator → knows data compresses well → accepts more rows.
        let mut batch2 = ArrowLogWriteBatch::new(
            4,
            Arc::clone(&physical_table_path),
            1,
            ArrowBatchConfig {
                schema_id: 1,
                row_type: row_type.clone(),
                stats_index_mapping: None,
                compression,
                write_limit,
                compression_ratio_estimator: Arc::clone(&estimator),
            },
            0,
            false,
        )
        .unwrap();

        let mut appended2 = 0;
        for i in 0..10_000 {
            let mut row = GenericRow::new(2);
            row.set_field(0, i);
            row.set_field(1, "aaaaaaaaaaaaaaaa");
            let record = WriteRecord::for_append(
                Arc::clone(&table_info),
                Arc::clone(&physical_table_path),
                1,
                &row,
            );
            match batch2.try_append(&record).unwrap() {
                Some(_) => appended2 += 1,
                None => break,
            }
        }
        assert!(
            appended2 > 500,
            "second batch should accept more records with updated ratio, got: {appended2}"
        );
    }
}