1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
/*!
Defines the [table metadata](https://iceberg.apache.org/spec/#table-metadata).
The main struct here is [TableMetadataV2] which defines the data for a table.
*/
use std::{
collections::HashMap,
time::{SystemTime, UNIX_EPOCH},
};
use crate::{
error::Error,
spec::{partition::PartitionSpec, snapshot::Reference, sort},
};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use uuid::Uuid;
use derive_builder::Builder;
use super::{schema::Schema, snapshot::Snapshot};
pub static MAIN_BRANCH: &str = "main";
static DEFAULT_SORT_ORDER_ID: i64 = 0;
static DEFAULT_SPEC_ID: i32 = 0;
use _serde::TableMetadataEnum;
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Default, Builder)]
#[serde(try_from = "TableMetadataEnum", into = "TableMetadataEnum")]
/// Fields for the version 2 of the table metadata.
pub struct TableMetadata {
#[builder(default)]
/// Integer Version for the format.
pub format_version: FormatVersion,
#[builder(default = "Uuid::new_v4()")]
/// A UUID that identifies the table
pub table_uuid: Uuid,
#[builder(setter(into))]
/// Location tables base location
pub location: String,
#[builder(default)]
/// The tables highest sequence number
pub last_sequence_number: i64,
#[builder(
default = "SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_micros() as i64"
)]
/// Timestamp in milliseconds from the unix epoch when the table was last updated.
pub last_updated_ms: i64,
#[builder(default)]
/// An integer; the highest assigned column ID for the table.
pub last_column_id: i32,
#[builder(setter(each(name = "with_schema")))]
/// A list of schemas, stored as objects with schema-id.
pub schemas: HashMap<i32, Schema>,
/// ID of the table’s current schema.
pub current_schema_id: i32,
#[builder(
setter(each(name = "with_partition_spec")),
default = "HashMap::from_iter(vec![(0,PartitionSpec::default())])"
)]
/// A list of partition specs, stored as full partition spec objects.
pub partition_specs: HashMap<i32, PartitionSpec>,
#[builder(default)]
/// ID of the “current” spec that writers should use by default.
pub default_spec_id: i32,
#[builder(default)]
/// An integer; the highest assigned partition field ID across all partition specs for the table.
pub last_partition_id: i32,
///A string to string map of table properties. This is used to control settings that
/// affect reading and writing and is not intended to be used for arbitrary metadata.
/// For example, commit.retry.num-retries is used to control the number of commit retries.
#[builder(default)]
pub properties: HashMap<String, String>,
/// long ID of the current table snapshot; must be the same as the current
/// ID of the main branch in refs.
#[builder(default)]
pub current_snapshot_id: Option<i64>,
///A list of valid snapshots. Valid snapshots are snapshots for which all
/// data files exist in the file system. A data file must not be deleted
/// from the file system until the last snapshot in which it was listed is
/// garbage collected.
#[builder(default)]
pub snapshots: HashMap<i64, Snapshot>,
/// A list (optional) of timestamp and snapshot ID pairs that encodes changes
/// to the current snapshot for the table. Each time the current-snapshot-id
/// is changed, a new entry should be added with the last-updated-ms
/// and the new current-snapshot-id. When snapshots are expired from
/// the list of valid snapshots, all entries before a snapshot that has
/// expired should be removed.
#[builder(default)]
pub snapshot_log: Vec<SnapshotLog>,
/// A list (optional) of timestamp and metadata file location pairs
/// that encodes changes to the previous metadata files for the table.
/// Each time a new metadata file is created, a new entry of the
/// previous metadata file location should be added to the list.
/// Tables can be configured to remove oldest metadata log entries and
/// keep a fixed-size log of the most recent entries after a commit.
#[builder(default)]
pub metadata_log: Vec<MetadataLog>,
#[builder(setter(each(name = "with_sort_order")), default)]
/// A list of sort orders, stored as full sort order objects.
pub sort_orders: HashMap<i64, sort::SortOrder>,
#[builder(default)]
/// Default sort order id of the table. Note that this could be used by
/// writers, but is not used when reading because reads use the specs
/// stored in manifest files.
pub default_sort_order_id: i64,
///A map of snapshot references. The map keys are the unique snapshot reference
/// names in the table, and the map values are snapshot reference objects.
/// There is always a main branch reference pointing to the current-snapshot-id
/// even if the refs map is null.
#[builder(default)]
pub refs: HashMap<String, Reference>,
}
impl TableMetadata {
/// Get current schema
#[inline]
pub fn current_schema(&self, branch: Option<&str>) -> Result<&Schema, Error> {
let schema_id = self
.current_snapshot(branch)?
.and_then(|x| x.schema_id)
.unwrap_or(self.current_schema_id);
self.schemas
.get(&schema_id)
.ok_or_else(|| Error::InvalidFormat("schema".to_string()))
}
/// Get schema for snapshot
#[inline]
pub fn schema(&self, snapshot_id: i64) -> Result<&Schema, Error> {
let schema_id = self
.snapshots
.get(&snapshot_id)
.and_then(|x| x.schema_id)
.unwrap_or(self.current_schema_id);
self.schemas
.get(&schema_id)
.ok_or_else(|| Error::InvalidFormat("schema".to_string()))
}
/// Get default partition spec
#[inline]
pub fn default_partition_spec(&self) -> Result<&PartitionSpec, Error> {
self.partition_specs
.get(&self.default_spec_id)
.ok_or_else(|| Error::InvalidFormat("partition spec".to_string()))
}
/// Get current snapshot
#[inline]
pub fn current_snapshot(&self, snapshot_ref: Option<&str>) -> Result<Option<&Snapshot>, Error> {
let snapshot_id = match snapshot_ref {
None => self.current_snapshot_id,
Some(reference) => self.refs.get(reference).map(|x| x.snapshot_id),
};
match snapshot_id {
Some(snapshot_id) => Ok(self.snapshots.get(&snapshot_id)),
None => {
if self.snapshots.is_empty() {
Ok(None)
} else {
Err(Error::InvalidFormat("snapshots".to_string()))
}
}
}
}
/// Get current snapshot
#[inline]
pub fn current_snapshot_mut(
&mut self,
snapshot_ref: Option<String>,
) -> Result<Option<&mut Snapshot>, Error> {
let snapshot_id = match snapshot_ref {
None => self.current_snapshot_id,
Some(reference) => self.refs.get(&reference).map(|x| x.snapshot_id),
};
match snapshot_id {
Some(-1) => {
if self.snapshots.is_empty() {
Ok(None)
} else {
Err(Error::InvalidFormat("snapshots".to_string()))
}
}
Some(snapshot_id) => Ok(self.snapshots.get_mut(&snapshot_id)),
None => {
if self.snapshots.is_empty() {
Ok(None)
} else {
Err(Error::InvalidFormat("snapshots".to_string()))
}
}
}
}
}
pub fn new_metadata_location(metadata: &TableMetadata) -> Result<String, Error> {
let transaction_uuid = Uuid::new_v4();
let version = metadata.last_sequence_number;
Ok(metadata.location.to_string()
+ "/metadata/"
+ &version.to_string()
+ "-"
+ &transaction_uuid.to_string()
+ ".metadata.json")
}
mod _serde {
use std::collections::HashMap;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::{
error::Error,
spec::{
partition::{PartitionField, PartitionSpec},
schema,
snapshot::{
Reference, Retention,
_serde::{SnapshotV1, SnapshotV2},
},
sort,
},
};
use super::{
FormatVersion, MetadataLog, SnapshotLog, TableMetadata, VersionNumber,
DEFAULT_SORT_ORDER_ID, DEFAULT_SPEC_ID, MAIN_BRANCH,
};
/// Metadata of an iceberg table
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub(super) enum TableMetadataEnum {
/// Version 2 of the table metadata
V2(TableMetadataV2),
/// Version 1 of the table metadata
V1(TableMetadataV1),
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
/// Fields for the version 2 of the table metadata.
pub(crate) struct TableMetadataV2 {
/// Integer Version for the format.
pub format_version: VersionNumber<2>,
/// A UUID that identifies the table
pub table_uuid: Uuid,
/// Location tables base location
pub location: String,
/// The tables highest sequence number
pub last_sequence_number: i64,
/// Timestamp in milliseconds from the unix epoch when the table was last updated.
pub last_updated_ms: i64,
/// An integer; the highest assigned column ID for the table.
pub last_column_id: i32,
/// A list of schemas, stored as objects with schema-id.
pub schemas: Vec<schema::SchemaV2>,
/// ID of the table’s current schema.
pub current_schema_id: i32,
/// A list of partition specs, stored as full partition spec objects.
pub partition_specs: Vec<PartitionSpec>,
/// ID of the “current” spec that writers should use by default.
pub default_spec_id: i32,
/// An integer; the highest assigned partition field ID across all partition specs for the table.
pub last_partition_id: i32,
///A string to string map of table properties. This is used to control settings that
/// affect reading and writing and is not intended to be used for arbitrary metadata.
/// For example, commit.retry.num-retries is used to control the number of commit retries.
#[serde(skip_serializing_if = "HashMap::is_empty", default)]
pub properties: HashMap<String, String>,
/// long ID of the current table snapshot; must be the same as the current
/// ID of the main branch in refs.
#[serde(skip_serializing_if = "Option::is_none")]
pub current_snapshot_id: Option<i64>,
///A list of valid snapshots. Valid snapshots are snapshots for which all
/// data files exist in the file system. A data file must not be deleted
/// from the file system until the last snapshot in which it was listed is
/// garbage collected.
#[serde(skip_serializing_if = "Option::is_none")]
pub snapshots: Option<Vec<SnapshotV2>>,
/// A list (optional) of timestamp and snapshot ID pairs that encodes changes
/// to the current snapshot for the table. Each time the current-snapshot-id
/// is changed, a new entry should be added with the last-updated-ms
/// and the new current-snapshot-id. When snapshots are expired from
/// the list of valid snapshots, all entries before a snapshot that has
/// expired should be removed.
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub snapshot_log: Vec<SnapshotLog>,
/// A list (optional) of timestamp and metadata file location pairs
/// that encodes changes to the previous metadata files for the table.
/// Each time a new metadata file is created, a new entry of the
/// previous metadata file location should be added to the list.
/// Tables can be configured to remove oldest metadata log entries and
/// keep a fixed-size log of the most recent entries after a commit.
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub metadata_log: Vec<MetadataLog>,
/// A list of sort orders, stored as full sort order objects.
pub sort_orders: Vec<sort::SortOrder>,
/// Default sort order id of the table. Note that this could be used by
/// writers, but is not used when reading because reads use the specs
/// stored in manifest files.
pub default_sort_order_id: i64,
///A map of snapshot references. The map keys are the unique snapshot reference
/// names in the table, and the map values are snapshot reference objects.
/// There is always a main branch reference pointing to the current-snapshot-id
/// even if the refs map is null.
#[serde(skip_serializing_if = "HashMap::is_empty", default)]
pub refs: HashMap<String, Reference>,
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
/// Fields for the version 1 of the table metadata.
pub(super) struct TableMetadataV1 {
/// Integer Version for the format.
pub format_version: VersionNumber<1>,
/// A UUID that identifies the table
#[serde(skip_serializing_if = "Option::is_none")]
pub table_uuid: Option<Uuid>,
/// Location tables base location
pub location: String,
/// Timestamp in milliseconds from the unix epoch when the table was last updated.
pub last_updated_ms: i64,
/// An integer; the highest assigned column ID for the table.
pub last_column_id: i32,
/// The table’s current schema.
pub schema: schema::SchemaV1,
/// A list of schemas, stored as objects with schema-id.
#[serde(skip_serializing_if = "Option::is_none")]
pub schemas: Option<Vec<schema::SchemaV1>>,
/// ID of the table’s current schema.
#[serde(skip_serializing_if = "Option::is_none")]
pub current_schema_id: Option<i32>,
/// The table’s current partition spec, stored as only fields. Note that this is used by writers to partition data,
/// but is not used when reading because reads use the specs stored in manifest files.
pub partition_spec: Vec<PartitionField>,
/// A list of partition specs, stored as full partition spec objects.
#[serde(skip_serializing_if = "Option::is_none")]
pub partition_specs: Option<Vec<PartitionSpec>>,
/// ID of the “current” spec that writers should use by default.
#[serde(skip_serializing_if = "Option::is_none")]
pub default_spec_id: Option<i32>,
/// An integer; the highest assigned partition field ID across all partition specs for the table.
#[serde(skip_serializing_if = "Option::is_none")]
pub last_partition_id: Option<i32>,
///A string to string map of table properties. This is used to control settings that
/// affect reading and writing and is not intended to be used for arbitrary metadata.
/// For example, commit.retry.num-retries is used to control the number of commit retries.
#[serde(skip_serializing_if = "HashMap::is_empty", default)]
pub properties: HashMap<String, String>,
/// long ID of the current table snapshot; must be the same as the current
/// ID of the main branch in refs.
#[serde(skip_serializing_if = "Option::is_none")]
pub current_snapshot_id: Option<i64>,
///A list of valid snapshots. Valid snapshots are snapshots for which all
/// data files exist in the file system. A data file must not be deleted
/// from the file system until the last snapshot in which it was listed is
/// garbage collected.
#[serde(skip_serializing_if = "Option::is_none")]
pub snapshots: Option<Vec<SnapshotV1>>,
/// A list (optional) of timestamp and snapshot ID pairs that encodes changes
/// to the current snapshot for the table. Each time the current-snapshot-id
/// is changed, a new entry should be added with the last-updated-ms
/// and the new current-snapshot-id. When snapshots are expired from
/// the list of valid snapshots, all entries before a snapshot that has
/// expired should be removed.
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub snapshot_log: Vec<SnapshotLog>,
/// A list (optional) of timestamp and metadata file location pairs
/// that encodes changes to the previous metadata files for the table.
/// Each time a new metadata file is created, a new entry of the
/// previous metadata file location should be added to the list.
/// Tables can be configured to remove oldest metadata log entries and
/// keep a fixed-size log of the most recent entries after a commit.
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub metadata_log: Vec<MetadataLog>,
/// A list of sort orders, stored as full sort order objects.
pub sort_orders: Option<Vec<sort::SortOrder>>,
/// Default sort order id of the table. Note that this could be used by
/// writers, but is not used when reading because reads use the specs
/// stored in manifest files.
pub default_sort_order_id: Option<i64>,
}
impl TryFrom<TableMetadataEnum> for TableMetadata {
type Error = Error;
fn try_from(value: TableMetadataEnum) -> Result<Self, Error> {
match value {
TableMetadataEnum::V2(value) => value.try_into(),
TableMetadataEnum::V1(value) => value.try_into(),
}
}
}
impl From<TableMetadata> for TableMetadataEnum {
fn from(value: TableMetadata) -> Self {
match value.format_version {
FormatVersion::V2 => TableMetadataEnum::V2(value.into()),
FormatVersion::V1 => TableMetadataEnum::V1(value.into()),
}
}
}
impl TryFrom<TableMetadataV2> for TableMetadata {
type Error = Error;
fn try_from(value: TableMetadataV2) -> Result<Self, Error> {
let current_snapshot_id = if let &Some(-1) = &value.current_snapshot_id {
None
} else {
value.current_snapshot_id
};
let schemas = HashMap::from_iter(
value
.schemas
.into_iter()
.map(|schema| Ok((schema.schema_id, schema.try_into()?)))
.collect::<Result<Vec<_>, Error>>()?,
);
let mut refs = value.refs;
if let Some(snapshot_id) = current_snapshot_id {
refs.entry(MAIN_BRANCH.to_string()).or_insert(Reference {
snapshot_id,
retention: Retention::default(),
});
}
Ok(TableMetadata {
format_version: FormatVersion::V2,
table_uuid: value.table_uuid,
location: value.location,
last_sequence_number: value.last_sequence_number,
last_updated_ms: value.last_updated_ms,
last_column_id: value.last_column_id,
current_schema_id: if schemas.keys().contains(&value.current_schema_id) {
Ok(value.current_schema_id)
} else {
Err(Error::InvalidFormat("schema".to_string()))
}?,
schemas,
partition_specs: HashMap::from_iter(
value.partition_specs.into_iter().map(|x| (x.spec_id, x)),
),
default_spec_id: value.default_spec_id,
last_partition_id: value.last_partition_id,
properties: value.properties,
current_snapshot_id,
snapshots: value
.snapshots
.map(|snapshots| {
HashMap::from_iter(snapshots.into_iter().map(|x| (x.snapshot_id, x.into())))
})
.unwrap_or_default(),
snapshot_log: value.snapshot_log,
metadata_log: value.metadata_log,
sort_orders: HashMap::from_iter(
value
.sort_orders
.into_iter()
.map(|x| (x.order_id as i64, x)),
),
default_sort_order_id: value.default_sort_order_id,
refs,
})
}
}
impl TryFrom<TableMetadataV1> for TableMetadata {
type Error = Error;
fn try_from(value: TableMetadataV1) -> Result<Self, Error> {
let schemas = value
.schemas
.map(|schemas| {
Ok::<_, Error>(HashMap::from_iter(
schemas
.into_iter()
.enumerate()
.map(|(i, schema)| {
Ok((schema.schema_id.unwrap_or(i as i32), schema.try_into()?))
})
.collect::<Result<Vec<_>, Error>>()?
.into_iter(),
))
})
.or_else(|| {
Some(Ok(HashMap::from_iter(vec![(
value.schema.schema_id.unwrap_or(0),
value.schema.try_into().ok()?,
)])))
})
.transpose()?
.unwrap_or_default();
let partition_specs = HashMap::from_iter(
value
.partition_specs
.unwrap_or_else(|| {
vec![PartitionSpec {
spec_id: DEFAULT_SPEC_ID,
fields: value.partition_spec,
}]
})
.into_iter()
.map(|x| (x.spec_id, x)),
);
Ok(TableMetadata {
format_version: FormatVersion::V1,
table_uuid: value.table_uuid.unwrap_or_default(),
location: value.location,
last_sequence_number: 0,
last_updated_ms: value.last_updated_ms,
last_column_id: value.last_column_id,
current_schema_id: value
.current_schema_id
.unwrap_or_else(|| schemas.keys().copied().max().unwrap_or_default()),
default_spec_id: value
.default_spec_id
.unwrap_or_else(|| partition_specs.keys().copied().max().unwrap_or_default()),
last_partition_id: value
.last_partition_id
.unwrap_or_else(|| partition_specs.keys().copied().max().unwrap_or_default()),
partition_specs,
schemas,
properties: value.properties,
current_snapshot_id: value.current_snapshot_id,
snapshots: value
.snapshots
.map(|snapshots| {
Ok::<_, Error>(HashMap::from_iter(
snapshots
.into_iter()
.map(|x| Ok((x.snapshot_id, x.into())))
.collect::<Result<Vec<_>, Error>>()?,
))
})
.transpose()?
.unwrap_or_default(),
snapshot_log: value.snapshot_log,
metadata_log: value.metadata_log,
sort_orders: match value.sort_orders {
Some(sort_orders) => {
HashMap::from_iter(sort_orders.into_iter().map(|x| (x.order_id as i64, x)))
}
None => HashMap::new(),
},
default_sort_order_id: value.default_sort_order_id.unwrap_or(DEFAULT_SORT_ORDER_ID),
refs: HashMap::from_iter(vec![(
MAIN_BRANCH.to_string(),
Reference {
snapshot_id: value.current_snapshot_id.unwrap_or_default(),
retention: Retention::Branch {
min_snapshots_to_keep: None,
max_snapshot_age_ms: None,
max_ref_age_ms: None,
},
},
)]),
})
}
}
impl From<TableMetadata> for TableMetadataV2 {
fn from(v: TableMetadata) -> Self {
TableMetadataV2 {
format_version: VersionNumber::<2>,
table_uuid: v.table_uuid,
location: v.location,
last_sequence_number: v.last_sequence_number,
last_updated_ms: v.last_updated_ms,
last_column_id: v.last_column_id,
schemas: v.schemas.into_values().map(|x| x.into()).collect(),
current_schema_id: v.current_schema_id,
partition_specs: v.partition_specs.into_values().collect(),
default_spec_id: v.default_spec_id,
last_partition_id: v.last_partition_id,
properties: v.properties,
current_snapshot_id: v.current_snapshot_id.or(Some(-1)),
snapshots: Some(v.snapshots.into_values().map(|x| x.into()).collect()),
snapshot_log: v.snapshot_log,
metadata_log: v.metadata_log,
sort_orders: v.sort_orders.into_values().collect(),
default_sort_order_id: v.default_sort_order_id,
refs: v.refs,
}
}
}
impl From<TableMetadata> for TableMetadataV1 {
fn from(v: TableMetadata) -> Self {
TableMetadataV1 {
format_version: VersionNumber::<1>,
table_uuid: Some(v.table_uuid),
location: v.location,
last_updated_ms: v.last_updated_ms,
last_column_id: v.last_column_id,
schema: v.schemas.get(&v.current_schema_id).unwrap().clone().into(),
schemas: Some(v.schemas.into_values().map(|x| x.into()).collect()),
current_schema_id: Some(v.current_schema_id),
partition_spec: v
.partition_specs
.get(&v.default_spec_id)
.map(|x| x.fields.clone())
.unwrap_or_default(),
partition_specs: Some(v.partition_specs.into_values().collect()),
default_spec_id: Some(v.default_spec_id),
last_partition_id: Some(v.last_partition_id),
properties: v.properties,
current_snapshot_id: v.current_snapshot_id.or(Some(-1)),
snapshots: Some(v.snapshots.into_values().map(|x| x.into()).collect()),
snapshot_log: v.snapshot_log,
metadata_log: v.metadata_log,
sort_orders: Some(v.sort_orders.into_values().collect()),
default_sort_order_id: Some(v.default_sort_order_id),
}
}
}
}
/// Helper to serialize and deserialize the format version.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct VersionNumber<const V: u8>;
impl<const V: u8> Serialize for VersionNumber<V> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_u8(V)
}
}
impl<'de, const V: u8> Deserialize<'de> for VersionNumber<V> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = u8::deserialize(deserializer)?;
if value == V {
Ok(VersionNumber::<V>)
} else {
Err(serde::de::Error::custom("Invalid Version"))
}
}
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
#[serde(rename_all = "kebab-case")]
/// Encodes changes to the previous metadata files for the table
pub struct MetadataLog {
/// The file for the log.
pub metadata_file: String,
/// Time new metadata was created
pub timestamp_ms: i64,
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
#[serde(rename_all = "kebab-case")]
/// A log of when each snapshot was made.
pub struct SnapshotLog {
/// Id of the snapshot.
pub snapshot_id: i64,
/// Last updated timestamp
pub timestamp_ms: i64,
}
#[derive(Debug, Serialize_repr, Deserialize_repr, PartialEq, Eq, Clone)]
#[repr(u8)]
/// Iceberg format version
#[derive(Default)]
pub enum FormatVersion {
/// Iceberg spec version 1
V1 = b'1',
/// Iceberg spec version 2
#[default]
V2 = b'2',
}
impl TryFrom<u8> for FormatVersion {
type Error = Error;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match char::from_u32(value as u32)
.ok_or_else(|| Error::Conversion("u8".to_string(), "char".to_string()))?
{
'1' => Ok(FormatVersion::V1),
'2' => Ok(FormatVersion::V2),
_ => Err(Error::Conversion(
"u8".to_string(),
"format version".to_string(),
)),
}
}
}
impl From<FormatVersion> for u8 {
fn from(value: FormatVersion) -> Self {
match value {
FormatVersion::V1 => b'1',
FormatVersion::V2 => b'2',
}
}
}
#[cfg(test)]
mod tests {
use crate::{error::Error, spec::table_metadata::TableMetadata};
#[test]
fn test_deserialize_table_data_v2() -> Result<(), Error> {
let data = r#"
{
"format-version" : 2,
"table-uuid": "fb072c92-a02b-11e9-ae9c-1bb7bc9eca94",
"location": "s3://b/wh/data.db/table",
"last-sequence-number" : 1,
"last-updated-ms": 1515100955770,
"last-column-id": 1,
"schemas": [
{
"schema-id" : 1,
"type" : "struct",
"fields" :[
{
"id": 1,
"name": "struct_name",
"required": true,
"type": "fixed[1]"
}
]
}
],
"current-schema-id" : 1,
"partition-specs": [
{
"spec-id": 1,
"fields": [
{
"source-id": 4,
"field-id": 1000,
"name": "ts_day",
"transform": "day"
}
]
}
],
"default-spec-id": 1,
"last-partition-id": 1,
"properties": {
"commit.retry.num-retries": "1"
},
"metadata-log": [
{
"metadata-file": "s3://bucket/.../v1.json",
"timestamp-ms": 1515100
}
],
"sort-orders": [],
"default-sort-order-id": 0
}
"#;
let metadata =
serde_json::from_str::<TableMetadata>(data).expect("Failed to deserialize json");
//test serialise deserialise works.
let metadata_two: TableMetadata = serde_json::from_str(
&serde_json::to_string(&metadata).expect("Failed to serialize metadata"),
)
.expect("Failed to serialize json");
assert_eq!(metadata, metadata_two);
Ok(())
}
#[test]
fn test_deserialize_table_data_v1() -> Result<(), Error> {
let data = r#"
{
"format-version" : 1,
"table-uuid" : "df838b92-0b32-465d-a44e-d39936e538b7",
"location" : "/home/iceberg/warehouse/nyc/taxis",
"last-updated-ms" : 1662532818843,
"last-column-id" : 5,
"schema" : {
"type" : "struct",
"schema-id" : 0,
"fields" : [ {
"id" : 1,
"name" : "vendor_id",
"required" : false,
"type" : "long"
}, {
"id" : 2,
"name" : "trip_id",
"required" : false,
"type" : "long"
}, {
"id" : 3,
"name" : "trip_distance",
"required" : false,
"type" : "float"
}, {
"id" : 4,
"name" : "fare_amount",
"required" : false,
"type" : "double"
}, {
"id" : 5,
"name" : "store_and_fwd_flag",
"required" : false,
"type" : "string"
} ]
},
"current-schema-id" : 0,
"schemas" : [ {
"type" : "struct",
"schema-id" : 0,
"fields" : [ {
"id" : 1,
"name" : "vendor_id",
"required" : false,
"type" : "long"
}, {
"id" : 2,
"name" : "trip_id",
"required" : false,
"type" : "long"
}, {
"id" : 3,
"name" : "trip_distance",
"required" : false,
"type" : "float"
}, {
"id" : 4,
"name" : "fare_amount",
"required" : false,
"type" : "double"
}, {
"id" : 5,
"name" : "store_and_fwd_flag",
"required" : false,
"type" : "string"
} ]
} ],
"partition-spec" : [ {
"name" : "vendor_id",
"transform" : "identity",
"source-id" : 1,
"field-id" : 1000
} ],
"default-spec-id" : 0,
"partition-specs" : [ {
"spec-id" : 0,
"fields" : [ {
"name" : "vendor_id",
"transform" : "identity",
"source-id" : 1,
"field-id" : 1000
} ]
} ],
"last-partition-id" : 1000,
"default-sort-order-id" : 0,
"sort-orders" : [ {
"order-id" : 0,
"fields" : [ ]
} ],
"properties" : {
"owner" : "root"
},
"current-snapshot-id" : 638933773299822130,
"refs" : {
"main" : {
"snapshot-id" : 638933773299822130,
"type" : "branch"
}
},
"snapshots" : [ {
"snapshot-id" : 638933773299822130,
"timestamp-ms" : 1662532818843,
"summary" : {
"operation" : "append",
"spark.app.id" : "local-1662532784305",
"added-data-files" : "4",
"added-records" : "4",
"added-files-size" : "6001",
"changed-partition-count" : "2",
"total-records" : "4",
"total-files-size" : "6001",
"total-data-files" : "4",
"total-delete-files" : "0",
"total-position-deletes" : "0",
"total-equality-deletes" : "0"
},
"manifest-list" : "/home/iceberg/warehouse/nyc/taxis/metadata/snap-638933773299822130-1-7e6760f0-4f6c-4b23-b907-0a5a174e3863.avro",
"schema-id" : 0
} ],
"snapshot-log" : [ {
"timestamp-ms" : 1662532818843,
"snapshot-id" : 638933773299822130
} ],
"metadata-log" : [ {
"timestamp-ms" : 1662532805245,
"metadata-file" : "/home/iceberg/warehouse/nyc/taxis/metadata/00000-8a62c37d-4573-4021-952a-c0baef7d21d0.metadata.json"
} ]
}
"#;
let metadata =
serde_json::from_str::<TableMetadata>(data).expect("Failed to deserialize json");
//test serialise deserialise works.
let metadata_two: TableMetadata = serde_json::from_str(
&serde_json::to_string(&metadata).expect("Failed to serialize metadata"),
)
.expect("Failed to serialize json");
dbg!(&metadata, &metadata_two);
assert_eq!(metadata, metadata_two);
Ok(())
}
}