Skip to main content

hyprstream_metrics/query/rules/
view.rs

1use crate::storage::view::ViewMetadata;
2use crate::storage::StorageBackend;
3use datafusion::error::Result;
4use datafusion::logical_expr::{LogicalPlan, TableScan};
5use datafusion::optimizer::optimizer::{OptimizerConfig, OptimizerRule};
6use datafusion::sql::TableReference;
7use datafusion_common::tree_node::Transformed;
8#[cfg(test)]
9use {
10    async_trait::async_trait,
11    datafusion::catalog::Session,
12    datafusion::datasource::{TableProvider, TableType},
13    datafusion::logical_expr::{Expr, TableSource},
14    datafusion::physical_plan::{empty::EmptyExec, ExecutionPlan},
15    datafusion_common::DFSchema,
16    duckdb::arrow::datatypes::SchemaRef,
17};
18
19#[cfg(test)]
20#[derive(Debug)]
21struct EmptyTableProvider {
22    schema: SchemaRef,
23}
24
25#[cfg(test)]
26impl TableSource for EmptyTableProvider {
27    fn as_any(&self) -> &dyn std::any::Any {
28        self
29    }
30
31    fn schema(&self) -> SchemaRef {
32        self.schema.clone()
33    }
34}
35
36#[async_trait]
37#[cfg(test)]
38impl TableProvider for EmptyTableProvider {
39    fn as_any(&self) -> &dyn std::any::Any {
40        self
41    }
42
43    fn schema(&self) -> SchemaRef {
44        self.schema.clone()
45    }
46
47    fn table_type(&self) -> TableType {
48        TableType::Base
49    }
50
51    async fn scan(
52        &self,
53        _state: &dyn Session,
54        _projection: Option<&Vec<usize>>,
55        _filters: &[Expr],
56        _limit: Option<usize>,
57    ) -> datafusion::error::Result<Arc<dyn ExecutionPlan>> {
58        Ok(Arc::new(EmptyExec::new(TableProvider::schema(self))))
59    }
60}
61use std::sync::Arc;
62
63/// Optimization rule that rewrites queries to use views when beneficial
64pub struct ViewOptimizationRule {
65    #[allow(dead_code)]
66    storage: Arc<dyn StorageBackend>,
67}
68
69impl std::fmt::Debug for ViewOptimizationRule {
70    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71        f.debug_struct("ViewOptimizationRule")
72            .field("storage", &"<StorageBackend>")
73            .finish()
74    }
75}
76
77impl ViewOptimizationRule {
78    pub fn new(storage: Arc<dyn StorageBackend>) -> Self {
79        Self { storage }
80    }
81
82    /// Check if a view can be used for this query
83    #[allow(dead_code)]
84    async fn find_matching_view(&self, plan: &LogicalPlan) -> Result<Option<ViewMetadata>> {
85        // Get list of available views
86        let views = self.storage.list_views().await.map_err(|e| {
87            datafusion::error::DataFusionError::Internal(format!("Failed to list views: {e}"))
88        })?;
89
90        // For each view, check if it can be used for this query
91        for view_name in views {
92            let view = self.storage.get_view(&view_name).await.map_err(|e| {
93                datafusion::error::DataFusionError::Internal(format!(
94                    "Failed to get view metadata: {e}"
95                ))
96            })?;
97
98            if self.can_use_view(plan, &view)? {
99                return Ok(Some(view));
100            }
101        }
102
103        Ok(None)
104    }
105
106    /// Check if a view can be used to answer this query
107    fn can_use_view(&self, plan: &LogicalPlan, view: &ViewMetadata) -> Result<bool> {
108        // TODO: Add support for more complex query patterns (Join, Aggregate, etc.)
109        if let LogicalPlan::TableScan(scan) = plan {
110            // Check if the query is accessing the view's source table
111            if scan.table_name.to_string() == view.definition.source_table {
112                // TODO: Add more sophisticated matching logic here
113                // - Check if required columns are available
114                // - Check if aggregations match
115                // - Check if grouping is compatible
116                // - Check if time windows align
117                return Ok(true);
118            }
119        }
120
121        Ok(false)
122    }
123
124    /// Rewrite the plan to use the view
125    fn rewrite_with_view(&self, plan: &LogicalPlan, view: &ViewMetadata) -> Result<LogicalPlan> {
126        match plan {
127            LogicalPlan::TableScan(scan) => {
128                // Convert Arrow schema to DataFusion schema
129                let arrow_schema = view.definition.schema.as_ref();
130                let df_schema = datafusion::common::DFSchema::try_from_qualified_schema(
131                    &view.name,
132                    arrow_schema,
133                )
134                .map_err(|e| {
135                    datafusion::error::DataFusionError::Internal(format!(
136                        "Failed to convert schema: {e}"
137                    ))
138                })?;
139                let df_schema = Arc::new(df_schema);
140
141                // Create new table scan using the view
142                let new_scan = LogicalPlan::TableScan(TableScan {
143                    table_name: TableReference::from(view.name.clone()),
144                    source: scan.source.clone(),
145                    projection: Some(
146                        view.definition
147                            .columns
148                            .iter()
149                            .enumerate()
150                            .map(|(i, _)| i)
151                            .collect(),
152                    ),
153                    projected_schema: df_schema,
154                    filters: scan.filters.clone(),
155                    fetch: scan.fetch,
156                });
157
158                Ok(new_scan)
159            }
160            // TODO: Add support for rewriting more complex plans
161            _ => Ok(plan.clone()),
162        }
163    }
164}
165
166impl OptimizerRule for ViewOptimizationRule {
167    fn name(&self) -> &str {
168        "view_optimization"
169    }
170
171    fn supports_rewrite(&self) -> bool {
172        true
173    }
174
175    fn rewrite(
176        &self,
177        plan: LogicalPlan,
178        _config: &dyn OptimizerConfig,
179    ) -> Result<Transformed<LogicalPlan>> {
180        // Get matching view and rewrite plan
181        // Note: This is a blocking operation, but it's acceptable for testing
182        let views = futures::executor::block_on(self.storage.list_views()).map_err(|e| {
183            datafusion::error::DataFusionError::Internal(format!("Failed to list views: {e}"))
184        })?;
185
186        // For each view, check if it can be used for this query
187        for view_name in views {
188            let view =
189                futures::executor::block_on(self.storage.get_view(&view_name)).map_err(|e| {
190                    datafusion::error::DataFusionError::Internal(format!(
191                        "Failed to get view metadata: {e}"
192                    ))
193                })?;
194
195            if self.can_use_view(&plan, &view)? {
196                let new_plan = self.rewrite_with_view(&plan, &view)?;
197                return Ok(Transformed::yes(new_plan));
198            }
199        }
200        Ok(Transformed::no(plan))
201    }
202}
203
204#[cfg(test)]
205mod tests {
206    use super::*;
207    use crate::storage::duckdb::DuckDbBackend;
208    use crate::storage::view::ViewDefinition;
209    use duckdb::arrow::datatypes::{DataType, Field, Schema};
210    use std::sync::Arc;
211
212    #[tokio::test]
213    async fn test_view_optimization() -> Result<()> {
214        // Create test backend
215        let backend = Arc::new(DuckDbBackend::new_in_memory().map_err(|e| {
216            datafusion::error::DataFusionError::Internal(format!("Failed to create backend: {e}"))
217        })?);
218        backend.init().await.map_err(|e| {
219            datafusion::error::DataFusionError::Internal(format!("Failed to init backend: {e}"))
220        })?;
221
222        // Create source table
223        let source_schema = Arc::new(Schema::new(vec![
224            Field::new("metric", DataType::Utf8, false),
225            Field::new("value", DataType::Float64, false),
226        ]));
227        backend
228            .create_table("test_source", &source_schema)
229            .await
230            .map_err(|e| {
231                datafusion::error::DataFusionError::Internal(format!("Failed to create table: {e}"))
232            })?;
233
234        // Create view
235        let view_def = ViewDefinition::new(
236            "test_source".to_owned(),
237            vec!["metric".to_owned(), "value".to_owned()],
238            vec![],
239            None,
240            None,
241            Arc::new(Schema::new(vec![
242                Field::new("metric", DataType::Utf8, false),
243                Field::new("value", DataType::Float64, false),
244            ])),
245        );
246        backend
247            .create_view("test_view", view_def)
248            .await
249            .map_err(|e| {
250                datafusion::error::DataFusionError::Internal(format!("Failed to create view: {e}"))
251            })?;
252
253        // Create optimizer rule
254        let rule = ViewOptimizationRule::new(backend);
255
256        // Create test plan
257        let arrow_schema = source_schema.as_ref().clone();
258        let df_schema = DFSchema::try_from(arrow_schema.clone()).map_err(|e| {
259            datafusion::error::DataFusionError::Internal(format!("Failed to convert schema: {e}"))
260        })?;
261        let plan = LogicalPlan::TableScan(TableScan {
262            table_name: "test_source".into(),
263            source: Arc::new(EmptyTableProvider {
264                schema: Arc::new(df_schema.clone().into()),
265            }),
266            projection: None,
267            projected_schema: Arc::new(df_schema),
268            filters: vec![],
269            fetch: None,
270        });
271
272        // Apply optimization
273        let transformed = rule.rewrite(
274            plan.clone(),
275            &datafusion::optimizer::OptimizerContext::new(),
276        )?;
277
278        // Verify plan was rewritten to use view
279        match transformed.data {
280            LogicalPlan::TableScan(scan) => {
281                assert_eq!(scan.table_name.to_string(), "test_view");
282            }
283            _ => panic!("Expected TableScan"),
284        }
285
286        Ok(())
287    }
288}