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
use avro_schema::schema::{
BytesLogical, Field as AvroField, Fixed, FixedLogical, IntLogical, LongLogical, Record,
Schema as AvroSchema,
};
use crate::datatypes::*;
use crate::error::{Error, Result};
pub fn to_record(schema: &Schema) -> Result<Record> {
let fields = schema
.fields
.iter()
.map(field_to_field)
.collect::<Result<_>>()?;
Ok(Record {
name: "".to_string(),
namespace: None,
doc: None,
aliases: vec![],
fields,
})
}
fn field_to_field(field: &Field) -> Result<AvroField> {
let schema = type_to_schema(field.data_type(), field.is_nullable)?;
Ok(AvroField::new(&field.name, schema))
}
fn type_to_schema(data_type: &DataType, is_nullable: bool) -> Result<AvroSchema> {
Ok(if is_nullable {
AvroSchema::Union(vec![AvroSchema::Null, _type_to_schema(data_type)?])
} else {
_type_to_schema(data_type)?
})
}
fn _type_to_schema(data_type: &DataType) -> Result<AvroSchema> {
Ok(match data_type.to_logical_type() {
DataType::Null => AvroSchema::Null,
DataType::Boolean => AvroSchema::Boolean,
DataType::Int32 => AvroSchema::Int(None),
DataType::Int64 => AvroSchema::Long(None),
DataType::Float32 => AvroSchema::Float,
DataType::Float64 => AvroSchema::Double,
DataType::Binary => AvroSchema::Bytes(None),
DataType::LargeBinary => AvroSchema::Bytes(None),
DataType::Utf8 => AvroSchema::String(None),
DataType::LargeUtf8 => AvroSchema::String(None),
DataType::LargeList(inner) | DataType::List(inner) => AvroSchema::Array(Box::new(
type_to_schema(&inner.data_type, inner.is_nullable)?,
)),
DataType::Struct(fields) => AvroSchema::Record(Record::new(
"",
fields
.iter()
.map(field_to_field)
.collect::<Result<Vec<_>>>()?,
)),
DataType::Date32 => AvroSchema::Int(Some(IntLogical::Date)),
DataType::Time32(TimeUnit::Millisecond) => AvroSchema::Int(Some(IntLogical::Time)),
DataType::Time64(TimeUnit::Microsecond) => AvroSchema::Long(Some(LongLogical::Time)),
DataType::Timestamp(TimeUnit::Millisecond, None) => {
AvroSchema::Long(Some(LongLogical::LocalTimestampMillis))
}
DataType::Timestamp(TimeUnit::Microsecond, None) => {
AvroSchema::Long(Some(LongLogical::LocalTimestampMicros))
}
DataType::Interval(IntervalUnit::MonthDayNano) => {
let mut fixed = Fixed::new("", 12);
fixed.logical = Some(FixedLogical::Duration);
AvroSchema::Fixed(fixed)
}
DataType::FixedSizeBinary(size) => AvroSchema::Fixed(Fixed::new("", *size)),
DataType::Decimal(p, s) => AvroSchema::Bytes(Some(BytesLogical::Decimal(*p, *s))),
other => {
return Err(Error::NotYetImplemented(format!(
"write {:?} to avro",
other
)))
}
})
}