forked from duckdb/duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrow-c.cpp
More file actions
323 lines (277 loc) · 11.3 KB
/
arrow-c.cpp
File metadata and controls
323 lines (277 loc) · 11.3 KB
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
321
322
323
#include "duckdb/common/arrow/arrow.hpp"
#include "duckdb/common/arrow/arrow_converter.hpp"
#include "duckdb/function/table/arrow.hpp"
#include "duckdb/main/capi/capi_internal.hpp"
#include "duckdb/main/prepared_statement_data.hpp"
using duckdb::ArrowConverter;
using duckdb::ArrowResultWrapper;
using duckdb::Connection;
using duckdb::DataChunk;
using duckdb::LogicalType;
using duckdb::MaterializedQueryResult;
using duckdb::PreparedStatementWrapper;
using duckdb::QueryResult;
using duckdb::QueryResultType;
duckdb_state duckdb_query_arrow(duckdb_connection connection, const char *query, duckdb_arrow *out_result) {
Connection *conn = (Connection *)connection;
auto wrapper = new ArrowResultWrapper();
wrapper->result = conn->Query(query);
*out_result = (duckdb_arrow)wrapper;
return !wrapper->result->HasError() ? DuckDBSuccess : DuckDBError;
}
duckdb_state duckdb_query_arrow_schema(duckdb_arrow result, duckdb_arrow_schema *out_schema) {
if (!out_schema) {
return DuckDBSuccess;
}
auto wrapper = reinterpret_cast<ArrowResultWrapper *>(result);
try {
ArrowConverter::ToArrowSchema((ArrowSchema *)*out_schema, wrapper->result->types, wrapper->result->names,
wrapper->result->client_properties);
} catch (...) {
return DuckDBError;
}
return DuckDBSuccess;
}
duckdb_state duckdb_prepared_arrow_schema(duckdb_prepared_statement prepared, duckdb_arrow_schema *out_schema) {
if (!out_schema) {
return DuckDBSuccess;
}
auto wrapper = reinterpret_cast<PreparedStatementWrapper *>(prepared);
if (!wrapper || !wrapper->statement || !wrapper->statement->data) {
return DuckDBError;
}
auto properties = wrapper->statement->context->GetClientProperties();
duckdb::vector<duckdb::LogicalType> prepared_types;
duckdb::vector<duckdb::string> prepared_names;
auto count = wrapper->statement->data->properties.parameter_count;
for (idx_t i = 0; i < count; i++) {
// Every prepared parameter type is UNKNOWN, which we need to map to NULL according to the spec of
// 'AdbcStatementGetParameterSchema'
const auto type = LogicalType::SQLNULL;
// FIXME: we don't support named parameters yet, but when we do, this needs to be updated
auto name = std::to_string(i);
prepared_types.push_back(type);
prepared_names.push_back(name);
}
auto result_schema = (ArrowSchema *)*out_schema;
if (!result_schema) {
return DuckDBError;
}
if (result_schema->release) {
// Need to release the existing schema before we overwrite it
result_schema->release(result_schema);
D_ASSERT(!result_schema->release);
}
ArrowConverter::ToArrowSchema(result_schema, prepared_types, prepared_names, properties);
return DuckDBSuccess;
}
duckdb_state duckdb_query_arrow_array(duckdb_arrow result, duckdb_arrow_array *out_array) {
if (!out_array) {
return DuckDBSuccess;
}
auto wrapper = reinterpret_cast<ArrowResultWrapper *>(result);
auto success = wrapper->result->TryFetch(wrapper->current_chunk, wrapper->result->GetErrorObject());
if (!success) { // LCOV_EXCL_START
return DuckDBError;
} // LCOV_EXCL_STOP
if (!wrapper->current_chunk || wrapper->current_chunk->size() == 0) {
return DuckDBSuccess;
}
auto extension_type_cast = duckdb::ArrowTypeExtensionData::GetExtensionTypes(
*wrapper->result->client_properties.client_context, wrapper->result->types);
ArrowConverter::ToArrowArray(*wrapper->current_chunk, reinterpret_cast<ArrowArray *>(*out_array),
wrapper->result->client_properties, extension_type_cast);
return DuckDBSuccess;
}
void duckdb_result_arrow_array(duckdb_result result, duckdb_data_chunk chunk, duckdb_arrow_array *out_array) {
if (!out_array) {
return;
}
auto dchunk = reinterpret_cast<duckdb::DataChunk *>(chunk);
auto &result_data = *(reinterpret_cast<duckdb::DuckDBResultData *>(result.internal_data));
auto extension_type_cast = duckdb::ArrowTypeExtensionData::GetExtensionTypes(
*result_data.result->client_properties.client_context, result_data.result->types);
ArrowConverter::ToArrowArray(*dchunk, reinterpret_cast<ArrowArray *>(*out_array),
result_data.result->client_properties, extension_type_cast);
}
idx_t duckdb_arrow_row_count(duckdb_arrow result) {
auto wrapper = reinterpret_cast<ArrowResultWrapper *>(result);
if (wrapper->result->HasError()) {
return 0;
}
return wrapper->result->RowCount();
}
idx_t duckdb_arrow_column_count(duckdb_arrow result) {
auto wrapper = reinterpret_cast<ArrowResultWrapper *>(result);
return wrapper->result->ColumnCount();
}
idx_t duckdb_arrow_rows_changed(duckdb_arrow result) {
auto wrapper = reinterpret_cast<ArrowResultWrapper *>(result);
if (wrapper->result->HasError()) {
return 0;
}
idx_t rows_changed = 0;
auto &collection = wrapper->result->Collection();
idx_t row_count = collection.Count();
if (row_count > 0 && wrapper->result->properties.return_type == duckdb::StatementReturnType::CHANGED_ROWS) {
auto rows = collection.GetRows();
D_ASSERT(row_count == 1);
D_ASSERT(rows.size() == 1);
rows_changed = duckdb::NumericCast<idx_t>(rows[0].GetValue(0).GetValue<int64_t>());
}
return rows_changed;
}
const char *duckdb_query_arrow_error(duckdb_arrow result) {
auto wrapper = reinterpret_cast<ArrowResultWrapper *>(result);
return wrapper->result->GetError().c_str();
}
void duckdb_destroy_arrow(duckdb_arrow *result) {
if (*result) {
auto wrapper = reinterpret_cast<ArrowResultWrapper *>(*result);
delete wrapper;
*result = nullptr;
}
}
void duckdb_destroy_arrow_stream(duckdb_arrow_stream *stream_p) {
auto stream = reinterpret_cast<ArrowArrayStream *>(*stream_p);
if (!stream) {
return;
}
if (stream->release) {
stream->release(stream);
}
D_ASSERT(!stream->release);
delete stream;
*stream_p = nullptr;
}
duckdb_state duckdb_execute_prepared_arrow(duckdb_prepared_statement prepared_statement, duckdb_arrow *out_result) {
auto wrapper = reinterpret_cast<PreparedStatementWrapper *>(prepared_statement);
if (!wrapper || !wrapper->statement || wrapper->statement->HasError() || !out_result) {
return DuckDBError;
}
auto arrow_wrapper = new ArrowResultWrapper();
auto result = wrapper->statement->Execute(wrapper->values, false);
D_ASSERT(result->type == QueryResultType::MATERIALIZED_RESULT);
arrow_wrapper->result = duckdb::unique_ptr_cast<QueryResult, MaterializedQueryResult>(std::move(result));
*out_result = reinterpret_cast<duckdb_arrow>(arrow_wrapper);
return !arrow_wrapper->result->HasError() ? DuckDBSuccess : DuckDBError;
}
namespace arrow_array_stream_wrapper {
namespace {
struct PrivateData {
ArrowSchema *schema;
ArrowArray *array;
bool done = false;
};
// LCOV_EXCL_START
// This function is never called, but used to set ArrowSchema's release functions to a non-null NOOP.
void EmptySchemaRelease(ArrowSchema *schema) {
schema->release = nullptr;
}
// LCOV_EXCL_STOP
void EmptyArrayRelease(ArrowArray *array) {
array->release = nullptr;
}
void EmptyStreamRelease(ArrowArrayStream *stream) {
stream->release = nullptr;
}
void FactoryGetSchema(ArrowArrayStream *stream, ArrowSchema &schema) {
stream->get_schema(stream, &schema);
}
int GetSchema(struct ArrowArrayStream *stream, struct ArrowSchema *out) {
auto private_data = static_cast<arrow_array_stream_wrapper::PrivateData *>((stream->private_data));
if (private_data->schema == nullptr) {
return DuckDBError;
}
*out = *private_data->schema;
return DuckDBSuccess;
}
int GetNext(struct ArrowArrayStream *stream, struct ArrowArray *out) {
auto private_data = static_cast<arrow_array_stream_wrapper::PrivateData *>((stream->private_data));
*out = *private_data->array;
if (private_data->done) {
out->release = nullptr;
} else {
out->release = EmptyArrayRelease;
}
private_data->done = true;
return DuckDBSuccess;
}
duckdb::unique_ptr<duckdb::ArrowArrayStreamWrapper> FactoryGetNext(uintptr_t stream_factory_ptr,
duckdb::ArrowStreamParameters ¶meters) {
auto stream = reinterpret_cast<ArrowArrayStream *>(stream_factory_ptr);
auto ret = duckdb::make_uniq<duckdb::ArrowArrayStreamWrapper>();
ret->arrow_array_stream = *stream;
ret->arrow_array_stream.release = EmptyStreamRelease;
return ret;
}
// LCOV_EXCL_START
// This function is never be called, because it's used to construct a stream wrapping around a caller-supplied
// ArrowArray. Thus, the stream itself cannot produce an error.
const char *GetLastError(struct ArrowArrayStream *stream) {
return nullptr;
}
// LCOV_EXCL_STOP
void Release(struct ArrowArrayStream *stream) {
if (stream->private_data != nullptr) {
auto private_data = reinterpret_cast<PrivateData *>(stream->private_data);
if (private_data->schema && private_data->schema->release) {
private_data->schema->release(private_data->schema);
private_data->schema = nullptr;
}
delete private_data;
}
stream->private_data = nullptr;
stream->release = nullptr;
}
duckdb_state Ingest(duckdb_connection connection, const char *table_name, struct ArrowArrayStream *input) {
try {
auto cconn = reinterpret_cast<duckdb::Connection *>(connection);
cconn
->TableFunction("arrow_scan", {duckdb::Value::POINTER((uintptr_t)input),
duckdb::Value::POINTER((uintptr_t)FactoryGetNext),
duckdb::Value::POINTER((uintptr_t)FactoryGetSchema)})
->CreateView(table_name, true, false);
} catch (...) { // LCOV_EXCL_START
// Tried covering this in tests, but it proved harder than expected. At the time of writing:
// - Passing any name to `CreateView` worked without throwing an exception
// - Passing a null Arrow array worked without throwing an exception
// - Passing an invalid schema (without any columns) led to an InternalException with SIGABRT, which is meant to
// be un-catchable. This case likely needs to be handled gracefully within `arrow_scan`.
// Ref: https://discord.com/channels/909674491309850675/921100573732909107/1115230468699336785
return DuckDBError;
} // LCOV_EXCL_STOP
return DuckDBSuccess;
}
} // namespace
} // namespace arrow_array_stream_wrapper
duckdb_state duckdb_arrow_scan(duckdb_connection connection, const char *table_name, duckdb_arrow_stream arrow) {
auto stream = reinterpret_cast<ArrowArrayStream *>(arrow);
if (!stream) {
return DuckDBError;
}
return arrow_array_stream_wrapper::Ingest(connection, table_name, stream);
}
duckdb_state duckdb_arrow_array_scan(duckdb_connection connection, const char *table_name,
duckdb_arrow_schema arrow_schema, duckdb_arrow_array arrow_array,
duckdb_arrow_stream *out_stream) {
if (!out_stream) {
return DuckDBError;
}
if (!arrow_schema || !arrow_array) {
*out_stream = nullptr;
return DuckDBError;
}
auto private_data = new arrow_array_stream_wrapper::PrivateData;
private_data->schema = reinterpret_cast<ArrowSchema *>(arrow_schema);
private_data->array = reinterpret_cast<ArrowArray *>(arrow_array);
private_data->done = false;
ArrowArrayStream *stream = new ArrowArrayStream;
*out_stream = reinterpret_cast<duckdb_arrow_stream>(stream);
stream->get_schema = arrow_array_stream_wrapper::GetSchema;
stream->get_next = arrow_array_stream_wrapper::GetNext;
stream->get_last_error = arrow_array_stream_wrapper::GetLastError;
stream->release = arrow_array_stream_wrapper::Release;
stream->private_data = private_data;
return duckdb_arrow_scan(connection, table_name, reinterpret_cast<duckdb_arrow_stream>(stream));
}