use crate::bigquery_api::main::query_schema::query_schema;
use crate::bigquery_source::_helpers::catalog::catalog_tables;
use crate::bigquery_source::_helpers::query::subquery;
use crate::bigquery_source::_helpers::type_map::source_column;
use crate::bigquery_source::errors::BigQuerySourceError;
use crate::bigquery_source::models::BigQueryReader;
use crate::postgres_source::models::SourceTable;
pub async fn read_catalog(
reader: &BigQueryReader,
(include, exclude): (&[String], &[String]),
queries: &[(String, String)],
) -> Result<Vec<SourceTable>, BigQuerySourceError> {
let mut tables: Vec<SourceTable> = match include.is_empty() {
true => Vec::new(),
false => catalog_tables(reader, (include, exclude)).await?,
};
for (name, sql) in queries {
let fields = query_schema(&reader.client, &format!("SELECT * FROM {}", subquery(sql)))
.await
.map_err(|error| {
BigQuerySourceError::Invalid(format!("query table {name}: {error}"))
})?;
tables.push(SourceTable {
split_pages: None,
schema: String::new(),
name: name.clone(),
columns: fields.iter().map(source_column).collect(),
primary_key: Vec::new(),
indexed_columns: Vec::new(),
estimated_rows: -1,
estimated_bytes: -1,
comment: None,
listed_keys: Vec::new(),
});
}
Ok(tables)
}