Skip to content

Commit db5330c

Browse files
committed
feat: support more types in PartitionSpec::partition_to_path
1 parent 42191e9 commit db5330c

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

crates/iceberg/src/spec/partition.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ impl PartitionSpec {
155155
true
156156
}
157157

158-
pub(crate) fn partition_to_path(&self, data: &Struct, schema: SchemaRef) -> String {
158+
/// Returns partition path string containing partition type and partition
159+
/// value as key-value pairs.
160+
pub fn partition_to_path(&self, data: &Struct, schema: SchemaRef) -> String {
159161
let partition_type = self.partition_type(&schema).unwrap();
160162
let field_types = partition_type.fields();
161163

@@ -1813,6 +1815,9 @@ mod tests {
18131815
.with_fields(vec![
18141816
NestedField::required(1, "id", Type::Primitive(PrimitiveType::Int)).into(),
18151817
NestedField::required(2, "name", Type::Primitive(PrimitiveType::String)).into(),
1818+
NestedField::required(3, "timestamp", Type::Primitive(PrimitiveType::Timestamp))
1819+
.into(),
1820+
NestedField::required(4, "empty", Type::Primitive(PrimitiveType::String)).into(),
18161821
])
18171822
.build()
18181823
.unwrap();
@@ -1822,14 +1827,23 @@ mod tests {
18221827
.unwrap()
18231828
.add_partition_field("name", "name", Transform::Identity)
18241829
.unwrap()
1830+
.add_partition_field("timestamp", "ts_hour", Transform::Hour)
1831+
.unwrap()
1832+
.add_partition_field("empty", "empty_void", Transform::Void)
1833+
.unwrap()
18251834
.build()
18261835
.unwrap();
18271836

1828-
let data = Struct::from_iter([Some(Literal::int(42)), Some(Literal::string("alice"))]);
1837+
let data = Struct::from_iter([
1838+
Some(Literal::int(42)),
1839+
Some(Literal::string("alice")),
1840+
Some(Literal::int(1000)),
1841+
Some(Literal::string("empty")),
1842+
]);
18291843

18301844
assert_eq!(
18311845
spec.partition_to_path(&data, schema.into()),
1832-
"id=42/name=alice"
1846+
"id=42/name=alice/ts_hour=1000/empty_void=null"
18331847
);
18341848
}
18351849
}

crates/iceberg/src/spec/transform.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,17 @@ pub enum Transform {
137137
impl Transform {
138138
/// Returns a human-readable String representation of a transformed value.
139139
pub fn to_human_string(&self, field_type: &Type, value: Option<&Literal>) -> String {
140-
if let Some(value) = value {
141-
if let Some(value) = value.as_primitive_literal() {
142-
let field_type = field_type.as_primitive_type().unwrap();
143-
let datum = Datum::new(field_type.clone(), value);
144-
match self {
145-
Self::Identity => datum.to_human_string(),
146-
Self::Void => "null".to_string(),
147-
_ => {
148-
todo!()
149-
}
150-
}
151-
} else {
152-
"null".to_string()
140+
let Some(value) = value else {
141+
return "null".to_string();
142+
};
143+
144+
if let Some(value) = value.as_primitive_literal() {
145+
let field_type = field_type.as_primitive_type().unwrap();
146+
let datum = Datum::new(field_type.clone(), value);
147+
148+
match self {
149+
Self::Void => "null".to_string(),
150+
_ => datum.to_human_string(),
153151
}
154152
} else {
155153
"null".to_string()

crates/iceberg/src/spec/values.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,7 @@ impl Datum {
12711271
///
12721272
/// For string literals, this returns the raw string value without quotes.
12731273
/// For all other literals, it falls back to [`to_string()`].
1274-
pub(crate) fn to_human_string(&self) -> String {
1274+
pub fn to_human_string(&self) -> String {
12751275
match self.literal() {
12761276
PrimitiveLiteral::String(s) => s.to_string(),
12771277
_ => self.to_string(),

0 commit comments

Comments
 (0)