Skip to content

Commit 8cd3672

Browse files
committed
added initial default logic
1 parent 77d4519 commit 8cd3672

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

crates/iceberg/src/avro/schema.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,41 @@ const MAP_LOGICAL_TYPE: &str = "map";
4343
// This const may better to maintain in avro-rs.
4444
const LOGICAL_TYPE: &str = "logicalType";
4545

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) => Ok(Value::Number(
61+
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+
}
80+
4681
struct SchemaToAvroSchema {
4782
schema: String,
4883
}
@@ -92,9 +127,12 @@ impl SchemaVisitor for SchemaToAvroSchema {
92127
custom_attributes: Default::default(),
93128
};
94129

95-
if !field.required {
130+
if let Some(default) = &field.initial_default {
131+
avro_record_field.default = Some(literal_to_json(default)?);
132+
} else if !field.required {
96133
avro_record_field.default = Some(Value::Null);
97134
}
135+
98136
avro_record_field.custom_attributes.insert(
99137
FILED_ID_PROP.to_string(),
100138
Value::Number(Number::from(field.id)),

crates/iceberg/src/spec/manifest/_serde.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl ManifestEntryV1 {
9999
#[serde_as]
100100
#[derive(Serialize, Deserialize)]
101101
pub(super) struct DataFileSerde {
102-
#[serde(default)]
102+
#[serde(default = "default_to_zero")]
103103
content: i32,
104104
file_path: String,
105105
file_format: String,
@@ -125,6 +125,10 @@ pub(super) struct DataFileSerde {
125125
content_size_in_bytes: Option<i64>,
126126
}
127127

128+
fn default_to_zero() -> i32 {
129+
0
130+
}
131+
128132
impl DataFileSerde {
129133
pub fn try_from(
130134
value: super::DataFile,
@@ -400,7 +404,7 @@ mod tests {
400404
}
401405

402406
#[tokio::test]
403-
async fn test_data_file_serialize_deserialize_v1() {
407+
async fn test_data_file_serialize_deserialize_v1_data_on_v2_reader() {
404408
let schema = Arc::new(
405409
Schema::builder()
406410
.with_fields(vec![
@@ -461,7 +465,7 @@ mod tests {
461465
&schema,
462466
0,
463467
&StructType::new(vec![]),
464-
FormatVersion::V1,
468+
FormatVersion::V2,
465469
)
466470
.unwrap();
467471

crates/iceberg/src/spec/manifest/entry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ static CONTENT: Lazy<NestedFieldRef> = {
234234
Lazy::new(|| {
235235
Arc::new(
236236
NestedField::required(134, "content", Type::Primitive(PrimitiveType::Int))
237-
.with_initial_default(Literal::Primitive(PrimitiveLiteral::Int(1))),
237+
.with_initial_default(Literal::Primitive(PrimitiveLiteral::Int(0))),
238238
)
239239
})
240240
};

0 commit comments

Comments
 (0)