use std::collections::HashSet;
use std::fmt;
use std::fs::File;
use std::io;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use clap::{Args, ValueHint};
use schemars::JsonSchema;
use serde::Serialize;
use tempfile::NamedTempFile;
use crate::diagnostic::{DiagnosticBag, Severity};
use crate::generate::{
CompileOptions, ExplainColumn, Generate, GenerateError, GenerateReport, GenerateRequest,
OutputTarget, RenderOptions, RunMode, SourceOptions, TableCountOverride,
};
use crate::parser::SqlDialect;
use crate::profile::ProfileDepth;
use crate::synthetic::{OutputMode, MAX_OUTPUT_BATCH_SIZE};
use super::common::{dash_is_stdout, FILTERING};
const INPUT_OUTPUT: &str = "Input/Model";
const VOLUME: &str = "Volume";
const RANDOMNESS: &str = "Randomness";
const RENDERING: &str = "Rendering";
const PREFLIGHT: &str = "Preflight/Reporting";
#[derive(clap::ValueEnum, Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum ProfileDepthArg {
#[default]
Basic,
Full,
}
impl ProfileDepthArg {
fn to_depth(self) -> ProfileDepth {
match self {
ProfileDepthArg::Basic => ProfileDepth::Basic,
ProfileDepthArg::Full => ProfileDepth::Full,
}
}
}
#[derive(clap::ValueEnum, Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompressFormat {
Gzip,
Bzip2,
Xz,
Zstd,
}
#[derive(Args)]
pub struct GenerateArgs {
#[arg(value_hint = ValueHint::FilePath, help_heading = INPUT_OUTPUT)]
input: Option<PathBuf>,
#[arg(short, long, value_hint = ValueHint::FilePath, help_heading = INPUT_OUTPUT)]
config: Option<PathBuf>,
#[arg(long, value_hint = ValueHint::FilePath, help_heading = INPUT_OUTPUT)]
emit_config: Option<PathBuf>,
#[arg(short, long, value_hint = ValueHint::FilePath, help_heading = INPUT_OUTPUT)]
output: Option<PathBuf>,
#[arg(long, value_enum, default_value_t = ProfileDepthArg::Basic, help_heading = INPUT_OUTPUT)]
profile_depth: ProfileDepthArg,
#[arg(long, help_heading = INPUT_OUTPUT)]
profile_sample: Option<usize>,
#[arg(long, help_heading = INPUT_OUTPUT)]
input_dialect: Option<SqlDialect>,
#[arg(long, help_heading = RENDERING)]
dialect: Option<SqlDialect>,
#[arg(long, conflicts_with = "rows", help_heading = VOLUME)]
scale: Option<f64>,
#[arg(long, conflicts_with = "scale", help_heading = VOLUME)]
rows: Option<u64>,
#[arg(long = "table-rows", help_heading = VOLUME)]
table_rows: Vec<String>,
#[arg(long = "table-scale", help_heading = VOLUME)]
table_scale: Vec<String>,
#[arg(long, help_heading = VOLUME)]
max_rows: Option<u64>,
#[arg(long, value_delimiter = ',', help_heading = FILTERING)]
tables: Vec<String>,
#[arg(long, value_delimiter = ',', help_heading = FILTERING)]
exclude: Vec<String>,
#[arg(long, conflicts_with = "randomize", help_heading = RANDOMNESS)]
seed: Option<u64>,
#[arg(long, conflicts_with = "seed", help_heading = RANDOMNESS)]
randomize: bool,
#[arg(long, conflicts_with = "data_only", help_heading = RENDERING)]
schema_only: bool,
#[arg(long, conflicts_with = "schema_only", help_heading = RENDERING)]
data_only: bool,
#[arg(long, default_value_t = 1_000, help_heading = RENDERING)]
batch_size: usize,
#[arg(long, help_heading = RENDERING)]
no_copy: bool,
#[arg(long, value_enum, help_heading = RENDERING)]
compress: Option<CompressFormat>,
#[arg(long, help_heading = RENDERING)]
mssql_production_style: bool,
#[arg(long, value_parser = clap::value_parser!(u64).range(1..), help_heading = RENDERING)]
mssql_go: Option<u64>,
#[arg(long, conflicts_with_all = ["dry_run", "verify"], help_heading = PREFLIGHT)]
check: bool,
#[arg(long, conflicts_with_all = ["check", "verify"], help_heading = PREFLIGHT)]
dry_run: bool,
#[arg(long, conflicts_with_all = ["check", "dry_run"], help_heading = PREFLIGHT)]
verify: bool,
#[arg(long, help_heading = PREFLIGHT)]
explain: bool,
#[arg(long, help_heading = PREFLIGHT)]
strict: bool,
#[arg(long, conflicts_with = "quiet", help_heading = PREFLIGHT)]
progress: bool,
#[arg(long, help_heading = PREFLIGHT)]
json: bool,
#[arg(long, conflicts_with = "progress", help_heading = PREFLIGHT)]
quiet: bool,
}
struct PreparedRequest {
request: GenerateRequest,
mode: RunMode,
explain: bool,
json: bool,
quiet: bool,
stdout_temp: Option<NamedTempFile>,
emit_stdout_temp: Option<NamedTempFile>,
}
#[derive(Debug)]
enum RequestError {
Usage(String),
Unavailable(String),
}
impl RequestError {
fn usage(message: impl Into<String>) -> Self {
RequestError::Usage(message.into())
}
fn unavailable(message: impl Into<String>) -> Self {
RequestError::Unavailable(message.into())
}
}
impl fmt::Display for RequestError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RequestError::Usage(message) | RequestError::Unavailable(message) => {
write!(f, "{message}")
}
}
}
}
impl std::error::Error for RequestError {}
impl GenerateArgs {
fn try_into_request(self) -> Result<PreparedRequest, RequestError> {
if self.check && self.input.is_some() {
return Err(RequestError::usage(
"--check requires a complete `--config` model and cannot profile an input dump; \
omit `[INPUT]`",
));
}
if self.check && self.config.is_none() {
return Err(RequestError::usage(
"--check requires a complete model supplied with `--config <PATH>`",
));
}
let mode = if self.check {
RunMode::Check
} else if self.dry_run {
RunMode::DryRun
} else if self.emit_config.is_some() && self.output.is_none() {
RunMode::EmitModel
} else {
RunMode::Generate
};
let output_explicit_dash = is_dash(&self.output);
let emit_config_dash = is_dash(&self.emit_config);
let sql_wants_stdout = mode == RunMode::Generate
&& !self.json
&& (self.output.is_none() || output_explicit_dash);
if self.json && output_explicit_dash {
return Err(RequestError::usage(
"--json and `--output -` both claim stdout; choose one",
));
}
if self.json && emit_config_dash {
return Err(RequestError::usage(
"--json and `--emit-config -` both claim stdout; choose one",
));
}
if mode == RunMode::Generate && self.json && self.output.is_none() {
return Err(RequestError::usage(
"--json in generate mode writes only the report to stdout; add `-o <path>` for the generated SQL, or use --check/--dry-run for a report-only run",
));
}
if sql_wants_stdout && emit_config_dash {
return Err(RequestError::usage(
"generated SQL and `--emit-config -` both claim stdout; choose one",
));
}
if self.verify {
let has_file_output = self
.output
.as_ref()
.is_some_and(|path| path.as_os_str() != "-");
if !has_file_output {
return Err(RequestError::usage(
"--verify requires a real output file (`-o <path>`), not stdout",
));
}
}
if self.batch_size == 0 {
return Err(RequestError::usage("--batch-size must be at least 1"));
}
if self.batch_size > MAX_OUTPUT_BATCH_SIZE as usize {
return Err(RequestError::usage(format!(
"--batch-size must be at most {MAX_OUTPUT_BATCH_SIZE}"
)));
}
if self.compress.is_some() && (self.output.is_none() || output_explicit_dash) {
return Err(RequestError::usage(
"--compress requires a real output file, not stdout",
));
}
let mut table_rows = Vec::with_capacity(self.table_rows.len());
let mut rows_tables = HashSet::new();
for raw in &self.table_rows {
let (table, value) = split_table_override(raw).ok_or_else(|| {
RequestError::usage(format!("--table-rows `{raw}` must be `table=count`"))
})?;
let count: u64 = value.parse().map_err(|_| {
RequestError::usage(format!("--table-rows `{raw}` has a non-numeric count"))
})?;
rows_tables.insert(table.to_string());
table_rows.push(TableCountOverride::rows(table, count));
}
let mut scale_tables = HashSet::new();
for raw in &self.table_scale {
let (table, value) = split_table_override(raw).ok_or_else(|| {
RequestError::usage(format!("--table-scale `{raw}` must be `table=factor`"))
})?;
let factor: f64 = value.parse().map_err(|_| {
RequestError::usage(format!("--table-scale `{raw}` has a non-numeric factor"))
})?;
if !factor.is_finite() || factor < 0.0 {
return Err(RequestError::usage(format!(
"--table-scale `{raw}` must be a finite, non-negative number"
)));
}
scale_tables.insert(table.to_string());
table_rows.push(TableCountOverride::scale(table, factor));
}
if let Some(table) = rows_tables.intersection(&scale_tables).next() {
return Err(RequestError::usage(format!(
"table `{table}` is targeted by both --table-rows and --table-scale"
)));
}
let effective_dialect = self.dialect.unwrap_or_default();
if (self.mssql_production_style || self.mssql_go.is_some())
&& effective_dialect != SqlDialect::Mssql
{
return Err(RequestError::usage(
"--mssql-production-style/--mssql-go require an explicit `--dialect mssql`",
));
}
if self.compress.is_some() {
return Err(RequestError::unavailable(
"--compress is not available yet: generated output cannot be compressed",
));
}
let seed = if self.randomize {
Some(rand::random::<u64>())
} else {
self.seed
};
let render_mode = match (self.schema_only, self.data_only) {
(true, false) => OutputMode::SchemaOnly,
(false, true) => OutputMode::DataOnly,
_ => OutputMode::SchemaAndData,
};
let render = RenderOptions {
dialect: effective_dialect,
source_dialect: None,
mode: render_mode,
no_copy: self.no_copy,
batch_size: self.batch_size,
mssql_production_style: self.mssql_production_style,
mssql_go: self.mssql_go,
};
let compile = CompileOptions {
seed,
scale: self.scale,
rows: self.rows,
max_rows: self.max_rows,
table_rows,
tables: self.tables,
exclude: self.exclude,
family_budget_bytes: None,
};
let (output, stdout_temp) = match mode {
RunMode::Check | RunMode::DryRun | RunMode::EmitModel => (OutputTarget::Discard, None),
RunMode::Generate => match dash_is_stdout(self.output) {
Some(path) => (OutputTarget::Path(path), None),
None if self.json => (OutputTarget::Discard, None),
None => {
let temp = NamedTempFile::new().map_err(|error| {
RequestError::unavailable(format!(
"failed to create a temporary file for stdout output: {error}"
))
})?;
let path = temp.path().to_path_buf();
(OutputTarget::Path(path), Some(temp))
}
},
};
let (emit, emit_stdout_temp) = match self.emit_config {
None => (None, None),
Some(path) if path.as_os_str() == "-" => {
let temp = NamedTempFile::new().map_err(|error| {
RequestError::unavailable(format!(
"failed to create a temporary file for stdout emit-config: {error}"
))
})?;
let emit_path = temp.path().to_path_buf();
(Some(OutputTarget::Path(emit_path)), Some(temp))
}
Some(path) => (Some(OutputTarget::Path(path)), None),
};
let source = SourceOptions {
dialect: self.input_dialect,
depth: Some(self.profile_depth.to_depth()),
sample: self.profile_sample,
};
let request = GenerateRequest {
input: self.input,
config: self.config,
output,
emit,
compile,
render,
output_dialect: self.dialect,
mode,
explain: self.explain,
verify: self.verify,
strict: self.strict,
source,
};
Ok(PreparedRequest {
request,
mode,
explain: self.explain,
emit_stdout_temp,
json: self.json,
quiet: self.quiet,
stdout_temp,
})
}
}
fn is_dash(path: &Option<PathBuf>) -> bool {
path.as_deref().is_some_and(|path| path.as_os_str() == "-")
}
fn split_table_override(raw: &str) -> Option<(&str, &str)> {
let (table, value) = raw.split_once('=')?;
let table = table.trim();
let value = value.trim();
if table.is_empty() || value.is_empty() {
return None;
}
Some((table, value))
}
pub fn run(args: GenerateArgs) -> anyhow::Result<ExitCode> {
let prepared = match args.try_into_request() {
Ok(prepared) => prepared,
Err(RequestError::Usage(message)) => {
eprintln!("error: {message}");
return Ok(ExitCode::from(2));
}
Err(RequestError::Unavailable(message)) => return Err(anyhow::anyhow!(message)),
};
let PreparedRequest {
request,
mode,
explain,
json,
quiet,
stdout_temp,
emit_stdout_temp,
} = prepared;
match Generate::run(request) {
Ok(report) => {
if let Some(temp) = &stdout_temp {
stream_to_stdout(temp.path())?;
}
if let Some(temp) = &emit_stdout_temp {
stream_to_stdout(temp.path())?;
}
write_report(&report, mode, explain, json, quiet, stdout_temp.is_some())?;
Ok(ExitCode::SUCCESS)
}
Err(GenerateError::Diagnostics(bag)) => {
write_diagnostics(&bag, json)?;
Ok(ExitCode::FAILURE)
}
Err(GenerateError::Diagnostic(diagnostic)) => {
write_diagnostics(
&DiagnosticBag {
diagnostics: vec![*diagnostic],
},
json,
)?;
Ok(ExitCode::FAILURE)
}
Err(error) => Err(error.into()),
}
}
fn stream_to_stdout(path: &Path) -> anyhow::Result<()> {
let mut file = File::open(path)?;
io::copy(&mut file, &mut io::stdout())?;
Ok(())
}
fn mode_label(mode: RunMode) -> &'static str {
match mode {
RunMode::Generate => "generate",
RunMode::Check => "check",
RunMode::DryRun => "dry_run",
RunMode::EmitModel => "emit_model",
}
}
fn summary_line(mode: RunMode, rows_written: u64) -> String {
match mode {
RunMode::Generate => format!("Generated {rows_written} row(s)."),
RunMode::Check => "Check passed: the model compiles.".to_string(),
RunMode::DryRun => format!("Dry run: the model would generate {rows_written} row(s)."),
RunMode::EmitModel => "Wrote resolved model.".to_string(),
}
}
fn write_report(
report: &GenerateReport,
mode: RunMode,
explain: bool,
json: bool,
quiet: bool,
sql_on_stdout: bool,
) -> anyhow::Result<()> {
if json {
let payload = GenerateJsonOutput {
mode: mode_label(mode).to_string(),
rows_written: report.rows_written,
effective_seed: report.effective_seed,
diagnostics: diagnostic_entries(&report.diagnostics),
explain: if explain {
explain_entries(&report.explain)
} else {
Vec::new()
},
};
println!("{}", serde_json::to_string_pretty(&payload)?);
return Ok(());
}
if quiet {
for diagnostic in report
.diagnostics
.diagnostics
.iter()
.filter(|diagnostic| diagnostic.severity == Severity::Advisory)
{
eprintln!("{diagnostic}");
}
return Ok(());
}
let line = summary_line(mode, report.rows_written);
if sql_on_stdout {
eprintln!("{line}");
} else {
println!("{line}");
}
for diagnostic in &report.diagnostics.diagnostics {
eprintln!("{diagnostic}");
}
if explain {
print_explain(&report.explain);
}
Ok(())
}
fn print_explain(columns: &[ExplainColumn]) {
for column in columns {
eprintln!(
"explain[{}] {} via {} (confidence {}{})",
column.column,
column.reason,
column.generator_kind,
column.confidence,
if column.source_derived {
", source-derived"
} else {
""
},
);
for rejected in &column.rejected {
eprintln!(
" rejected {} ({}, precedence {}, confidence {})",
rejected.generator_kind, rejected.reason, rejected.precedence, rejected.confidence,
);
}
}
}
fn write_diagnostics(bag: &DiagnosticBag, json: bool) -> anyhow::Result<()> {
if json {
let payload = serde_json::json!({ "diagnostics": diagnostic_entries(bag) });
println!("{}", serde_json::to_string_pretty(&payload)?);
} else {
for diagnostic in &bag.diagnostics {
eprintln!("{diagnostic}");
}
}
Ok(())
}
#[derive(Serialize, JsonSchema)]
pub(crate) struct GenerateJsonOutput {
mode: String,
rows_written: u64,
#[serde(skip_serializing_if = "Option::is_none")]
effective_seed: Option<u64>,
diagnostics: Vec<DiagnosticEntry>,
#[serde(skip_serializing_if = "Vec::is_empty")]
explain: Vec<ExplainEntry>,
}
#[derive(Serialize, JsonSchema)]
pub(crate) struct ExplainEntry {
column: String,
reason: String,
confidence: String,
generator_kind: String,
source_derived: bool,
#[serde(skip_serializing_if = "Vec::is_empty")]
rejected: Vec<ExplainRejectedEntry>,
}
#[derive(Serialize, JsonSchema)]
pub(crate) struct ExplainRejectedEntry {
generator_kind: String,
reason: String,
precedence: String,
confidence: String,
}
fn explain_entries(columns: &[ExplainColumn]) -> Vec<ExplainEntry> {
columns
.iter()
.map(|column| ExplainEntry {
column: column.column.clone(),
reason: column.reason.clone(),
confidence: column.confidence.clone(),
generator_kind: column.generator_kind.clone(),
source_derived: column.source_derived,
rejected: column
.rejected
.iter()
.map(|rejected| ExplainRejectedEntry {
generator_kind: rejected.generator_kind.clone(),
reason: rejected.reason.clone(),
precedence: rejected.precedence.clone(),
confidence: rejected.confidence.clone(),
})
.collect(),
})
.collect()
}
#[derive(Serialize, JsonSchema)]
pub(crate) struct DiagnosticEntry {
code: String,
severity: String,
path: String,
message: String,
#[serde(skip_serializing_if = "Option::is_none")]
documentation_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
help: Option<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
related: Vec<RelatedLocationEntry>,
}
#[derive(Serialize, JsonSchema)]
pub(crate) struct RelatedLocationEntry {
path: String,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
}
fn diagnostic_entries(bag: &DiagnosticBag) -> Vec<DiagnosticEntry> {
bag.diagnostics
.iter()
.map(|diagnostic| DiagnosticEntry {
code: diagnostic.code.clone(),
severity: match diagnostic.severity {
Severity::Info => "info".to_string(),
Severity::Advisory => "advisory".to_string(),
Severity::Warning => "warning".to_string(),
Severity::Error => "error".to_string(),
},
path: diagnostic.path.clone(),
message: diagnostic.message.clone(),
documentation_url: diagnostic.documentation_url(),
help: diagnostic.help.clone(),
related: diagnostic
.related
.iter()
.map(|location| RelatedLocationEntry {
path: location.path.clone(),
description: location.description.clone(),
})
.collect(),
})
.collect()
}