heliosdb-nano 3.23.2

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
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
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
//! Type inference for SQL expressions
//!
//! This module provides type inference capabilities for logical expressions,
//! enabling the query executor to determine correct column types at compile time
//! rather than defaulting everything to Text.

use crate::{DataType, Schema, Column, Error, Result};
use super::{LogicalExpr, BinaryOperator, UnaryOperator, AggregateFunction};

/// Type inference trait for logical expressions
pub trait TypeInference {
    /// Infer the data type of this expression given a schema context
    ///
    /// # Arguments
    ///
    /// * `schema` - The input schema providing column type information
    ///
    /// # Returns
    ///
    /// The inferred data type or an error if type cannot be determined
    fn infer_type(&self, schema: &Schema) -> Result<DataType>;

    /// Infer nullability of this expression
    ///
    /// # Arguments
    ///
    /// * `schema` - The input schema providing column nullability information
    ///
    /// # Returns
    ///
    /// True if the expression can produce NULL values, false otherwise
    fn infer_nullable(&self, schema: &Schema) -> bool;

    /// Create a Column definition from this expression
    ///
    /// Infers both type and nullability to create a complete column definition.
    ///
    /// # Arguments
    ///
    /// * `name` - The column name (or alias)
    /// * `schema` - The input schema providing type information
    ///
    /// # Returns
    ///
    /// A Column definition with inferred type and nullability
    fn to_column(&self, name: String, schema: &Schema) -> Column;
}

impl TypeInference for LogicalExpr {
    // SAFETY: All args[0] accesses are guarded by args.is_empty() checks.
    #[allow(clippy::indexing_slicing)]
    fn infer_type(&self, schema: &Schema) -> Result<DataType> {
        match self {
            // Column reference: look up in schema
            LogicalExpr::Column { name, .. } => {
                schema.get_column(name)
                    .map(|col| col.data_type.clone())
                    .ok_or_else(|| Error::type_conversion(format!("Column '{}' not found in schema", name)))
            }

            // Literal: get type from value
            LogicalExpr::Literal(value) => {
                Ok(value.data_type())
            }

            // Binary expression: apply type coercion rules
            LogicalExpr::BinaryExpr { left, op, right } => {
                let left_type = left.infer_type(schema)?;
                let right_type = right.infer_type(schema)?;
                coerce_binary_types(left_type, right_type, op)
            }

            // Unary expression: preserve operand type (with exceptions)
            LogicalExpr::UnaryExpr { op, expr } => {
                let expr_type = expr.infer_type(schema)?;
                match op {
                    UnaryOperator::Not => Ok(DataType::Boolean),
                    UnaryOperator::Minus | UnaryOperator::Plus => Ok(expr_type),
                }
            }

            // Aggregate function: determine result type based on function
            LogicalExpr::AggregateFunction { fun, args, .. } => {
                match fun {
                    AggregateFunction::Count => Ok(DataType::Int8),
                    AggregateFunction::Sum => {
                        if args.is_empty() {
                            return Err(Error::type_conversion("SUM requires at least one argument"));
                        }
                        // Preserve input type for SUM
                        args[0].infer_type(schema)
                    }
                    AggregateFunction::Avg => {
                        // AVG always returns Float8
                        Ok(DataType::Float8)
                    }
                    AggregateFunction::Min | AggregateFunction::Max => {
                        if args.is_empty() {
                            return Err(Error::type_conversion(format!("{:?} requires at least one argument", fun)));
                        }
                        // Preserve input type for MIN/MAX
                        args[0].infer_type(schema)
                    }
                    AggregateFunction::JsonAgg => {
                        // JSON_AGG always returns Jsonb
                        Ok(DataType::Jsonb)
                    }
                    AggregateFunction::ArrayAgg => {
                        // ARRAY_AGG returns an array of the input type
                        if args.is_empty() {
                            return Ok(DataType::Array(Box::new(DataType::Text)));
                        }
                        let elem_type = args[0].infer_type(schema)?;
                        Ok(DataType::Array(Box::new(elem_type)))
                    }
                    AggregateFunction::StringAgg { .. } => {
                        // STRING_AGG always returns Text
                        Ok(DataType::Text)
                    }
                }
            }

            // Scalar function: need function registry for proper typing
            // For now, use conservative Text default
            LogicalExpr::ScalarFunction { fun, args } => {
                match fun.to_lowercase().as_str() {
                    "length" | "char_length" | "character_length" => Ok(DataType::Int8),
                    "upper" | "lower" | "trim" | "ltrim" | "rtrim" => Ok(DataType::Text),
                    "substring" | "substr" => Ok(DataType::Text),
                    "concat" => Ok(DataType::Text),
                    "abs" => {
                        if args.is_empty() {
                            Ok(DataType::Float8)
                        } else {
                            args[0].infer_type(schema)
                        }
                    }
                    "round" | "floor" | "ceil" | "ceiling" => Ok(DataType::Float8),
                    "now" | "current_timestamp" => Ok(DataType::Timestamp),
                    "current_date" => Ok(DataType::Date),
                    "current_time" => Ok(DataType::Time),

                    // Phase 1: New JSON/JSONB functions
                    "jsonb_build_object" | "json_build_object" => Ok(DataType::Jsonb),
                    "jsonb_build_array" | "json_build_array" => Ok(DataType::Jsonb),
                    "jsonb_set" | "json_set" => Ok(DataType::Jsonb),
                    "jsonb_concat" => Ok(DataType::Jsonb),
                    "jsonb_delete" => Ok(DataType::Jsonb),
                    "jsonb_extract_path" | "json_extract_path" => Ok(DataType::Jsonb),
                    "jsonb_extract_path_text" | "json_extract_path_text" => Ok(DataType::Text),
                    "jsonb_array_elements" => Ok(DataType::Jsonb),
                    "jsonb_array_elements_text" => Ok(DataType::Array(Box::new(DataType::Text))),
                    "jsonb_each" => Ok(DataType::Array(Box::new(DataType::Text))),
                    "jsonb_each_text" => Ok(DataType::Array(Box::new(DataType::Text))),
                    "jsonb_object_keys" => Ok(DataType::Array(Box::new(DataType::Text))),
                    "jsonb_array_length" => Ok(DataType::Int4),
                    "jsonb_typeof" => Ok(DataType::Text),
                    "jsonb_path_query" => Ok(DataType::Jsonb),

                    _ => Ok(DataType::Text), // Conservative fallback
                }
            }

            // CASE expression: infer from THEN/ELSE branches
            LogicalExpr::Case { when_then, else_result, .. } => {
                // Try to infer from first THEN branch
                if let Some((_, then_expr)) = when_then.first() {
                    return then_expr.infer_type(schema);
                }
                // Otherwise try ELSE branch
                if let Some(else_expr) = else_result {
                    return else_expr.infer_type(schema);
                }
                // Conservative fallback
                Ok(DataType::Text)
            }

            // CAST expression: use target type
            LogicalExpr::Cast { data_type, .. } => {
                Ok(data_type.clone())
            }

            // IS NULL / IS NOT NULL: always boolean
            LogicalExpr::IsNull { .. } => {
                Ok(DataType::Boolean)
            }

            // BETWEEN: always boolean
            LogicalExpr::Between { .. } => {
                Ok(DataType::Boolean)
            }

            // IN list: always boolean
            LogicalExpr::InList { .. } => {
                Ok(DataType::Boolean)
            }

            // IN set (HashSet-based): always boolean
            LogicalExpr::InSet { .. } => {
                Ok(DataType::Boolean)
            }

            // IN subquery: always returns boolean
            LogicalExpr::InSubquery { .. } => {
                Ok(DataType::Boolean)
            }

            // Scalar subquery: type is the first column of the subplan
            LogicalExpr::ScalarSubquery { subquery } => {
                subquery.schema().columns.first()
                    .map(|c| c.data_type.clone())
                    .ok_or_else(|| Error::type_conversion(
                        "Scalar subquery returned no columns".to_string()
                    ))
            }

            // DEFAULT marker — the real type is the target column's,
            // resolved by the INSERT executor at execute time.
            LogicalExpr::DefaultValue => Ok(DataType::Text),

            // EXISTS subquery: always returns boolean
            LogicalExpr::Exists { .. } => {
                Ok(DataType::Boolean)
            }

            // Wildcard: cannot infer type
            LogicalExpr::Wildcard => {
                Err(Error::type_conversion("Cannot infer type for wildcard expression"))
            }

            // Parameter: cannot infer without parameter values
            LogicalExpr::Parameter { index } => {
                Err(Error::type_conversion(format!("Cannot infer type for parameter ${}", index)))
            }

            // NEW row: look up in schema (same as column reference)
            LogicalExpr::NewRow { column } => {
                schema.get_column(column)
                    .map(|col| col.data_type.clone())
                    .ok_or_else(|| Error::type_conversion(format!("Column '{}' not found in NEW row", column)))
            }

            // OLD row: look up in schema (same as column reference)
            LogicalExpr::OldRow { column } => {
                schema.get_column(column)
                    .map(|col| col.data_type.clone())
                    .ok_or_else(|| Error::type_conversion(format!("Column '{}' not found in OLD row", column)))
            }

            // Array subscript: arr[n] returns element type
            LogicalExpr::ArraySubscript { array, .. } => {
                let array_type = array.infer_type(schema)?;
                match array_type {
                    DataType::Array(elem_type) => Ok(*elem_type),
                    _ => Err(Error::type_conversion(
                        "Array subscript requires an array type".to_string()
                    )),
                }
            }

            // Row constructor: only appears inside comparisons, where the
            // overall expression type is Boolean. If someone asks for the
            // type of a bare tuple, fall back to Boolean (caller will error
            // at evaluation time anyway).
            LogicalExpr::Tuple { .. } => Ok(DataType::Boolean),

            // Window function: infer based on function type
            LogicalExpr::WindowFunction { fun, args, .. } => {
                use super::logical_plan::WindowFunctionType;
                match fun {
                    WindowFunctionType::RowNumber |
                    WindowFunctionType::Rank |
                    WindowFunctionType::DenseRank |
                    WindowFunctionType::Ntile => Ok(DataType::Int8),
                    WindowFunctionType::PercentRank |
                    WindowFunctionType::CumeDist => Ok(DataType::Float8),
                    WindowFunctionType::Lag |
                    WindowFunctionType::Lead |
                    WindowFunctionType::FirstValue |
                    WindowFunctionType::LastValue |
                    WindowFunctionType::NthValue => {
                        // Return type matches the argument type
                        if args.is_empty() {
                            Ok(DataType::Text) // Fallback
                        } else {
                            args[0].infer_type(schema)
                        }
                    }
                    WindowFunctionType::Aggregate(aggr) => {
                        // Delegate to aggregate type inference
                        match aggr {
                            crate::sql::AggregateFunction::Count => Ok(DataType::Int8),
                            crate::sql::AggregateFunction::Sum => {
                                if args.is_empty() {
                                    Ok(DataType::Float8)
                                } else {
                                    args[0].infer_type(schema)
                                }
                            }
                            crate::sql::AggregateFunction::Avg => Ok(DataType::Float8),
                            crate::sql::AggregateFunction::Min |
                            crate::sql::AggregateFunction::Max => {
                                if args.is_empty() {
                                    Ok(DataType::Float8)
                                } else {
                                    args[0].infer_type(schema)
                                }
                            }
                            crate::sql::AggregateFunction::JsonAgg => Ok(DataType::Jsonb),
                            crate::sql::AggregateFunction::ArrayAgg => {
                                if args.is_empty() {
                                    Ok(DataType::Array(Box::new(DataType::Text)))
                                } else {
                                    let elem_type = args[0].infer_type(schema)?;
                                    Ok(DataType::Array(Box::new(elem_type)))
                                }
                            }
                            crate::sql::AggregateFunction::StringAgg { .. } => Ok(DataType::Text),
                        }
                    }
                }
            }
        }
    }

    fn infer_nullable(&self, schema: &Schema) -> bool {
        match self {
            // Column: check schema for nullability
            LogicalExpr::Column { name, .. } => {
                schema.get_column(name)
                    .map(|col| col.nullable)
                    .unwrap_or(true) // Default to nullable if not found
            }

            // Literal: only NULL is nullable
            LogicalExpr::Literal(value) => {
                matches!(value, crate::Value::Null)
            }

            // Binary expressions: nullable if either operand is nullable
            LogicalExpr::BinaryExpr { left, right, .. } => {
                left.infer_nullable(schema) || right.infer_nullable(schema)
            }

            // Unary expressions: preserve operand nullability
            LogicalExpr::UnaryExpr { expr, .. } => {
                expr.infer_nullable(schema)
            }

            // Aggregate functions: generally nullable except COUNT(*)
            LogicalExpr::AggregateFunction { fun, args, .. } => {
                match fun {
                    AggregateFunction::Count => {
                        // COUNT(*) is never null, COUNT(col) can be
                        if args.is_empty() {
                            false // COUNT(*)
                        } else {
                            true // COUNT(col) - returns NULL if no rows
                        }
                    }
                    // Other aggregates can return NULL if no rows match
                    AggregateFunction::Sum | AggregateFunction::Avg |
                    AggregateFunction::Min | AggregateFunction::Max |
                    AggregateFunction::JsonAgg |
                    AggregateFunction::ArrayAgg |
                    AggregateFunction::StringAgg { .. } => true,
                }
            }

            // Scalar functions: generally nullable if any arg is nullable
            LogicalExpr::ScalarFunction { args, .. } => {
                args.iter().any(|arg| arg.infer_nullable(schema))
            }

            // CASE: nullable if any branch is nullable
            LogicalExpr::Case { when_then, else_result, .. } => {
                // Check all THEN branches
                let any_then_nullable = when_then.iter()
                    .any(|(_, then_expr)| then_expr.infer_nullable(schema));

                // Check ELSE branch
                let else_nullable = else_result.as_ref()
                    .map(|expr| expr.infer_nullable(schema))
                    .unwrap_or(true); // No ELSE means implicit NULL

                any_then_nullable || else_nullable
            }

            // CAST: preserve source nullability
            LogicalExpr::Cast { expr, .. } => {
                expr.infer_nullable(schema)
            }

            // IS NULL/IS NOT NULL: never nullable (always returns boolean)
            LogicalExpr::IsNull { .. } => false,

            // BETWEEN: never nullable (always returns boolean)
            LogicalExpr::Between { .. } => false,

            // IN list: never nullable (always returns boolean)
            LogicalExpr::InList { .. } => false,

            // IN set (HashSet-based): never nullable (always returns boolean)
            LogicalExpr::InSet { .. } => false,

            // IN subquery: never nullable (always returns boolean)
            LogicalExpr::InSubquery { .. } => false,

            // Scalar subquery: may return zero rows → NULL.
            LogicalExpr::ScalarSubquery { .. } => true,

            // DEFAULT marker — nullable iff the column is.
            LogicalExpr::DefaultValue => true,

            // EXISTS subquery: never nullable (always returns boolean)
            LogicalExpr::Exists { .. } => false,

            // Wildcard: treated as nullable
            LogicalExpr::Wildcard => true,

            // Parameter: treated as nullable (unknown)
            LogicalExpr::Parameter { .. } => true,

            // NEW row: check schema for nullability (same as column)
            LogicalExpr::NewRow { column } => {
                schema.get_column(column)
                    .map(|col| col.nullable)
                    .unwrap_or(true) // Default to nullable if not found
            }

            // OLD row: check schema for nullability (same as column)
            LogicalExpr::OldRow { column } => {
                schema.get_column(column)
                    .map(|col| col.nullable)
                    .unwrap_or(true) // Default to nullable if not found
            }

            // Array subscript: arr[n] is nullable (could be out of bounds or null input)
            LogicalExpr::ArraySubscript { .. } => true,

            // Row constructor: boolean comparison, nullable only if any
            // inner element is nullable.
            LogicalExpr::Tuple { items } => items.iter().any(|e| e.infer_nullable(schema)),

            // Window function: most are nullable
            LogicalExpr::WindowFunction { fun, .. } => {
                use super::logical_plan::WindowFunctionType;
                match fun {
                    // Ranking functions are never null
                    WindowFunctionType::RowNumber |
                    WindowFunctionType::Rank |
                    WindowFunctionType::DenseRank |
                    WindowFunctionType::Ntile => false,
                    // Statistical functions are never null
                    WindowFunctionType::PercentRank |
                    WindowFunctionType::CumeDist => false,
                    // Offset and value functions can return null
                    _ => true,
                }
            }
        }
    }

    fn to_column(&self, name: String, schema: &Schema) -> Column {
        let data_type = self.infer_type(schema)
            .unwrap_or(DataType::Text); // Fallback to Text if inference fails
        let nullable = self.infer_nullable(schema);

        Column {
            name,
            data_type,
            nullable,
            primary_key: false,
            source_table: None,
            source_table_name: None,
            default_expr: None,
            unique: false,
            storage_mode: crate::ColumnStorageMode::Default,
        }
    }
}

/// Type coercion rules for binary operations
///
/// Implements SQL-standard type promotion and coercion rules.
///
/// # Arguments
///
/// * `left` - Left operand type
/// * `right` - Right operand type
/// * `op` - Binary operator
///
/// # Returns
///
/// The result type after type coercion or an error if types are incompatible
fn coerce_binary_types(left: DataType, right: DataType, op: &BinaryOperator) -> Result<DataType> {
    match op {
        // Comparison operators always return boolean
        BinaryOperator::Eq | BinaryOperator::NotEq |
        BinaryOperator::Lt | BinaryOperator::LtEq |
        BinaryOperator::Gt | BinaryOperator::GtEq => {
            Ok(DataType::Boolean)
        }

        // Logical operators require boolean operands and return boolean
        BinaryOperator::And | BinaryOperator::Or => {
            Ok(DataType::Boolean)
        }

        // String pattern matching operators
        BinaryOperator::Like | BinaryOperator::NotLike |
        BinaryOperator::ILike | BinaryOperator::NotILike |
        BinaryOperator::RegexMatch | BinaryOperator::RegexIMatch |
        BinaryOperator::NotRegexMatch | BinaryOperator::NotRegexIMatch |
        BinaryOperator::SimilarTo | BinaryOperator::NotSimilarTo |
        // FTS match
        BinaryOperator::TsMatch => {
            Ok(DataType::Boolean)
        }

        // Arithmetic operators: numeric type promotion
        BinaryOperator::Plus | BinaryOperator::Minus |
        BinaryOperator::Multiply | BinaryOperator::Divide |
        BinaryOperator::Modulo => {
            coerce_numeric_types(left, right)
        }

        // Vector similarity operators return float distance
        BinaryOperator::VectorL2Distance |
        BinaryOperator::VectorCosineDistance |
        BinaryOperator::VectorInnerProduct => {
            // Verify both operands are vectors
            match (&left, &right) {
                (DataType::Vector(_), DataType::Vector(_)) => Ok(DataType::Float8),
                _ => Err(Error::type_conversion(
                    format!("Vector operators require vector operands, got {:?} and {:?}", left, right)
                ))
            }
        }

        // JSONB operators
        BinaryOperator::JsonGet => {
            // -> returns JSON
            Ok(DataType::Jsonb)
        }
        BinaryOperator::JsonGetText => {
            // ->> returns text
            Ok(DataType::Text)
        }
        BinaryOperator::JsonContains |
        BinaryOperator::JsonContainedBy |
        BinaryOperator::JsonExists |
        BinaryOperator::JsonExistsAny |
        BinaryOperator::JsonExistsAll => {
            // All return boolean
            Ok(DataType::Boolean)
        }

        // String concatenation: || operator
        // If either operand is an array, treat as array concatenation
        // Otherwise returns text
        BinaryOperator::StringConcat => {
            match (&left, &right) {
                (DataType::Array(elem), DataType::Array(_)) => {
                    Ok(DataType::Array(elem.clone()))
                }
                (DataType::Array(elem), _) | (_, DataType::Array(elem)) => {
                    Ok(DataType::Array(elem.clone()))
                }
                _ => Ok(DataType::Text),
            }
        }

        // Array operators
        BinaryOperator::ArrayConcat => {
            // Both operands must be arrays of compatible types
            match (&left, &right) {
                (DataType::Array(left_elem), DataType::Array(right_elem)) => {
                    // Both are arrays - return array of left element type
                    // PostgreSQL behavior: array || array -> array
                    Ok(DataType::Array(left_elem.clone()))
                }
                (DataType::Array(elem), other) | (other, DataType::Array(elem)) => {
                    // One is array, other is scalar - wrap scalar in array
                    // PostgreSQL behavior: array || element -> array
                    Ok(DataType::Array(elem.clone()))
                }
                _ => Err(Error::type_conversion(
                    format!("Array concatenation requires at least one array operand, got {:?} and {:?}", left, right)
                ))
            }
        }
    }
}

/// Numeric type coercion following SQL standard rules
///
/// Type promotion hierarchy (lowest to highest):
/// Int2 -> Int4 -> Int8 -> Float4 -> Float8 -> Numeric
///
/// # Arguments
///
/// * `left` - Left operand type
/// * `right` - Right operand type
///
/// # Returns
///
/// The promoted numeric type or an error if not numeric
fn coerce_numeric_types(left: DataType, right: DataType) -> Result<DataType> {
    use DataType::*;

    match (left, right) {
        // If either is Numeric, result is Numeric
        (Numeric, _) | (_, Numeric) => Ok(Numeric),

        // If either is Float8, result is Float8
        (Float8, _) | (_, Float8) => Ok(Float8),

        // If either is Float4, result is Float4 (unless other is Int8)
        (Float4, Int8) | (Int8, Float4) => Ok(Float8),
        (Float4, _) | (_, Float4) => Ok(Float4),

        // Integer promotions
        (Int8, Int8) => Ok(Int8),
        (Int8, Int4) | (Int4, Int8) => Ok(Int8),
        (Int8, Int2) | (Int2, Int8) => Ok(Int8),
        (Int4, Int4) => Ok(Int4),
        (Int4, Int2) | (Int2, Int4) => Ok(Int4),
        (Int2, Int2) => Ok(Int2),

        // Text can be coerced for arithmetic (PostgreSQL behavior)
        (Text, n) | (n, Text) if is_numeric(&n) => Ok(n),

        // Non-numeric types in arithmetic
        (l, r) => Err(Error::type_conversion(
            format!("Cannot perform arithmetic on types {:?} and {:?}", l, r)
        ))
    }
}

/// Check if a type is numeric
fn is_numeric(data_type: &DataType) -> bool {
    matches!(data_type,
        DataType::Int2 | DataType::Int4 | DataType::Int8 |
        DataType::Float4 | DataType::Float8 | DataType::Numeric
    )
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::{Column, Value};

    fn test_schema() -> Schema {
        Schema::new(vec![
            Column::new("id", DataType::Int4),
            Column::new("name", DataType::Text),
            Column::new("price", DataType::Float8),
            Column::new("quantity", DataType::Int8),
            Column::new("active", DataType::Boolean),
            Column::new("embedding", DataType::Vector(128)),
        ])
    }

    #[test]
    fn test_column_type_inference() {
        let schema = test_schema();
        let expr = LogicalExpr::Column { table: None, name: "price".to_string()  };
        assert_eq!(expr.infer_type(&schema).unwrap(), DataType::Float8);
    }

    #[test]
    fn test_literal_type_inference() {
        let schema = test_schema();
        let expr = LogicalExpr::Literal(Value::Int4(42));
        assert_eq!(expr.infer_type(&schema).unwrap(), DataType::Int4);
    }

    #[test]
    fn test_aggregate_count() {
        let schema = test_schema();
        let expr = LogicalExpr::AggregateFunction {
            fun: AggregateFunction::Count,
            args: vec![LogicalExpr::Column { table: None, name: "id".to_string()  }],
            distinct: false,
        };
        assert_eq!(expr.infer_type(&schema).unwrap(), DataType::Int8);
    }

    #[test]
    fn test_aggregate_sum_preserves_type() {
        let schema = test_schema();
        let expr = LogicalExpr::AggregateFunction {
            fun: AggregateFunction::Sum,
            args: vec![LogicalExpr::Column { table: None, name: "price".to_string()  }],
            distinct: false,
        };
        assert_eq!(expr.infer_type(&schema).unwrap(), DataType::Float8);
    }

    #[test]
    fn test_aggregate_avg_returns_float() {
        let schema = test_schema();
        let expr = LogicalExpr::AggregateFunction {
            fun: AggregateFunction::Avg,
            args: vec![LogicalExpr::Column { table: None, name: "quantity".to_string()  }],
            distinct: false,
        };
        assert_eq!(expr.infer_type(&schema).unwrap(), DataType::Float8);
    }

    #[test]
    fn test_arithmetic_type_coercion() {
        let schema = test_schema();
        // Int4 + Float8 = Float8
        let expr = LogicalExpr::BinaryExpr {
            left: Box::new(LogicalExpr::Column { table: None, name: "id".to_string()  }),
            op: BinaryOperator::Plus,
            right: Box::new(LogicalExpr::Column { table: None, name: "price".to_string()  }),
        };
        assert_eq!(expr.infer_type(&schema).unwrap(), DataType::Float8);
    }

    #[test]
    fn test_comparison_returns_boolean() {
        let schema = test_schema();
        let expr = LogicalExpr::BinaryExpr {
            left: Box::new(LogicalExpr::Column { table: None, name: "price".to_string()  }),
            op: BinaryOperator::Gt,
            right: Box::new(LogicalExpr::Literal(Value::Float8(100.0))),
        };
        assert_eq!(expr.infer_type(&schema).unwrap(), DataType::Boolean);
    }

    #[test]
    fn test_cast_expression() {
        let schema = test_schema();
        let expr = LogicalExpr::Cast {
            expr: Box::new(LogicalExpr::Column { table: None, name: "id".to_string()  }),
            data_type: DataType::Text,
        };
        assert_eq!(expr.infer_type(&schema).unwrap(), DataType::Text);
    }

    #[test]
    fn test_is_null_returns_boolean() {
        let schema = test_schema();
        let expr = LogicalExpr::IsNull {
            expr: Box::new(LogicalExpr::Column { table: None, name: "name".to_string()  }),
            is_null: true,
        };
        assert_eq!(expr.infer_type(&schema).unwrap(), DataType::Boolean);
    }

    #[test]
    fn test_vector_distance_returns_float() {
        let schema = test_schema();
        let expr = LogicalExpr::BinaryExpr {
            left: Box::new(LogicalExpr::Column { table: None, name: "embedding".to_string()  }),
            op: BinaryOperator::VectorL2Distance,
            right: Box::new(LogicalExpr::Column { table: None, name: "embedding".to_string()  }),
        };
        assert_eq!(expr.infer_type(&schema).unwrap(), DataType::Float8);
    }

    #[test]
    fn test_unknown_column_error() {
        let schema = test_schema();
        let expr = LogicalExpr::Column { table: None, name: "nonexistent".to_string()  };
        assert!(expr.infer_type(&schema).is_err());
    }

    // ===== Nullability Inference Tests =====

    #[test]
    fn test_nullable_column() {
        let schema = test_schema();
        let expr = LogicalExpr::Column { table: None, name: "name".to_string()  };
        assert!(expr.infer_nullable(&schema)); // name is nullable
    }

    #[test]
    fn test_non_nullable_literal() {
        let schema = test_schema();
        let expr = LogicalExpr::Literal(Value::Int4(42));
        assert!(!expr.infer_nullable(&schema)); // Literal is not nullable
    }

    #[test]
    fn test_nullable_literal() {
        let schema = test_schema();
        let expr = LogicalExpr::Literal(Value::Null);
        assert!(expr.infer_nullable(&schema)); // NULL is nullable
    }

    #[test]
    fn test_binary_expr_nullability() {
        let schema = test_schema();
        // nullable + non-nullable = nullable
        let expr = LogicalExpr::BinaryExpr {
            left: Box::new(LogicalExpr::Column { table: None, name: "name".to_string()  }),
            op: BinaryOperator::Plus,
            right: Box::new(LogicalExpr::Literal(Value::Int4(1))),
        };
        assert!(expr.infer_nullable(&schema));
    }

    #[test]
    fn test_count_star_not_nullable() {
        let schema = test_schema();
        // COUNT(*) is never nullable
        let expr = LogicalExpr::AggregateFunction {
            fun: AggregateFunction::Count,
            args: vec![], // COUNT(*)
            distinct: false,
        };
        assert!(!expr.infer_nullable(&schema));
    }

    #[test]
    fn test_count_column_nullable() {
        let schema = test_schema();
        // COUNT(col) can return NULL if no rows
        let expr = LogicalExpr::AggregateFunction {
            fun: AggregateFunction::Count,
            args: vec![LogicalExpr::Column { table: None, name: "id".to_string()  }],
            distinct: false,
        };
        assert!(expr.infer_nullable(&schema));
    }

    #[test]
    fn test_sum_nullable() {
        let schema = test_schema();
        let expr = LogicalExpr::AggregateFunction {
            fun: AggregateFunction::Sum,
            args: vec![LogicalExpr::Column { table: None, name: "price".to_string()  }],
            distinct: false,
        };
        assert!(expr.infer_nullable(&schema)); // SUM returns NULL for empty set
    }

    #[test]
    fn test_is_null_not_nullable() {
        let schema = test_schema();
        let expr = LogicalExpr::IsNull {
            expr: Box::new(LogicalExpr::Column { table: None, name: "name".to_string()  }),
            is_null: true,
        };
        assert!(!expr.infer_nullable(&schema)); // IS NULL always returns boolean
    }

    #[test]
    fn test_case_without_else_nullable() {
        let schema = test_schema();
        let expr = LogicalExpr::Case {
            expr: None,
            when_then: vec![(
                LogicalExpr::Literal(Value::Boolean(true)),
                LogicalExpr::Literal(Value::Int4(1)),
            )],
            else_result: None, // No ELSE = implicit NULL
        };
        assert!(expr.infer_nullable(&schema));
    }

    #[test]
    fn test_case_with_nullable_branch() {
        let schema = test_schema();
        let expr = LogicalExpr::Case {
            expr: None,
            when_then: vec![(
                LogicalExpr::Literal(Value::Boolean(true)),
                LogicalExpr::Literal(Value::Null), // Nullable branch
            )],
            else_result: Some(Box::new(LogicalExpr::Literal(Value::Int4(0)))),
        };
        assert!(expr.infer_nullable(&schema));
    }

    #[test]
    fn test_cast_preserves_nullability() {
        let schema = test_schema();
        let expr = LogicalExpr::Cast {
            expr: Box::new(LogicalExpr::Column { table: None, name: "name".to_string()  }),
            data_type: DataType::Int4,
        };
        assert!(expr.infer_nullable(&schema)); // Cast preserves nullability
    }

    // ===== to_column() Tests =====

    #[test]
    fn test_to_column_from_literal() {
        let schema = test_schema();
        let expr = LogicalExpr::Literal(Value::Int4(42));
        let col = expr.to_column("test_col".to_string(), &schema);

        assert_eq!(col.name, "test_col");
        assert_eq!(col.data_type, DataType::Int4);
        assert!(!col.nullable);
        assert!(!col.primary_key);
    }

    #[test]
    fn test_to_column_from_nullable_column() {
        let schema = test_schema();
        let expr = LogicalExpr::Column { table: None, name: "name".to_string()  };
        let col = expr.to_column("result".to_string(), &schema);

        assert_eq!(col.name, "result");
        assert_eq!(col.data_type, DataType::Text);
        assert!(col.nullable);
    }

    #[test]
    fn test_to_column_from_aggregate() {
        let schema = test_schema();
        let expr = LogicalExpr::AggregateFunction {
            fun: AggregateFunction::Avg,
            args: vec![LogicalExpr::Column { table: None, name: "price".to_string()  }],
            distinct: false,
        };
        let col = expr.to_column("avg_price".to_string(), &schema);

        assert_eq!(col.name, "avg_price");
        assert_eq!(col.data_type, DataType::Float8);
        assert!(col.nullable);
    }

    #[test]
    fn test_to_column_from_arithmetic() {
        let schema = test_schema();
        // price * quantity (Float8 * Int8 = Float8)
        let expr = LogicalExpr::BinaryExpr {
            left: Box::new(LogicalExpr::Column { table: None, name: "price".to_string()  }),
            op: BinaryOperator::Multiply,
            right: Box::new(LogicalExpr::Column { table: None, name: "quantity".to_string()  }),
        };
        let col = expr.to_column("total".to_string(), &schema);

        assert_eq!(col.name, "total");
        assert_eq!(col.data_type, DataType::Float8);
        assert!(col.nullable); // Either operand could be nullable
    }

    // ===== Complex Expression Tests =====

    #[test]
    fn test_nested_arithmetic_coercion() {
        let schema = test_schema();
        // id + (price * 2.0) -> Int4 + Float8 = Float8
        let expr = LogicalExpr::BinaryExpr {
            left: Box::new(LogicalExpr::Column { table: None, name: "id".to_string()  }),
            op: BinaryOperator::Plus,
            right: Box::new(LogicalExpr::BinaryExpr {
                left: Box::new(LogicalExpr::Column { table: None, name: "price".to_string()  }),
                op: BinaryOperator::Multiply,
                right: Box::new(LogicalExpr::Literal(Value::Float8(2.0))),
            }),
        };
        assert_eq!(expr.infer_type(&schema).unwrap(), DataType::Float8);
    }

    #[test]
    fn test_scalar_function_string_length() {
        let schema = test_schema();
        let expr = LogicalExpr::ScalarFunction {
            fun: "length".to_string(),
            args: vec![LogicalExpr::Column { table: None, name: "name".to_string()  }],
        };
        assert_eq!(expr.infer_type(&schema).unwrap(), DataType::Int8);
        assert!(expr.infer_nullable(&schema)); // Function arg is nullable
    }

    #[test]
    fn test_scalar_function_concat() {
        let schema = test_schema();
        let expr = LogicalExpr::ScalarFunction {
            fun: "concat".to_string(),
            args: vec![
                LogicalExpr::Column { table: None, name: "name".to_string()  },
                LogicalExpr::Literal(Value::String(" suffix".to_string())),
            ],
        };
        assert_eq!(expr.infer_type(&schema).unwrap(), DataType::Text);
    }

    #[test]
    fn test_multiple_aggregates_distinct_types() {
        let schema = test_schema();

        // COUNT(*)
        let count_expr = LogicalExpr::AggregateFunction {
            fun: AggregateFunction::Count,
            args: vec![],
            distinct: false,
        };
        assert_eq!(count_expr.infer_type(&schema).unwrap(), DataType::Int8);
        assert!(!count_expr.infer_nullable(&schema));

        // SUM(quantity)
        let sum_expr = LogicalExpr::AggregateFunction {
            fun: AggregateFunction::Sum,
            args: vec![LogicalExpr::Column { table: None, name: "quantity".to_string()  }],
            distinct: false,
        };
        assert_eq!(sum_expr.infer_type(&schema).unwrap(), DataType::Int8);
        assert!(sum_expr.infer_nullable(&schema));

        // AVG(price)
        let avg_expr = LogicalExpr::AggregateFunction {
            fun: AggregateFunction::Avg,
            args: vec![LogicalExpr::Column { table: None, name: "price".to_string()  }],
            distinct: false,
        };
        assert_eq!(avg_expr.infer_type(&schema).unwrap(), DataType::Float8);
        assert!(avg_expr.infer_nullable(&schema));
    }

    #[test]
    fn test_json_operators() {
        let mut schema_cols = test_schema().columns;
        schema_cols.push(Column::new("metadata", DataType::Jsonb));
        let schema = Schema::new(schema_cols);

        // -> returns JSONB
        let json_get = LogicalExpr::BinaryExpr {
            left: Box::new(LogicalExpr::Column { table: None, name: "metadata".to_string()  }),
            op: BinaryOperator::JsonGet,
            right: Box::new(LogicalExpr::Literal(Value::String("key".to_string()))),
        };
        assert_eq!(json_get.infer_type(&schema).unwrap(), DataType::Jsonb);

        // ->> returns TEXT
        let json_get_text = LogicalExpr::BinaryExpr {
            left: Box::new(LogicalExpr::Column { table: None, name: "metadata".to_string()  }),
            op: BinaryOperator::JsonGetText,
            right: Box::new(LogicalExpr::Literal(Value::String("key".to_string()))),
        };
        assert_eq!(json_get_text.infer_type(&schema).unwrap(), DataType::Text);

        // @> returns BOOLEAN
        let json_contains = LogicalExpr::BinaryExpr {
            left: Box::new(LogicalExpr::Column { table: None, name: "metadata".to_string()  }),
            op: BinaryOperator::JsonContains,
            right: Box::new(LogicalExpr::Literal(Value::Json("{}".to_string()))),
        };
        assert_eq!(json_contains.infer_type(&schema).unwrap(), DataType::Boolean);
    }
}