c-thiel commented on code in PR #331: URL: https://github.com/apache/iceberg-rust/pull/331#discussion_r1688276072
########## crates/iceberg/src/spec/view_version.rs: ########## @@ -0,0 +1,477 @@ +// 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. + +/*! + * View Versions! +*/ +use crate::error::Result; +use chrono::{DateTime, MappedLocalTime, TimeZone, Utc}; +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; +use std::sync::Arc; +use typed_builder::TypedBuilder; + +use super::view_metadata::ViewVersionLog; +use crate::catalog::NamespaceIdent; +use crate::spec::{SchemaId, SchemaRef, ViewMetadata}; +use crate::{Error, ErrorKind}; +use _serde::ViewVersionV1; + +/// Reference to [`ViewVersion`]. +pub type ViewVersionRef = Arc<ViewVersion>; + +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, TypedBuilder)] +#[serde(from = "ViewVersionV1", into = "ViewVersionV1")] +#[builder(field_defaults(setter(prefix = "with_")))] +/// A view versions represents the definition of a view at a specific point in time. +pub struct ViewVersion { + /// A unique long ID + version_id: i64, + /// ID of the schema for the view version + schema_id: SchemaId, + /// Timestamp when the version was created (ms from epoch) + timestamp_ms: i64, + /// A string to string map of summary metadata about the version + summary: HashMap<String, String>, + /// A list of representations for the view definition. + representations: ViewRepresentations, + /// Catalog name to use when a reference in the SELECT does not contain a catalog + #[builder(default = None)] + default_catalog: Option<String>, + /// Namespace to use when a reference in the SELECT is a single identifier + default_namespace: NamespaceIdent, +} + +impl ViewVersion { + /// Get the version id of this view version. + #[inline] + pub fn version_id(&self) -> i64 { + self.version_id + } + + /// Get the schema id of this view version. + #[inline] + pub fn schema_id(&self) -> SchemaId { + self.schema_id + } + + /// Get the timestamp of when the view version was created + #[inline] + pub fn timestamp(&self) -> MappedLocalTime<DateTime<Utc>> { + Utc.timestamp_millis_opt(self.timestamp_ms) + } + + /// Get the timestamp of when the view version was created in milliseconds since epoch + #[inline] + pub fn timestamp_ms(&self) -> i64 { + self.timestamp_ms + } + + /// Get summary of the view version + #[inline] + pub fn summary(&self) -> &HashMap<String, String> { + &self.summary + } + + /// Get this views representations + #[inline] + pub fn representations(&self) -> &ViewRepresentations { + &self.representations + } + + /// Get the default catalog for this view version + #[inline] + pub fn default_catalog(&self) -> Option<&String> { + self.default_catalog.as_ref() + } + + /// Get the default namespace to use when a reference in the SELECT is a single identifier + #[inline] + pub fn default_namespace(&self) -> &NamespaceIdent { + &self.default_namespace + } + + /// Get the schema of this snapshot. + pub fn schema(&self, view_metadata: &ViewMetadata) -> Result<SchemaRef> { + let r = view_metadata + .schema_by_id(self.schema_id()) + .ok_or_else(|| { + Error::new( + ErrorKind::DataInvalid, + format!("Schema with id {} not found", self.schema_id()), + ) + }) + .cloned(); + r + } + + /// Retrieve the history log entry for this view version. + #[allow(dead_code)] + pub(crate) fn log(&self) -> ViewVersionLog { + ViewVersionLog::new(self.version_id, self.timestamp_ms) + } +} + +/// A list of view representations. +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +pub struct ViewRepresentations(Vec<ViewRepresentation>); + +impl ViewRepresentations { + #[inline] + /// Turn these representations into a builder. + /// Use this method to modify the representations. + pub fn into_builder(self) -> ViewRepresentationsBuilder { + ViewRepresentationsBuilder(self.0) + } + + #[inline] + /// Create a new builder for the representations. + pub fn builder() -> ViewRepresentationsBuilder { + ViewRepresentationsBuilder::new() + } + + #[inline] + /// Get the number of representations + pub fn len(&self) -> usize { + self.0.len() + } + + #[inline] + /// Check if there are no representations + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } +} + +// Iterator for ViewRepresentations +impl IntoIterator for ViewRepresentations { + type Item = ViewRepresentation; + type IntoIter = std::vec::IntoIter<Self::Item>; + + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} +impl<'a> IntoIterator for &'a ViewRepresentations { + type Item = &'a ViewRepresentation; + type IntoIter = std::slice::Iter<'a, ViewRepresentation>; + + fn into_iter(self) -> Self::IntoIter { + self.0.iter() + } +} + +/// A builder for [`ViewRepresentations`]. +pub struct ViewRepresentationsBuilder(Vec<ViewRepresentation>); + +impl ViewRepresentationsBuilder { + /// Create a new builder. + pub fn new() -> Self { + Self(Vec::new()) + } + + /// Add a or overwrite a representation for a view + /// + /// SQL representations dialects must be unique (case insensitive). If a representation with the same + /// dialect already exists, it will be overwritten. + pub fn add_or_overwrite_representation(mut self, representation: ViewRepresentation) -> Self { + let dialect = match &representation { + ViewRepresentation::SqlViewRepresentation(sql) => &sql.dialect, + }; + self.0.retain(|r| { + let ViewRepresentation::SqlViewRepresentation(sql) = r; + !sql.dialect.eq_ignore_ascii_case(dialect) + }); + self.0.push(representation); + self + } + + /// Add a SQL representation for a view. Fails if a representation with the same dialect already exists. + pub fn add_representation(self, representation: ViewRepresentation) -> Result<Self> { + let dialect = match &representation { + ViewRepresentation::SqlViewRepresentation(sql) => &sql.dialect, + }; + if self + .0 + .iter() + .any(|r| matches!(r, ViewRepresentation::SqlViewRepresentation(sql) if sql.dialect.eq_ignore_ascii_case(dialect))) + { + return Err(Error::new( + ErrorKind::DataInvalid, + format!("Representation with dialect {} already exists", dialect), + )); + } + Ok(self.add_or_overwrite_representation(representation)) + } + + /// Add a or overwrite a SQL representation for a view + /// + /// SQL representations dialects must be unique. If a representation with the same + /// dialect already exists, it will be overwritten. + pub fn add_or_overwrite_sql_representation(self, sql: String, dialect: String) -> Self { + self.add_or_overwrite_representation(ViewRepresentation::SqlViewRepresentation( + SqlViewRepresentation { sql, dialect }, + )) + } + + /// Add a SQL representation for a view. Fails if a representation with the same dialect already exists. + pub fn add_sql_representation(self, sql: String, dialect: String) -> Result<Self> { + self.add_representation(ViewRepresentation::SqlViewRepresentation( + SqlViewRepresentation { sql, dialect }, + )) + } + + /// Build the list of representations. + pub fn build(self) -> ViewRepresentations { + ViewRepresentations(self.0) + } +} + +impl Default for ViewRepresentationsBuilder { + fn default() -> Self { + Self::new() + } +} + +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] +#[serde(tag = "type")] +/// View definitions can be represented in multiple ways. +/// Representations are documented ways to express a view definition. +// ToDo: Make unique per Dialect +pub enum ViewRepresentation { + #[serde(rename = "sql")] + /// The SQL representation stores the view definition as a SQL SELECT, + SqlViewRepresentation(SqlViewRepresentation), +} + +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] +#[serde(rename_all = "kebab-case")] +/// The SQL representation stores the view definition as a SQL SELECT, +/// with metadata such as the SQL dialect. +pub struct SqlViewRepresentation { + #[serde(rename = "sql")] + /// The SQL SELECT statement that defines the view. + pub sql: String, + #[serde(rename = "dialect")] + /// The dialect of the sql SELECT statement (e.g., "trino" or "spark") + pub dialect: String, +} + +pub(super) mod _serde { + /// This is a helper module that defines types to help with serialization/deserialization. + /// For deserialization the input first gets read into either the [SnapshotV1] or [SnapshotV2] struct + /// and then converted into the [Snapshot] struct. Serialization works the other way around. + /// [SnapshotV1] and [SnapshotV2] are internal struct that are only used for serialization and deserialization. Review Comment: You are right, strange - pressed the GitHub button. https://github.com/c-thiel/iceberg-rust/commit/ec4facb02b0fea29d36dd88640fd2af8f708bedb -- 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