1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
use std::collections::HashSet;
use itertools::Itertools;
use sea_query::{Asterisk, ExprTrait, Order, Query};
use crate::ducklake::SnapshotAccess;
use crate::spec::{DucklakeColumn, ducklake_column};
use crate::table::TableInfo;
use crate::{
Ducklake,
DucklakeError,
DucklakeResult,
IfExistsStrategy,
Table,
TableName,
WriteDataFile,
io,
scan,
utils,
};
/// A table to transfer, optionally with a new name in the target DuckLake.
pub struct TableTransfer<'a> {
table: &'a Table,
name: Option<TableName>,
}
impl<'a> From<&'a Table> for TableTransfer<'a> {
fn from(table: &'a Table) -> Self {
Self { table, name: None }
}
}
impl<'a> From<(TableName, &'a Table)> for TableTransfer<'a> {
fn from((name, table): (TableName, &'a Table)) -> Self {
Self {
table,
name: Some(name),
}
}
}
/* -------------------------------------- TABLE TRANSFER -------------------------------------- */
struct TableTransferInfo {
info: TableInfo,
scan: scan::TableTransferScan,
retired_columns: Vec<DucklakeColumn>,
}
impl Table {
async fn transfer_info(&self) -> DucklakeResult<TableTransferInfo> {
let snapshot = self.conn.snapshot(SnapshotAccess::Any).await?;
let catalog = snapshot.catalog().await?;
let table = catalog.table(self.id)?;
let info = table.info();
let data_path = table.data_path(&self.conn.metadata().data_path());
// Find all retired column IDs. These IDs must stay reserved to not expose any IDs.
let active_column_ids: HashSet<_> = info
.schema
.columns
.values()
.flat_map(crate::Column::flatten)
.filter_map(|column| column.column.field_id)
.collect();
let query = Query::select()
.column(Asterisk)
.from(ducklake_column::Table)
.and_where(ducklake_column::Column::TableId.col().eq(self.id))
.order_by(ducklake_column::Column::BeginSnapshot, Order::Desc)
.to_owned();
let columns: Vec<DucklakeColumn> = self.conn.pool().fetch_all(&query).await?;
let retired_columns = columns
.into_iter()
.filter(|column| !active_column_ids.contains(&column.column_id))
.unique_by(|column| column.column_id)
.collect();
// Scan the table
let scan = scan::scan_table_for_transfer(
self.conn.pool(),
self.id,
snapshot,
self.conn.snapshot_cache(),
&data_path,
)
.await?;
Ok(TableTransferInfo {
info,
scan,
retired_columns,
})
}
}
impl Ducklake {
/// Copy tables from this DuckLake into `target` in a single transaction.
///
/// Each copied table owns newly-created copies of the corresponding source table's data files.
/// All tables are created and populated within one transaction, so the entire batch is
/// committed as a single snapshot (or fails without any partial changes to the catalog).
/// File copies precede the commit, so a failed copy or commit may leave orphaned target files.
///
/// Passing tables directly retains their names. Passing `(TableName, &Table)` pairs renames
/// them in the target. All tables must belong to this DuckLake and no target table may already
/// exist.
pub async fn copy_tables<'a, I, T>(
&self,
tables: I,
target: &Ducklake,
) -> DucklakeResult<Vec<Table>>
where
I: IntoIterator<Item = T>,
T: Into<TableTransfer<'a>>,
{
let tables = tables.into_iter().map(Into::into).collect::<Vec<_>>();
self.transfer_tables(&tables, target, true).await
}
/// Move tables from this DuckLake into `target` in a single transaction.
///
/// The moved tables re-register the existing data files using absolute paths. Once the target
/// commit succeeds, the source tables are dropped and their file metadata is detached in a
/// single source-side transaction, so source maintenance cannot delete files now owned by this
/// DuckLake. Note that this means that time travel on the source DuckLake cannot recover moved
/// files.
///
/// The target creation is atomic, but because the source and target are distinct catalogs the
/// overall move is not: if the source changed since the transfer began, the source drop is
/// rejected with [`DucklakeError::TableChangedDuringTransfer`] and the (already committed)
/// target tables are retained.
///
/// Passing tables directly retains their names. Passing `(TableName, &Table)` pairs renames
/// them in the target. All tables must belong to this DuckLake and no target table may already
/// exist.
pub async fn move_tables<'a, I, T>(
&self,
tables: I,
target: &Ducklake,
) -> DucklakeResult<Vec<Table>>
where
I: IntoIterator<Item = T>,
T: Into<TableTransfer<'a>>,
{
let tables = tables.into_iter().map(Into::into).collect::<Vec<_>>();
self.transfer_tables(&tables, target, false).await
}
async fn transfer_tables(
&self,
tables: &[TableTransfer<'_>],
target: &Ducklake,
copy_files: bool,
) -> DucklakeResult<Vec<Table>> {
if tables.is_empty() {
return Ok(Vec::new());
}
let source_conn = &self.conn;
if !tables
.iter()
.all(|transfer| transfer.table.conn.is_same_catalog(source_conn))
{
return Err(DucklakeError::InvalidTableTransfer(
"all tables must belong to the source DuckLake".to_string(),
));
}
if target.conn.is_same_catalog(source_conn) {
return Err(DucklakeError::InvalidTableTransfer(
"the source and target must be different DuckLake catalogs".to_string(),
));
}
if !copy_files {
let mut seen = std::collections::HashSet::with_capacity(tables.len());
if !tables.iter().all(|transfer| seen.insert(transfer.table.id)) {
return Err(DucklakeError::InvalidTableTransfer(
"a table cannot occur more than once in a move".to_string(),
));
}
}
// For a move, capture the source's head snapshot up front (also asserting the source is
// writable) so we can detect concurrent writers before detaching files below.
let source_snapshot_id = if copy_files {
None
} else {
Some(source_conn.snapshot(SnapshotAccess::Write).await?.info().id)
};
// Scan all sources before making any target changes.
let transfer_info = futures::future::try_join_all(
tables.iter().map(|transfer| transfer.table.transfer_info()),
)
.await?;
let source_names = transfer_info
.iter()
.map(|transfer| transfer.info.name.clone())
.collect::<Vec<_>>();
// Resolve the target names, defaulting to the source names.
let target_names = tables
.iter()
.zip(&source_names)
.map(|(transfer, source_name)| {
transfer.name.clone().unwrap_or_else(|| source_name.clone())
})
.collect::<Vec<_>>();
// Reject duplicate target names up front so a collision surfaces clearly rather than as a
// confusing "already exists" error midway through the transaction.
let mut seen = std::collections::HashSet::with_capacity(target_names.len());
for name in &target_names {
if !seen.insert(name) {
return Err(DucklakeError::InvalidTableTransfer(format!(
"multiple tables target the same name '{name}'"
)));
}
}
// Create every table and write its data within a single target transaction. File copies
// (for `copy`) happen mid-transaction.
let mut tx = target.conn.transaction(None).await?;
for ((transfer, info), name) in tables.iter().zip(transfer_info).zip(&target_names) {
let source = transfer.table;
let TableTransferInfo {
info,
scan,
retired_columns,
} = info;
tx.create_schema(&name.schema, None, IfExistsStrategy::Skip)?;
let mut table = tx.create_transfer_table(name.clone(), info, retired_columns)?;
let (_, generator) = table.get_write_info()?;
let mut data_files = Vec::with_capacity(scan.result.data_files.len());
for (data_file, metadata) in scan
.result
.data_files
.into_iter()
.zip(scan.partition_values)
{
let mut path = data_file.path;
let mut delete_files = data_file.delete_files;
if copy_files {
path = copy_transfer_file(
source.conn.storage_options(),
target.conn.storage_options(),
&generator,
&path,
)
.await?;
for delete_file in &mut delete_files {
delete_file.path = copy_transfer_file(
source.conn.storage_options(),
target.conn.storage_options(),
&generator,
&delete_file.path,
)
.await?;
}
}
data_files.push(crate::transaction::TransferDataFile {
data_file: WriteDataFile {
path,
statistics: Some(data_file.statistics),
partition_values: None,
},
partition_values: metadata,
delete_files,
inline_deletes: data_file
.inline_deletes
.map(|row_ids| row_ids.values().to_vec())
.unwrap_or_default(),
});
}
if !data_files.is_empty() {
table.write_transfer_data_files(data_files).await?;
}
if !scan.result.inline_data.is_empty() {
table.write_inline_data(scan.result.inline_data)?;
}
}
tx.commit().await?;
// For a move, drop the source tables now that the target commit succeeded. A single
// snapshot guard suffices because all sources share one catalog: reject the drop if any
// concurrent writer advanced the source snapshot, since their files were not transferred.
if let Some(snapshot_id) = source_snapshot_id {
if source_conn.snapshot(SnapshotAccess::Write).await?.info().id != snapshot_id {
return Err(DucklakeError::TableChangedDuringTransfer);
}
let mut tx = source_conn.transaction(None).await?;
for name in &source_names {
tx.delete_table_transferring_file_ownership(name)?;
}
tx.commit().await?;
}
futures::future::try_join_all(target_names.into_iter().map(|name| target.table(name)))
.await
}
}
/// Copy a file into the target's data directory and return its table-relative path.
async fn copy_transfer_file(
source_options: &[(String, String)],
target_options: &[(String, String)],
generator: &utils::DataFilePathGenerator,
source_path: &str,
) -> DucklakeResult<String> {
let source = source_path.parse::<io::DucklakePath>()?;
let relative_path = generator.generate_relative(&Default::default());
let destination = generator
.base_path()
.parse::<io::DucklakePath>()?
.join_str(&relative_path);
io::copy_file(&source, source_options, &destination, target_options).await?;
Ok(relative_path)
}