Skip to main content

sql_splitter/generate/generators/
mod.rs

1//! Built-in generator factories.
2//!
3//! This module is the home of the generator catalog. [`ConstantFactory`]
4//! provides the minimal factory/compiled exemplar. [`core`] provides the
5//! literal and structural generators (`null`, `sequence`,
6//! `copy`, `template`, `pattern`, `database_default`, `json_value`), the
7//! typed random generators (`integer`, `decimal`, `boolean`, `string`,
8//! `bytes`, `uuid`), `choice`/`weighted_choice`, and the built-in modifiers.
9//! [`semantic`] provides semantic, temporal, and credential generators;
10//! [`relation`] provides relationship-aware generators.
11
12mod core;
13pub(crate) mod observed;
14pub(crate) mod relation;
15pub(crate) mod semantic;
16
17pub(crate) use core::register_all;
18
19use crate::diagnostic::DiagnosticBag;
20use crate::synthetic::model::GeneratorConfig;
21use crate::synthetic::schema::SqlTypeFamily;
22
23use super::registry::{
24    ArgumentSpec, Buffering, ColumnScope, CompileContext, CompiledGenerator, Determinism,
25    GeneratorDescriptor, GeneratorFactory, RowContext, Verification,
26};
27use super::value::{GenerateError, GeneratedValue};
28
29/// The `constant` generator: emits the same configured value for every row.
30///
31/// It is the minimal end-to-end exemplar of the factory/compiled split — a
32/// factory that reads one optional `value` argument and compiles it into a
33/// [`CompiledConstant`] that overwrites the output slot with a fixed value.
34pub struct ConstantFactory;
35
36static CONSTANT_DESCRIPTOR: GeneratorDescriptor = GeneratorDescriptor {
37    kind: "constant",
38    aliases: &["const"],
39    summary: "Emits the same configured value for every row.",
40    arguments: &[ArgumentSpec {
41        name: "value",
42        required: false,
43        summary: "The constant value to emit; SQL NULL when omitted.",
44    }],
45    accepts: &[
46        SqlTypeFamily::Integer,
47        SqlTypeFamily::BigInteger,
48        SqlTypeFamily::Decimal,
49        SqlTypeFamily::Boolean,
50        SqlTypeFamily::Text,
51        SqlTypeFamily::Bytes,
52        SqlTypeFamily::Uuid,
53        SqlTypeFamily::DateTime,
54        SqlTypeFamily::Json,
55        SqlTypeFamily::Other,
56    ],
57    writes: ColumnScope::OwnColumn,
58    reads: ColumnScope::None,
59    determinism: Determinism::Deterministic,
60    buffering: Buffering::Streaming,
61    verification: Verification::Unsupported,
62};
63
64impl GeneratorFactory for ConstantFactory {
65    fn descriptor(&self) -> &'static GeneratorDescriptor {
66        &CONSTANT_DESCRIPTOR
67    }
68
69    fn compile(
70        &self,
71        config: &GeneratorConfig,
72        _context: &CompileContext<'_>,
73    ) -> Result<Box<dyn CompiledGenerator>, DiagnosticBag> {
74        let value = config
75            .args
76            .get("value")
77            .map_or(GeneratedValue::Null, yaml_to_value);
78        Ok(Box::new(CompiledConstant { value }))
79    }
80}
81
82/// The compiled form of [`ConstantFactory`]: a captured value replayed per row.
83struct CompiledConstant {
84    value: GeneratedValue,
85}
86
87impl CompiledGenerator for CompiledConstant {
88    fn generate(
89        &mut self,
90        _context: &RowContext<'_>,
91        output: &mut GeneratedValue,
92    ) -> Result<(), GenerateError> {
93        output.clone_from(&self.value);
94        Ok(())
95    }
96}
97
98/// Minimal YAML → [`GeneratedValue`] mapping for the constant exemplar.
99///
100/// Only the scalar shapes a hand-authored `value:` is likely to use are
101/// mapped. Family-aware coercion for decimals with scale, byte literals, and
102/// typed date/time values is outside this constant-specific helper.
103fn yaml_to_value(value: &serde_yaml_ng::Value) -> GeneratedValue {
104    match value {
105        serde_yaml_ng::Value::Null => GeneratedValue::Null,
106        serde_yaml_ng::Value::Bool(b) => GeneratedValue::Boolean(*b),
107        serde_yaml_ng::Value::Number(n) => match n.as_i64() {
108            Some(i) => GeneratedValue::Integer(i128::from(i)),
109            None => GeneratedValue::Text(n.to_string()),
110        },
111        serde_yaml_ng::Value::String(s) => GeneratedValue::Text(s.clone()),
112        other => GeneratedValue::Text(
113            serde_yaml_ng::to_string(other)
114                .unwrap_or_default()
115                .trim_end()
116                .to_string(),
117        ),
118    }
119}