pyro-spec 0.2.0

Schema and specs for functions and rows in pyroduct
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
//! Pyro-native type system and schema representation.
//!
//! This crate provides a lightweight schema representation optimized for
//! `PyroValue`. Unlike Arrow's `DataType` (which has ~40 variants for timestamps,
//! decimals, run-end-encoded, etc.), `PyroType` mirrors *exactly* the variants that
//! `PyroValue` can represent, making match arms exhaustive and tiny.
//!
//! Core types:
//! - [`PyroType`] — the main type enum, mirroring `PyroValue` discriminants 1:1
//! - [`PyroField`] — a named, nullable column descriptor (equivalent to Arrow `Field`)
//! - [`PyroSchema`] — an ordered collection of `PyroField`s (equivalent to Arrow `Schema`)
//! - [`coerce_pyro_types`] — type coercion to find common supertypes
//!
//! Conversion to/from `arrow::datatypes::DataType` lives in the `arrow` module
//! (behind the `arrow` feature flag).

// =============================================================================
// Pyro-native type system
// =============================================================================
//
// A lightweight schema representation optimized for PyroValue.
// Unlike Arrow's DataType (which has ~40 variants for timestamps, decimals,
// run-end-encoded, etc.), PyroType mirrors *exactly* the variants that
// PyroValue can represent, making match arms exhaustive and tiny.
//
// Conversion to/from `arrow::datatypes::DataType` lives in `value::arrow::schema`.

#[cfg(feature = "arrow")]
mod arrow;

use std::borrow::Cow;
use std::collections::BTreeMap;
use std::fmt;

use serde::{Deserialize, Serialize};

/// The kind of module execution model
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ModuleKind {
    #[default]
    Normal,
    Session,
    SessionDiff,
}

/// Documentation for the main function of a module
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ModuleFunc<'a> {
    pub name: Cow<'a, str>,
    pub description: Option<Cow<'a, str>>,
    pub input: PyroSchema<'a>,
    pub output: PyroSchema<'a>,
    #[serde(default)]
    pub kind: ModuleKind,
}

/// The root specification object.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct InterfaceSpec<'a> {
    pub capability: Cow<'a, str>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<Cow<'a, str>>,

    pub classes: Vec<ClassSpec<'a>>,

    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub structs: BTreeMap<Cow<'a, str>, PyroSchema<'a>>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ClassSpec<'a> {
    pub name: Cow<'a, str>,
    pub description: Option<Cow<'a, str>>,
    pub methods: Vec<CapabilityFunc<'a>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub client: Option<PyroSchema<'a>>,
    pub config: Option<PyroSchema<'a>>,
}

/// Documentation for a capability function
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CapabilityFunc<'a> {
    pub name: Cow<'a, str>,
    pub description: Option<Cow<'a, str>>,
    pub input: PyroSchema<'a>,
    pub output: PyroType<'a>,
}

// =============================================================================
// PyroType
// =============================================================================

/// A data type enum that mirrors the variants of [`PyroValue`] exactly.
///
/// This is intentionally much smaller than `arrow::datatypes::DataType`.
/// Every variant here has a 1:1 correspondence with a `PyroValue` discriminant.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PyroType<'a> {
    /// No value / unknown type (corresponds to `PyroValue::Null`).
    Null,
    /// Scalar primitive (Bool, Int, Float).
    PrimitiveScalar(PrimitiveDataType),
    /// UTF-8 string (corresponds to `PyroValue::Str`).
    Str,
    /// Day + millisecond interval (corresponds to `PyroValue::Timestamp`).
    Timestamp,
    /// Homogeneous list of a single primitive type (corresponds to `PyroValue::PrimitiveList`).
    PrimitiveList(PrimitiveDataType),
    /// Fixed-size homogeneous list of a single primitive type.
    PrimitiveFixedList(PrimitiveDataType, usize),
    /// Heterogeneous list of arbitrary pyro values (corresponds to `PyroValue::List`).
    ///
    /// Fields: `(element_type, element_nullable)`.
    List(Box<PyroType<'a>>, bool),
    /// Named struct / row (corresponds to `PyroValue::Group`).
    Group(Cow<'a, [PyroField<'a>]>),
    /// Key-value map (corresponds to `PyroValue::MapInternal`).
    Map {
        key: Box<PyroType<'a>>,
        value: Box<PyroType<'a>>,
    },
}

impl<'a> fmt::Display for PyroType<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            // Primitives
            PyroType::Null => write!(f, "Null"),
            PyroType::PrimitiveScalar(t) => write!(f, "{}", t),
            PyroType::Str => write!(f, "Str"),
            PyroType::Timestamp => write!(f, "Timestamp"),

            // Complex Types
            PyroType::PrimitiveList(inner_type) => {
                write!(f, "[{}]", inner_type)
            }
            PyroType::PrimitiveFixedList(inner_type, len) => {
                write!(f, "[{}; {}]", inner_type, len)
            }
            PyroType::List(inner_type, _nullable) => {
                write!(f, "[{}]", inner_type)
            }
            PyroType::Group(fields) => {
                write!(f, "{{ ")?;
                for (i, field) in fields.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{}: {}", field.name, field.data_type)?;
                }
                write!(f, " }}")
            }
            PyroType::Map { key, value } => {
                write!(f, "Map<{}, {}>", key, value)
            }
        }
    }
}

/// The primitive element type inside a `PrimitiveValueList`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PrimitiveDataType {
    Bool,
    U8,
    U16,
    U32,
    U64,
    I8,
    I16,
    I32,
    I64,
    F16,
    F32,
    F64,
}

impl fmt::Display for PrimitiveDataType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PrimitiveDataType::Bool => write!(f, "Bool"),
            PrimitiveDataType::U8 => write!(f, "U8"),
            PrimitiveDataType::U16 => write!(f, "U16"),
            PrimitiveDataType::U32 => write!(f, "U32"),
            PrimitiveDataType::U64 => write!(f, "U64"),
            PrimitiveDataType::I8 => write!(f, "I8"),
            PrimitiveDataType::I16 => write!(f, "I16"),
            PrimitiveDataType::I32 => write!(f, "I32"),
            PrimitiveDataType::I64 => write!(f, "I64"),
            PrimitiveDataType::F16 => write!(f, "F16"),
            PrimitiveDataType::F32 => write!(f, "F32"),
            PrimitiveDataType::F64 => write!(f, "F64"),
        }
    }
}

impl<'a> PyroType<'a> {
    pub fn into_owned(self) -> PyroType<'static> {
        match self {
            PyroType::Null => PyroType::Null,
            PyroType::PrimitiveScalar(p) => PyroType::PrimitiveScalar(p),
            PyroType::Str => PyroType::Str,
            PyroType::Timestamp => PyroType::Timestamp,
            PyroType::PrimitiveList(p) => PyroType::PrimitiveList(p),
            PyroType::PrimitiveFixedList(p, l) => PyroType::PrimitiveFixedList(p, l),
            PyroType::List(inner, n) => PyroType::List(Box::new(inner.into_owned()), n),
            PyroType::Group(fields) => {
                let owned_fields: Vec<PyroField<'static>> =
                    fields.iter().map(|f| f.clone().into_owned()).collect();
                PyroType::Group(Cow::Owned(owned_fields))
            }
            PyroType::Map { key, value } => PyroType::Map {
                key: Box::new(key.into_owned()),
                value: Box::new(value.into_owned()),
            },
        }
    }
}

// =============================================================================
// PyroField
// =============================================================================

/// A named, nullable column descriptor — the Pyro equivalent of `arrow::datatypes::Field`.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct PyroField<'a> {
    pub name: Cow<'a, str>,
    pub documentation: Option<Cow<'a, str>>,
    pub data_type: PyroType<'a>,
    pub nullable: bool,
}

impl<'a> PyroField<'a> {
    /// Create a new field.
    /// Accepts `&'static str`, `String`, or `Cow<'a, str>`.
    pub fn new(name: impl Into<Cow<'a, str>>, data_type: PyroType<'a>, nullable: bool) -> Self {
        Self {
            name: name.into(),
            documentation: None,
            data_type,
            nullable,
        }
    }

    #[inline]
    pub fn name(&self) -> &str {
        &self.name
    }

    #[inline]
    pub fn data_type(&self) -> &PyroType<'a> {
        &self.data_type
    }

    #[inline]
    pub fn is_nullable(&self) -> bool {
        self.nullable
    }

    pub fn with_nullable(mut self, nullable: bool) -> Self {
        self.nullable = nullable;
        self
    }

    /// Convert to an owned version (PyroField<'static>) by cloning data.
    pub fn into_owned(self) -> PyroField<'static> {
        PyroField {
            name: Cow::Owned(self.name.into_owned()),
            documentation: self.documentation.map(|d| Cow::Owned(d.into_owned())),
            data_type: self.data_type.into_owned(),
            nullable: self.nullable,
        }
    }

    pub fn add_docstring(mut self, doc: impl Into<Cow<'a, str>>) -> Self {
        self.documentation = Some(doc.into());
        self
    }
}

impl<'a> fmt::Display for PyroField<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}: {:?}{}",
            self.name,
            self.data_type,
            if self.nullable { " (nullable)" } else { "" }
        )
    }
}

// =============================================================================
// PyroSchema
// =============================================================================

/// An ordered collection of [`PyroField`]s — the Pyro equivalent of `arrow::datatypes::Schema`.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct PyroSchema<'a> {
    pub documentation: Option<Cow<'a, str>>,
    pub fields: Cow<'a, [PyroField<'a>]>,
}

impl<'a> PyroSchema<'a> {
    pub fn new(fields: Vec<PyroField<'a>>) -> Self {
        Self {
            documentation: None,
            fields: Cow::Owned(fields),
        }
    }

    pub fn empty() -> Self {
        Self {
            documentation: None,
            fields: Cow::Owned(Vec::new()),
        }
    }

    #[inline]
    pub fn fields(&self) -> &[PyroField<'a>] {
        &self.fields
    }

    #[inline]
    pub fn num_fields(&self) -> usize {
        self.fields.len()
    }

    /// Look up a field by name (linear scan).
    pub fn field_with_name(&self, name: &str) -> Option<&PyroField<'a>> {
        self.fields.iter().find(|f| f.name == name)
    }

    /// Get a field by index.
    pub fn field(&self, index: usize) -> &PyroField<'a> {
        &self.fields[index]
    }

    /// Returns column index for the given name, if present.
    pub fn index_of(&self, name: &str) -> Option<usize> {
        self.fields.iter().position(|f| f.name == name)
    }

    /// Convert to an fully owned schema (useful for inference results).
    pub fn into_owned(self) -> PyroSchema<'static> {
        PyroSchema {
            documentation: self.documentation.map(|d| Cow::Owned(d.into_owned())),
            fields: self.fields.iter().map(|f| f.clone().into_owned()).collect(),
        }
    }

    pub fn add_docstring(mut self, doc: impl Into<Cow<'a, str>>) -> Self {
        self.documentation = Some(doc.into());
        self
    }
}

impl<'a> fmt::Display for PyroSchema<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "PyroSchema {{")?;
        for field in self.fields.iter() {
            writeln!(f, "  {field},")?;
        }
        write!(f, "}}")
    }
}

impl<'a> From<Vec<PyroField<'a>>> for PyroSchema<'a> {
    fn from(fields: Vec<PyroField<'a>>) -> Self {
        Self::new(fields)
    }
}

/// Coerce two [`PyroType`]s to a common supertype. Returns `None` if incompatible.
pub fn coerce_pyro_types<'a>(a: &PyroType<'a>, b: &PyroType<'a>) -> Option<PyroType<'a>> {
    if a == b {
        return Some(a.clone());
    }

    use PyroType::*;

    match (a, b) {
        // Null widens to anything
        (Null, other) | (other, Null) => Some(other.clone()),

        // --- Primitive Scalar coercion ---
        (PrimitiveScalar(pa), PrimitiveScalar(pb)) => {
            coerce_primitive_types(*pa, *pb).map(PrimitiveScalar)
        }

        // --- List coercion (merge nullability) ---
        (List(inner_a, null_a), List(inner_b, null_b)) => {
            let merged_null = *null_a || *null_b;
            coerce_pyro_types(inner_a, inner_b).map(|c| List(Box::new(c), merged_null))
        }

        // --- PrimitiveList coercion ---
        (PrimitiveList(pa), PrimitiveList(pb)) => {
            coerce_primitive_types(*pa, *pb).map(PrimitiveList)
        }

        // --- PrimitiveFixedList coercion ---
        // Same size + coercible element type → PrimitiveFixedList
        // Different size → promote to PrimitiveList
        (PrimitiveFixedList(pa, sa), PrimitiveFixedList(pb, sb)) => {
            let coerced_elem = coerce_primitive_types(*pa, *pb)?;
            if sa == sb {
                Some(PrimitiveFixedList(coerced_elem, *sa))
            } else {
                Some(PrimitiveList(coerced_elem))
            }
        }

        // PrimitiveFixedList + PrimitiveList → PrimitiveList
        (PrimitiveFixedList(pa, _), PrimitiveList(pb))
        | (PrimitiveList(pa), PrimitiveFixedList(pb, _)) => {
            coerce_primitive_types(*pa, *pb).map(PrimitiveList)
        }

        // --- Group (struct) coercion: merge fields ---
        (Group(fields_a), Group(fields_b)) => {
            let mut merged_map: BTreeMap<String, PyroField> = BTreeMap::new();

            for f in fields_a.iter().chain(fields_b.iter()) {
                match merged_map.get(f.name()) {
                    None => {
                        // Field only in one side so far — mark nullable since the other side lacks it
                        merged_map.insert(
                            f.name().to_string(),
                            PyroField::new(
                                Cow::Owned(f.name().to_string()),
                                f.data_type().clone(),
                                true,
                            ),
                        );
                    }
                    Some(existing) => {
                        let coerced = coerce_pyro_types(existing.data_type(), f.data_type())?;
                        let nullable = existing.is_nullable() || f.is_nullable();
                        merged_map.insert(
                            f.name().to_string(),
                            PyroField::new(Cow::Owned(f.name().to_string()), coerced, nullable),
                        );
                    }
                }
            }

            Some(Group(Cow::Owned(merged_map.into_values().collect())))
        }

        // --- Map Coercion ---
        (Map { key: ka, value: va }, Map { key: kb, value: vb }) => {
            let coerced_key = coerce_pyro_types(ka, kb)?;
            let coerced_val = coerce_pyro_types(va, vb)?;
            Some(Map {
                key: Box::new(coerced_key),
                value: Box::new(coerced_val),
            })
        }

        _ => None,
    }
}

fn coerce_primitive_types(a: PrimitiveDataType, b: PrimitiveDataType) -> Option<PrimitiveDataType> {
    if a == b {
        return Some(a);
    }

    use PrimitiveDataType as P;

    match (a, b) {
        (P::I8, P::I16) | (P::I16, P::I8) => Some(P::I16),
        (P::I8, P::I32) | (P::I32, P::I8) => Some(P::I32),
        (P::I8, P::I64) | (P::I64, P::I8) => Some(P::I64),
        (P::I16, P::I32) | (P::I32, P::I16) => Some(P::I32),
        (P::I16, P::I64) | (P::I64, P::I16) => Some(P::I64),
        (P::I32, P::I64) | (P::I64, P::I32) => Some(P::I64),

        (P::U8, P::U16) | (P::U16, P::U8) => Some(P::U16),
        (P::U8, P::U32) | (P::U32, P::U8) => Some(P::U32),
        (P::U8, P::U64) | (P::U64, P::U8) => Some(P::U64),
        (P::U16, P::U32) | (P::U32, P::U16) => Some(P::U32),
        (P::U16, P::U64) | (P::U64, P::U16) => Some(P::U64),
        (P::U32, P::U64) | (P::U64, P::U32) => Some(P::U64),

        (P::F16, P::F32) | (P::F32, P::F16) => Some(P::F32),
        (P::F32, P::F64) | (P::F64, P::F32) => Some(P::F64),
        (P::F16, P::F64) | (P::F64, P::F16) => Some(P::F64),

        // --- Int to Float promotion ---
        (P::I8 | P::I16 | P::I32 | P::I64, P::F64) | (P::F64, P::I8 | P::I16 | P::I32 | P::I64) => {
            Some(P::F64)
        }
        (P::U8 | P::U16 | P::U32 | P::U64, P::F64) | (P::F64, P::U8 | P::U16 | P::U32 | P::U64) => {
            Some(P::F64)
        }

        _ => None,
    }
}