xlog-cuda 0.5.0

CUDA kernel provider, buffers, and interop for XLOG
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
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
//! Arithmetic column operations: add, sub, mul, div, mod, abs, min, max, pow, cast, select, combine.

use crate::{DeviceRepr, DeviceSlice, LaunchAsync, LaunchConfig};
use xlog_core::{Result, ScalarType, Schema, XlogError};

use super::{arith_kernels, ARITH_MODULE};
use crate::memory::TrackedCudaSlice;
use crate::CudaBuffer;

impl super::CudaKernelProvider {
    /// Create a column filled with a constant value
    ///
    /// # Arguments
    /// * `value_bytes` - The raw bytes of the constant value (in little-endian format)
    /// * `col_type` - The ScalarType of the column
    /// * `num_rows` - Number of rows to create
    ///
    /// # Returns
    /// A new single-column CudaBuffer filled with the constant value
    pub fn create_constant_column(
        &self,
        value_bytes: &[u8],
        col_type: ScalarType,
        num_rows: u64,
    ) -> Result<CudaBuffer> {
        if num_rows == 0 {
            let schema = Schema::new(vec![("const".to_string(), col_type)]);
            return self.create_empty_buffer(schema);
        }

        let elem_size = col_type.size_bytes();
        if value_bytes.len() != elem_size {
            return Err(XlogError::Kernel(format!(
                "Value bytes length {} doesn't match type size {}",
                value_bytes.len(),
                elem_size
            )));
        }

        if num_rows > u32::MAX as u64 {
            return Err(XlogError::Kernel(format!(
                "Constant column supports at most {} rows, got {}",
                u32::MAX,
                num_rows
            )));
        }

        let total_bytes = (num_rows as usize)
            .checked_mul(elem_size)
            .ok_or_else(|| XlogError::Kernel("Constant column size overflow".to_string()))?;

        let mut dst_col = self.memory.alloc::<u8>(total_bytes)?;
        let n = num_rows as u32;

        macro_rules! launch_fill_const {
            ($kernel:expr, $value:expr) => {{
                let func = self
                    .device
                    .inner()
                    .get_func(ARITH_MODULE, $kernel)
                    .ok_or_else(|| XlogError::Kernel("arith fill kernel not found".to_string()))?;
                let config = LaunchConfig::for_num_elems(n);
                unsafe { func.clone().launch(config, ($value, n, &mut dst_col)) }
                    .map_err(|e| XlogError::Kernel(format!("fill const failed: {}", e)))?;
            }};
        }

        match col_type {
            ScalarType::U32 | ScalarType::Symbol => {
                let value = u32::from_le_bytes(value_bytes.try_into().unwrap());
                launch_fill_const!(arith_kernels::ARITH_FILL_CONST_U32, value);
            }
            ScalarType::U64 => {
                let value = u64::from_le_bytes(value_bytes.try_into().unwrap());
                launch_fill_const!(arith_kernels::ARITH_FILL_CONST_U64, value);
            }
            ScalarType::I64 => {
                let value = i64::from_le_bytes(value_bytes.try_into().unwrap());
                launch_fill_const!(arith_kernels::ARITH_FILL_CONST_I64, value);
            }
            ScalarType::I32 => {
                let value = i32::from_le_bytes(value_bytes.try_into().unwrap());
                launch_fill_const!(arith_kernels::ARITH_FILL_CONST_I32, value);
            }
            ScalarType::F64 => {
                let value = f64::from_le_bytes(value_bytes.try_into().unwrap());
                launch_fill_const!(arith_kernels::ARITH_FILL_CONST_F64, value);
            }
            ScalarType::F32 => {
                let value = f32::from_le_bytes(value_bytes.try_into().unwrap());
                launch_fill_const!(arith_kernels::ARITH_FILL_CONST_F32, value);
            }
            ScalarType::Bool => {
                let value = value_bytes[0];
                launch_fill_const!(arith_kernels::ARITH_FILL_CONST_U8, value);
            }
        }

        self.device.synchronize()?;

        let schema = Schema::new(vec![("const".to_string(), col_type)]);
        self.buffer_from_columns(vec![dst_col.into()], num_rows, schema)
    }

    /// Create a constant column sized to `row_cap` while preserving device row count from `d_num_rows_src`.
    pub fn create_constant_column_with_device_count(
        &self,
        value_bytes: &[u8],
        col_type: ScalarType,
        row_cap: u64,
        d_num_rows_src: &TrackedCudaSlice<u32>,
    ) -> Result<CudaBuffer> {
        if row_cap == 0 {
            let schema = Schema::new(vec![("const".to_string(), col_type)]);
            return self.create_empty_buffer(schema);
        }

        let elem_size = col_type.size_bytes();
        if value_bytes.len() != elem_size {
            return Err(XlogError::Kernel(format!(
                "Value bytes length {} doesn't match type size {}",
                value_bytes.len(),
                elem_size
            )));
        }

        if row_cap > u32::MAX as u64 {
            return Err(XlogError::Kernel(format!(
                "Constant column supports at most {} rows, got {}",
                u32::MAX,
                row_cap
            )));
        }

        let total_bytes = (row_cap as usize)
            .checked_mul(elem_size)
            .ok_or_else(|| XlogError::Kernel("Constant column size overflow".to_string()))?;

        let mut dst_col = self.memory.alloc::<u8>(total_bytes)?;
        let n = row_cap as u32;

        macro_rules! launch_fill_const {
            ($kernel:expr, $value:expr) => {{
                let func = self
                    .device
                    .inner()
                    .get_func(ARITH_MODULE, $kernel)
                    .ok_or_else(|| XlogError::Kernel("arith fill kernel not found".to_string()))?;
                let config = LaunchConfig::for_num_elems(n);
                unsafe { func.clone().launch(config, ($value, n, &mut dst_col)) }
                    .map_err(|e| XlogError::Kernel(format!("fill const failed: {}", e)))?;
            }};
        }

        match col_type {
            ScalarType::U32 | ScalarType::Symbol => {
                let value = u32::from_le_bytes(value_bytes.try_into().unwrap());
                launch_fill_const!(arith_kernels::ARITH_FILL_CONST_U32, value);
            }
            ScalarType::U64 => {
                let value = u64::from_le_bytes(value_bytes.try_into().unwrap());
                launch_fill_const!(arith_kernels::ARITH_FILL_CONST_U64, value);
            }
            ScalarType::I64 => {
                let value = i64::from_le_bytes(value_bytes.try_into().unwrap());
                launch_fill_const!(arith_kernels::ARITH_FILL_CONST_I64, value);
            }
            ScalarType::I32 => {
                let value = i32::from_le_bytes(value_bytes.try_into().unwrap());
                launch_fill_const!(arith_kernels::ARITH_FILL_CONST_I32, value);
            }
            ScalarType::F64 => {
                let value = f64::from_le_bytes(value_bytes.try_into().unwrap());
                launch_fill_const!(arith_kernels::ARITH_FILL_CONST_F64, value);
            }
            ScalarType::F32 => {
                let value = f32::from_le_bytes(value_bytes.try_into().unwrap());
                launch_fill_const!(arith_kernels::ARITH_FILL_CONST_F32, value);
            }
            ScalarType::Bool => {
                let value = value_bytes[0];
                launch_fill_const!(arith_kernels::ARITH_FILL_CONST_U8, value);
            }
        }

        self.device.synchronize()?;

        let schema = Schema::new(vec![("const".to_string(), col_type)]);
        let mut d_num_rows = self.memory.alloc::<u32>(1)?;
        self.device
            .inner()
            .dtod_copy(d_num_rows_src, &mut d_num_rows)
            .map_err(|e| XlogError::Kernel(format!("Failed to copy row count: {}", e)))?;

        Ok(CudaBuffer::from_columns(
            vec![dst_col.into()],
            row_cap,
            d_num_rows,
            schema,
        ))
    }

    /// Element-wise addition of two single-column buffers
    ///
    /// Performs element-wise addition using GPU kernels.
    /// Uses wrapping arithmetic for integer overflow.
    ///
    /// # Arguments
    /// * `a` - First operand buffer (single column)
    /// * `b` - Second operand buffer (single column)
    ///
    /// # Returns
    /// A new CudaBuffer containing the element-wise sum
    ///
    /// # Errors
    /// Returns `XlogError::Kernel` if:
    /// - Row counts don't match
    /// - Buffers are not single-column
    /// - Type is not supported for arithmetic
    pub fn add_columns(&self, a: &CudaBuffer, b: &CudaBuffer) -> Result<CudaBuffer> {
        match a.schema().column_type(0) {
            Some(ScalarType::I64) => {
                self.binary_arith_op_device::<i64>(a, b, 0, arith_kernels::ARITH_BINARY_I64)
            }
            Some(ScalarType::I32) => {
                self.binary_arith_op_device::<i32>(a, b, 0, arith_kernels::ARITH_BINARY_I32)
            }
            Some(ScalarType::U64) => {
                self.binary_arith_op_device::<u64>(a, b, 0, arith_kernels::ARITH_BINARY_U64)
            }
            Some(ScalarType::U32 | ScalarType::Symbol) => {
                self.binary_arith_op_device::<u32>(a, b, 0, arith_kernels::ARITH_BINARY_U32)
            }
            Some(ScalarType::F64) => {
                self.binary_arith_op_device::<f64>(a, b, 0, arith_kernels::ARITH_BINARY_F64)
            }
            Some(ScalarType::F32) => {
                self.binary_arith_op_device::<f32>(a, b, 0, arith_kernels::ARITH_BINARY_F32)
            }
            other => Err(XlogError::Kernel(format!(
                "Arithmetic not supported for {:?}",
                other
            ))),
        }
    }

    /// Element-wise subtraction of two single-column buffers
    ///
    /// Performs element-wise subtraction using GPU kernels.
    /// Uses wrapping arithmetic for integer overflow.
    ///
    /// # Arguments
    /// * `a` - First operand buffer (single column)
    /// * `b` - Second operand buffer (single column)
    ///
    /// # Returns
    /// A new CudaBuffer containing the element-wise difference
    ///
    /// # Errors
    /// Returns `XlogError::Kernel` if:
    /// - Row counts don't match
    /// - Buffers are not single-column
    /// - Type is not supported for arithmetic
    pub fn sub_columns(&self, a: &CudaBuffer, b: &CudaBuffer) -> Result<CudaBuffer> {
        match a.schema().column_type(0) {
            Some(ScalarType::I64) => {
                self.binary_arith_op_device::<i64>(a, b, 1, arith_kernels::ARITH_BINARY_I64)
            }
            Some(ScalarType::I32) => {
                self.binary_arith_op_device::<i32>(a, b, 1, arith_kernels::ARITH_BINARY_I32)
            }
            Some(ScalarType::U64) => {
                self.binary_arith_op_device::<u64>(a, b, 1, arith_kernels::ARITH_BINARY_U64)
            }
            Some(ScalarType::U32 | ScalarType::Symbol) => {
                self.binary_arith_op_device::<u32>(a, b, 1, arith_kernels::ARITH_BINARY_U32)
            }
            Some(ScalarType::F64) => {
                self.binary_arith_op_device::<f64>(a, b, 1, arith_kernels::ARITH_BINARY_F64)
            }
            Some(ScalarType::F32) => {
                self.binary_arith_op_device::<f32>(a, b, 1, arith_kernels::ARITH_BINARY_F32)
            }
            other => Err(XlogError::Kernel(format!(
                "Arithmetic not supported for {:?}",
                other
            ))),
        }
    }

    /// Element-wise multiplication of two single-column buffers
    ///
    /// Performs element-wise multiplication using GPU kernels.
    /// Uses wrapping arithmetic for integer overflow.
    ///
    /// # Arguments
    /// * `a` - First operand buffer (single column)
    /// * `b` - Second operand buffer (single column)
    ///
    /// # Returns
    /// A new CudaBuffer containing the element-wise product
    ///
    /// # Errors
    /// Returns `XlogError::Kernel` if:
    /// - Row counts don't match
    /// - Buffers are not single-column
    /// - Type is not supported for arithmetic
    pub fn mul_columns(&self, a: &CudaBuffer, b: &CudaBuffer) -> Result<CudaBuffer> {
        match a.schema().column_type(0) {
            Some(ScalarType::I64) => {
                self.binary_arith_op_device::<i64>(a, b, 2, arith_kernels::ARITH_BINARY_I64)
            }
            Some(ScalarType::I32) => {
                self.binary_arith_op_device::<i32>(a, b, 2, arith_kernels::ARITH_BINARY_I32)
            }
            Some(ScalarType::U64) => {
                self.binary_arith_op_device::<u64>(a, b, 2, arith_kernels::ARITH_BINARY_U64)
            }
            Some(ScalarType::U32 | ScalarType::Symbol) => {
                self.binary_arith_op_device::<u32>(a, b, 2, arith_kernels::ARITH_BINARY_U32)
            }
            Some(ScalarType::F64) => {
                self.binary_arith_op_device::<f64>(a, b, 2, arith_kernels::ARITH_BINARY_F64)
            }
            Some(ScalarType::F32) => {
                self.binary_arith_op_device::<f32>(a, b, 2, arith_kernels::ARITH_BINARY_F32)
            }
            other => Err(XlogError::Kernel(format!(
                "Arithmetic not supported for {:?}",
                other
            ))),
        }
    }

    /// Element-wise division of two single-column buffers
    ///
    /// Performs element-wise division using GPU kernels.
    /// For signed integers, division by zero returns i64::MAX/i32::MAX.
    /// For unsigned integers, division by zero returns u64::MAX/u32::MAX.
    /// For floats, division by zero produces Inf/NaN as per IEEE 754.
    ///
    /// # Arguments
    /// * `a` - Dividend buffer (single column)
    /// * `b` - Divisor buffer (single column)
    ///
    /// # Returns
    /// A new CudaBuffer containing the element-wise quotient
    ///
    /// # Errors
    /// Returns `XlogError::Kernel` if:
    /// - Row counts don't match
    /// - Buffers are not single-column
    /// - Type is not supported for arithmetic
    pub fn div_columns(&self, a: &CudaBuffer, b: &CudaBuffer) -> Result<CudaBuffer> {
        match a.schema().column_type(0) {
            Some(ScalarType::I64) => {
                self.binary_arith_op_device::<i64>(a, b, 3, arith_kernels::ARITH_BINARY_I64)
            }
            Some(ScalarType::I32) => {
                self.binary_arith_op_device::<i32>(a, b, 3, arith_kernels::ARITH_BINARY_I32)
            }
            Some(ScalarType::U64) => {
                self.binary_arith_op_device::<u64>(a, b, 3, arith_kernels::ARITH_BINARY_U64)
            }
            Some(ScalarType::U32 | ScalarType::Symbol) => {
                self.binary_arith_op_device::<u32>(a, b, 3, arith_kernels::ARITH_BINARY_U32)
            }
            Some(ScalarType::F64) => {
                self.binary_arith_op_device::<f64>(a, b, 3, arith_kernels::ARITH_BINARY_F64)
            }
            Some(ScalarType::F32) => {
                self.binary_arith_op_device::<f32>(a, b, 3, arith_kernels::ARITH_BINARY_F32)
            }
            other => Err(XlogError::Kernel(format!(
                "Arithmetic not supported for {:?}",
                other
            ))),
        }
    }

    /// Element-wise modulo of two single-column buffers
    ///
    /// Performs element-wise modulo using GPU kernels.
    /// For integers, modulo by zero returns 0.
    /// For floats, modulo by zero produces NaN as per IEEE 754.
    ///
    /// # Arguments
    /// * `a` - Dividend buffer (single column)
    /// * `b` - Divisor buffer (single column)
    ///
    /// # Returns
    /// A new CudaBuffer containing the element-wise remainder
    ///
    /// # Errors
    /// Returns `XlogError::Kernel` if:
    /// - Row counts don't match
    /// - Buffers are not single-column
    /// - Type is not supported for arithmetic
    pub fn mod_columns(&self, a: &CudaBuffer, b: &CudaBuffer) -> Result<CudaBuffer> {
        match a.schema().column_type(0) {
            Some(ScalarType::I64) => {
                self.binary_arith_op_device::<i64>(a, b, 4, arith_kernels::ARITH_BINARY_I64)
            }
            Some(ScalarType::I32) => {
                self.binary_arith_op_device::<i32>(a, b, 4, arith_kernels::ARITH_BINARY_I32)
            }
            Some(ScalarType::U64) => {
                self.binary_arith_op_device::<u64>(a, b, 4, arith_kernels::ARITH_BINARY_U64)
            }
            Some(ScalarType::U32 | ScalarType::Symbol) => {
                self.binary_arith_op_device::<u32>(a, b, 4, arith_kernels::ARITH_BINARY_U32)
            }
            Some(ScalarType::F64) => {
                self.binary_arith_op_device::<f64>(a, b, 4, arith_kernels::ARITH_BINARY_F64)
            }
            Some(ScalarType::F32) => {
                self.binary_arith_op_device::<f32>(a, b, 4, arith_kernels::ARITH_BINARY_F32)
            }
            other => Err(XlogError::Kernel(format!(
                "Arithmetic not supported for {:?}",
                other
            ))),
        }
    }

    /// Element-wise absolute value of a single-column buffer
    ///
    /// Performs element-wise absolute value using GPU kernels.
    ///
    /// # Arguments
    /// * `a` - Input buffer (single column)
    ///
    /// # Returns
    /// A new CudaBuffer containing the absolute values
    ///
    /// # Errors
    /// Returns `XlogError::Kernel` if:
    /// - Buffer is not single-column
    /// - Type is not supported for arithmetic
    pub fn abs_column(&self, a: &CudaBuffer) -> Result<CudaBuffer> {
        if a.arity() != 1 {
            return Err(XlogError::Kernel(
                "Arithmetic requires single-column buffers".into(),
            ));
        }

        if a.num_rows() == 0 {
            return self.create_empty_buffer(a.schema().clone());
        }

        let n: u32 = a.num_rows().try_into().map_err(|_| {
            XlogError::Kernel(format!(
                "abs_column: row count {} exceeds u32::MAX",
                a.num_rows()
            ))
        })?;
        let col = a
            .column(0)
            .ok_or_else(|| XlogError::Kernel("Missing column 0".into()))?;
        let config = LaunchConfig::for_num_elems(n);

        match a.schema().column_type(0) {
            Some(ScalarType::I64) => {
                let expected_bytes = (n as usize)
                    .checked_mul(std::mem::size_of::<i64>())
                    .ok_or_else(|| XlogError::Kernel("abs_column size overflow".into()))?;
                if col.num_bytes() != expected_bytes {
                    return Err(XlogError::Kernel(format!(
                        "Column 0 has {} bytes but expected {} for {} rows",
                        col.num_bytes(),
                        expected_bytes,
                        a.num_rows()
                    )));
                }
                let mut out = self.memory.alloc::<u8>(expected_bytes)?;
                let func = self
                    .device
                    .inner()
                    .get_func(ARITH_MODULE, arith_kernels::ARITH_ABS_I64)
                    .ok_or_else(|| XlogError::Kernel("arith_abs_i64 not found".into()))?;
                unsafe { func.clone().launch(config, (col, n, &mut out)) }
                    .map_err(|e| XlogError::Kernel(format!("abs_i64 failed: {}", e)))?;
                self.device.synchronize()?;
                self.buffer_from_columns_with_device_count(
                    vec![out.into()],
                    a.num_rows(),
                    a.schema.clone(),
                    a,
                )
            }
            Some(ScalarType::I32) => {
                let expected_bytes = (n as usize)
                    .checked_mul(std::mem::size_of::<i32>())
                    .ok_or_else(|| XlogError::Kernel("abs_column size overflow".into()))?;
                if col.num_bytes() != expected_bytes {
                    return Err(XlogError::Kernel(format!(
                        "Column 0 has {} bytes but expected {} for {} rows",
                        col.num_bytes(),
                        expected_bytes,
                        a.num_rows()
                    )));
                }
                let mut out = self.memory.alloc::<u8>(expected_bytes)?;
                let func = self
                    .device
                    .inner()
                    .get_func(ARITH_MODULE, arith_kernels::ARITH_ABS_I32)
                    .ok_or_else(|| XlogError::Kernel("arith_abs_i32 not found".into()))?;
                unsafe { func.clone().launch(config, (col, n, &mut out)) }
                    .map_err(|e| XlogError::Kernel(format!("abs_i32 failed: {}", e)))?;
                self.device.synchronize()?;
                self.buffer_from_columns_with_device_count(
                    vec![out.into()],
                    a.num_rows(),
                    a.schema.clone(),
                    a,
                )
            }
            Some(ScalarType::F64) => {
                let expected_bytes = (n as usize)
                    .checked_mul(std::mem::size_of::<f64>())
                    .ok_or_else(|| XlogError::Kernel("abs_column size overflow".into()))?;
                if col.num_bytes() != expected_bytes {
                    return Err(XlogError::Kernel(format!(
                        "Column 0 has {} bytes but expected {} for {} rows",
                        col.num_bytes(),
                        expected_bytes,
                        a.num_rows()
                    )));
                }
                let mut out = self.memory.alloc::<u8>(expected_bytes)?;
                let func = self
                    .device
                    .inner()
                    .get_func(ARITH_MODULE, arith_kernels::ARITH_ABS_F64)
                    .ok_or_else(|| XlogError::Kernel("arith_abs_f64 not found".into()))?;
                unsafe { func.clone().launch(config, (col, n, &mut out)) }
                    .map_err(|e| XlogError::Kernel(format!("abs_f64 failed: {}", e)))?;
                self.device.synchronize()?;
                self.buffer_from_columns_with_device_count(
                    vec![out.into()],
                    a.num_rows(),
                    a.schema.clone(),
                    a,
                )
            }
            Some(ScalarType::F32) => {
                let expected_bytes = (n as usize)
                    .checked_mul(std::mem::size_of::<f32>())
                    .ok_or_else(|| XlogError::Kernel("abs_column size overflow".into()))?;
                if col.num_bytes() != expected_bytes {
                    return Err(XlogError::Kernel(format!(
                        "Column 0 has {} bytes but expected {} for {} rows",
                        col.num_bytes(),
                        expected_bytes,
                        a.num_rows()
                    )));
                }
                let mut out = self.memory.alloc::<u8>(expected_bytes)?;
                let func = self
                    .device
                    .inner()
                    .get_func(ARITH_MODULE, arith_kernels::ARITH_ABS_F32)
                    .ok_or_else(|| XlogError::Kernel("arith_abs_f32 not found".into()))?;
                unsafe { func.clone().launch(config, (col, n, &mut out)) }
                    .map_err(|e| XlogError::Kernel(format!("abs_f32 failed: {}", e)))?;
                self.device.synchronize()?;
                self.buffer_from_columns_with_device_count(
                    vec![out.into()],
                    a.num_rows(),
                    a.schema.clone(),
                    a,
                )
            }
            Some(ScalarType::U32 | ScalarType::U64 | ScalarType::Bool | ScalarType::Symbol) => {
                self.clone_buffer(a)
            }
            other => Err(XlogError::Kernel(format!(
                "Abs not supported for {:?}",
                other
            ))),
        }
    }

    /// Element-wise minimum of two single-column buffers
    ///
    /// Performs element-wise minimum using GPU kernels.
    ///
    /// # Arguments
    /// * `a` - First operand buffer (single column)
    /// * `b` - Second operand buffer (single column)
    ///
    /// # Returns
    /// A new CudaBuffer containing the element-wise minimums
    ///
    /// # Errors
    /// Returns `XlogError::Kernel` if:
    /// - Row counts don't match
    /// - Buffers are not single-column
    /// - Type is not supported for arithmetic
    pub fn min_columns(&self, a: &CudaBuffer, b: &CudaBuffer) -> Result<CudaBuffer> {
        match a.schema().column_type(0) {
            Some(ScalarType::I64) => {
                self.binary_arith_op_device::<i64>(a, b, 5, arith_kernels::ARITH_BINARY_I64)
            }
            Some(ScalarType::I32) => {
                self.binary_arith_op_device::<i32>(a, b, 5, arith_kernels::ARITH_BINARY_I32)
            }
            Some(ScalarType::U64) => {
                self.binary_arith_op_device::<u64>(a, b, 5, arith_kernels::ARITH_BINARY_U64)
            }
            Some(ScalarType::U32 | ScalarType::Symbol) => {
                self.binary_arith_op_device::<u32>(a, b, 5, arith_kernels::ARITH_BINARY_U32)
            }
            Some(ScalarType::F64) => {
                self.binary_arith_op_device::<f64>(a, b, 5, arith_kernels::ARITH_BINARY_F64)
            }
            Some(ScalarType::F32) => {
                self.binary_arith_op_device::<f32>(a, b, 5, arith_kernels::ARITH_BINARY_F32)
            }
            other => Err(XlogError::Kernel(format!(
                "Arithmetic not supported for {:?}",
                other
            ))),
        }
    }

    /// Element-wise maximum of two single-column buffers
    ///
    /// Performs element-wise maximum using GPU kernels.
    ///
    /// # Arguments
    /// * `a` - First operand buffer (single column)
    /// * `b` - Second operand buffer (single column)
    ///
    /// # Returns
    /// A new CudaBuffer containing the element-wise maximums
    ///
    /// # Errors
    /// Returns `XlogError::Kernel` if:
    /// - Row counts don't match
    /// - Buffers are not single-column
    /// - Type is not supported for arithmetic
    pub fn max_columns(&self, a: &CudaBuffer, b: &CudaBuffer) -> Result<CudaBuffer> {
        match a.schema().column_type(0) {
            Some(ScalarType::I64) => {
                self.binary_arith_op_device::<i64>(a, b, 6, arith_kernels::ARITH_BINARY_I64)
            }
            Some(ScalarType::I32) => {
                self.binary_arith_op_device::<i32>(a, b, 6, arith_kernels::ARITH_BINARY_I32)
            }
            Some(ScalarType::U64) => {
                self.binary_arith_op_device::<u64>(a, b, 6, arith_kernels::ARITH_BINARY_U64)
            }
            Some(ScalarType::U32 | ScalarType::Symbol) => {
                self.binary_arith_op_device::<u32>(a, b, 6, arith_kernels::ARITH_BINARY_U32)
            }
            Some(ScalarType::F64) => {
                self.binary_arith_op_device::<f64>(a, b, 6, arith_kernels::ARITH_BINARY_F64)
            }
            Some(ScalarType::F32) => {
                self.binary_arith_op_device::<f32>(a, b, 6, arith_kernels::ARITH_BINARY_F32)
            }
            other => Err(XlogError::Kernel(format!(
                "Arithmetic not supported for {:?}",
                other
            ))),
        }
    }

    /// Element-wise power of two single-column buffers
    ///
    /// Converts both operands to f64, computes x^y on the GPU, and returns f64 result.
    /// This matches the behavior of most database systems where pow() returns a float.
    ///
    /// # Arguments
    /// * `base` - Base values buffer (single column)
    /// * `exp` - Exponent values buffer (single column)
    ///
    /// # Returns
    /// A new CudaBuffer containing the element-wise powers as f64
    ///
    /// # Errors
    /// Returns `XlogError::Kernel` if:
    /// - Row counts don't match
    /// - Buffers are not single-column
    /// - Type is not supported for arithmetic
    pub fn pow_columns(&self, base: &CudaBuffer, exp: &CudaBuffer) -> Result<CudaBuffer> {
        if base.num_rows() != exp.num_rows() {
            return Err(XlogError::Kernel("Row count mismatch".into()));
        }
        if base.arity() != 1 || exp.arity() != 1 {
            return Err(XlogError::Kernel(
                "Arithmetic requires single-column buffers".into(),
            ));
        }

        if base.num_rows() == 0 {
            let schema = Schema::new(vec![("result".to_string(), ScalarType::F64)]);
            return self.create_empty_buffer(schema);
        }

        let n: u32 = base.num_rows().try_into().map_err(|_| {
            XlogError::Kernel(format!(
                "pow_columns: row count {} exceeds u32::MAX",
                base.num_rows()
            ))
        })?;

        let base_f64_buf = if base.schema().column_type(0) == Some(ScalarType::F64) {
            None
        } else {
            Some(self.cast_column(base, ScalarType::F64)?)
        };
        let base_buf = base_f64_buf.as_ref().unwrap_or(base);

        let exp_f64_buf = if exp.schema().column_type(0) == Some(ScalarType::F64) {
            None
        } else {
            Some(self.cast_column(exp, ScalarType::F64)?)
        };
        let exp_buf = exp_f64_buf.as_ref().unwrap_or(exp);

        let base_col = base_buf
            .column(0)
            .ok_or_else(|| XlogError::Kernel("Missing base column".into()))?;
        let exp_col = exp_buf
            .column(0)
            .ok_or_else(|| XlogError::Kernel("Missing exp column".into()))?;

        let expected_bytes = (n as usize)
            .checked_mul(std::mem::size_of::<f64>())
            .ok_or_else(|| XlogError::Kernel("pow_columns size overflow".into()))?;
        if base_col.num_bytes() != expected_bytes || exp_col.num_bytes() != expected_bytes {
            return Err(XlogError::Kernel(format!(
                "pow_columns: expected {} bytes for {} rows",
                expected_bytes,
                base.num_rows()
            )));
        }

        let mut out = self.memory.alloc::<u8>(expected_bytes)?;
        let func = self
            .device
            .inner()
            .get_func(ARITH_MODULE, arith_kernels::ARITH_POW_F64)
            .ok_or_else(|| XlogError::Kernel("arith_pow_f64 not found".into()))?;
        let config = LaunchConfig::for_num_elems(n);

        unsafe {
            func.clone()
                .launch(config, (base_col, exp_col, n, &mut out))
        }
        .map_err(|e| XlogError::Kernel(format!("pow_f64 failed: {}", e)))?;

        self.device.synchronize()?;

        let schema = Schema::new(vec![("result".to_string(), ScalarType::F64)]);
        self.buffer_from_columns_with_device_count(vec![out.into()], base.num_rows(), schema, base)
    }

    /// Conditional select between two single-column buffers based on a boolean mask.
    ///
    /// For each row: out[i] = mask[i] ? then_vals[i] : else_vals[i]
    ///
    /// # Arguments
    /// * `mask` - Boolean mask buffer (single column, type Bool/u8)
    /// * `then_vals` - Values to select when mask is true
    /// * `else_vals` - Values to select when mask is false
    ///
    /// # Returns
    /// A new CudaBuffer with values selected based on the mask
    ///
    /// # Errors
    /// Returns `XlogError::Kernel` if:
    /// - Row counts don't match
    /// - Buffers are not single-column
    /// - Types of then/else values don't match
    pub fn select_columns(
        &self,
        mask: &CudaBuffer,
        then_vals: &CudaBuffer,
        else_vals: &CudaBuffer,
    ) -> Result<CudaBuffer> {
        if mask.num_rows() != then_vals.num_rows() || mask.num_rows() != else_vals.num_rows() {
            return Err(XlogError::Kernel("Row count mismatch in select".into()));
        }
        if mask.arity() != 1 || then_vals.arity() != 1 || else_vals.arity() != 1 {
            return Err(XlogError::Kernel(
                "Select requires single-column buffers".into(),
            ));
        }

        let then_type = then_vals.schema().column_type(0);
        let else_type = else_vals.schema().column_type(0);
        if then_type != else_type {
            return Err(XlogError::Kernel(format!(
                "Type mismatch in select: then={:?}, else={:?}",
                then_type, else_type
            )));
        }

        if mask.num_rows() == 0 {
            let result_type = then_type.unwrap_or(ScalarType::I64);
            let schema = Schema::new(vec![("result".to_string(), result_type)]);
            return self.create_empty_buffer(schema);
        }

        let n: u32 = mask.num_rows().try_into().map_err(|_| {
            XlogError::Kernel(format!(
                "select_columns: row count {} exceeds u32::MAX",
                mask.num_rows()
            ))
        })?;

        let mask_col = mask
            .column(0)
            .ok_or_else(|| XlogError::Kernel("Missing mask column".into()))?;
        let then_col = then_vals
            .column(0)
            .ok_or_else(|| XlogError::Kernel("Missing then column".into()))?;
        let else_col = else_vals
            .column(0)
            .ok_or_else(|| XlogError::Kernel("Missing else column".into()))?;

        let result_type = then_type.unwrap_or(ScalarType::I64);
        let elem_size = result_type.size_bytes();
        let expected_bytes = (n as usize)
            .checked_mul(elem_size)
            .ok_or_else(|| XlogError::Kernel("select_columns size overflow".into()))?;

        let mut out = self.memory.alloc::<u8>(expected_bytes)?;

        let kernel_name = match result_type {
            ScalarType::I64 => arith_kernels::ARITH_SELECT_I64,
            ScalarType::I32 => arith_kernels::ARITH_SELECT_I32,
            ScalarType::U64 => arith_kernels::ARITH_SELECT_U64,
            ScalarType::U32 | ScalarType::Symbol => arith_kernels::ARITH_SELECT_U32,
            ScalarType::F64 => arith_kernels::ARITH_SELECT_F64,
            ScalarType::F32 => arith_kernels::ARITH_SELECT_F32,
            ScalarType::Bool => {
                // Bool is stored as u8, treat as u8 select (use fill + mask trick)
                // For simplicity, cast to u32 and back
                return self.select_columns_bool(mask, then_vals, else_vals);
            }
        };

        let func = self
            .device
            .inner()
            .get_func(ARITH_MODULE, kernel_name)
            .ok_or_else(|| XlogError::Kernel(format!("{} not found", kernel_name)))?;
        let config = LaunchConfig::for_num_elems(n);

        unsafe {
            func.clone()
                .launch(config, (mask_col, then_col, else_col, n, &mut out))
        }
        .map_err(|e| XlogError::Kernel(format!("select kernel failed: {}", e)))?;

        self.device.synchronize()?;

        let schema = Schema::new(vec![("result".to_string(), result_type)]);
        self.buffer_from_columns_with_device_count(vec![out.into()], mask.num_rows(), schema, mask)
    }

    /// Helper for select_columns when result type is Bool
    fn select_columns_bool(
        &self,
        mask: &CudaBuffer,
        then_vals: &CudaBuffer,
        else_vals: &CudaBuffer,
    ) -> Result<CudaBuffer> {
        // Cast bool columns to u32, select, then cast back
        let then_u32 = self.cast_column(then_vals, ScalarType::U32)?;
        let else_u32 = self.cast_column(else_vals, ScalarType::U32)?;
        let result_u32 = self.select_columns(mask, &then_u32, &else_u32)?;
        self.cast_column(&result_u32, ScalarType::Bool)
    }

    /// Cast a single-column buffer to a different type
    ///
    /// Casts data on the GPU using the arithmetic cast kernel.
    ///
    /// # Arguments
    /// * `a` - Input buffer (single column)
    /// * `target` - Target scalar type
    ///
    /// # Returns
    /// A new CudaBuffer with the cast values
    ///
    /// # Errors
    /// Returns `XlogError::Kernel` if:
    /// - Buffer is not single-column
    /// - Source or target type is not supported for casting
    pub fn cast_column(&self, a: &CudaBuffer, target: ScalarType) -> Result<CudaBuffer> {
        if a.arity() != 1 {
            return Err(XlogError::Kernel(
                "Cast requires single-column buffer".into(),
            ));
        }

        let source_type = a
            .schema()
            .column_type(0)
            .ok_or_else(|| XlogError::Kernel("Missing column type".into()))?;

        let schema = Schema::new(vec![("result".to_string(), target)]);

        if a.num_rows() == 0 {
            return self.create_empty_buffer(schema);
        }

        let n: u32 = a.num_rows().try_into().map_err(|_| {
            XlogError::Kernel(format!(
                "cast_column: row count {} exceeds u32::MAX",
                a.num_rows()
            ))
        })?;

        let src_col = a
            .column(0)
            .ok_or_else(|| XlogError::Kernel("Missing column 0".into()))?;
        let src_bytes = (n as usize)
            .checked_mul(source_type.size_bytes())
            .ok_or_else(|| XlogError::Kernel("cast_column size overflow".into()))?;
        if src_col.num_bytes() != src_bytes {
            return Err(XlogError::Kernel(format!(
                "Column 0 has {} bytes but expected {} for {} rows",
                src_col.num_bytes(),
                src_bytes,
                a.num_rows()
            )));
        }

        let dst_bytes = (n as usize)
            .checked_mul(target.size_bytes())
            .ok_or_else(|| XlogError::Kernel("cast_column size overflow".into()))?;
        let mut out = self.memory.alloc::<u8>(dst_bytes)?;

        let func = self
            .device
            .inner()
            .get_func(ARITH_MODULE, arith_kernels::ARITH_CAST)
            .ok_or_else(|| XlogError::Kernel("arith_cast not found".into()))?;
        let config = LaunchConfig::for_num_elems(n);

        unsafe {
            func.clone().launch(
                config,
                (
                    src_col,
                    &mut out,
                    n,
                    source_type.to_code(),
                    target.to_code(),
                ),
            )
        }
        .map_err(|e| XlogError::Kernel(format!("cast failed: {}", e)))?;

        self.device.synchronize()?;

        self.buffer_from_columns_with_device_count(vec![out.into()], a.num_rows(), schema, a)
    }

    /// Helper for binary arithmetic operations on device.
    fn binary_arith_op_device<T: DeviceRepr>(
        &self,
        a: &CudaBuffer,
        b: &CudaBuffer,
        op: u8,
        kernel: &str,
    ) -> Result<CudaBuffer> {
        if a.num_rows() != b.num_rows() {
            return Err(XlogError::Kernel("Row count mismatch".into()));
        }
        if a.arity() != 1 || b.arity() != 1 {
            return Err(XlogError::Kernel(
                "Arithmetic requires single-column buffers".into(),
            ));
        }
        if a.schema().column_type(0) != b.schema().column_type(0) {
            return Err(XlogError::Kernel(
                "Arithmetic requires matching column types".into(),
            ));
        }
        if a.num_rows() == 0 {
            return self.create_empty_buffer(a.schema.clone());
        }

        let n: u32 = a.num_rows().try_into().map_err(|_| {
            XlogError::Kernel(format!(
                "arith: row count {} exceeds u32::MAX",
                a.num_rows()
            ))
        })?;

        let expected_bytes = (n as usize)
            .checked_mul(std::mem::size_of::<T>())
            .ok_or_else(|| XlogError::Kernel("arith output size overflow".into()))?;

        let col_a = a
            .column(0)
            .ok_or_else(|| XlogError::Kernel("Missing column 0".into()))?;
        let col_b = b
            .column(0)
            .ok_or_else(|| XlogError::Kernel("Missing column 0".into()))?;

        if col_a.num_bytes() != expected_bytes || col_b.num_bytes() != expected_bytes {
            return Err(XlogError::Kernel(format!(
                "Arithmetic expects {} bytes per column for {} rows",
                expected_bytes,
                a.num_rows()
            )));
        }

        let mut out = self.memory.alloc::<u8>(expected_bytes)?;
        let func = self
            .device
            .inner()
            .get_func(ARITH_MODULE, kernel)
            .ok_or_else(|| XlogError::Kernel("arith kernel not found".into()))?;
        let config = LaunchConfig::for_num_elems(n);

        unsafe { func.clone().launch(config, (col_a, col_b, n, op, &mut out)) }
            .map_err(|e| XlogError::Kernel(format!("arith binary failed: {}", e)))?;

        self.device.synchronize()?;
        self.buffer_from_columns_with_device_count(
            vec![out.into()],
            a.num_rows(),
            a.schema.clone(),
            a,
        )
    }

    /// Combine multiple single-column buffers into a multi-column buffer
    ///
    /// # Arguments
    /// * `columns` - Vector of single-column CudaBuffers to combine
    /// * `types` - Vector of ScalarTypes for each column
    ///
    /// # Returns
    /// A new CudaBuffer with all columns combined
    pub fn combine_columns(
        &self,
        columns: Vec<CudaBuffer>,
        types: Vec<ScalarType>,
    ) -> Result<CudaBuffer> {
        if columns.is_empty() {
            let schema_cols: Vec<(String, ScalarType)> = types
                .iter()
                .enumerate()
                .map(|(i, t)| (format!("col_{}", i), *t))
                .collect();
            let schema = Schema::new(schema_cols);
            return self.create_empty_buffer(schema);
        }

        let row_cap = columns[0].row_cap;

        // Verify all columns have the same row capacity and are single-column
        for (i, col) in columns.iter().enumerate() {
            if col.row_cap != row_cap {
                return Err(XlogError::Kernel(format!(
                    "Column {} has row capacity {}, expected {}",
                    i, col.row_cap, row_cap
                )));
            }
            if col.arity() != 1 {
                return Err(XlogError::Kernel(format!(
                    "Column {} buffer has {} columns, expected 1",
                    i,
                    col.arity()
                )));
            }
        }

        let device = self.device.inner();
        let mut d_num_rows = self.memory.alloc::<u32>(1)?;
        device
            .dtod_copy(columns[0].num_rows_device(), &mut d_num_rows)
            .map_err(|e| XlogError::Kernel(format!("Failed to copy row count: {}", e)))?;

        let mut result_columns = Vec::with_capacity(columns.len());
        for (i, col_buf) in columns.into_iter().enumerate() {
            let src_col = col_buf
                .columns
                .into_iter()
                .next()
                .ok_or_else(|| XlogError::Kernel(format!("Column {} buffer has no data", i)))?;
            result_columns.push(src_col);
        }

        let schema_cols: Vec<(String, ScalarType)> = types
            .iter()
            .enumerate()
            .map(|(i, t)| (format!("col_{}", i), *t))
            .collect();
        let schema = Schema::new(schema_cols);

        Ok(CudaBuffer::from_columns(
            result_columns,
            row_cap,
            d_num_rows,
            schema,
        ))
    }
}