Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion crates/iceberg/src/arrow/record_batch_partition_spliter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use parquet::arrow::PARQUET_FIELD_ID_META_KEY;

use super::record_batch_projector::RecordBatchProjector;
use crate::arrow::{arrow_struct_to_literal, type_to_arrow_type};
use crate::spec::{Literal, PartitionSpecRef, SchemaRef, Struct, StructType, Type};
use crate::spec::{Literal, PartitionSpec, PartitionSpecRef, SchemaRef, Struct, StructType, Type};
use crate::transform::{create_transform_function, BoxedTransformFunction};
use crate::{Error, ErrorKind, Result};

Expand Down Expand Up @@ -186,6 +186,10 @@ impl RecordBatchPartitionSpliter {
})
}

pub(crate) fn partition_spec(&self) -> &PartitionSpec {
self.partition_spec.as_ref()
}

/// Split the record batch into multiple record batches by the partition spec.
pub(crate) fn split(&self, batch: &RecordBatch) -> Result<Vec<(OwnedRow, RecordBatch)>> {
// get array using partition spec
Expand Down
4 changes: 4 additions & 0 deletions crates/iceberg/src/spec/manifest/data_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ impl DataFile {
pub(crate) fn rewrite_partition(&mut self, partition: Struct) {
self.partition = partition;
}

pub(crate) fn rewrite_partition_id(&mut self, partition_spec_id: i32) {
self.partition_spec_id = partition_spec_id;
}
}

/// Convert data files to avro bytes and write to writer.
Expand Down
2 changes: 2 additions & 0 deletions crates/iceberg/src/transaction/append.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ mod tests {

// check add data file with incompatible partition value
let data_file = DataFileBuilder::default()
.partition_spec_id(0)
.content(DataContentType::Data)
.file_path("test/3.parquet".to_string())
.file_format(DataFileFormat::Parquet)
Expand All @@ -457,6 +458,7 @@ mod tests {
let action = tx.fast_append();

let data_file = DataFileBuilder::default()
.partition_spec_id(0)
.content(DataContentType::Data)
.file_path("test/3.parquet".to_string())
.file_format(DataFileFormat::Parquet)
Expand Down
84 changes: 79 additions & 5 deletions crates/iceberg/src/transaction/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const META_ROOT_PATH: &str = "metadata";

pub(crate) trait SnapshotProduceOperation: Send + Sync {
fn operation(&self) -> Operation;
#[allow(unused)]
fn delete_entries(
&self,
snapshot_produce: &SnapshotProducer,
Expand Down Expand Up @@ -168,7 +167,11 @@ impl<'a> SnapshotProducer<'a> {
Ok(())
}

fn new_manifest_writer(&mut self, content: ManifestContentType) -> Result<ManifestWriter> {
fn new_manifest_writer(
&mut self,
content: ManifestContentType,
partition_spec_id: i32,
) -> Result<ManifestWriter> {
let new_manifest_path = format!(
"{}/{}/{}-m{}.{}",
self.table.metadata().location(),
Expand All @@ -185,7 +188,14 @@ impl<'a> SnapshotProducer<'a> {
self.table.metadata().current_schema().clone(),
self.table
.metadata()
.default_partition_spec()
.partition_spec_by_id(partition_spec_id)
.ok_or_else(|| {
Error::new(
ErrorKind::DataInvalid,
"Invalid partition spec id for new manifest writer",
)
.with_context("partition spec id", partition_spec_id.to_string())
})?
.as_ref()
.clone(),
);
Expand Down Expand Up @@ -278,13 +288,72 @@ impl<'a> SnapshotProducer<'a> {
builder.build()
}
});
let mut writer = self.new_manifest_writer(manifest_content_type)?;

let mut writer = self.new_manifest_writer(
manifest_content_type,
self.table.metadata().default_partition_spec_id(),
)?;
for entry in manifest_entries {
writer.add_entry(entry)?;
}
writer.write_manifest_file().await
}

async fn write_delete_manifest(
&mut self,
deleted_entries: Vec<ManifestEntry>,
) -> Result<Vec<ManifestFile>> {
if deleted_entries.is_empty() {
return Ok(vec![]);
}

// Group deleted entries by spec_id
let mut partition_groups = HashMap::new();
for entry in deleted_entries {
partition_groups
.entry(entry.data_file().partition_spec_id)
.or_insert_with(Vec::new)
.push(entry);
}

// Write a delete manifest per spec_id group
let mut deleted_manifests = Vec::new();
for (spec_id, entries) in partition_groups {
let mut data_file_writer: Option<ManifestWriter> = None;
let mut delete_file_writer: Option<ManifestWriter> = None;
for entry in entries {
match entry.content_type() {
DataContentType::Data => {
if data_file_writer.is_none() {
data_file_writer =
Some(self.new_manifest_writer(ManifestContentType::Data, spec_id)?);
}
data_file_writer.as_mut().unwrap().add_delete_entry(entry)?;
}
DataContentType::EqualityDeletes | DataContentType::PositionDeletes => {
if delete_file_writer.is_none() {
delete_file_writer = Some(
self.new_manifest_writer(ManifestContentType::Deletes, spec_id)?,
);
}
delete_file_writer
.as_mut()
.unwrap()
.add_delete_entry(entry)?;
}
}
}
if let Some(writer) = data_file_writer {
deleted_manifests.push(writer.write_manifest_file().await?);
}
if let Some(writer) = delete_file_writer {
deleted_manifests.push(writer.write_manifest_file().await?);
}
}

Ok(deleted_manifests)
}

async fn manifest_file<OP: SnapshotProduceOperation, MP: ManifestProcess>(
&mut self,
snapshot_produce_operation: &OP,
Expand Down Expand Up @@ -318,6 +387,11 @@ impl<'a> SnapshotProducer<'a> {
manifest_files.push(added_manifest);
}

let delete_manifests = self
.write_delete_manifest(snapshot_produce_operation.delete_entries(self).await?)
.await?;
manifest_files.extend(delete_manifests);

manifest_process
.process_manifests(self, manifest_files)
.await
Expand Down Expand Up @@ -618,7 +692,7 @@ impl MergeManifestManager {
Box<dyn Future<Output = Result<Vec<ManifestFile>>> + Send>,
>)
} else {
let writer = snapshot_produce.new_manifest_writer(self.content)?;
let writer = snapshot_produce.new_manifest_writer(self.content, snapshot_produce.table.metadata().default_partition_spec_id())?;
let snapshot_id = snapshot_produce.snapshot_id;
let file_io = snapshot_produce.table.file_io().clone();
Ok((Box::pin(async move {
Expand Down
115 changes: 52 additions & 63 deletions crates/iceberg/src/writer/base_writer/equality_delete_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ mod test {
use arrow_schema::{DataType, Field, Fields};
use arrow_select::concat::concat_batches;
use itertools::Itertools;
use parquet::arrow::PARQUET_FIELD_ID_META_KEY;
use parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder;
use parquet::arrow::PARQUET_FIELD_ID_META_KEY;
use parquet::file::properties::WriterProperties;
use tempfile::TempDir;
use uuid::Uuid;
Expand All @@ -197,9 +197,9 @@ mod test {
use crate::writer::base_writer::equality_delete_writer::{
EqualityDeleteFileWriterBuilder, EqualityDeleteWriterConfig,
};
use crate::writer::file_writer::ParquetWriterBuilder;
use crate::writer::file_writer::location_generator::DefaultFileNameGenerator;
use crate::writer::file_writer::location_generator::test::MockLocationGenerator;
use crate::writer::file_writer::location_generator::DefaultFileNameGenerator;
use crate::writer::file_writer::ParquetWriterBuilder;
use crate::writer::{IcebergWriter, IcebergWriterBuilder};

async fn check_parquet_data_file_with_equality_delete_write(
Expand Down Expand Up @@ -296,10 +296,12 @@ mod test {
NestedField::required(
1,
"col1",
Type::Struct(StructType::new(vec![
NestedField::required(5, "sub_col", Type::Primitive(PrimitiveType::Int))
.into(),
])),
Type::Struct(StructType::new(vec![NestedField::required(
5,
"sub_col",
Type::Primitive(PrimitiveType::Int),
)
.into()])),
)
.into(),
NestedField::required(2, "col2", Type::Primitive(PrimitiveType::String)).into(),
Expand All @@ -315,21 +317,17 @@ mod test {
NestedField::required(
4,
"col4",
Type::Struct(StructType::new(vec![
NestedField::required(
7,
"sub_col",
Type::Struct(StructType::new(vec![
NestedField::required(
8,
"sub_sub_col",
Type::Primitive(PrimitiveType::Int),
)
.into(),
])),
Type::Struct(StructType::new(vec![NestedField::required(
7,
"sub_col",
Type::Struct(StructType::new(vec![NestedField::required(
8,
"sub_sub_col",
Type::Primitive(PrimitiveType::Int),
)
.into(),
])),
.into()])),
)
.into()])),
)
.into(),
])
Expand Down Expand Up @@ -441,27 +439,23 @@ mod test {
NestedField::required(
3,
"col3",
Type::Struct(StructType::new(vec![
NestedField::required(
4,
"sub_col",
Type::Primitive(PrimitiveType::Int),
)
.into(),
])),
Type::Struct(StructType::new(vec![NestedField::required(
4,
"sub_col",
Type::Primitive(PrimitiveType::Int),
)
.into()])),
)
.into(),
NestedField::optional(
5,
"col4",
Type::Struct(StructType::new(vec![
NestedField::required(
6,
"sub_col2",
Type::Primitive(PrimitiveType::Int),
)
.into(),
])),
Type::Struct(StructType::new(vec![NestedField::required(
6,
"sub_col2",
Type::Primitive(PrimitiveType::Int),
)
.into()])),
)
.into(),
NestedField::required(
Expand Down Expand Up @@ -680,30 +674,28 @@ mod test {
NestedField::optional(
1,
"col1",
Type::Struct(StructType::new(vec![
NestedField::optional(2, "sub_col", Type::Primitive(PrimitiveType::Int))
.into(),
])),
Type::Struct(StructType::new(vec![NestedField::optional(
2,
"sub_col",
Type::Primitive(PrimitiveType::Int),
)
.into()])),
)
.into(),
NestedField::optional(
3,
"col2",
Type::Struct(StructType::new(vec![
NestedField::optional(
4,
"sub_struct_col",
Type::Struct(StructType::new(vec![
NestedField::optional(
5,
"sub_sub_col",
Type::Primitive(PrimitiveType::Int),
)
.into(),
])),
Type::Struct(StructType::new(vec![NestedField::optional(
4,
"sub_struct_col",
Type::Struct(StructType::new(vec![NestedField::optional(
5,
"sub_sub_col",
Type::Primitive(PrimitiveType::Int),
)
.into(),
])),
.into()])),
)
.into()])),
)
.into(),
])
Expand All @@ -730,14 +722,11 @@ mod test {
let inner_col = {
let nulls = NullBuffer::from(vec![true, false, true]);
Arc::new(StructArray::new(
Fields::from(vec![
Field::new("sub_sub_col", DataType::Int32, true).with_metadata(
HashMap::from([(
PARQUET_FIELD_ID_META_KEY.to_string(),
"5".to_string(),
)]),
),
]),
Fields::from(vec![Field::new("sub_sub_col", DataType::Int32, true)
.with_metadata(HashMap::from([(
PARQUET_FIELD_ID_META_KEY.to_string(),
"5".to_string(),
)]))]),
vec![Arc::new(Int32Array::from(vec![Some(1), Some(2), None]))],
Some(nulls),
))
Expand Down
Loading
Loading