use arrow::record_batch::RecordBatch;
use async_trait::async_trait;
use futures_util::stream::{BoxStream, StreamExt};
use std::path::Path;
use crate::{
connectors::arrow_values::record_batch_to_rows,
schema::{DataType, Row},
Result,
};
#[derive(Debug, Clone, PartialEq)]
pub struct ColumnInfo {
pub name: String,
pub data_type: DataType,
pub nullable: bool,
}
pub struct QueryStream {
pub columns: Vec<ColumnInfo>,
pub total_rows: Option<u64>,
pub batches: BoxStream<'static, Result<RecordBatch>>,
}
#[async_trait]
pub trait SnowflakeClient: Send + Sync {
async fn execute(&self, sql: &str, binds: Vec<Option<String>>) -> Result<()>;
async fn query(&self, sql: &str) -> Result<QueryStream>;
async fn put(&self, local_file: &Path, stage_location: &str) -> Result<()>;
async fn close(&self) -> Result<()>;
}
pub async fn query_rows(client: &dyn SnowflakeClient, sql: &str) -> Result<Vec<Row>> {
let mut result = client.query(sql).await?;
let mut rows = Vec::new();
while let Some(batch) = result.batches.next().await {
rows.extend(record_batch_to_rows(&batch?)?);
}
Ok(rows)
}