hyperQL 0.1.0

Query language for hyperspatial graph databases with temporal and spatial semantics
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
//! # IR Operators - Low-Level Query Operations
//!
//! This module defines the complete set of IR operators that form the building blocks
//! for executing HyperQL queries in hyperbolic space. Each operator is optimized for
//! efficient execution while maintaining the semantic richness of the HyperQL language.

use super::{IRResult, NodeId};
use std::collections::HashMap;
use crate::types::Position3D;

/// Base trait for all IR operators
pub trait IROperator: std::fmt::Debug + Send + Sync {
    /// Execute the operator with given input data
    fn execute(&self, input: OperatorInput) -> IRResult<OperatorOutput>;
    
    /// Get the output schema for this operator
    fn output_schema(&self) -> &Schema;
    
    /// Get estimated cost for execution planning
    fn estimated_cost(&self) -> ExecutionCost;
    
    /// Get operator statistics
    fn get_stats(&self) -> OperatorStats;
    
    /// Check if operator can be parallelized
    fn is_parallelizable(&self) -> bool;
    
    /// Get required memory for execution
    fn memory_requirement(&self) -> MemoryRequirement;
}

/// Input data for operator execution
#[derive(Debug, Clone)]
pub struct OperatorInput {
    /// Input data rows
    pub data: Vec<DataRow>,
    /// Execution context
    pub context: ExecutionContext,
    /// Performance hints
    pub hints: ExecutionHints,
}

/// Output data from operator execution
#[derive(Debug, Clone)]
pub struct OperatorOutput {
    /// Output data rows
    pub data: Vec<DataRow>,
    /// Execution metadata
    pub metadata: ExecutionMetadata,
    /// Performance metrics
    pub metrics: OperatorMetrics,
}

/// Data row representation
#[derive(Debug, Clone)]
pub struct DataRow {
    /// Column values
    pub values: Vec<Value>,
    /// Hyperbolic position (if applicable)
    pub position: Option<Position3D>,
    /// Row metadata
    pub metadata: RowMetadata,
}

/// Column schema definition
#[derive(Debug, Clone)]
pub struct Schema {
    /// Column definitions
    pub columns: Vec<ColumnDef>,
    /// Primary key columns
    pub primary_key: Vec<usize>,
    /// Hyperbolic positioning info
    pub positioning: Option<PositioningInfo>,
}

/// Column definition
#[derive(Debug, Clone)]
pub struct ColumnDef {
    pub name: String,
    pub data_type: DataType,
    pub nullable: bool,
    pub indexed: bool,
}

/// Data types supported in IR
#[derive(Debug, Clone, PartialEq)]
pub enum DataType {
    Boolean,
    Integer,
    Float,
    String,
    DateTime,
    Array(Box<DataType>),
    Object,
    // Hyperbolic-specific types
    Position,
    Vector(usize), // Vector with dimension
    Distance,
    Embedding(usize),
    EntityId,
    Measure,
}

/// Values that can be stored in data rows
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
    Null,
    Boolean(bool),
    Integer(i64),
    Float(f64),
    String(String),
    DateTime(chrono::DateTime<chrono::Utc>),
    Array(Vec<Value>),
    Object(HashMap<String, Value>),
    // Hyperbolic-specific values
    Position(Position3D),
    Vector(Vec<f64>),
    Distance(f64),
    Embedding(Vec<f64>),
    EntityId(String),
    Measure(MeasureValue),
}

// Note: Using Position3D from types module instead of local definition

/// Measure value with metadata
#[derive(Debug, Clone, PartialEq)]
pub struct MeasureValue {
    pub name: String,
    pub value: f64,
    pub source: Option<String>,
    pub decay_factor: Option<f64>,
    pub timestamp: chrono::DateTime<chrono::Utc>,
}

// =============================================================================
// RELATIONAL OPERATORS
// =============================================================================

/// Scan operator - reads data from storage
#[derive(Debug, Clone)]
pub struct ScanOperator {
    pub node_id: NodeId,
    pub collection_name: String,
    pub schema: Schema,
    pub predicate: Option<Predicate>,
    pub limit: Option<usize>,
}

/// Filter operator - applies predicates to data
#[derive(Debug, Clone)]
pub struct FilterOperator {
    pub node_id: NodeId,
    pub predicate: Predicate,
    pub input_schema: Schema,
}

/// Projection operator - selects columns
#[derive(Debug, Clone)]
pub struct ProjectOperator {
    pub node_id: NodeId,
    pub columns: Vec<ColumnExpression>,
    pub input_schema: Schema,
    pub output_schema: Schema,
}

/// Join operator - combines multiple data sources
#[derive(Debug, Clone)]
pub struct JoinOperator {
    pub node_id: NodeId,
    pub join_type: JoinType,
    pub left_schema: Schema,
    pub right_schema: Schema,
    pub join_condition: JoinCondition,
    pub output_schema: Schema,
}

// =============================================================================
// GRAPH OPERATORS  
// =============================================================================

/// Traversal operator - navigates graph relationships
#[derive(Debug, Clone)]
pub struct TraversalOperator {
    pub node_id: NodeId,
    pub edge_type: String,
    pub direction: TraversalDirection,
    pub depth_range: (usize, usize), // (min_depth, max_depth)
    pub traversal_predicate: Option<Predicate>,
    pub input_schema: Schema,
    pub output_schema: Schema,
}

/// Path finding operator - finds paths between entities
#[derive(Debug, Clone)]
pub struct PathFindOperator {
    pub node_id: NodeId,
    pub source_expression: Expression,
    pub target_expression: Expression,
    pub path_type: PathType,
    pub max_depth: usize,
    pub weight_function: Option<WeightFunction>,
}

// =============================================================================
// VECTOR OPERATORS
// =============================================================================

/// Similarity operator - computes vector similarities
#[derive(Debug, Clone)]
pub struct SimilarityOperator {
    pub node_id: NodeId,
    pub vector_column: String,
    pub target_vector: Vec<f64>,
    pub similarity_function: SimilarityFunction,
    pub threshold: Option<f64>,
    pub k: Option<usize>, // For k-NN queries
}

/// K-nearest neighbors operator
#[derive(Debug, Clone)]
pub struct KNNOperator {
    pub node_id: NodeId,
    pub vector_column: String,
    pub query_vector: Vec<f64>,
    pub k: usize,
    pub distance_function: DistanceFunction,
    pub index_hint: Option<String>,
}

// =============================================================================
// GEOMETRIC OPERATORS
// =============================================================================

/// Hyperbolic distance operator
#[derive(Debug, Clone)]
pub struct HyperbolicDistanceOperator {
    pub node_id: NodeId,
    pub position_column: String,
    pub target_position: Position3D,
    pub distance_threshold: Option<f64>,
    pub output_distance: bool,
}

/// Geometric filtering operator
#[derive(Debug, Clone)]
pub struct GeometricFilterOperator {
    pub node_id: NodeId,
    pub geometric_predicate: GeometricPredicate,
    pub position_column: String,
}

// =============================================================================
// AGGREGATION OPERATORS
// =============================================================================

/// Aggregation operator
#[derive(Debug, Clone)]
pub struct AggregateOperator {
    pub node_id: NodeId,
    pub group_by: Vec<String>,
    pub aggregates: Vec<AggregateFunction>,
    pub having: Option<Predicate>,
    pub input_schema: Schema,
    pub output_schema: Schema,
}

/// Sort operator
#[derive(Debug, Clone)]
pub struct SortOperator {
    pub node_id: NodeId,
    pub sort_keys: Vec<SortKey>,
    pub limit: Option<usize>,
    pub offset: Option<usize>,
}

// =============================================================================
// SUPPORTING TYPES
// =============================================================================

/// Predicate expressions for filtering
#[derive(Debug, Clone)]
pub enum Predicate {
    // Comparison predicates
    Equal(Expression, Expression),
    NotEqual(Expression, Expression),
    LessThan(Expression, Expression),
    LessThanOrEqual(Expression, Expression),
    GreaterThan(Expression, Expression),
    GreaterThanOrEqual(Expression, Expression),
    
    // Logical predicates
    And(Box<Predicate>, Box<Predicate>),
    Or(Box<Predicate>, Box<Predicate>),
    Not(Box<Predicate>),
    
    // Pattern predicates
    Like(Expression, String),
    In(Expression, Vec<Value>),
    Between(Expression, Value, Value),
    IsNull(Expression),
    
    // Hyperbolic predicates
    HyperbolicDistance(Expression, Expression, f64), // position1, position2, max_distance
    VectorSimilarity(Expression, Expression, f64, SimilarityFunction), // vec1, vec2, min_similarity, function
    GeometricWithin(Expression, GeometricShape),
}

/// Expression types
#[derive(Debug, Clone)]
pub enum Expression {
    Column(String),
    Literal(Value),
    Parameter(String),
    Function(String, Vec<Expression>),
    BinaryOp(BinaryOperator, Box<Expression>, Box<Expression>),
    UnaryOp(UnaryOperator, Box<Expression>),
    Case(Vec<(Predicate, Expression)>, Box<Expression>), // (when, then), else
}

/// Binary operators
#[derive(Debug, Clone)]
pub enum BinaryOperator {
    Add, Subtract, Multiply, Divide, Modulo,
    Concat,
    // Hyperbolic operators
    HyperbolicDistance,
    VectorSimilarity(SimilarityFunction),
    VectorDotProduct,
}

/// Unary operators
#[derive(Debug, Clone)]
pub enum UnaryOperator {
    Negate, Absolute,
    // Vector operators
    VectorMagnitude,
    VectorNormalize,
}

/// Column expressions for projection
#[derive(Debug, Clone)]
pub struct ColumnExpression {
    pub expression: Expression,
    pub alias: Option<String>,
}

/// Join types
#[derive(Debug, Clone, PartialEq)]
pub enum JoinType {
    Inner,
    LeftOuter,
    RightOuter,
    FullOuter,
    Cross,
    // Hyperbolic joins
    ProximityJoin(f64), // Join based on hyperbolic distance
    SimilarityJoin(f64, SimilarityFunction), // Join based on vector similarity
}

/// Join conditions
#[derive(Debug, Clone)]
pub enum JoinCondition {
    Equi(Vec<(String, String)>), // (left_column, right_column) pairs
    Theta(Predicate),
    Cross, // No condition (cartesian product)
}

/// Traversal directions
#[derive(Debug, Clone, PartialEq)]
pub enum TraversalDirection {
    Outgoing,
    Incoming,
    Both,
}

/// Path types for path finding
#[derive(Debug, Clone, PartialEq)]
pub enum PathType {
    Shortest,
    All,
    Simple, // No repeated vertices
}

/// Weight functions for path finding
#[derive(Debug, Clone)]
pub enum WeightFunction {
    EdgeWeight,
    HyperbolicDistance,
    Custom(String), // Custom function name
}

/// Similarity functions
#[derive(Debug, Clone, PartialEq)]
pub enum SimilarityFunction {
    Cosine,
    Euclidean,
    Manhattan,
    Jaccard,
    Hamming,
    Custom(String),
}

/// Distance functions
#[derive(Debug, Clone, PartialEq)]
pub enum DistanceFunction {
    Euclidean,
    Manhattan,
    Cosine,
    Hyperbolic,
    Custom(String),
}

/// Geometric predicates
#[derive(Debug, Clone)]
pub enum GeometricPredicate {
    Within(GeometricShape),
    Intersects(GeometricShape),
    Contains(GeometricShape),
    DistanceLessThan(Position3D, f64),
    DistanceGreaterThan(Position3D, f64),
}

/// Geometric shapes
#[derive(Debug, Clone)]
pub enum GeometricShape {
    Circle(Position3D, f64), // center, radius
    Rectangle(Position3D, Position3D), // bottom-left, top-right
    Polygon(Vec<Position3D>),
    HyperbolicBall(Position3D, f64), // center, hyperbolic radius
}

/// Aggregate functions
#[derive(Debug, Clone)]
pub enum AggregateFunction {
    Count,
    CountDistinct(String), // column name
    Sum(String),
    Average(String),
    Min(String),
    Max(String),
    StdDev(String),
    Variance(String),
    // Hyperbolic aggregates
    GeometricMean(String), // For positions
    VectorSum(String),     // For vectors
    VectorAverage(String), // For vectors
    MeasureSum(String),    // For measures
    MeasureAverage(String), // For measures
}

/// Sort keys
#[derive(Debug, Clone)]
pub struct SortKey {
    pub expression: Expression,
    pub direction: SortDirection,
    pub null_ordering: NullOrdering,
}

/// Sort directions
#[derive(Debug, Clone, PartialEq)]
pub enum SortDirection {
    Ascending,
    Descending,
}

/// Null ordering
#[derive(Debug, Clone, PartialEq)]
pub enum NullOrdering {
    First,
    Last,
}

// =============================================================================
// EXECUTION SUPPORT TYPES
// =============================================================================

/// Execution context
#[derive(Debug, Clone)]
pub struct ExecutionContext {
    pub query_id: String,
    pub user_id: Option<String>,
    pub parameters: HashMap<String, Value>,
    pub session_variables: HashMap<String, Value>,
    pub transaction_id: Option<String>,
}

/// Execution hints
#[derive(Debug, Clone, Default)]
pub struct ExecutionHints {
    pub prefer_parallel: bool,
    pub max_memory_mb: Option<usize>,
    pub timeout_seconds: Option<u64>,
    pub index_hints: Vec<String>,
}

/// Execution metadata
#[derive(Debug, Clone, Default)]
pub struct ExecutionMetadata {
    pub rows_processed: usize,
    pub execution_time_ms: u64,
    pub memory_used_bytes: usize,
    pub warnings: Vec<String>,
}

/// Operator metrics
#[derive(Debug, Clone, Default)]
pub struct OperatorMetrics {
    pub cpu_time_ms: u64,
    pub wall_time_ms: u64,
    pub memory_peak_bytes: usize,
    pub io_operations: u64,
    pub cache_hits: u64,
    pub cache_misses: u64,
}

/// Row metadata
#[derive(Debug, Clone, Default)]
pub struct RowMetadata {
    pub source_collection: Option<String>,
    pub source_row_id: Option<String>,
    pub version: Option<u64>,
    pub timestamp: Option<chrono::DateTime<chrono::Utc>>,
}

/// Positioning information for schema
#[derive(Debug, Clone)]
pub struct PositioningInfo {
    pub position_column: String,
    pub coordinate_system: CoordinateSystem,
    pub index_type: Option<String>,
}

/// Coordinate systems
#[derive(Debug, Clone, PartialEq)]
pub enum CoordinateSystem {
    Cartesian,
    Polar,
    Hyperbolic,
    Spherical,
}

/// Execution cost estimation
#[derive(Debug, Clone)]
pub struct ExecutionCost {
    pub cpu_cost: f64,
    pub memory_cost: f64,
    pub io_cost: f64,
    pub network_cost: f64,
    pub estimated_rows: usize,
}

/// Operator statistics
#[derive(Debug, Clone, Default)]
pub struct OperatorStats {
    pub executions: u64,
    pub total_time_ms: u64,
    pub rows_processed: u64,
    pub bytes_processed: u64,
    pub errors: u32,
}

/// Memory requirements
#[derive(Debug, Clone)]
pub struct MemoryRequirement {
    pub minimum_bytes: usize,
    pub optimal_bytes: usize,
    pub maximum_bytes: usize,
    pub scales_with_input: bool,
}

// TODO: Implement operator execution logic for each operator type
// TODO: Add support for custom operator plugins
// TODO: Implement operator parallelization strategies
// TODO: Add memory management and spilling for large operators
// TODO: Implement operator fusion optimizations
// TODO: Add comprehensive operator testing framework
// TODO: Implement operator performance profiling
// TODO: Add support for streaming operators