@@ -15,7 +15,9 @@ use super::{CustomExecuteHandler, Operation};
1515use crate :: delta_datafusion:: expr:: fmt_expr_to_sql;
1616use crate :: delta_datafusion:: { create_session, register_store, DeltaDataChecker , DeltaScanBuilder } ;
1717use crate :: kernel:: transaction:: { CommitBuilder , CommitProperties } ;
18- use crate :: kernel:: { EagerSnapshot , MetadataExt , ProtocolExt as _, ProtocolInner } ;
18+ use crate :: kernel:: {
19+ resolve_snapshot, EagerSnapshot , MetadataExt , ProtocolExt as _, ProtocolInner ,
20+ } ;
1921use crate :: logstore:: LogStoreRef ;
2022use crate :: operations:: datafusion_utils:: Expression ;
2123use crate :: protocol:: DeltaOperation ;
@@ -25,7 +27,7 @@ use crate::{DeltaResult, DeltaTable, DeltaTableError};
2527/// Build a constraint to add to a table
2628pub struct ConstraintBuilder {
2729 /// A snapshot of the table's state
28- snapshot : EagerSnapshot ,
30+ snapshot : Option < EagerSnapshot > ,
2931 /// Name of the constraint
3032 name : Option < String > ,
3133 /// Constraint expression
@@ -50,7 +52,7 @@ impl super::Operation<()> for ConstraintBuilder {
5052
5153impl ConstraintBuilder {
5254 /// Create a new builder
53- pub fn new ( log_store : LogStoreRef , snapshot : EagerSnapshot ) -> Self {
55+ pub fn new ( log_store : LogStoreRef , snapshot : Option < EagerSnapshot > ) -> Self {
5456 Self {
5557 name : None ,
5658 expr : None ,
@@ -101,11 +103,8 @@ impl std::future::IntoFuture for ConstraintBuilder {
101103 let this = self ;
102104
103105 Box :: pin ( async move {
104- if !this. snapshot . load_config ( ) . require_files {
105- return Err ( DeltaTableError :: NotInitializedWithFiles (
106- "ADD CONSTRAINTS" . into ( ) ,
107- ) ) ;
108- }
106+ let snapshot = resolve_snapshot ( & this. log_store , this. snapshot . clone ( ) , true ) . await ?;
107+
109108 let operation_id = this. get_operation_id ( ) ;
110109 this. pre_execute ( operation_id) . await ?;
111110
@@ -118,7 +117,7 @@ impl std::future::IntoFuture for ConstraintBuilder {
118117 . expr
119118 . ok_or_else ( || DeltaTableError :: Generic ( "No Expression provided" . to_string ( ) ) ) ?;
120119
121- let mut metadata = this . snapshot . metadata ( ) . clone ( ) ;
120+ let mut metadata = snapshot. metadata ( ) . clone ( ) ;
122121 let configuration_key = format ! ( "delta.constraints.{name}" ) ;
123122
124123 if metadata. configuration ( ) . contains_key ( & configuration_key) {
@@ -132,10 +131,9 @@ impl std::future::IntoFuture for ConstraintBuilder {
132131 . unwrap_or_else ( || Arc :: new ( create_session ( ) . into_inner ( ) . state ( ) ) ) ;
133132 register_store ( this. log_store . clone ( ) , session. runtime_env ( ) . as_ref ( ) ) ;
134133
135- let scan =
136- DeltaScanBuilder :: new ( & this. snapshot , this. log_store . clone ( ) , session. as_ref ( ) )
137- . build ( )
138- . await ?;
134+ let scan = DeltaScanBuilder :: new ( & snapshot, this. log_store . clone ( ) , session. as_ref ( ) )
135+ . build ( )
136+ . await ?;
139137
140138 let schema = scan. schema ( ) . to_dfschema ( ) ?;
141139 let expr = into_expr ( expr, & schema, session. as_ref ( ) ) ?;
@@ -175,7 +173,7 @@ impl std::future::IntoFuture for ConstraintBuilder {
175173 metadata =
176174 metadata. add_config_key ( format ! ( "delta.constraints.{name}" ) , expr_str. clone ( ) ) ?;
177175
178- let old_protocol = this . snapshot . protocol ( ) ;
176+ let old_protocol = snapshot. protocol ( ) ;
179177 let protocol = ProtocolInner {
180178 min_reader_version : if old_protocol. min_reader_version ( ) > 1 {
181179 old_protocol. min_reader_version ( )
@@ -213,7 +211,7 @@ impl std::future::IntoFuture for ConstraintBuilder {
213211 . with_actions ( actions)
214212 . with_operation_id ( operation_id)
215213 . with_post_commit_hook_handler ( this. custom_execute_handler . clone ( ) )
216- . build ( Some ( & this . snapshot ) , this. log_store . clone ( ) , operation)
214+ . build ( Some ( & snapshot) , this. log_store . clone ( ) , operation)
217215 . await ?;
218216
219217 if let Some ( handler) = this. custom_execute_handler {
0 commit comments