wingfoil-python 8.0.0

python bindings for wingfoil - graph based stream processing framework
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
//! Python bindings for the PostgreSQL adapter.

use crate::PyNode;
use crate::py_element::PyElement;
use crate::py_stream::PyStream;
use crate::types::{DICT_INSERT_INFALLIBLE, INTO_PY_INFALLIBLE, LIST_NEW_INFALLIBLE};

use anyhow::bail;
use chrono::NaiveDateTime;
use pyo3::conversion::IntoPyObject;
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyDict, PyList};
use std::rc::Rc;
use wingfoil::adapters::postgres::{
    PostgresConnection, PostgresDeserialize, PostgresRowExt, PostgresSerialize, Row, ToSql, Type,
    postgres_notify_trigger_sql, postgres_read, postgres_sub, postgres_timestamp, postgres_write,
    quote_ident,
};
use wingfoil::{Burst, NanoTime, Node, Stream, StreamOperators};

/// A deserialized PostgreSQL row stored as column name/value pairs.
/// Each row becomes a Python dict.
#[derive(Debug, Clone, Default)]
struct PyPgRow {
    columns: Vec<(String, PyPgValue)>,
}

/// Intermediate value type for PostgreSQL -> Python conversion.
#[derive(Debug, Clone, Default)]
enum PyPgValue {
    #[default]
    Null,
    Bool(bool),
    Int(i64),
    Float(f64),
    Text(String),
    Bytes(Vec<u8>),
    /// A timestamp rendered with [`postgres_timestamp`] formatting.
    Timestamp(String),
}

impl PyPgValue {
    fn to_py(&self, py: Python<'_>) -> Py<PyAny> {
        match self {
            PyPgValue::Null => py.None(),
            PyPgValue::Bool(v) => v
                .into_pyobject(py)
                .expect(INTO_PY_INFALLIBLE)
                .to_owned()
                .into_any()
                .unbind(),
            PyPgValue::Int(v) => v
                .into_pyobject(py)
                .expect(INTO_PY_INFALLIBLE)
                .into_any()
                .unbind(),
            PyPgValue::Float(v) => v
                .into_pyobject(py)
                .expect(INTO_PY_INFALLIBLE)
                .into_any()
                .unbind(),
            PyPgValue::Text(v) => v
                .into_pyobject(py)
                .expect(INTO_PY_INFALLIBLE)
                .into_any()
                .unbind(),
            PyPgValue::Bytes(v) => PyBytes::new(py, v).into_any().unbind(),
            PyPgValue::Timestamp(v) => v
                .into_pyobject(py)
                .expect(INTO_PY_INFALLIBLE)
                .into_any()
                .unbind(),
        }
    }
}

/// Read a single column, dispatching on the column's declared SQL type.
///
/// The type is resolved from the row metadata (no per-cell trial and error), and an
/// unsupported column type is an **error** — not a silent `None` — so a schema
/// mismatch (e.g. a `numeric` or `uuid` column) aborts on the first row instead of
/// producing rows full of nulls that are indistinguishable from SQL NULL.
fn column_value(row: &Row, idx: usize) -> anyhow::Result<PyPgValue> {
    let ty = row.columns()[idx].type_();
    let value = if *ty == Type::BOOL {
        row.try_get::<_, Option<bool>>(idx)?
            .map_or(PyPgValue::Null, PyPgValue::Bool)
    } else if *ty == Type::INT2 {
        row.try_get::<_, Option<i16>>(idx)?
            .map_or(PyPgValue::Null, |i| PyPgValue::Int(i as i64))
    } else if *ty == Type::INT4 {
        row.try_get::<_, Option<i32>>(idx)?
            .map_or(PyPgValue::Null, |i| PyPgValue::Int(i as i64))
    } else if *ty == Type::INT8 {
        row.try_get::<_, Option<i64>>(idx)?
            .map_or(PyPgValue::Null, PyPgValue::Int)
    } else if *ty == Type::FLOAT4 {
        row.try_get::<_, Option<f32>>(idx)?
            .map_or(PyPgValue::Null, |f| PyPgValue::Float(f as f64))
    } else if *ty == Type::FLOAT8 {
        row.try_get::<_, Option<f64>>(idx)?
            .map_or(PyPgValue::Null, PyPgValue::Float)
    } else if *ty == Type::TEXT || *ty == Type::VARCHAR || *ty == Type::BPCHAR || *ty == Type::NAME
    {
        row.try_get::<_, Option<String>>(idx)?
            .map_or(PyPgValue::Null, PyPgValue::Text)
    } else if *ty == Type::TIMESTAMP {
        row.try_get::<_, Option<NaiveDateTime>>(idx)?
            .map_or(PyPgValue::Null, |t| {
                PyPgValue::Timestamp(postgres_timestamp(t.into()))
            })
    } else if *ty == Type::TIMESTAMPTZ {
        row.try_get::<_, Option<chrono::DateTime<chrono::Utc>>>(idx)?
            .map_or(PyPgValue::Null, |t| {
                PyPgValue::Timestamp(postgres_timestamp(t.naive_utc().into()))
            })
    } else if *ty == Type::BYTEA {
        row.try_get::<_, Option<Vec<u8>>>(idx)?
            .map_or(PyPgValue::Null, PyPgValue::Bytes)
    } else {
        bail!(
            "postgres_read: unsupported column type `{ty}` for column `{}`; supported: \
             bool, int2/int4/int8, float4/float8, text/varchar, timestamp/timestamptz, bytea",
            row.columns()[idx].name()
        );
    };
    Ok(value)
}

impl PostgresDeserialize for PyPgRow {
    fn from_row(row: &Row) -> anyhow::Result<(NanoTime, Self)> {
        // Convention: the query selects its timestamp column first (col 0).
        let time = row.get_nanotime(0)?;
        let columns = row
            .columns()
            .iter()
            .enumerate()
            .map(|(i, col)| Ok((col.name().to_string(), column_value(row, i)?)))
            .collect::<anyhow::Result<Vec<_>>>()?;
        Ok((time, PyPgRow { columns }))
    }
}

/// Read a time-partitioned PostgreSQL table.
///
/// The `query` is wrapped as a subquery and filtered per time slice on `time_col`.
/// **The query must select `time_col` as its first column** so it can be extracted
/// as the on-graph timestamp. Each tick yields a `dict` of `{column_name: value}`.
///
/// `time_col` is identifier-quoted, so it must match the column name exactly as
/// stored (lower-case unless the column was created quoted). A trailing `;` on the
/// query is stripped. A column whose SQL type is unsupported (e.g. `numeric`,
/// `uuid`, `json`) fails the run on the first row rather than reading as `None`.
/// Rows must have distinct timestamps: rows sharing one timestamp arrive in a
/// single tick, which yields only the last of them.
///
/// Args:
///     conn_str: libpq connection string, e.g.
///         `"host=localhost user=postgres password=postgres dbname=postgres"`
///     query: SQL selecting the rows (time column first), e.g.
///         `"SELECT time, sym, price FROM trades"`
///     time_col: name of the `timestamp`/`timestamptz` column used for slicing
///     chunk_size: duration of each time slice in seconds (default: 3600)
///
/// Requires RunMode::HistoricalFrom with a non-zero start time and RunFor::Duration.
#[pyfunction]
#[pyo3(signature = (conn_str, query, time_col, chunk_size=3600))]
pub fn py_postgres_read(
    conn_str: String,
    query: String,
    time_col: String,
    chunk_size: u64,
) -> PyStream {
    let conn = PostgresConnection::new(conn_str);
    // Strip a trailing `;` so `SELECT ...;` survives the subquery wrap below.
    let query = query.trim().trim_end_matches(';').trim_end().to_string();
    let tc = quote_ident(&time_col);
    let stream: Rc<dyn Stream<Burst<PyPgRow>>> = postgres_read::<PyPgRow>(
        conn,
        std::time::Duration::from_secs(chunk_size),
        move |(t0, t1), _date, _iter| {
            format!(
                "SELECT sub.* FROM ({q}) AS sub \
                 WHERE sub.{tc} >= '{t0}' AND sub.{tc} < '{t1}' ORDER BY sub.{tc}",
                q = query,
                tc = tc,
                t0 = postgres_timestamp(t0),
                t1 = postgres_timestamp(t1),
            )
        },
    );

    rows_to_py_stream(stream)
}

/// Marshal one row into a Python dict.
fn row_to_py_dict(py: Python<'_>, row: &PyPgRow) -> Py<PyAny> {
    let dict = PyDict::new(py);
    for (name, value) in &row.columns {
        dict.set_item(name, value.to_py(py))
            .expect(DICT_INSERT_INFALLIBLE);
    }
    dict.into_any().unbind()
}

/// Collapse a burst-of-rows stream and marshal each row into a Python dict.
///
/// Suitable for **historical** reads, where each graph tick carries the rows of a
/// single timestamp (one row when timestamps are distinct). Real-time sources must
/// use [`bursts_to_py_stream`] instead — in real-time mode a tick can carry many
/// rows, and `collapse` keeps only the last.
fn rows_to_py_stream(stream: Rc<dyn Stream<Burst<PyPgRow>>>) -> PyStream {
    let py_stream = stream
        .collapse()
        .map(|row: PyPgRow| Python::attach(|py| PyElement::new(row_to_py_dict(py, &row))));
    PyStream(py_stream)
}

/// Marshal each burst into a Python list of row dicts (one list per tick).
///
/// The real-time counterpart of [`rows_to_py_stream`]: lossless when several rows
/// arrive in one graph cycle. Matches the etcd/kafka/redis sub bindings' shape.
fn bursts_to_py_stream(stream: Rc<dyn Stream<Burst<PyPgRow>>>) -> PyStream {
    let py_stream = stream.map(|burst: Burst<PyPgRow>| {
        Python::attach(|py| {
            let items: Vec<Py<PyAny>> = burst.iter().map(|row| row_to_py_dict(py, row)).collect();
            PyElement::new(
                PyList::new(py, items)
                    .expect(LIST_NEW_INFALLIBLE)
                    .into_any()
                    .unbind(),
            )
        })
    });
    PyStream(py_stream)
}

/// Live-tail a PostgreSQL table in real time using `LISTEN`/`NOTIFY`.
///
/// The `query` is wrapped as a subquery and re-run past a time cursor whenever a
/// notification arrives on `channel` (notifications are a wake-up signal only — rows
/// always come from the query). **The query must select `time_col` as its first
/// column.** Each tick yields a `list` of `{column_name: value}` dicts — a list,
/// not a single dict, because several rows can arrive in one real-time cycle.
///
/// Install the notify trigger on the table first — `postgres_notify_trigger_sql`
/// returns the SQL. Requires a real-time run (`realtime=True`); the time column must
/// be **strictly increasing** across inserts — the cursor query is `time > cursor`,
/// so a row stamped at or before the cursor (including one that ties the current max)
/// is silently dropped.
///
/// Args:
///     conn_str: libpq connection string
///     query: SQL selecting the rows (time column first), e.g.
///         `"SELECT time, sym, price FROM trades"`
///     time_col: name of the `timestamp`/`timestamptz` cursor column
///     channel: NOTIFY channel to LISTEN on (must match the trigger)
///     start: Unix seconds to start the cursor from (default 0.0 = emit all
///         existing rows as a catch-up, then tail live inserts)
#[pyfunction]
#[pyo3(signature = (conn_str, query, time_col, channel, start=0.0))]
pub fn py_postgres_sub(
    conn_str: String,
    query: String,
    time_col: String,
    channel: String,
    start: f64,
) -> PyStream {
    let conn = PostgresConnection::new(conn_str);
    // Strip a trailing `;` so `SELECT ...;` survives the subquery wrap below.
    let query = query.trim().trim_end_matches(';').trim_end().to_string();
    let tc = quote_ident(&time_col);
    let start_from = NanoTime::new((start * 1e9) as u64);
    let stream: Rc<dyn Stream<Burst<PyPgRow>>> =
        postgres_sub::<PyPgRow, _>(conn, channel, start_from, move |cursor| {
            format!(
                "SELECT sub.* FROM ({q}) AS sub \
                 WHERE sub.{tc} > '{c}' ORDER BY sub.{tc}",
                q = query,
                tc = tc,
                c = postgres_timestamp(cursor),
            )
        });
    bursts_to_py_stream(stream)
}

/// SQL that installs an AFTER INSERT trigger firing `pg_notify(channel, '')`.
///
/// Run it against the database (e.g. with psycopg) to wire a table up for
/// `postgres_sub`. Idempotent.
#[pyfunction]
pub fn py_postgres_notify_trigger_sql(table: String, channel: String) -> String {
    postgres_notify_trigger_sql(&table, &channel)
}

/// A single write row: business column values in table order (after time).
#[derive(Debug, Clone, Default)]
struct PyPgWriteRow {
    values: Vec<PgParam>,
}

/// An owned, typed parameter produced from a Python value.
///
/// `None` inside a variant is a **typed** SQL NULL, so nullable columns bind with the
/// correct parameter type regardless of the column's SQL type.
#[derive(Debug, Clone)]
enum PgParam {
    Bool(Option<bool>),
    Int4(Option<i32>),
    Int8(Option<i64>),
    Float4(Option<f32>),
    Float8(Option<f64>),
    Text(Option<String>),
    Bytes(Option<Vec<u8>>),
}

impl PgParam {
    fn boxed(&self) -> Box<dyn ToSql + Sync + Send> {
        match self {
            PgParam::Bool(v) => Box::new(*v),
            PgParam::Int4(v) => Box::new(*v),
            PgParam::Int8(v) => Box::new(*v),
            PgParam::Float4(v) => Box::new(*v),
            PgParam::Float8(v) => Box::new(*v),
            PgParam::Text(v) => Box::new(v.clone()),
            PgParam::Bytes(v) => Box::new(v.clone()),
        }
    }
}

impl PostgresSerialize for PyPgWriteRow {
    fn to_params(&self) -> Vec<Box<dyn ToSql + Sync + Send>> {
        self.values.iter().map(PgParam::boxed).collect()
    }
}

/// Extract the declared columns from a Python dict into an ordered write row.
///
/// Fails loudly: a missing key, an unsupported declared type, or a wrong-typed value
/// is an error that aborts the run — never a silently-inserted NULL. Pass an explicit
/// Python `None` to write a SQL NULL.
fn dict_to_write_row(
    dict: &Bound<'_, PyDict>,
    columns: &[(String, String)],
) -> anyhow::Result<PyPgWriteRow> {
    let values = columns
        .iter()
        .map(|(name, ty)| {
            let value = dict
                .get_item(name)
                .map_err(|e| anyhow::anyhow!("postgres_write: reading key '{name}': {e}"))?
                .ok_or_else(|| {
                    anyhow::anyhow!(
                        "postgres_write: missing column '{name}' in dict \
                         (pass an explicit None to write SQL NULL)"
                    )
                })?;
            // Extract Some(T) from the Python value, or None for Python None.
            macro_rules! extract {
                ($t:ty) => {
                    if value.is_none() {
                        None
                    } else {
                        Some(value.extract::<$t>().map_err(|e| {
                            anyhow::anyhow!("postgres_write: column '{name}' ({ty}): {e}")
                        })?)
                    }
                };
            }
            Ok(match ty.as_str() {
                "bool" => PgParam::Bool(extract!(bool)),
                "int" | "int4" | "integer" => PgParam::Int4(extract!(i32)),
                "long" | "int8" | "bigint" => PgParam::Int8(extract!(i64)),
                "float4" | "real" => PgParam::Float4(extract!(f32)),
                "float" | "float8" | "double" => PgParam::Float8(extract!(f64)),
                "text" | "str" => PgParam::Text(extract!(String)),
                "bytes" | "bytea" => PgParam::Bytes(extract!(Vec<u8>)),
                other => bail!(
                    "postgres_write: unsupported column type '{other}' for column '{name}'; \
                     supported: bool, int/int4/integer, long/int8/bigint, float4/real, \
                     float/float8/double, text/str, bytes/bytea"
                ),
            })
        })
        .collect::<anyhow::Result<Vec<_>>>()?;
    Ok(PyPgWriteRow { values })
}

/// Inner implementation for the `.postgres_write()` stream method.
///
/// The stream must yield either a single `dict` of the declared columns, or a `list`
/// of such dicts for multiple rows per tick. The graph timestamp is prepended.
/// Marshaling failures abort the run (see [`dict_to_write_row`]).
pub fn py_postgres_write_inner(
    stream: &Rc<dyn Stream<PyElement>>,
    conn_str: String,
    table: String,
    columns: Vec<(String, String)>,
) -> Rc<dyn Node> {
    let conn = PostgresConnection::new(conn_str);

    let burst_stream: Rc<dyn Stream<Burst<PyPgWriteRow>>> = stream.try_map(move |elem| {
        Python::attach(|py| -> anyhow::Result<Burst<PyPgWriteRow>> {
            let obj = elem.as_ref().bind(py);
            if let Ok(dict) = obj.cast_exact::<PyDict>() {
                Ok(std::iter::once(dict_to_write_row(dict, &columns)?).collect())
            } else if let Ok(list) = obj.cast_exact::<PyList>() {
                list.iter()
                    .map(|item| {
                        let dict = item.cast_exact::<PyDict>().map_err(|_| {
                            anyhow::anyhow!(
                                "postgres_write: list items must be dicts, got {}",
                                item.get_type()
                            )
                        })?;
                        dict_to_write_row(dict, &columns)
                    })
                    .collect::<anyhow::Result<Burst<PyPgWriteRow>>>()
            } else {
                bail!(
                    "postgres_write: stream value must be a dict or list of dicts, got {}",
                    obj.get_type()
                );
            }
        })
    });

    postgres_write(conn, table, &burst_stream)
}

/// Write this stream to a PostgreSQL table.
///
/// See [`PyStream::postgres_write`] — this pyfunction form exists for symmetry with
/// `py_postgres_read`; end users normally call the `.postgres_write()` method.
#[pyfunction]
#[pyo3(signature = (conn_str, table, columns, upstream))]
pub fn py_postgres_write(
    conn_str: String,
    table: String,
    columns: Vec<(String, String)>,
    upstream: &PyStream,
) -> PyNode {
    PyNode::new(py_postgres_write_inner(
        &upstream.0,
        conn_str,
        table,
        columns,
    ))
}