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
//! Support for `bigquery-schema` locators.

use std::{fmt, str::FromStr};

use crate::common::*;
use crate::drivers::bigquery_shared::{BqColumn, BqTable, TableName, Usage};

/// A JSON file containing BigQuery table schema.
#[derive(Clone, Debug)]
pub struct BigQuerySchemaLocator {
    path: PathOrStdio,
}

impl fmt::Display for BigQuerySchemaLocator {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.path.fmt_locator_helper(Self::scheme(), f)
    }
}

impl FromStr for BigQuerySchemaLocator {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        let path = PathOrStdio::from_str_locator_helper(Self::scheme(), s)?;
        Ok(BigQuerySchemaLocator { path })
    }
}

impl Locator for BigQuerySchemaLocator {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn schema(&self, ctx: Context) -> BoxFuture<Option<Table>> {
        schema_helper(ctx, self.to_owned()).boxed()
    }

    fn write_schema(
        &self,
        ctx: Context,
        table: Table,
        if_exists: IfExists,
    ) -> BoxFuture<()> {
        write_schema_helper(ctx, self.to_owned(), table, if_exists).boxed()
    }
}

impl LocatorStatic for BigQuerySchemaLocator {
    fn scheme() -> &'static str {
        "bigquery-schema:"
    }

    fn features() -> Features {
        Features {
            locator: LocatorFeatures::SCHEMA | LocatorFeatures::WRITE_SCHEMA,
            write_schema_if_exists: IfExistsFeatures::no_append(),
            source_args: SourceArgumentsFeatures::empty(),
            dest_args: DestinationArgumentsFeatures::empty(),
            dest_if_exists: IfExistsFeatures::empty(),
            _placeholder: (),
        }
    }
}

/// Implementation of `schema`, but as a real `async` function.
async fn schema_helper(
    _ctx: Context,
    source: BigQuerySchemaLocator,
) -> Result<Option<Table>> {
    // Read our input.
    let input = source.path.open_async().await?;
    let data = async_read_to_end(input)
        .await
        .with_context(|_| format!("error reading {}", source.path))?;

    // Parse our input as a list of columns.
    let columns: Vec<BqColumn> = serde_json::from_slice(&data)
        .with_context(|_| format!("error parsing {}", source.path))?;

    // Build a `BqTable`, convert it, and set a placeholder name.
    let arbitrary_name = TableName::from_str(&"unused:unused.unused")?;
    let bq_table = BqTable {
        name: arbitrary_name,
        columns,
    };
    let mut table = bq_table.to_table()?;
    table.name = "unnamed".to_owned();
    Ok(Some(table))
}

/// Implementation of `write_schema`, but as a real `async` function.
async fn write_schema_helper(
    ctx: Context,
    dest: BigQuerySchemaLocator,
    table: Table,
    if_exists: IfExists,
) -> Result<()> {
    // The BigQuery table name doesn't matter here, because our BigQuery schema
    // won't use it. We could convert `table.name` into a valid BigQuery table
    // name, but because BigQuery table names obey fairly strict restrictions,
    // it's not worth doing the work if we're just going throw it away.
    let arbitrary_name = TableName::from_str(&"unused:unused.unused")?;

    // Convert our schema to a BigQuery table.
    let bq_table = BqTable::for_table_name_and_columns(
        arbitrary_name,
        &table.columns,
        Usage::FinalTable,
    )?;

    // Output our schema to our destination.
    let f = dest.path.create_async(ctx, if_exists).await?;
    buffer_sync_write_and_copy_to_async(f, |buff| bq_table.write_json_schema(buff))
        .await
        .with_context(|_| format!("error writing to {}", dest.path))?;
    Ok(())
}