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
//! Jobs: get, delete and list, through the v2 `JobService`, next to
//! [`BigQueryDb::cancel_job`].

use crate::admin::{logging_errors, paged};
use crate::db::proto::timestamp_ms;
use crate::errors::{BigQueryError, BigQueryJobErrorEntry};
use crate::BigQueryInstant;
use crate::{BigQueryDb, BigQueryJobRef, BigQueryResult, BigQueryStatementType};
use crate::{BigQueryJobId, BigQueryLabels};
use futures::stream::BoxStream;
use gcloud_sdk::google::cloud::bigquery::v2;
use rsb_derive::Builder;
use std::fmt::{Display, Formatter};
use tracing::Span;

/// Where a job is in its run, as BigQuery names it in `JobStatus.state`: `PENDING`, `RUNNING`
/// or `DONE`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum BigQueryJobState {
    /// Waiting to run.
    Pending,
    /// Running.
    Running,
    /// Finished, with or without an error.
    Done,
    /// A state this crate does not know, as BigQuery named it.
    Other(String),
}

impl BigQueryJobState {
    /// The name as BigQuery writes it, such as `RUNNING`.
    pub fn as_str(&self) -> &str {
        match self {
            Self::Pending => "PENDING",
            Self::Running => "RUNNING",
            Self::Done => "DONE",
            Self::Other(name) => name,
        }
    }

    fn known(name: &str) -> Option<Self> {
        Some(match name {
            "PENDING" => Self::Pending,
            "RUNNING" => Self::Running,
            "DONE" => Self::Done,
            _ => return None,
        })
    }
}

impl From<&str> for BigQueryJobState {
    fn from(name: &str) -> Self {
        Self::known(name).unwrap_or_else(|| Self::Other(name.to_string()))
    }
}

impl From<String> for BigQueryJobState {
    fn from(name: String) -> Self {
        Self::known(&name).unwrap_or(Self::Other(name))
    }
}

impl Display for BigQueryJobState {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// What kind of work a job does, as BigQuery names it in `JobConfiguration.job_type`: `QUERY`,
/// `LOAD`, `EXTRACT`, `COPY` or `UNKNOWN`.
///
/// A name BigQuery adds later reads as [`Other`](Self::Other) with BigQuery's text, so
/// [`as_str`](Self::as_str) gives back what BigQuery sent for every value.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum BigQueryJobType {
    /// A query.
    Query,
    /// A load into a table.
    Load,
    /// An extract from a table to Cloud Storage.
    Extract,
    /// A table copy.
    Copy,
    /// BigQuery's own `UNKNOWN`, which it reports for a job whose kind it cannot tell.
    Unknown,
    /// A type this crate does not know, as BigQuery named it.
    Other(String),
}

impl BigQueryJobType {
    /// The name as BigQuery writes it, such as `QUERY`.
    pub fn as_str(&self) -> &str {
        match self {
            Self::Query => "QUERY",
            Self::Load => "LOAD",
            Self::Extract => "EXTRACT",
            Self::Copy => "COPY",
            Self::Unknown => "UNKNOWN",
            Self::Other(name) => name,
        }
    }

    fn known(name: &str) -> Option<Self> {
        Some(match name {
            "QUERY" => Self::Query,
            "LOAD" => Self::Load,
            "EXTRACT" => Self::Extract,
            "COPY" => Self::Copy,
            "UNKNOWN" => Self::Unknown,
            _ => return None,
        })
    }
}

impl From<&str> for BigQueryJobType {
    fn from(name: &str) -> Self {
        Self::known(name).unwrap_or_else(|| Self::Other(name.to_string()))
    }
}

impl From<String> for BigQueryJobType {
    fn from(name: String) -> Self {
        Self::known(&name).unwrap_or(Self::Other(name))
    }
}

impl Display for BigQueryJobType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// A job as `GetJob` or `ListJobs` returns it.
///
/// A job that failed is still a job: its failure is in [`error`](Self::error), not an `Err`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BigQueryJob {
    /// The job.
    pub reference: BigQueryJobRef,
    /// What kind of work the job does.
    pub job_type: Option<BigQueryJobType>,
    /// Where the job is in its run.
    pub state: Option<BigQueryJobState>,
    /// Why a finished job failed.
    pub error: Option<BigQueryJobErrorEntry>,
    /// Who ran the job.
    pub user_email: Option<String>,
    /// The job's labels.
    pub labels: BigQueryLabels,
    /// For a query, the kind of statement it ran.
    pub statement_type: Option<BigQueryStatementType>,
    /// When the job was created.
    pub creation_time: Option<BigQueryInstant>,
    /// When the job started running.
    pub start_time: Option<BigQueryInstant>,
    /// When the job finished.
    pub end_time: Option<BigQueryInstant>,
    /// The bytes the job processed.
    pub total_bytes_processed: Option<i64>,
    /// For a query, the bytes billed.
    pub total_bytes_billed: Option<i64>,
}

/// The parts `Job` and `ListFormatJob` share, so both convert one way.
struct JobParts {
    reference: Option<v2::JobReference>,
    configuration: Option<v2::JobConfiguration>,
    statistics: Option<v2::JobStatistics>,
    status: Option<v2::JobStatus>,
    user_email: String,
}

impl TryFrom<JobParts> for BigQueryJob {
    type Error = BigQueryError;

    fn try_from(job: JobParts) -> Result<Self, Self::Error> {
        let reference = job
            .reference
            .ok_or_else(|| {
                BigQueryError::unexpected_response("BigQuery returned no job_reference")
            })?
            .into();
        let configuration = job.configuration.unwrap_or_default();
        let statistics = job.statistics.unwrap_or_default();
        let query = statistics.query.clone().unwrap_or_default();
        let status = job.status.unwrap_or_default();
        Ok(Self {
            reference,
            job_type: Some(configuration.job_type)
                .filter(|v| !v.is_empty())
                .map(Into::into),
            state: Some(status.state).filter(|v| !v.is_empty()).map(Into::into),
            error: status.error_result.map(Into::into),
            user_email: Some(job.user_email).filter(|v| !v.is_empty()),
            labels: configuration.labels.into_iter().collect(),
            statement_type: Some(query.statement_type)
                .filter(|v| !v.is_empty())
                .map(Into::into),
            creation_time: timestamp_ms("creation_time", statistics.creation_time)?,
            start_time: timestamp_ms("start_time", statistics.start_time)?,
            end_time: timestamp_ms("end_time", statistics.end_time)?,
            total_bytes_processed: statistics.total_bytes_processed,
            total_bytes_billed: query.total_bytes_billed,
        })
    }
}

/// # Errors
/// [`BigQueryError::SystemError`] for a missing job reference, and
/// [`BigQueryError::DeserializeError`] for a time out of range.
impl TryFrom<v2::Job> for BigQueryJob {
    type Error = BigQueryError;

    fn try_from(job: v2::Job) -> Result<Self, Self::Error> {
        JobParts {
            reference: job.job_reference,
            configuration: job.configuration,
            statistics: job.statistics,
            status: job.status,
            user_email: job.user_email,
        }
        .try_into()
    }
}

/// # Errors
/// As for a `Job`.
impl TryFrom<v2::ListFormatJob> for BigQueryJob {
    type Error = BigQueryError;

    fn try_from(job: v2::ListFormatJob) -> Result<Self, Self::Error> {
        JobParts {
            reference: job.job_reference,
            configuration: job.configuration,
            statistics: job.statistics,
            status: job.status,
            user_email: job.user_email,
        }
        .try_into()
    }
}

/// Which jobs [`BigQueryDb::stream_jobs`] lists, newest first.
#[derive(Debug, PartialEq, Clone, Builder)]
pub struct BigQueryListJobsParams {
    /// The project whose jobs are listed. Unset, the client's
    /// [`google_project_id`](crate::BigQueryDbOptions::google_project_id).
    pub project_id: Option<String>,
    /// Every user's jobs rather than only the caller's, which needs the Owner role on the
    /// project.
    #[default = "false"]
    pub all_users: bool,
    /// Only jobs created at or after this time.
    pub min_creation_time: Option<BigQueryInstant>,
    /// Only jobs created at or before this time.
    pub max_creation_time: Option<BigQueryInstant>,
    /// Only jobs in one of these states; empty is every state. [`Other`](BigQueryJobState::Other) cannot be sent.
    #[default = "Vec::new()"]
    pub states: Vec<BigQueryJobState>,
    /// Only the child jobs of this script job.
    pub parent_job_id: Option<BigQueryJobId>,
    /// How many jobs one `ListJobs` call returns; unset is BigQuery's default.
    pub page_size: Option<u32>,
}

impl BigQueryDb {
    /// Reads `job`.
    ///
    /// # Errors
    /// [`DataNotFoundError`](BigQueryError::DataNotFoundError) for a job that does not exist,
    /// or that is looked up in the wrong `location`.
    pub async fn get_job(&self, job: &BigQueryJobRef) -> BigQueryResult<BigQueryJob> {
        let request = v2::GetJobRequest {
            project_id: job.project_id.clone(),
            job_id: job.job_id.to_string(),
            location: job.location_field(),
        };
        self.retry(&job.admin_span(), "get a job", &request, |r| {
            let mut client = self.job_client();
            async move { client.get_job(r).await }
        })
        .await?
        .try_into()
    }

    /// Deletes the metadata of a finished `job`, such as a failed query's SQL that should not
    /// stay in the job history. It does not cancel a running job: see
    /// [`cancel_job`](Self::cancel_job).
    ///
    /// # Errors
    /// What BigQuery returns for a job that is still running, and
    /// [`DataNotFoundError`](BigQueryError::DataNotFoundError) for a missing one.
    pub async fn delete_job(&self, job: &BigQueryJobRef) -> BigQueryResult<()> {
        let request = v2::DeleteJobRequest {
            project_id: job.project_id.clone(),
            job_id: job.job_id.to_string(),
            location: job.location_field(),
        };
        self.retry(&job.admin_span(), "delete a job", &request, |r| {
            let mut client = self.job_client();
            async move { client.delete_job(r).await }
        })
        .await
    }

    /// Streams the jobs `params` selects, newest first, fetching each page as the stream
    /// reaches it. A failed page is logged and ends the stream; see
    /// [`stream_jobs_with_errors`](Self::stream_jobs_with_errors) to see it.
    ///
    /// # Errors
    /// [`InvalidParametersError`](BigQueryError::InvalidParametersError) for a creation time
    /// before 1970 or an [`Other`](BigQueryJobState::Other) state, before any request.
    pub async fn stream_jobs<'b>(
        &self,
        params: BigQueryListJobsParams,
    ) -> BigQueryResult<BoxStream<'b, BigQueryJob>> {
        Ok(logging_errors(
            self.stream_jobs_with_errors(params).await?,
            "jobs",
        ))
    }

    /// Like [`stream_jobs`](Self::stream_jobs), with a failed page as the stream's last item.
    ///
    /// # Errors
    /// As for [`stream_jobs`](Self::stream_jobs).
    pub async fn stream_jobs_with_errors<'b>(
        &self,
        params: BigQueryListJobsParams,
    ) -> BigQueryResult<BoxStream<'b, BigQueryResult<BigQueryJob>>> {
        let template = params.into_request(&self.options().google_project_id)?;
        let db = self.clone();
        let span = tracing::debug_span!(
            "BigQuery jobs",
            "/bigquery/project" = template.project_id.as_str()
        );
        Ok(paged(move |page_token| {
            let db = db.clone();
            let span = span.clone();
            let request = v2::ListJobsRequest {
                page_token,
                ..template.clone()
            };
            async move {
                let page = db
                    .retry(&span, "list jobs", &request, |r| {
                        let mut client = db.job_client();
                        async move { client.list_jobs(r).await }
                    })
                    .await?;
                let jobs = page
                    .jobs
                    .into_iter()
                    .map(TryInto::try_into)
                    .collect::<BigQueryResult<_>>()?;
                Ok((jobs, page.next_page_token))
            }
        }))
    }
}

impl BigQueryJobRef {
    /// The span of one job admin call.
    fn admin_span(&self) -> Span {
        tracing::debug_span!("BigQuery job", "/bigquery/job_id" = self.job_id.as_str())
    }
}

/// Milliseconds since the epoch, as `ListJobs` takes its creation-time bounds.
fn creation_bound(field: &str, at: BigQueryInstant) -> BigQueryResult<u64> {
    u64::try_from(at.as_millisecond())
        .map_err(|_| BigQueryError::invalid_parameters(field, format!("{at} is before 1970")))
}

impl BigQueryListJobsParams {
    /// The first page's request, with `default_project_id` for an unset project.
    fn into_request(self, default_project_id: &str) -> BigQueryResult<v2::ListJobsRequest> {
        use v2::list_jobs_request::{Projection, StateFilter};
        let state_filter = self
            .states
            .into_iter()
            .map(|state| match state {
                BigQueryJobState::Pending => Ok(StateFilter::Pending.into()),
                BigQueryJobState::Running => Ok(StateFilter::Running.into()),
                BigQueryJobState::Done => Ok(StateFilter::Done.into()),
                BigQueryJobState::Other(name) => Err(BigQueryError::invalid_parameters(
                    "states",
                    format!("{name:?} is not a state ListJobs filters on"),
                )),
            })
            .collect::<BigQueryResult<_>>()?;
        Ok(v2::ListJobsRequest {
            project_id: self
                .project_id
                .unwrap_or_else(|| default_project_id.to_string()),
            all_users: self.all_users,
            max_results: self.page_size.map(|n| i32::try_from(n).unwrap_or(i32::MAX)),
            min_creation_time: self
                .min_creation_time
                .map(|at| creation_bound("min_creation_time", at))
                .transpose()?
                .unwrap_or(0),
            max_creation_time: self
                .max_creation_time
                .map(|at| creation_bound("max_creation_time", at))
                .transpose()?,
            page_token: String::new(),
            projection: Projection::Full.into(),
            state_filter,
            parent_job_id: self
                .parent_job_id
                .map(|id| id.to_string())
                .unwrap_or_default(),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn job_types_map_known_names_and_keep_unknown_ones() {
        for name in ["QUERY", "LOAD", "EXTRACT", "COPY", "UNKNOWN"] {
            let parsed = BigQueryJobType::from(name);
            assert!(
                !matches!(parsed, BigQueryJobType::Other(_)),
                "{name} reads as {parsed:?}"
            );
            assert_eq!(parsed.as_str(), name);
        }
        let unknown = BigQueryJobType::from("SNAPSHOT".to_string());
        assert_eq!(unknown, BigQueryJobType::Other("SNAPSHOT".into()));
        assert_eq!(unknown.to_string(), "SNAPSHOT");
    }

    #[test]
    fn job_states_map_known_names_and_keep_unknown_ones() {
        for name in ["PENDING", "RUNNING", "DONE"] {
            let parsed = BigQueryJobState::from(name);
            assert!(
                !matches!(parsed, BigQueryJobState::Other(_)),
                "{name} reads as {parsed:?}"
            );
            assert_eq!(parsed.as_str(), name);
        }
        let unknown = BigQueryJobState::from("PAUSED".to_string());
        assert_eq!(unknown, BigQueryJobState::Other("PAUSED".into()));
        assert_eq!(unknown.to_string(), "PAUSED");
    }
}