Skip to main content

sql_splitter/profile/
evidence.rs

1//! Neutral profiling evidence: observations and confidence only.
2//!
3//! These types record *what was observed* in a dump — counts, bounded
4//! summaries, and sketched estimates — with no opinion about how to regenerate
5//! it. Nothing here names a generator kind (no "email", no "faker"); mapping
6//! evidence to generation strategies is a separate concern. Inference
7//! heuristics consume these structs without depending on the accumulator
8//! internals that produced them.
9//!
10//! Every summary is bounded: a column that scans a billion rows produces the
11//! same size evidence as one that scans a thousand. Raw values are never
12//! retained beyond a small, budget-capped sample.
13
14use crate::diagnostic::Diagnostic;
15use crate::profile::ProfileDepth;
16use crate::synthetic::schema::PortableSchema;
17use serde::{Deserialize, Serialize};
18
19/// Top-level profile of a whole dump.
20#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
21pub struct DumpProfile {
22    /// How deep the profiler was asked to look.
23    pub depth: ProfileDepth,
24    /// The dump's DDL schema, captured in the same single pass that gathered
25    /// the value evidence. A portable, dialect-neutral snapshot so consumers
26    /// (inference) never have to re-read the dump to recover its schema.
27    pub schema: PortableSchema,
28    /// Per-table evidence, in discovery order.
29    pub tables: Vec<TableEvidence>,
30    /// Structured diagnostics raised while profiling (e.g.
31    /// `GEN-PROFILE-SCHEMA-LATE` when data preceded its table's DDL by more than
32    /// the retained sample). Empty on a clean, schema-before-data dump.
33    #[serde(default, skip_serializing_if = "Vec::is_empty")]
34    pub warnings: Vec<Diagnostic>,
35}
36
37/// Evidence gathered for a single table.
38#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
39pub struct TableEvidence {
40    /// Table name as it appeared in the dump.
41    pub table: String,
42    /// Observed row count, when the profiler counted rows.
43    pub row_count: Option<u64>,
44    /// Per-column evidence, in column order.
45    pub columns: Vec<ColumnEvidence>,
46    /// Observed relationships to other tables.
47    pub relationships: Vec<RelationshipEvidence>,
48    /// Confidence in this table's evidence, in `[0, 1]`.
49    pub confidence: f64,
50}
51
52/// Evidence gathered for a single column. Only the sub-summaries relevant to
53/// the observed value shapes are populated; the rest stay `None`.
54#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
55pub struct ColumnEvidence {
56    /// Column name (filled in by the profiler; empty when produced directly
57    /// from a bare [`crate::profile::ColumnSketches`]).
58    pub name: String,
59    /// Total values observed, including nulls.
60    pub total_count: u64,
61    /// How many observed values were SQL `NULL`.
62    pub null_count: u64,
63    /// `null_count / total_count`, or `0.0` when nothing was observed.
64    pub null_rate: f64,
65    /// HyperLogLog estimate of the number of distinct non-null values.
66    pub distinct_estimate: f64,
67    /// A small, budget-bounded sample of retained value renderings.
68    pub sample_values: Vec<String>,
69    /// How many retained samples were byte-truncated to fit the ceiling.
70    pub truncated_sample_count: u64,
71    /// Present when boolean values were observed.
72    pub boolean: Option<BooleanEvidence>,
73    /// Present when numeric (integer/decimal) values were observed.
74    pub numeric: Option<NumericEvidence>,
75    /// Maximum decimal scale observed, when decimals were observed.
76    pub decimal_scale: Option<u8>,
77    /// Present when text values were observed.
78    pub string_shape: Option<StringShapeEvidence>,
79    /// Heavy hitters (approximate top-k), most frequent first.
80    pub top_k: Vec<TopKEntry>,
81    /// Present when timestamp values were observed.
82    pub timestamp_range: Option<TimestampRangeEvidence>,
83    /// Fraction of observed JSON values that parsed as valid JSON.
84    pub json_valid_rate: Option<f64>,
85    /// Confidence in this column's evidence, in `[0, 1]`.
86    pub confidence: f64,
87}
88
89/// True/false tallies for a boolean-valued column.
90#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
91pub struct BooleanEvidence {
92    pub true_count: u64,
93    pub false_count: u64,
94}
95
96/// Bounded numeric summary: extent, mean, and interpolated quantiles from a
97/// streaming histogram.
98#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
99pub struct NumericEvidence {
100    pub min: f64,
101    pub max: f64,
102    pub mean: f64,
103    pub p50: f64,
104    pub p90: f64,
105    pub p99: f64,
106}
107
108/// Which character classes appeared anywhere in the observed strings.
109#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
110pub struct CharClasses {
111    pub lower: bool,
112    pub upper: bool,
113    pub digit: bool,
114    pub whitespace: bool,
115    pub punctuation: bool,
116    pub non_ascii: bool,
117}
118
119/// Shape of a text column: lengths, empty rate, alphabet, and the longest
120/// common prefix/suffix shared by every observed value.
121#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
122pub struct StringShapeEvidence {
123    pub count: u64,
124    pub empty_count: u64,
125    pub empty_rate: f64,
126    pub min_len: usize,
127    pub max_len: usize,
128    pub mean_len: f64,
129    pub classes: CharClasses,
130    pub common_prefix: String,
131    pub common_suffix: String,
132    /// Whether the prefix/suffix was capped at the affix byte ceiling.
133    pub truncated_affix: bool,
134}
135
136/// One approximate heavy hitter: a value, its (over)estimated count, and the
137/// maximum possible overestimate.
138#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
139pub struct TopKEntry {
140    pub value: String,
141    pub count: u64,
142    pub error: u64,
143}
144
145/// Lexical min/max of observed timestamp strings (ISO-8601 sorts lexically).
146#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
147pub struct TimestampRangeEvidence {
148    pub min: String,
149    pub max: String,
150}
151
152/// Evidence about an observed relationship between two columns.
153///
154/// The profiler does not infer relationships from observed values; this type
155/// keeps the neutral evidence surface complete and stable for downstream
156/// consumers.
157#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
158pub struct RelationshipEvidence {
159    pub from_column: String,
160    pub to_table: String,
161    pub to_column: String,
162    /// Fraction of child values found in the parent, in `[0, 1]`.
163    pub coverage: f64,
164    /// Confidence in this relationship, in `[0, 1]`.
165    pub confidence: f64,
166}