Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: SortOrder methods should take schema ref if possible #613

Merged
merged 3 commits into from
Sep 8, 2024
Merged
Changes from 1 commit
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
Next Next commit
SortOrder methods should take schema ref if possible
c-thiel committed Sep 8, 2024

Verified

This commit was signed with the committer’s verified signature.
c-thiel Christian
commit dd93910ee2418c85aaedacf72eeef51c435d4147
49 changes: 43 additions & 6 deletions crates/iceberg/src/spec/sort.rs
Original file line number Diff line number Diff line change
@@ -133,6 +133,14 @@ impl SortOrder {
pub fn is_unsorted(&self) -> bool {
self.fields.is_empty()
}

/// Set the order id for the sort order
pub fn with_order_id(&self, order_id: i64) -> SortOrder {
SortOrder {
order_id,
fields: self.fields.clone(),
}
}
}

impl SortOrderBuilder {
@@ -160,13 +168,13 @@ impl SortOrderBuilder {
}

/// Creates a new bound sort order.
pub fn build(&self, schema: Schema) -> Result<SortOrder> {
pub fn build(&self, schema: &Schema) -> Result<SortOrder> {
let unbound_sort_order = self.build_unbound()?;
SortOrderBuilder::check_compatibility(unbound_sort_order, schema)
}

/// Returns the given sort order if it is compatible with the given schema
fn check_compatibility(sort_order: SortOrder, schema: Schema) -> Result<SortOrder> {
fn check_compatibility(sort_order: SortOrder, schema: &Schema) -> Result<SortOrder> {
let sort_fields = &sort_order.fields;
for sort_field in sort_fields {
match schema.field_by_id(sort_field.source_id) {
@@ -290,6 +298,35 @@ mod tests {
)
}

#[test]
fn test_build_unbound_returns_correct_default_order_id_for_no_fields() {
assert_eq!(
SortOrder::builder()
.build_unbound()
.expect("Expected an Ok value")
.order_id,
SortOrder::UNSORTED_ORDER_ID
)
}

#[test]
fn test_build_unbound_returns_correct_default_order_id_for_fields() {
let sort_field = SortField::builder()
.source_id(2)
.direction(SortDirection::Ascending)
.null_order(NullOrder::First)
.transform(Transform::Identity)
.build();
assert_ne!(
SortOrder::builder()
.with_sort_field(sort_field.clone())
.build_unbound()
.expect("Expected an Ok value")
.order_id,
SortOrder::UNSORTED_ORDER_ID
)
}

#[test]
fn test_build_unbound_should_return_unsorted_sort_order() {
assert_eq!(
@@ -367,7 +404,7 @@ mod tests {
.transform(Transform::Identity)
.build(),
)
.build(schema);
.build(&schema);

assert_eq!(
sort_order_builder_result
@@ -406,7 +443,7 @@ mod tests {
.transform(Transform::Identity)
.build(),
)
.build(schema);
.build(&schema);

assert_eq!(
sort_order_builder_result
@@ -438,7 +475,7 @@ mod tests {
.transform(Transform::Year)
.build(),
)
.build(schema);
.build(&schema);

assert_eq!(
sort_order_builder_result
@@ -468,7 +505,7 @@ mod tests {

let sort_order_builder_result = SortOrder::builder()
.with_sort_field(sort_field.clone())
.build(schema);
.build(&schema);

assert_eq!(
sort_order_builder_result.expect("Expected an Ok value"),