Skip to main content

micromegas_analytics/lakehouse/
catalog.rs

1//! Catalog utilities for discovering and managing view schemas.
2//!
3//! This module provides utilities for:
4//! - Discovering current schema versions from the ViewFactory
5//! - Comparing partition schema hashes with current versions
6//! - Finding outdated partitions across view sets
7
8use super::view_factory::ViewFactory;
9use anyhow::Result;
10use datafusion::arrow::datatypes::Schema;
11use std::sync::Arc;
12
13/// Information about a view set's current schema
14#[derive(Debug, Clone)]
15pub struct ViewSetInfo {
16    pub view_set_name: String,
17    pub current_schema_hash: Vec<u8>,
18    pub schema: String,
19    pub has_view_maker: bool,
20    pub global_instance_available: bool,
21    pub description: Option<Arc<str>>,
22    /// One `name: DataType` line per field.
23    pub columns: String,
24    /// Ascending columns the view set's partitions are normally written sorted by; `None` when
25    /// unordered. Informational: not guaranteed for every partition.
26    pub sort_order: Option<Vec<String>>,
27}
28
29fn format_columns(schema: &Schema) -> String {
30    schema
31        .fields()
32        .iter()
33        .map(|f| format!("{}: {}", f.name(), f.data_type()))
34        .collect::<Vec<_>>()
35        .join("\n")
36}
37
38/// List all view sets with their current schema information from the ViewFactory.
39pub fn list_view_sets(view_factory: &ViewFactory) -> Result<Vec<ViewSetInfo>> {
40    let mut schema_infos = Vec::new();
41    let mut processed_view_sets = std::collections::HashSet::new();
42
43    for global_view in view_factory.get_global_views() {
44        let view_set_name = global_view.get_view_set_name().to_string();
45
46        if processed_view_sets.contains(&view_set_name) {
47            continue;
48        }
49        processed_view_sets.insert(view_set_name.clone());
50
51        let current_schema_hash = global_view.get_file_schema_hash();
52        let schema = format!("{:?}", global_view.get_file_schema());
53
54        // Check if this view set has a ViewMaker (supports non-global instances)
55        let has_view_maker = view_factory.get_view_sets().contains_key(&view_set_name);
56
57        let columns = format_columns(&global_view.get_file_schema());
58        let sort_order = global_view.get_reported_sort_order();
59
60        let description = view_factory.get_description(&view_set_name);
61        schema_infos.push(ViewSetInfo {
62            view_set_name,
63            current_schema_hash,
64            schema,
65            has_view_maker,
66            global_instance_available: true,
67            description,
68            columns,
69            sort_order,
70        });
71    }
72
73    // Then, collect schema info from view sets that have ViewMakers but no global views
74    for (view_set_name, view_maker) in view_factory.get_view_sets() {
75        // Skip if we already processed this view set from global views
76        if processed_view_sets.contains(view_set_name) {
77            continue;
78        }
79
80        // Get schema information directly from the ViewMaker
81        let current_schema_hash = view_maker.get_schema_hash();
82        let schema = format!("{:?}", view_maker.get_schema());
83
84        schema_infos.push(ViewSetInfo {
85            view_set_name: view_set_name.clone(),
86            current_schema_hash,
87            schema,
88            has_view_maker: true,
89            global_instance_available: false,
90            description: view_factory.get_description(view_set_name),
91            columns: format_columns(&view_maker.get_schema()),
92            sort_order: view_maker.get_scan_output_ordering().sort_columns(),
93        });
94    }
95
96    Ok(schema_infos)
97}