iceberg_rust/view/mod.rs
1//! View management for Apache Iceberg tables
2//!
3//! This module provides the core functionality for working with Iceberg views:
4//!
5//! - Creating and managing views through the [View] struct
6//! - Atomic updates via [transaction] support
7//! - Schema evolution and versioning
8//! - View metadata management
9//! - Integration with catalogs and object stores
10//!
11//! Views provide a logical abstraction over underlying data, supporting:
12//! - SQL-based view definitions
13//! - Schema evolution tracking
14//! - Version history
15//! - Properties and configuration
16//! - Metadata management
17//!
18//! # Example
19//! ```rust,no_run
20//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
21//! use iceberg_rust::view::View;
22//!
23//! // Create a new view using the builder pattern
24//! let mut view = View::builder()
25//! .with_name("example_view")
26//! .with_schema(/* ... */)
27//! .build()
28//! .await?;
29//!
30//! // Start a transaction to update the view
31//! view.new_transaction(None)
32//! .update_properties(vec![("comment".into(), "Example view".into())])
33//! .commit()
34//! .await?;
35//! # Ok(())
36//! # }
37//! ```
38
39use std::sync::Arc;
40
41use iceberg_rust_spec::spec::{schema::Schema, view_metadata::ViewMetadata};
42
43use crate::{
44 catalog::{create::CreateViewBuilder, identifier::Identifier, Catalog},
45 error::Error,
46};
47
48use self::transaction::Transaction as ViewTransaction;
49
50pub mod transaction;
51
52#[derive(Debug, Clone)]
53/// An Iceberg view provides a logical view over underlying data with schema evolution and versioning
54///
55/// Views store:
56/// - SQL query or other representation of the view logic
57/// - Schema evolution history
58/// - Version history for view definitions
59/// - Properties for configuration
60/// - Metadata about view location and catalog
61///
62/// Views can be either filesystem-based or managed by a metastore catalog.
63/// They support transactions for atomic updates to their definition and properties.
64///
65/// The View struct provides the main interface for:
66/// - Creating and managing views
67/// - Accessing view metadata and schemas
68/// - Starting transactions for atomic updates
69/// - Interacting with the underlying storage system
70pub struct View {
71 /// Type of the View, either filesystem or metastore.
72 identifier: Identifier,
73 /// Metadata for the iceberg view according to the iceberg view spec
74 metadata: ViewMetadata,
75 /// Catalog of the table
76 catalog: Arc<dyn Catalog>,
77}
78
79/// Public interface of the table.
80impl View {
81 /// Creates a new builder for configuring and creating an Iceberg view
82 ///
83 /// Returns a `CreateViewBuilder` that provides a fluent interface for:
84 /// - Setting the view name and location
85 /// - Configuring view properties
86 /// - Defining the view schema
87 /// - Setting SQL or other view representations
88 /// - Specifying the catalog and namespace
89 ///
90 /// # Returns
91 /// * `CreateViewBuilder<Option<()>>` - A builder for creating new views
92 pub fn builder() -> CreateViewBuilder<Option<()>> {
93 CreateViewBuilder::default()
94 }
95 /// Creates a new Iceberg view instance with the given identifier, catalog and metadata
96 ///
97 /// # Arguments
98 /// * `identifier` - The unique identifier for this view in the catalog
99 /// * `catalog` - The catalog that will manage this view
100 /// * `metadata` - The view metadata containing schema, versions, and other view information
101 ///
102 /// # Returns
103 /// * `Result<Self, Error>` - The new view instance or an error if creation fails
104 ///
105 /// This is typically called by catalog implementations rather than directly by users.
106 /// For creating new views, use the `builder()` method instead.
107 pub async fn new(
108 identifier: Identifier,
109 catalog: Arc<dyn Catalog>,
110 metadata: ViewMetadata,
111 ) -> Result<Self, Error> {
112 Ok(View {
113 identifier,
114 metadata,
115 catalog,
116 })
117 }
118 /// Gets the unique identifier for this view in the catalog
119 ///
120 /// The identifier contains:
121 /// - The namespace path for the view
122 /// - The view name
123 ///
124 /// # Returns
125 /// * `&Identifier` - A reference to this view's identifier
126 pub fn identifier(&self) -> &Identifier {
127 &self.identifier
128 }
129 /// Gets the catalog that manages this view
130 ///
131 /// The catalog handles:
132 /// - View metadata storage and retrieval
133 /// - Schema management
134 /// - View versioning
135 /// - Access control
136 ///
137 /// # Returns
138 /// * `Arc<dyn Catalog>` - A thread-safe reference to the catalog
139 pub fn catalog(&self) -> Arc<dyn Catalog> {
140 self.catalog.clone()
141 }
142 /// Gets the current schema for this view, optionally for a specific branch
143 ///
144 /// # Arguments
145 /// * `branch` - Optional branch name to get the schema for. If None, returns the main branch schema
146 ///
147 /// # Returns
148 /// * `Result<&Schema, Error>` - The current schema for the view/branch, or an error if not found
149 ///
150 /// The schema defines the structure of the view's output, including:
151 /// - Column names and types
152 /// - Column IDs and documentation
153 /// - Whether columns are required/optional
154 pub fn current_schema(&self, branch: Option<&str>) -> Result<&Schema, Error> {
155 self.metadata.current_schema(branch).map_err(Error::from)
156 }
157 /// Gets the underlying view metadata that defines this view
158 ///
159 /// The metadata contains:
160 /// - View UUID and format version
161 /// - Storage location information
162 /// - Version history and schema evolution
163 /// - View properties and configurations
164 /// - SQL representations and other view definitions
165 ///
166 /// # Returns
167 /// * `&ViewMetadata` - A reference to the view's metadata
168 pub fn metadata(&self) -> &ViewMetadata {
169 &self.metadata
170 }
171 /// Creates a new transaction for performing atomic updates to this view
172 ///
173 /// # Arguments
174 /// * `branch` - Optional branch name to create the transaction for. If None, uses the main branch
175 ///
176 /// # Returns
177 /// * `ViewTransaction` - A new transaction that can be used to:
178 /// - Update view representations and schemas
179 /// - Modify view properties
180 /// - Commit changes atomically
181 ///
182 /// Transactions ensure that all changes are applied atomically with ACID guarantees.
183 /// Multiple operations can be chained and will be committed together.
184 pub fn new_transaction(&mut self, branch: Option<&str>) -> ViewTransaction<'_> {
185 ViewTransaction::new(self, branch)
186 }
187}