sql-cli 1.70.0

SQL query tool for CSV/JSON with both interactive TUI and non-interactive CLI modes - perfect for exploration and automation
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
use anyhow::{anyhow, Result};

use crate::data::datatable::DataValue;
use crate::sql::functions::{ArgCount, FunctionCategory, FunctionSignature, SqlFunction};

/// Helper function to extract vector from DataValue
fn get_vector(value: &DataValue) -> Result<Vec<f64>> {
    match value {
        DataValue::Vector(v) => Ok(v.clone()),
        DataValue::String(s) => parse_vector_string(s),
        _ => Err(anyhow!("Expected vector, got {:?}", value.data_type())),
    }
}

/// Parse vector from string representation: "[1,2,3]" or "1 2 3"
fn parse_vector_string(s: &str) -> Result<Vec<f64>> {
    let trimmed = s.trim();

    // Handle "[1,2,3]" format
    let content = if trimmed.starts_with('[') && trimmed.ends_with(']') {
        &trimmed[1..trimmed.len() - 1]
    } else {
        trimmed
    };

    // Parse components (comma or space separated)
    let components: Result<Vec<f64>> = if content.contains(',') {
        content
            .split(',')
            .map(|s| {
                s.trim()
                    .parse::<f64>()
                    .map_err(|e| anyhow!("Failed to parse vector component '{}': {}", s.trim(), e))
            })
            .collect()
    } else {
        content
            .split_whitespace()
            .map(|s| {
                s.parse::<f64>()
                    .map_err(|e| anyhow!("Failed to parse vector component '{}': {}", s, e))
            })
            .collect()
    };

    components
}

/// VEC(x, y, z, ...) - Construct a vector from components
pub struct VecFunction;

impl SqlFunction for VecFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "VEC",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Variadic,
            description: "Construct a vector from numeric components",
            returns: "Vector",
            examples: vec!["SELECT VEC(1, 2, 3)", "SELECT VEC(10, 20)"],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        if args.is_empty() {
            return Err(anyhow!("VEC() requires at least one argument"));
        }

        let components: Result<Vec<f64>> = args
            .iter()
            .map(|arg| match arg {
                DataValue::Integer(i) => Ok(*i as f64),
                DataValue::Float(f) => Ok(*f),
                DataValue::Null => Err(anyhow!("Cannot create vector with NULL component")),
                _ => Err(anyhow!(
                    "VEC() requires numeric arguments, got {:?}",
                    arg.data_type()
                )),
            })
            .collect();

        Ok(DataValue::Vector(components?))
    }
}

/// VEC_ADD(v1, v2) - Add two vectors element-wise
pub struct VecAddFunction;

impl SqlFunction for VecAddFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "VEC_ADD",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(2),
            description: "Add two vectors element-wise",
            returns: "Vector",
            examples: vec![
                "SELECT VEC_ADD(VEC(1,2,3), VEC(4,5,6))",
                "SELECT VEC_ADD(position, velocity)",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let v1 = get_vector(&args[0])?;
        let v2 = get_vector(&args[1])?;

        if v1.len() != v2.len() {
            return Err(anyhow!(
                "Vector dimension mismatch: {} != {}",
                v1.len(),
                v2.len()
            ));
        }

        let result: Vec<f64> = v1.iter().zip(v2.iter()).map(|(a, b)| a + b).collect();
        Ok(DataValue::Vector(result))
    }
}

/// VEC_SUB(v1, v2) - Subtract two vectors element-wise
pub struct VecSubFunction;

impl SqlFunction for VecSubFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "VEC_SUB",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(2),
            description: "Subtract two vectors element-wise (v1 - v2)",
            returns: "Vector",
            examples: vec![
                "SELECT VEC_SUB(VEC(10,20,30), VEC(1,2,3))",
                "SELECT VEC_SUB(position_end, position_start)",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let v1 = get_vector(&args[0])?;
        let v2 = get_vector(&args[1])?;

        if v1.len() != v2.len() {
            return Err(anyhow!(
                "Vector dimension mismatch: {} != {}",
                v1.len(),
                v2.len()
            ));
        }

        let result: Vec<f64> = v1.iter().zip(v2.iter()).map(|(a, b)| a - b).collect();
        Ok(DataValue::Vector(result))
    }
}

/// VEC_SCALE(vector, scalar) - Multiply vector by scalar
pub struct VecScaleFunction;

impl SqlFunction for VecScaleFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "VEC_SCALE",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(2),
            description: "Multiply vector by scalar value",
            returns: "Vector",
            examples: vec![
                "SELECT VEC_SCALE(VEC(1,2,3), 2.5)",
                "SELECT VEC_SCALE(velocity, time)",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let v = get_vector(&args[0])?;
        let scalar = match &args[1] {
            DataValue::Integer(i) => *i as f64,
            DataValue::Float(f) => *f,
            _ => {
                return Err(anyhow!(
                    "Scalar must be numeric, got {:?}",
                    args[1].data_type()
                ))
            }
        };

        let result: Vec<f64> = v.iter().map(|x| x * scalar).collect();
        Ok(DataValue::Vector(result))
    }
}

/// VEC_DOT(v1, v2) - Compute dot product
pub struct VecDotFunction;

impl SqlFunction for VecDotFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "VEC_DOT",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(2),
            description: "Compute dot product of two vectors",
            returns: "Float",
            examples: vec![
                "SELECT VEC_DOT(VEC(1,2,3), VEC(4,5,6))",
                "SELECT VEC_DOT(velocity1, velocity2)",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let v1 = get_vector(&args[0])?;
        let v2 = get_vector(&args[1])?;

        if v1.len() != v2.len() {
            return Err(anyhow!(
                "Vector dimension mismatch: {} != {}",
                v1.len(),
                v2.len()
            ));
        }

        let dot_product: f64 = v1.iter().zip(v2.iter()).map(|(a, b)| a * b).sum();
        Ok(DataValue::Float(dot_product))
    }
}

/// VEC_MAG(vector) - Compute magnitude (length) of vector
pub struct VecMagFunction;

impl SqlFunction for VecMagFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "VEC_MAG",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(1),
            description: "Compute magnitude (length) of a vector",
            returns: "Float",
            examples: vec!["SELECT VEC_MAG(VEC(3,4))", "SELECT VEC_MAG(velocity)"],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let v = get_vector(&args[0])?;
        let magnitude = v.iter().map(|x| x * x).sum::<f64>().sqrt();
        Ok(DataValue::Float(magnitude))
    }
}

/// VEC_NORMALIZE(vector) - Normalize vector to unit length
pub struct VecNormalizeFunction;

impl SqlFunction for VecNormalizeFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "VEC_NORMALIZE",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(1),
            description: "Normalize vector to unit length",
            returns: "Vector",
            examples: vec![
                "SELECT VEC_NORMALIZE(VEC(3,4))",
                "SELECT VEC_NORMALIZE(direction)",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let v = get_vector(&args[0])?;
        let magnitude = v.iter().map(|x| x * x).sum::<f64>().sqrt();

        if magnitude == 0.0 {
            return Err(anyhow!("Cannot normalize zero vector"));
        }

        let normalized: Vec<f64> = v.iter().map(|x| x / magnitude).collect();
        Ok(DataValue::Vector(normalized))
    }
}

/// VEC_DISTANCE(v1, v2) - Compute Euclidean distance between two vectors
pub struct VecDistanceFunction;

impl SqlFunction for VecDistanceFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "VEC_DISTANCE",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(2),
            description: "Compute Euclidean distance between two vectors",
            returns: "Float",
            examples: vec![
                "SELECT VEC_DISTANCE(VEC(0,0), VEC(3,4))",
                "SELECT VEC_DISTANCE(position1, position2)",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let v1 = get_vector(&args[0])?;
        let v2 = get_vector(&args[1])?;

        if v1.len() != v2.len() {
            return Err(anyhow!(
                "Vector dimension mismatch: {} != {}",
                v1.len(),
                v2.len()
            ));
        }

        let distance = v1
            .iter()
            .zip(v2.iter())
            .map(|(a, b)| (a - b).powi(2))
            .sum::<f64>()
            .sqrt();

        Ok(DataValue::Float(distance))
    }
}

/// VEC_CROSS(v1, v2) - Compute cross product (3D only)
pub struct VecCrossFunction;

impl SqlFunction for VecCrossFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "VEC_CROSS",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(2),
            description: "Compute cross product of two 3D vectors",
            returns: "Vector",
            examples: vec![
                "SELECT VEC_CROSS(VEC(1,0,0), VEC(0,1,0))",
                "SELECT VEC_CROSS(velocity, force)",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let v1 = get_vector(&args[0])?;
        let v2 = get_vector(&args[1])?;

        if v1.len() != 3 || v2.len() != 3 {
            return Err(anyhow!(
                "VEC_CROSS requires 3D vectors, got dimensions {} and {}",
                v1.len(),
                v2.len()
            ));
        }

        let cross = vec![
            v1[1] * v2[2] - v1[2] * v2[1],
            v1[2] * v2[0] - v1[0] * v2[2],
            v1[0] * v2[1] - v1[1] * v2[0],
        ];

        Ok(DataValue::Vector(cross))
    }
}

/// VEC_ANGLE(v1, v2) - Compute angle between two vectors in radians
pub struct VecAngleFunction;

impl SqlFunction for VecAngleFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "VEC_ANGLE",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(2),
            description: "Compute angle between two vectors in radians",
            returns: "Float",
            examples: vec![
                "SELECT VEC_ANGLE(VEC(1,0), VEC(0,1))",
                "SELECT VEC_ANGLE(direction1, direction2)",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let v1 = get_vector(&args[0])?;
        let v2 = get_vector(&args[1])?;

        if v1.len() != v2.len() {
            return Err(anyhow!(
                "Vector dimension mismatch: {} != {}",
                v1.len(),
                v2.len()
            ));
        }

        let dot: f64 = v1.iter().zip(v2.iter()).map(|(a, b)| a * b).sum();
        let mag1 = v1.iter().map(|x| x * x).sum::<f64>().sqrt();
        let mag2 = v2.iter().map(|x| x * x).sum::<f64>().sqrt();

        if mag1 == 0.0 || mag2 == 0.0 {
            return Err(anyhow!("Cannot compute angle with zero vector"));
        }

        let cos_angle = dot / (mag1 * mag2);
        // Clamp to [-1, 1] to handle floating point errors
        let cos_angle = cos_angle.max(-1.0).min(1.0);
        let angle = cos_angle.acos();

        Ok(DataValue::Float(angle))
    }
}

/// LINE_INTERSECT(p1, p2, p3, p4) - Find exact intersection point of two 2D lines
pub struct LineIntersectFunction;

impl SqlFunction for LineIntersectFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "LINE_INTERSECT",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(4),
            description: "Find intersection point of two 2D lines (returns NULL if parallel)",
            returns: "Vector or NULL",
            examples: vec![
                "SELECT LINE_INTERSECT(VEC(0,0), VEC(4,4), VEC(0,4), VEC(4,0))",
                "SELECT LINE_INTERSECT(line1_p1, line1_p2, line2_p1, line2_p2)",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        // Get four 2D points defining two lines
        let p1 = get_vector(&args[0])?;
        let p2 = get_vector(&args[1])?;
        let p3 = get_vector(&args[2])?;
        let p4 = get_vector(&args[3])?;

        if p1.len() != 2 || p2.len() != 2 || p3.len() != 2 || p4.len() != 2 {
            return Err(anyhow!("LINE_INTERSECT requires 2D points"));
        }

        // Line 1: p1 + t * (p2 - p1)
        // Line 2: p3 + s * (p4 - p3)
        //
        // Solving: p1 + t*(p2-p1) = p3 + s*(p4-p3)
        // This gives us two equations (for x and y)
        //
        // Using determinant method:
        let x1 = p1[0];
        let y1 = p1[1];
        let x2 = p2[0];
        let y2 = p2[1];
        let x3 = p3[0];
        let y3 = p3[1];
        let x4 = p4[0];
        let y4 = p4[1];

        let denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);

        // If denominator is 0, lines are parallel
        if denom.abs() < 1e-10 {
            return Ok(DataValue::Null);
        }

        // Calculate intersection point
        let t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / denom;

        let intersect_x = x1 + t * (x2 - x1);
        let intersect_y = y1 + t * (y2 - y1);

        Ok(DataValue::Vector(vec![intersect_x, intersect_y]))
    }
}

/// SEGMENT_INTERSECT(p1, p2, p3, p4) - Check if two line segments intersect
pub struct SegmentIntersectFunction;

impl SqlFunction for SegmentIntersectFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "SEGMENT_INTERSECT",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(4),
            description:
                "Check if two 2D line segments intersect (returns intersection point or NULL)",
            returns: "Vector or NULL",
            examples: vec![
                "SELECT SEGMENT_INTERSECT(VEC(0,0), VEC(2,2), VEC(0,2), VEC(2,0))",
                "SELECT SEGMENT_INTERSECT(seg1_p1, seg1_p2, seg2_p1, seg2_p2)",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        // Get four 2D points defining two segments
        let p1 = get_vector(&args[0])?;
        let p2 = get_vector(&args[1])?;
        let p3 = get_vector(&args[2])?;
        let p4 = get_vector(&args[3])?;

        if p1.len() != 2 || p2.len() != 2 || p3.len() != 2 || p4.len() != 2 {
            return Err(anyhow!("SEGMENT_INTERSECT requires 2D points"));
        }

        let x1 = p1[0];
        let y1 = p1[1];
        let x2 = p2[0];
        let y2 = p2[1];
        let x3 = p3[0];
        let y3 = p3[1];
        let x4 = p4[0];
        let y4 = p4[1];

        let denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);

        // If denominator is 0, segments are parallel
        if denom.abs() < 1e-10 {
            return Ok(DataValue::Null);
        }

        // Calculate parameters t and s
        let t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / denom;
        let s = ((x1 - x3) * (y1 - y2) - (y1 - y3) * (x1 - x2)) / denom;

        // Check if intersection is within both segments (t and s in [0, 1])
        if t >= 0.0 && t <= 1.0 && s >= 0.0 && s <= 1.0 {
            let intersect_x = x1 + t * (x2 - x1);
            let intersect_y = y1 + t * (y2 - y1);
            Ok(DataValue::Vector(vec![intersect_x, intersect_y]))
        } else {
            // Segments don't intersect (they would if extended to lines)
            Ok(DataValue::Null)
        }
    }
}

/// CLOSEST_POINT_ON_LINE(point, line_point, line_dir) - Find closest point on line to given point
pub struct ClosestPointOnLineFunction;

impl SqlFunction for ClosestPointOnLineFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "CLOSEST_POINT_ON_LINE",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(3),
            description: "Find closest point on a line to a given point (projection)",
            returns: "Vector",
            examples: vec![
                "SELECT CLOSEST_POINT_ON_LINE(VEC(2,2), VEC(0,0), VEC(1,0))",
                "SELECT CLOSEST_POINT_ON_LINE(point, line_start, line_direction)",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let point = get_vector(&args[0])?;
        let line_point = get_vector(&args[1])?;
        let line_dir = get_vector(&args[2])?;

        if point.len() != line_point.len() || point.len() != line_dir.len() {
            return Err(anyhow!(
                "All vectors must have same dimension, got {}, {}, {}",
                point.len(),
                line_point.len(),
                line_dir.len()
            ));
        }

        // Vector from line point to target point
        let to_point: Vec<f64> = point
            .iter()
            .zip(line_point.iter())
            .map(|(p, lp)| p - lp)
            .collect();

        // Project onto line direction: t = (to_point · line_dir) / (line_dir · line_dir)
        let dot_product: f64 = to_point
            .iter()
            .zip(line_dir.iter())
            .map(|(a, b)| a * b)
            .sum();
        let dir_mag_sq: f64 = line_dir.iter().map(|x| x * x).sum();

        if dir_mag_sq < 1e-10 {
            return Err(anyhow!("Line direction vector cannot be zero"));
        }

        let t = dot_product / dir_mag_sq;

        // Closest point = line_point + t * line_dir
        let closest: Vec<f64> = line_point
            .iter()
            .zip(line_dir.iter())
            .map(|(lp, ld)| lp + t * ld)
            .collect();

        Ok(DataValue::Vector(closest))
    }
}

/// POINT_LINE_DISTANCE(point, line_point, line_dir) - Distance from point to line
pub struct PointLineDistanceFunction;

impl SqlFunction for PointLineDistanceFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "POINT_LINE_DISTANCE",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(3),
            description: "Compute perpendicular distance from point to line",
            returns: "Float",
            examples: vec![
                "SELECT POINT_LINE_DISTANCE(VEC(2,2), VEC(0,0), VEC(1,0))",
                "SELECT POINT_LINE_DISTANCE(point, line_start, line_direction)",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let point = get_vector(&args[0])?;
        let line_point = get_vector(&args[1])?;
        let line_dir = get_vector(&args[2])?;

        // For 2D and 3D, we can use the cross product method
        if point.len() == 2 {
            // Extend to 3D for cross product
            let point_3d = vec![point[0], point[1], 0.0];
            let line_point_3d = vec![line_point[0], line_point[1], 0.0];
            let line_dir_3d = vec![line_dir[0], line_dir[1], 0.0];

            let to_point: Vec<f64> = point_3d
                .iter()
                .zip(line_point_3d.iter())
                .map(|(p, lp)| p - lp)
                .collect();

            // Cross product
            let cross_x = to_point[1] * line_dir_3d[2] - to_point[2] * line_dir_3d[1];
            let cross_y = to_point[2] * line_dir_3d[0] - to_point[0] * line_dir_3d[2];
            let cross_z = to_point[0] * line_dir_3d[1] - to_point[1] * line_dir_3d[0];

            let cross_mag = (cross_x * cross_x + cross_y * cross_y + cross_z * cross_z).sqrt();
            let dir_mag = (line_dir[0] * line_dir[0] + line_dir[1] * line_dir[1]).sqrt();

            if dir_mag < 1e-10 {
                return Err(anyhow!("Line direction cannot be zero"));
            }

            Ok(DataValue::Float(cross_mag / dir_mag))
        } else if point.len() == 3 {
            let to_point: Vec<f64> = point
                .iter()
                .zip(line_point.iter())
                .map(|(p, lp)| p - lp)
                .collect();

            // 3D cross product
            let cross_x = to_point[1] * line_dir[2] - to_point[2] * line_dir[1];
            let cross_y = to_point[2] * line_dir[0] - to_point[0] * line_dir[2];
            let cross_z = to_point[0] * line_dir[1] - to_point[1] * line_dir[0];

            let cross_mag = (cross_x * cross_x + cross_y * cross_y + cross_z * cross_z).sqrt();
            let dir_mag = line_dir.iter().map(|x| x * x).sum::<f64>().sqrt();

            if dir_mag < 1e-10 {
                return Err(anyhow!("Line direction cannot be zero"));
            }

            Ok(DataValue::Float(cross_mag / dir_mag))
        } else {
            Err(anyhow!(
                "POINT_LINE_DISTANCE only supports 2D and 3D, got {}D",
                point.len()
            ))
        }
    }
}

/// LINE_REFLECT_POINT(point, line_point, line_dir) - Reflect point across line
pub struct LineReflectPointFunction;

impl SqlFunction for LineReflectPointFunction {
    fn signature(&self) -> FunctionSignature {
        FunctionSignature {
            name: "LINE_REFLECT_POINT",
            category: FunctionCategory::Mathematical,
            arg_count: ArgCount::Fixed(3),
            description: "Reflect a point across a line",
            returns: "Vector",
            examples: vec![
                "SELECT LINE_REFLECT_POINT(VEC(2,2), VEC(0,0), VEC(1,0))",
                "SELECT LINE_REFLECT_POINT(point, line_start, line_direction)",
            ],
        }
    }

    fn evaluate(&self, args: &[DataValue]) -> Result<DataValue> {
        self.validate_args(args)?;

        let point = get_vector(&args[0])?;
        let line_point = get_vector(&args[1])?;
        let line_dir = get_vector(&args[2])?;

        if point.len() != line_point.len() || point.len() != line_dir.len() {
            return Err(anyhow!("All vectors must have same dimension"));
        }

        // Find closest point on line (projection)
        let to_point: Vec<f64> = point
            .iter()
            .zip(line_point.iter())
            .map(|(p, lp)| p - lp)
            .collect();

        let dot_product: f64 = to_point
            .iter()
            .zip(line_dir.iter())
            .map(|(a, b)| a * b)
            .sum();
        let dir_mag_sq: f64 = line_dir.iter().map(|x| x * x).sum();

        if dir_mag_sq < 1e-10 {
            return Err(anyhow!("Line direction vector cannot be zero"));
        }

        let t = dot_product / dir_mag_sq;

        let closest: Vec<f64> = line_point
            .iter()
            .zip(line_dir.iter())
            .map(|(lp, ld)| lp + t * ld)
            .collect();

        // Reflection = point + 2 * (closest - point) = 2 * closest - point
        let reflected: Vec<f64> = closest
            .iter()
            .zip(point.iter())
            .map(|(c, p)| 2.0 * c - p)
            .collect();

        Ok(DataValue::Vector(reflected))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_vector_string() {
        assert_eq!(parse_vector_string("[1,2,3]").unwrap(), vec![1.0, 2.0, 3.0]);
        assert_eq!(parse_vector_string("1 2 3").unwrap(), vec![1.0, 2.0, 3.0]);
        assert_eq!(
            parse_vector_string("1.5, 2.5, 3.5").unwrap(),
            vec![1.5, 2.5, 3.5]
        );
    }

    #[test]
    fn test_vec_function() {
        let func = VecFunction;
        let args = vec![
            DataValue::Integer(1),
            DataValue::Integer(2),
            DataValue::Integer(3),
        ];
        let result = func.evaluate(&args).unwrap();
        assert_eq!(result, DataValue::Vector(vec![1.0, 2.0, 3.0]));
    }

    #[test]
    fn test_vec_add() {
        let func = VecAddFunction;
        let args = vec![
            DataValue::Vector(vec![1.0, 2.0, 3.0]),
            DataValue::Vector(vec![4.0, 5.0, 6.0]),
        ];
        let result = func.evaluate(&args).unwrap();
        assert_eq!(result, DataValue::Vector(vec![5.0, 7.0, 9.0]));
    }

    #[test]
    fn test_vec_mag() {
        let func = VecMagFunction;
        let args = vec![DataValue::Vector(vec![3.0, 4.0])];
        let result = func.evaluate(&args).unwrap();
        assert_eq!(result, DataValue::Float(5.0));
    }

    #[test]
    fn test_vec_dot() {
        let func = VecDotFunction;
        let args = vec![
            DataValue::Vector(vec![1.0, 2.0, 3.0]),
            DataValue::Vector(vec![4.0, 5.0, 6.0]),
        ];
        let result = func.evaluate(&args).unwrap();
        assert_eq!(result, DataValue::Float(32.0)); // 1*4 + 2*5 + 3*6 = 32
    }

    #[test]
    fn test_vec_cross() {
        let func = VecCrossFunction;
        let args = vec![
            DataValue::Vector(vec![1.0, 0.0, 0.0]),
            DataValue::Vector(vec![0.0, 1.0, 0.0]),
        ];
        let result = func.evaluate(&args).unwrap();
        assert_eq!(result, DataValue::Vector(vec![0.0, 0.0, 1.0]));
    }
}