Skip to content

Commit a5aba9a

Browse files
authored
feat: SortOrder methods should take schema ref if possible (apache#613)
* SortOrder methods should take schema ref if possible * Fix test type * with_order_id should not take reference
1 parent 5812399 commit a5aba9a

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

crates/catalog/memory/src/catalog.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ mod tests {
371371
let expected_sorted_order = SortOrder::builder()
372372
.with_order_id(0)
373373
.with_fields(vec![])
374-
.build(expected_schema.clone())
374+
.build(expected_schema)
375375
.unwrap();
376376

377377
assert_eq!(

crates/iceberg/src/spec/sort.rs

+43-6
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ impl SortOrder {
133133
pub fn is_unsorted(&self) -> bool {
134134
self.fields.is_empty()
135135
}
136+
137+
/// Set the order id for the sort order
138+
pub fn with_order_id(self, order_id: i64) -> SortOrder {
139+
SortOrder {
140+
order_id,
141+
fields: self.fields,
142+
}
143+
}
136144
}
137145

138146
impl SortOrderBuilder {
@@ -160,13 +168,13 @@ impl SortOrderBuilder {
160168
}
161169

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

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

301+
#[test]
302+
fn test_build_unbound_returns_correct_default_order_id_for_no_fields() {
303+
assert_eq!(
304+
SortOrder::builder()
305+
.build_unbound()
306+
.expect("Expected an Ok value")
307+
.order_id,
308+
SortOrder::UNSORTED_ORDER_ID
309+
)
310+
}
311+
312+
#[test]
313+
fn test_build_unbound_returns_correct_default_order_id_for_fields() {
314+
let sort_field = SortField::builder()
315+
.source_id(2)
316+
.direction(SortDirection::Ascending)
317+
.null_order(NullOrder::First)
318+
.transform(Transform::Identity)
319+
.build();
320+
assert_ne!(
321+
SortOrder::builder()
322+
.with_sort_field(sort_field.clone())
323+
.build_unbound()
324+
.expect("Expected an Ok value")
325+
.order_id,
326+
SortOrder::UNSORTED_ORDER_ID
327+
)
328+
}
329+
293330
#[test]
294331
fn test_build_unbound_should_return_unsorted_sort_order() {
295332
assert_eq!(
@@ -367,7 +404,7 @@ mod tests {
367404
.transform(Transform::Identity)
368405
.build(),
369406
)
370-
.build(schema);
407+
.build(&schema);
371408

372409
assert_eq!(
373410
sort_order_builder_result
@@ -406,7 +443,7 @@ mod tests {
406443
.transform(Transform::Identity)
407444
.build(),
408445
)
409-
.build(schema);
446+
.build(&schema);
410447

411448
assert_eq!(
412449
sort_order_builder_result
@@ -438,7 +475,7 @@ mod tests {
438475
.transform(Transform::Year)
439476
.build(),
440477
)
441-
.build(schema);
478+
.build(&schema);
442479

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

469506
let sort_order_builder_result = SortOrder::builder()
470507
.with_sort_field(sort_field.clone())
471-
.build(schema);
508+
.build(&schema);
472509

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

0 commit comments

Comments
 (0)