emkornfield commented on code in PR #1489: URL: https://github.com/apache/iceberg-rust/pull/1489#discussion_r2347110899
########## crates/iceberg/src/inspect/refs.rs: ########## @@ -0,0 +1,192 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::collections::HashMap; +use std::sync::Arc; + +use arrow_array::RecordBatch; +use arrow_array::builder::{PrimitiveBuilder, StringBuilder}; +use arrow_array::types::{Int32Type, Int64Type}; +use futures::{StreamExt, stream}; + +use crate::Result; +use crate::arrow::schema_to_arrow_schema; +use crate::scan::ArrowRecordBatchStream; +use crate::spec::{NestedField, PrimitiveType, SnapshotReference, SnapshotRetention, Type}; +use crate::table::Table; + +/// Refs table. +pub struct RefsTable<'a> { + table: &'a Table, +} + +impl<'a> RefsTable<'a> { + /// Create a new Refs table instance. + pub fn new(table: &'a Table) -> Self { + Self { table } + } + + /// Returns the iceberg schema of the refs table. + pub fn schema(&self) -> crate::spec::Schema { + let fields = vec![ + NestedField::required(1, "name", Type::Primitive(PrimitiveType::String)), + NestedField::required(2, "type", Type::Primitive(PrimitiveType::String)), + NestedField::required(3, "snapshot_id", Type::Primitive(PrimitiveType::Long)), + NestedField::optional( + 4, + "max_reference_age_in_ms", + Type::Primitive(PrimitiveType::Long), + ), + NestedField::optional( + 5, + "min_snapshots_to_keep", + Type::Primitive(PrimitiveType::Int), + ), + NestedField::optional( + 6, + "max_snapshot_age_in_ms", + Type::Primitive(PrimitiveType::Long), + ), + ]; + crate::spec::Schema::builder() + .with_fields(fields.into_iter().map(|f| f.into())) + .build() + .unwrap() + } + + /// Scans the refs table. + pub async fn scan(&self) -> Result<ArrowRecordBatchStream> { + let schema = schema_to_arrow_schema(&self.schema())?; + + let mut names = StringBuilder::new(); + let mut ref_type = StringBuilder::new(); + let mut snapshot_id = PrimitiveBuilder::<Int64Type>::new(); + let mut max_reference_age_in_ms = PrimitiveBuilder::<Int64Type>::new(); + let mut min_keep = PrimitiveBuilder::<Int32Type>::new(); + let mut max_snapshot_age_in_ms = PrimitiveBuilder::<Int64Type>::new(); + + let refs: &HashMap<String, SnapshotReference> = &self.table.metadata().refs; + for (name, snapshot_ref) in refs { + names.append_value(name); + snapshot_id.append_value(snapshot_ref.snapshot_id); + + match &snapshot_ref.retention { + SnapshotRetention::Branch { + min_snapshots_to_keep, + max_snapshot_age_ms, + max_ref_age_ms, + } => { + ref_type.append_value("BRANCH"); + max_reference_age_in_ms.append_option(*max_ref_age_ms); + min_keep.append_option(*min_snapshots_to_keep); + max_snapshot_age_in_ms.append_option(*max_snapshot_age_ms); + } + SnapshotRetention::Tag { max_ref_age_ms } => { + ref_type.append_value("TAG"); + max_reference_age_in_ms.append_option(*max_ref_age_ms); + min_keep.append_null(); + max_snapshot_age_in_ms.append_null(); + } + } + } + + let batch = RecordBatch::try_new(Arc::new(schema), vec![ + Arc::new(names.finish()), + Arc::new(ref_type.finish()), + Arc::new(snapshot_id.finish()), + Arc::new(max_reference_age_in_ms.finish()), + Arc::new(min_keep.finish()), + Arc::new(max_snapshot_age_in_ms.finish()), + ])?; + + Ok(stream::iter(vec![Ok(batch)]).boxed()) Review Comment: Sorry for the late reply, maybe we can at least add a note here? Or maybe the collection could happen on the python side? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org