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 = match config.args.get("value") {
75            Some(value) => {
76                let Some(column) = context.column() else {
77                    let mut bag = DiagnosticBag::default();
78                    bag.error(
79                        crate::diagnostic::codes::CONSTANT_INVALID_VALUE.code,
80                        context.path(),
81                        "constant generator requires a target column",
82                    );
83                    return Err(bag);
84                };
85                core::coerce_value(value, &column.family).map_err(|message| {
86                    let mut bag = DiagnosticBag::default();
87                    bag.error(
88                        crate::diagnostic::codes::CONSTANT_INVALID_VALUE.code,
89                        context.path(),
90                        message,
91                    );
92                    bag
93                })?
94            }
95            None => GeneratedValue::Null,
96        };
97        Ok(Box::new(CompiledConstant { value }))
98    }
99}
100
101/// The compiled form of [`ConstantFactory`]: a captured value replayed per row.
102struct CompiledConstant {
103    value: GeneratedValue,
104}
105
106impl CompiledGenerator for CompiledConstant {
107    fn generate(
108        &mut self,
109        _context: &RowContext<'_>,
110        output: &mut GeneratedValue,
111    ) -> Result<(), GenerateError> {
112        output.clone_from(&self.value);
113        Ok(())
114    }
115}