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
//! The rules a test registers, how a call finds the one that answers it, and the faults a rule
//! can answer with.

use crate::errors::BigQueryError;
use crate::testing::matcher::BigQueryFakeQuery;
use crate::testing::state::TableKey;
use crate::{BigQueryDmlStats, BigQueryResult, BigQueryStatementType, BigQueryTableSchema};
use arrow_array::RecordBatch;
use gcloud_sdk::google::cloud::bigquery::v2::{QueryParameter, QueryParameterValue};
use gcloud_sdk::tonic::Code;
use std::fmt::{Debug, Display, Formatter};
use std::num::NonZeroUsize;
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

/// A registered rule, to read how many calls it answered. Clones share one counter.
///
/// Every rule a [`BigQueryFake`](super::BigQueryFake) registers stays registered until the fake
/// is dropped.
#[derive(Clone, Debug)]
pub struct BigQueryFakeRule {
    counter: Arc<RuleCounter>,
}

#[derive(Debug)]
struct RuleCounter {
    /// What the rule matches, as the unmatched report and `verify` list it.
    description: String,
    /// The calls the rule answers before it stops matching, if limited.
    times: Option<NonZeroUsize>,
    calls: AtomicUsize,
}

impl BigQueryFakeRule {
    fn new(description: String, times: Option<NonZeroUsize>) -> Self {
        Self {
            counter: Arc::new(RuleCounter {
                description,
                times,
                calls: AtomicUsize::new(0),
            }),
        }
    }

    /// A rule that answers every call it matches.
    pub(super) fn unlimited(description: String) -> Self {
        Self::new(description, None)
    }

    /// A rule that answers `times` calls if set, and every call it matches if not.
    ///
    /// # Errors
    /// [`BigQueryError::InvalidParametersError`] for the field `times` for `times(0)`, a rule
    /// that could never answer.
    pub(super) fn limited(description: String, times: Option<usize>) -> BigQueryResult<Self> {
        match times.map(NonZeroUsize::new) {
            Some(None) => Err(BigQueryError::invalid_parameters(
                "times",
                format!("{description} would answer no call; leave times unset or pass 1 or more"),
            )),
            Some(times) => Ok(Self::new(description, times)),
            None => Ok(Self::unlimited(description)),
        }
    }

    /// The RPCs this rule answered, retries included. For
    /// [`when_rows_rejected`](super::BigQueryFake::when_rows_rejected), the append requests it
    /// rejected.
    pub fn calls(&self) -> usize {
        self.counter.calls.load(Ordering::SeqCst)
    }

    /// Counts one call if the rule has calls left, and says whether it did.
    fn claim(&self) -> bool {
        let calls = &self.counter.calls;
        let mut current = calls.load(Ordering::SeqCst);
        loop {
            if self
                .counter
                .times
                .is_some_and(|times| current >= times.get())
            {
                return false;
            }
            match calls.compare_exchange_weak(
                current,
                current + 1,
                Ordering::SeqCst,
                Ordering::SeqCst,
            ) {
                Ok(_) => return true,
                Err(actual) => current = actual,
            }
        }
    }

    /// Counts one call whatever the rule's limit.
    pub(super) fn count(&self) {
        self.counter.calls.fetch_add(1, Ordering::SeqCst);
    }

    /// The rule as the unmatched report lists it: `description (answered n of times(m))`.
    fn listed(&self) -> String {
        let calls = self.calls();
        match self.counter.times {
            Some(times) => format!(
                "{} (answered {calls} of times({times}))",
                self.counter.description
            ),
            None => format!("{} (answered {calls})", self.counter.description),
        }
    }

    /// Why this rule fails `verify`: it was limited to `times(n)` and answered another number
    /// of calls.
    fn shortfall(&self) -> Option<String> {
        let times = self.counter.times?.get();
        let calls = self.calls();
        (calls != times).then(|| {
            format!(
                "{} answered {calls} of its times({times}) calls",
                self.counter.description
            )
        })
    }
}

/// How a scripted call fails.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum BigQueryFakeFault {
    /// Ends the call with a gRPC status before its first response message, as BigQuery does
    /// for a refused request. The client maps it as it maps BigQuery's own: `NotFound` is a
    /// [`DataNotFoundError`](crate::errors::BigQueryError::DataNotFoundError), `AlreadyExists`
    /// a [`DataConflictError`](crate::errors::BigQueryError::DataConflictError), `Aborted`,
    /// `Unavailable`, `ResourceExhausted`, `Internal`, and `PermissionDenied` or
    /// `InvalidArgument` with "exceeded rate limits" in the message a retryable
    /// [`DatabaseError`](crate::errors::BigQueryError::DatabaseError), and anything else one
    /// that is not retried.
    ///
    /// On `AppendRows` it is instead the in-band error of the append request that drew it,
    /// which the writer handles by its own rules: it sends the request again after `Internal`,
    /// `Aborted` or `OutOfRange`, takes `AlreadyExists` on a stream written at offsets as the
    /// request already written, and fails the request's rows on any other code, `Unavailable`
    /// and `ResourceExhausted` included.
    ///
    /// [`status`](Self::status) makes one.
    #[non_exhaustive]
    Status {
        /// The gRPC code.
        code: BigQueryFakeCode,
        /// The status message.
        message: String,
    },
    /// Closes the connection without answering, as a response lost in transit. The client
    /// sees a transport error, which it retries. Every other call on the same connection is
    /// lost with it.
    ConnectionDropped,
    /// Never answers, as a call stuck in flight. The client waits until its own timeout, if
    /// it has one.
    Hang,
}

impl BigQueryFakeFault {
    /// A [`Status`](Self::Status) fault.
    pub fn status(code: BigQueryFakeCode, message: impl Into<String>) -> Self {
        Self::Status {
            code,
            message: message.into(),
        }
    }
}

/// The gRPC status codes a fault can answer with: every code but `OK`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BigQueryFakeCode {
    /// `CANCELLED`.
    Cancelled,
    /// `UNKNOWN`.
    Unknown,
    /// `INVALID_ARGUMENT`.
    InvalidArgument,
    /// `DEADLINE_EXCEEDED`.
    DeadlineExceeded,
    /// `NOT_FOUND`.
    NotFound,
    /// `ALREADY_EXISTS`.
    AlreadyExists,
    /// `PERMISSION_DENIED`.
    PermissionDenied,
    /// `RESOURCE_EXHAUSTED`.
    ResourceExhausted,
    /// `FAILED_PRECONDITION`.
    FailedPrecondition,
    /// `ABORTED`.
    Aborted,
    /// `OUT_OF_RANGE`.
    OutOfRange,
    /// `UNIMPLEMENTED`.
    Unimplemented,
    /// `INTERNAL`.
    Internal,
    /// `UNAVAILABLE`.
    Unavailable,
    /// `DATA_LOSS`.
    DataLoss,
    /// `UNAUTHENTICATED`.
    Unauthenticated,
}

impl BigQueryFakeCode {
    /// The code on the wire. A method rather than `From`, which would put the transport's
    /// type in the public API.
    pub(crate) fn grpc_code(self) -> Code {
        match self {
            BigQueryFakeCode::Cancelled => Code::Cancelled,
            BigQueryFakeCode::Unknown => Code::Unknown,
            BigQueryFakeCode::InvalidArgument => Code::InvalidArgument,
            BigQueryFakeCode::DeadlineExceeded => Code::DeadlineExceeded,
            BigQueryFakeCode::NotFound => Code::NotFound,
            BigQueryFakeCode::AlreadyExists => Code::AlreadyExists,
            BigQueryFakeCode::PermissionDenied => Code::PermissionDenied,
            BigQueryFakeCode::ResourceExhausted => Code::ResourceExhausted,
            BigQueryFakeCode::FailedPrecondition => Code::FailedPrecondition,
            BigQueryFakeCode::Aborted => Code::Aborted,
            BigQueryFakeCode::OutOfRange => Code::OutOfRange,
            BigQueryFakeCode::Unimplemented => Code::Unimplemented,
            BigQueryFakeCode::Internal => Code::Internal,
            BigQueryFakeCode::Unavailable => Code::Unavailable,
            BigQueryFakeCode::DataLoss => Code::DataLoss,
            BigQueryFakeCode::Unauthenticated => Code::Unauthenticated,
        }
    }
}

/// The RPCs a fault can target. `Query` and `InsertJob` are not among them: a query rule's
/// [`fails`](super::BigQueryFakeQueryBuilder::fails) scripts their failures.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum BigQueryFakeRpc {
    /// `JobService.GetQueryResults`, which waits for a query job.
    GetQueryResults,
    /// `JobService.GetJob`.
    GetJob,
    /// `JobService.CancelJob`.
    CancelJob,
    /// `BigQueryRead.CreateReadSession`.
    CreateReadSession,
    /// `BigQueryRead.ReadRows`.
    ReadRows,
    /// `BigQueryWrite.GetWriteStream`, which opens the default stream.
    GetWriteStream,
    /// `BigQueryWrite.CreateWriteStream`.
    CreateWriteStream,
    /// `BigQueryWrite.AppendRows`.
    AppendRows,
    /// `BigQueryWrite.FlushRows`.
    FlushRows,
    /// `BigQueryWrite.FinalizeWriteStream`.
    FinalizeWriteStream,
    /// `BigQueryWrite.BatchCommitWriteStreams`.
    BatchCommitWriteStreams,
    /// `TableService.GetTable`.
    GetTable,
    /// `TableService.InsertTable`.
    InsertTable,
    /// `TableService.PatchTable`.
    PatchTable,
    /// `TableService.UpdateTable`.
    UpdateTable,
    /// `TableService.DeleteTable`.
    DeleteTable,
    /// `TableService.ListTables`.
    ListTables,
    /// `DatasetService.GetDataset`.
    GetDataset,
    /// `DatasetService.InsertDataset`.
    InsertDataset,
    /// `DatasetService.UpdateDataset`.
    UpdateDataset,
    /// `DatasetService.DeleteDataset`.
    DeleteDataset,
    /// `DatasetService.ListDatasets`.
    ListDatasets,
}

impl BigQueryFakeRpc {
    /// Whether a call of this RPC belongs to one table, which a fault can be narrowed to.
    pub(super) fn names_a_table(self) -> bool {
        match self {
            BigQueryFakeRpc::CreateReadSession
            | BigQueryFakeRpc::ReadRows
            | BigQueryFakeRpc::GetWriteStream
            | BigQueryFakeRpc::CreateWriteStream
            | BigQueryFakeRpc::AppendRows
            | BigQueryFakeRpc::FlushRows
            | BigQueryFakeRpc::FinalizeWriteStream
            | BigQueryFakeRpc::BatchCommitWriteStreams
            | BigQueryFakeRpc::GetTable
            | BigQueryFakeRpc::InsertTable
            | BigQueryFakeRpc::PatchTable
            | BigQueryFakeRpc::UpdateTable
            | BigQueryFakeRpc::DeleteTable => true,
            BigQueryFakeRpc::GetQueryResults
            | BigQueryFakeRpc::GetJob
            | BigQueryFakeRpc::CancelJob
            | BigQueryFakeRpc::ListTables
            | BigQueryFakeRpc::GetDataset
            | BigQueryFakeRpc::InsertDataset
            | BigQueryFakeRpc::UpdateDataset
            | BigQueryFakeRpc::DeleteDataset
            | BigQueryFakeRpc::ListDatasets => false,
        }
    }
}

/// The `error_result` of a failed query job, which the client reads back as
/// [`BigQueryError::JobError`](crate::errors::BigQueryError::JobError).
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct BigQueryFakeJobFailure {
    /// BigQuery's short error code, such as `invalidQuery`.
    pub reason: String,
    /// The error message.
    pub message: String,
}

impl BigQueryFakeJobFailure {
    /// A failure of BigQuery's error code `reason`, such as `invalidQuery`, with `message`.
    pub fn new(reason: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            reason: reason.into(),
            message: message.into(),
        }
    }
}

/// Which calls a query rule matches.
pub(super) enum QueryMatcher {
    /// The SQL text, compared exactly.
    Exact(String),
    /// A test's predicate over the call.
    Matching(Box<dyn Fn(&BigQueryFakeQuery<'_>) -> bool + Send + Sync>),
}

impl QueryMatcher {
    /// Whether `query` matches.
    ///
    /// # Errors
    /// A panic of the test's predicate, as an internal failure of the fake: a server task that
    /// panics leaves its client waiting for an answer.
    fn matches(&self, query: &BigQueryFakeQuery<'_>) -> Result<bool, String> {
        match self {
            QueryMatcher::Exact(expected) => Ok(expected == query.sql()),
            QueryMatcher::Matching(predicate) => {
                catch_unwind(AssertUnwindSafe(|| predicate(query)))
                    .map_err(|_| format!("a when_query matcher panicked on {:?}", query.sql()))
            }
        }
    }
}

/// `query "SQL"` or `a when_query matcher`.
impl Display for QueryMatcher {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            QueryMatcher::Exact(sql) => write!(f, "query {sql:?}"),
            QueryMatcher::Matching(_) => f.write_str("a when_query matcher"),
        }
    }
}

impl Debug for QueryMatcher {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        Display::fmt(self, f)
    }
}

/// What a query rule answers with.
#[derive(Debug)]
pub(super) enum QueryAnswer {
    /// A result of `schema`, whose rows are `rows`.
    Rows {
        schema: BigQueryTableSchema,
        rows: RecordBatch,
    },
    /// A DML statement and the rows it changed.
    Dml {
        statement_type: BigQueryStatementType,
        stats: BigQueryDmlStats,
    },
    /// A statement without rows or DML counts, such as DDL.
    Statement(BigQueryStatementType),
    /// A refused call.
    Fault(BigQueryFakeFault),
    /// A job that runs and fails.
    JobFailure(BigQueryFakeJobFailure),
}

/// A query rule's answer, and the figures every purpose of the call reports.
#[derive(Debug)]
pub(super) struct QueryReply {
    pub answer: QueryAnswer,
    pub bytes_processed: Option<i64>,
}

/// One query rule.
pub(super) struct QueryRule {
    pub matcher: QueryMatcher,
    /// `None` answers a call whatever its parameters; otherwise named parameters must equal
    /// these as a set and positional ones in order.
    pub parameters: Option<Vec<QueryParameter>>,
    pub reply: Arc<QueryReply>,
    pub rule: BigQueryFakeRule,
}

impl QueryRule {
    fn matches(&self, sql: &str, parameters: &[QueryParameter]) -> Result<bool, String> {
        if !self
            .matcher
            .matches(&BigQueryFakeQuery::new(sql, parameters))?
        {
            return Ok(false);
        }
        let Some(expected) = &self.parameters else {
            return Ok(true);
        };
        let (expected_named, expected_positional): (Vec<_>, Vec<_>) = expected
            .iter()
            .partition(|parameter| !parameter.name.is_empty());
        let (named, positional): (Vec<_>, Vec<_>) = parameters
            .iter()
            .partition(|parameter| !parameter.name.is_empty());
        Ok(expected_positional == positional
            && expected_named
                .iter()
                .all(|parameter| named.contains(parameter))
            && named
                .iter()
                .all(|parameter| expected_named.contains(parameter)))
    }
}

/// One rule for read sessions.
pub(super) struct ReadRule {
    pub table: TableKey,
    /// `None` matches a session without a row restriction.
    pub row_restriction: Option<String>,
    pub rows: RecordBatch,
    pub rule: BigQueryFakeRule,
}

/// One fault rule.
pub(super) struct FaultRule {
    pub rpc: BigQueryFakeRpc,
    /// `None` matches every call of `rpc`.
    pub table: Option<TableKey>,
    pub fault: BigQueryFakeFault,
    pub rule: BigQueryFakeRule,
}

/// The indexes, within an appended batch, of the rows a `when_rows_rejected` predicate refuses,
/// or the error decoding them into the test's type.
pub(super) type RowRejection =
    Box<dyn Fn(&RecordBatch) -> BigQueryResult<Vec<usize>> + Send + Sync>;

/// One `when_rows_rejected` rule.
pub(super) struct RejectRule {
    pub table: TableKey,
    /// The message of each row error.
    pub reason: String,
    pub rejects: RowRejection,
    pub rule: BigQueryFakeRule,
}

/// Every rule a test registered, in registration order per kind.
#[derive(Default)]
pub(super) struct FakeRules {
    queries: Vec<QueryRule>,
    reads: Vec<ReadRule>,
    faults: Vec<FaultRule>,
    rejections: Vec<Arc<RejectRule>>,
}

impl FakeRules {
    pub(super) fn add_query(&mut self, rule: QueryRule) {
        self.queries.push(rule);
    }

    pub(super) fn add_read(&mut self, rule: ReadRule) {
        self.reads.push(rule);
    }

    pub(super) fn add_fault(&mut self, rule: FaultRule) {
        self.faults.push(rule);
    }

    pub(super) fn add_rejection(&mut self, rule: RejectRule) {
        self.rejections.push(Arc::new(rule));
    }

    /// The reply of the first query rule with calls left that matches, counting the call.
    ///
    /// # Errors
    /// An internal failure of the fake, such as a panicking `when_query` matcher.
    pub(super) fn answer_query(
        &self,
        sql: &str,
        parameters: &[QueryParameter],
    ) -> Result<Option<Arc<QueryReply>>, String> {
        for rule in &self.queries {
            if rule.matches(sql, parameters)? && rule.rule.claim() {
                return Ok(Some(rule.reply.clone()));
            }
        }
        Ok(None)
    }

    /// The rows of the first read rule with calls left for `table` and exactly
    /// `row_restriction`, counting the call.
    pub(super) fn answer_read(
        &self,
        table: &TableKey,
        row_restriction: Option<&str>,
    ) -> Option<RecordBatch> {
        self.reads
            .iter()
            .find(|rule| {
                rule.table == *table
                    && rule.row_restriction.as_deref() == row_restriction
                    && rule.rule.claim()
            })
            .map(|rule| rule.rows.clone())
    }

    /// The fault of the first fault rule with calls left for `rpc` on `table`, counting the
    /// call. Faults are tried before any other rule and before state.
    pub(super) fn answer_fault(
        &self,
        rpc: BigQueryFakeRpc,
        table: Option<&TableKey>,
    ) -> Option<BigQueryFakeFault> {
        self.faults
            .iter()
            .find(|rule| {
                rule.rpc == rpc
                    && rule.table.as_ref().is_none_or(|only| Some(only) == table)
                    && rule.rule.claim()
            })
            .map(|rule| rule.fault.clone())
    }

    /// The `when_rows_rejected` rules of `table`, in registration order, to apply outside the
    /// lock.
    pub(super) fn rejections(&self, table: &TableKey) -> Vec<Arc<RejectRule>> {
        self.rejections
            .iter()
            .filter(|rule| rule.table == *table)
            .cloned()
            .collect()
    }

    /// The query rules, as the unmatched report lists them.
    pub(super) fn describe_queries(&self) -> Vec<String> {
        Self::describe(self.queries.iter().map(|rule| &rule.rule))
    }

    /// The read rules, as the unmatched report lists them.
    pub(super) fn describe_reads(&self) -> Vec<String> {
        Self::describe(self.reads.iter().map(|rule| &rule.rule))
    }

    fn describe<'r>(rules: impl Iterator<Item = &'r BigQueryFakeRule>) -> Vec<String> {
        rules.map(BigQueryFakeRule::listed).collect()
    }

    /// Every `times(n)` rule that did not answer exactly n calls.
    pub(super) fn shortfalls(&self) -> Vec<String> {
        let queries = self.queries.iter().map(|rule| &rule.rule);
        let reads = self.reads.iter().map(|rule| &rule.rule);
        let faults = self.faults.iter().map(|rule| &rule.rule);
        queries
            .chain(reads)
            .chain(faults)
            .filter_map(BigQueryFakeRule::shortfall)
            .collect()
    }
}

/// Query parameters as the unmatched report shows them: `@name = value` or `?n = value`, with
/// arrays in brackets and structs in braces.
pub(super) struct ShownParameters<'p>(pub &'p [QueryParameter]);

impl Display for ShownParameters<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if self.0.is_empty() {
            return f.write_str("no parameters");
        }
        for (index, parameter) in self.0.iter().enumerate() {
            if index > 0 {
                f.write_str(", ")?;
            }
            if parameter.name.is_empty() {
                write!(f, "?{} = ", index + 1)?;
            } else {
                write!(f, "@{} = ", parameter.name)?;
            }
            match &parameter.parameter_value {
                Some(value) => ShownValue(value).fmt(f)?,
                None => f.write_str("NULL")?,
            }
        }
        Ok(())
    }
}

struct ShownValue<'v>(&'v QueryParameterValue);

impl Display for ShownValue<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let value = self.0;
        if let Some(scalar) = &value.value {
            return write!(f, "{scalar:?}");
        }
        if !value.array_values.is_empty() {
            f.write_str("[")?;
            for (index, element) in value.array_values.iter().enumerate() {
                if index > 0 {
                    f.write_str(", ")?;
                }
                ShownValue(element).fmt(f)?;
            }
            return f.write_str("]");
        }
        if !value.struct_values.is_empty() {
            let mut fields: Vec<_> = value.struct_values.iter().collect();
            fields.sort_by_key(|(name, _)| *name);
            f.write_str("{")?;
            for (index, (name, field)) in fields.into_iter().enumerate() {
                if index > 0 {
                    f.write_str(", ")?;
                }
                write!(f, "{name}: ")?;
                ShownValue(field).fmt(f)?;
            }
            return f.write_str("}");
        }
        if let Some(range) = &value.range_value {
            let bound = |bound: &Option<Box<QueryParameterValue>>| {
                bound.as_deref().map_or_else(
                    || "UNBOUNDED".to_string(),
                    |bound| ShownValue(bound).to_string(),
                )
            };
            return write!(f, "[{}, {})", bound(&range.start), bound(&range.end));
        }
        f.write_str("NULL")
    }
}