tinyetl 0.12.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
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
use crate::{
    connectors::{self, Source, Target},
    protocols::{extension_from_path, Protocol},
    schema::{Row, Schema},
    Result, TinyEtlError,
};
use async_trait::async_trait;
use futures_util::StreamExt;
use object_store::{
    aws::AmazonS3Builder, buffered::BufWriter, path::Path as ObjectPath, ObjectStore,
    ObjectStoreExt,
};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tempfile::TempDir;
use tokio::io::AsyncWriteExt;
use tracing::{info, warn};
use url::Url;

/// File formats that can be staged through S3 (must match the file connectors).
const SUPPORTED_FORMATS: [&str; 8] = [
    "csv", "tsv", "json", "jsonl", "ndjson", "parquet", "avro", "xlsx",
];

/// Endpoint options read by S3 sources and targets (see `crate::options`); the file format's
/// options (CSV `delimiter`) are accepted too
pub const OPTIONS: &[&str] = &[
    "region",
    "endpoint",
    "access_key_id",
    "secret_access_key",
    "session_token",
    "allow_http",
    "path_style",
];

/// S3 protocol for reading and writing single objects (`s3://bucket/path/to/file.ext`).
///
/// Objects are staged through a local temporary file: sources are downloaded and then read
/// by the matching file connector; targets are written locally by the file connector and
/// uploaded when the transfer is finalized.
///
/// Credentials and settings come from the standard `AWS_*` environment variables and can be
/// overridden per endpoint with options (`region`, `endpoint`, `access_key_id`,
/// `secret_access_key`, `session_token`, `allow_http`, `path_style`).
pub struct S3Protocol;

impl Default for S3Protocol {
    fn default() -> Self {
        Self::new()
    }
}

impl S3Protocol {
    pub fn new() -> Self {
        Self
    }

    /// Split an s3:// URL into (bucket, object key)
    fn parse_location(url: &Url) -> Result<(String, ObjectPath)> {
        let bucket = url
            .host_str()
            .filter(|b| !b.is_empty())
            .ok_or_else(|| {
                TinyEtlError::Configuration(format!(
                    "S3 URL must specify a bucket (s3://bucket/key): {}",
                    url
                ))
            })?
            .to_string();

        let raw_key = url.path().trim_start_matches('/');
        if raw_key.is_empty() || raw_key.ends_with('/') {
            return Err(TinyEtlError::Configuration(format!(
                "S3 URL must point to a single object (s3://bucket/path/file.ext): {}",
                url
            )));
        }

        let key = ObjectPath::from_url_path(raw_key).map_err(|e| {
            TinyEtlError::Configuration(format!("Invalid S3 object key in '{}': {}", url, e))
        })?;

        Ok((bucket, key))
    }

    /// Work out the file format from the object key, or from an explicit type override
    fn resolve_format(url: &Url, source_type: Option<&str>) -> Result<String> {
        let format = source_type
            .map(|t| t.to_lowercase())
            .or_else(|| extension_from_path(url.path()));

        // Compressed objects (`csv.gz`, `parquet.zst`) are supported when the inner format is
        let inner_format = |f: &str| -> String {
            crate::connectors::compressed::Codec::from_path(f)
                .map_or(f, |(_, inner)| inner)
                .to_string()
        };
        match format {
            Some(f) if SUPPORTED_FORMATS.contains(&inner_format(&f).as_str()) => Ok(f),
            Some(f) => Err(TinyEtlError::Configuration(format!(
                "Unsupported file format '{}' for {}. Supported formats: {}",
                f,
                url,
                SUPPORTED_FORMATS.join(", ")
            ))),
            None => Err(TinyEtlError::Configuration(format!(
                "Cannot determine the file format of {}. The object key needs a file extension ({}), or use --source-type for sources.",
                url,
                SUPPORTED_FORMATS.join(", ")
            ))),
        }
    }

    fn parse_bool(key: &str, value: &str) -> Result<bool> {
        match value.to_lowercase().as_str() {
            "true" | "1" | "yes" => Ok(true),
            "false" | "0" | "no" => Ok(false),
            _ => Err(TinyEtlError::Configuration(format!(
                "Invalid value '{}' for S3 option '{}': expected true or false",
                value, key
            ))),
        }
    }

    /// Build the S3 client from AWS_* environment variables plus per-endpoint options
    fn build_store(
        bucket: &str,
        options: &HashMap<String, String>,
    ) -> Result<Arc<dyn ObjectStore>> {
        let mut builder = AmazonS3Builder::from_env().with_bucket_name(bucket);
        let mut allow_http: Option<bool> = None;
        let mut http_endpoint = false;

        // Unknown keys were rejected by `crate::options::check`; the rest belong to the file format
        for (key, value) in options {
            builder = match key.as_str() {
                "region" => builder.with_region(value),
                "endpoint" => {
                    http_endpoint = value.starts_with("http://");
                    builder.with_endpoint(value)
                }
                "access_key_id" => builder.with_access_key_id(value),
                "secret_access_key" => builder.with_secret_access_key(value),
                "session_token" => builder.with_token(value),
                "allow_http" => {
                    allow_http = Some(Self::parse_bool(key, value)?);
                    builder
                }
                "path_style" => {
                    builder.with_virtual_hosted_style_request(!Self::parse_bool(key, value)?)
                }
                _ => builder,
            };
        }

        // A plain http:// endpoint (e.g. a local S3-compatible server) only works with allow_http, so imply it
        // unless the user set it explicitly.
        if let Some(allow) = allow_http.or(http_endpoint.then_some(true)) {
            builder = builder.with_allow_http(allow);
        }

        let store = builder.build().map_err(|e| {
            TinyEtlError::Configuration(format!("Failed to configure S3 client: {}", e))
        })?;

        Ok(Arc::new(store))
    }
}

#[async_trait]
impl Protocol for S3Protocol {
    async fn create_source(
        &self,
        url: &Url,
        options: &HashMap<String, String>,
    ) -> Result<Box<dyn Source>> {
        self.create_source_with_type(url, None, options).await
    }

    async fn create_source_with_type(
        &self,
        url: &Url,
        source_type: Option<&str>,
        options: &HashMap<String, String>,
    ) -> Result<Box<dyn Source>> {
        let (bucket, key) = Self::parse_location(url)?;
        let format = Self::resolve_format(url, source_type)?;
        let store = Self::build_store(&bucket, options)?;
        let location = format!("s3://{}/{}", bucket, key);

        Ok(Box::new(
            S3Source::download(store, &key, &location, &format, options).await?,
        ))
    }

    async fn create_target(
        &self,
        url: &Url,
        options: &HashMap<String, String>,
    ) -> Result<Box<dyn Target>> {
        let (bucket, key) = Self::parse_location(url)?;
        let format = Self::resolve_format(url, None)?;
        let store = Self::build_store(&bucket, options)?;
        let location = format!("s3://{}/{}", bucket, key);

        Ok(Box::new(S3Target::new(
            store, key, location, &format, options,
        )?))
    }

    fn validate_url(&self, url: &Url) -> Result<()> {
        if url.scheme() != "s3" {
            return Err(TinyEtlError::Configuration(format!(
                "S3 protocol requires s3:// scheme, got: {}",
                url.scheme()
            )));
        }

        Self::parse_location(url).map(|_| ())
    }

    fn name(&self) -> &'static str {
        "s3"
    }
}

/// Convert an object store error into a TinyETL error with the object location in the message
fn s3_error(action: &str, location: &str, error: object_store::Error) -> TinyEtlError {
    match error {
        object_store::Error::NotFound { .. } => {
            TinyEtlError::Connection(format!("S3 object not found: {}", location))
        }
        other => {
            let message = other.to_string();
            // With no credentials configured the client falls back to the EC2 instance
            // metadata service, which fails with an unhelpful error anywhere outside AWS.
            let hint = if message.contains("169.254.169.254") {
                " (no AWS credentials found: set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, or the access_key_id / secret_access_key options)"
            } else {
                ""
            };
            TinyEtlError::Connection(format!(
                "Failed to {} {}: {}{}",
                action, location, message, hint
            ))
        }
    }
}

fn staging_dir() -> Result<TempDir> {
    tempfile::Builder::new()
        .prefix("tinyetl_s3_")
        .tempdir()
        .map_err(TinyEtlError::Io)
}

/// Source that reads an S3 object through a locally staged copy.
/// The staged file is removed when the source is dropped.
pub struct S3Source {
    inner: Box<dyn Source>,
    _staging: TempDir,
}

impl S3Source {
    /// Download `key` from `store` into a temporary file and open it with the file connector
    /// for `format`. `location` is the human-readable location used in logs and errors.
    pub async fn download(
        store: Arc<dyn ObjectStore>,
        key: &ObjectPath,
        location: &str,
        format: &str,
        options: &HashMap<String, String>,
    ) -> Result<Self> {
        let staging = staging_dir()?;
        let local_path = staging.path().join(format!("source.{}", format));

        info!("Downloading {}", location);

        let result = store
            .get(key)
            .await
            .map_err(|e| s3_error("download", location, e))?;

        let mut file = tokio::fs::File::create(&local_path).await?;
        let mut stream = result.into_stream();
        let mut total_bytes: u64 = 0;

        while let Some(chunk) = stream.next().await {
            let chunk = chunk.map_err(|e| s3_error("download", location, e))?;
            total_bytes += chunk.len() as u64;
            file.write_all(&chunk).await?;
        }
        file.flush().await?;
        drop(file);

        info!("Download completed, {} bytes received", total_bytes);

        let inner = connectors::create_source_with_options(&local_path.to_string_lossy(), options)?;

        Ok(Self {
            inner,
            _staging: staging,
        })
    }
}

#[async_trait]
impl Source for S3Source {
    async fn connect(&mut self) -> Result<()> {
        self.inner.connect().await
    }

    async fn infer_schema(&mut self, sample_size: usize) -> Result<Schema> {
        self.inner.infer_schema(sample_size).await
    }

    async fn read_batch(&mut self, batch_size: usize) -> Result<Vec<Row>> {
        self.inner.read_batch(batch_size).await
    }

    async fn estimated_row_count(&self) -> Result<Option<usize>> {
        self.inner.estimated_row_count().await
    }

    async fn reset(&mut self) -> Result<()> {
        self.inner.reset().await
    }

    fn has_more(&self) -> bool {
        self.inner.has_more()
    }
}

/// Target that writes a local staging file with the file connector and uploads it to S3 when
/// the transfer is finalized. An existing object is replaced; nothing is uploaded if the
/// transfer fails before `finalize`.
pub struct S3Target {
    inner: Box<dyn Target>,
    store: Arc<dyn ObjectStore>,
    key: ObjectPath,
    location: String,
    local_path: PathBuf,
    _staging: TempDir,
}

impl S3Target {
    /// Create a target that uploads to `key` in `store`, written in `format`.
    /// `location` is the human-readable location used in logs and errors.
    pub fn new(
        store: Arc<dyn ObjectStore>,
        key: ObjectPath,
        location: String,
        format: &str,
        options: &HashMap<String, String>,
    ) -> Result<Self> {
        let staging = staging_dir()?;
        let local_path = staging.path().join(format!("target.{}", format));
        let inner = connectors::create_target_with_options(&local_path.to_string_lossy(), options)?;

        Ok(Self {
            inner,
            store,
            key,
            location,
            local_path,
            _staging: staging,
        })
    }

    async fn object_exists(&self) -> Result<bool> {
        match self.store.head(&self.key).await {
            Ok(_) => Ok(true),
            Err(object_store::Error::NotFound { .. }) => Ok(false),
            Err(e) => Err(s3_error("access", &self.location, e)),
        }
    }
}

#[async_trait]
impl Target for S3Target {
    async fn connect(&mut self) -> Result<()> {
        // Check the bucket and credentials up front so that problems surface before any data
        // is read, rather than at the final upload.
        self.object_exists().await?;
        self.inner.connect().await
    }

    async fn create_table(&mut self, table_name: &str, schema: &Schema) -> Result<()> {
        self.inner.create_table(table_name, schema).await
    }

    async fn write_batch(&mut self, rows: &[Row]) -> Result<usize> {
        self.inner.write_batch(rows).await
    }

    async fn finalize(&mut self) -> Result<()> {
        self.inner.finalize().await?;

        if !self.local_path.exists() {
            warn!("No data was written, skipping upload to {}", self.location);
            return Ok(());
        }

        info!("Uploading to {}", self.location);

        let mut file = tokio::fs::File::open(&self.local_path).await?;
        let mut writer = BufWriter::new(Arc::clone(&self.store), self.key.clone());

        let upload = async {
            let bytes = tokio::io::copy(&mut file, &mut writer).await?;
            writer.shutdown().await?;
            Ok::<u64, std::io::Error>(bytes)
        };

        let bytes = upload.await.map_err(|e| {
            TinyEtlError::Connection(format!("Failed to upload {}: {}", self.location, e))
        })?;

        info!("Upload completed, {} bytes sent", bytes);
        Ok(())
    }

    async fn exists(&self, _table_name: &str) -> Result<bool> {
        self.object_exists().await
    }

    async fn truncate(&mut self, _table_name: &str) -> Result<()> {
        // The staging file always starts empty and the upload replaces the whole object,
        // so there is nothing to clear.
        Ok(())
    }

    fn supports_append(&self) -> bool {
        false
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::schema::{Column, DataType, Value};
    use object_store::memory::InMemory;

    fn memory_store() -> Arc<dyn ObjectStore> {
        Arc::new(InMemory::new())
    }

    fn sample_schema() -> Schema {
        Schema {
            columns: vec![
                Column {
                    name: "id".to_string(),
                    data_type: DataType::Integer,
                    nullable: false,
                },
                Column {
                    name: "name".to_string(),
                    data_type: DataType::String,
                    nullable: false,
                },
            ],
            estimated_rows: None,
            primary_key_candidate: None,
        }
    }

    fn sample_rows() -> Vec<Row> {
        [(1, "Alice"), (2, "Bob")]
            .into_iter()
            .map(|(id, name)| {
                let mut row = Row::new();
                row.insert("id".to_string(), Value::Integer(id));
                row.insert("name".to_string(), Value::String(name.to_string()));
                row
            })
            .collect()
    }

    async fn write_rows(store: &Arc<dyn ObjectStore>, key: &str, format: &str) {
        let mut target = S3Target::new(
            Arc::clone(store),
            ObjectPath::from(key),
            format!("s3://bucket/{}", key),
            format,
            &HashMap::new(),
        )
        .unwrap();

        target.connect().await.unwrap();
        target.create_table("data", &sample_schema()).await.unwrap();
        assert_eq!(target.write_batch(&sample_rows()).await.unwrap(), 2);
        target.finalize().await.unwrap();
    }

    async fn read_rows(store: &Arc<dyn ObjectStore>, key: &str, format: &str) -> Vec<Row> {
        let mut source = S3Source::download(
            Arc::clone(store),
            &ObjectPath::from(key),
            &format!("s3://bucket/{}", key),
            format,
            &HashMap::new(),
        )
        .await
        .unwrap();

        source.connect().await.unwrap();
        source.infer_schema(100).await.unwrap();
        source.reset().await.unwrap();
        source.read_batch(100).await.unwrap()
    }

    #[test]
    fn test_validate_s3_url() {
        let protocol = S3Protocol::new();

        let url = Url::parse("s3://my-bucket/data.csv").unwrap();
        assert!(protocol.validate_url(&url).is_ok());

        let url = Url::parse("s3://my-bucket/nested/path/data.parquet").unwrap();
        assert!(protocol.validate_url(&url).is_ok());

        // Wrong scheme
        let url = Url::parse("https://my-bucket/data.csv").unwrap();
        assert!(protocol.validate_url(&url).is_err());

        // No object key
        let url = Url::parse("s3://my-bucket").unwrap();
        assert!(protocol.validate_url(&url).is_err());
        let url = Url::parse("s3://my-bucket/").unwrap();
        assert!(protocol.validate_url(&url).is_err());

        // Prefix rather than a single object
        let url = Url::parse("s3://my-bucket/prefix/").unwrap();
        assert!(protocol.validate_url(&url).is_err());
    }

    #[test]
    fn test_parse_location() {
        let url = Url::parse("s3://my-bucket/exports/2024/orders.parquet").unwrap();
        let (bucket, key) = S3Protocol::parse_location(&url).unwrap();
        assert_eq!(bucket, "my-bucket");
        assert_eq!(key.as_ref(), "exports/2024/orders.parquet");

        // Percent-encoded characters are decoded into the object key
        let url = Url::parse("s3://my-bucket/my%20folder/orders.csv").unwrap();
        let (_, key) = S3Protocol::parse_location(&url).unwrap();
        assert_eq!(key.as_ref(), "my folder/orders.csv");
    }

    #[test]
    fn test_resolve_format() {
        let url = Url::parse("s3://bucket/data.CSV").unwrap();
        assert_eq!(S3Protocol::resolve_format(&url, None).unwrap(), "csv");

        // Explicit type wins over the extension
        let url = Url::parse("s3://bucket/export.txt").unwrap();
        assert_eq!(
            S3Protocol::resolve_format(&url, Some("JSON")).unwrap(),
            "json"
        );

        // Unsupported or missing extension
        assert!(S3Protocol::resolve_format(&url, None).is_err());
        let url = Url::parse("s3://bucket/export").unwrap();
        assert!(S3Protocol::resolve_format(&url, None).is_err());
    }

    #[test]
    fn test_build_store_options() {
        let mut options = HashMap::new();
        options.insert("region".to_string(), "eu-west-1".to_string());
        options.insert("endpoint".to_string(), "http://localhost:9000".to_string());
        options.insert("access_key_id".to_string(), "key".to_string());
        options.insert("secret_access_key".to_string(), "secret".to_string());
        options.insert("path_style".to_string(), "true".to_string());
        assert!(S3Protocol::build_store("bucket", &options).is_ok());

        let mut options = HashMap::new();
        options.insert("allow_http".to_string(), "maybe".to_string());
        assert!(S3Protocol::build_store("bucket", &options).is_err());
    }

    #[tokio::test]
    async fn test_target_requires_supported_extension() {
        let protocol = S3Protocol::new();
        let url = Url::parse("s3://bucket/output").unwrap();
        let result = protocol.create_target(&url, &HashMap::new()).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_source_reads_object() {
        let store = memory_store();
        store
            .put(
                &ObjectPath::from("in/people.csv"),
                "id,name\n1,Alice\n2,Bob\n".into(),
            )
            .await
            .unwrap();

        let rows = read_rows(&store, "in/people.csv", "csv").await;
        assert_eq!(rows.len(), 2);
        assert_eq!(
            rows[0].get("name"),
            Some(&Value::String("Alice".to_string()))
        );
    }

    #[tokio::test]
    async fn test_source_object_not_found() {
        let store = memory_store();
        let result = S3Source::download(
            store,
            &ObjectPath::from("missing.csv"),
            "s3://bucket/missing.csv",
            "csv",
            &HashMap::new(),
        )
        .await;

        let err = result.err().unwrap().to_string();
        assert!(err.contains("S3 object not found: s3://bucket/missing.csv"));
    }

    #[tokio::test]
    async fn test_target_uploads_on_finalize() {
        let store = memory_store();
        let key = ObjectPath::from("out/people.csv");

        let mut target = S3Target::new(
            Arc::clone(&store),
            key.clone(),
            "s3://bucket/out/people.csv".to_string(),
            "csv",
            &HashMap::new(),
        )
        .unwrap();

        target.connect().await.unwrap();
        assert!(!target.exists("people").await.unwrap());
        target
            .create_table("people", &sample_schema())
            .await
            .unwrap();
        target.write_batch(&sample_rows()).await.unwrap();

        // Nothing is uploaded until the transfer is finalized
        assert!(store.head(&key).await.is_err());

        target.finalize().await.unwrap();
        assert!(target.exists("people").await.unwrap());

        let bytes = store.get(&key).await.unwrap().bytes().await.unwrap();
        let content = String::from_utf8(bytes.to_vec()).unwrap();
        assert_eq!(content, "id,name\n1,Alice\n2,Bob\n");
    }

    #[tokio::test]
    async fn test_target_overwrites_existing_object() {
        let store = memory_store();
        let key = ObjectPath::from("out/people.csv");
        store.put(&key, "stale contents".into()).await.unwrap();

        write_rows(&store, "out/people.csv", "csv").await;

        let bytes = store.get(&key).await.unwrap().bytes().await.unwrap();
        assert!(!String::from_utf8_lossy(&bytes).contains("stale"));
    }

    #[tokio::test]
    async fn test_round_trip_all_formats() {
        let store = memory_store();

        for format in SUPPORTED_FORMATS {
            let key = format!("round_trip/people.{}", format);
            write_rows(&store, &key, format).await;

            let rows = read_rows(&store, &key, format).await;
            assert_eq!(rows.len(), 2, "row count for {}", format);

            let mut names: Vec<String> = rows
                .iter()
                .map(|row| match row.get("name") {
                    Some(Value::String(name)) => name.clone(),
                    other => panic!("unexpected name value for {}: {:?}", format, other),
                })
                .collect();
            names.sort();
            assert_eq!(names, vec!["Alice", "Bob"], "names for {}", format);
        }
    }
}