tinyetl 0.11.0

Fast, zero-config ETL in a single binary for transforming data between formats and databases
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
use async_trait::async_trait;
use base64::{engine::general_purpose, Engine as _};
use chrono::{DateTime, Utc};
use duckdb::{types::ValueRef, Connection};
use rust_decimal::Decimal;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};

use crate::{
    connectors::{Source, Target},
    schema::{Column as SchemaColumn, DataType, Row, Schema, Value},
    Result, TinyEtlError,
};

/// Efficient batch insert using DuckDB's Appender API
fn insert_with_appender(
    conn: &Connection,
    table_name: &str,
    rows: &[Row],
    schema: &Schema,
) -> Result<usize> {
    let mut appender = conn
        .appender(table_name)
        .map_err(|e| TinyEtlError::DataTransfer(format!("Failed to create appender: {}", e)))?;

    // Insert each row using the appender
    for row in rows {
        // Collect values in schema order as owned values
        let mut row_values: Vec<duckdb::types::Value> = Vec::new();

        for col in &schema.columns {
            let value = row.get(&col.name).unwrap_or(&Value::Null);

            let duckdb_value = match value {
                Value::String(s) => duckdb::types::Value::Text(s.clone()),
                Value::Integer(i) => duckdb::types::Value::BigInt(*i),
                Value::Decimal(d) => {
                    // Convert decimal to f64 for DuckDB
                    let f: f64 = (*d).try_into().unwrap_or(0.0);
                    duckdb::types::Value::Double(f)
                }
                Value::Boolean(b) => duckdb::types::Value::Boolean(*b),
                Value::Date(dt) => {
                    // Convert datetime to string for DuckDB
                    let timestamp_str = dt.to_rfc3339();
                    duckdb::types::Value::Text(timestamp_str)
                }
                Value::Json(j) => {
                    // DuckDB can store JSON as text or use JSON type
                    let json_str = serde_json::to_string(j).unwrap_or_else(|_| "{}".to_string());
                    duckdb::types::Value::Text(json_str)
                }
                Value::Null => duckdb::types::Value::Null,
            };

            row_values.push(duckdb_value);
        }

        // Convert to refs for appender
        let params_refs: Vec<&dyn duckdb::types::ToSql> = row_values
            .iter()
            .map(|v| v as &dyn duckdb::types::ToSql)
            .collect();

        appender
            .append_row(params_refs.as_slice())
            .map_err(|e| TinyEtlError::DataTransfer(format!("Failed to append row: {}", e)))?;
    }

    // Flush the appender to commit the data
    appender
        .flush()
        .map_err(|e| TinyEtlError::DataTransfer(format!("Failed to flush appender: {}", e)))?;

    Ok(rows.len())
}

/// DuckDB source connector for reading data from DuckDB databases
pub struct DuckdbSource {
    connection_string: String,
    connection: Option<Arc<Mutex<Connection>>>,
    table_name: String,
    query: Option<String>,
    current_offset: usize,
    total_rows: Option<usize>,
}

impl DuckdbSource {
    pub fn new(connection_string: &str) -> Result<Self> {
        // Parse connection string - could be "file.duckdb" or "duckdb:file.duckdb#table" or "file.duckdb#table"
        let (db_path, table) = if connection_string.contains('#') {
            let parts: Vec<&str> = connection_string.split('#').collect();
            if parts.len() != 2 {
                return Err(TinyEtlError::Configuration(
                    "DuckDB connection string format: file.duckdb#table".to_string(),
                ));
            }
            (parts[0].trim_start_matches("duckdb:"), parts[1])
        } else {
            return Err(TinyEtlError::Configuration(
                "DuckDB source requires table specification: file.duckdb#table".to_string(),
            ));
        };

        Ok(Self {
            connection_string: db_path.to_string(),
            connection: None,
            table_name: table.to_string(),
            query: None,
            current_offset: 0,
            total_rows: None,
        })
    }
}

#[async_trait]
impl Source for DuckdbSource {
    async fn connect(&mut self) -> Result<()> {
        // DuckDB connection is synchronous, but we wrap it for async compatibility
        let conn = Connection::open(&self.connection_string)
            .map_err(|e| TinyEtlError::Connection(format!(
                "Failed to connect to DuckDB database '{}': {}. Make sure the file exists and is readable.",
                self.connection_string,
                e
            )))?;

        self.connection = Some(Arc::new(Mutex::new(conn)));
        Ok(())
    }

    async fn infer_schema(&mut self, _sample_size: usize) -> Result<Schema> {
        if self.connection.is_none() {
            self.connect().await?;
        }

        let conn = self.connection.as_ref().unwrap();
        let conn = conn.lock().unwrap();

        // Get table schema using PRAGMA or DESCRIBE
        let query = format!("DESCRIBE \"{}\"", self.table_name);
        let mut stmt = conn.prepare(&query).map_err(|e| {
            TinyEtlError::DataTransfer(format!(
                "Failed to describe table '{}': {}",
                self.table_name, e
            ))
        })?;

        let rows = stmt
            .query_map([], |row| {
                Ok((
                    row.get::<_, String>(0)?, // column_name
                    row.get::<_, String>(1)?, // column_type
                    row.get::<_, String>(2)?, // null
                ))
            })
            .map_err(|e| {
                TinyEtlError::DataTransfer(format!("Failed to query table schema: {}", e))
            })?;

        let mut columns = Vec::new();
        for row_result in rows {
            let (name, duckdb_type, null_str) = row_result.map_err(|e| {
                TinyEtlError::DataTransfer(format!("Failed to read schema row: {}", e))
            })?;

            let data_type = match duckdb_type.to_uppercase().as_str() {
                t if t.contains("INT") || t.contains("INTEGER") => DataType::Integer,
                t if t.contains("DOUBLE")
                    || t.contains("FLOAT")
                    || t.contains("REAL")
                    || t.contains("DECIMAL")
                    || t.contains("NUMERIC") =>
                {
                    DataType::Decimal
                }
                t if t.contains("VARCHAR") || t.contains("TEXT") || t.contains("STRING") => {
                    DataType::String
                }
                t if t.contains("BOOL") => DataType::Boolean,
                t if t.contains("DATE") && !t.contains("TIME") => DataType::Date,
                t if t.contains("TIMESTAMP") || t.contains("DATETIME") => DataType::DateTime,
                _ => DataType::String,
            };

            let nullable = null_str.to_uppercase() == "YES";

            columns.push(SchemaColumn {
                name,
                data_type,
                nullable,
            });
        }

        // Get estimated row count
        let count_query = format!("SELECT COUNT(*) FROM \"{}\"", self.table_name);
        let count: i64 = conn
            .query_row(&count_query, [], |row| row.get(0))
            .map_err(|e| TinyEtlError::DataTransfer(format!("Failed to count rows: {}", e)))?;

        self.total_rows = Some(count as usize);

        Ok(Schema {
            columns,
            estimated_rows: Some(count as usize),
            primary_key_candidate: None,
        })
    }

    async fn read_batch(&mut self, batch_size: usize) -> Result<Vec<Row>> {
        if self.connection.is_none() {
            self.connect().await?;
        }

        let conn = self.connection.as_ref().unwrap();
        let conn = conn.lock().unwrap();

        // Use LIMIT and OFFSET for proper pagination
        let query = format!(
            "SELECT * FROM \"{}\" LIMIT {} OFFSET {}",
            self.table_name, batch_size, self.current_offset
        );

        let mut stmt = conn
            .prepare(&query)
            .map_err(|e| TinyEtlError::DataTransfer(format!("Failed to prepare query: {}", e)))?;

        // Execute the query first to get the rows iterator
        let mut rows_iter = stmt
            .query([])
            .map_err(|e| TinyEtlError::DataTransfer(format!("Failed to execute query: {}", e)))?;

        let mut result_rows = Vec::new();

        // Process each row
        while let Some(row) = rows_iter
            .next()
            .map_err(|e| TinyEtlError::DataTransfer(format!("Failed to fetch row: {}", e)))?
        {
            let mut data_row = Row::new();

            // Use row.as_ref() to get column count from the row itself
            let column_count = row.as_ref().column_count();

            for i in 0..column_count {
                // Get column name from the row
                let column_name = row
                    .as_ref()
                    .column_name(i)
                    .map(|s| s.to_string())
                    .unwrap_or_else(|_| format!("column_{}", i));

                let value = match row.get_ref(i) {
                    Ok(value_ref) => match value_ref {
                        ValueRef::Null => Value::Null,
                        ValueRef::Boolean(b) => Value::Boolean(b),
                        ValueRef::TinyInt(n) => Value::Integer(n as i64),
                        ValueRef::SmallInt(n) => Value::Integer(n as i64),
                        ValueRef::Int(n) => Value::Integer(n as i64),
                        ValueRef::BigInt(n) => Value::Integer(n),
                        ValueRef::HugeInt(n) => Value::Integer(n as i64),
                        ValueRef::UTinyInt(n) => Value::Integer(n as i64),
                        ValueRef::USmallInt(n) => Value::Integer(n as i64),
                        ValueRef::UInt(n) => Value::Integer(n as i64),
                        ValueRef::UBigInt(n) => Value::Integer(n as i64),
                        ValueRef::Float(f) => match Decimal::try_from(f) {
                            Ok(d) => Value::Decimal(d),
                            Err(_) => Value::String(f.to_string()),
                        },
                        ValueRef::Double(f) => match Decimal::try_from(f) {
                            Ok(d) => Value::Decimal(d),
                            Err(_) => Value::String(f.to_string()),
                        },
                        ValueRef::Decimal(d) => {
                            // Convert DuckDB decimal to rust_decimal
                            Value::String(d.to_string())
                        }
                        ValueRef::Timestamp(_unit, value) => {
                            // DuckDB timestamp is stored as microseconds since epoch
                            // Convert to a timestamp string in ISO 8601 format
                            let micros = value;
                            let seconds = micros / 1_000_000;
                            let nanos = ((micros % 1_000_000) * 1000) as u32;

                            match DateTime::<Utc>::from_timestamp(seconds, nanos) {
                                Some(dt) => Value::String(dt.to_rfc3339()),
                                None => Value::Null,
                            }
                        }
                        ValueRef::Text(bytes) => match std::str::from_utf8(bytes) {
                            Ok(s) => Value::String(s.to_string()),
                            Err(_) => Value::Null,
                        },
                        ValueRef::Blob(bytes) => {
                            // Convert blob to base64 string
                            Value::String(general_purpose::STANDARD.encode(bytes))
                        }
                        ValueRef::Date32(_) => {
                            // Get as string
                            match row.get::<_, String>(i) {
                                Ok(s) => Value::String(s),
                                Err(_) => Value::Null,
                            }
                        }
                        _ => {
                            // Fallback: try to get as string
                            match row.get::<_, String>(i) {
                                Ok(s) => Value::String(s),
                                Err(_) => Value::Null,
                            }
                        }
                    },
                    Err(_) => Value::Null,
                };

                data_row.insert(column_name, value);
            }

            result_rows.push(data_row);
        }

        // Update offset for next batch
        self.current_offset += result_rows.len();

        Ok(result_rows)
    }
    async fn estimated_row_count(&self) -> Result<Option<usize>> {
        if let Some(conn) = &self.connection {
            let conn = conn.lock().unwrap();
            let count_query = format!("SELECT COUNT(*) FROM \"{}\"", self.table_name);
            let count: i64 = conn
                .query_row(&count_query, [], |row| row.get(0))
                .map_err(|e| TinyEtlError::DataTransfer(format!("Failed to count rows: {}", e)))?;
            Ok(Some(count as usize))
        } else {
            Ok(None)
        }
    }

    async fn reset(&mut self) -> Result<()> {
        // Reset pagination state
        self.current_offset = 0;
        self.query = None;
        Ok(())
    }

    fn has_more(&self) -> bool {
        // Check if there are more rows to read based on pagination state
        if let Some(total) = self.total_rows {
            self.current_offset < total
        } else {
            false
        }
    }
}

/// DuckDB target connector for writing data to DuckDB databases
pub struct DuckdbTarget {
    connection_string: String,
    connection: Option<Arc<Mutex<Connection>>>,
    table_name: String,
    schema: Option<Schema>, // Cache the schema to avoid re-inference
}

impl DuckdbTarget {
    pub fn new(connection_string: &str) -> Result<Self> {
        // Parse connection string - support multiple formats:
        // - "file.duckdb" or "file.duckdb#table" (legacy)
        // - "duckdb:file.duckdb" or "duckdb:file.duckdb#table"
        let normalized = connection_string.trim_start_matches("duckdb:");

        let (db_path, table) = if normalized.contains('#') {
            let parts: Vec<&str> = normalized.split('#').collect();
            if parts.len() != 2 {
                return Err(TinyEtlError::Configuration(
                    "DuckDB connection string format: file.duckdb#table or duckdb:file.duckdb#table".to_string()
                ));
            }
            (parts[0], parts[1])
        } else {
            // Default table name if not specified
            (normalized, "data")
        };

        Ok(Self {
            connection_string: db_path.to_string(),
            connection: None,
            table_name: table.to_string(),
            schema: None,
        })
    }

    fn get_db_path(&self) -> Result<PathBuf> {
        Ok(PathBuf::from(&self.connection_string))
    }

    fn map_data_type_to_duckdb(&self, data_type: &DataType) -> &'static str {
        match data_type {
            DataType::Integer => "BIGINT",
            DataType::Decimal => "DOUBLE",
            DataType::String => "VARCHAR",
            DataType::Boolean => "BOOLEAN",
            DataType::Date => "DATE",
            DataType::DateTime => "TIMESTAMP",
            DataType::Json => "JSON", // DuckDB has native JSON type
            DataType::Null => "VARCHAR",
        }
    }
}

#[async_trait]
impl Target for DuckdbTarget {
    async fn connect(&mut self) -> Result<()> {
        // Ensure parent directory exists for the database file
        let db_path = self.get_db_path()?;
        if let Some(parent) = db_path.parent() {
            std::fs::create_dir_all(parent)?;
        }

        // DuckDB will automatically create the database file if it doesn't exist
        let conn = Connection::open(&self.connection_string).map_err(|e| {
            TinyEtlError::Connection(format!(
                "Failed to connect to DuckDB database '{}': {}. Check file path and permissions.",
                db_path.display(),
                e
            ))
        })?;

        self.connection = Some(Arc::new(Mutex::new(conn)));
        Ok(())
    }

    async fn create_table(&mut self, table_name: &str, schema: &Schema) -> Result<()> {
        if self.connection.is_none() {
            self.connect().await?;
        }

        let conn = self.connection.as_ref().unwrap();
        let conn = conn.lock().unwrap();

        // Determine the actual table name to use
        let actual_table_name = if table_name.is_empty() {
            self.table_name.clone()
        } else {
            table_name.to_string()
        };

        // Update our internal table name to match what we're actually creating
        self.table_name = actual_table_name.clone();

        // Store the schema for efficient Arrow-based writes
        self.schema = Some(schema.clone());

        // Build CREATE TABLE statement with IF NOT EXISTS (append-first philosophy)
        let column_definitions: Vec<String> = schema
            .columns
            .iter()
            .map(|col| {
                let duckdb_type = self.map_data_type_to_duckdb(&col.data_type);
                let nullable = if col.nullable { "" } else { " NOT NULL" };
                format!("\"{}\" {}{}", col.name, duckdb_type, nullable)
            })
            .collect();

        // Use CREATE TABLE IF NOT EXISTS to support append-first philosophy
        let create_sql = format!(
            "CREATE TABLE IF NOT EXISTS \"{}\" ({})",
            actual_table_name,
            column_definitions.join(", ")
        );

        conn.execute(&create_sql, [])
            .map_err(|e| TinyEtlError::DataTransfer(format!("Failed to create table: {}", e)))?;

        Ok(())
    }

    async fn write_batch(&mut self, rows: &[Row]) -> Result<usize> {
        if self.connection.is_none() {
            return Err(TinyEtlError::Connection(
                "Connection not established".to_string(),
            ));
        }

        if rows.is_empty() {
            return Ok(0);
        }

        let conn = self.connection.as_ref().unwrap();
        let conn = conn.lock().unwrap();

        // Use cached schema if available, otherwise infer from rows
        let schema = match &self.schema {
            Some(s) => s.clone(),
            None => {
                // Fallback: infer schema from rows (less efficient)
                crate::schema::SchemaInferer::infer_from_rows(rows)?
            }
        };

        // Use DuckDB's high-performance Appender API for batch insertion
        let affected = insert_with_appender(&conn, &self.table_name, rows, &schema)?;

        Ok(affected)
    }

    async fn finalize(&mut self) -> Result<()> {
        // DuckDB doesn't require explicit finalization
        Ok(())
    }

    async fn exists(&self, table_name: &str) -> Result<bool> {
        if let Some(conn) = &self.connection {
            let conn = conn.lock().unwrap();
            let actual_table_name = if table_name.is_empty() {
                &self.table_name
            } else {
                table_name
            };

            let query = "SELECT table_name FROM information_schema.tables WHERE table_name = ?";
            let result = conn.query_row(query, [actual_table_name], |_| Ok(true));

            Ok(result.unwrap_or(false))
        } else {
            Ok(false)
        }
    }

    async fn truncate(&mut self, table_name: &str) -> Result<()> {
        if let Some(conn) = &self.connection {
            let conn = conn.lock().unwrap();
            let actual_table_name = if table_name.is_empty() {
                &self.table_name
            } else {
                table_name
            };

            conn.execute(&format!("DELETE FROM \"{}\"", actual_table_name), [])
                .map_err(|e| {
                    TinyEtlError::DataTransfer(format!("Failed to truncate table: {}", e))
                })?;
        }
        Ok(())
    }

    fn supports_append(&self) -> bool {
        // DuckDB databases support appending new rows
        true
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_duckdb_source_new() {
        let source = DuckdbSource::new("test.duckdb#users");
        assert!(source.is_ok());
    }

    #[test]
    fn test_duckdb_source_invalid_format() {
        let source = DuckdbSource::new("test.duckdb");
        assert!(source.is_err());
    }

    #[test]
    fn test_duckdb_target_new() {
        let target = DuckdbTarget::new("test.duckdb#users");
        assert!(target.is_ok());

        let target2 = DuckdbTarget::new("test.duckdb");
        assert!(target2.is_ok());
    }
}