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 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::str::FromStr;
// use std::io::{Cursor, Read};
// use std::sync::Arc;
// use roaring::RoaringTreemap;
use serde::{Deserialize, Serialize};
use tracing::warn;
use url::Url;
use super::schema::StructType;
use crate::kernel::{error::Error, DeltaResult};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
/// Defines a file format used in table
pub struct Format {
/// Name of the encoding for files in this table
pub provider: String,
/// A map containing configuration options for the format
pub options: HashMap<String, Option<String>>,
}
impl Format {
/// Allows creation of a new action::Format
pub fn new(provider: String, options: Option<HashMap<String, Option<String>>>) -> Self {
let options = options.unwrap_or_default();
Self { provider, options }
}
/// Return the Format provider
pub fn get_provider(self) -> String {
self.provider
}
}
impl Default for Format {
fn default() -> Self {
Self {
provider: String::from("parquet"),
options: HashMap::new(),
}
}
}
/// Return a default empty schema to be used for edge-cases when a schema is missing
fn default_schema() -> String {
warn!("A `metaData` action was missing a `schemaString` and has been given an empty schema");
r#"{"type":"struct", "fields": []}"#.into()
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Default)]
#[serde(rename_all = "camelCase")]
/// Defines a metadata action
pub struct Metadata {
/// Unique identifier for this table
pub id: String,
/// User-provided identifier for this table
pub name: Option<String>,
/// User-provided description for this table
pub description: Option<String>,
/// Specification of the encoding for the files stored in the table
pub format: Format,
/// Schema of the table
#[serde(default = "default_schema")]
pub schema_string: String,
/// Column names by which the data should be partitioned
pub partition_columns: Vec<String>,
/// The time when this metadata action is created, in milliseconds since the Unix epoch
pub created_time: Option<i64>,
/// Configuration options for the metadata action
pub configuration: HashMap<String, Option<String>>,
}
impl Metadata {
/// Create a new metadata action
pub fn try_new(
schema: StructType,
partition_columns: impl IntoIterator<Item = impl Into<String>>,
configuration: HashMap<String, Option<String>>,
) -> DeltaResult<Self> {
Ok(Self {
id: uuid::Uuid::new_v4().to_string(),
format: Default::default(),
schema_string: serde_json::to_string(&schema)?,
partition_columns: partition_columns.into_iter().map(|c| c.into()).collect(),
configuration,
name: None,
description: None,
created_time: None,
})
}
/// set the table name in the metadata action
pub fn with_name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
/// set the table description in the metadata action
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
/// set the table creation time in the metadata action
pub fn with_created_time(mut self, created_time: i64) -> Self {
self.created_time = Some(created_time);
self
}
/// get the table schema
pub fn schema(&self) -> DeltaResult<StructType> {
Ok(serde_json::from_str(&self.schema_string)?)
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Default)]
#[serde(rename_all = "camelCase")]
/// Defines a protocol action
pub struct Protocol {
/// The minimum version of the Delta read protocol that a client must implement
/// in order to correctly read this table
pub min_reader_version: i32,
/// The minimum version of the Delta write protocol that a client must implement
/// in order to correctly write this table
pub min_writer_version: i32,
/// A collection of features that a client must implement in order to correctly
/// read this table (exist only when minReaderVersion is set to 3)
#[serde(skip_serializing_if = "Option::is_none")]
pub reader_features: Option<HashSet<ReaderFeatures>>,
/// A collection of features that a client must implement in order to correctly
/// write this table (exist only when minWriterVersion is set to 7)
#[serde(skip_serializing_if = "Option::is_none")]
pub writer_features: Option<HashSet<WriterFeatures>>,
}
impl Protocol {
/// Create a new protocol action
pub fn new(min_reader_version: i32, min_wrriter_version: i32) -> Self {
Self {
min_reader_version,
min_writer_version: min_wrriter_version,
reader_features: None,
writer_features: None,
}
}
/// set the reader features in the protocol action
pub fn with_reader_features(
mut self,
reader_features: impl IntoIterator<Item = impl Into<ReaderFeatures>>,
) -> Self {
self.reader_features = Some(reader_features.into_iter().map(|c| c.into()).collect());
self
}
/// set the writer features in the protocol action
pub fn with_writer_features(
mut self,
writer_features: impl IntoIterator<Item = impl Into<WriterFeatures>>,
) -> Self {
self.writer_features = Some(writer_features.into_iter().map(|c| c.into()).collect());
self
}
}
/// Features table readers can support as well as let users know
/// what is supported
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
#[serde(rename_all = "camelCase")]
pub enum ReaderFeatures {
/// Mapping of one column to another
ColumnMapping,
/// Deletion vectors for merge, update, delete
DeletionVectors,
/// timestamps without timezone support
#[serde(rename = "timestampNtz")]
TimestampWithoutTimezone,
/// version 2 of checkpointing
V2Checkpoint,
/// If we do not match any other reader features
#[serde(untagged)]
Other(String),
}
impl From<&parquet::record::Field> for ReaderFeatures {
fn from(value: &parquet::record::Field) -> Self {
match value {
parquet::record::Field::Str(feature) => match feature.as_str() {
"columnMapping" => ReaderFeatures::ColumnMapping,
"deletionVectors" | "delta.enableDeletionVectors" => {
ReaderFeatures::DeletionVectors
}
"timestampNtz" => ReaderFeatures::TimestampWithoutTimezone,
"v2Checkpoint" => ReaderFeatures::V2Checkpoint,
f => ReaderFeatures::Other(f.to_string()),
},
f => ReaderFeatures::Other(f.to_string()),
}
}
}
impl From<String> for ReaderFeatures {
fn from(value: String) -> Self {
value.as_str().into()
}
}
impl From<&str> for ReaderFeatures {
fn from(value: &str) -> Self {
match value {
"columnMapping" => ReaderFeatures::ColumnMapping,
"deletionVectors" => ReaderFeatures::DeletionVectors,
"timestampNtz" => ReaderFeatures::TimestampWithoutTimezone,
"v2Checkpoint" => ReaderFeatures::V2Checkpoint,
f => ReaderFeatures::Other(f.to_string()),
}
}
}
impl AsRef<str> for ReaderFeatures {
fn as_ref(&self) -> &str {
match self {
ReaderFeatures::ColumnMapping => "columnMapping",
ReaderFeatures::DeletionVectors => "deletionVectors",
ReaderFeatures::TimestampWithoutTimezone => "timestampNtz",
ReaderFeatures::V2Checkpoint => "v2Checkpoint",
ReaderFeatures::Other(f) => f,
}
}
}
impl fmt::Display for ReaderFeatures {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_ref())
}
}
/// Features table writers can support as well as let users know
/// what is supported
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
#[serde(rename_all = "camelCase")]
pub enum WriterFeatures {
/// Append Only Tables
AppendOnly,
/// Table invariants
Invariants,
/// Check constraints on columns
CheckConstraints,
/// CDF on a table
ChangeDataFeed,
/// Columns with generated values
GeneratedColumns,
/// Mapping of one column to another
ColumnMapping,
/// ID Columns
IdentityColumns,
/// Deletion vectors for merge, update, delete
DeletionVectors,
/// Row tracking on tables
RowTracking,
/// timestamps without timezone support
#[serde(rename = "timestampNtz")]
TimestampWithoutTimezone,
/// domain specific metadata
DomainMetadata,
/// version 2 of checkpointing
V2Checkpoint,
/// Iceberg compatibility support
IcebergCompatV1,
/// If we do not match any other reader features
#[serde(untagged)]
Other(String),
}
impl From<String> for WriterFeatures {
fn from(value: String) -> Self {
value.as_str().into()
}
}
impl From<&str> for WriterFeatures {
fn from(value: &str) -> Self {
match value {
"appendOnly" | "delta.appendOnly" => WriterFeatures::AppendOnly,
"invariants" | "delta.invariants" => WriterFeatures::Invariants,
"checkConstraints" | "delta.checkConstraints" => WriterFeatures::CheckConstraints,
"changeDataFeed" | "delta.enableChangeDataFeed" => WriterFeatures::ChangeDataFeed,
"generatedColumns" => WriterFeatures::GeneratedColumns,
"columnMapping" => WriterFeatures::ColumnMapping,
"identityColumns" => WriterFeatures::IdentityColumns,
"deletionVectors" | "delta.enableDeletionVectors" => WriterFeatures::DeletionVectors,
"rowTracking" | "delta.enableRowTracking" => WriterFeatures::RowTracking,
"timestampNtz" => WriterFeatures::TimestampWithoutTimezone,
"domainMetadata" => WriterFeatures::DomainMetadata,
"v2Checkpoint" => WriterFeatures::V2Checkpoint,
"icebergCompatV1" => WriterFeatures::IcebergCompatV1,
f => WriterFeatures::Other(f.to_string()),
}
}
}
impl AsRef<str> for WriterFeatures {
fn as_ref(&self) -> &str {
match self {
WriterFeatures::AppendOnly => "appendOnly",
WriterFeatures::Invariants => "invariants",
WriterFeatures::CheckConstraints => "checkConstraints",
WriterFeatures::ChangeDataFeed => "changeDataFeed",
WriterFeatures::GeneratedColumns => "generatedColumns",
WriterFeatures::ColumnMapping => "columnMapping",
WriterFeatures::IdentityColumns => "identityColumns",
WriterFeatures::DeletionVectors => "deletionVectors",
WriterFeatures::RowTracking => "rowTracking",
WriterFeatures::TimestampWithoutTimezone => "timestampNtz",
WriterFeatures::DomainMetadata => "domainMetadata",
WriterFeatures::V2Checkpoint => "v2Checkpoint",
WriterFeatures::IcebergCompatV1 => "icebergCompatV1",
WriterFeatures::Other(f) => f,
}
}
}
impl fmt::Display for WriterFeatures {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_ref())
}
}
impl From<&parquet::record::Field> for WriterFeatures {
fn from(value: &parquet::record::Field) -> Self {
match value {
parquet::record::Field::Str(feature) => match feature.as_str() {
"appendOnly" => WriterFeatures::AppendOnly,
"invariants" => WriterFeatures::Invariants,
"checkConstraints" => WriterFeatures::CheckConstraints,
"changeDataFeed" => WriterFeatures::ChangeDataFeed,
"generatedColumns" => WriterFeatures::GeneratedColumns,
"columnMapping" => WriterFeatures::ColumnMapping,
"identityColumns" => WriterFeatures::IdentityColumns,
"deletionVectors" => WriterFeatures::DeletionVectors,
"rowTracking" => WriterFeatures::RowTracking,
"timestampNtz" => WriterFeatures::TimestampWithoutTimezone,
"domainMetadata" => WriterFeatures::DomainMetadata,
"v2Checkpoint" => WriterFeatures::V2Checkpoint,
"icebergCompatV1" => WriterFeatures::IcebergCompatV1,
f => WriterFeatures::Other(f.to_string()),
},
f => WriterFeatures::Other(f.to_string()),
}
}
}
///Storage type of deletion vector
#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Eq)]
pub enum StorageType {
/// Stored at relative path derived from a UUID.
#[serde(rename = "u")]
UuidRelativePath,
/// Stored as inline string.
#[serde(rename = "i")]
Inline,
/// Stored at an absolute path.
#[serde(rename = "p")]
AbsolutePath,
}
impl Default for StorageType {
fn default() -> Self {
Self::UuidRelativePath // seems to be used by Databricks and therefore most common
}
}
impl FromStr for StorageType {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"u" => Ok(Self::UuidRelativePath),
"i" => Ok(Self::Inline),
"p" => Ok(Self::AbsolutePath),
_ => Err(Error::DeletionVector(format!(
"Unknown storage format: '{s}'."
))),
}
}
}
impl AsRef<str> for StorageType {
fn as_ref(&self) -> &str {
match self {
Self::UuidRelativePath => "u",
Self::Inline => "i",
Self::AbsolutePath => "p",
}
}
}
impl ToString for StorageType {
fn to_string(&self) -> String {
self.as_ref().into()
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
/// Defines a deletion vector
pub struct DeletionVectorDescriptor {
/// A single character to indicate how to access the DV. Legal options are: ['u', 'i', 'p'].
pub storage_type: StorageType,
/// Three format options are currently proposed:
/// - If `storageType = 'u'` then `<random prefix - optional><base85 encoded uuid>`:
/// The deletion vector is stored in a file with a path relative to the data
/// directory of this Delta table, and the file name can be reconstructed from
/// the UUID. See Derived Fields for how to reconstruct the file name. The random
/// prefix is recovered as the extra characters before the (20 characters fixed length) uuid.
/// - If `storageType = 'i'` then `<base85 encoded bytes>`: The deletion vector
/// is stored inline in the log. The format used is the `RoaringBitmapArray`
/// format also used when the DV is stored on disk and described in [Deletion Vector Format].
/// - If `storageType = 'p'` then `<absolute path>`: The DV is stored in a file with an
/// absolute path given by this path, which has the same format as the `path` field
/// in the `add`/`remove` actions.
///
/// [Deletion Vector Format]: https://github.com/delta-io/delta/blob/master/PROTOCOL.md#Deletion-Vector-Format
pub path_or_inline_dv: String,
/// Start of the data for this DV in number of bytes from the beginning of the file it is stored in.
/// Always None (absent in JSON) when `storageType = 'i'`.
#[serde(skip_serializing_if = "Option::is_none")]
pub offset: Option<i32>,
/// Size of the serialized DV in bytes (raw data size, i.e. before base85 encoding, if inline).
pub size_in_bytes: i32,
/// Number of rows the given DV logically removes from the file.
pub cardinality: i64,
}
impl DeletionVectorDescriptor {
/// get the absolute path of the deletion vector
pub fn absolute_path(&self, parent: &Url) -> DeltaResult<Option<Url>> {
match &self.storage_type {
StorageType::UuidRelativePath => {
let prefix_len = self.path_or_inline_dv.len() as i32 - 20;
if prefix_len < 0 {
return Err(Error::DeletionVector("Invalid length".to_string()));
}
let decoded = z85::decode(&self.path_or_inline_dv[(prefix_len as usize)..])
.map_err(|_| Error::DeletionVector("Failed to decode DV uuid".to_string()))?;
let uuid = uuid::Uuid::from_slice(&decoded)
.map_err(|err| Error::DeletionVector(err.to_string()))?;
let mut dv_suffix = format!("deletion_vector_{uuid}.bin");
if prefix_len > 0 {
dv_suffix = format!(
"{}/{}",
&self.path_or_inline_dv[..(prefix_len as usize)],
dv_suffix
);
}
let dv_path = parent
.join(&dv_suffix)
.map_err(|_| Error::DeletionVector(format!("invalid path: {}", dv_suffix)))?;
Ok(Some(dv_path))
}
StorageType::AbsolutePath => {
Ok(Some(Url::parse(&self.path_or_inline_dv).map_err(|_| {
Error::DeletionVector(format!("invalid path: {}", self.path_or_inline_dv))
})?))
}
StorageType::Inline => Ok(None),
}
}
// TODO read only required byte ranges
// pub fn read(
// &self,
// fs_client: Arc<dyn FileSystemClient>,
// parent: Url,
// ) -> DeltaResult<RoaringTreemap> {
// match self.absolute_path(&parent)? {
// None => {
// let bytes = z85::decode(&self.path_or_inline_dv)
// .map_err(|_| Error::DeletionVector("Failed to decode DV".to_string()))?;
// RoaringTreemap::deserialize_from(&bytes[12..])
// .map_err(|err| Error::DeletionVector(err.to_string()))
// }
// Some(path) => {
// let offset = self.offset;
// let size_in_bytes = self.size_in_bytes;
//
// let dv_data = fs_client
// .read_files(vec![(path, None)])?
// .next()
// .ok_or(Error::MissingData("No deletion Vector data".to_string()))??;
//
// let mut cursor = Cursor::new(dv_data);
// if let Some(offset) = offset {
// // TODO should we read the datasize from the DV file?
// // offset plus datasize bytes
// cursor.set_position((offset + 4) as u64);
// }
//
// let mut buf = vec![0; 4];
// cursor
// .read(&mut buf)
// .map_err(|err| Error::DeletionVector(err.to_string()))?;
// let magic =
// i32::from_le_bytes(buf.try_into().map_err(|_| {
// Error::DeletionVector("filed to read magic bytes".to_string())
// })?);
// println!("magic --> : {}", magic);
// // assert!(magic == 1681511377);
//
// let mut buf = vec![0; size_in_bytes as usize];
// cursor
// .read(&mut buf)
// .map_err(|err| Error::DeletionVector(err.to_string()))?;
//
// RoaringTreemap::deserialize_from(Cursor::new(buf))
// .map_err(|err| Error::DeletionVector(err.to_string()))
// }
// }
// }
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
/// Defines an add action
pub struct Add {
/// A relative path to a data file from the root of the table or an absolute path to a file
/// that should be added to the table. The path is a URI as specified by
/// [RFC 2396 URI Generic Syntax], which needs to be decoded to get the data file path.
///
/// [RFC 2396 URI Generic Syntax]: https://www.ietf.org/rfc/rfc2396.txt
#[serde(with = "serde_path")]
pub path: String,
/// A map from partition column to value for this logical file.
pub partition_values: HashMap<String, Option<String>>,
/// The size of this data file in bytes
pub size: i64,
/// The time this logical file was created, as milliseconds since the epoch.
pub modification_time: i64,
/// When `false` the logical file must already be present in the table or the records
/// in the added file must be contained in one or more remove actions in the same version.
pub data_change: bool,
/// Contains [statistics] (e.g., count, min/max values for columns) about the data in this logical file.
///
/// [statistics]: https://github.com/delta-io/delta/blob/master/PROTOCOL.md#Per-file-Statistics
pub stats: Option<String>,
/// Map containing metadata about this logical file.
pub tags: Option<HashMap<String, Option<String>>>,
/// Information about deletion vector (DV) associated with this add action
pub deletion_vector: Option<DeletionVectorDescriptor>,
/// Default generated Row ID of the first row in the file. The default generated Row IDs
/// of the other rows in the file can be reconstructed by adding the physical index of the
/// row within the file to the base Row ID
pub base_row_id: Option<i64>,
/// First commit version in which an add action with the same path was committed to the table.
pub default_row_commit_version: Option<i64>,
/// The name of the clustering implementation
pub clustering_provider: Option<String>,
/// Contains statistics (e.g., count, min/max values for columns) about the data in this file in
/// raw parquet format. This field needs to be written when statistics are available and the
/// table property: delta.checkpoint.writeStatsAsStruct is set to true.
///
/// This field is only available in add action records read from checkpoints
#[serde(skip_serializing, skip_deserializing)]
pub stats_parsed: Option<parquet::record::Row>,
}
/// Represents a tombstone (deleted file) in the Delta log.
#[derive(Serialize, Deserialize, Debug, Clone, Eq, Default)]
#[serde(rename_all = "camelCase")]
pub struct Remove {
/// A relative path to a data file from the root of the table or an absolute path to a file
/// that should be added to the table. The path is a URI as specified by
/// [RFC 2396 URI Generic Syntax], which needs to be decoded to get the data file path.
///
/// [RFC 2396 URI Generic Syntax]: https://www.ietf.org/rfc/rfc2396.txt
#[serde(with = "serde_path")]
pub path: String,
/// When `false` the logical file must already be present in the table or the records
/// in the added file must be contained in one or more remove actions in the same version.
pub data_change: bool,
/// The time this logical file was created, as milliseconds since the epoch.
#[serde(skip_serializing_if = "Option::is_none")]
pub deletion_timestamp: Option<i64>,
/// When true the fields `partition_values`, `size`, and `tags` are present
#[serde(skip_serializing_if = "Option::is_none")]
pub extended_file_metadata: Option<bool>,
/// A map from partition column to value for this logical file.
#[serde(skip_serializing_if = "Option::is_none")]
pub partition_values: Option<HashMap<String, Option<String>>>,
/// The size of this data file in bytes
#[serde(skip_serializing_if = "Option::is_none")]
pub size: Option<i64>,
/// Map containing metadata about this logical file.
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<HashMap<String, Option<String>>>,
/// Information about deletion vector (DV) associated with this add action
#[serde(skip_serializing_if = "Option::is_none")]
pub deletion_vector: Option<DeletionVectorDescriptor>,
/// Default generated Row ID of the first row in the file. The default generated Row IDs
/// of the other rows in the file can be reconstructed by adding the physical index of the
/// row within the file to the base Row ID
#[serde(skip_serializing_if = "Option::is_none")]
pub base_row_id: Option<i64>,
/// First commit version in which an add action with the same path was committed to the table.
#[serde(skip_serializing_if = "Option::is_none")]
pub default_row_commit_version: Option<i64>,
}
/// Delta AddCDCFile action that describes a parquet CDC data file.
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct AddCDCFile {
/// A relative path, from the root of the table, or an
/// absolute path to a CDC file
#[serde(with = "serde_path")]
pub path: String,
/// The size of this file in bytes
pub size: i64,
/// A map from partition column to value for this file
pub partition_values: HashMap<String, Option<String>>,
/// Should always be set to false because they do not change the underlying data of the table
pub data_change: bool,
/// Map containing metadata about this file
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<HashMap<String, Option<String>>>,
}
/// Action used by streaming systems to track progress using application-specific versions to
/// enable idempotency.
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Txn {
/// A unique identifier for the application performing the transaction.
pub app_id: String,
/// An application-specific numeric identifier for this transaction.
pub version: i64,
/// The time when this transaction action was created in milliseconds since the Unix epoch.
#[serde(skip_serializing_if = "Option::is_none")]
pub last_updated: Option<i64>,
}
/// The commitInfo is a fairly flexible action within the delta specification, where arbitrary data can be stored.
/// However the reference implementation as well as delta-rs store useful information that may for instance
/// allow us to be more permissive in commit conflict resolution.
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct CommitInfo {
/// Timestamp in millis when the commit was created
#[serde(skip_serializing_if = "Option::is_none")]
pub timestamp: Option<i64>,
/// Id of the user invoking the commit
#[serde(skip_serializing_if = "Option::is_none")]
pub user_id: Option<String>,
/// Name of the user invoking the commit
#[serde(skip_serializing_if = "Option::is_none")]
pub user_name: Option<String>,
/// The operation performed during the
#[serde(skip_serializing_if = "Option::is_none")]
pub operation: Option<String>,
/// Parameters used for table operation
#[serde(skip_serializing_if = "Option::is_none")]
pub operation_parameters: Option<HashMap<String, serde_json::Value>>,
/// Version of the table when the operation was started
#[serde(skip_serializing_if = "Option::is_none")]
pub read_version: Option<i64>,
/// The isolation level of the commit
#[serde(skip_serializing_if = "Option::is_none")]
pub isolation_level: Option<IsolationLevel>,
/// TODO
#[serde(skip_serializing_if = "Option::is_none")]
pub is_blind_append: Option<bool>,
/// Delta engine which created the commit.
#[serde(skip_serializing_if = "Option::is_none")]
pub engine_info: Option<String>,
/// Additional provenance information for the commit
#[serde(flatten, default)]
pub info: HashMap<String, serde_json::Value>,
}
/// The domain metadata action contains a configuration (string) for a named metadata domain
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct DomainMetadata {
/// Identifier for this domain (system or user-provided)
pub domain: String,
/// String containing configuration for the metadata domain
pub configuration: String,
/// When `true` the action serves as a tombstone
pub removed: bool,
}
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)]
/// This action is only allowed in checkpoints following V2 spec. It describes the details about the checkpoint.
pub struct CheckpointMetadata {
/// The flavor of the V2 checkpoint. Allowed values: "flat".
pub flavor: String,
/// Map containing any additional metadata about the v2 spec checkpoint.
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<HashMap<String, Option<String>>>,
}
/// The sidecar action references a sidecar file which provides some of the checkpoint's file actions.
/// This action is only allowed in checkpoints following V2 spec.
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Sidecar {
/// The name of the sidecar file (not a path).
/// The file must reside in the _delta_log/_sidecars directory.
pub file_name: String,
/// The size of the sidecar file in bytes
pub size_in_bytes: i64,
/// The time this sidecar file was created, as milliseconds since the epoch.
pub modification_time: i64,
/// Type of sidecar. Valid values are: "fileaction".
/// This could be extended in future to allow different kinds of sidecars.
#[serde(rename = "type")]
pub sidecar_type: String,
/// Map containing any additional metadata about the checkpoint sidecar file.
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<HashMap<String, Option<String>>>,
}
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
/// The isolation level applied during transaction
pub enum IsolationLevel {
/// The strongest isolation level. It ensures that committed write operations
/// and all reads are Serializable. Operations are allowed as long as there
/// exists a serial sequence of executing them one-at-a-time that generates
/// the same outcome as that seen in the table. For the write operations,
/// the serial sequence is exactly the same as that seen in the table’s history.
Serializable,
/// A weaker isolation level than Serializable. It ensures only that the write
/// operations (that is, not reads) are serializable. However, this is still stronger
/// than Snapshot isolation. WriteSerializable is the default isolation level because
/// it provides great balance of data consistency and availability for most common operations.
WriteSerializable,
/// SnapshotIsolation is a guarantee that all reads made in a transaction will see a consistent
/// snapshot of the database (in practice it reads the last committed values that existed at the
/// time it started), and the transaction itself will successfully commit only if no updates
/// it has made conflict with any concurrent updates made since that snapshot.
SnapshotIsolation,
}
// Spark assumes Serializable as default isolation level
// https://github.com/delta-io/delta/blob/abb171c8401200e7772b27e3be6ea8682528ac72/core/src/main/scala/org/apache/spark/sql/delta/OptimisticTransaction.scala#L1023
impl Default for IsolationLevel {
fn default() -> Self {
Self::Serializable
}
}
impl AsRef<str> for IsolationLevel {
fn as_ref(&self) -> &str {
match self {
Self::Serializable => "Serializable",
Self::WriteSerializable => "WriteSerializable",
Self::SnapshotIsolation => "SnapshotIsolation",
}
}
}
impl FromStr for IsolationLevel {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_ascii_lowercase().as_str() {
"serializable" => Ok(Self::Serializable),
"writeserializable" | "write_serializable" => Ok(Self::WriteSerializable),
"snapshotisolation" | "snapshot_isolation" => Ok(Self::SnapshotIsolation),
_ => Err(Error::Generic("Invalid string for IsolationLevel".into())),
}
}
}
pub(crate) mod serde_path {
use std::str::Utf8Error;
use percent_encoding::{percent_decode_str, percent_encode, AsciiSet, CONTROLS};
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
pub fn deserialize<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
decode_path(&s).map_err(serde::de::Error::custom)
}
pub fn serialize<S>(value: &str, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let encoded = encode_path(value);
String::serialize(&encoded, serializer)
}
pub const _DELIMITER: &str = "/";
/// The path delimiter as a single byte
pub const _DELIMITER_BYTE: u8 = _DELIMITER.as_bytes()[0];
/// Characters we want to encode.
const INVALID: &AsciiSet = &CONTROLS
// The delimiter we are reserving for internal hierarchy
// .add(DELIMITER_BYTE)
// Characters AWS recommends avoiding for object keys
// https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html
.add(b'\\')
.add(b'{')
.add(b'^')
.add(b'}')
.add(b'%')
.add(b'`')
.add(b']')
.add(b'"')
.add(b'>')
.add(b'[')
// .add(b'~')
.add(b'<')
.add(b'#')
.add(b'|')
// Characters Google Cloud Storage recommends avoiding for object names
// https://cloud.google.com/storage/docs/naming-objects
.add(b'\r')
.add(b'\n')
.add(b'*')
.add(b'?');
fn encode_path(path: &str) -> String {
percent_encode(path.as_bytes(), INVALID).to_string()
}
pub fn decode_path(path: &str) -> Result<String, Utf8Error> {
Ok(percent_decode_str(path).decode_utf8()?.to_string())
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
// use std::sync::Arc;
// use object_store::local::LocalFileSystem;
use crate::kernel::PrimitiveType;
use super::*;
// use crate::client::filesystem::ObjectStoreFileSystemClient;
// use crate::executor::tokio::TokioBackgroundExecutor;
fn dv_relateive() -> DeletionVectorDescriptor {
DeletionVectorDescriptor {
storage_type: "u".parse().unwrap(),
path_or_inline_dv: "ab^-aqEH.-t@S}K{vb[*k^".to_string(),
offset: Some(4),
size_in_bytes: 40,
cardinality: 6,
}
}
fn dv_absolute() -> DeletionVectorDescriptor {
DeletionVectorDescriptor {
storage_type: "p".parse().unwrap(),
path_or_inline_dv:
"s3://mytable/deletion_vector_d2c639aa-8816-431a-aaf6-d3fe2512ff61.bin".to_string(),
offset: Some(4),
size_in_bytes: 40,
cardinality: 6,
}
}
fn dv_inline() -> DeletionVectorDescriptor {
DeletionVectorDescriptor {
storage_type: "i".parse().unwrap(),
path_or_inline_dv: "wi5b=000010000siXQKl0rr91000f55c8Xg0@@D72lkbi5=-{L".to_string(),
offset: None,
size_in_bytes: 40,
cardinality: 6,
}
}
fn dv_example() -> DeletionVectorDescriptor {
DeletionVectorDescriptor {
storage_type: "u".parse().unwrap(),
path_or_inline_dv: "vBn[lx{q8@P<9BNH/isA".to_string(),
offset: Some(1),
size_in_bytes: 36,
cardinality: 2,
}
}
#[test]
fn test_deletion_vector_absolute_path() {
let parent = Url::parse("s3://mytable/").unwrap();
let relative = dv_relateive();
let expected =
Url::parse("s3://mytable/ab/deletion_vector_d2c639aa-8816-431a-aaf6-d3fe2512ff61.bin")
.unwrap();
assert_eq!(expected, relative.absolute_path(&parent).unwrap().unwrap());
let absolute = dv_absolute();
let expected =
Url::parse("s3://mytable/deletion_vector_d2c639aa-8816-431a-aaf6-d3fe2512ff61.bin")
.unwrap();
assert_eq!(expected, absolute.absolute_path(&parent).unwrap().unwrap());
let inline = dv_inline();
assert_eq!(None, inline.absolute_path(&parent).unwrap());
let path = std::fs::canonicalize(PathBuf::from("../test/tests/data/table-with-dv-small/"))
.unwrap();
let parent = url::Url::from_directory_path(path).unwrap();
let dv_url = parent
.join("deletion_vector_61d16c75-6994-46b7-a15b-8b538852e50e.bin")
.unwrap();
let example = dv_example();
assert_eq!(dv_url, example.absolute_path(&parent).unwrap().unwrap());
}
#[test]
fn test_primitive() {
let types: PrimitiveType = serde_json::from_str("\"string\"").unwrap();
println!("{:?}", types);
}
// #[test]
// fn test_deletion_vector_read() {
// let store = Arc::new(LocalFileSystem::new());
// let path =
// std::fs::canonicalize(PathBuf::from("../deltalake-test/tests/data/table-with-dv-small/")).unwrap();
// let parent = url::Url::from_directory_path(path).unwrap();
// let root = object_store::path::Path::from(parent.path());
// let fs_client = Arc::new(ObjectStoreFileSystemClient::new(
// store,
// root,
// Arc::new(TokioBackgroundExecutor::new()),
// ));
//
// let example = dv_example();
// let tree_map = example.read(fs_client, parent).unwrap();
//
// let expected: Vec<u64> = vec![0, 9];
// let found = tree_map.iter().collect::<Vec<_>>();
// assert_eq!(found, expected)
// }
}