operon 0.7.0

A workflow engine for parallel, incremental scheduling of DAG-defined multiplex tasks.
Documentation
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
use async_trait::async_trait;
use bytes::Bytes;
use futures::SinkExt;
use serde::Serialize;
use serde::de::DeserializeOwned;

use crate::schema::{Entity, EntityMetadata};
use crate::storage::StorageResult;
use crate::storage::psql::client::StorageClient;
use crate::storage::psql::{PsqlStorageError, PsqlStorageResult};
use crate::utils::{
    SchemaPrefix, SchemaPrefixOwned, ShapeAction, ShapeTable, SqlParams, build_tables,
    hash_metadata, psql_identifier, shape_query,
};

/// The table recording each entity's shape ID, keyed by entity ID.
pub(super) const ENTITY_SHAPES: ShapeTable<'static> = ShapeTable {
    table: "_entity_hash",
    column: "hash",
};

pub trait PsqlEntity: Serialize + DeserializeOwned + Send + Sync + 'static {}
impl<T> PsqlEntity for T where T: Serialize + DeserializeOwned + Send + Sync + 'static {}

/// Helper struct for building SQL queries related to entities.
#[derive(Debug)]
pub struct EntityQueryBuilder<'a, const N: usize, T: PsqlEntity> {
    client: StorageClient<'a>,
    entity_meta: EntityMetadata<N, T>,
}

impl<'a> StorageClient<'a> {
    /// Constructs an `EntityQueryBuilder` for the given entity metadata.
    pub fn entity<const N: usize, T: PsqlEntity>(
        self,
        entity_meta: EntityMetadata<N, T>,
    ) -> EntityQueryBuilder<'a, N, T> {
        EntityQueryBuilder {
            client: self,
            entity_meta,
        }
    }
}

impl<const N: usize, T: PsqlEntity> EntityQueryBuilder<'_, N, T> {
    /// Initializes the entity table.
    pub async fn init(&self) -> PsqlStorageResult<()> {
        self.entity_meta.init(&self.client).await
    }

    /// Gets the entity for the given primary key.
    pub async fn get(&self, coordinate: [usize; N]) -> PsqlStorageResult<Option<T>> {
        let schema_prefix = self.client.schema_prefix();
        let stmt = GetEntityQuery(schema_prefix, self.entity_meta);
        let params = SqlParams::from_usize(coordinate)?;
        let Some(row) = self.client.query_opt_stmt(&stmt, &params.borrow()).await? else {
            return Ok(None);
        };
        let value = serde_json::from_value::<T>(row.get(0))?;
        Ok(Some(value))
    }

    /// Puts an entity into the table.
    pub async fn put(&self, entity: Entity<N, T>) -> PsqlStorageResult<()> {
        let schema_prefix = self.client.schema_prefix();
        let stmt = PutEntityQuery(schema_prefix, self.entity_meta);
        let params = SqlParams::from_usize(entity.coordinate)?
            .extend(vec![Box::new(serde_json::to_value(&entity.value)?)]);
        let _num_rows = self.client.execute_stmt(&stmt, &params.borrow()).await?;
        Ok(())
    }

    /// Gets a vector of entity from the table.
    pub async fn batch_get<const M: usize, const K: usize>(
        &self,
        coordinate: [usize; M],
        over: [&'static str; K],
    ) -> PsqlStorageResult<Vec<Entity<K, T>>> {
        const { assert!(M + K == N) }

        let schema_prefix = self.client.schema_prefix();
        let stmt = BatchGetQuery(schema_prefix, self.entity_meta, over);
        let params = SqlParams::from_usize(coordinate)?;
        let rows = self.client.query_stmt(&stmt, &params.borrow()).await?;
        let entities = rows
            .into_iter()
            .map(|r| -> PsqlStorageResult<Entity<K, T>> {
                let value = serde_json::from_value::<T>(r.get(0))?;
                let coordinate: [usize; K] = (1..=K)
                    .map(|idx| usize::try_from(r.get::<_, i64>(idx)))
                    .collect::<Result<Vec<_>, _>>()?
                    .try_into()
                    .expect("Length is always K");
                Ok(Entity { value, coordinate })
            })
            .collect::<Result<Vec<_>, _>>()?;
        Ok(entities)
    }

    /// Puts a vector of entity into the table.
    pub async fn batch_put<const M: usize>(
        &mut self,
        entity: Entity<M, Vec<T>>,
    ) -> PsqlStorageResult<()> {
        const { assert!(M + 1 == N) }

        let schema_prefix = self.client.schema_prefix().into_owned();
        let tx = self.client.transaction().await?;

        let temp_table_stmt = BatchPutTempTableQuery(&schema_prefix, self.entity_meta).to_string();
        let _num_rows = tx.execute(&temp_table_stmt, &[]).await?;

        let mut writer = csv::WriterBuilder::new()
            .has_headers(false)
            .from_writer(vec![]);
        for (idx, value) in entity.value.iter().enumerate() {
            let mut record: Vec<String> = Vec::with_capacity(M + 2);
            record.extend(entity.coordinate.iter().map(|c| c.to_string()));
            record.push(idx.to_string());
            record.push(serde_json::to_value(value)?.to_string());
            writer.write_record(&record)?;
        }

        let copy_stmt = BatchPutCopyQuery(self.entity_meta).to_string();
        let sink = tx.copy_in(&copy_stmt).await?;
        let mut sink = Box::pin(sink);
        sink.send(Bytes::from(writer.into_inner()?)).await?;
        sink.close().await?;

        let insert_stmt = BatchPutInsertQuery(&schema_prefix, self.entity_meta).to_string();
        let _num_rows = tx.execute(&insert_stmt, &[]).await?;

        tx.commit().await?;
        Ok(())
    }
}

/// Prepares one entity's table, erased of the entity's arity and type.
///
/// The generated storage implements this over every entity of a pipeline, so that its `init` walks
/// one collection.
#[async_trait]
pub trait EntityQueries: Send + Sync + 'static {
    /// Creates this entity's table, rebuilding it when the recorded shape no longer matches the
    /// entity.
    async fn init(&self, client: &StorageClient<'_>) -> StorageResult<(), PsqlStorageError>;
}

#[async_trait]
impl<const N: usize, T: Send + Sync + 'static> EntityQueries for EntityMetadata<N, T> {
    async fn init(&self, client: &StorageClient<'_>) -> PsqlStorageResult<()> {
        let schema_prefix = client.schema_prefix();
        let record = ENTITY_SHAPES.record(self.id);
        let table = psql_identifier("entity", self.id);
        let tables = [table.as_str()];
        let shape_id = hash_metadata(self);

        let shape_stmt = shape_query(record, &tables, schema_prefix);
        let row = client.query_opt(&shape_stmt, &[]).await?;
        let action = ShapeAction::from_row(row.as_ref(), &shape_id);

        if let Some(stmt) = build_tables(
            record,
            &tables,
            &shape_id,
            schema_prefix,
            action,
            InitEntityQuery(schema_prefix, *self),
        ) {
            client.batch_execute(&stmt).await?;
        }
        Ok(())
    }
}

/// A helper struct to generate SQL query for initializing a entity table.
struct InitEntityQuery<'a, const N: usize, T>(SchemaPrefix<'a>, EntityMetadata<N, T>);

impl<const N: usize, T> std::fmt::Display for InitEntityQuery<'_, N, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let schema = self.0;
        let id = self.1.id;
        let dims = self.1.dims;
        let table = psql_identifier("entity", id);

        writeln!(f, "CREATE TABLE IF NOT EXISTS {schema}{table} (",)?;

        for dim in &dims {
            writeln!(f, "    {dim} BIGINT,")?;
        }
        if dims.is_empty() {
            writeln!(f, "    id BOOLEAN PRIMARY KEY DEFAULT TRUE CHECK (id),")?;
        }

        writeln!(f, "    value JSONB,")?;
        write!(f, "    created_at TIMESTAMP WITH TIME ZONE DEFAULT now()")?;

        if !dims.is_empty() {
            writeln!(f, ",")?;
            writeln!(f, "    PRIMARY KEY ({})", dims.join(", "))?;
        } else {
            writeln!(f)?;
        }

        write!(f, ");")
    }
}

/// A helper struct to generate SQL query for getting an entity.
struct GetEntityQuery<'a, const N: usize, T>(SchemaPrefix<'a>, EntityMetadata<N, T>);

impl<const N: usize, T> std::fmt::Display for GetEntityQuery<'_, N, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let schema = self.0;
        let id = self.1.id;
        let dims = self.1.dims;
        let table = psql_identifier("entity", id);

        write!(f, "SELECT value FROM {schema}{table}")?;
        for (idx, dim) in dims.iter().enumerate() {
            if idx == 0 {
                write!(f, " WHERE")?;
            } else {
                write!(f, " AND")?;
            }

            write!(f, " {} = ${}", dim, idx + 1)?;
        }
        write!(f, ";")
    }
}

/// A helper struct to generate SQL query for inserting an entity.
struct PutEntityQuery<'a, const N: usize, T>(SchemaPrefix<'a>, EntityMetadata<N, T>);

impl<const N: usize, T> std::fmt::Display for PutEntityQuery<'_, N, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let schema = self.0;
        let id = self.1.id;
        let dims = self.1.dims;
        let table = psql_identifier("entity", id);

        write!(f, "INSERT INTO {schema}{table} (")?;
        for dim in dims {
            write!(f, "{dim}, ")?;
        }
        writeln!(f, "value)")?;

        write!(f, "VALUES (")?;
        for idx in 0..=dims.len() {
            if idx != 0 {
                write!(f, ", ")?;
            }
            write!(f, "${}", idx + 1)?;
        }
        writeln!(f, ")")?;

        write!(f, "ON CONFLICT (")?;
        if !dims.is_empty() {
            write!(f, "{}", dims.join(", "))?;
        } else {
            write!(f, "id")?;
        }
        write!(f, ") DO UPDATE SET value = EXCLUDED.value;")
    }
}

/// A helper struct to generate SQL query for creating temp tables for batch get.
struct BatchGetQuery<'a, const N: usize, const M: usize, T>(
    SchemaPrefix<'a>,
    EntityMetadata<N, T>,
    [&'static str; M],
);

impl<const N: usize, const M: usize, T> std::fmt::Display for BatchGetQuery<'_, N, M, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let schema = self.0;
        let id = self.1.id;
        let dims = self.1.dims;
        let over_dims = self.2;
        let table = psql_identifier("entity", id);

        write!(f, "SELECT value")?;
        for over_dim in over_dims {
            write!(f, ", {over_dim}")?;
        }
        writeln!(f)?;

        writeln!(f, "FROM {schema}{table}")?;

        for (idx, dim) in dims
            .iter()
            .filter(|dim| !over_dims.contains(dim))
            .enumerate()
        {
            if idx == 0 {
                write!(f, "WHERE")?;
            } else {
                write!(f, " AND")?;
            }
            write!(f, " {} = ${}", dim, idx + 1)?;
        }
        writeln!(f)?;

        for (idx, dim) in over_dims.iter().enumerate() {
            if idx == 0 {
                write!(f, "ORDER BY {dim}")?;
            } else {
                write!(f, ", {dim}")?;
            }
        }
        Ok(())
    }
}

/// A helper struct to generate SQL query for creating temp tables for batch insertion.
struct BatchPutTempTableQuery<'a, const N: usize, T>(&'a SchemaPrefixOwned, EntityMetadata<N, T>);

impl<const N: usize, T> std::fmt::Display for BatchPutTempTableQuery<'_, N, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let schema = self.0;
        let id = self.1.id;
        let table = psql_identifier("entity", id);

        writeln!(f, "CREATE TEMP TABLE temp (")?;
        writeln!(f, "    LIKE {schema}{table} INCLUDING ALL")?;
        writeln!(f, ")")?;
        write!(f, "ON COMMIT DROP;")
    }
}

/// A helper struct to generate SQL query for inserting data into temp tables for batch insertion.
struct BatchPutCopyQuery<const N: usize, T>(EntityMetadata<N, T>);

impl<const N: usize, T> std::fmt::Display for BatchPutCopyQuery<N, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let dims = self.0.dims;

        write!(f, "COPY temp (")?;
        for dim in dims {
            write!(f, "{dim}, ")?;
        }
        write!(f, "value) FROM STDIN WITH (FORMAT csv);")
    }
}

/// A helper struct to generate SQL query for inserting data from temp table to main table for batch
/// insertion.
struct BatchPutInsertQuery<'a, const N: usize, T>(&'a SchemaPrefixOwned, EntityMetadata<N, T>);

impl<const N: usize, T> std::fmt::Display for BatchPutInsertQuery<'_, N, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let schema = self.0;
        let id = self.1.id;
        let dims = self.1.dims;
        let table = psql_identifier("entity", id);

        write!(f, "INSERT INTO {schema}{table} (")?;
        for dim in dims {
            write!(f, "{dim}, ")?;
        }
        writeln!(f, "value)")?;

        write!(f, "SELECT ")?;
        for dim in dims {
            write!(f, "{dim}, ")?;
        }
        writeln!(f, "value FROM temp")?;
        write!(f, "ON CONFLICT (")?;
        write!(f, "{}", dims.join(", "))?;
        write!(f, ") DO UPDATE SET value = EXCLUDED.value;")
    }
}

#[cfg(test)]
mod test {
    use indoc::indoc;
    use rstest::{fixture, rstest};

    use super::*;

    #[fixture]
    fn schema_prefix() -> SchemaPrefix<'static> {
        SchemaPrefix(Some("test_meta"))
    }

    fn entity_a() -> EntityMetadata<0, ()> {
        EntityMetadata {
            id: "a",
            dims: [],
            _phantom: std::marker::PhantomData,
        }
    }

    fn entity_b() -> EntityMetadata<1, ()> {
        EntityMetadata {
            id: "b",
            dims: ["i"],
            _phantom: std::marker::PhantomData,
        }
    }

    #[rstest]
    #[case(
        entity_a(),
        indoc! {"
            CREATE TABLE IF NOT EXISTS test_meta.entity_a (
                id BOOLEAN PRIMARY KEY DEFAULT TRUE CHECK (id),
                value JSONB,
                created_at TIMESTAMP WITH TIME ZONE DEFAULT now()
            );"
        }
    )]
    #[case(
        entity_b(),
        indoc! {"
            CREATE TABLE IF NOT EXISTS test_meta.entity_b (
                i BIGINT,
                value JSONB,
                created_at TIMESTAMP WITH TIME ZONE DEFAULT now(),
                PRIMARY KEY (i)
            );"
        }
    )]
    fn test_init_entity_query<const N: usize>(
        schema_prefix: SchemaPrefix<'_>,
        #[case] metadata: EntityMetadata<N, ()>,
        #[case] expected: &str,
    ) {
        let stmt = InitEntityQuery(schema_prefix, metadata).to_string();
        assert_eq!(stmt, expected);
    }

    #[rstest]
    #[case(entity_a(), "SELECT value FROM test_meta.entity_a;")]
    #[case(entity_b(), "SELECT value FROM test_meta.entity_b WHERE i = $1;")]
    fn test_get_entity_query<const N: usize>(
        schema_prefix: SchemaPrefix<'_>,
        #[case] metadata: EntityMetadata<N, ()>,
        #[case] expected: &str,
    ) {
        let stmt = GetEntityQuery(schema_prefix, metadata).to_string();
        assert_eq!(stmt, expected);
    }

    #[rstest]
    #[case(entity_a(), indoc! {"
        INSERT INTO test_meta.entity_a (value)
        VALUES ($1)
        ON CONFLICT (id) DO UPDATE SET value = EXCLUDED.value;"
    })]
    #[case(entity_b(), indoc! {"
        INSERT INTO test_meta.entity_b (i, value)
        VALUES ($1, $2)
        ON CONFLICT (i) DO UPDATE SET value = EXCLUDED.value;"
    })]
    fn test_put_entity_query<const N: usize>(
        schema_prefix: SchemaPrefix<'_>,
        #[case] metadata: EntityMetadata<N, ()>,
        #[case] expected: &str,
    ) {
        let stmt = PutEntityQuery(schema_prefix, metadata).to_string();
        assert_eq!(stmt, expected);
    }

    #[rstest]
    #[case::simple(
        entity_b(),
        indoc! {"
            CREATE TEMP TABLE temp (
                LIKE test_meta.entity_b INCLUDING ALL
            )
            ON COMMIT DROP;"
        }
    )]
    fn test_batch_put_temp_table_query<const N: usize>(
        schema_prefix: SchemaPrefix<'_>,
        #[case] metadata: EntityMetadata<N, ()>,
        #[case] expected: &str,
    ) {
        let stmt = BatchPutTempTableQuery(&schema_prefix.into_owned(), metadata).to_string();
        assert_eq!(stmt, expected);
    }

    #[rstest]
    #[case::simple(entity_b(), "COPY temp (i, value) FROM STDIN WITH (FORMAT csv);")]
    fn test_batch_put_copy_query<const N: usize>(
        #[case] metadata: EntityMetadata<N, ()>,
        #[case] expected: &str,
    ) {
        let stmt = BatchPutCopyQuery(metadata).to_string();
        assert_eq!(stmt, expected);
    }

    #[rstest]
    #[case::simple(
        entity_b(),
        indoc! {"
            INSERT INTO test_meta.entity_b (i, value)
            SELECT i, value FROM temp
            ON CONFLICT (i) DO UPDATE SET value = EXCLUDED.value;"
        }
    )]
    fn test_batch_put_insert_query<const N: usize>(
        schema_prefix: SchemaPrefix<'_>,
        #[case] metadata: EntityMetadata<N, ()>,
        #[case] expected: &str,
    ) {
        let query = BatchPutInsertQuery(&schema_prefix.into_owned(), metadata).to_string();
        assert_eq!(query, expected);
    }
}