sql_splitter/generate/generators/
mod.rs1mod 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
29pub 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
82struct 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
98fn 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}