Skip to content

Commit 623fd06

Browse files
authored
duckdb: batch processing in copy function (#9505)
- Support parallel and batched writes in COPY TO function. - Fix PARTITON_BY segfault in copy function. - Fix aligned loads crash while exporting 128-bit width types like UUID. Resolves: #9484 Signed-off-by: Mikhail Kot <[email protected]>
1 parent cd10f22 commit 623fd06

10 files changed

Lines changed: 271 additions & 71 deletions

File tree

‎vortex-duckdb/cpp/copy_function.cpp‎

Lines changed: 80 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,51 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
#include "copy_function.hpp"
35
#include "data.hpp"
46
#include "error.hpp"
57
#include "vortex_duckdb.h"
68
#include "table_function.h"
79
#include "vortex.h"
8-
#include "duckdb/function/copy_function.hpp"
10+
#include "duckdb/common/types/column/column_data_collection.hpp"
911
#include "duckdb/main/capi/capi_internal.hpp"
1012
#include "duckdb/main/client_context.hpp"
1113
#include "duckdb/main/connection.hpp"
1214
#include "duckdb/parser/parsed_data/create_copy_function_info.hpp"
1315

14-
using namespace duckdb;
15-
16-
struct CopyBindData final : TableFunctionData {
17-
CopyBindData(unique_ptr<CData> ffi_data) : ffi_data(std::move(ffi_data)) {
18-
}
19-
unique_ptr<CData> ffi_data;
20-
};
21-
22-
struct CopyGlobalData final : GlobalFunctionData {
23-
CopyGlobalData(unique_ptr<CData> ffi_data) : ffi_data(std::move(ffi_data)) {
24-
}
25-
26-
unique_ptr<CData> ffi_data;
27-
};
28-
2916
unique_ptr<FunctionData> copy_to_bind(ClientContext &,
3017
CopyFunctionBindInput &,
31-
const vector<string> &column_names,
32-
const vector<LogicalType> &column_types) {
33-
vector<const char *> ffi_column_names(column_names.size());
34-
for (size_t i = 0; i < column_names.size(); ++i) {
35-
ffi_column_names[i] = column_names[i].c_str();
18+
const vector<string> &names,
19+
const vector<LogicalType> &types) {
20+
vector<const char *> ffi_names(names.size());
21+
for (size_t i = 0; i < names.size(); ++i) {
22+
ffi_names[i] = names[i].c_str();
3623
}
3724

38-
vector<duckdb_logical_type> ffi_column_types(column_types.size());
39-
for (size_t i = 0; i < column_types.size(); ++i) {
25+
vector<duckdb_logical_type> ffi_types(types.size());
26+
for (size_t i = 0; i < types.size(); ++i) {
4027
// duckdb C api doesn't allow passing const LogicalTypes. We never
4128
// modify input in copy function.
42-
ffi_column_types[i] =
43-
reinterpret_cast<duckdb_logical_type>(const_cast<LogicalType *>(&column_types[i]));
29+
ffi_types[i] = reinterpret_cast<duckdb_logical_type>(const_cast<LogicalType *>(&types[i]));
4430
}
4531

4632
duckdb_vx_error error_out = nullptr;
47-
const duckdb_vx_data ffi_bind_data = duckdb_copy_function_copy_to_bind(ffi_column_names.data(),
48-
ffi_column_names.size(),
49-
ffi_column_types.data(),
50-
ffi_column_types.size(),
33+
const duckdb_vx_data ffi_bind_data = duckdb_copy_function_copy_to_bind(ffi_names.data(),
34+
ffi_names.size(),
35+
ffi_types.data(),
36+
ffi_types.size(),
5137
&error_out);
5238
if (error_out) {
5339
throw BinderException(IntoErrString(error_out));
5440
}
5541
auto cdata = unique_ptr<CData>(reinterpret_cast<CData *>(ffi_bind_data));
56-
return make_uniq<CopyBindData>(std::move(cdata));
42+
return make_uniq<VortexCopyBindData>(std::move(cdata));
5743
}
5844

5945
unique_ptr<GlobalFunctionData>
6046
copy_to_initialize_global(ClientContext &, FunctionData &bind_data, const string &file_path) {
61-
void *const ffi_bind = bind_data.Cast<CopyBindData>().ffi_data->DataPtr();
47+
const VortexCopyBindData &bind = bind_data.Cast<VortexCopyBindData>();
48+
const void *const ffi_bind = bind.ffi_bind->DataPtr();
6249

6350
duckdb_vx_error error_out = nullptr;
6451
const duckdb_vx_data ffi_global =
@@ -68,17 +55,21 @@ copy_to_initialize_global(ClientContext &, FunctionData &bind_data, const string
6855
}
6956

7057
auto cdata = unique_ptr<CData>(reinterpret_cast<CData *>(ffi_global));
71-
return make_uniq<CopyGlobalData>(std::move(cdata));
58+
return make_uniq<VortexCopyGlobalState>(std::move(cdata));
7259
}
7360

7461
void copy_to_sink(ExecutionContext &,
7562
FunctionData &bind_data,
7663
GlobalFunctionData &gstate,
7764
LocalFunctionData &,
7865
DataChunk &input) {
79-
void *const ffi_bind = bind_data.Cast<CopyBindData>().ffi_data->DataPtr();
80-
void *const ffi_global = gstate.Cast<CopyGlobalData>().ffi_data->DataPtr();
81-
auto ffi_chunk = reinterpret_cast<duckdb_data_chunk>(&input);
66+
const VortexCopyBindData &bind = bind_data.Cast<VortexCopyBindData>();
67+
const VortexCopyGlobalState &global = gstate.Cast<VortexCopyGlobalState>();
68+
69+
const void *const ffi_bind = bind.ffi_bind->DataPtr();
70+
const void *const ffi_global = global.ffi_global->DataPtr();
71+
72+
duckdb_data_chunk ffi_chunk = reinterpret_cast<duckdb_data_chunk>(&input);
8273
duckdb_vx_error error_out = nullptr;
8374
duckdb_copy_function_copy_to_sink(ffi_bind, ffi_global, ffi_chunk, &error_out);
8475
if (error_out) {
@@ -87,14 +78,52 @@ void copy_to_sink(ExecutionContext &,
8778
}
8879

8980
void copy_to_finalize(ClientContext &, FunctionData &, GlobalFunctionData &gstate) {
90-
void *const ffi_global = gstate.Cast<CopyGlobalData>().ffi_data->DataPtr();
81+
const VortexCopyGlobalState &global = gstate.Cast<VortexCopyGlobalState>();
82+
83+
void *const ffi_global = global.ffi_global->DataPtr();
9184
duckdb_vx_error error_out = nullptr;
9285
duckdb_copy_function_copy_to_finalize(ffi_global, &error_out);
9386
if (error_out) {
9487
throw ExecutorException(IntoErrString(error_out));
9588
}
9689
}
9790

91+
unique_ptr<PreparedBatchData> copy_to_prepare_batch(ClientContext &,
92+
FunctionData &bind_data,
93+
GlobalFunctionData &,
94+
unique_ptr<ColumnDataCollection> collection) {
95+
const VortexCopyBindData &bind = bind_data.Cast<VortexCopyBindData>();
96+
97+
const void *const ffi_bind = bind.ffi_bind->DataPtr();
98+
auto ffi_batch = unique_ptr<CData>(reinterpret_cast<CData *>(duckdb_copy_function_prepare_batch_new()));
99+
duckdb_vx_error error_out = nullptr;
100+
101+
for (DataChunk &chunk : collection->Chunks()) {
102+
duckdb_data_chunk ffi_chunk = reinterpret_cast<duckdb_data_chunk>(&chunk);
103+
duckdb_copy_function_prepare_batch_push(ffi_bind, ffi_batch->DataPtr(), ffi_chunk, &error_out);
104+
if (error_out) {
105+
throw ExecutorException(IntoErrString(error_out));
106+
}
107+
}
108+
return make_uniq<VortexCopyPreparedBatchData>(std::move(ffi_batch));
109+
}
110+
111+
void copy_to_flush_batch(ClientContext &,
112+
FunctionData &,
113+
GlobalFunctionData &gstate,
114+
PreparedBatchData &batch_data) {
115+
const VortexCopyGlobalState &global = gstate.Cast<VortexCopyGlobalState>();
116+
const VortexCopyPreparedBatchData &batch = batch_data.Cast<VortexCopyPreparedBatchData>();
117+
118+
const void *const ffi_global = global.ffi_global->DataPtr();
119+
const void *const ffi_batch = batch.ffi_copy_prepared->DataPtr();
120+
duckdb_vx_error error_out = nullptr;
121+
duckdb_copy_function_flush_batch(ffi_global, ffi_batch, &error_out);
122+
if (error_out) {
123+
throw ExecutorException(IntoErrString(error_out));
124+
}
125+
}
126+
98127
extern "C" duckdb_state duckdb_vx_register_copy_function(duckdb_database ffi_db) {
99128
D_ASSERT(ffi_db);
100129
const DatabaseWrapper &wrapper = *reinterpret_cast<DatabaseWrapper *>(ffi_db);
@@ -103,18 +132,29 @@ extern "C" duckdb_state duckdb_vx_register_copy_function(duckdb_database ffi_db)
103132
CopyFunction fn("vortex");
104133
fn.copy_to_bind = copy_to_bind;
105134
fn.copy_to_initialize_global = copy_to_initialize_global;
135+
// required by duckdb
106136
fn.copy_to_initialize_local = [](auto &, auto &) {
107137
return make_uniq<LocalFunctionData>();
108138
};
109139
fn.copy_to_sink = copy_to_sink;
140+
// required by duckdb for PARTITION_BY
141+
fn.copy_to_combine = [](ExecutionContext &, FunctionData &, GlobalFunctionData &, LocalFunctionData &) {
142+
};
110143
fn.copy_to_finalize = copy_to_finalize;
144+
fn.prepare_batch = copy_to_prepare_batch;
145+
fn.flush_batch = copy_to_flush_batch;
111146
fn.extension = "vortex";
112147

113-
// TODO(joe): expose this via c our api
114-
fn.execution_mode = [](bool, bool) {
115-
return CopyFunctionExecutionMode::REGULAR_COPY_TO_FILE;
148+
fn.execution_mode = [](bool preserve_insertion_order, bool supports_batch_index) {
149+
using enum CopyFunctionExecutionMode;
150+
if (!preserve_insertion_order) {
151+
return PARALLEL_COPY_TO_FILE;
152+
}
153+
if (supports_batch_index) {
154+
return BATCH_COPY_TO_FILE;
155+
}
156+
return REGULAR_COPY_TO_FILE;
116157
};
117-
// TODO(joe): handle parameters as in table_function
118158

119159
try {
120160
Catalog &system_catalog = Catalog::GetSystemCatalog(db);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
#pragma once
5+
#include "data.hpp"
6+
#include "duckdb/function/copy_function.hpp"
7+
8+
using namespace duckdb;
9+
10+
struct VortexCopyBindData final : TableFunctionData {
11+
VortexCopyBindData(unique_ptr<CData> ffi_bind) : ffi_bind(std::move(ffi_bind)) {
12+
}
13+
unique_ptr<CData> ffi_bind;
14+
};
15+
16+
struct VortexCopyGlobalState final : GlobalFunctionData {
17+
VortexCopyGlobalState(unique_ptr<CData> ffi_global) : ffi_global(std::move(ffi_global)) {
18+
}
19+
unique_ptr<CData> ffi_global;
20+
};
21+
22+
struct VortexCopyPreparedBatchData final : PreparedBatchData {
23+
VortexCopyPreparedBatchData(unique_ptr<CData> ffi_copy_prepared)
24+
: ffi_copy_prepared(std::move(ffi_copy_prepared)) {
25+
}
26+
unique_ptr<CData> ffi_copy_prepared;
27+
};

‎vortex-duckdb/include/vortex.h‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,25 @@ duckdb_vx_data duckdb_copy_function_copy_to_initialize_global(const void *bind_d
8888

8989
extern
9090
void duckdb_copy_function_copy_to_sink(const void *bind_data,
91-
void *global_data,
91+
const void *global_data,
9292
duckdb_data_chunk data_chunk,
9393
duckdb_vx_error *error_out);
9494

9595
extern void duckdb_copy_function_copy_to_finalize(void *global_data, duckdb_vx_error *error_out);
9696

97+
extern duckdb_vx_data duckdb_copy_function_prepare_batch_new(void);
98+
99+
extern
100+
void duckdb_copy_function_prepare_batch_push(const void *bind,
101+
void *batch,
102+
duckdb_data_chunk chunk,
103+
duckdb_vx_error *error);
104+
105+
extern
106+
void duckdb_copy_function_flush_batch(const void *global,
107+
const void *batch,
108+
duckdb_vx_error *error);
109+
97110
#ifdef __cplusplus
98111
} // extern "C"
99112
#endif // __cplusplus

‎vortex-duckdb/src/convert/vector.rs‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ fn vector_as_slice<T: NativePType>(vector: &VectorRef, len: usize) -> ArrayRef {
8282
.into_array()
8383
}
8484

85+
fn vector_i128_values(vector: &VectorRef, len: usize) -> impl Iterator<Item = i128> {
86+
let base = unsafe { crate::cpp::duckdb_vector_get_data(vector.as_ptr()) }.cast::<i128>();
87+
// In batch copy, columns are 8 byte aligned, so reading vector's values
88+
// as &[i128] is UB. Read data unaligned specifically
89+
(0..len).map(move |i| unsafe { base.add(i).read_unaligned() })
90+
}
91+
8592
fn vector_mapped<T, P: NativePType, F: Fn(&T) -> P>(
8693
vector: &VectorRef,
8794
len: usize,
@@ -311,18 +318,17 @@ pub fn flat_vector_to_vortex(vector: &VectorRef, len: usize) -> VortexResult<Arr
311318
DecimalArray::try_new(Buffer::copy_from(data), decimal_dtype, validity)
312319
}
313320
DecimalType::I128 => {
314-
let data = vector.as_slice_with_len::<i128>(len);
315-
DecimalArray::try_new(Buffer::copy_from(data), decimal_dtype, validity)
321+
let data = Buffer::from_iter(vector_i128_values(vector, len));
322+
DecimalArray::try_new(data, decimal_dtype, validity)
316323
}
317324
_ => vortex_bail!("Unsupported decimal precision: {precision}"),
318325
}
319326
.map(|a| a.into_array())
320327
}
321328
DUCKDB_TYPE::DUCKDB_TYPE_UUID => {
322-
let data = vector.as_slice_with_len::<i128>(len);
323329
let mut bytes = BufferMut::<u8>::with_capacity(len * 16);
324-
for v in data {
325-
let be = (*v as u128) ^ (1u128 << 127);
330+
for v in vector_i128_values(vector, len) {
331+
let be = (v as u128) ^ (1u128 << 127);
326332
bytes.extend_from_slice(&be.to_be_bytes());
327333
}
328334

‎vortex-duckdb/src/copy.rs‎

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use vortex::io::compat::Compat;
3131
use vortex::io::object_store::ObjectStoreWrite;
3232
use vortex::io::runtime::BlockingRuntime;
3333
use vortex::io::runtime::Task;
34-
use vortex::io::runtime::current::CurrentThreadWorkerPool;
3534
use vortex::io::session::RuntimeSessionExt;
3635

3736
use crate::REGISTRY;
@@ -57,12 +56,6 @@ assert_impl_all!(CopyFunctionBind: Send, Clone);
5756
pub struct CopyFunctionGlobal {
5857
write_task: Mutex<Option<Task<VortexResult<WriteSummary>>>>,
5958
sink: Option<Sender<VortexResult<ArrayRef>>>,
60-
// Pool of background workers helping to drive the write task.
61-
// Note that this is optional and without it, we would only drive the task when DuckDB calls
62-
// into us, and we call `RUNTIME.block_on`.
63-
// TODO(myrrc): we should rely only on host threads, remove this
64-
#[expect(dead_code)]
65-
worker_pool: CurrentThreadWorkerPool,
6659
}
6760
assert_impl_all!(CopyFunctionGlobal: Send, Sync);
6861

@@ -87,27 +80,18 @@ pub fn copy_to_bind(
8780
})
8881
}
8982

90-
pub fn copy_to_sink(
91-
bind_data: &CopyFunctionBind,
92-
init_global: &CopyFunctionGlobal,
93-
chunk: &mut DataChunkRef,
94-
) -> VortexResult<()> {
95-
let chunk = data_chunk_to_vortex(bind_data.fields.names(), chunk);
96-
let mut sink = init_global
83+
fn push_to_writer(global: &CopyFunctionGlobal, array: ArrayRef) -> VortexResult<()> {
84+
let mut sink = global
9785
.sink
9886
.as_ref()
9987
.ok_or_else(|| vortex_err!("sink closed early"))?
10088
.clone();
10189
RUNTIME.block_on(async {
102-
// sink.send may error with "receiver is gone" which isn't the
103-
// real error
104-
if sink.send(chunk).await.is_ok() {
90+
// send may error with "receiver is gone" which isn't the real error
91+
if sink.send(Ok(array)).await.is_ok() {
10592
return Ok(());
10693
}
107-
let task;
108-
{
109-
task = init_global.write_task.lock().take();
110-
}
94+
let task = global.write_task.lock().take();
11195
if let Some(task) = task {
11296
// we can get the real error (i.e invalid path) from here
11397
task.await?;
@@ -116,6 +100,40 @@ pub fn copy_to_sink(
116100
})
117101
}
118102

103+
pub fn copy_to_sink(
104+
bind_data: &CopyFunctionBind,
105+
init_global: &CopyFunctionGlobal,
106+
chunk: &mut DataChunkRef,
107+
) -> VortexResult<()> {
108+
push_to_writer(
109+
init_global,
110+
data_chunk_to_vortex(bind_data.fields.names(), chunk)?,
111+
)
112+
}
113+
114+
#[derive(Default)]
115+
pub struct CopyPreparedBatch {
116+
arrays: Vec<ArrayRef>,
117+
}
118+
119+
pub fn prepare_batch_push(
120+
bind: &CopyFunctionBind,
121+
batch: &mut CopyPreparedBatch,
122+
chunk: &DataChunkRef,
123+
) -> VortexResult<()> {
124+
batch
125+
.arrays
126+
.push(data_chunk_to_vortex(bind.fields.names(), chunk)?);
127+
Ok(())
128+
}
129+
130+
pub fn flush_batch(global: &CopyFunctionGlobal, batch: &CopyPreparedBatch) -> VortexResult<()> {
131+
for array in &batch.arrays {
132+
push_to_writer(global, array.clone())?;
133+
}
134+
Ok(())
135+
}
136+
119137
pub fn copy_to_finalize(init_global: &mut CopyFunctionGlobal) -> VortexResult<()> {
120138
RUNTIME.block_on(async {
121139
if let Some(sink) = init_global.sink.take() {
@@ -171,10 +189,7 @@ pub fn copy_to_initialize_global(
171189
})
172190
};
173191

174-
let worker_pool = RUNTIME.new_pool();
175-
worker_pool.set_workers_to_available_parallelism();
176192
Ok(CopyFunctionGlobal {
177-
worker_pool,
178193
write_task: Mutex::new(Some(write_task)),
179194
sink: Some(sink),
180195
})

0 commit comments

Comments
 (0)