bigquery 0.10.0

Library provides a simple API for Google BigQuery based on the gRPC Storage Read/Write and v2 APIs
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
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
//! Streaming writers over the Storage Write API.

use crate::errors::{BigQueryCodecErrorKind, BigQueryError};
use crate::types::error::CodecError;
use crate::write::arrow::{ArrowWriterSchema, RecordBatchSlices};
use crate::write::batch::{BatchRows, Batcher, RequestTarget, MAX_REQUEST_BYTES};
use crate::write::connection::{
    Command, ConnectionTask, FinishKind, Finished, Shared, TaskSettings, TASK_ENDED,
};
use crate::write::descriptor::{relaxes_a_required_field, WritePlan};
use crate::write::encoder::Encoder;
use crate::{
    BigQueryDb, BigQueryFinalizedStream, BigQueryResult, BigQueryStreamingWriteOptions,
    BigQueryTableRef, BigQueryTableSchema, BigQueryWriteMode, BigQueryWriteResponse,
    BigQueryWriteSummary,
};
use crate::{BigQueryInstant, BigQueryWriteStreamName};
use arrow_array::RecordBatch;
use futures::stream::BoxStream;
use futures::StreamExt;
use gcloud_sdk::google::cloud::bigquery::storage::v1::storage_error::StorageErrorCode;
use gcloud_sdk::google::cloud::bigquery::storage::v1::write_stream::Type as WriteStreamType;
use gcloud_sdk::google::cloud::bigquery::storage::v1::{
    BatchCommitWriteStreamsRequest, CreateWriteStreamRequest, FinalizeWriteStreamRequest,
    FlushRowsRequest, GetWriteStreamRequest, WriteStream, WriteStreamView,
};
use gcloud_sdk::prost_types::Timestamp;
use serde::Serialize;
use std::marker::PhantomData;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{mpsc, oneshot};
use tokio::task::JoinHandle;
use tokio::time::Instant;
use tracing::field::Empty;
use tracing::{debug_span, warn, Span};

/// The schema a write stream reports.
///
/// # Errors
/// [`BigQueryError::WriteStreamError`] with code `NO_TABLE_SCHEMA` for a stream that came back
/// without one, and the errors of the schema conversion.
impl TryFrom<&WriteStream> for BigQueryTableSchema {
    type Error = BigQueryError;

    fn try_from(stream: &WriteStream) -> Result<Self, Self::Error> {
        let schema = stream.table_schema.as_ref().ok_or_else(|| {
            BigQueryError::write_stream(
                "NO_TABLE_SCHEMA",
                stream.name.clone(),
                "the write stream came back without its table schema",
            )
        })?;
        BigQueryTableSchema::try_from(schema)
    }
}

impl BigQueryDb {
    pub(crate) async fn get_write_stream(
        &self,
        span: &Span,
        name: &BigQueryWriteStreamName,
        view: WriteStreamView,
    ) -> BigQueryResult<WriteStream> {
        let request = GetWriteStreamRequest {
            name: name.as_str().to_string(),
            view: view.into(),
        };
        self.retry(span, "get the write stream", &request, |request| {
            let mut client = self.write_client();
            async move { client.get_write_stream(request).await }
        })
        .await
    }

    async fn create_write_stream(
        &self,
        span: &Span,
        table_path: &str,
        stream_type: WriteStreamType,
    ) -> BigQueryResult<WriteStream> {
        let request = CreateWriteStreamRequest {
            parent: table_path.to_string(),
            write_stream: Some(WriteStream {
                r#type: stream_type.into(),
                ..Default::default()
            }),
        };
        self.retry(span, "create a write stream", &request, |request| {
            let mut client = self.write_client();
            async move { client.create_write_stream(request).await }
        })
        .await
    }

    /// Finalizes a committed or pending stream and returns the rows it holds.
    pub(crate) async fn finalize_write_stream(
        &self,
        span: &Span,
        name: &BigQueryWriteStreamName,
    ) -> BigQueryResult<i64> {
        let request = FinalizeWriteStreamRequest {
            name: name.as_str().to_string(),
        };
        let response = self
            .retry(span, "finalize the write stream", &request, |request| {
                let mut client = self.write_client();
                async move { client.finalize_write_stream(request).await }
            })
            .await?;
        Ok(response.row_count)
    }

    /// Makes the rows of a buffered stream readable up to and including `offset`, and returns
    /// the offset BigQuery reports as flushed.
    pub(crate) async fn flush_rows(
        &self,
        span: &Span,
        name: &BigQueryWriteStreamName,
        offset: i64,
    ) -> BigQueryResult<i64> {
        let request = FlushRowsRequest {
            write_stream: name.as_str().to_string(),
            offset: Some(offset),
        };
        let response = self
            .retry(span, "flush the write stream", &request, |request| {
                let mut client = self.write_client();
                async move { client.flush_rows(request).await }
            })
            .await?;
        Ok(response.offset)
    }

    /// Commits finalized pending streams of the table at `table_path` together.
    ///
    /// A retried commit can find every stream already committed. When each of them is a pending
    /// stream reporting the same commit time, that time is returned. Any other stream error,
    /// an already committed stream on the first attempt, streams committed at different
    /// times, or a failed lookup of their commit times included, is returned as BigQuery's
    /// stream error.
    pub(crate) async fn batch_commit(
        &self,
        span: &Span,
        table_path: &str,
        streams: &[BigQueryWriteStreamName],
    ) -> BigQueryResult<BigQueryInstant> {
        let request = BatchCommitWriteStreamsRequest {
            parent: table_path.to_string(),
            write_streams: streams.iter().map(|s| s.as_str().to_string()).collect(),
        };
        let attempts = AtomicUsize::new(0);
        let response = self
            .retry(span, "commit the write streams", &request, |request| {
                attempts.fetch_add(1, Ordering::Relaxed);
                let mut client = self.write_client();
                async move { client.batch_commit_write_streams(request).await }
            })
            .await?;
        let commit_time = match response.stream_errors.first() {
            None => response.commit_time.ok_or_else(|| {
                BigQueryError::write_stream(
                    "NO_COMMIT_TIME",
                    request.write_streams.join(", "),
                    "the commit reported no error and no commit time",
                )
            })?,
            Some(error) => {
                let already_committed = response.stream_errors.iter().all(|stream_error| {
                    stream_error.code() == StorageErrorCode::StreamAlreadyCommitted
                });
                let earlier_commit = if already_committed && attempts.into_inner() > 1 {
                    self.common_commit_time(span, streams).await
                } else {
                    None
                };
                earlier_commit.ok_or_else(|| {
                    let code = StorageErrorCode::try_from(error.code)
                        .map(|code| code.as_str_name().to_string())
                        .unwrap_or_else(|_| error.code.to_string());
                    BigQueryError::write_stream(
                        &code,
                        error.entity.clone(),
                        error.error_message.clone(),
                    )
                })?
            }
        };
        BigQueryInstant::new(commit_time.seconds, commit_time.nanos).map_err(|err| {
            BigQueryError::write_stream(
                "INVALID_COMMIT_TIME",
                request.write_streams.join(", "),
                format!("the commit time {commit_time:?} is not a timestamp: {err}"),
            )
        })
    }

    /// The commit time every one of `streams` reports, or `None` if a lookup fails, one of
    /// them is not a committed pending stream, or they were committed at different times.
    ///
    /// Only a pending stream's `commit_time` is the time of a commit; a committed-type stream
    /// reports its creation time there.
    async fn common_commit_time(
        &self,
        span: &Span,
        streams: &[BigQueryWriteStreamName],
    ) -> Option<Timestamp> {
        let mut common: Option<Timestamp> = None;
        for stream in streams {
            let stream = self
                .get_write_stream(span, stream, WriteStreamView::Basic)
                .await
                .ok()?;
            if stream.r#type() != WriteStreamType::Pending {
                return None;
            }
            let commit_time = stream.commit_time?;
            if *common.get_or_insert(commit_time) != commit_time {
                return None;
            }
        }
        common
    }
}

impl BigQueryStreamingWriteOptions {
    fn check(&self, cdc: bool) -> BigQueryResult<()> {
        if self.max_request_bytes == 0 || self.max_request_bytes > MAX_REQUEST_BYTES {
            return Err(BigQueryError::invalid_parameters(
                "max_request_bytes",
                format!(
                "{} is outside 1..={MAX_REQUEST_BYTES}; BigQuery ends the whole connection on a \
                 request over about 20 MB",
                self.max_request_bytes
            ),
            ));
        }
        if self.max_batch_rows == Some(0) {
            return Err(BigQueryError::invalid_parameters(
                "max_batch_rows",
                "a batch holds at least one row",
            ));
        }
        if self.max_inflight_requests == 0 {
            return Err(BigQueryError::invalid_parameters(
                "max_inflight_requests",
                "at least one request must be allowed in flight",
            ));
        }
        if cdc && self.mode != BigQueryWriteMode::Default {
            return Err(BigQueryError::invalid_parameters(
                "mode",
                format!(
                    "CDC writes go through the default stream only, not {:?}",
                    self.mode
                ),
            ));
        }
        Ok(())
    }
}

impl BigQueryCodecErrorKind {
    /// Whether a failed encode can be caused by a schema the writer has not seen yet: a column
    /// added, or a REQUIRED column relaxed to NULLABLE.
    fn may_be_a_stale_schema(self) -> bool {
        matches!(
            self,
            BigQueryCodecErrorKind::UnknownField
                | BigQueryCodecErrorKind::NullForRequired
                | BigQueryCodecErrorKind::MissingRequiredField
        )
    }
}

/// The untyped writer behind [`BigQueryStreamingWriter`] and
/// [`BigQueryCdcWriter`](crate::BigQueryCdcWriter).
pub(crate) struct WriterCore {
    db: BigQueryDb,
    span: Span,
    table: BigQueryTableRef,
    stream: BigQueryWriteStreamName,
    mode: BigQueryWriteMode,
    cdc: bool,
    refresh_interval: Duration,
    last_refresh: Option<Instant>,
    encoder: Encoder,
    row_hint: usize,
    /// The schema of the last Arrow record batch written, and the bytes a request has for a
    /// record batch with it.
    arrow_schema: Option<(Arc<ArrowWriterSchema>, usize)>,
    shared: Arc<Shared>,
    commands: mpsc::UnboundedSender<Command>,
    task: Option<JoinHandle<()>>,
    finished: bool,
}

pub(crate) type ResponseStream<'b> = BoxStream<'b, BigQueryResult<BigQueryWriteResponse>>;

impl WriterCore {
    pub(crate) async fn open<'b>(
        db: &BigQueryDb,
        table: BigQueryTableRef,
        options: BigQueryStreamingWriteOptions,
        cdc: bool,
    ) -> BigQueryResult<(Self, ResponseStream<'b>)> {
        options.check(cdc)?;
        let table_path = table.table_path(&db.options().google_project_id);
        let span = debug_span!(
            "BigQuery streaming write",
            "/bigquery/table" = %table,
            "/bigquery/write_mode" = ?options.mode,
            "/bigquery/rows_appended" = Empty,
            "/bigquery/bytes_sent" = Empty,
            "/bigquery/appends" = Empty,
            "/bigquery/retries" = Empty,
        );
        let stream = match options.mode {
            BigQueryWriteMode::Default => {
                let default =
                    BigQueryWriteStreamName::reported(format!("{table_path}/streams/_default"));
                db.get_write_stream(&span, &default, WriteStreamView::Full)
                    .await?
            }
            BigQueryWriteMode::Committed => {
                db.create_write_stream(&span, &table_path, WriteStreamType::Committed)
                    .await?
            }
            BigQueryWriteMode::Pending => {
                db.create_write_stream(&span, &table_path, WriteStreamType::Pending)
                    .await?
            }
            BigQueryWriteMode::Buffered => {
                db.create_write_stream(&span, &table_path, WriteStreamType::Buffered)
                    .await?
            }
        };
        let schema = BigQueryTableSchema::try_from(&stream)?;
        let stream_name = BigQueryWriteStreamName::reported(stream.name.clone());
        let plan = Arc::new(WritePlan::new(&schema, cdc));
        let target = RequestTarget {
            write_stream: stream.name.clone(),
            trace_id: options.trace_id.as_ref().map(ToString::to_string),
            missing_value: options.missing_value,
        };
        let batcher = Batcher::new(
            plan.clone(),
            &target,
            options.max_request_bytes,
            options.max_batch_rows,
        );
        let shared = Arc::new(Shared::new(
            batcher,
            options.max_inflight_requests,
            options.max_inflight_bytes,
        ));
        let (commands, commands_rx) = mpsc::unbounded_channel();
        let (responses, responses_rx) = futures::channel::mpsc::unbounded();
        let task = ConnectionTask::new(
            TaskSettings {
                db: db.clone(),
                span: span.clone(),
                mode: options.mode,
                target,
                stream: stream_name.clone(),
                table: table.clone(),
                table_path,
                batch_delay: options.max_batch_delay,
            },
            shared.clone(),
            commands_rx,
            responses,
        );
        let task = tokio::spawn(task.run());
        let core = WriterCore {
            db: db.clone(),
            span,
            table,
            stream: stream_name,
            mode: options.mode,
            cdc,
            refresh_interval: options.schema_refresh_interval,
            last_refresh: None,
            encoder: Encoder::new(plan),
            row_hint: 0,
            arrow_schema: None,
            shared,
            commands,
            task: Some(task),
            finished: false,
        };
        Ok((core, responses_rx.boxed()))
    }

    pub(crate) fn mode(&self) -> BigQueryWriteMode {
        self.mode
    }

    pub(crate) fn stream_name(&self) -> &BigQueryWriteStreamName {
        &self.stream
    }

    fn check_failed(&self) -> BigQueryResult<()> {
        match &self.shared.lock().failed {
            Some(err) => Err(err.clone()),
            None => Ok(()),
        }
    }

    fn command(&self, command: Command) -> BigQueryResult<()> {
        self.commands
            .send(command)
            .map_err(|_| BigQueryError::system("WRITER_TASK_ENDED", TASK_ENDED))
    }

    /// Encodes one row with `encode` and adds it to the open batch. A row that fails because
    /// the writer's schema may be stale makes it read the schema again, at most once per
    /// `schema_refresh_interval`, and encode the row once more.
    pub(crate) async fn write_with<F>(&mut self, mut encode: F) -> BigQueryResult<()>
    where
        F: FnMut(&mut Encoder, &mut Vec<u8>) -> Result<(), CodecError>,
    {
        self.check_failed()?;
        let pending = self.shared.lock().pending_schema.take();
        if let Some(schema) = pending {
            self.switch_plan(schema).await?;
        }
        let index = self.shared.lock().batcher.next_row();
        let mut row = Vec::with_capacity(self.row_hint);
        if let Err(err) = encode(&mut self.encoder, &mut row) {
            if !(err.kind()).may_be_a_stale_schema() || !self.refresh_schema().await? {
                return Err(err.with_row(index).into_serialize());
            }
            row.clear();
            encode(&mut self.encoder, &mut row)
                .map_err(|err| err.with_row(index).into_serialize())?;
        }
        self.row_hint = row.len();
        let cost = self
            .shared
            .lock()
            .batcher
            .cost(row.len())
            .map_err(|err| err.with_row(index).into_serialize())?;
        if !self.shared.lock().batcher.fits(cost) {
            self.seal_open().await?;
        }
        let (opened, full) = {
            let mut state = self.shared.lock();
            if let Some(err) = &state.failed {
                return Err(err.clone());
            }
            let opened = state.batcher.push(row, cost, Instant::now());
            (opened, state.batcher.is_full())
        };
        if opened {
            self.command(Command::Wake)?;
        }
        if full {
            self.seal_open().await?;
        }
        Ok(())
    }

    /// Writes `batch` as one batch per slice that fits a request under `max_request_bytes` and
    /// `max_batch_rows`, each sealed at once, so a slice never shares a request with another.
    /// The batch's own schema is the writer schema, and BigQuery checks it against the table.
    pub(crate) async fn write_record_batch(&mut self, batch: &RecordBatch) -> BigQueryResult<()> {
        self.check_failed()?;
        let (schema, capacity) = match &self.arrow_schema {
            Some((schema, capacity)) if schema.describes(batch) => (schema.clone(), *capacity),
            _ => {
                let schema = Arc::new(ArrowWriterSchema::new(batch.schema())?);
                let capacity = self.shared.lock().batcher.arrow_capacity(&schema);
                self.arrow_schema = Some((schema.clone(), capacity));
                (schema, capacity)
            }
        };
        let (first_row, max_rows) = {
            let state = self.shared.lock();
            (state.batcher.next_row(), state.batcher.max_rows())
        };
        let slices = RecordBatchSlices::new(batch, &schema, capacity, max_rows, first_row);
        for slice in slices {
            let slice = slice.map_err(CodecError::into_serialize)?;
            let rows = BatchRows::Arrow {
                schema: schema.clone(),
                record_batch: slice.serialized,
                row_count: slice.row_count as u64,
            };
            self.seal_rows(rows, slice.cost).await?;
        }
        Ok(())
    }

    /// Seals `rows` as a batch of their own once the in-flight limits allow it.
    async fn seal_rows(&self, rows: BatchRows, bytes: usize) -> BigQueryResult<()> {
        self.seal_open().await?;
        loop {
            let notified = self.shared.capacity.notified();
            tokio::pin!(notified);
            notified.as_mut().enable();
            {
                let mut state = self.shared.lock();
                if let Some(err) = &state.failed {
                    return Err(err.clone());
                }
                if self.shared.has_room(&state, bytes) {
                    let batch = state.batcher.seal_rows(rows, bytes);
                    Shared::queue_locked(&mut state, batch);
                    drop(state);
                    return self.command(Command::Wake);
                }
            }
            notified.await;
        }
    }

    /// Reads the stream's schema again unless it was read within `schema_refresh_interval`.
    /// Returns whether the plan changed.
    async fn refresh_schema(&mut self) -> BigQueryResult<bool> {
        let now = Instant::now();
        if self
            .last_refresh
            .is_some_and(|last| now < last + self.refresh_interval)
        {
            return Ok(false);
        }
        self.last_refresh = Some(now);
        let stream = self
            .db
            .get_write_stream(&self.span, &self.stream, WriteStreamView::Full)
            .await?;
        let schema = BigQueryTableSchema::try_from(&stream)?;
        self.switch_plan(schema).await
    }

    /// Encodes the next rows against `schema`, at a batch boundary. Returns whether the plan
    /// changed.
    async fn switch_plan(&mut self, schema: BigQueryTableSchema) -> BigQueryResult<bool> {
        if &schema == self.encoder.plan().schema() {
            return Ok(false);
        }
        self.seal_open().await?;
        let relaxed =
            relaxes_a_required_field(&self.encoder.plan().schema().fields, &schema.fields);
        let plan = Arc::new(WritePlan::new(&schema, self.cdc));
        self.shared.lock().batcher.set_plan(plan.clone());
        self.encoder = Encoder::new(plan);
        if relaxed {
            self.command(Command::Reconnect)?;
        }
        Ok(true)
    }

    /// Seals the open batch, waiting while the in-flight limits are reached.
    async fn seal_open(&self) -> BigQueryResult<()> {
        loop {
            let notified = self.shared.capacity.notified();
            tokio::pin!(notified);
            notified.as_mut().enable();
            {
                let mut state = self.shared.lock();
                if let Some(err) = &state.failed {
                    return Err(err.clone());
                }
                if state.batcher.open_bytes().is_none() {
                    return Ok(());
                }
                if self.shared.can_seal(&state) {
                    self.shared.seal_locked(&mut state);
                    drop(state);
                    return self.command(Command::Wake);
                }
            }
            notified.await;
        }
    }

    /// Sends the open batch and waits until every batch so far has an outcome. Returns the
    /// rows then written on a stream with offsets, which is the offset of the next row.
    pub(crate) async fn flush(&mut self) -> BigQueryResult<i64> {
        self.seal_open().await?;
        let (tx, rx) = oneshot::channel();
        self.command(Command::Flush(tx))?;
        rx.await
            .map_err(|_| BigQueryError::system("WRITER_TASK_ENDED", TASK_ENDED))?
    }

    /// Fails with [`BigQueryError::InvalidParametersError`] for the field `mode` unless the
    /// writer is in `mode`, naming `operation` as the call that needs it.
    fn check_mode(&self, mode: BigQueryWriteMode, operation: &str) -> BigQueryResult<()> {
        if self.mode == mode {
            return Ok(());
        }
        Err(BigQueryError::invalid_parameters(
            "mode",
            format!(
                "{operation} is for {mode:?} streams, the writer is {:?}",
                self.mode
            ),
        ))
    }

    /// Flushes a buffered stream up to its last written row once every batch so far has an
    /// outcome; `None` while the stream holds no rows.
    pub(crate) async fn flush_rows(&mut self) -> BigQueryResult<Option<i64>> {
        self.check_mode(BigQueryWriteMode::Buffered, "flush_rows()")?;
        let written = self.flush().await?;
        if written == 0 {
            return Ok(None);
        }
        self.flush_rows_to(written - 1).await.map(Some)
    }

    /// Flushes a buffered stream up to and including `offset`, whether or not the writer
    /// failed.
    pub(crate) async fn flush_rows_to(&mut self, offset: i64) -> BigQueryResult<i64> {
        self.check_mode(BigQueryWriteMode::Buffered, "flush_rows_to()")?;
        self.db.flush_rows(&self.span, &self.stream, offset).await
    }

    pub(crate) async fn finish(&mut self, kind: FinishKind) -> BigQueryResult<Finished> {
        self.finished = true;
        let sealed = self.seal_open().await;
        let (tx, rx) = oneshot::channel();
        self.command(Command::Finish(kind, tx))?;
        let finished = rx
            .await
            .map_err(|_| BigQueryError::system("WRITER_TASK_ENDED", TASK_ENDED))?;
        if let Some(task) = self.task.take() {
            let _ = task.await;
        }
        sealed?;
        finished
    }

    /// Stops the writer without the drop warning, for callers that report their own error.
    pub(crate) fn abandon(&mut self) {
        self.finished = true;
        if let Some(task) = self.task.take() {
            task.abort();
        }
    }

    pub(crate) fn table(&self) -> &BigQueryTableRef {
        &self.table
    }
}

impl Drop for WriterCore {
    fn drop(&mut self) {
        if !self.finished {
            warn!(
                parent: &self.span,
                stream = %self.stream,
                "A BigQuery streaming writer was dropped without finish(); rows not yet \
                 acknowledged may be lost and a pending stream stays uncommitted."
            );
        }
        if let Some(task) = self.task.take() {
            task.abort();
        }
    }
}

/// A writer that streams rows of `T` into one table over one `AppendRows` connection.
///
/// `T` is a [`Serialize`] row type, written with [`write`](Self::write) as protobuf against the
/// table's schema, or Arrow's [`RecordBatch`], written with
/// [`write_batch`](BigQueryStreamingWriter::write_batch) as Arrow IPC with the batch's own
/// schema; [`BigQueryDb::create_record_batch_writer`] opens that one.
///
/// Rows are encoded as they are written and sent in batches under `max_request_bytes`, as
/// configured in [`BigQueryStreamingWriteOptions`]. A background task owns the connection, so
/// the writer is `Send` but not `Clone`; open more writers for more connections. It holds no
/// `T`, so `T` needs neither `Send` nor `'static`.
///
/// The response stream returned with it yields one item per batch, in batch order, and ends
/// when the writer finishes or fails for good. Reading it is optional: [`finish`] reports
/// failed rows either way.
///
/// Call [`finish`] (or [`finalize`] in pending mode) when done. Dropping the writer logs a
/// warning, drops rows not yet acknowledged, and leaves a pending stream uncommitted.
///
/// The writer picks up schema changes: an `updated_schema` from BigQuery, or a field with no
/// column, makes it encode the next batches against the new schema. A dropped column is not
/// seen in time: BigQuery accepts its values and drops them silently for about 9 seconds
/// before it rejects the rows, so drop a column only after every writer has stopped sending it.
///
/// [`finish`]: BigQueryStreamingWriter::finish
/// [`finalize`]: BigQueryStreamingWriter::finalize
pub struct BigQueryStreamingWriter<T> {
    core: WriterCore,
    _row: PhantomData<fn(&T)>,
}

impl<T: Serialize> BigQueryStreamingWriter<T> {
    /// Writes one row. It waits while `max_inflight_requests` or `max_inflight_bytes` are
    /// unacknowledged.
    ///
    /// # Errors
    /// [`BigQueryError::SerializeError`] for a row that does not fit the table schema, naming
    /// the row by its index in write order; nothing of it is sent and the writer goes on. Once
    /// the writer has failed for good, that error, on every call.
    pub async fn write(&mut self, row: &T) -> BigQueryResult<()> {
        self.core
            .write_with(|encoder, out| encoder.encode(row, out))
            .await
    }

    /// Writes every row in order, stopping at the first error, as [`write`](Self::write).
    pub async fn write_all<'r, I>(&mut self, rows: I) -> BigQueryResult<()>
    where
        I: IntoIterator<Item = &'r T>,
        T: 'r,
    {
        for row in rows {
            self.write(row).await?;
        }
        Ok(())
    }
}

impl BigQueryStreamingWriter<RecordBatch> {
    /// Writes the rows of one Arrow record batch, sent with the batch's own schema. A batch too
    /// large for `max_request_bytes`, or longer than `max_batch_rows`, goes as slices of it,
    /// one request each; a batch never shares a request with another. It waits while
    /// `max_inflight_requests` or `max_inflight_bytes` are unacknowledged.
    ///
    /// The schema is not checked against the table: BigQuery rejects a batch that does not
    /// fit, as a failed batch on the response stream and in the summary. How Arrow types map to
    /// BigQuery types is in Google's
    /// [supported data types](https://cloud.google.com/bigquery/docs/supported-data-types).
    ///
    /// # Errors
    /// [`BigQueryError::SerializeError`] of kind `RowTooLarge` for a row that fits no request
    /// alone, naming it by its index in write order; the slices before it are sent and the
    /// writer goes on. Once the writer has failed for good, that error, on every call.
    pub async fn write_batch(&mut self, batch: &RecordBatch) -> BigQueryResult<()> {
        self.core.write_record_batch(batch).await
    }
}

/// The stream's side of the writer, which does not depend on how rows are encoded.
impl<T> BigQueryStreamingWriter<T> {
    fn from_core(core: WriterCore) -> Self {
        Self {
            core,
            _row: PhantomData,
        }
    }

    /// Sends the open batch and waits until every batch written so far has an outcome.
    ///
    /// # Errors
    /// The writer's failure, if it failed for good. Failed batches are reported on the
    /// response stream and in the summary, not here.
    pub async fn flush(&mut self) -> BigQueryResult<()> {
        self.core.flush().await.map(|_| ())
    }

    /// Flushes, waits for every acknowledgement, and closes the stream: the default stream is
    /// left as it is, a committed one is finalized, a buffered one is flushed up to its last
    /// written row and finalized, so every written row becomes readable, and a pending one is
    /// finalized and committed.
    ///
    /// # Errors
    /// The writer's failure, if it failed for good. In pending mode,
    /// [`BigQueryError::WriteStreamError`] with code `NOT_COMMITTED` if any batch failed,
    /// since then nothing is committed.
    pub async fn finish(mut self) -> BigQueryResult<BigQueryWriteSummary> {
        Ok(self.core.finish(FinishKind::Close).await?.summary)
    }

    /// Pending mode only: flushes, waits for every acknowledgement and finalizes the stream,
    /// leaving the commit to [`BigQueryDb::commit_write_streams`], so several writers can
    /// commit together.
    ///
    /// # Errors
    /// [`BigQueryError::InvalidParametersError`] in any other mode, and the errors of
    /// [`finish`](Self::finish).
    pub async fn finalize(mut self) -> BigQueryResult<BigQueryFinalizedStream> {
        if let Err(err) = self
            .core
            .check_mode(BigQueryWriteMode::Pending, "finalize()")
        {
            self.core.abandon();
            return Err(err);
        }
        let finished = self.core.finish(FinishKind::Finalize).await?;
        Ok(BigQueryFinalizedStream {
            table: self.core.table().clone(),
            name: self.core.stream_name().clone(),
            row_count: finished.finalized_rows.unwrap_or_default(),
        })
    }

    /// Buffered mode only: sends the open batch, waits until every batch written so far has an
    /// outcome, and flushes the stream up to its last written row, so that every row written
    /// so far becomes readable. Returns the flushed offset, or `None` while the stream holds
    /// no rows.
    ///
    /// # Errors
    /// [`BigQueryError::InvalidParametersError`] for the field `mode` in any other mode, the
    /// writer's failure if it failed for good, and the `FlushRows` failure.
    pub async fn flush_rows(&mut self) -> BigQueryResult<Option<i64>> {
        self.core.flush_rows().await
    }

    /// Buffered mode only: flushes the stream up to and including `offset`, and returns the
    /// offset BigQuery reports as flushed. The last row of an acknowledged batch is at
    /// [`offset`](BigQueryWriteResponse::offset) `+ row_count - 1` of its response. It does
    /// not wait for the batches in flight, and BigQuery checks the offset.
    ///
    /// It needs no `AppendRows` connection, so it works after the writer failed for good too:
    /// flushing to the last acknowledged row then makes every acknowledged row readable.
    ///
    /// # Errors
    /// [`BigQueryError::InvalidParametersError`] for the field `mode` in any other mode, and
    /// the `FlushRows` failure.
    pub async fn flush_rows_to(&mut self, offset: i64) -> BigQueryResult<i64> {
        self.core.flush_rows_to(offset).await
    }

    /// The write stream's name; `None` for the default stream.
    pub fn stream_name(&self) -> Option<&BigQueryWriteStreamName> {
        (self.core.mode() != BigQueryWriteMode::Default).then(|| self.core.stream_name())
    }
}

impl BigQueryDb {
    /// Opens a streaming writer on `table` with default options: the default stream, at
    /// least once.
    ///
    /// Opening costs one `GetWriteStream` round trip, about 300 ms, so many small writes
    /// should share one writer.
    ///
    /// # Errors
    /// The `GetWriteStream` failure, such as [`BigQueryError::DataNotFoundError`] for a missing
    /// table.
    pub async fn create_streaming_writer<'b, T: Serialize>(
        &self,
        table: impl Into<BigQueryTableRef>,
    ) -> BigQueryResult<(
        BigQueryStreamingWriter<T>,
        BoxStream<'b, BigQueryResult<BigQueryWriteResponse>>,
    )> {
        self.create_streaming_writer_with_options(table, BigQueryStreamingWriteOptions::new())
            .await
    }

    /// Opens a streaming writer on `table` with `options`. A committed, pending or buffered
    /// mode creates a new write stream.
    ///
    /// # Errors
    /// [`BigQueryError::InvalidParametersError`] for options that cannot be sent, such as a
    /// `max_request_bytes` over 19,000,000; otherwise the failure to open the stream.
    pub async fn create_streaming_writer_with_options<'b, T: Serialize>(
        &self,
        table: impl Into<BigQueryTableRef>,
        options: BigQueryStreamingWriteOptions,
    ) -> BigQueryResult<(
        BigQueryStreamingWriter<T>,
        BoxStream<'b, BigQueryResult<BigQueryWriteResponse>>,
    )> {
        let (core, responses) = WriterCore::open(self, table.into(), options, false).await?;
        Ok((BigQueryStreamingWriter::from_core(core), responses))
    }

    /// Opens a writer of Arrow record batches on `table` with default options: the default
    /// stream, at least once. See [`write_batch`](BigQueryStreamingWriter::write_batch).
    ///
    /// # Errors
    /// As [`create_streaming_writer`](Self::create_streaming_writer).
    pub async fn create_record_batch_writer<'b>(
        &self,
        table: impl Into<BigQueryTableRef>,
    ) -> BigQueryResult<(
        BigQueryStreamingWriter<RecordBatch>,
        BoxStream<'b, BigQueryResult<BigQueryWriteResponse>>,
    )> {
        self.create_record_batch_writer_with_options(table, BigQueryStreamingWriteOptions::new())
            .await
    }

    /// Opens a writer of Arrow record batches on `table` with `options`. `max_batch_delay`
    /// and `schema_refresh_interval` do not apply, since every record batch is sent as it is
    /// written, with its own schema.
    ///
    /// # Errors
    /// As [`create_streaming_writer_with_options`](Self::create_streaming_writer_with_options).
    pub async fn create_record_batch_writer_with_options<'b>(
        &self,
        table: impl Into<BigQueryTableRef>,
        options: BigQueryStreamingWriteOptions,
    ) -> BigQueryResult<(
        BigQueryStreamingWriter<RecordBatch>,
        BoxStream<'b, BigQueryResult<BigQueryWriteResponse>>,
    )> {
        let (core, responses) = WriterCore::open(self, table.into(), options, false).await?;
        Ok((BigQueryStreamingWriter::from_core(core), responses))
    }

    /// Commits finalized pending streams of one table together: all of their rows become
    /// visible at the returned commit time, or none do.
    ///
    /// # Errors
    /// [`BigQueryError::InvalidParametersError`] for no streams or streams of more than one
    /// table; [`BigQueryError::WriteStreamError`] when BigQuery reports a stream it could not
    /// commit.
    pub async fn commit_write_streams(
        &self,
        streams: Vec<BigQueryFinalizedStream>,
    ) -> BigQueryResult<BigQueryInstant> {
        let Some(first) = streams.first() else {
            return Err(BigQueryError::invalid_parameters(
                "streams",
                "there is no stream to commit",
            ));
        };
        let table = first.table.clone();
        if let Some(other) = streams.iter().find(|s| s.table != table) {
            return Err(BigQueryError::invalid_parameters(
                "streams",
                format!(
                    "BigQuery commits streams of one table together, got {table} and {}",
                    other.table
                ),
            ));
        }
        let table_path = table.table_path(&self.options().google_project_id);
        let span = debug_span!("BigQuery commit write streams", "/bigquery/table" = %table);
        self.batch_commit(
            &span,
            &table_path,
            &streams.into_iter().map(|s| s.name).collect::<Vec<_>>(),
        )
        .await
    }
}

#[cfg(test)]
mod tests;