Skip to content

Commit 6afeeb0

Browse files
committed
Clippy & Fix existing tests
1 parent 9a6eff3 commit 6afeeb0

File tree

3 files changed

+73
-63
lines changed

3 files changed

+73
-63
lines changed

crates/catalog/memory/src/catalog.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ mod tests {
373373
let expected_sorted_order = SortOrder::builder()
374374
.with_order_id(0)
375375
.with_fields(vec![])
376-
.build(&expected_schema)
376+
.build(expected_schema)
377377
.unwrap();
378378

379379
assert_eq!(

crates/iceberg/src/spec/table_metadata.rs

+57-51
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ pub(crate) static EMPTY_SNAPSHOT_ID: i64 = -1;
4444
pub(crate) static INITIAL_SEQUENCE_NUMBER: i64 = 0;
4545

4646
/// Property key for the format version.
47-
pub const PROPERTY_FORMAT_VERSION: &'static str = "format-version";
47+
pub const PROPERTY_FORMAT_VERSION: &str = "format-version";
4848
/// Property key for max number of previous versions to keep.
49-
pub const PROPERTY_METADATA_PREVIOUS_VERSIONS_MAX: &'static str =
50-
"write.metadata.previous-versions-max";
49+
pub const PROPERTY_METADATA_PREVIOUS_VERSIONS_MAX: &str = "write.metadata.previous-versions-max";
5150
/// Default value for max number of previous versions to keep.
5251
pub const PROPERTY_METADATA_PREVIOUS_VERSIONS_MAX_DEFAULT: usize = 100;
5352

@@ -137,9 +136,9 @@ impl TableMetadata {
137136
format_version: FormatVersion,
138137
properties: HashMap<String, String>,
139138
) -> Result<Self> {
140-
super::table_metadata_builder::TableMetadataBuilder::new(
139+
Self::builder(
141140
schema,
142-
spec.into(),
141+
spec,
143142
sort_order,
144143
location,
145144
format_version,
@@ -149,19 +148,32 @@ impl TableMetadata {
149148
.map(|x| x.metadata)
150149
}
151150

151+
/// Create a new table metadata builder
152+
pub fn builder(
153+
schema: Schema,
154+
spec: impl Into<UnboundPartitionSpec>,
155+
sort_order: SortOrder,
156+
location: String,
157+
format_version: FormatVersion,
158+
properties: HashMap<String, String>,
159+
) -> Result<TableMetadataBuilder> {
160+
TableMetadataBuilder::new(
161+
schema,
162+
spec.into(),
163+
sort_order,
164+
location,
165+
format_version,
166+
properties,
167+
)
168+
}
169+
152170
/// Convert this Table Metadata into a builder for modification.
153171
///
154172
/// `current_file_location` is the location where the current version
155173
/// of the metadata file is stored. This is used to update the metadata log.
156174
#[must_use]
157-
pub fn into_builder(
158-
self,
159-
current_file_location: impl Into<String>,
160-
) -> super::table_metadata_builder::TableMetadataBuilder {
161-
super::table_metadata_builder::TableMetadataBuilder::new_from_metadata(
162-
self,
163-
current_file_location,
164-
)
175+
pub fn into_builder(self, current_file_location: impl Into<String>) -> TableMetadataBuilder {
176+
TableMetadataBuilder::new_from_metadata(self, current_file_location)
165177
}
166178

167179
/// Returns format version of this metadata.
@@ -277,7 +289,7 @@ impl TableMetadata {
277289
pub fn snapshot_for_ref(&self, ref_name: &str) -> Option<&SnapshotRef> {
278290
self.refs.get(ref_name).map(|r| {
279291
self.snapshot_by_id(r.snapshot_id)
280-
.expect(format!("Snapshot id of ref {} doesn't exist", ref_name).as_str())
292+
.unwrap_or_else(|| panic!("Snapshot id of ref {} doesn't exist", ref_name))
281293
})
282294
}
283295

@@ -397,16 +409,14 @@ impl TableMetadata {
397409
if let Some(current_snapshot_id) = self.current_snapshot_id {
398410
if current_snapshot_id == EMPTY_SNAPSHOT_ID {
399411
self.current_snapshot_id = None;
400-
} else {
401-
if self.snapshot_by_id(current_snapshot_id).is_none() {
402-
return Err(Error::new(
412+
} else if self.snapshot_by_id(current_snapshot_id).is_none() {
413+
return Err(Error::new(
403414
ErrorKind::DataInvalid,
404415
format!(
405416
"Snapshot for current snapshot id {} does not exist in the existing snapshots list",
406417
current_snapshot_id
407418
),
408419
));
409-
}
410420
}
411421
}
412422
Ok(())
@@ -438,30 +448,27 @@ impl TableMetadata {
438448
));
439449
}
440450
}
441-
} else {
442-
if main_ref.is_none() {
443-
return Err(Error::new(
444-
ErrorKind::DataInvalid,
445-
"Current snapshot is not set, but main branch exists",
446-
));
447-
}
451+
} else if main_ref.is_some() {
452+
return Err(Error::new(
453+
ErrorKind::DataInvalid,
454+
"Current snapshot is not set, but main branch exists",
455+
));
448456
}
449457

450458
Ok(())
451459
}
452460

453461
fn validate_format_version_specifics(&self) -> Result<()> {
454-
if self.format_version < FormatVersion::V2 {
455-
if self.last_sequence_number != 0 {
456-
return Err(Error::new(
457-
ErrorKind::DataInvalid,
458-
format!(
459-
"Last sequence number must be 0 in v1. Found {}",
460-
self.last_sequence_number
461-
),
462-
));
463-
}
462+
if self.format_version < FormatVersion::V2 && self.last_sequence_number != 0 {
463+
return Err(Error::new(
464+
ErrorKind::DataInvalid,
465+
format!(
466+
"Last sequence number must be 0 in v1. Found {}",
467+
self.last_sequence_number
468+
),
469+
));
464470
}
471+
465472
Ok(())
466473
}
467474

@@ -1132,7 +1139,7 @@ mod tests {
11321139
"current-schema-id" : 1,
11331140
"partition-specs": [
11341141
{
1135-
"spec-id": 1,
1142+
"spec-id": 0,
11361143
"fields": [
11371144
{
11381145
"source-id": 4,
@@ -1143,7 +1150,7 @@ mod tests {
11431150
]
11441151
}
11451152
],
1146-
"default-spec-id": 1,
1153+
"default-spec-id": 0,
11471154
"last-partition-id": 1000,
11481155
"properties": {
11491156
"commit.retry.num-retries": "1"
@@ -1154,7 +1161,12 @@ mod tests {
11541161
"timestamp-ms": 1515100
11551162
}
11561163
],
1157-
"sort-orders": [],
1164+
"sort-orders": [
1165+
{
1166+
"order-id": 0,
1167+
"fields": []
1168+
}
1169+
],
11581170
"default-sort-order-id": 0
11591171
}
11601172
"#;
@@ -1170,7 +1182,7 @@ mod tests {
11701182
.unwrap();
11711183

11721184
let partition_spec = PartitionSpec {
1173-
spec_id: 1,
1185+
spec_id: 0,
11741186
fields: vec![PartitionField {
11751187
name: "ts_day".to_string(),
11761188
transform: Transform::Day,
@@ -1187,11 +1199,11 @@ mod tests {
11871199
last_column_id: 1,
11881200
schemas: HashMap::from_iter(vec![(1, Arc::new(schema))]),
11891201
current_schema_id: 1,
1190-
partition_specs: HashMap::from_iter(vec![(1, partition_spec.into())]),
1191-
default_spec_id: 1,
1202+
partition_specs: HashMap::from_iter(vec![(0, partition_spec.into())]),
1203+
default_spec_id: 0,
11921204
last_partition_id: 1000,
11931205
default_sort_order_id: 0,
1194-
sort_orders: HashMap::from_iter(vec![]),
1206+
sort_orders: HashMap::from_iter(vec![(0, SortOrder::unsorted_order().into())]),
11951207
snapshots: HashMap::default(),
11961208
current_snapshot_id: None,
11971209
last_sequence_number: 1,
@@ -1663,21 +1675,15 @@ mod tests {
16631675
default_spec_id: 0,
16641676
last_partition_id: 0,
16651677
default_sort_order_id: 0,
1666-
sort_orders: HashMap::new(),
1678+
// Sort order is added during deserialization for V2 compatibility
1679+
sort_orders: HashMap::from_iter(vec![(0, SortOrder::unsorted_order().into())]),
16671680
snapshots: HashMap::new(),
16681681
current_snapshot_id: None,
16691682
last_sequence_number: 0,
16701683
properties: HashMap::new(),
16711684
snapshot_log: vec![],
16721685
metadata_log: Vec::new(),
1673-
refs: HashMap::from_iter(vec![("main".to_string(), SnapshotReference {
1674-
snapshot_id: -1,
1675-
retention: SnapshotRetention::Branch {
1676-
min_snapshots_to_keep: None,
1677-
max_snapshot_age_ms: None,
1678-
max_ref_age_ms: None,
1679-
},
1680-
})]),
1686+
refs: HashMap::new(),
16811687
};
16821688

16831689
check_table_metadata_serde(&metadata, expected);

crates/iceberg/src/spec/table_metadata_builder.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ pub struct TableMetadataBuildResult {
4545
impl TableMetadataBuilder {
4646
const LAST_ADDED: i32 = -1;
4747

48-
#[must_use]
4948
/// Create a `TableMetadata` object from scratch.
5049
///
5150
/// This method re-assign ids of fields in the schema, schema.id, sort_order.id and
@@ -76,7 +75,7 @@ impl TableMetadataBuilder {
7675
current_schema_id: -1, // Overwritten immediately by add_and_set_current_schema
7776
schemas: HashMap::new(),
7877
partition_specs: HashMap::new(),
79-
default_spec_id: DEFAULT_PARTITION_SPEC_ID,
78+
default_spec_id: DEFAULT_PARTITION_SPEC_ID, // Overwritten immediately by add_default_partition_spec
8079
last_partition_id: UNPARTITIONED_LAST_ASSIGNED_ID,
8180
properties: HashMap::new(),
8281
current_snapshot_id: None,
@@ -160,7 +159,7 @@ impl TableMetadataBuilder {
160159
/// Get the current schema with all changes applied up to this point.
161160
#[inline]
162161
pub fn current_schema(&self) -> &SchemaRef {
163-
&self.metadata.current_schema()
162+
self.metadata.current_schema()
164163
}
165164

166165
/// Get the current last column id
@@ -534,7 +533,7 @@ impl TableMetadataBuilder {
534533
// Set schema-id
535534
let schema = match new_schema_id == schema.schema_id() {
536535
true => schema,
537-
false => schema.with_schema_id(new_schema_id.into()),
536+
false => schema.with_schema_id(new_schema_id),
538537
};
539538

540539
self.metadata
@@ -826,10 +825,10 @@ impl TableMetadataBuilder {
826825
let partition_spec = Arc::unwrap_or_clone(self.get_default_partition_spec()?);
827826
let sort_order = Arc::unwrap_or_clone(self.get_default_sort_order()?);
828827

829-
partition_spec.to_unbound().bind(&schema)?;
828+
partition_spec.to_unbound().bind(schema)?;
830829
SortOrder::builder()
831830
.with_fields(sort_order.fields)
832-
.build(&schema)?;
831+
.build(schema)?;
833832

834833
self.expire_metadata_log();
835834
self.update_snapshot_log()?;
@@ -857,7 +856,7 @@ impl TableMetadataBuilder {
857856
if self.metadata.metadata_log.len() > max_size {
858857
self.metadata
859858
.metadata_log
860-
.drain(0..self.metadata.metadata_log.len() - max_size as usize);
859+
.drain(0..self.metadata.metadata_log.len() - max_size);
861860
}
862861
}
863862

@@ -958,7 +957,7 @@ impl TableMetadataBuilder {
958957

959958
// Re-build partition spec with new ids
960959
let mut fresh_spec = PartitionSpecBuilder::new(&fresh_schema);
961-
for field in spec.fields().to_owned() {
960+
for field in spec.fields() {
962961
let source_field_name = previous_id_to_name.get(&field.source_id).ok_or_else(|| {
963962
Error::new(
964963
ErrorKind::DataInvalid,
@@ -969,7 +968,7 @@ impl TableMetadataBuilder {
969968
)
970969
})?;
971970
fresh_spec =
972-
fresh_spec.add_partition_field(source_field_name, field.name, field.transform)?;
971+
fresh_spec.add_partition_field(source_field_name, &field.name, field.transform)?;
973972
}
974973
let fresh_spec = fresh_spec.build()?;
975974

@@ -1007,7 +1006,7 @@ impl TableMetadataBuilder {
10071006
fn handle_set_format_version_property(self, format_version: &str) -> Result<Self> {
10081007
// format_version is either "1" or "2" and should not be persisted in the properties.
10091008
let format_version =
1010-
serde_json::from_str::<FormatVersion>(&format_version).map_err(|_e| {
1009+
serde_json::from_str::<FormatVersion>(format_version).map_err(|_e| {
10111010
Error::new(
10121011
ErrorKind::DataInvalid,
10131012
format!(
@@ -1093,7 +1092,6 @@ impl TableMetadataBuilder {
10931092
.unwrap_or_else(|| {
10941093
self.get_highest_spec_id()
10951094
.unwrap_or(DEFAULT_PARTITION_SPEC_ID)
1096-
+ 1
10971095
})
10981096
}
10991097

@@ -1124,3 +1122,9 @@ impl TableMetadataBuilder {
11241122
self.metadata.sort_orders.keys().max().copied()
11251123
}
11261124
}
1125+
1126+
impl From<TableMetadataBuildResult> for TableMetadata {
1127+
fn from(result: TableMetadataBuildResult) -> Self {
1128+
result.metadata
1129+
}
1130+
}

0 commit comments

Comments
 (0)