Skip to main content

hyprstream_metrics/query/physical/
mod.rs

1//! Physical operators for query execution
2//!
3//! This module provides implementations of physical operators that can be used
4//! in query execution plans. These operators are optimized for performance and
5//! include specialized implementations for vector operations.
6
7mod vector;
8
9pub use vector::VectorizedOperator;
10
11use datafusion::arrow::datatypes::Schema;
12use datafusion::arrow::record_batch::RecordBatch;
13use datafusion::error::Result;
14use std::sync::Arc;
15
16/// Trait for physical operators that can be executed
17pub trait PhysicalOperator: Send + Sync {
18    /// Get the schema of the output
19    fn schema(&self) -> &Arc<Schema>;
20
21    /// Execute the operator and produce output batches
22    fn execute(&self, input: Vec<RecordBatch>) -> Result<Vec<RecordBatch>>;
23
24    /// Get children operators
25    fn children(&self) -> Vec<Arc<dyn PhysicalOperator>>;
26}
27
28/// Statistics about operator execution
29#[derive(Debug, Default)]
30pub struct OperatorMetrics {
31    /// Number of input rows processed
32    pub input_rows: usize,
33    /// Number of output rows produced
34    pub output_rows: usize,
35    /// Execution time in milliseconds
36    pub execution_time_ms: u64,
37    /// Memory used in bytes
38    pub memory_used: usize,
39}
40
41/// Base implementation for physical operators
42pub struct BaseOperator {
43    /// Output schema
44    schema: Arc<Schema>,
45    /// Child operators
46    children: Vec<Arc<dyn PhysicalOperator>>,
47    /// Execution metrics
48    metrics: Arc<parking_lot::RwLock<OperatorMetrics>>,
49}
50
51impl BaseOperator {
52    pub fn new(schema: Arc<Schema>, children: Vec<Arc<dyn PhysicalOperator>>) -> Self {
53        Self {
54            schema,
55            children,
56            metrics: Arc::new(parking_lot::RwLock::new(OperatorMetrics::default())),
57        }
58    }
59
60    pub fn metrics(&self) -> Arc<parking_lot::RwLock<OperatorMetrics>> {
61        self.metrics.clone()
62    }
63}
64
65impl PhysicalOperator for BaseOperator {
66    fn schema(&self) -> &Arc<Schema> {
67        &self.schema
68    }
69
70    fn execute(&self, input: Vec<RecordBatch>) -> Result<Vec<RecordBatch>> {
71        // Base implementation just passes through input
72        Ok(input)
73    }
74
75    fn children(&self) -> Vec<Arc<dyn PhysicalOperator>> {
76        self.children.clone()
77    }
78}
79
80/// Factory for creating physical operators
81pub struct OperatorFactory;
82
83impl OperatorFactory {
84    /// Create a new physical operator
85    pub fn create(
86        operator_type: &str,
87        schema: Arc<Schema>,
88        children: Vec<Arc<dyn PhysicalOperator>>,
89        properties: std::collections::HashMap<String, String>,
90    ) -> Result<Arc<dyn PhysicalOperator>> {
91        match operator_type {
92            "vector" => Ok(Arc::new(vector::VectorizedOperator::new(
93                schema, children, properties,
94            )?)),
95            _ => Err(datafusion::error::DataFusionError::NotImplemented(format!(
96                "Operator type {operator_type} not implemented"
97            ))),
98        }
99    }
100}