data-modelling-core 2.4.0

Core SDK library for model operations across platforms
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
//! Databricks Metric Views (DBMV) model
//!
//! Defines the data structures for Databricks Metric Views — a semantic layer
//! format that transforms raw tables into standardised business metrics.
//!
//! ## File Format
//!
//! DBMV documents use the `.dbmv.yaml` extension and contain one or more
//! metric view definitions per file, wrapped in an SDK envelope format.
//!
//! The envelope uses **camelCase** keys (`apiVersion`, `kind`, `metricViews`)
//! while the inner Databricks-native content uses **snake_case** keys
//! (`display_name`, `materialized_views`).
//!
//! ## Example
//!
//! ```yaml
//! apiVersion: v1.0.0
//! kind: MetricViews
//! system: my-databricks-system
//! metricViews:
//!   - name: orders_metrics
//!     source: catalog.schema.orders
//!     dimensions:
//!       - name: order_date
//!         expr: order_date
//!     measures:
//!       - name: total_revenue
//!         expr: SUM(revenue)
//! ```

use serde::{Deserialize, Serialize};

/// Default version for metric views
fn default_version() -> String {
    "1.1".to_string()
}

/// Default API version
fn default_api_version() -> String {
    "v1.0.0".to_string()
}

/// Default kind
fn default_kind() -> String {
    "MetricViews".to_string()
}

/// DBMV Document — wrapper envelope for multiple metric views
///
/// Uses camelCase for the envelope fields to match SDK conventions.
/// One document per system, containing multiple metric view definitions.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct DBMVDocument {
    /// API version of the DBMV format (e.g., "v1.0.0")
    #[serde(default = "default_api_version")]
    pub api_version: String,
    /// Document kind — always "MetricViews"
    #[serde(default = "default_kind")]
    pub kind: String,
    /// System name this document belongs to
    pub system: String,
    /// Optional description of the metric views collection
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Metric view definitions
    #[serde(default)]
    pub metric_views: Vec<DBMVMetricView>,
}

impl Default for DBMVDocument {
    fn default() -> Self {
        Self {
            api_version: default_api_version(),
            kind: default_kind(),
            system: String::new(),
            description: None,
            metric_views: Vec::new(),
        }
    }
}

impl DBMVDocument {
    /// Create a new DBMV document for a system
    pub fn new(system: impl Into<String>) -> Self {
        Self {
            system: system.into(),
            ..Default::default()
        }
    }

    /// Add a metric view to the document
    pub fn add_metric_view(&mut self, view: DBMVMetricView) {
        self.metric_views.push(view);
    }

    /// Get a metric view by name
    pub fn get_metric_view(&self, name: &str) -> Option<&DBMVMetricView> {
        self.metric_views.iter().find(|v| v.name == name)
    }

    /// Import from YAML
    pub fn from_yaml(yaml_content: &str) -> Result<Self, serde_yaml::Error> {
        serde_yaml::from_str(yaml_content)
    }

    /// Export to YAML
    pub fn to_yaml(&self) -> Result<String, serde_yaml::Error> {
        serde_yaml::to_string(self)
    }
}

/// Databricks Metric View definition
///
/// Uses snake_case (Rust default) to match Databricks native YAML format.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DBMVMetricView {
    /// Metric view name
    pub name: String,
    /// Version of the metric view definition
    #[serde(default = "default_version")]
    pub version: String,
    /// Fully qualified source table (e.g., "catalog.schema.table")
    pub source: String,
    /// Optional SQL filter expression applied to the source
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filter: Option<String>,
    /// Optional comment/description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
    /// Dimension definitions
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub dimensions: Vec<DBMVDimension>,
    /// Measure definitions
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub measures: Vec<DBMVMeasure>,
    /// Join definitions (supports nested joins for snowflake schemas)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub joins: Vec<DBMVJoin>,
    /// Materialization configuration
    #[serde(skip_serializing_if = "Option::is_none")]
    pub materialization: Option<DBMVMaterialization>,
}

impl Default for DBMVMetricView {
    fn default() -> Self {
        Self {
            name: String::new(),
            version: default_version(),
            source: String::new(),
            filter: None,
            comment: None,
            dimensions: Vec::new(),
            measures: Vec::new(),
            joins: Vec::new(),
            materialization: None,
        }
    }
}

/// Dimension definition in a metric view
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DBMVDimension {
    /// Dimension name
    pub name: String,
    /// SQL expression for the dimension
    pub expr: String,
    /// Human-readable display name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub display_name: Option<String>,
    /// Optional comment/description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

/// Measure definition in a metric view
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DBMVMeasure {
    /// Measure name
    pub name: String,
    /// SQL aggregation expression (e.g., "SUM(revenue)")
    pub expr: String,
    /// Human-readable display name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub display_name: Option<String>,
    /// Optional comment/description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
    /// Format specification for the measure
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format: Option<DBMVMeasureFormat>,
    /// Window function specifications
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub window: Vec<DBMVWindow>,
}

/// Format specification for a measure
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DBMVMeasureFormat {
    /// Format type (e.g., "currency", "percentage", "number")
    #[serde(rename = "type")]
    pub format_type: String,
}

/// Window function specification for a measure
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DBMVWindow {
    /// Column to order by
    pub order: String,
    /// Window range (e.g., "cumulative", "unbounded")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub range: Option<String>,
    /// Semi-additive behaviour (e.g., "last", "first")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub semiadditive: Option<String>,
}

/// Join definition (supports recursive nesting for snowflake schemas)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DBMVJoin {
    /// Join alias name
    pub name: String,
    /// Fully qualified source table for the join
    pub source: String,
    /// Join condition expression (e.g., "source.customer_id = customers.id")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub on: Option<String>,
    /// Column names for equi-join (alternative to `on`)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub using: Vec<String>,
    /// Nested joins (for snowflake schema patterns)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub joins: Vec<DBMVJoin>,
}

/// Materialization configuration for a metric view
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DBMVMaterialization {
    /// Refresh schedule (e.g., "every 6 hours", "daily")
    pub schedule: String,
    /// Materialization mode (e.g., "relaxed", "strict")
    pub mode: String,
    /// Pre-computed materialized views
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub materialized_views: Vec<DBMVMaterializedView>,
}

/// Pre-computed materialized view definition
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DBMVMaterializedView {
    /// Materialized view name
    pub name: String,
    /// View type: "aggregated" or "unaggregated"
    #[serde(rename = "type")]
    pub view_type: String,
    /// Dimensions to include (for aggregated views)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub dimensions: Vec<String>,
    /// Measures to include (for aggregated views)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub measures: Vec<String>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_document_new() {
        let doc = DBMVDocument::new("my-system");
        assert_eq!(doc.system, "my-system");
        assert_eq!(doc.api_version, "v1.0.0");
        assert_eq!(doc.kind, "MetricViews");
        assert!(doc.metric_views.is_empty());
    }

    #[test]
    fn test_document_add_metric_view() {
        let mut doc = DBMVDocument::new("test-system");
        doc.add_metric_view(DBMVMetricView {
            name: "orders".to_string(),
            source: "catalog.schema.orders".to_string(),
            ..Default::default()
        });
        assert_eq!(doc.metric_views.len(), 1);
        assert_eq!(doc.get_metric_view("orders").unwrap().name, "orders");
        assert!(doc.get_metric_view("nonexistent").is_none());
    }

    #[test]
    fn test_default_version() {
        let view = DBMVMetricView::default();
        assert_eq!(view.version, "1.1");
    }

    #[test]
    fn test_measure_format_type_rename() {
        let format = DBMVMeasureFormat {
            format_type: "currency".to_string(),
        };
        let yaml = serde_yaml::to_string(&format).unwrap();
        assert!(yaml.contains("type: currency"));
    }

    #[test]
    fn test_materialized_view_type_rename() {
        let mv = DBMVMaterializedView {
            name: "test".to_string(),
            view_type: "aggregated".to_string(),
            dimensions: vec![],
            measures: vec![],
        };
        let yaml = serde_yaml::to_string(&mv).unwrap();
        assert!(yaml.contains("type: aggregated"));
    }

    #[test]
    fn test_document_yaml_roundtrip() {
        let mut doc = DBMVDocument::new("test-system");
        doc.description = Some("Test metrics".to_string());
        doc.add_metric_view(DBMVMetricView {
            name: "orders_metrics".to_string(),
            source: "catalog.schema.orders".to_string(),
            dimensions: vec![DBMVDimension {
                name: "order_date".to_string(),
                expr: "order_date".to_string(),
                display_name: Some("Order Date".to_string()),
                comment: None,
            }],
            measures: vec![DBMVMeasure {
                name: "total_revenue".to_string(),
                expr: "SUM(revenue)".to_string(),
                display_name: Some("Total Revenue".to_string()),
                comment: None,
                format: Some(DBMVMeasureFormat {
                    format_type: "currency".to_string(),
                }),
                window: vec![],
            }],
            ..Default::default()
        });

        let yaml = doc.to_yaml().unwrap();
        let parsed = DBMVDocument::from_yaml(&yaml).unwrap();
        assert_eq!(doc, parsed);
    }

    #[test]
    fn test_camel_case_envelope_snake_case_inner() {
        let doc = DBMVDocument::new("test");
        let yaml = doc.to_yaml().unwrap();

        // Envelope fields should be camelCase
        assert!(yaml.contains("apiVersion:"));
        assert!(yaml.contains("metricViews:"));

        // These should NOT appear (wrong casing)
        assert!(!yaml.contains("api_version:"));
        assert!(!yaml.contains("metric_views:"));
    }

    #[test]
    fn test_inner_fields_snake_case() {
        let mut doc = DBMVDocument::new("test");
        doc.add_metric_view(DBMVMetricView {
            name: "test_view".to_string(),
            source: "catalog.schema.table".to_string(),
            dimensions: vec![DBMVDimension {
                name: "dim1".to_string(),
                expr: "col1".to_string(),
                display_name: Some("Dimension 1".to_string()),
                comment: None,
            }],
            measures: vec![DBMVMeasure {
                name: "measure1".to_string(),
                expr: "SUM(col2)".to_string(),
                display_name: None,
                comment: None,
                format: None,
                window: vec![],
            }],
            ..Default::default()
        });

        let yaml = doc.to_yaml().unwrap();
        // Inner fields should be snake_case (Rust default, no rename)
        assert!(yaml.contains("display_name:"));
    }

    #[test]
    fn test_nested_joins() {
        let join = DBMVJoin {
            name: "customers".to_string(),
            source: "catalog.schema.customers".to_string(),
            on: Some("source.customer_id = customers.id".to_string()),
            using: vec![],
            joins: vec![DBMVJoin {
                name: "nation".to_string(),
                source: "catalog.schema.nations".to_string(),
                on: Some("customers.nation_id = nation.id".to_string()),
                using: vec![],
                joins: vec![],
            }],
        };

        let yaml = serde_yaml::to_string(&join).unwrap();
        assert!(yaml.contains("nation"));
        assert!(yaml.contains("customers.nation_id"));

        // Roundtrip
        let parsed: DBMVJoin = serde_yaml::from_str(&yaml).unwrap();
        assert_eq!(join, parsed);
    }

    #[test]
    fn test_window_measure() {
        let measure = DBMVMeasure {
            name: "ytd_revenue".to_string(),
            expr: "SUM(revenue)".to_string(),
            display_name: None,
            comment: None,
            format: None,
            window: vec![DBMVWindow {
                order: "order_date".to_string(),
                range: Some("cumulative".to_string()),
                semiadditive: Some("last".to_string()),
            }],
        };

        let yaml = serde_yaml::to_string(&measure).unwrap();
        let parsed: DBMVMeasure = serde_yaml::from_str(&yaml).unwrap();
        assert_eq!(measure, parsed);
    }

    #[test]
    fn test_materialization() {
        let mat = DBMVMaterialization {
            schedule: "every 6 hours".to_string(),
            mode: "relaxed".to_string(),
            materialized_views: vec![
                DBMVMaterializedView {
                    name: "baseline".to_string(),
                    view_type: "unaggregated".to_string(),
                    dimensions: vec![],
                    measures: vec![],
                },
                DBMVMaterializedView {
                    name: "revenue_by_date".to_string(),
                    view_type: "aggregated".to_string(),
                    dimensions: vec!["order_date".to_string()],
                    measures: vec!["total_revenue".to_string()],
                },
            ],
        };

        let yaml = serde_yaml::to_string(&mat).unwrap();
        assert!(yaml.contains("materialized_views:"));

        let parsed: DBMVMaterialization = serde_yaml::from_str(&yaml).unwrap();
        assert_eq!(mat, parsed);
    }

    #[test]
    fn test_optional_fields_omitted() {
        let view = DBMVMetricView {
            name: "simple".to_string(),
            source: "catalog.schema.table".to_string(),
            ..Default::default()
        };

        let yaml = serde_yaml::to_string(&view).unwrap();
        assert!(!yaml.contains("filter:"));
        assert!(!yaml.contains("comment:"));
        assert!(!yaml.contains("dimensions:"));
        assert!(!yaml.contains("measures:"));
        assert!(!yaml.contains("joins:"));
        assert!(!yaml.contains("materialization:"));
    }
}