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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
use crate::ast::*;
use crate::error::*;

use super::expression::ExpressionCompiler;
use super::{CompiledProjection, CompiledSortKey, CompiledExpression, ExecutionPlan, VectorOpType};
use std::collections::HashMap;

pub struct SelectCompiler {
    expression_compiler: ExpressionCompiler,
}

impl SelectCompiler {
    pub fn new() -> Self {
        Self {
            expression_compiler: ExpressionCompiler::new(),
        }
    }

    pub fn compile_select(&self, select: SelectStatement) -> Result<ExecutionPlan> {
        let mut plan = self.create_base_scan(&select)?;

        // Handle JOINs after base scan
        if !select.joins.is_empty() {
            plan = self.compile_joins(plan, &select.joins)?;
        }

        if let Some(traverse_clause) = select.traverse_clause {
            let compiled_patterns = self.compile_traverse_patterns(&traverse_clause.patterns)?;
            let traverse_plan = ExecutionPlan::Traverse {
                patterns: compiled_patterns,
            };
            if let ExecutionPlan::Scan { .. } = plan {
                plan = traverse_plan;
            } else {
                plan = traverse_plan;
            }
        }

        // CRITICAL: Check for vector/geometric operations in WHERE clause
        if let Some(where_expr) = select.where_clause {
            let compiled_predicate = self.expression_compiler.compile_expression(where_expr)?;

            // Detect if this is a geometric operation that should use GeometricOperation plan
            if self.is_geometric_function(&compiled_predicate) {
                // Generate GeometricOperation plan instead of Filter plan
                plan = self.create_geometric_operation_plan(compiled_predicate, plan)?;
            } else if self.is_vector_function(&compiled_predicate) {
                // Generate VectorOperation plan instead of Filter plan
                plan = self.create_vector_operation_plan(compiled_predicate, plan)?;
            } else {
                // Regular filter
                plan = ExecutionPlan::Filter {
                    input: Box::new(plan),
                    predicate: compiled_predicate,
                };
            }
        }

        // Compile projections first to check for aggregate functions
        let projections = self.compile_select_list(&select.select_list)?;

        // Check if any projection contains an aggregate function
        let has_aggregates = projections.iter().any(|proj| self.contains_aggregate(&proj.expression));

        if !select.group_by.is_empty() || has_aggregates {
            // GROUP BY with explicit grouping columns, or implicit grouping (aggregates without GROUP BY)
            let group_expressions = if !select.group_by.is_empty() {
                select.group_by.iter()
                    .map(|expr| self.expression_compiler.compile_expression(expr.clone()))
                    .collect::<Result<Vec<_>>>()?
            } else {
                // No GROUP BY clause but has aggregates - implicit single-group aggregation
                vec![]
            };

            plan = ExecutionPlan::GroupBy {
                input: Box::new(plan),
                group_expressions,
                aggregate_expressions: projections,
            };
        } else {
            // No aggregates - regular projection
            if !projections.is_empty() {
                plan = ExecutionPlan::Project {
                    input: Box::new(plan),
                    expressions: projections,
                    distinct: select.distinct,
                };
            }
        }

        if let Some(having_expr) = select.having {
            let compiled_having = self.expression_compiler.compile_expression(having_expr)?;
            plan = ExecutionPlan::Having {
                input: Box::new(plan),
                predicate: compiled_having,
            };
        }

        // CRITICAL: Check for vector operations in ORDER BY (k-NN pattern)
        if !select.order_by.is_empty() {
            // Check if ORDER BY uses a vector distance function (k-NN pattern)
            if let Some(vector_plan) = self.try_create_knn_plan(&select.order_by, select.limit, plan.clone())? {
                plan = vector_plan;
            } else {
                // Regular sort
                let sort_keys = self.compile_order_by(&select.order_by)?;
                plan = ExecutionPlan::Sort {
                    input: Box::new(plan),
                    sort_keys,
                };
            }
        }

        if let Some(limit) = select.limit {
            plan = ExecutionPlan::Limit {
                input: Box::new(plan),
                count: limit,
                offset: select.offset,
            };
        }

        Ok(plan)
    }

    fn create_base_scan(&self, select: &SelectStatement) -> Result<ExecutionPlan> {
        let (table_name, entity_type, alias) = match &select.from {
            Some(FromClause::Table { collection, entity_type, alias }) => {
                // Convert Option<String> to String (use empty string when None)
                let entity_type_str = entity_type.as_ref().map(|s| s.clone()).unwrap_or_default();
                (collection.clone(), entity_type_str, alias.clone())
            }
            Some(FromClause::Subquery { .. }) => {
                return Err(HyperQLError::SemanticError {
                    message: "Subqueries are not yet supported".to_string(),
                    context: vec!["compiler".to_string()],
                });
            }
            None => {
                return Err(HyperQLError::SemanticError {
                    message: "FROM clause is required".to_string(),
                    context: vec!["compiler".to_string()],
                });
            }
        };

        // CRITICAL OPTIMIZATION: Pass LIMIT down to Scan when query is simple enough
        // Push LIMIT to scan when it's safe for early termination
        // Safe cases:
        // 1. No WHERE/ORDER BY/GROUP BY/HAVING: Can stop after LIMIT entities
        // 2. WHERE only (no ORDER BY/GROUP BY): Can stop after finding LIMIT matching entities
        //
        // NOT safe:
        // - ORDER BY: Need all entities to sort correctly
        // - GROUP BY: Need all entities to compute groups
        // - HAVING: Need all groups to filter
        let scan_limit = if select.where_clause.is_none()
            && select.order_by.is_empty()
            && select.group_by.is_empty()
            && select.having.is_none() {
            // No filtering/sorting/grouping: safe to push LIMIT
            select.limit
        } else if select.where_clause.is_some()
            && select.order_by.is_empty()
            && select.group_by.is_empty()
            && select.having.is_none() {
            // WHERE-only queries: push LIMIT for early termination
            // The scan will stop after finding LIMIT matching entities
            select.limit
        } else {
            // ORDER BY or GROUP BY present: need all data
            None
        };

        Ok(ExecutionPlan::Scan {
            table: table_name,
            entity_type,
            alias,
            filter: None,
            projection: vec![],
            limit: scan_limit,
        })
    }

    fn compile_select_list(&self, select_list: &[SelectItem]) -> Result<Vec<CompiledProjection>> {
        let mut projections = Vec::new();

        for item in select_list {
            match item {
                SelectItem::Wildcard => {
                    projections.push(CompiledProjection {
                        expression: CompiledExpression::Literal(crate::types::Value::String("*".to_string())),
                        alias: None,
                        output_name: "*".to_string(),
                    });
                }
                SelectItem::Expression { expr, alias } => {
                    let compiled_expr = self.expression_compiler.compile_expression(expr.clone())?;
                    let output_name = alias.clone().unwrap_or_else(|| {
                        self.generate_expression_name(expr)
                    });

                    projections.push(CompiledProjection {
                        expression: compiled_expr,
                        alias: alias.clone(),
                        output_name,
                    });
                }
            }
        }

        Ok(projections)
    }

    fn compile_order_by(&self, order_by: &[OrderByItem]) -> Result<Vec<CompiledSortKey>> {
        let mut sort_keys = Vec::new();

        for item in order_by {
            let compiled_expr = self.expression_compiler.compile_expression(item.expr.clone())?;
            sort_keys.push(CompiledSortKey {
                expression: compiled_expr,
                direction: item.direction.clone(),
            });
        }

        Ok(sort_keys)
    }

    fn generate_expression_name(&self, expr: &Expression) -> String {
        match expr {
            Expression::Column(col_ref) => {
                // Include table prefix if present
                if let Some(ref table) = col_ref.table {
                    format!("{}.{}", table, col_ref.name)
                } else {
                    col_ref.name.clone()
                }
            }
            Expression::Function { name, args } => {
                // Generate proper function call syntax for better column naming
                if args.is_empty() {
                    // COUNT() with no args
                    format!("{}(*)", name)
                } else if args.len() == 1 {
                    // Check if arg is a wildcard column reference
                    if let Expression::Column(col_ref) = &args[0] {
                        if col_ref.name == "*" {
                            return format!("{}(*)", name);
                        }
                        // For column references, include the column name
                        return format!("{}({})", name, self.generate_expression_name(&args[0]));
                    }
                    // For other single-arg functions
                    format!("{}({})", name, self.generate_expression_name(&args[0]))
                } else {
                    // Multi-arg functions
                    format!("{}(...)", name)
                }
            }
            Expression::Literal(literal) => format!("{:?}", literal),
            _ => "expr".to_string(),
        }
    }

    fn compile_traverse_patterns(&self, patterns: &[crate::ast::TraversePattern]) -> Result<Vec<super::CompiledTraversePattern>> {
        let mut compiled_patterns = Vec::new();

        for pattern in patterns {
            let compiled_start_node = super::CompiledNodePattern {
                variable: pattern.start_node.variable.clone(),
                label: pattern.start_node.label.clone(),
                properties: if let Some(props) = &pattern.start_node.properties {
                    Some(self.expression_compiler.compile_expression(props.clone())?)
                } else {
                    None
                },
            };

            let compiled_end_node = super::CompiledNodePattern {
                variable: pattern.end_node.variable.clone(),
                label: pattern.end_node.label.clone(),
                properties: if let Some(props) = &pattern.end_node.properties {
                    Some(self.expression_compiler.compile_expression(props.clone())?)
                } else {
                    None
                },
            };

            let compiled_relationship = super::CompiledRelationshipPattern {
                variable: pattern.relationship.variable.clone(),
                rel_type: pattern.relationship.rel_type.clone(),
                direction: pattern.relationship.direction.clone(),
                variable_length: pattern.relationship.variable_length.clone(),
                optional: pattern.relationship.optional,
                properties: if let Some(props) = &pattern.relationship.properties {
                    Some(self.expression_compiler.compile_expression(props.clone())?)
                } else {
                    None
                },
            };

            compiled_patterns.push(super::CompiledTraversePattern {
                start_node: compiled_start_node,
                relationship: compiled_relationship,
                end_node: compiled_end_node,
            });
        }

        Ok(compiled_patterns)
    }

    /// Check if an expression contains an aggregate function (COUNT, SUM, AVG, MIN, MAX, etc.)
    fn contains_aggregate(&self, expr: &CompiledExpression) -> bool {
        match expr {
            CompiledExpression::Function { name, args, .. } => {
                // Check if this function is an aggregate
                let is_aggregate = matches!(
                    name.to_uppercase().as_str(),
                    "COUNT" | "SUM" | "AVG" | "MIN" | "MAX" | "ARRAY_AGG" | "STRING_AGG"
                );

                if is_aggregate {
                    return true;
                }

                // Recursively check args for nested aggregates
                args.iter().any(|arg| self.contains_aggregate(arg))
            }
            CompiledExpression::Binary { left, right, .. } => {
                self.contains_aggregate(left) || self.contains_aggregate(right)
            }
            CompiledExpression::Unary { expr, .. } => {
                self.contains_aggregate(expr)
            }
            _ => false,
        }
    }

    /// Compile JOIN clauses into execution plan
    fn compile_joins(&self, left_plan: ExecutionPlan, joins: &[JoinClause]) -> Result<ExecutionPlan> {
        let mut current_plan = left_plan;

        for join_clause in joins {
            // Convert Option<String> to String (use empty string when None)
            let entity_type_str = join_clause.entity_type.as_ref().map(|s| s.clone()).unwrap_or_default();

            // Create scan plan for the right table with alias
            let right_plan = ExecutionPlan::Scan {
                table: join_clause.collection.clone(),
                entity_type: entity_type_str,
                alias: join_clause.alias.clone(),
                filter: None,
                projection: vec![],
                limit: None,
            };

            // Compile the ON condition
            let compiled_condition = self.expression_compiler.compile_expression(join_clause.on_condition.clone())?;

            // Create JOIN plan
            current_plan = ExecutionPlan::Join {
                left: Box::new(current_plan),
                right: Box::new(right_plan),
                join_type: join_clause.join_type.clone(),
                on_condition: compiled_condition,
            };
        }

        Ok(current_plan)
    }

    /// Check if an expression contains a geometric function
    fn is_geometric_function(&self, expr: &CompiledExpression) -> bool {
        match expr {
            CompiledExpression::Function { name, args, .. } => {
                // Check if this is a geometric function
                // Note: "near" and "distance" are parser-generated names (not from ExpressionCompiler)
                let is_geometric = matches!(
                    name.to_uppercase().as_str(),
                    "HYPERBOLIC_DISTANCE" | "GEODESIC_DISTANCE" |
                    "WITHIN_RADIUS" | "NEAR_POSITIONS" |
                    "CONTAINS" | "INTERSECTS" |
                    "NEAR" | "DISTANCE"  // Parser-generated geometric functions
                );

                if is_geometric {
                    return true;
                }

                // Recursively check args for nested geometric functions
                args.iter().any(|arg| self.is_geometric_function(arg))
            }
            CompiledExpression::Binary { left, right, .. } => {
                self.is_geometric_function(left) || self.is_geometric_function(right)
            }
            CompiledExpression::Unary { expr, .. } => {
                self.is_geometric_function(expr)
            }
            _ => false,
        }
    }

    /// Extract the geometric operation type from a function name
    fn function_name_to_geometric_op_type(&self, name: &str) -> Option<super::GeometricOpType> {
        match name.to_uppercase().as_str() {
            "HYPERBOLIC_DISTANCE" => Some(super::GeometricOpType::HyperbolicDistance),
            "GEODESIC_DISTANCE" => Some(super::GeometricOpType::GeodesicDistance),
            "WITHIN_RADIUS" => Some(super::GeometricOpType::WithinRadius),
            "NEAR_POSITIONS" => Some(super::GeometricOpType::NearPositions),
            "CONTAINS" => Some(super::GeometricOpType::Contains),
            "INTERSECTS" => Some(super::GeometricOpType::Intersects),
            // Parser-generated function names
            "NEAR" => Some(super::GeometricOpType::NearPositions),
            "DISTANCE" => Some(super::GeometricOpType::HyperbolicDistance),
            _ => None,
        }
    }

    /// Extract the primary geometric function from an expression tree
    fn extract_geometric_function(&self, expr: &CompiledExpression) -> Option<(super::GeometricOpType, Vec<CompiledExpression>)> {
        match expr {
            CompiledExpression::Function { name, args, .. } => {
                if let Some(op_type) = self.function_name_to_geometric_op_type(name) {
                    return Some((op_type, args.clone()));
                }
                // Search in arguments
                for arg in args {
                    if let Some(result) = self.extract_geometric_function(arg) {
                        return Some(result);
                    }
                }
                None
            }
            CompiledExpression::Binary { left, right, .. } => {
                // Check left first, then right
                self.extract_geometric_function(left)
                    .or_else(|| self.extract_geometric_function(right))
            }
            CompiledExpression::Unary { expr, .. } => {
                self.extract_geometric_function(expr)
            }
            _ => None,
        }
    }

    /// Create a GeometricOperation execution plan from a compiled expression
    fn create_geometric_operation_plan(
        &self,
        expr: CompiledExpression,
        input: ExecutionPlan,
    ) -> Result<ExecutionPlan> {
        // Extract the geometric function and its parameters
        let (op_type, args) = self.extract_geometric_function(&expr)
            .ok_or_else(|| HyperQLError::SemanticError {
                message: "No geometric function found in expression".to_string(),
                context: vec!["create_geometric_operation_plan".to_string()],
            })?;

        // Build parameter map from function arguments
        let mut params = HashMap::new();

        // Map arguments based on operation type
        match op_type {
            super::GeometricOpType::HyperbolicDistance | super::GeometricOpType::GeodesicDistance => {
                // Parser-generated "distance" function: args = [reference]
                // Compiled HYPERBOLIC_DISTANCE: args = [entity1, entity2]
                if args.len() == 1 {
                    // Parser format: distance(reference)
                    // Create column reference for "position" and use reference as entity2
                    params.insert("entity1".to_string(), CompiledExpression::Column {
                        table: None,
                        name: "position".to_string(),
                        value_type: super::ValueType::Position,
                    });
                    params.insert("entity2".to_string(), args[0].clone());
                } else if args.len() >= 2 {
                    // Compiled format
                    params.insert("entity1".to_string(), args[0].clone());
                    params.insert("entity2".to_string(), args[1].clone());
                }
                // Optional third argument for max_distance in NEAR context
                if args.len() >= 3 {
                    params.insert("max_distance".to_string(), args[2].clone());
                }
            }
            super::GeometricOpType::WithinRadius => {
                // WITHIN_RADIUS takes: target, reference, radius
                if args.len() >= 3 {
                    params.insert("target".to_string(), args[0].clone());
                    params.insert("center".to_string(), args[1].clone());
                    params.insert("radius".to_string(), args[2].clone());
                }
            }
            super::GeometricOpType::NearPositions => {
                // Parser-generated "near" function: args = [reference, radius]
                // Compiled NEAR_POSITIONS: args = [reference, limit]
                if args.len() >= 2 {
                    // Parser format: near(reference, radius)
                    params.insert("reference".to_string(), args[0].clone());
                    params.insert("radius".to_string(), args[1].clone());
                } else if !args.is_empty() {
                    // Compiled format
                    params.insert("reference".to_string(), args[0].clone());
                    if args.len() >= 2 {
                        params.insert("limit".to_string(), args[1].clone());
                    }
                }
            }
            super::GeometricOpType::Contains | super::GeometricOpType::Intersects => {
                // These take: geometry1, geometry2
                if args.len() >= 2 {
                    params.insert("geometry1".to_string(), args[0].clone());
                    params.insert("geometry2".to_string(), args[1].clone());
                }
            }
        }

        // Store the full expression for evaluation
        params.insert("_full_expression".to_string(), expr);

        Ok(ExecutionPlan::GeometricOperation {
            op_type,
            params,
            input: Some(Box::new(input)),
        })
    }

    /// Check if an expression contains a vector function
    fn is_vector_function(&self, expr: &CompiledExpression) -> bool {
        match expr {
            CompiledExpression::Function { name, args, .. } => {
                // Check if this is a vector function
                let is_vector = matches!(
                    name.to_uppercase().as_str(),
                    "COSINE_SIMILARITY" | "EUCLIDEAN_DISTANCE" |
                    "DOT_PRODUCT" | "NORMALIZE" |
                    "KNN" | "SIMILARITY_SEARCH"
                );

                if is_vector {
                    return true;
                }

                // Recursively check args for nested vector functions
                args.iter().any(|arg| self.is_vector_function(arg))
            }
            CompiledExpression::Binary { left, right, .. } => {
                self.is_vector_function(left) || self.is_vector_function(right)
            }
            CompiledExpression::Unary { expr, .. } => {
                self.is_vector_function(expr)
            }
            _ => false,
        }
    }

    /// Extract the vector operation type from a function name
    fn function_name_to_vector_op_type(&self, name: &str) -> Option<VectorOpType> {
        match name.to_uppercase().as_str() {
            "COSINE_SIMILARITY" => Some(VectorOpType::CosineSimilarity),
            "EUCLIDEAN_DISTANCE" => Some(VectorOpType::EuclideanDistance),
            "DOT_PRODUCT" => Some(VectorOpType::DotProduct),
            "NORMALIZE" => Some(VectorOpType::Normalize),
            "KNN" => Some(VectorOpType::KNN),
            "SIMILARITY_SEARCH" => Some(VectorOpType::SimilaritySearch),
            _ => None,
        }
    }

    /// Extract the primary vector function from an expression tree
    fn extract_vector_function(&self, expr: &CompiledExpression) -> Option<(VectorOpType, Vec<CompiledExpression>)> {
        match expr {
            CompiledExpression::Function { name, args, .. } => {
                if let Some(op_type) = self.function_name_to_vector_op_type(name) {
                    return Some((op_type, args.clone()));
                }
                // Search in arguments
                for arg in args {
                    if let Some(result) = self.extract_vector_function(arg) {
                        return Some(result);
                    }
                }
                None
            }
            CompiledExpression::Binary { left, right, .. } => {
                // Check left first, then right
                self.extract_vector_function(left)
                    .or_else(|| self.extract_vector_function(right))
            }
            CompiledExpression::Unary { expr, .. } => {
                self.extract_vector_function(expr)
            }
            _ => None,
        }
    }

    /// Create a VectorOperation execution plan from a compiled expression
    fn create_vector_operation_plan(
        &self,
        expr: CompiledExpression,
        input: ExecutionPlan,
    ) -> Result<ExecutionPlan> {
        // Extract the vector function and its parameters
        let (op_type, args) = self.extract_vector_function(&expr)
            .ok_or_else(|| HyperQLError::SemanticError {
                message: "No vector function found in expression".to_string(),
                context: vec!["create_vector_operation_plan".to_string()],
            })?;

        // Build parameter map from function arguments
        let mut params = HashMap::new();

        // Map arguments based on operation type
        match op_type {
            VectorOpType::CosineSimilarity | VectorOpType::EuclideanDistance | VectorOpType::DotProduct => {
                // These take: vector_name, reference, metric, threshold, vector_type
                if args.len() >= 2 {
                    params.insert("vector_name".to_string(), args[0].clone());
                    params.insert("reference".to_string(), args[1].clone());
                }
                if args.len() >= 3 {
                    params.insert("metric".to_string(), args[2].clone());
                }
                if args.len() >= 4 {
                    params.insert("threshold".to_string(), args[3].clone());
                }
                if args.len() >= 5 {
                    params.insert("vector_type".to_string(), args[4].clone());
                }
            }
            VectorOpType::KNN => {
                // KNN takes: vector_name, reference, k, metric, vector_type
                if args.len() >= 3 {
                    params.insert("vector_name".to_string(), args[0].clone());
                    params.insert("reference".to_string(), args[1].clone());
                    params.insert("k".to_string(), args[2].clone());
                }
                if args.len() >= 4 {
                    params.insert("metric".to_string(), args[3].clone());
                }
                if args.len() >= 5 {
                    params.insert("vector_type".to_string(), args[4].clone());
                }
            }
            VectorOpType::Normalize => {
                // Normalize takes: vector
                if !args.is_empty() {
                    params.insert("vector".to_string(), args[0].clone());
                }
            }
            VectorOpType::SimilaritySearch => {
                // SimilaritySearch takes similar args to KNN
                if args.len() >= 2 {
                    params.insert("vector_name".to_string(), args[0].clone());
                    params.insert("reference".to_string(), args[1].clone());
                }
                if args.len() >= 3 {
                    params.insert("metric".to_string(), args[2].clone());
                }
                if args.len() >= 4 {
                    params.insert("threshold".to_string(), args[3].clone());
                }
            }
        }

        // Store the full expression for evaluation
        params.insert("_full_expression".to_string(), expr);

        Ok(ExecutionPlan::VectorOperation {
            op_type,
            params,
            input: Some(Box::new(input)),
        })
    }

    /// Try to create a k-NN plan from ORDER BY clause
    /// This handles the pattern: ORDER BY distance_function(...) LIMIT k
    fn try_create_knn_plan(
        &self,
        order_by: &[OrderByItem],
        limit: Option<u64>,
        input: ExecutionPlan,
    ) -> Result<Option<ExecutionPlan>> {
        // k-NN pattern requires exactly one ORDER BY expression
        if order_by.len() != 1 {
            return Ok(None);
        }

        // Compile the ORDER BY expression
        let order_expr = self.expression_compiler.compile_expression(order_by[0].expr.clone())?;

        // Check if it's a vector distance function
        if !self.is_vector_function(&order_expr) {
            return Ok(None);
        }

        // Extract the vector function
        let (op_type, args) = match self.extract_vector_function(&order_expr) {
            Some(result) => result,
            None => return Ok(None),
        };

        // Only distance functions make sense for k-NN
        if !matches!(op_type, VectorOpType::CosineSimilarity | VectorOpType::EuclideanDistance | VectorOpType::DotProduct) {
            return Ok(None);
        }

        // Build KNN operation parameters
        let mut params = HashMap::new();

        if args.len() >= 2 {
            params.insert("vector_name".to_string(), args[0].clone());
            params.insert("reference".to_string(), args[1].clone());
        }
        if args.len() >= 3 {
            params.insert("metric".to_string(), args[2].clone());
        }
        if args.len() >= 5 {
            params.insert("vector_type".to_string(), args[4].clone());
        }

        // Add k from LIMIT clause
        if let Some(k) = limit {
            params.insert("k".to_string(), CompiledExpression::Literal(crate::types::Value::Int(k as i64)));
        }

        // Add sort direction (ASC for distance = nearest first)
        let direction = match order_by[0].direction {
            OrderDirection::Asc => "asc",
            OrderDirection::Desc => "desc",
        };
        params.insert("direction".to_string(), CompiledExpression::Literal(crate::types::Value::String(direction.to_string())));

        Ok(Some(ExecutionPlan::VectorOperation {
            op_type: VectorOpType::KNN,
            params,
            input: Some(Box::new(input)),
        }))
    }
}

impl Default for SelectCompiler {
    fn default() -> Self {
        Self::new()
    }
}