Skip to content

Commit 68bd35d

Browse files
committed
new attempt to serialize
1 parent 09bd236 commit 68bd35d

File tree

1 file changed

+13
-37
lines changed

1 file changed

+13
-37
lines changed

crates/iceberg/src/avro/schema.rs

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717

1818
//! Conversion between iceberg and avro schema.
19+
use std::any::Any;
1920
use std::collections::BTreeMap;
2021

2122
use apache_avro::Schema as AvroSchema;
@@ -28,7 +29,7 @@ use serde_json::{Number, Value};
2829

2930
use crate::spec::{
3031
ListType, MapType, NestedField, NestedFieldRef, PrimitiveType, Schema, SchemaVisitor,
31-
StructType, Type, visit_schema,
32+
StructType, Type, visit_schema, Datum, RawLiteral,
3233
};
3334
use crate::{Error, ErrorKind, Result, ensure_data_valid};
3435

@@ -43,40 +44,7 @@ const MAP_LOGICAL_TYPE: &str = "map";
4344
// This const may better to maintain in avro-rs.
4445
const LOGICAL_TYPE: &str = "logicalType";
4546

46-
fn literal_to_json(literal: &crate::spec::Literal) -> Result<Value> {
47-
match literal {
48-
crate::spec::Literal::Primitive(p) => match p {
49-
crate::spec::PrimitiveLiteral::Boolean(b) => Ok(Value::Bool(*b)),
50-
crate::spec::PrimitiveLiteral::Int(i) => Ok(Value::Number(Number::from(*i))),
51-
crate::spec::PrimitiveLiteral::Long(l) => Ok(Value::Number(Number::from(*l))),
52-
crate::spec::PrimitiveLiteral::Float(f) => Ok(Value::Number(
53-
Number::from_f64(f.0 as f64).ok_or_else(|| {
54-
Error::new(
55-
ErrorKind::DataInvalid,
56-
"Failed to convert float to json number",
57-
)
58-
})?,
59-
)),
60-
crate::spec::PrimitiveLiteral::Double(d) => {
61-
Ok(Value::Number(Number::from_f64(d.0).ok_or_else(|| {
62-
Error::new(
63-
ErrorKind::DataInvalid,
64-
"Failed to convert double to json number",
65-
)
66-
})?))
67-
}
68-
crate::spec::PrimitiveLiteral::String(s) => Ok(Value::String(s.clone())),
69-
_ => Err(Error::new(
70-
ErrorKind::FeatureUnsupported,
71-
"Unsupported literal type to convert to json",
72-
)),
73-
},
74-
_ => Err(Error::new(
75-
ErrorKind::FeatureUnsupported,
76-
"Unsupported literal type to convert to json",
77-
)),
78-
}
79-
}
47+
8048

8149
struct SchemaToAvroSchema {
8250
schema: String,
@@ -116,8 +84,16 @@ impl SchemaVisitor for SchemaToAvroSchema {
11684
field_schema = avro_optional(field_schema)?;
11785
}
11886

119-
let default = if let Some(default) = &field.initial_default {
120-
Some(literal_to_json(default)?)
87+
let default = if let Some(literal) = &field.initial_default {
88+
let raw_literal = RawLiteral::try_from(literal.clone(), &field.field_type)?;
89+
let json_value = serde_json::to_value(raw_literal).map_err(|e| {
90+
Error::new(
91+
ErrorKind::DataInvalid,
92+
"Failed to serialize default value to json",
93+
)
94+
.with_source(e)
95+
})?;
96+
Some(json_value)
12197
} else if !field.required {
12298
Some(Value::Null)
12399
} else {

0 commit comments

Comments
 (0)