-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathduckdb.rs
837 lines (703 loc) · 27.5 KB
/
duckdb.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
use crate::sql::sql_provider_datafusion;
use crate::util::constraints::get_primary_keys_from_constraints;
use crate::util::{
self,
column_reference::{self, ColumnReference},
constraints,
indexes::IndexType,
on_conflict::{self, OnConflict},
};
use crate::{
sql::db_connection_pool::{
self,
dbconnection::{
duckdbconn::{
flatten_table_function_name, is_table_function, DuckDBParameter, DuckDbConnection,
},
get_schema, DbConnection,
},
duckdbpool::DuckDbConnectionPool,
DbConnectionPool, DbInstanceKey, Mode,
},
UnsupportedTypeAction,
};
use arrow::{array::RecordBatch, datatypes::SchemaRef};
use async_trait::async_trait;
use datafusion::sql::unparser::dialect::{Dialect, DuckDBDialect};
use datafusion::{
catalog::{Session, TableProviderFactory},
common::Constraints,
datasource::TableProvider,
error::{DataFusionError, Result as DataFusionResult},
logical_expr::CreateExternalTable,
sql::TableReference,
};
use duckdb::{AccessMode, DuckdbConnectionManager, Transaction};
use itertools::Itertools;
use snafu::prelude::*;
use std::collections::HashSet;
use std::{cmp, collections::HashMap, sync::Arc};
use tokio::sync::Mutex;
use self::{creator::TableCreator, sql_table::DuckDBTable, write::DuckDBTableWriter};
#[cfg(feature = "duckdb-federation")]
mod federation;
mod creator;
mod sql_table;
pub mod write;
#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("DbConnectionError: {source}"))]
DbConnectionError {
source: db_connection_pool::dbconnection::GenericError,
},
#[snafu(display("DbConnectionPoolError: {source}"))]
DbConnectionPoolError { source: db_connection_pool::Error },
#[snafu(display("DuckDBDataFusionError: {source}"))]
DuckDBDataFusion {
source: sql_provider_datafusion::Error,
},
#[snafu(display("Unable to downcast DbConnection to DuckDbConnection"))]
UnableToDowncastDbConnection {},
#[snafu(display("Unable to drop duckdb table: {source}"))]
UnableToDropDuckDBTable { source: duckdb::Error },
#[snafu(display("Unable to create duckdb table: {source}"))]
UnableToCreateDuckDBTable { source: duckdb::Error },
#[snafu(display("Unable to create index on duckdb table: {source}"))]
UnableToCreateIndexOnDuckDBTable { source: duckdb::Error },
#[snafu(display("Unable to retrieve existing primary keys from DuckDB table: {source}"))]
UnableToGetPrimaryKeysOnDuckDBTable { source: duckdb::Error },
#[snafu(display("Unable to drop index on duckdb table: {source}"))]
UnableToDropIndexOnDuckDBTable { source: duckdb::Error },
#[snafu(display("Unable to rename duckdb table: {source}"))]
UnableToRenameDuckDBTable { source: duckdb::Error },
#[snafu(display("Unable to insert into duckdb table: {source}"))]
UnableToInsertToDuckDBTable { source: duckdb::Error },
#[snafu(display("Unable to get appender to duckdb table: {source}"))]
UnableToGetAppenderToDuckDBTable { source: duckdb::Error },
#[snafu(display("Unable to delete data from the duckdb table: {source}"))]
UnableToDeleteDuckdbData { source: duckdb::Error },
#[snafu(display("Unable to query data from the duckdb table: {source}"))]
UnableToQueryData { source: duckdb::Error },
#[snafu(display("Unable to commit transaction: {source}"))]
UnableToCommitTransaction { source: duckdb::Error },
#[snafu(display("Unable to begin duckdb transaction: {source}"))]
UnableToBeginTransaction { source: duckdb::Error },
#[snafu(display("Unable to rollback transaction: {source}"))]
UnableToRollbackTransaction { source: duckdb::Error },
#[snafu(display("Unable to delete all data from the DuckDB table: {source}"))]
UnableToDeleteAllTableData { source: duckdb::Error },
#[snafu(display("Unable to insert data into the DuckDB table: {source}"))]
UnableToInsertIntoTableAsync { source: duckdb::Error },
#[snafu(display("The table '{table_name}' doesn't exist in the DuckDB server"))]
TableDoesntExist { table_name: String },
#[snafu(display("Constraint Violation: {source}"))]
ConstraintViolation { source: constraints::Error },
#[snafu(display("Error parsing column reference: {source}"))]
UnableToParseColumnReference { source: column_reference::Error },
#[snafu(display("Error parsing on_conflict: {source}"))]
UnableToParseOnConflict { source: on_conflict::Error },
#[snafu(display(
"Failed to create '{table_name}': creating a table with a schema is not supported"
))]
TableWithSchemaCreationNotSupported { table_name: String },
#[snafu(display("Failed to parse memory_limit value '{value}': {source}\nProvide a valid value, e.g. '2GB', '512MiB' (expected: KB, MB, GB, TB for 1000^i units or KiB, MiB, GiB, TiB for 1024^i units)"))]
UnableToParseMemoryLimit {
value: String,
source: byte_unit::ParseError,
},
}
type Result<T, E = Error> = std::result::Result<T, E>;
pub struct DuckDBTableProviderFactory {
access_mode: AccessMode,
instances: Arc<Mutex<HashMap<DbInstanceKey, DuckDbConnectionPool>>>,
unsupported_type_action: UnsupportedTypeAction,
dialect: Arc<dyn Dialect>,
}
// Dialect trait does not implement Debug so we implement Debug manually
impl std::fmt::Debug for DuckDBTableProviderFactory {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DuckDBTableProviderFactory")
.field("access_mode", &self.access_mode)
.field("instances", &self.instances)
.field("unsupported_type_action", &self.unsupported_type_action)
.finish()
}
}
const DUCKDB_DB_PATH_PARAM: &str = "open";
const DUCKDB_DB_BASE_FOLDER_PARAM: &str = "data_directory";
const DUCKDB_ATTACH_DATABASES_PARAM: &str = "attach_databases";
const DUCKDB_SETTING_MEMORY_LIMIT: &str = "memory_limit";
impl DuckDBTableProviderFactory {
#[must_use]
pub fn new(access_mode: AccessMode) -> Self {
Self {
access_mode,
instances: Arc::new(Mutex::new(HashMap::new())),
unsupported_type_action: UnsupportedTypeAction::Error,
dialect: Arc::new(DuckDBDialect::new()),
}
}
#[must_use]
pub fn with_unsupported_type_action(
mut self,
unsupported_type_action: UnsupportedTypeAction,
) -> Self {
self.unsupported_type_action = unsupported_type_action;
self
}
#[must_use]
pub fn with_dialect(mut self, dialect: Arc<dyn Dialect + Send + Sync>) -> Self {
self.dialect = dialect;
self
}
#[must_use]
pub fn attach_databases(&self, options: &HashMap<String, String>) -> Vec<Arc<str>> {
options
.get(DUCKDB_ATTACH_DATABASES_PARAM)
.map(|attach_databases| {
attach_databases
.split(';')
.map(Arc::from)
.collect::<Vec<Arc<str>>>()
})
.unwrap_or_default()
}
/// Get the path to the DuckDB file database.
///
/// ## Errors
///
/// - If the path includes absolute sequences to escape the current directory, like `./`, `../`, or `/`.
pub fn duckdb_file_path(
&self,
name: &str,
options: &mut HashMap<String, String>,
) -> Result<String, Error> {
let options = util::remove_prefix_from_hashmap_keys(options.clone(), "duckdb_");
let db_base_folder = options
.get(DUCKDB_DB_BASE_FOLDER_PARAM)
.cloned()
.unwrap_or(".".to_string()); // default to the current directory
let default_filepath = &format!("{db_base_folder}/{name}.db");
let filepath = options
.get(DUCKDB_DB_PATH_PARAM)
.unwrap_or(default_filepath);
Ok(filepath.to_string())
}
pub async fn get_or_init_memory_instance(&self) -> Result<DuckDbConnectionPool> {
let key = DbInstanceKey::memory();
let mut instances = self.instances.lock().await;
if let Some(instance) = instances.get(&key) {
return Ok(instance.clone());
}
let pool = DuckDbConnectionPool::new_memory()
.context(DbConnectionPoolSnafu)?
.with_unsupported_type_action(self.unsupported_type_action);
instances.insert(key, pool.clone());
Ok(pool)
}
pub async fn get_or_init_file_instance(
&self,
db_path: impl Into<Arc<str>>,
) -> Result<DuckDbConnectionPool> {
let db_path = db_path.into();
let key = DbInstanceKey::file(Arc::clone(&db_path));
let mut instances = self.instances.lock().await;
if let Some(instance) = instances.get(&key) {
return Ok(instance.clone());
}
let pool = DuckDbConnectionPool::new_file(&db_path, &self.access_mode)
.context(DbConnectionPoolSnafu)?
.with_unsupported_type_action(self.unsupported_type_action);
instances.insert(key, pool.clone());
Ok(pool)
}
}
type DynDuckDbConnectionPool = dyn DbConnectionPool<r2d2::PooledConnection<DuckdbConnectionManager>, DuckDBParameter>
+ Send
+ Sync;
#[async_trait]
impl TableProviderFactory for DuckDBTableProviderFactory {
async fn create(
&self,
_state: &dyn Session,
cmd: &CreateExternalTable,
) -> DataFusionResult<Arc<dyn TableProvider>> {
if cmd.name.schema().is_some() {
TableWithSchemaCreationNotSupportedSnafu {
table_name: cmd.name.to_string(),
}
.fail()
.map_err(to_datafusion_error)?;
}
let name = cmd.name.to_string();
let mut options = cmd.options.clone();
let mode = remove_option(&mut options, "mode").unwrap_or_default();
let mode: Mode = mode.as_str().into();
let indexes_option_str = remove_option(&mut options, "indexes");
let unparsed_indexes: HashMap<String, IndexType> = match indexes_option_str {
Some(indexes_str) => util::hashmap_from_option_string(&indexes_str),
None => HashMap::new(),
};
let unparsed_indexes = unparsed_indexes
.into_iter()
.map(|(key, value)| {
let columns = ColumnReference::try_from(key.as_str())
.context(UnableToParseColumnReferenceSnafu)
.map_err(to_datafusion_error);
(columns, value)
})
.collect::<Vec<(Result<ColumnReference, DataFusionError>, IndexType)>>();
let mut indexes: Vec<(ColumnReference, IndexType)> = Vec::new();
for (columns, index_type) in unparsed_indexes {
let columns = columns?;
indexes.push((columns, index_type));
}
let mut on_conflict: Option<OnConflict> = None;
if let Some(on_conflict_str) = remove_option(&mut options, "on_conflict") {
on_conflict = Some(
OnConflict::try_from(on_conflict_str.as_str())
.context(UnableToParseOnConflictSnafu)
.map_err(to_datafusion_error)?,
);
}
let pool: DuckDbConnectionPool = match &mode {
Mode::File => {
// open duckdb at given path or create a new one
let db_path = self
.duckdb_file_path(&name, &mut options)
.map_err(to_datafusion_error)?;
self.get_or_init_file_instance(db_path)
.await
.map_err(to_datafusion_error)?
}
Mode::Memory => self
.get_or_init_memory_instance()
.await
.map_err(to_datafusion_error)?,
};
let read_pool = match &mode {
Mode::File => {
let read_pool = pool.clone();
read_pool.set_attached_databases(&self.attach_databases(&options))
}
Mode::Memory => pool.clone(),
};
let schema: SchemaRef = Arc::new(cmd.schema.as_ref().into());
let duckdb = TableCreator::new(name.clone(), Arc::clone(&schema), Arc::new(pool))
.constraints(cmd.constraints.clone())
.indexes(indexes.clone())
.create()
.map_err(to_datafusion_error)?;
// If the table is already created, we don't create it again and don't apply primary keys and remove previosly created indexes (if any).
// Thus we verify that primary keys and indexes for the table created match the configuration.
let mut table_schema_matches = true;
table_schema_matches &= duckdb
.verify_primary_keys_match()
.await
.map_err(to_datafusion_error)?;
table_schema_matches &= duckdb
.verify_indexes_match(&indexes)
.await
.map_err(to_datafusion_error)?;
if !table_schema_matches {
tracing::warn!(
"Schema mismatch detected for table '{table_name}' in database '{db_path}'.\n\
The local table definition does not match the expected schema.\n\
To resolve this issue, drop the existing table. A new table with the correct schema will be created automatically on the next access.",
db_path = duckdb.pool.db_path(),
table_name = duckdb.table_name
);
}
let dyn_pool: Arc<DynDuckDbConnectionPool> = Arc::new(read_pool);
if let Some(memory_limit) = options.get("memory_limit") {
apply_memory_limit(&dyn_pool, memory_limit).await?;
}
let read_provider = Arc::new(DuckDBTable::new_with_schema(
&dyn_pool,
Arc::clone(&schema),
TableReference::bare(name.clone()),
None,
Some(self.dialect.clone()),
));
#[cfg(feature = "duckdb-federation")]
let read_provider: Arc<dyn TableProvider> =
Arc::new(read_provider.create_federated_table_provider()?);
Ok(DuckDBTableWriter::create(
read_provider,
duckdb,
on_conflict,
))
}
}
fn to_datafusion_error(error: Error) -> DataFusionError {
DataFusionError::External(Box::new(error))
}
pub struct DuckDB {
table_name: String,
pool: Arc<DuckDbConnectionPool>,
schema: SchemaRef,
constraints: Constraints,
table_creator: Option<TableCreator>,
}
impl std::fmt::Debug for DuckDB {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DuckDB")
.field("table_name", &self.table_name)
.field("schema", &self.schema)
.field("constraints", &self.constraints)
.finish()
}
}
impl DuckDB {
#[must_use]
pub fn existing_table(
table_name: String,
pool: Arc<DuckDbConnectionPool>,
schema: SchemaRef,
constraints: Constraints,
) -> Self {
Self {
table_name,
pool,
schema,
constraints,
table_creator: None,
}
}
#[must_use]
pub fn table_name(&self) -> &str {
&self.table_name
}
#[must_use]
pub fn constraints(&self) -> &Constraints {
&self.constraints
}
pub fn connect_sync(
&self,
) -> Result<
Box<dyn DbConnection<r2d2::PooledConnection<DuckdbConnectionManager>, DuckDBParameter>>,
> {
Arc::clone(&self.pool)
.connect_sync()
.context(DbConnectionSnafu)
}
pub fn connect_sync_direct(self: Arc<Self>) -> Result<DuckDbConnection> {
Arc::clone(&self.pool)
.connect_sync_direct()
.context(DbConnectionSnafu)
}
pub fn duckdb_conn(
db_connection: &mut Box<
dyn DbConnection<r2d2::PooledConnection<DuckdbConnectionManager>, DuckDBParameter>,
>,
) -> Result<&mut DuckDbConnection> {
db_connection
.as_any_mut()
.downcast_mut::<DuckDbConnection>()
.context(UnableToDowncastDbConnectionSnafu)
}
const MAX_BATCH_SIZE: usize = 2048;
fn split_batch(batch: &RecordBatch) -> Vec<RecordBatch> {
let mut result = vec![];
(0..=batch.num_rows())
.step_by(Self::MAX_BATCH_SIZE)
.for_each(|offset| {
let length = cmp::min(Self::MAX_BATCH_SIZE, batch.num_rows() - offset);
result.push(batch.slice(offset, length));
});
result
}
fn insert_table_into(
&self,
tx: &Transaction<'_>,
table_to_insert_into: &DuckDB,
on_conflict: Option<&OnConflict>,
) -> Result<()> {
let mut insert_sql = format!(
r#"INSERT INTO "{}" SELECT * FROM "{}""#,
table_to_insert_into.table_name, self.table_name
);
if let Some(on_conflict) = on_conflict {
let on_conflict_sql = on_conflict.build_on_conflict_statement(&self.schema);
insert_sql.push_str(&format!(" {on_conflict_sql}"));
}
tracing::debug!("{insert_sql}");
tx.execute(&insert_sql, [])
.context(UnableToInsertToDuckDBTableSnafu)?;
Ok(())
}
fn insert_batch_no_constraints(
&self,
transaction: &Transaction<'_>,
batch: &RecordBatch,
) -> Result<()> {
let mut appender = transaction
.appender(&self.table_name)
.context(UnableToGetAppenderToDuckDBTableSnafu)?;
for batch in Self::split_batch(batch) {
appender
.append_record_batch(batch.clone())
.context(UnableToInsertToDuckDBTableSnafu)?;
}
appender.flush().context(UnableToInsertToDuckDBTableSnafu)?;
Ok(())
}
fn delete_all_table_data(&self, transaction: &Transaction<'_>) -> Result<()> {
transaction
.execute(format!(r#"DELETE FROM "{}""#, self.table_name).as_str(), [])
.context(UnableToDeleteAllTableDataSnafu)?;
Ok(())
}
pub async fn verify_primary_keys_match(&self) -> Result<bool> {
let expected_pk_keys_str_map: HashSet<String> =
get_primary_keys_from_constraints(&self.constraints, &self.schema)
.into_iter()
.collect();
let mut db_conn = self.connect_sync()?;
let actual_pk_keys_str_map = TableCreator::get_existing_primary_keys(
DuckDB::duckdb_conn(&mut db_conn)?,
&self.table_name,
)
.await?;
tracing::debug!(
"Expected primary keys: {:?}\nActual primary keys: {:?}",
expected_pk_keys_str_map,
actual_pk_keys_str_map
);
let missing_in_actual = expected_pk_keys_str_map
.difference(&actual_pk_keys_str_map)
.collect::<Vec<_>>();
let extra_in_actual = actual_pk_keys_str_map
.difference(&expected_pk_keys_str_map)
.collect::<Vec<_>>();
if !missing_in_actual.is_empty() {
tracing::warn!(
"Missing primary key(s) detected for the table '{name}': {:?}.",
missing_in_actual.iter().join(", "),
name = self.table_name
);
}
if !extra_in_actual.is_empty() {
tracing::warn!(
"The table '{name}' has unexpected primary key(s) not defined in the configuration: {:?}.",
extra_in_actual.iter().join(", "),
name = self.table_name
);
}
Ok(missing_in_actual.is_empty() && extra_in_actual.is_empty())
}
async fn verify_indexes_match(&self, indexes: &[(ColumnReference, IndexType)]) -> Result<bool> {
let expected_indexes_str_map: HashSet<String> = indexes
.iter()
.map(|index| TableCreator::get_index_name(&self.table_name, index))
.collect();
let mut db_conn = self.connect_sync()?;
let actual_indexes_str_map = TableCreator::get_existing_indexes(
DuckDB::duckdb_conn(&mut db_conn)?,
&self.table_name,
)
.await?;
tracing::debug!(
"Expected indexes: {:?}\nActual indexes: {:?}",
expected_indexes_str_map,
actual_indexes_str_map
);
let missing_in_actual = expected_indexes_str_map
.difference(&actual_indexes_str_map)
.collect::<Vec<_>>();
let extra_in_actual = actual_indexes_str_map
.difference(&expected_indexes_str_map)
.collect::<Vec<_>>();
if !missing_in_actual.is_empty() {
tracing::warn!(
"Missing index(es) detected for the table '{name}': {:?}.",
missing_in_actual.iter().join(", "),
name = self.table_name
);
}
if !extra_in_actual.is_empty() {
tracing::warn!(
"Unexpected index(es) detected in table '{name}': {}.\n\
These indexes are not defined in the configuration.",
extra_in_actual.iter().join(", "),
name = self.table_name
);
}
Ok(missing_in_actual.is_empty() && extra_in_actual.is_empty())
}
}
fn remove_option(options: &mut HashMap<String, String>, key: &str) -> Option<String> {
options
.remove(key)
.or_else(|| options.remove(&format!("duckdb.{key}")))
}
pub struct DuckDBTableFactory {
pool: Arc<DuckDbConnectionPool>,
dialect: Arc<dyn Dialect>,
}
impl DuckDBTableFactory {
#[must_use]
pub fn new(pool: Arc<DuckDbConnectionPool>) -> Self {
Self {
pool,
dialect: Arc::new(DuckDBDialect::new()),
}
}
#[must_use]
pub fn with_dialect(mut self, dialect: Arc<dyn Dialect + Send + Sync>) -> Self {
self.dialect = dialect;
self
}
pub async fn table_provider(
&self,
table_reference: TableReference,
) -> Result<Arc<dyn TableProvider + 'static>, Box<dyn std::error::Error + Send + Sync>> {
let pool = Arc::clone(&self.pool);
let conn = Arc::clone(&pool).connect().await?;
let dyn_pool: Arc<DynDuckDbConnectionPool> = pool;
let schema = get_schema(conn, &table_reference).await?;
let (tbl_ref, cte) = if is_table_function(&table_reference) {
let tbl_ref_view = create_table_function_view_name(&table_reference);
(
tbl_ref_view.clone(),
Some(HashMap::from_iter(vec![(
tbl_ref_view.to_string(),
table_reference.table().to_string(),
)])),
)
} else {
(table_reference.clone(), None)
};
let table_provider = Arc::new(DuckDBTable::new_with_schema(
&dyn_pool,
schema,
tbl_ref,
cte,
Some(self.dialect.clone()),
));
#[cfg(feature = "duckdb-federation")]
let table_provider: Arc<dyn TableProvider> =
Arc::new(table_provider.create_federated_table_provider()?);
Ok(table_provider)
}
pub async fn read_write_table_provider(
&self,
table_reference: TableReference,
) -> Result<Arc<dyn TableProvider + 'static>, Box<dyn std::error::Error + Send + Sync>> {
let read_provider = Self::table_provider(self, table_reference.clone()).await?;
let schema = read_provider.schema();
let table_name = table_reference.to_string();
let duckdb = DuckDB::existing_table(
table_name,
Arc::clone(&self.pool),
schema,
Constraints::empty(),
);
Ok(DuckDBTableWriter::create(read_provider, duckdb, None))
}
}
/// For a [`TableReference`] that is a table function, create a name for a view on the original [`TableReference`]
///
/// ### Example
///
/// ```rust,ignore
/// use datafusion_table_providers::duckdb::create_table_function_view_name;
/// use datafusion::common::TableReference;
///
/// let table_reference = TableReference::from("catalog.schema.read_parquet('cleaned_sales_data.parquet')");
/// let view_name = create_table_function_view_name(&table_reference);
/// assert_eq!(view_name.to_string(), "catalog.schema.read_parquet_cleaned_sales_dataparquet__view");
/// ```
fn create_table_function_view_name(table_reference: &TableReference) -> TableReference {
let tbl_ref_view = [
table_reference.catalog(),
table_reference.schema(),
Some(&flatten_table_function_name(table_reference)),
]
.iter()
.flatten()
.join(".");
TableReference::from(&tbl_ref_view)
}
async fn apply_memory_limit(
pool: &Arc<DynDuckDbConnectionPool>,
memory_limit: &str,
) -> DataFusionResult<()> {
tracing::debug!("Setting DuckDB memory limit to {memory_limit}");
if let Err(err) = byte_unit::Byte::parse_str(memory_limit, true) {
return Err(to_datafusion_error(Error::UnableToParseMemoryLimit {
value: memory_limit.to_string(),
source: err,
}));
}
let db_conn = pool.connect().await?;
let Some(conn) = db_conn.as_sync() else {
// should never happen
return Err(to_datafusion_error(Error::DbConnectionError {
source: "Failed to get sync DuckDbConnection using DbConnection".into(),
}));
};
conn.execute(
&format!("SET {DUCKDB_SETTING_MEMORY_LIMIT} = '{memory_limit}'"),
&[],
)?;
Ok(())
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use arrow::datatypes::{DataType, Field, Schema};
use datafusion::common::{Constraints, ToDFSchema};
use datafusion::logical_expr::CreateExternalTable;
use datafusion::prelude::SessionContext;
use datafusion::sql::TableReference;
use std::collections::HashMap;
use std::sync::Arc;
#[tokio::test]
async fn test_create_with_memory_limit() {
let table_name = TableReference::bare("test_table");
let schema = Schema::new(vec![Field::new("dummy", DataType::Int32, false)]);
let mut options = HashMap::new();
options.insert("mode".to_string(), "memory".to_string());
options.insert("memory_limit".to_string(), "123MiB".to_string());
let factory = DuckDBTableProviderFactory::new(duckdb::AccessMode::ReadWrite);
let ctx = SessionContext::new();
let cmd = CreateExternalTable {
schema: Arc::new(schema.to_dfschema().expect("to df schema")),
name: table_name.into(),
location: "".to_string(),
file_type: "".to_string(),
table_partition_cols: vec![],
if_not_exists: false,
definition: None,
order_exprs: vec![],
unbounded: false,
options,
constraints: Constraints::empty(),
column_defaults: HashMap::new(),
temporary: false,
};
let table_provider = factory
.create(&ctx.state(), &cmd)
.await
.expect("table provider created");
let writer = table_provider
.as_any()
.downcast_ref::<DuckDBTableWriter>()
.expect("cast to DuckDBTableWriter");
let mut conn_box = writer.duckdb().connect_sync().expect("to get connection");
let conn = DuckDB::duckdb_conn(&mut conn_box).expect("to get DuckDB connection");
let mut stmt = conn
.conn
.prepare("SELECT value FROM duckdb_settings() WHERE name = 'memory_limit'")
.expect("to prepare statement");
let memory_limit = stmt
.query_row([], |row| row.get::<usize, String>(0))
.expect("to query memory limit");
println!("Memory limit: {memory_limit}");
assert_eq!(
memory_limit, "123.0 MiB",
"Memory limit must be set to 123.0 MiB"
);
}
}