use std::path::PathBuf;
use std::fs::File;
use std::io::{BufReader, BufWriter, Write};
use std::collections::HashMap;
use async_trait::async_trait;
use apache_avro::{
Reader, Writer, Schema as AvroSchema, from_value, to_value,
types::Value as AvroValue, to_avro_datum,
};
use serde_json::{Value as JsonValue, json};
use rust_decimal::Decimal;
use crate::{
Result, TinyEtlError,
schema::{Schema, Row, Value, Column, DataType, SchemaInferer},
connectors::{Source, Target},
};
pub struct AvroSource {
file_path: PathBuf,
current_position: usize,
total_records: Option<usize>,
has_more: bool,
}
impl AvroSource {
pub fn new(file_path: &str) -> Result<Self> {
Ok(Self {
file_path: PathBuf::from(file_path),
current_position: 0,
total_records: None,
has_more: true,
})
}
fn avro_type_to_schema_type(avro_type: &JsonValue) -> DataType {
match avro_type {
JsonValue::String(type_name) => match type_name.as_str() {
"string" => DataType::String,
"int" | "long" => DataType::Integer,
"float" | "double" => DataType::Decimal,
"boolean" => DataType::Boolean,
"date" => DataType::Date,
"timestamp-millis" | "timestamp-micros" => DataType::DateTime,
_ => DataType::String, },
JsonValue::Array(union_types) => {
for union_type in union_types {
if let JsonValue::String(type_str) = union_type {
if type_str != "null" {
return Self::avro_type_to_schema_type(union_type);
}
}
}
DataType::String },
JsonValue::Object(obj) => {
if let Some(logical_type) = obj.get("logicalType") {
match logical_type.as_str().unwrap_or("") {
"date" => DataType::Date,
"timestamp-millis" | "timestamp-micros" => DataType::DateTime,
_ => DataType::String,
}
} else if let Some(type_val) = obj.get("type") {
Self::avro_type_to_schema_type(type_val)
} else {
DataType::String
}
},
_ => DataType::String,
}
}
fn is_nullable(avro_type: &JsonValue) -> bool {
match avro_type {
JsonValue::Array(union_types) => {
union_types.iter().any(|t| {
if let JsonValue::String(type_str) = t {
type_str == "null"
} else {
false
}
})
},
_ => false,
}
}
fn avro_value_to_value(avro_value: &AvroValue) -> Result<Value> {
match avro_value {
AvroValue::Null => Ok(Value::Null),
AvroValue::Boolean(b) => Ok(Value::Boolean(*b)),
AvroValue::Int(i) => Ok(Value::Integer(*i as i64)),
AvroValue::Long(l) => Ok(Value::Integer(*l)),
AvroValue::Float(f) => {
match Decimal::try_from(*f as f64) {
Ok(d) => Ok(Value::Decimal(d)),
Err(_) => Ok(Value::String(f.to_string())),
}
},
AvroValue::Double(d) => {
match Decimal::try_from(*d) {
Ok(decimal) => Ok(Value::Decimal(decimal)),
Err(_) => Ok(Value::String(d.to_string())),
}
},
AvroValue::Bytes(b) => Ok(Value::String(format!("{:?}", b))),
AvroValue::String(s) => Ok(Value::String(s.clone())),
AvroValue::Fixed(_, bytes) => Ok(Value::String(format!("{:?}", bytes))),
AvroValue::Enum(_, symbol) => Ok(Value::String(symbol.clone())),
AvroValue::Union(_, boxed_value) => Self::avro_value_to_value(boxed_value),
AvroValue::Array(values) => {
let json_values: std::result::Result<Vec<JsonValue>, _> = values.iter()
.map(|v| Self::avro_value_to_json_value(v))
.collect();
match json_values {
Ok(vals) => Ok(Value::String(serde_json::to_string(&vals).unwrap_or_default())),
Err(_) => Ok(Value::String("[]".to_string())),
}
},
AvroValue::Map(map) => {
let mut json_map = serde_json::Map::new();
for (k, v) in map {
if let Ok(json_val) = Self::avro_value_to_json_value(v) {
json_map.insert(k.clone(), json_val);
}
}
Ok(Value::String(serde_json::to_string(&json_map).unwrap_or_default()))
},
AvroValue::Record(fields) => {
let mut json_map = serde_json::Map::new();
for (field_name, field_value) in fields {
if let Ok(json_val) = Self::avro_value_to_json_value(field_value) {
json_map.insert(field_name.clone(), json_val);
}
}
Ok(Value::String(serde_json::to_string(&json_map).unwrap_or_default()))
},
AvroValue::Date(days) => {
let base_date = chrono::NaiveDate::from_ymd_opt(1970, 1, 1)
.ok_or_else(|| TinyEtlError::DataTransfer("Invalid base date".to_string()))?;
let target_date = base_date + chrono::Duration::days(*days as i64);
let datetime = target_date.and_hms_opt(0, 0, 0)
.ok_or_else(|| TinyEtlError::DataTransfer("Invalid time conversion".to_string()))?
.and_utc();
Ok(Value::Date(datetime))
},
AvroValue::TimeMillis(millis) => {
Ok(Value::String(format!("{}ms", millis)))
},
AvroValue::TimeMicros(micros) => {
Ok(Value::String(format!("{}μs", micros)))
},
AvroValue::TimestampMillis(millis) => {
let datetime = chrono::DateTime::from_timestamp(*millis / 1000, ((*millis % 1000) * 1_000_000) as u32)
.ok_or_else(|| TinyEtlError::DataTransfer("Invalid timestamp".to_string()))?;
Ok(Value::Date(datetime))
},
AvroValue::TimestampMicros(micros) => {
let datetime = chrono::DateTime::from_timestamp(*micros / 1_000_000, ((*micros % 1_000_000) * 1000) as u32)
.ok_or_else(|| TinyEtlError::DataTransfer("Invalid timestamp".to_string()))?;
Ok(Value::Date(datetime))
},
AvroValue::LocalTimestampMillis(millis) => {
let datetime = chrono::DateTime::from_timestamp(*millis / 1000, ((*millis % 1000) * 1_000_000) as u32)
.ok_or_else(|| TinyEtlError::DataTransfer("Invalid local timestamp".to_string()))?;
Ok(Value::Date(datetime))
},
AvroValue::LocalTimestampMicros(micros) => {
let datetime = chrono::DateTime::from_timestamp(*micros / 1_000_000, ((*micros % 1_000_000) * 1000) as u32)
.ok_or_else(|| TinyEtlError::DataTransfer("Invalid local timestamp".to_string()))?;
Ok(Value::Date(datetime))
},
AvroValue::Decimal(decimal) => {
Ok(Value::String(format!("{:?}", decimal)))
},
AvroValue::Uuid(uuid) => {
Ok(Value::String(uuid.to_string()))
},
AvroValue::Duration(duration) => {
Ok(Value::String(format!("{:?}", duration)))
},
}
}
fn avro_value_to_json_value(avro_value: &AvroValue) -> Result<JsonValue> {
match avro_value {
AvroValue::Null => Ok(JsonValue::Null),
AvroValue::Boolean(b) => Ok(JsonValue::Bool(*b)),
AvroValue::Int(i) => Ok(JsonValue::Number(serde_json::Number::from(*i))),
AvroValue::Long(l) => Ok(JsonValue::Number(serde_json::Number::from(*l))),
AvroValue::Float(f) => Ok(JsonValue::Number(
serde_json::Number::from_f64(*f as f64).unwrap_or(serde_json::Number::from(0))
)),
AvroValue::Double(d) => Ok(JsonValue::Number(
serde_json::Number::from_f64(*d).unwrap_or(serde_json::Number::from(0))
)),
AvroValue::String(s) => Ok(JsonValue::String(s.clone())),
AvroValue::Union(_, boxed_value) => Self::avro_value_to_json_value(boxed_value),
_ => Ok(JsonValue::String(format!("{:?}", avro_value))),
}
}
}
#[async_trait]
impl Source for AvroSource {
async fn connect(&mut self) -> Result<()> {
if !self.file_path.exists() {
return Err(TinyEtlError::Connection(
format!("Avro file not found: {}", self.file_path.display())
));
}
let file = File::open(&self.file_path)?;
let buf_reader = BufReader::new(file);
let _reader = Reader::new(buf_reader)
.map_err(|e| TinyEtlError::DataTransfer(format!("Invalid Avro file: {}", e)))?;
self.current_position = 0;
self.has_more = true;
Ok(())
}
async fn infer_schema(&mut self, _sample_size: usize) -> Result<Schema> {
if !self.file_path.exists() {
return Err(TinyEtlError::Connection(
format!("Avro file not found: {}", self.file_path.display())
));
}
let file = File::open(&self.file_path)?;
let buf_reader = BufReader::new(file);
let reader = Reader::new(buf_reader)
.map_err(|e| TinyEtlError::DataTransfer(format!("Failed to create Avro reader: {}", e)))?;
let avro_schema = reader.writer_schema();
let schema_json: JsonValue = serde_json::from_str(&avro_schema.canonical_form())
.map_err(|e| TinyEtlError::DataTransfer(format!("Failed to parse Avro schema: {}", e)))?;
let mut columns = Vec::new();
if let JsonValue::Object(obj) = &schema_json {
if let Some(JsonValue::Array(fields)) = obj.get("fields") {
for field in fields {
if let JsonValue::Object(field_obj) = field {
let name = field_obj.get("name")
.and_then(|v| v.as_str())
.unwrap_or("unknown")
.to_string();
let default_type = JsonValue::String("string".to_string());
let field_type = field_obj.get("type").unwrap_or(&default_type);
let data_type = Self::avro_type_to_schema_type(field_type);
let nullable = Self::is_nullable(field_type);
columns.push(Column {
name,
data_type,
nullable,
});
}
}
}
}
Ok(Schema {
columns,
estimated_rows: self.total_records,
primary_key_candidate: None,
})
}
async fn read_batch(&mut self, batch_size: usize) -> Result<Vec<Row>> {
if !self.has_more {
return Ok(vec![]);
}
let file = File::open(&self.file_path)?;
let buf_reader = BufReader::new(file);
let reader = Reader::new(buf_reader)
.map_err(|e| TinyEtlError::DataTransfer(format!("Failed to create Avro reader: {}", e)))?;
let mut rows = Vec::new();
let mut count = 0;
let mut current_index = 0;
let mut reached_end = false;
for value_result in reader {
if current_index < self.current_position {
current_index += 1;
continue;
}
if count >= batch_size {
break;
}
match value_result {
Ok(avro_value) => {
if let AvroValue::Record(fields) = avro_value {
let mut row = HashMap::new();
for (field_name, field_value) in fields {
let converted_value = Self::avro_value_to_value(&field_value)?;
row.insert(field_name, converted_value);
}
rows.push(row);
count += 1;
self.current_position += 1;
current_index += 1;
}
}
Err(e) => {
return Err(TinyEtlError::DataTransfer(format!("Failed to read Avro record: {}", e)));
}
}
}
if count < batch_size {
reached_end = true;
}
if rows.is_empty() || reached_end {
self.has_more = false;
}
Ok(rows)
}
async fn estimated_row_count(&self) -> Result<Option<usize>> {
Ok(self.total_records)
}
async fn reset(&mut self) -> Result<()> {
self.current_position = 0;
self.has_more = true;
self.connect().await
}
fn has_more(&self) -> bool {
self.has_more
}
}
pub struct AvroTarget {
file_path: PathBuf,
schema: Option<AvroSchema>,
buffer: Vec<Row>,
}
impl AvroTarget {
pub fn new(file_path: &str) -> Result<Self> {
Ok(Self {
file_path: PathBuf::from(file_path),
schema: None,
buffer: Vec::new(),
})
}
fn schema_to_avro_schema(schema: &Schema) -> Result<AvroSchema> {
let mut fields = Vec::new();
for column in &schema.columns {
let field_type = match column.data_type {
DataType::String => {
if column.nullable {
json!(["null", "string"])
} else {
json!("string")
}
},
DataType::Integer => {
if column.nullable {
json!(["null", "long"])
} else {
json!("long")
}
},
DataType::Decimal => {
if column.nullable {
json!(["null", "double"])
} else {
json!("double")
}
},
DataType::Boolean => {
if column.nullable {
json!(["null", "boolean"])
} else {
json!("boolean")
}
},
DataType::Date => {
if column.nullable {
json!(["null", {"type": "int", "logicalType": "date"}])
} else {
json!({"type": "int", "logicalType": "date"})
}
},
DataType::DateTime => {
if column.nullable {
json!(["null", {"type": "long", "logicalType": "timestamp-millis"}])
} else {
json!({"type": "long", "logicalType": "timestamp-millis"})
}
},
DataType::Null => json!(["null", "string"]),
};
fields.push(json!({
"name": column.name,
"type": field_type
}));
}
let avro_schema_json = json!({
"type": "record",
"name": "Record",
"fields": fields
});
AvroSchema::parse(&avro_schema_json)
.map_err(|e| TinyEtlError::Configuration(format!("Failed to create Avro schema: {}", e)))
}
fn value_to_avro_value(value: &Value, data_type: &DataType) -> Result<AvroValue> {
match (value, data_type) {
(Value::Null, _) => Ok(AvroValue::Union(0, Box::new(AvroValue::Null))),
(Value::String(s), DataType::String) => Ok(AvroValue::String(s.clone())),
(Value::Integer(i), DataType::Integer) => Ok(AvroValue::Long(*i)),
(Value::Decimal(d), DataType::Decimal) => {
let f: f64 = (*d).try_into().unwrap_or(0.0);
Ok(AvroValue::Double(f))
},
(Value::Boolean(b), DataType::Boolean) => Ok(AvroValue::Boolean(*b)),
(Value::Date(dt), DataType::Date) => {
let days_since_epoch = (dt.timestamp() / 86400) as i32;
Ok(AvroValue::Date(days_since_epoch))
},
(Value::Date(dt), DataType::DateTime) => {
Ok(AvroValue::TimestampMillis(dt.timestamp_millis()))
},
(Value::String(s), DataType::Integer) => {
let parsed = s.parse::<i64>()
.map_err(|_| TinyEtlError::DataTransfer(format!("Cannot convert '{}' to integer", s)))?;
Ok(AvroValue::Long(parsed))
},
(Value::String(s), DataType::Decimal) => {
let parsed = s.parse::<Decimal>()
.map_err(|_| TinyEtlError::DataTransfer(format!("Cannot convert '{}' to decimal", s)))?;
let f: f64 = parsed.try_into().unwrap_or(0.0);
Ok(AvroValue::Double(f))
},
(Value::String(s), DataType::Boolean) => {
let parsed = s.parse::<bool>()
.map_err(|_| TinyEtlError::DataTransfer(format!("Cannot convert '{}' to boolean", s)))?;
Ok(AvroValue::Boolean(parsed))
},
(Value::Integer(i), DataType::String) => Ok(AvroValue::String(i.to_string())),
(Value::Decimal(d), DataType::String) => Ok(AvroValue::String(d.to_string())),
(Value::Boolean(b), DataType::String) => Ok(AvroValue::String(b.to_string())),
_ => Err(TinyEtlError::DataTransfer(format!(
"Cannot convert value {:?} to Avro type {:?}", value, data_type
))),
}
}
}
#[async_trait]
impl Target for AvroTarget {
async fn connect(&mut self) -> Result<()> {
Ok(())
}
async fn create_table(&mut self, _table_name: &str, schema: &Schema) -> Result<()> {
let avro_schema = Self::schema_to_avro_schema(schema)?;
self.schema = Some(avro_schema);
Ok(())
}
async fn write_batch(&mut self, rows: &[Row]) -> Result<usize> {
if self.schema.is_none() {
return Err(TinyEtlError::Configuration("Schema not initialized. Call create_table() first.".to_string()));
}
self.buffer.extend_from_slice(rows);
Ok(rows.len())
}
async fn finalize(&mut self) -> Result<()> {
if self.schema.is_none() {
return Ok(());
}
let schema = self.schema.as_ref().unwrap();
let file = File::create(&self.file_path)?;
let buf_writer = BufWriter::new(file);
let mut writer = Writer::new(schema, buf_writer);
for row in &self.buffer {
let mut record_fields = Vec::new();
let schema_json: JsonValue = serde_json::from_str(&schema.canonical_form())
.map_err(|e| TinyEtlError::DataTransfer(format!("Failed to parse schema: {}", e)))?;
if let JsonValue::Object(obj) = &schema_json {
if let Some(JsonValue::Array(fields)) = obj.get("fields") {
for field in fields {
if let JsonValue::Object(field_obj) = field {
let field_name = field_obj.get("name")
.and_then(|v| v.as_str())
.unwrap_or("unknown");
let default_type = JsonValue::String("string".to_string());
let field_type = field_obj.get("type").unwrap_or(&default_type);
let data_type = AvroSource::avro_type_to_schema_type(field_type);
let value = row.get(field_name).unwrap_or(&Value::Null);
let avro_value = Self::value_to_avro_value(value, &data_type)?;
record_fields.push((field_name.to_string(), avro_value));
}
}
}
}
let record = AvroValue::Record(record_fields);
writer.append(record)
.map_err(|e| TinyEtlError::DataTransfer(format!("Failed to write Avro record: {}", e)))?;
}
writer.flush()
.map_err(|e| TinyEtlError::DataTransfer(format!("Failed to flush Avro writer: {}", e)))?;
self.buffer.clear();
Ok(())
}
async fn exists(&self, _table_name: &str) -> Result<bool> {
Ok(self.file_path.exists())
}
async fn truncate(&mut self, _table_name: &str) -> Result<()> {
self.buffer.clear();
Ok(())
}
fn supports_append(&self) -> bool {
false
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::NamedTempFile;
use std::io::Write;
use chrono::{DateTime, Utc};
fn create_test_avro_file() -> Result<NamedTempFile> {
let temp_file = NamedTempFile::new()?;
let schema_json = json!({
"type": "record",
"name": "TestRecord",
"fields": [
{"name": "id", "type": "long"},
{"name": "name", "type": "string"},
{"name": "email", "type": ["null", "string"]},
{"name": "age", "type": "int"},
{"name": "salary", "type": "double"},
{"name": "active", "type": "boolean"},
{"name": "created_at", "type": {"type": "long", "logicalType": "timestamp-millis"}}
]
});
let schema = AvroSchema::parse(&schema_json)
.map_err(|e| TinyEtlError::DataTransfer(format!("Failed to create test schema: {}", e)))?;
let file = temp_file.reopen()?;
let mut writer = Writer::new(&schema, file);
let test_records = vec![
AvroValue::Record(vec![
("id".to_string(), AvroValue::Long(1)),
("name".to_string(), AvroValue::String("John Doe".to_string())),
("email".to_string(), AvroValue::Union(1, Box::new(AvroValue::String("john@example.com".to_string())))),
("age".to_string(), AvroValue::Int(30)),
("salary".to_string(), AvroValue::Double(50000.0)),
("active".to_string(), AvroValue::Boolean(true)),
("created_at".to_string(), AvroValue::TimestampMillis(1609459200000)), ]),
AvroValue::Record(vec![
("id".to_string(), AvroValue::Long(2)),
("name".to_string(), AvroValue::String("Jane Smith".to_string())),
("email".to_string(), AvroValue::Union(0, Box::new(AvroValue::Null))),
("age".to_string(), AvroValue::Int(25)),
("salary".to_string(), AvroValue::Double(60000.0)),
("active".to_string(), AvroValue::Boolean(false)),
("created_at".to_string(), AvroValue::TimestampMillis(1609545600000)), ]),
];
for record in test_records {
writer.append(record)
.map_err(|e| TinyEtlError::DataTransfer(format!("Failed to write test record: {}", e)))?;
}
writer.flush()
.map_err(|e| TinyEtlError::DataTransfer(format!("Failed to flush test writer: {}", e)))?;
Ok(temp_file)
}
#[tokio::test]
async fn test_avro_source_new() {
let source = AvroSource::new("test.avro");
assert!(source.is_ok());
let source = source.unwrap();
assert_eq!(source.file_path.to_string_lossy(), "test.avro");
assert_eq!(source.current_position, 0);
assert_eq!(source.total_records, None);
assert!(source.has_more);
}
#[tokio::test]
async fn test_avro_source_connect_file_not_found() {
let mut source = AvroSource::new("nonexistent.avro").unwrap();
let result = source.connect().await;
assert!(result.is_err());
if let Err(TinyEtlError::Connection(msg)) = result {
assert!(msg.contains("Avro file not found"));
} else {
panic!("Expected Connection error");
}
}
#[tokio::test]
async fn test_avro_source_connect_success() {
let temp_file = create_test_avro_file().unwrap();
let mut source = AvroSource::new(temp_file.path().to_str().unwrap()).unwrap();
let result = source.connect().await;
assert!(result.is_ok());
assert_eq!(source.current_position, 0);
assert!(source.has_more);
}
#[tokio::test]
async fn test_avro_source_infer_schema() {
let temp_file = create_test_avro_file().unwrap();
let mut source = AvroSource::new(temp_file.path().to_str().unwrap()).unwrap();
let schema = source.infer_schema(100).await.unwrap();
assert_eq!(schema.columns.len(), 7);
let id_col = &schema.columns[0];
assert_eq!(id_col.name, "id");
assert_eq!(id_col.data_type, DataType::Integer);
assert!(!id_col.nullable);
let name_col = &schema.columns[1];
assert_eq!(name_col.name, "name");
assert_eq!(name_col.data_type, DataType::String);
let email_col = &schema.columns[2];
assert_eq!(email_col.name, "email");
assert_eq!(email_col.data_type, DataType::String);
assert!(email_col.nullable);
}
#[tokio::test]
async fn test_avro_source_read_batch() {
let temp_file = create_test_avro_file().unwrap();
let mut source = AvroSource::new(temp_file.path().to_str().unwrap()).unwrap();
source.connect().await.unwrap();
let rows = source.read_batch(10).await.unwrap();
assert_eq!(rows.len(), 2);
let first_row = &rows[0];
assert_eq!(first_row.get("id"), Some(&Value::Integer(1)));
assert_eq!(first_row.get("name"), Some(&Value::String("John Doe".to_string())));
assert_eq!(first_row.get("age"), Some(&Value::Integer(30)));
assert_eq!(first_row.get("salary"), Some(&Value::Decimal(Decimal::new(50000, 0))));
assert_eq!(first_row.get("active"), Some(&Value::Boolean(true)));
let second_row = &rows[1];
assert_eq!(second_row.get("id"), Some(&Value::Integer(2)));
assert_eq!(second_row.get("name"), Some(&Value::String("Jane Smith".to_string())));
assert_eq!(second_row.get("email"), Some(&Value::Null));
}
#[tokio::test]
async fn test_avro_source_read_batch_pagination() {
let temp_file = create_test_avro_file().unwrap();
let mut source = AvroSource::new(temp_file.path().to_str().unwrap()).unwrap();
source.connect().await.unwrap();
let rows1 = source.read_batch(1).await.unwrap();
assert_eq!(rows1.len(), 1);
assert_eq!(rows1[0].get("id"), Some(&Value::Integer(1)));
let rows2 = source.read_batch(1).await.unwrap();
assert_eq!(rows2.len(), 1);
assert_eq!(rows2[0].get("id"), Some(&Value::Integer(2)));
let rows3 = source.read_batch(1).await.unwrap();
assert!(rows3.is_empty());
assert!(!source.has_more());
}
#[tokio::test]
async fn test_avro_source_reset() {
let temp_file = create_test_avro_file().unwrap();
let mut source = AvroSource::new(temp_file.path().to_str().unwrap()).unwrap();
source.connect().await.unwrap();
source.read_batch(10).await.unwrap();
assert!(!source.has_more());
source.reset().await.unwrap();
assert!(source.has_more());
assert_eq!(source.current_position, 0);
let rows = source.read_batch(10).await.unwrap();
assert_eq!(rows.len(), 2);
}
#[tokio::test]
async fn test_avro_type_to_schema_type() {
assert_eq!(AvroSource::avro_type_to_schema_type(&json!("string")), DataType::String);
assert_eq!(AvroSource::avro_type_to_schema_type(&json!("int")), DataType::Integer);
assert_eq!(AvroSource::avro_type_to_schema_type(&json!("long")), DataType::Integer);
assert_eq!(AvroSource::avro_type_to_schema_type(&json!("float")), DataType::Decimal);
assert_eq!(AvroSource::avro_type_to_schema_type(&json!("double")), DataType::Decimal);
assert_eq!(AvroSource::avro_type_to_schema_type(&json!("boolean")), DataType::Boolean);
assert_eq!(
AvroSource::avro_type_to_schema_type(&json!({"type": "int", "logicalType": "date"})),
DataType::Date
);
assert_eq!(
AvroSource::avro_type_to_schema_type(&json!({"type": "long", "logicalType": "timestamp-millis"})),
DataType::DateTime
);
assert_eq!(
AvroSource::avro_type_to_schema_type(&json!(["null", "string"])),
DataType::String
);
}
#[tokio::test]
async fn test_is_nullable() {
assert!(!AvroSource::is_nullable(&json!("string")));
assert!(AvroSource::is_nullable(&json!(["null", "string"])));
assert!(AvroSource::is_nullable(&json!(["string", "null"])));
assert!(!AvroSource::is_nullable(&json!(["string", "int"])));
}
#[tokio::test]
async fn test_avro_value_to_value() {
assert_eq!(
AvroSource::avro_value_to_value(&AvroValue::Null).unwrap(),
Value::Null
);
assert_eq!(
AvroSource::avro_value_to_value(&AvroValue::Boolean(true)).unwrap(),
Value::Boolean(true)
);
assert_eq!(
AvroSource::avro_value_to_value(&AvroValue::Int(42)).unwrap(),
Value::Integer(42)
);
assert_eq!(
AvroSource::avro_value_to_value(&AvroValue::Long(100)).unwrap(),
Value::Integer(100)
);
let float_result = AvroSource::avro_value_to_value(&AvroValue::Float(3.14)).unwrap();
if let Value::Decimal(d) = float_result {
let f: f64 = d.try_into().unwrap();
assert!((f - 3.14).abs() < 0.01, "Expected ~3.14, got {}", f);
} else {
panic!("Expected Decimal value");
}
assert_eq!(
AvroSource::avro_value_to_value(&AvroValue::Double(2.718)).unwrap(),
Value::Decimal(Decimal::new(2718, 3))
);
assert_eq!(
AvroSource::avro_value_to_value(&AvroValue::String("test".to_string())).unwrap(),
Value::String("test".to_string())
);
let date_result = AvroSource::avro_value_to_value(&AvroValue::Date(0)).unwrap();
if let Value::Date(_) = date_result {
} else {
panic!("Expected Date value");
}
let timestamp_result = AvroSource::avro_value_to_value(&AvroValue::TimestampMillis(1609459200000)).unwrap();
if let Value::Date(_) = timestamp_result {
} else {
panic!("Expected Date value");
}
let union_value = AvroValue::Union(1, Box::new(AvroValue::String("test".to_string())));
assert_eq!(
AvroSource::avro_value_to_value(&union_value).unwrap(),
Value::String("test".to_string())
);
}
#[tokio::test]
async fn test_avro_target_new() {
let target = AvroTarget::new("output.avro");
assert!(target.is_ok());
let target = target.unwrap();
assert_eq!(target.file_path.to_string_lossy(), "output.avro");
assert!(target.schema.is_none());
assert!(target.buffer.is_empty());
}
#[tokio::test]
async fn test_avro_target_connect() {
let mut target = AvroTarget::new("output.avro").unwrap();
let result = target.connect().await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_avro_target_create_table() {
let mut target = AvroTarget::new("output.avro").unwrap();
let schema = Schema {
columns: vec![
Column {
name: "id".to_string(),
data_type: DataType::Integer,
nullable: false,
},
Column {
name: "name".to_string(),
data_type: DataType::String,
nullable: true,
},
Column {
name: "active".to_string(),
data_type: DataType::Boolean,
nullable: false,
},
Column {
name: "score".to_string(),
data_type: DataType::Decimal,
nullable: true,
},
Column {
name: "created".to_string(),
data_type: DataType::DateTime,
nullable: false,
},
],
estimated_rows: None,
primary_key_candidate: None,
};
let result = target.create_table("test_table", &schema).await;
assert!(result.is_ok());
assert!(target.schema.is_some());
}
#[tokio::test]
async fn test_schema_to_avro_schema() {
let schema = Schema {
columns: vec![
Column {
name: "id".to_string(),
data_type: DataType::Integer,
nullable: false,
},
Column {
name: "name".to_string(),
data_type: DataType::String,
nullable: true,
},
Column {
name: "active".to_string(),
data_type: DataType::Boolean,
nullable: false,
},
],
estimated_rows: None,
primary_key_candidate: None,
};
let avro_schema = AvroTarget::schema_to_avro_schema(&schema);
assert!(avro_schema.is_ok());
let schema_json: JsonValue = serde_json::from_str(&avro_schema.unwrap().canonical_form()).unwrap();
if let JsonValue::Object(obj) = schema_json {
if let Some(JsonValue::Array(fields)) = obj.get("fields") {
assert_eq!(fields.len(), 3);
let id_field = &fields[0];
if let JsonValue::Object(field_obj) = id_field {
assert_eq!(field_obj.get("name").unwrap(), "id");
assert_eq!(field_obj.get("type").unwrap(), "long");
}
let name_field = &fields[1];
if let JsonValue::Object(field_obj) = name_field {
assert_eq!(field_obj.get("name").unwrap(), "name");
assert_eq!(field_obj.get("type").unwrap(), &json!(["null", "string"]));
}
}
}
}
#[tokio::test]
async fn test_value_to_avro_value() {
assert_eq!(
AvroTarget::value_to_avro_value(&Value::Null, &DataType::String).unwrap(),
AvroValue::Union(0, Box::new(AvroValue::Null))
);
assert_eq!(
AvroTarget::value_to_avro_value(&Value::String("test".to_string()), &DataType::String).unwrap(),
AvroValue::String("test".to_string())
);
assert_eq!(
AvroTarget::value_to_avro_value(&Value::Integer(42), &DataType::Integer).unwrap(),
AvroValue::Long(42)
);
assert_eq!(
AvroTarget::value_to_avro_value(&Value::Decimal(Decimal::new(314, 2)), &DataType::Decimal).unwrap(),
AvroValue::Double(3.14)
);
assert_eq!(
AvroTarget::value_to_avro_value(&Value::Boolean(true), &DataType::Boolean).unwrap(),
AvroValue::Boolean(true)
);
assert_eq!(
AvroTarget::value_to_avro_value(&Value::String("42".to_string()), &DataType::Integer).unwrap(),
AvroValue::Long(42)
);
assert_eq!(
AvroTarget::value_to_avro_value(&Value::String("3.14".to_string()), &DataType::Decimal).unwrap(),
AvroValue::Double(3.14)
);
assert_eq!(
AvroTarget::value_to_avro_value(&Value::String("true".to_string()), &DataType::Boolean).unwrap(),
AvroValue::Boolean(true)
);
let datetime = DateTime::from_timestamp(1609459200, 0).unwrap();
let date_value = Value::Date(datetime);
let result = AvroTarget::value_to_avro_value(&date_value, &DataType::Date).unwrap();
if let AvroValue::Date(_) = result {
} else {
panic!("Expected Date value");
}
let result = AvroTarget::value_to_avro_value(&date_value, &DataType::DateTime).unwrap();
if let AvroValue::TimestampMillis(_) = result {
} else {
panic!("Expected TimestampMillis value");
}
}
#[tokio::test]
async fn test_value_to_avro_value_invalid_conversions() {
let result = AvroTarget::value_to_avro_value(&Value::String("not_a_number".to_string()), &DataType::Integer);
assert!(result.is_err());
let result = AvroTarget::value_to_avro_value(&Value::String("not_a_float".to_string()), &DataType::Decimal);
assert!(result.is_err());
let result = AvroTarget::value_to_avro_value(&Value::String("not_a_bool".to_string()), &DataType::Boolean);
assert!(result.is_err());
}
#[tokio::test]
async fn test_avro_target_write_batch_without_schema() {
let mut target = AvroTarget::new("output.avro").unwrap();
let row = std::collections::HashMap::from([
("id".to_string(), Value::Integer(1)),
("name".to_string(), Value::String("test".to_string())),
]);
let result = target.write_batch(&[row]).await;
assert!(result.is_err());
if let Err(TinyEtlError::Configuration(msg)) = result {
assert!(msg.contains("Schema not initialized"));
} else {
panic!("Expected Configuration error");
}
}
#[tokio::test]
async fn test_avro_target_full_workflow() {
let temp_file = NamedTempFile::new().unwrap();
let mut target = AvroTarget::new(temp_file.path().to_str().unwrap()).unwrap();
let schema = Schema {
columns: vec![
Column {
name: "id".to_string(),
data_type: DataType::Integer,
nullable: false,
},
Column {
name: "name".to_string(),
data_type: DataType::String,
nullable: false,
},
Column {
name: "active".to_string(),
data_type: DataType::Boolean,
nullable: false,
},
],
estimated_rows: None,
primary_key_candidate: None,
};
target.connect().await.unwrap();
target.create_table("test_table", &schema).await.unwrap();
let rows = vec![
std::collections::HashMap::from([
("id".to_string(), Value::Integer(1)),
("name".to_string(), Value::String("John".to_string())),
("active".to_string(), Value::Boolean(true)),
]),
std::collections::HashMap::from([
("id".to_string(), Value::Integer(2)),
("name".to_string(), Value::String("Jane".to_string())),
("active".to_string(), Value::Boolean(false)),
]),
];
let write_result = target.write_batch(&rows).await.unwrap();
assert_eq!(write_result, 2);
target.finalize().await.unwrap();
let mut source = AvroSource::new(temp_file.path().to_str().unwrap()).unwrap();
source.connect().await.unwrap();
let read_rows = source.read_batch(10).await.unwrap();
assert_eq!(read_rows.len(), 2);
assert_eq!(read_rows[0].get("id"), Some(&Value::Integer(1)));
assert_eq!(read_rows[0].get("name"), Some(&Value::String("John".to_string())));
assert_eq!(read_rows[0].get("active"), Some(&Value::Boolean(true)));
assert_eq!(read_rows[1].get("id"), Some(&Value::Integer(2)));
assert_eq!(read_rows[1].get("name"), Some(&Value::String("Jane".to_string())));
assert_eq!(read_rows[1].get("active"), Some(&Value::Boolean(false)));
}
#[tokio::test]
async fn test_avro_target_exists() {
let temp_file = NamedTempFile::new().unwrap();
let target = AvroTarget::new(temp_file.path().to_str().unwrap()).unwrap();
assert!(target.exists("test_table").await.unwrap());
let target_nonexistent = AvroTarget::new("nonexistent.avro").unwrap();
assert!(!target_nonexistent.exists("test_table").await.unwrap());
}
#[tokio::test]
async fn test_avro_source_invalid_file_content() {
let temp_file = NamedTempFile::new().unwrap();
std::fs::write(temp_file.path(), "invalid avro content").unwrap();
let mut source = AvroSource::new(temp_file.path().to_str().unwrap()).unwrap();
let result = source.connect().await;
assert!(result.is_err());
if let Err(TinyEtlError::DataTransfer(msg)) = result {
assert!(msg.contains("Invalid Avro file"));
} else {
panic!("Expected DataTransfer error");
}
}
#[test]
fn test_avro_value_to_json_value() {
assert_eq!(
AvroSource::avro_value_to_json_value(&AvroValue::Null).unwrap(),
JsonValue::Null
);
assert_eq!(
AvroSource::avro_value_to_json_value(&AvroValue::Boolean(true)).unwrap(),
JsonValue::Bool(true)
);
assert_eq!(
AvroSource::avro_value_to_json_value(&AvroValue::Int(42)).unwrap(),
JsonValue::Number(serde_json::Number::from(42))
);
assert_eq!(
AvroSource::avro_value_to_json_value(&AvroValue::String("test".to_string())).unwrap(),
JsonValue::String("test".to_string())
);
let union_value = AvroValue::Union(1, Box::new(AvroValue::String("test".to_string())));
assert_eq!(
AvroSource::avro_value_to_json_value(&union_value).unwrap(),
JsonValue::String("test".to_string())
);
}
}