jdockerty commented on code in PR #1116: URL: https://github.com/apache/iceberg-rust/pull/1116#discussion_r2046990836
########## crates/iceberg/src/spec/name_mapping/mod.rs: ########## @@ -17,14 +17,76 @@ //! Iceberg name mapping. +use std::collections::HashMap; + use serde::{Deserialize, Serialize}; use serde_with::{serde_as, DefaultOnNull}; +use crate::Error; + +/// Property name for name mapping. +pub const DEFAULT_SCHEMA_NAME_MAPPING: &str = "schema.name-mapping.default"; + /// Iceberg fallback field name to ID mapping. #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] #[serde(transparent)] pub struct NameMapping { - pub root: Vec<MappedField>, + root: Vec<MappedField>, + #[serde(skip)] + name_to_id: HashMap<String, i32>, + #[serde(skip)] + id_to_field: HashMap<i32, MappedField>, +} + +impl NameMapping { + /// Create a new [`NameMapping`] given a collection of mapped fields. + pub fn try_new(fields: Vec<MappedField>) -> Result<NameMapping, Error> { + let mut name_to_id = HashMap::new(); + let mut id_to_field = HashMap::new(); + + for field in &fields { + if let Some(id) = field.field_id() { + if id_to_field.contains_key(&id) { + return Err(Error::new( + crate::ErrorKind::DataInvalid, + format!("duplicate id '{id}' is not allowed"), + )); + } + + id_to_field.insert(id, field.clone()); + for name in field.names() { + if name_to_id.contains_key(name) { + return Err(Error::new( + crate::ErrorKind::DataInvalid, + format!("duplicate name '{name}' is not allowed"), + )); + } + name_to_id.insert(name.to_string(), id); + } + } + } Review Comment: > remove these two fields in this pr By removing these fields, doesn't that also mean the [`Arc` suggestion above](https://github.com/apache/iceberg-rust/pull/1116/files#r2046801281) is not to be implemented as well? ```rust id_to_field: HashMap<i32, Arc<MappedField>> // <-- no need to Arc here, this field is to be removed now. ``` -- 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