use crate::types::{TypedValue, ValueKind};
use crate::ast::ArgValue;
use std::error::Error;
use std::fmt;
#[derive(Debug)]
pub enum InterchangeError {
ConversionError(String),
UnsupportedType(String),
}
impl fmt::Display for InterchangeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
InterchangeError::ConversionError(msg) => write!(f, "Conversion error: {}", msg),
InterchangeError::UnsupportedType(msg) => write!(f, "Unsupported type: {}", msg),
}
}
}
impl Error for InterchangeError {}
#[cfg(feature = "duckdb")]
pub fn record_batch_to_table(
_batch: duckdb::arrow::record_batch::RecordBatch,
) -> Result<TypedValue, Box<dyn Error>> {
Ok(TypedValue::new(ValueKind::Table, ArgValue::Table(Vec::new())))
}
#[cfg(feature = "duckdb")]
pub fn table_to_record_batch(
_table: &TypedValue,
) -> Result<duckdb::arrow::record_batch::RecordBatch, Box<dyn Error>> {
use duckdb::arrow::array::ArrayRef;
use duckdb::arrow::datatypes::{Field, Schema};
use std::sync::Arc;
let schema = Arc::new(Schema::new(Vec::<Field>::new()));
let columns = Vec::<ArrayRef>::new();
Ok(duckdb::arrow::record_batch::RecordBatch::try_new(schema, columns)?)
}
use duckdb::arrow::record_batch::RecordBatch as DuckDBRecordBatch;
pub fn record_batch_to_typed_value(_batch: DuckDBRecordBatch) -> Result<TypedValue, Box<dyn Error>> {
Ok(TypedValue::new(ValueKind::Table, ArgValue::Table(vec![])))
}
pub fn typed_value_to_record_batch(_value: &TypedValue) -> Result<DuckDBRecordBatch, Box<dyn Error>> {
Err("Conversion from TypedValue to RecordBatch not yet implemented".into())
}
pub fn extract_schema(_batch: &DuckDBRecordBatch) -> Result<(), Box<dyn Error>> {
Ok(())
}
pub fn duckdb_value_to_typed_value(value: &duckdb::types::Value) -> Result<TypedValue, Box<dyn Error>> {
match value {
duckdb::types::Value::Null => Ok(TypedValue::new(ValueKind::String, ArgValue::String("null".to_string()))),
duckdb::types::Value::Boolean(b) => Ok(TypedValue::new(ValueKind::String, ArgValue::String(b.to_string()))),
duckdb::types::Value::Int(i) => Ok(TypedValue::new(ValueKind::String, ArgValue::String(i.to_string()))),
duckdb::types::Value::BigInt(i) => Ok(TypedValue::new(ValueKind::String, ArgValue::String(i.to_string()))),
duckdb::types::Value::Float(f) => Ok(TypedValue::new(ValueKind::String, ArgValue::String(f.to_string()))),
duckdb::types::Value::Double(d) => Ok(TypedValue::new(ValueKind::String, ArgValue::String(d.to_string()))),
duckdb::types::Value::Text(s) => Ok(TypedValue::new(ValueKind::String, ArgValue::String(s.clone()))),
_ => Err(format!("Unsupported DuckDB value type: {:?}", value).into()),
}
}