-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathlib.rs
534 lines (492 loc) · 22.2 KB
/
lib.rs
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! ADBC: Arrow Database Connectivity
//!
//! ADBC is a set of APIs and libraries for [Arrow](https://arrow.apache.org/)-native
//! access to databases. Execute SQL and [Substrait](https://substrait.io/)
//! queries, query database catalogs, and more, all using Arrow data to
//! eliminate unnecessary data copies, speed up access, and make it more
//! convenient to build analytical applications.
//!
//! Read more about ADBC at <https://arrow.apache.org/adbc/>
//!
//! This library currently provides:
//! - An abstract Rust API to be implemented by vendor-specific drivers.
//! - A driver manager which implements this same API, but dynamically loads
//! drivers internally and forwards calls appropriately using the [C API](https://github.com/apache/arrow-adbc/blob/main/adbc.h).
//! - A driver exporter that takes an implementation of the abstract API and
//! turns it into an object file that implements the C API.
//!
//! # Native Rust drivers
//!
//! Native Rust drivers will implement the abstract API consisting of the traits:
//! - [Driver]
//! - [Database]
//! - [Connection]
//! - [Statement]
//!
//! For drivers implemented in Rust, using these will be more efficient and
//! safe, since it avoids the overhead of going through C FFI.
//!
//! # Driver Manager
//!
//! The [driver_manager] module allows loading drivers exposing the C API,
//! either from an initialization function (link-time, either static or dynamic)
//! or by dynamically finding such a function in a dynamic library (run-time).
//! The driver manager is gated behind the `driver_manager` feature flag.
//!
//! # Driver Exporter
//!
//! The driver exporter allows exposing native Rust drivers as C drivers to be
//! used by other langages via their own driver manager. Once you have an
//! implementation of [Driver], provided that it also implements [Default], you
//! can build it as an object file implementing the C API with the
//! [export_driver] macro.
mod driver_exporter;
#[doc(hidden)]
pub use driver_exporter::FFIDriver;
#[cfg(feature = "driver_manager")]
pub mod driver_manager;
pub mod error;
pub mod ffi;
pub mod options;
pub mod schemas;
use std::collections::HashSet;
use arrow_array::{RecordBatch, RecordBatchReader};
use arrow_schema::Schema;
use error::Result;
use options::{OptionConnection, OptionDatabase, OptionStatement, OptionValue};
/// Ability to configure an object by setting/getting options.
pub trait Optionable {
type Option: AsRef<str>;
/// Set a post-init option.
fn set_option(&mut self, key: Self::Option, value: OptionValue) -> Result<()>;
/// Get a string option value by key.
fn get_option_string(&self, key: Self::Option) -> Result<String>;
/// Get a bytes option value by key.
fn get_option_bytes(&self, key: Self::Option) -> Result<Vec<u8>>;
/// Get an integer option value by key.
fn get_option_int(&self, key: Self::Option) -> Result<i64>;
/// Get a float option value by key.
fn get_option_double(&self, key: Self::Option) -> Result<f64>;
}
/// A handle to an ADBC driver.
pub trait Driver {
type DatabaseType: Database;
/// Allocate and initialize a new database without pre-init options.
fn new_database(&mut self) -> Result<Self::DatabaseType>;
/// Allocate and initialize a new database with pre-init options.
fn new_database_with_opts(
&mut self,
opts: impl IntoIterator<Item = (OptionDatabase, OptionValue)>,
) -> Result<Self::DatabaseType>;
}
/// A handle to an ADBC database.
///
/// Databases hold state shared by multiple connections. This typically means
/// configuration and caches. For in-memory databases, it provides a place to
/// hold ownership of the in-memory database.
///
/// Databases must be kept alive as long as any connections exist.
pub trait Database: Optionable<Option = OptionDatabase> {
type ConnectionType: Connection;
/// Allocate and initialize a new connection without pre-init options.
fn new_connection(&mut self) -> Result<Self::ConnectionType>;
/// Allocate and initialize a new connection with pre-init options.
fn new_connection_with_opts(
&mut self,
opts: impl IntoIterator<Item = (options::OptionConnection, OptionValue)>,
) -> Result<Self::ConnectionType>;
}
/// A handle to an ADBC connection.
///
/// Connections provide methods for query execution, managing prepared
/// statements, using transactions, and so on.
///
/// # Autocommit
///
/// Connections should start in autocommit mode. They can be moved out by
/// setting [options::OptionConnection::AutoCommit] to "false". Turning off
/// autocommit allows customizing the isolation level.
pub trait Connection: Optionable<Option = OptionConnection> {
type StatementType: Statement;
/// Allocate and initialize a new statement.
fn new_statement(&mut self) -> Result<Self::StatementType>;
/// Cancel the in-progress operation on a connection.
fn cancel(&mut self) -> Result<()>;
/// Get metadata about the database/driver.
///
/// # Arguments
///
/// - `codes` - Requested metadata. If `None`, retrieve all available metadata.
///
/// # Result
///
/// The result is an Arrow dataset with the following schema:
///
/// Field Name | Field Type
/// ----------------------------|------------------------
/// info_name | uint32 not null
/// info_value | INFO_SCHEMA
///
/// INFO_SCHEMA is a dense union with members:
///
/// Field Name (Type Code) | Field Type
/// ----------------------------|------------------------
/// string_value (0) | utf8
/// bool_value (1) | bool
/// int64_value (2) | int64
/// int32_bitmask (3) | int32
/// string_list (4) | list\<utf8\>
/// int32_to_int32_list_map (5) | map\<int32, list\<int32\>\>
fn get_info(
&self,
codes: Option<HashSet<options::InfoCode>>,
) -> Result<impl RecordBatchReader + Send>;
/// Get a hierarchical view of all catalogs, database schemas, tables, and
/// columns.
///
/// # Arguments
///
/// - `depth` - The level of nesting to query.
/// - `catalog` - Only show tables in the given catalog. If `None`,
/// do not filter by catalog. If an empty string, only show tables
/// without a catalog. May be a search pattern.
/// - `db_schema` - Only show tables in the given database schema. If
/// `None`, do not filter by database schema. If an empty string, only show
/// tables without a database schema. May be a search pattern.
/// - `table_name` - Only show tables with the given name. If `None`, do not
/// filter by name. May be a search pattern.
/// - `table_type` - Only show tables matching one of the given table
/// types. If `None`, show tables of any type. Valid table types can be fetched
/// from [Connection::get_table_types].
/// - `column_name` - Only show columns with the given name. If
/// `None`, do not filter by name. May be a search pattern..
///
/// # Result
///
/// The result is an Arrow dataset with the following schema:
///
/// | Field Name | Field Type |
/// |--------------------------|--------------------------|
/// | catalog_name | utf8 |
/// | catalog_db_schemas | list\<DB_SCHEMA_SCHEMA\> |
///
/// DB_SCHEMA_SCHEMA is a Struct with fields:
///
/// | Field Name | Field Type |
/// |--------------------------|-------------------------|
/// | db_schema_name | utf8 |
/// | db_schema_tables | list\<TABLE_SCHEMA\> |
///
/// TABLE_SCHEMA is a Struct with fields:
///
/// | Field Name | Field Type |
/// |--------------------------|---------------------------|
/// | table_name | utf8 not null |
/// | table_type | utf8 not null |
/// | table_columns | list\<COLUMN_SCHEMA\> |
/// | table_constraints | list\<CONSTRAINT_SCHEMA\> |
///
/// COLUMN_SCHEMA is a Struct with fields:
///
/// | Field Name | Field Type | Comments |
/// |--------------------------|-------------------------|----------|
/// | column_name | utf8 not null | |
/// | ordinal_position | int32 | (1) |
/// | remarks | utf8 | (2) |
/// | xdbc_data_type | int16 | (3) |
/// | xdbc_type_name | utf8 | (3) |
/// | xdbc_column_size | int32 | (3) |
/// | xdbc_decimal_digits | int16 | (3) |
/// | xdbc_num_prec_radix | int16 | (3) |
/// | xdbc_nullable | int16 | (3) |
/// | xdbc_column_def | utf8 | (3) |
/// | xdbc_sql_data_type | int16 | (3) |
/// | xdbc_datetime_sub | int16 | (3) |
/// | xdbc_char_octet_length | int32 | (3) |
/// | xdbc_is_nullable | utf8 | (3) |
/// | xdbc_scope_catalog | utf8 | (3) |
/// | xdbc_scope_schema | utf8 | (3) |
/// | xdbc_scope_table | utf8 | (3) |
/// | xdbc_is_autoincrement | bool | (3) |
/// | xdbc_is_generatedcolumn | bool | (3) |
///
/// 1. The column's ordinal position in the table (starting from 1).
/// 2. Database-specific description of the column.
/// 3. Optional value. Should be null if not supported by the driver.
/// `xdbc_` values are meant to provide JDBC/ODBC-compatible metadata
/// in an agnostic manner.
///
/// CONSTRAINT_SCHEMA is a Struct with fields:
///
/// | Field Name | Field Type | Comments |
/// |--------------------------|-------------------------|----------|
/// | constraint_name | utf8 | |
/// | constraint_type | utf8 not null | (1) |
/// | constraint_column_names | list\<utf8\> not null | (2) |
/// | constraint_column_usage | list\<USAGE_SCHEMA\> | (3) |
///
/// 1. One of `CHECK`, `FOREIGN KEY`, `PRIMARY KEY`, or `UNIQUE`.
/// 2. The columns on the current table that are constrained, in
/// order.
/// 3. For `FOREIGN KEY` only, the referenced table and columns.
///
/// USAGE_SCHEMA is a Struct with fields:
///
/// | Field Name | Field Type |
/// |--------------------------|-------------------------|
/// | fk_catalog | utf8 |
/// | fk_db_schema | utf8 |
/// | fk_table | utf8 not null |
/// | fk_column_name | utf8 not null |
///
fn get_objects<'a>(
&'_ self,
depth: options::ObjectDepth,
catalog: Option<&'_ str>,
db_schema: Option<&'_ str>,
table_name: Option<&'_ str>,
table_type: Option<Vec<&'_ str>>,
column_name: Option<&'_ str>,
) -> Result<impl RecordBatchReader + Send + 'a>;
/// Get the Arrow schema of a table.
///
/// # Arguments
///
/// - `catalog` - The catalog (or `None` if not applicable).
/// - `db_schema` - The database schema (or `None` if not applicable).
/// - `table_name` - The table name.
fn get_table_schema(
&self,
catalog: Option<&str>,
db_schema: Option<&str>,
table_name: &str,
) -> Result<Schema>;
/// Get a list of table types in the database.
///
/// # Result
///
/// The result is an Arrow dataset with the following schema:
///
/// Field Name | Field Type
/// ---------------|--------------
/// table_type | utf8 not null
fn get_table_types(&self) -> Result<impl RecordBatchReader + Send>;
/// Get the names of statistics specific to this driver.
///
/// # Result
///
/// The result is an Arrow dataset with the following schema:
///
/// Field Name | Field Type
/// ---------------|----------------
/// statistic_name | utf8 not null
/// statistic_key | int16 not null
///
/// # Since
/// ADBC API revision 1.1.0
fn get_statistic_names(&self) -> Result<impl RecordBatchReader + Send>;
/// Get statistics about the data distribution of table(s).
///
/// # Arguments
///
/// - `catalog` - The catalog (or `None` if not applicable). May be a search pattern.
/// - `db_schema` - The database schema (or `None` if not applicable). May be a search pattern
/// - `table_name` - The table name (or `None` if not applicable). May be a search pattern
/// - `approximate` - If false, request exact values of statistics, else
/// allow for best-effort, approximate, or cached values. The database may
/// return approximate values regardless, as indicated in the result.
/// Requesting exact values may be expensive or unsupported.
///
/// # Result
///
/// The result is an Arrow dataset with the following schema:
///
/// | Field Name | Field Type |
/// |--------------------------|----------------------------------|
/// | catalog_name | utf8 |
/// | catalog_db_schemas | list\<DB_SCHEMA_SCHEMA\> not null|
///
/// DB_SCHEMA_SCHEMA is a Struct with fields:
///
/// | Field Name | Field Type |
/// |--------------------------|-----------------------------------|
/// | db_schema_name | utf8 |
/// | db_schema_statistics | list\<STATISTICS_SCHEMA\> not null|
///
/// STATISTICS_SCHEMA is a Struct with fields:
///
/// | Field Name | Field Type | Comments |
/// |--------------------------|----------------------------------| -------- |
/// | table_name | utf8 not null | |
/// | column_name | utf8 | (1) |
/// | statistic_key | int16 not null | (2) |
/// | statistic_value | VALUE_SCHEMA not null | |
/// | statistic_is_approximate | bool not null | (3) |
///
/// 1. If null, then the statistic applies to the entire table.
/// 2. A dictionary-encoded statistic name (although we do not use the Arrow
/// dictionary type). Values in [0, 1024) are reserved for ADBC. Other
/// values are for implementation-specific statistics. For the definitions
/// of predefined statistic types, see [options::Statistics]. To get
/// driver-specific statistic names, use [Connection::get_statistic_names].
/// 3. If true, then the value is approximate or best-effort.
///
/// VALUE_SCHEMA is a dense union with members:
///
/// | Field Name | Field Type |
/// |--------------------------|----------------------------------|
/// | int64 | int64 |
/// | uint64 | uint64 |
/// | float64 | float64 |
/// | binary | binary |
///
/// # Since
///
/// ADBC API revision 1.1.0
fn get_statistics(
&self,
catalog: Option<&str>,
db_schema: Option<&str>,
table_name: Option<&str>,
approximate: bool,
) -> Result<impl RecordBatchReader + Send>;
/// Commit any pending transactions. Only used if autocommit is disabled.
///
/// Behavior is undefined if this is mixed with SQL transaction statements.
fn commit(&mut self) -> Result<()>;
/// Roll back any pending transactions. Only used if autocommit is disabled.
///
/// Behavior is undefined if this is mixed with SQL transaction statements.
fn rollback(&mut self) -> Result<()>;
/// Retrieve a given partition of data.
///
/// A partition can be retrieved from [Statement::execute_partitions].
///
/// # Arguments
///
/// - `partition` - The partition descriptor.
fn read_partition(&self, partition: impl AsRef<[u8]>) -> Result<impl RecordBatchReader + Send>;
}
/// A handle to an ADBC statement.
///
/// A statement is a container for all state needed to execute a database query,
/// such as the query itself, parameters for prepared statements, driver
/// parameters, etc.
///
/// Statements may represent queries or prepared statements.
///
/// Statements may be used multiple times and can be reconfigured
/// (e.g. they can be reused to execute multiple different queries).
/// However, executing a statement (and changing certain other state)
/// will invalidate result sets obtained prior to that execution.
///
/// Multiple statements may be created from a single connection.
/// However, the driver may block or error if they are used concurrently
/// (whether from a single thread or multiple threads).
pub trait Statement: Optionable<Option = OptionStatement> {
/// Bind Arrow data. This can be used for bulk inserts or prepared
/// statements.
fn bind(&mut self, batch: RecordBatch) -> Result<()>;
/// Bind Arrow data. This can be used for bulk inserts or prepared
/// statements.
// TODO(alexandreyc): should we use a generic here instead of a trait object?
// See: https://github.com/apache/arrow-adbc/pull/1725#discussion_r1567750972
fn bind_stream(&mut self, reader: Box<dyn RecordBatchReader + Send>) -> Result<()>;
/// Execute a statement and get the results.
///
/// This invalidates any prior result sets.
// TODO(alexandreyc): is the Send bound absolutely necessary? same question
// for all methods that return an impl RecordBatchReader
// See: https://github.com/apache/arrow-adbc/pull/1725#discussion_r1567748242
fn execute(&mut self) -> Result<impl RecordBatchReader + Send>;
/// Execute a statement that doesn’t have a result set and get the number
/// of affected rows.
///
/// This invalidates any prior result sets.
///
/// # Result
///
/// Will return the number of rows affected. If the affected row count is
/// unknown or unsupported by the database, will return `None`.
fn execute_update(&mut self) -> Result<Option<i64>>;
/// Get the schema of the result set of a query without executing it.
///
/// This invalidates any prior result sets.
///
/// Depending on the driver, this may require first executing
/// [Statement::prepare].
///
/// # Since
///
/// ADBC API revision 1.1.0
fn execute_schema(&mut self) -> Result<Schema>;
/// Execute a statement and get the results as a partitioned result set.
fn execute_partitions(&mut self) -> Result<PartitionedResult>;
/// Get the schema for bound parameters.
///
/// This retrieves an Arrow schema describing the number, names, and
/// types of the parameters in a parameterized statement. The fields
/// of the schema should be in order of the ordinal position of the
/// parameters; named parameters should appear only once.
///
/// If the parameter does not have a name, or the name cannot be
/// determined, the name of the corresponding field in the schema will
/// be an empty string. If the type cannot be determined, the type of
/// the corresponding field will be NA (NullType).
///
/// This should be called after [Statement::prepare].
fn get_parameter_schema(&self) -> Result<Schema>;
/// Turn this statement into a prepared statement to be executed multiple
/// times.
///
/// This invalidates any prior result sets.
fn prepare(&mut self) -> Result<()>;
/// Set the SQL query to execute.
///
/// The query can then be executed with [Statement::execute]. For queries
/// expected to be executed repeatedly, call [Statement::prepare] first.
fn set_sql_query(&mut self, query: impl AsRef<str>) -> Result<()>;
/// Set the Substrait plan to execute.
///
/// The query can then be executed with [Statement::execute]. For queries
/// expected to be executed repeatedly, call [Statement::prepare] first.
fn set_substrait_plan(&mut self, plan: impl AsRef<[u8]>) -> Result<()>;
/// Cancel execution of an in-progress query.
///
/// This can be called during [Statement::execute] (or similar), or while
/// consuming a result set returned from such.
///
/// # Since
///
/// ADBC API revision 1.1.0
fn cancel(&mut self) -> Result<()>;
}
/// Each data partition is described by an opaque byte array and can be
/// retrieved with [Connection::read_partition].
pub type Partitions = Vec<Vec<u8>>;
/// A partitioned result set as returned by [Statement::execute_partitions].
#[derive(Debug, PartialEq, Eq)]
pub struct PartitionedResult {
/// The result partitions.
pub partitions: Partitions,
/// The schema of the result set.
pub schema: Schema,
/// The number of rows affected if known, else -1.
pub rows_affected: i64,
}