use crate::db::proto::millis;
#[cfg(doc)]
use crate::errors::BigQueryError;
use crate::BigQueryResult;
use crate::{
BigQueryDatasetRef, BigQueryFieldType, BigQueryReadOptions, BigQueryStatementType,
BigQueryTableRef, BigQueryTableSchema,
};
use crate::{BigQueryJobId, BigQueryLabels, BigQueryLocation, BigQueryQueryId, BigQueryRequestId};
use gcloud_sdk::google::cloud::bigquery::v2::QueryParameter;
use rsb_derive::Builder;
use std::time::Duration;
#[derive(Debug, PartialEq, Clone, Builder)]
pub struct BigQueryQueryParams {
pub sql: String,
#[default = "Vec::new()"]
pub(crate) query_parameters: Vec<QueryParameter>,
pub location: Option<BigQueryLocation>,
pub default_dataset: Option<BigQueryDatasetRef>,
#[default = "BigQueryLabels::new()"]
pub labels: BigQueryLabels,
pub maximum_bytes_billed: Option<i64>,
pub use_query_cache: Option<bool>,
pub timeout: Option<Duration>,
pub job_timeout: Option<Duration>,
pub request_id: Option<BigQueryRequestId>,
pub inline_rows_limit: Option<u32>,
#[default = "BigQueryReadOptions::new()"]
pub read_options: BigQueryReadOptions,
#[default = "BigQueryJobCreation::Optional"]
pub job_creation: BigQueryJobCreation,
pub destination: Option<BigQueryQueryDestination>,
}
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(10);
impl BigQueryQueryParams {
pub(crate) fn timeout_ms(&self) -> BigQueryResult<u32> {
millis("timeout", self.timeout.unwrap_or(DEFAULT_TIMEOUT))
}
pub(crate) fn job_timeout_ms(&self) -> BigQueryResult<Option<i64>> {
self.job_timeout
.map(|d| millis("job_timeout", d))
.transpose()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BigQueryJobCreation {
#[default]
Optional,
Required,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BigQueryQueryDestination {
pub table: BigQueryTableRef,
pub write: BigQueryDestinationWrite,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum BigQueryDestinationWrite {
#[default]
IfEmpty,
Append,
Overwrite,
}
impl BigQueryDestinationWrite {
pub(crate) fn disposition(self) -> &'static str {
match self {
Self::IfEmpty => "WRITE_EMPTY",
Self::Append => "WRITE_APPEND",
Self::Overwrite => "WRITE_TRUNCATE",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BigQueryParamType {
pub field_type: BigQueryFieldType,
pub repeated: bool,
}
impl From<BigQueryFieldType> for BigQueryParamType {
fn from(field_type: BigQueryFieldType) -> Self {
Self {
field_type,
repeated: false,
}
}
}
impl BigQueryParamType {
pub fn array_of(element: BigQueryFieldType) -> Self {
Self {
field_type: element,
repeated: true,
}
}
}
pub type BigQueryQueryOutcome = BigQueryJobStats;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct BigQueryJobStats {
pub job: Option<BigQueryJobRef>,
pub query_id: Option<BigQueryQueryId>,
pub statement_type: Option<BigQueryStatementType>,
pub total_rows: Option<u64>,
pub total_bytes_processed: Option<i64>,
pub total_bytes_billed: Option<i64>,
pub total_slot_ms: Option<i64>,
pub cache_hit: Option<bool>,
pub num_dml_affected_rows: Option<i64>,
pub dml_stats: Option<BigQueryDmlStats>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BigQueryDmlStats {
pub inserted: i64,
pub updated: i64,
pub deleted: i64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BigQueryDryRunResult {
pub total_bytes_processed: Option<i64>,
pub schema: Option<BigQueryTableSchema>,
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct BigQueryJobRef {
pub project_id: String,
pub job_id: BigQueryJobId,
pub location: Option<BigQueryLocation>,
}
impl BigQueryJobRef {
pub(crate) fn location_field(&self) -> String {
self.location
.as_ref()
.map(ToString::to_string)
.unwrap_or_default()
}
}
impl From<gcloud_sdk::google::cloud::bigquery::v2::JobReference> for BigQueryJobRef {
fn from(job: gcloud_sdk::google::cloud::bigquery::v2::JobReference) -> Self {
Self {
project_id: job.project_id,
job_id: BigQueryJobId::reported(job.job_id),
location: job.location.and_then(BigQueryLocation::reported),
}
}
}
impl From<BigQueryDmlStats> for gcloud_sdk::google::cloud::bigquery::v2::DmlStats {
fn from(stats: BigQueryDmlStats) -> Self {
Self {
inserted_row_count: Some(stats.inserted),
deleted_row_count: Some(stats.deleted),
updated_row_count: Some(stats.updated),
..Default::default()
}
}
}
impl From<gcloud_sdk::google::cloud::bigquery::v2::DmlStats> for BigQueryDmlStats {
fn from(stats: gcloud_sdk::google::cloud::bigquery::v2::DmlStats) -> Self {
Self {
inserted: stats.inserted_row_count.unwrap_or(0),
updated: stats.updated_row_count.unwrap_or(0),
deleted: stats.deleted_row_count.unwrap_or(0),
}
}
}