dtcs 0.10.1

Reference implementation of the Data Transformation Contract Standard (DTCS)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! Transformation plan optimization (SPEC Ch 13 §9, Ch 8 §14, Ch 17–19 §11).

use std::collections::{BTreeSet, HashMap, HashSet};

use serde::{Deserialize, Serialize};

use crate::analysis::expr::ast::{Expr, LiteralValue, Span};
use crate::analysis::expr::{eval, format, parse, rewrite};
use crate::diagnostics::{
    codes, optimization_error, Diagnostic, DiagnosticCategory, DiagnosticStage, Severity,
};
use crate::model::{types_assignable, ActionOrdering, RegistryCategory, RegistryDocument};
use crate::registry;

use super::graph;
use super::model::{PlanNodeKind, TransformationPlan};
use super::rule_key;
use super::validate::{plan_as_contract, validate_with_registry};

/// Options controlling optimization passes.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OptimizeOptions {
    /// Run expression rewrites (constant folding and algebraic simplification).
    #[serde(default = "default_true")]
    pub expressions: bool,
    /// Run deterministic function-call evaluation in expressions.
    #[serde(default = "default_true")]
    pub functions: bool,
    /// Run semantic action fusion passes.
    #[serde(default = "default_true")]
    pub actions: bool,
    /// Run rule deduplication.
    #[serde(default = "default_true")]
    pub rules: bool,
    /// Remove unused expression nodes after other passes.
    #[serde(default = "default_true")]
    pub dead_expressions: bool,
    /// Validate the optimized plan before returning it.
    #[serde(default = "default_true")]
    pub validate: bool,
}

impl Default for OptimizeOptions {
    fn default() -> Self {
        Self {
            expressions: true,
            functions: true,
            actions: true,
            rules: true,
            dead_expressions: true,
            validate: true,
        }
    }
}

fn default_true() -> bool {
    true
}

/// Record of an applied optimization rewrite.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransformRecord {
    /// Optimization pass name.
    pub pass: String,
    /// Affected plan node identifier.
    pub node_id: String,
    /// Human-readable description of the rewrite.
    pub description: String,
}

/// Result of optimizing a transformation plan.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OptimizeResult {
    /// Optimized plan when optimization succeeded.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub plan: Option<TransformationPlan>,
    /// Diagnostics from optimization and validation.
    pub diagnostics: Vec<Diagnostic>,
    /// Informational log of applied rewrites.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub transforms: Vec<TransformRecord>,
}

impl OptimizeResult {
    /// Returns `true` when no error-level diagnostics are present.
    #[must_use]
    pub fn is_valid(&self) -> bool {
        !self.diagnostics.iter().any(|d| d.severity.is_error())
    }
}

/// Optimize a validated transformation plan.
#[must_use]
pub fn optimize(plan: &TransformationPlan) -> OptimizeResult {
    optimize_with_registry(
        plan,
        registry::default_registry(),
        &OptimizeOptions::default(),
    )
}

/// Optimize a transformation plan with a registry catalog and options.
#[must_use]
pub fn optimize_with_registry(
    plan: &TransformationPlan,
    registry_doc: &RegistryDocument,
    options: &OptimizeOptions,
) -> OptimizeResult {
    let mut result = OptimizeResult::default();

    if options.validate {
        let input_validation = validate_with_registry(plan, registry_doc);
        if !input_validation.is_valid() {
            result.diagnostics = input_validation.diagnostics;
            result.diagnostics.push(
                optimization_error(
                    codes::INVALID_OPTIMIZATION,
                    DiagnosticCategory::Semantic,
                    "input plan failed validation",
                )
                .with_object_ref("plan"),
            );
            return result;
        }
    }

    let mut working = plan.clone();
    let contract = plan_as_contract(&working);

    if options.expressions {
        optimize_expressions(
            &mut working,
            &contract,
            registry_doc,
            &mut result.transforms,
            &mut result.diagnostics,
        );
    }
    if options.functions {
        optimize_functions(
            &mut working,
            &contract,
            registry_doc,
            &mut result.transforms,
            &mut result.diagnostics,
        );
    }
    if options.actions {
        optimize_actions(&mut working, &mut result.transforms);
    }
    if options.rules {
        optimize_rules(&mut working, &mut result.transforms);
    }

    rebuild_dependencies(&mut working, registry_doc, &mut result);
    if !result.is_valid() {
        return result;
    }

    if options.dead_expressions {
        let node_count_before = working.nodes.len();
        eliminate_dead_expressions(&mut working, &mut result.transforms);
        if working.nodes.len() != node_count_before {
            rebuild_dependencies(&mut working, registry_doc, &mut result);
            if !result.is_valid() {
                return result;
            }
        }
    }

    if options.validate {
        let validation = validate_with_registry(&working, registry_doc);
        let validation_failed = !validation.is_valid();
        result.diagnostics.extend(validation.diagnostics);
        if validation_failed {
            result.diagnostics.push(
                optimization_error(
                    codes::INVALID_OPTIMIZATION,
                    DiagnosticCategory::Semantic,
                    "optimized plan failed validation",
                )
                .with_object_ref("plan"),
            );
            return result;
        }
    }

    result.plan = Some(working);
    result
}

fn rebuild_dependencies(
    plan: &mut TransformationPlan,
    registry_doc: &RegistryDocument,
    result: &mut OptimizeResult,
) {
    let contract = plan_as_contract(plan);
    let graph_result = graph::build(&contract, &plan.nodes, registry_doc);
    let graph_failed = graph_result
        .diagnostics
        .iter()
        .any(|d| d.severity.is_error());
    result.diagnostics.extend(graph_result.diagnostics);
    if graph_failed {
        result.diagnostics.push(
            optimization_error(
                codes::INVALID_OPTIMIZATION,
                DiagnosticCategory::Semantic,
                "failed to rebuild dependency graph after optimization",
            )
            .with_object_ref("dependencies"),
        );
        return;
    }
    plan.dependencies = graph_result.dependencies;
}

fn optimize_expressions(
    plan: &mut TransformationPlan,
    contract: &crate::model::TransformationContract,
    registry_doc: &RegistryDocument,
    transforms: &mut Vec<TransformRecord>,
    diagnostics: &mut Vec<Diagnostic>,
) {
    for node in &mut plan.nodes {
        let PlanNodeKind::Expression(expression) = &mut node.kind else {
            continue;
        };
        let Some(body) = expression.expr.as_deref() else {
            continue;
        };
        if body.trim().is_empty() {
            continue;
        }
        let original_body = body.to_string();
        let Ok(mut ast) = parse::parse_expression(body) else {
            continue;
        };
        let before = format::format_expression(&ast);
        ast = rewrite::simplify_expression(&ast);
        ast = fold_expression_calls(&ast, registry_doc);
        ast = rewrite::simplify_expression(&ast);
        if let Some(value) = eval::evaluate(&ast) {
            ast = eval::literal_expr(value, ast_span(&ast));
        }
        let after = format::format_expression(&ast);
        if after != before {
            if expression_rewrite_accepted(&ast, expression, contract, registry_doc) {
                expression.expr = Some(after);
                transforms.push(TransformRecord {
                    pass: "expression".into(),
                    node_id: node.id.clone(),
                    description: format!(
                        "rewrote expression '{before}' to '{}'",
                        expression.expr.as_deref().unwrap_or("")
                    ),
                });
                plan.findings
                    .retain(|finding| finding.object_ref != node.object_ref);
            } else {
                expression.expr = Some(original_body);
                diagnostics.push(optimization_skipped(
                    format!(
                        "skipped expression rewrite for node '{}' due to type guard",
                        node.id
                    ),
                    &node.object_ref,
                ));
            }
        }
    }
}

fn optimize_functions(
    plan: &mut TransformationPlan,
    contract: &crate::model::TransformationContract,
    registry_doc: &RegistryDocument,
    transforms: &mut Vec<TransformRecord>,
    diagnostics: &mut Vec<Diagnostic>,
) {
    for node in &mut plan.nodes {
        let PlanNodeKind::Expression(expression) = &mut node.kind else {
            continue;
        };
        let Some(body) = expression.expr.as_deref() else {
            continue;
        };
        let original_body = body.to_string();
        let Ok(mut ast) = parse::parse_expression(body) else {
            continue;
        };
        let before = format::format_expression(&ast);
        ast = fold_expression_calls(&ast, registry_doc);
        if let Some(value) = eval::evaluate(&ast) {
            ast = eval::literal_expr(value, ast_span(&ast));
        }
        let after = format::format_expression(&ast);
        if after != before {
            if expression_rewrite_accepted(&ast, expression, contract, registry_doc) {
                expression.expr = Some(after.clone());
                transforms.push(TransformRecord {
                    pass: "function".into(),
                    node_id: node.id.clone(),
                    description: format!("evaluated function expression '{before}' to '{after}'"),
                });
                plan.findings
                    .retain(|finding| finding.object_ref != node.object_ref);
            } else {
                expression.expr = Some(original_body);
                diagnostics.push(optimization_skipped(
                    format!(
                        "skipped function rewrite for node '{}' due to type guard",
                        node.id
                    ),
                    &node.object_ref,
                ));
            }
        }
    }
}

fn fold_expression_calls(expr: &Expr, registry_doc: &RegistryDocument) -> Expr {
    match expr {
        Expr::Call { callee, args, span } => {
            let folded_args: Vec<Expr> = args
                .iter()
                .map(|arg| fold_expression_calls(arg, registry_doc))
                .collect();
            if callee.starts_with("dtcs:")
                && is_deterministic_registry_function(callee, registry_doc)
            {
                let const_args: Option<Vec<LiteralValue>> =
                    folded_args.iter().map(eval::evaluate).collect();
                if let Some(values) = const_args {
                    if let Some(result) = eval::evaluate_registry_call(callee, &values) {
                        return eval::literal_expr(result, span.clone());
                    }
                }
            }
            Expr::Call {
                callee: callee.clone(),
                args: folded_args,
                span: span.clone(),
            }
        }
        Expr::Unary { op, expr, span } => Expr::Unary {
            op: *op,
            span: span.clone(),
            expr: Box::new(fold_expression_calls(expr, registry_doc)),
        },
        Expr::Binary {
            op,
            left,
            right,
            span,
        } => Expr::Binary {
            op: *op,
            span: span.clone(),
            left: Box::new(fold_expression_calls(left, registry_doc)),
            right: Box::new(fold_expression_calls(right, registry_doc)),
        },
        other => other.clone(),
    }
}

fn expression_rewrite_accepted(
    ast: &Expr,
    expression: &crate::model::Expression,
    contract: &crate::model::TransformationContract,
    registry_doc: &RegistryDocument,
) -> bool {
    let Some(type_name) = expression.type_name.as_deref() else {
        return false;
    };
    let Ok(declared) = crate::model::parse_logical_type(type_name) else {
        return false;
    };
    let Ok(inferred) =
        crate::analysis::expr::types::infer_expression_type(ast, contract, registry_doc)
    else {
        return false;
    };
    types_assignable(&inferred.logical, &declared)
}

fn optimization_skipped(message: impl Into<String>, object_ref: &str) -> Diagnostic {
    Diagnostic::new(
        codes::OPTIMIZATION_SKIPPED,
        Severity::Information,
        DiagnosticStage::Optimization,
        DiagnosticCategory::Semantic,
        message,
    )
    .with_object_ref(object_ref)
}

fn optimize_actions(plan: &mut TransformationPlan, transforms: &mut Vec<TransformRecord>) {
    let contract = plan_as_contract(plan);
    let order = graph::topological_order(&contract, &plan.nodes, &plan.dependencies);
    let mut action_nodes: Vec<(String, String, String)> = Vec::new();
    for id in &order {
        let Some(node) = plan.nodes.iter().find(|n| &n.id == id) else {
            continue;
        };
        if let PlanNodeKind::SemanticAction(action) = &node.kind {
            action_nodes.push((
                node.id.clone(),
                action.action.clone(),
                action.target.clone(),
            ));
        }
    }

    let mut remove_ids = HashSet::new();
    let mut previous_by_target: HashMap<String, (String, String)> = HashMap::new();

    for (id, action, target) in action_nodes {
        if let Some((prev_id, prev_action)) = previous_by_target.get(&target) {
            if prev_action == &action && is_idempotent_action(&action) {
                remove_ids.insert(id.clone());
                transforms.push(TransformRecord {
                    pass: "action".into(),
                    node_id: id,
                    description: format!(
                        "removed redundant idempotent action '{action}' on target '{target}' after '{prev_id}'"
                    ),
                });
                continue;
            }
        }
        previous_by_target.insert(target, (id, action));
    }

    if remove_ids.is_empty() {
        return;
    }

    plan.nodes.retain(|node| !remove_ids.contains(&node.id));
    update_ordering_after_removals(plan, &remove_ids);
}

fn optimize_rules(plan: &mut TransformationPlan, transforms: &mut Vec<TransformRecord>) {
    let protected: BTreeSet<String> = plan
        .guarantees
        .input_preconditions
        .iter()
        .map(|c| c.rule_id.clone())
        .chain(
            plan.guarantees
                .output_postconditions
                .iter()
                .map(|c| c.rule_id.clone()),
        )
        .collect();

    let mut seen = HashSet::new();
    let mut remove_ids = HashSet::new();

    for node in &plan.nodes {
        let PlanNodeKind::Rule(rule) = &node.kind else {
            continue;
        };
        let key = rule_key::rule_dedup_key(rule);
        if !seen.insert(key) && !protected.contains(&node.id) {
            remove_ids.insert(node.id.clone());
            transforms.push(TransformRecord {
                pass: "rule".into(),
                node_id: node.id.clone(),
                description: format!(
                    "removed duplicate rule '{}' on target '{}' ({})",
                    rule.rule,
                    rule.target,
                    rule.phase.as_str()
                ),
            });
        }
    }

    if remove_ids.is_empty() {
        return;
    }

    plan.nodes.retain(|node| !remove_ids.contains(&node.id));
}

fn eliminate_dead_expressions(
    plan: &mut TransformationPlan,
    transforms: &mut Vec<TransformRecord>,
) {
    let protected: BTreeSet<String> = plan
        .guarantees
        .input_preconditions
        .iter()
        .map(|c| c.rule_id.clone())
        .chain(
            plan.guarantees
                .output_postconditions
                .iter()
                .map(|c| c.rule_id.clone()),
        )
        .collect();

    let referenced: HashSet<String> = plan
        .dependencies
        .iter()
        .map(|edge| edge.to.clone())
        .chain(protected)
        .collect();

    let mut remove_ids = HashSet::new();
    for node in &plan.nodes {
        let PlanNodeKind::Expression(_) = &node.kind else {
            continue;
        };
        if !referenced.contains(&node.id) {
            remove_ids.insert(node.id.clone());
            transforms.push(TransformRecord {
                pass: "deadExpression".into(),
                node_id: node.id.clone(),
                description: "removed unused expression node".into(),
            });
        }
    }

    if remove_ids.is_empty() {
        return;
    }

    plan.nodes.retain(|node| !remove_ids.contains(&node.id));
    plan.findings
        .retain(|finding| !remove_ids.iter().any(|id| finding.object_ref.contains(id)));
}

fn update_ordering_after_removals(plan: &mut TransformationPlan, removed: &HashSet<String>) {
    let Some(semantics) = plan.guarantees.semantics.as_mut() else {
        return;
    };
    let Some(ordering) = semantics.ordering.as_mut() else {
        return;
    };
    let ActionOrdering::Explicit { order } = ordering else {
        return;
    };
    order.retain(|id| !removed.contains(id));
}

fn is_idempotent_action(action: &str) -> bool {
    matches!(
        action,
        "dtcs:lowercase" | "dtcs:uppercase" | "dtcs:trim" | "dtcs:normalize_whitespace"
    )
}

fn is_deterministic_registry_function(name: &str, registry_doc: &RegistryDocument) -> bool {
    let Some(entry) = registry::resolve(registry_doc, name) else {
        return false;
    };
    if entry.category != RegistryCategory::Function {
        return false;
    }
    let Some(definition) = entry.definition.as_deref() else {
        return false;
    };
    #[derive(Deserialize)]
    struct Def {
        deterministic: Option<bool>,
    }
    serde_json::from_str::<Def>(definition)
        .ok()
        .and_then(|def| def.deterministic)
        .unwrap_or(false)
}

fn ast_span(expr: &Expr) -> Span {
    match expr {
        Expr::Literal { span, .. }
        | Expr::FieldRef { span, .. }
        | Expr::Unary { span, .. }
        | Expr::Binary { span, .. }
        | Expr::Call { span, .. } => span.clone(),
    }
}