Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions crates/iceberg/src/spec/partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ impl PartitionSpec {
true
}

pub(crate) fn partition_to_path(&self, data: &Struct, schema: SchemaRef) -> String {
/// Returns partition path string containing partition type and partition
/// value as key-value pairs.
pub fn partition_to_path(&self, data: &Struct, schema: SchemaRef) -> String {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need to make this pub? It's used internally by LocationGenerator

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @liurenjie1024, this is helpful if user-code is using as iceberg-rs as a low-level library. In my particular case, I want to split my data into chunks based on the partition value, hence the need to generate partition path.

I see that PartitionKey::to_path effectively achieves the same and provides partition path.

let partition_type = self.partition_type(&schema).unwrap();
let field_types = partition_type.fields();

Expand Down Expand Up @@ -1813,6 +1815,9 @@ mod tests {
.with_fields(vec![
NestedField::required(1, "id", Type::Primitive(PrimitiveType::Int)).into(),
NestedField::required(2, "name", Type::Primitive(PrimitiveType::String)).into(),
NestedField::required(3, "timestamp", Type::Primitive(PrimitiveType::Timestamp))
.into(),
NestedField::required(4, "empty", Type::Primitive(PrimitiveType::String)).into(),
])
.build()
.unwrap();
Expand All @@ -1822,14 +1827,23 @@ mod tests {
.unwrap()
.add_partition_field("name", "name", Transform::Identity)
.unwrap()
.add_partition_field("timestamp", "ts_hour", Transform::Hour)
.unwrap()
.add_partition_field("empty", "empty_void", Transform::Void)
.unwrap()
.build()
.unwrap();

let data = Struct::from_iter([Some(Literal::int(42)), Some(Literal::string("alice"))]);
let data = Struct::from_iter([
Some(Literal::int(42)),
Some(Literal::string("alice")),
Some(Literal::int(1000)),
Some(Literal::string("empty")),
]);

assert_eq!(
spec.partition_to_path(&data, schema.into()),
"id=42/name=alice"
"id=42/name=alice/ts_hour=1000/empty_void=null"
);
}
}
24 changes: 11 additions & 13 deletions crates/iceberg/src/spec/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,17 @@ pub enum Transform {
impl Transform {
/// Returns a human-readable String representation of a transformed value.
pub fn to_human_string(&self, field_type: &Type, value: Option<&Literal>) -> String {
if let Some(value) = value {
if let Some(value) = value.as_primitive_literal() {
let field_type = field_type.as_primitive_type().unwrap();
let datum = Datum::new(field_type.clone(), value);
match self {
Self::Identity => datum.to_human_string(),
Self::Void => "null".to_string(),
_ => {
todo!()
}
}
} else {
"null".to_string()
let Some(value) = value else {
return "null".to_string();
};

if let Some(value) = value.as_primitive_literal() {
let field_type = field_type.as_primitive_type().unwrap();
let datum = Datum::new(field_type.clone(), value);

match self {
Self::Void => "null".to_string(),
_ => datum.to_human_string(),
}
} else {
"null".to_string()
Expand Down
2 changes: 1 addition & 1 deletion crates/iceberg/src/spec/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@ impl Datum {
///
/// For string literals, this returns the raw string value without quotes.
/// For all other literals, it falls back to [`to_string()`].
pub(crate) fn to_human_string(&self) -> String {
pub fn to_human_string(&self) -> String {
match self.literal() {
PrimitiveLiteral::String(s) => s.to_string(),
_ => self.to_string(),
Expand Down
Loading