dbx-core 0.2.2

High-performance file-based database engine with 5-Tier Hybrid Storage
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
//! HashJoin Operator — Hash-based join implementation

use crate::error::{DbxError, DbxResult};
use crate::sql::executor::hash_utils;
use crate::sql::executor::operators::PhysicalOperator;
use crate::sql::executor::spill::SpillContext;
use crate::sql::planner::JoinType;
use ahash::AHashMap;
use arrow::array::*;
use arrow::compute;
use arrow::datatypes::{DataType, Schema};
use arrow::record_batch::RecordBatch;
// use rayon::prelude::*; // Not used currently in sequential probe
use smallvec::{SmallVec, smallvec};
use std::path::PathBuf;
use std::sync::Arc;

/// Hash Join 실행 상태
#[derive(Debug, Clone, PartialEq)]
enum JoinState {
    InMemory,
    Partitioning,
    JoiningPartitions { current_partition: usize },
}

pub struct HashJoinOperator {
    left: Box<dyn PhysicalOperator>,
    right: Box<dyn PhysicalOperator>,
    schema: Arc<Schema>,
    on: Vec<(usize, usize)>,
    join_type: JoinType,
    build_table: Option<AHashMap<u64, Vec<usize>>>,
    left_batch: Option<RecordBatch>,
    right_batches: Option<Vec<RecordBatch>>,
    right_batch_idx: usize,
    done: bool,
    build_memory_budget: usize,
    state: JoinState,
    spill_ctx: Option<SpillContext>,
    /// 파티션 수 (Grace Hash Join)
    num_partitions: usize,
    /// 왼쪽 파티션 파일 경로 목록: [partition_idx][file_paths]
    left_partitions: Vec<Vec<PathBuf>>,
    /// 오른쪽 파티션 파일 경로 목록: [partition_idx][file_paths]
    right_partitions: Vec<Vec<PathBuf>>,
    /// Hybrid Hash Join: Partition 0을 메모리에 유지
    left_partition0: Vec<RecordBatch>,
    /// Hybrid Hash Join: Partition 0의 오른쪽 데이터 메모리에 유지
    right_partition0: Vec<RecordBatch>,
    /// Partition 0의 현재 메모리 사용량 추적
    partition0_mem_used: usize,
    /// 재귀적 파티셔닝 깊이 추적: [partition_idx]
    partition_depths: Vec<usize>,
}

impl HashJoinOperator {
    pub fn new(
        left: Box<dyn PhysicalOperator>,
        right: Box<dyn PhysicalOperator>,
        schema: Arc<Schema>,
        on: Vec<(usize, usize)>,
        join_type: JoinType,
    ) -> Self {
        Self {
            left,
            right,
            schema,
            on,
            join_type,
            build_table: None,
            left_batch: None,
            right_batches: None,
            right_batch_idx: 0,
            done: false,
            build_memory_budget: 256 * 1024 * 1024,
            state: JoinState::InMemory,
            spill_ctx: None,
            num_partitions: 32,
            left_partitions: (0..32).map(|_| Vec::new()).collect(),
            right_partitions: (0..32).map(|_| Vec::new()).collect(),
            left_partition0: Vec::new(),
            right_partition0: Vec::new(),
            partition0_mem_used: 0,
            partition_depths: vec![0; 32],
        }
    }

    pub fn with_spill(mut self, ctx: SpillContext) -> Self {
        self.spill_ctx = Some(ctx);
        self
    }

    pub fn with_build_memory_limit(mut self, bytes: usize) -> Self {
        self.build_memory_budget = bytes;
        self
    }

    fn partition_batch(
        &self,
        batch: &RecordBatch,
        key_indices: &[usize],
        seed: u64,
    ) -> DbxResult<Vec<Option<RecordBatch>>> {
        let num_partitions = self.num_partitions;
        let mut row_indices: Vec<Vec<u32>> = (0..num_partitions).map(|_| Vec::new()).collect();

        let hashes = hash_utils::hash_batch(batch, key_indices, seed)?;
        for row_idx in 0..batch.num_rows() {
            let hash = hashes.value(row_idx);
            let part_idx = (hash % num_partitions as u64) as usize;
            row_indices[part_idx].push(row_idx as u32);
        }

        let mut partitions = Vec::with_capacity(num_partitions);
        for indices in row_indices {
            if indices.is_empty() {
                partitions.push(None);
            } else {
                let idx_array = UInt32Array::from(indices);
                let columns = batch
                    .columns()
                    .iter()
                    .map(|col| compute::take(col.as_ref(), &idx_array, None))
                    .collect::<Result<Vec<_>, _>>()?;
                let partitioned_batch = RecordBatch::try_new(batch.schema(), columns)?;
                partitions.push(Some(partitioned_batch));
            }
        }
        Ok(partitions)
    }

    fn handle_partitioning(
        &mut self,
        batch: RecordBatch,
        key_indices: &[usize],
        is_left: bool,
        depth: usize,
        base_idx: usize,
    ) -> DbxResult<()> {
        let _num_partitions = self.num_partitions;
        let parts = self.partition_batch(&batch, key_indices, depth as u64)?;

        for (sub_idx, part) in parts.into_iter().enumerate() {
            if let Some(p) = part {
                let target_idx = base_idx + sub_idx;
                let p_bytes = SpillContext::estimate_batch_bytes(&p);

                // Hybrid: Level 0의 Partition 0만 메모리에 유지
                if depth == 0
                    && target_idx == 0
                    && self.partition0_mem_used + p_bytes < (self.build_memory_budget * 7 / 10)
                {
                    if is_left {
                        self.left_partition0.push(p);
                    } else {
                        self.right_partition0.push(p);
                    }
                    self.partition0_mem_used += p_bytes;
                } else {
                    let spill_ctx = self.spill_ctx.as_mut().unwrap();
                    let prefix = if is_left { "left" } else { "right" };

                    // P0 메모리 초과 시 디스크로 스필 (Grace 모드 폴백)
                    if depth == 0
                        && target_idx == 0
                        && (if is_left {
                            !self.left_partition0.is_empty()
                        } else {
                            !self.right_partition0.is_empty()
                        })
                    {
                        let to_spill = if is_left {
                            std::mem::take(&mut self.left_partition0)
                        } else {
                            std::mem::take(&mut self.right_partition0)
                        };
                        for p0_batch in to_spill {
                            let path = spill_ctx.spill_partition_batch(prefix, 0, p0_batch)?;
                            if is_left {
                                self.left_partitions[0].push(path);
                            } else {
                                self.right_partitions[0].push(path);
                            }
                        }
                    }

                    let path = spill_ctx.spill_partition_batch(prefix, target_idx, p)?;
                    if is_left {
                        self.left_partitions[target_idx].push(path);
                    } else {
                        self.right_partitions[target_idx].push(path);
                    }
                }
            }
        }
        Ok(())
    }

    fn build_phase(&mut self) -> DbxResult<()> {
        let mut left_batches: SmallVec<[RecordBatch; 8]> = smallvec![];
        let mut left_bytes = 0usize;

        while let Some(batch) = self.left.next()? {
            if batch.num_rows() == 0 {
                continue;
            }
            let batch_bytes = SpillContext::estimate_batch_bytes(&batch);
            left_bytes += batch_bytes;
            left_batches.push(batch);

            if left_bytes > self.build_memory_budget && self.spill_ctx.is_some() {
                self.state = JoinState::Partitioning;
                break;
            }
        }

        if matches!(self.state, JoinState::Partitioning) {
            let left_indices: Vec<usize> = self.on.iter().map(|(l, _)| *l).collect();
            let right_indices: Vec<usize> = self.on.iter().map(|(_, r)| *r).collect();

            for batch in left_batches {
                self.handle_partitioning(batch, &left_indices, true, 0, 0)?;
            }

            while let Some(batch) = self.left.next()? {
                if batch.num_rows() == 0 {
                    continue;
                }
                self.handle_partitioning(batch, &left_indices, true, 0, 0)?;
            }

            while let Some(batch) = self.right.next()? {
                if batch.num_rows() == 0 {
                    continue;
                }
                self.handle_partitioning(batch, &right_indices, false, 0, 0)?;
            }

            self.state = JoinState::JoiningPartitions {
                current_partition: 0,
            };
            return Ok(());
        }

        let mut right_batches: SmallVec<[RecordBatch; 8]> = smallvec![];
        let mut right_bytes = 0usize;
        while let Some(batch) = self.right.next()? {
            if batch.num_rows() > 0 {
                right_bytes += SpillContext::estimate_batch_bytes(&batch);
                right_batches.push(batch);
            }
        }

        let build_side_bytes = left_bytes.min(right_bytes);
        if build_side_bytes > self.build_memory_budget {
            return Err(DbxError::Storage(format!(
                "OOM: HashJoin 빌드 테이블 크기 ({} MB)가 메모리 한도 ({} MB)를 초과합니다.",
                build_side_bytes / (1024 * 1024),
                self.build_memory_budget / (1024 * 1024),
            )));
        }

        if left_batches.is_empty() || right_batches.is_empty() {
            self.build_table = Some(AHashMap::new());
            self.left_batch = None;
            self.right_batches = Some(Vec::new());
            return Ok(());
        }

        let left_rows: usize = left_batches.iter().map(|b| b.num_rows()).sum();
        let right_rows: usize = right_batches.iter().map(|b| b.num_rows()).sum();

        let (build_batches, probe_batches, build_is_left) =
            if right_rows < left_rows && matches!(self.join_type, JoinType::Inner) {
                (right_batches, left_batches, false)
            } else {
                (left_batches, right_batches, true)
            };

        let schema = build_batches[0].schema();
        let merged = super::super::concat_batches(&schema, build_batches.as_slice())?;

        let key_columns: Vec<usize> = if build_is_left {
            self.on.iter().map(|(left_col, _)| *left_col).collect()
        } else {
            self.on.iter().map(|(_, right_col)| *right_col).collect()
        };

        let mut hash_table: AHashMap<u64, Vec<usize>> = AHashMap::new();
        let hashes = hash_utils::hash_batch(&merged, &key_columns, 0)?;
        for row_idx in 0..merged.num_rows() {
            let hash = hashes.value(row_idx);
            hash_table.entry(hash).or_default().push(row_idx);
        }

        self.left_batch = Some(merged);
        self.right_batches = Some(probe_batches.into_vec());
        self.build_table = Some(hash_table);

        Ok(())
    }

    fn next_in_memory(&mut self) -> DbxResult<Option<RecordBatch>> {
        if self.left_batch.is_none() || self.right_batches.is_none() {
            self.done = true;
            return Ok(None);
        }
        let build_table = self.build_table.as_ref().unwrap();
        let left_batch = self.left_batch.as_ref().unwrap();
        let right_batches = self.right_batches.as_ref().unwrap();

        Self::do_probe(
            &mut self.right_batch_idx,
            build_table,
            left_batch,
            right_batches,
            self.join_type,
            &self.on,
            &self.schema,
        )
    }

    fn estimate_partition_size(&self, paths: &[PathBuf]) -> DbxResult<usize> {
        let mut total = 0;
        for path in paths {
            let meta = std::fs::metadata(path).map_err(|e| DbxError::Storage(e.to_string()))?;
            total += meta.len() as usize;
        }
        Ok(total)
    }

    fn next_partitioned(&mut self, current_part_ptr: &mut usize) -> DbxResult<Option<RecordBatch>> {
        loop {
            if let Some(right_batches) = &self.right_batches
                && self.right_batch_idx < right_batches.len()
            {
                let build_table = self
                    .build_table
                    .as_ref()
                    .expect("Partition build table must exist");
                let left_batch = self
                    .left_batch
                    .as_ref()
                    .expect("Partition left batch must exist");
                if let Some(result) = Self::do_probe(
                    &mut self.right_batch_idx,
                    build_table,
                    left_batch,
                    right_batches,
                    self.join_type,
                    &self.on,
                    &self.schema,
                )? {
                    return Ok(Some(result));
                }
            }

            if *current_part_ptr >= self.num_partitions {
                self.done = true;
                return Ok(None);
            }

            let part_idx = *current_part_ptr;
            let depth = self.partition_depths[part_idx];
            *current_part_ptr += 1;

            // Hybrid: Partition 0이 메모리에 있으면 바로 사용 (디스크 로드 생략)
            if part_idx == 0 && !self.left_partition0.is_empty() {
                let l_schema = Arc::new(self.left.schema().clone());
                let l0_batches = std::mem::take(&mut self.left_partition0);
                let left_merged = super::super::concat_batches(&l_schema, &l0_batches)?;

                let key_indices: Vec<usize> = self.on.iter().map(|(l, _)| *l).collect();
                let mut hash_table: AHashMap<u64, Vec<usize>> = AHashMap::new();
                let hashes = hash_utils::hash_batch(&left_merged, &key_indices, 0)?;
                for row_idx in 0..left_merged.num_rows() {
                    let hash = hashes.value(row_idx);
                    hash_table.entry(hash).or_default().push(row_idx);
                }

                let r0_batches = std::mem::take(&mut self.right_partition0);
                self.partition0_mem_used = 0;

                self.left_batch = Some(left_merged);
                self.right_batches = Some(r0_batches);
                self.build_table = Some(hash_table);
                self.right_batch_idx = 0;
                continue;
            }

            if self.left_partitions[part_idx].is_empty() {
                continue;
            }

            // Recursive Partitioning: 단일 파티션이 여전히 메모리 예산을 초과하면 더 나눔
            let left_size = self.estimate_partition_size(&self.left_partitions[part_idx])?;
            if left_size > self.build_memory_budget {
                if depth >= 3 {
                    return Err(DbxError::Storage(format!(
                        "OOM: 최대 재귀 깊이(3)에 도달했습니다. 데이터 스큐가 너무 심해 파티셔닝이 불가합니다. (Part: {})",
                        part_idx
                    )));
                }

                let next_depth = depth + 1;
                let base_idx = self.num_partitions;

                // 파티션 목록 확장
                self.num_partitions += 32;
                self.left_partitions.resize(self.num_partitions, Vec::new());
                self.right_partitions
                    .resize(self.num_partitions, Vec::new());
                self.partition_depths
                    .resize(self.num_partitions, next_depth);

                let left_paths = std::mem::take(&mut self.left_partitions[part_idx]);
                let right_paths = std::mem::take(&mut self.right_partitions[part_idx]);

                let left_indices: Vec<usize> = self.on.iter().map(|(l, _)| *l).collect();
                let right_indices: Vec<usize> = self.on.iter().map(|(_, r)| *r).collect();

                for path in left_paths {
                    for batch in SpillContext::reload_batches(&path)? {
                        self.handle_partitioning(batch, &left_indices, true, next_depth, base_idx)?;
                    }
                }
                for path in right_paths {
                    for batch in SpillContext::reload_batches(&path)? {
                        self.handle_partitioning(
                            batch,
                            &right_indices,
                            false,
                            next_depth,
                            base_idx,
                        )?;
                    }
                }
                continue;
            }

            let mut l_batches = Vec::new();
            for path in &self.left_partitions[part_idx] {
                l_batches.extend(SpillContext::reload_batches(path)?);
            }
            let l_schema = Arc::new(self.left.schema().clone());
            let left_merged = super::super::concat_batches(&l_schema, &l_batches)?;

            let key_indices: Vec<usize> = self.on.iter().map(|(l, _)| *l).collect();
            let mut hash_table: AHashMap<u64, Vec<usize>> = AHashMap::new();
            let hashes = hash_utils::hash_batch(&left_merged, &key_indices, 0)?;
            for row_idx in 0..left_merged.num_rows() {
                let hash = hashes.value(row_idx);
                hash_table.entry(hash).or_default().push(row_idx);
            }

            let mut r_batches = Vec::new();
            for path in &self.right_partitions[part_idx] {
                r_batches.extend(SpillContext::reload_batches(path)?);
            }

            self.left_batch = Some(left_merged);
            self.right_batches = Some(r_batches);
            self.build_table = Some(hash_table);
            self.right_batch_idx = 0;
        }
    }

    /// 실제 Probing 작업을 처리하는 메서드 (Borrow Checker 회피를 위해 static으로 분리)
    fn do_probe(
        right_batch_idx: &mut usize,
        build_table: &AHashMap<u64, Vec<usize>>,
        left_batch: &RecordBatch,
        right_batches: &[RecordBatch],
        join_type: JoinType,
        on: &[(usize, usize)],
        output_schema: &Arc<Schema>,
    ) -> DbxResult<Option<RecordBatch>> {
        while *right_batch_idx < right_batches.len() {
            let right_batch = &right_batches[*right_batch_idx];
            *right_batch_idx += 1;
            if right_batch.num_rows() == 0 {
                continue;
            }

            let mut left_indices = Vec::new();
            let mut right_indices = Vec::new();

            let mut matched_left_rows = if matches!(join_type, JoinType::Left) {
                Some(std::collections::HashSet::new())
            } else {
                None
            };
            let mut matched_right_rows = if matches!(join_type, JoinType::Right) {
                Some(vec![false; right_batch.num_rows()])
            } else {
                None
            };

            let right_key_columns: Vec<usize> = on.iter().map(|(_, r)| *r).collect();
            let left_key_columns: Vec<usize> = on.iter().map(|(l, _)| *l).collect();

            let hashes = hash_utils::hash_batch(right_batch, &right_key_columns, 0)?;
            for right_row in 0..right_batch.num_rows() {
                let hash = hashes.value(right_row);
                if let Some(left_rows) = build_table.get(&hash) {
                    for &left_row in left_rows {
                        // 해시 충돌 대응: 실제로 값이 같은지 확인
                        if compare_rows(
                            left_batch,
                            &left_key_columns,
                            left_row,
                            right_batch,
                            &right_key_columns,
                            right_row,
                        ) {
                            left_indices.push(left_row as u32);
                            right_indices.push(right_row as u32);
                            if let Some(ref mut m) = matched_left_rows {
                                m.insert(left_row);
                            }
                            if let Some(ref mut m) = matched_right_rows {
                                m[right_row] = true;
                            }
                        }
                    }
                }
            }

            if let Some(matched) = matched_left_rows {
                for left_row in 0..left_batch.num_rows() {
                    if !matched.contains(&left_row) {
                        left_indices.push(left_row as u32);
                        right_indices.push(u32::MAX);
                    }
                }
            }
            if let Some(matched) = matched_right_rows {
                for (right_row, &was_matched) in matched.iter().enumerate() {
                    if !was_matched {
                        left_indices.push(u32::MAX);
                        right_indices.push(right_row as u32);
                    }
                }
            }

            if left_indices.is_empty() {
                continue;
            }

            let mut output_columns: Vec<ArrayRef> = Vec::new();
            for col in left_batch.columns() {
                output_columns.push(create_column_with_nulls(col, &left_indices, u32::MAX)?);
            }
            for col in right_batch.columns() {
                output_columns.push(create_column_with_nulls(col, &right_indices, u32::MAX)?);
            }

            let result = RecordBatch::try_new(Arc::clone(output_schema), output_columns)?;
            if result.num_rows() > 0 {
                return Ok(Some(result));
            }
        }
        Ok(None)
    }
}

impl PhysicalOperator for HashJoinOperator {
    fn schema(&self) -> &Schema {
        &self.schema
    }
    fn next(&mut self) -> DbxResult<Option<RecordBatch>> {
        if self.done {
            return Ok(None);
        }
        if self.build_table.is_none() && matches!(self.state, JoinState::InMemory) {
            self.build_phase()?;
        }

        let state = self.state.clone();
        match state {
            JoinState::InMemory | JoinState::Partitioning => self.next_in_memory(),
            JoinState::JoiningPartitions {
                mut current_partition,
            } => {
                let res = self.next_partitioned(&mut current_partition);
                self.state = JoinState::JoiningPartitions { current_partition };
                res
            }
        }
    }
    fn reset(&mut self) -> DbxResult<()> {
        self.build_table = None;
        self.left_batch = None;
        self.right_batches = None;
        self.right_batch_idx = 0;
        self.done = false;
        self.state = JoinState::InMemory;
        self.left.reset()?;
        self.right.reset()
    }
}

/// 두 행의 지정된 컬럼 값이 모두 일치하는지 비교합니다 (해시 충돌 방어용).
fn compare_rows(
    left_batch: &RecordBatch,
    left_cols: &[usize],
    left_row: usize,
    right_batch: &RecordBatch,
    right_cols: &[usize],
    right_row: usize,
) -> bool {
    for i in 0..left_cols.len() {
        let l_col = left_batch.column(left_cols[i]);
        let r_col = right_batch.column(right_cols[i]);

        if !compare_column_values(l_col, left_row, r_col, right_row) {
            return false;
        }
    }
    true
}

fn compare_column_values(l_col: &ArrayRef, l_row: usize, r_col: &ArrayRef, r_row: usize) -> bool {
    if l_col.is_null(l_row) || r_col.is_null(r_row) {
        return l_col.is_null(l_row) && r_col.is_null(r_row);
    }

    match l_col.data_type() {
        DataType::Int32 => {
            let l = l_col.as_any().downcast_ref::<Int32Array>().unwrap();
            let r = r_col.as_any().downcast_ref::<Int32Array>().unwrap();
            l.value(l_row) == r.value(r_row)
        }
        DataType::Int64 => {
            let l = l_col.as_any().downcast_ref::<Int64Array>().unwrap();
            let r = r_col.as_any().downcast_ref::<Int64Array>().unwrap();
            l.value(l_row) == r.value(r_row)
        }
        DataType::Float64 => {
            let l = l_col.as_any().downcast_ref::<Float64Array>().unwrap();
            let r = r_col.as_any().downcast_ref::<Float64Array>().unwrap();
            l.value(l_row) == r.value(r_row)
        }
        DataType::Utf8 => {
            let l = l_col.as_any().downcast_ref::<StringArray>().unwrap();
            let r = r_col.as_any().downcast_ref::<StringArray>().unwrap();
            l.value(l_row) == r.value(r_row)
        }
        _ => format!("{:?}", l_col.as_any()) == format!("{:?}", r_col.as_any()),
    }
}

fn create_column_with_nulls(
    source_col: &ArrayRef,
    indices: &[u32],
    null_sentinel: u32,
) -> DbxResult<ArrayRef> {
    let num_rows = indices.len();
    let has_nulls = indices.contains(&null_sentinel);
    if !has_nulls {
        let idx_arr = UInt32Array::from(indices.to_vec());
        return compute::take(source_col.as_ref(), &idx_arr, None).map_err(Into::into);
    }

    match source_col.data_type() {
        DataType::Int32 => {
            let source = source_col.as_any().downcast_ref::<Int32Array>().unwrap();
            let mut builder = Int32Builder::with_capacity(num_rows);
            for &idx in indices {
                if idx == null_sentinel {
                    builder.append_null();
                } else {
                    builder.append_value(source.value(idx as usize));
                }
            }
            Ok(Arc::new(builder.finish()))
        }
        DataType::Int64 => {
            let source = source_col.as_any().downcast_ref::<Int64Array>().unwrap();
            let mut builder = Int64Builder::with_capacity(num_rows);
            for &idx in indices {
                if idx == null_sentinel {
                    builder.append_null();
                } else {
                    builder.append_value(source.value(idx as usize));
                }
            }
            Ok(Arc::new(builder.finish()))
        }
        DataType::Float64 => {
            let source = source_col.as_any().downcast_ref::<Float64Array>().unwrap();
            let mut builder = Float64Builder::with_capacity(num_rows);
            for &idx in indices {
                if idx == null_sentinel {
                    builder.append_null();
                } else {
                    builder.append_value(source.value(idx as usize));
                }
            }
            Ok(Arc::new(builder.finish()))
        }
        DataType::Utf8 => {
            let source = source_col.as_any().downcast_ref::<StringArray>().unwrap();
            let mut builder = StringBuilder::with_capacity(num_rows, num_rows * 10);
            for &idx in indices {
                if idx == null_sentinel {
                    builder.append_null();
                } else {
                    builder.append_value(source.value(idx as usize));
                }
            }
            Ok(Arc::new(builder.finish()))
        }
        DataType::Boolean => {
            let source = source_col.as_any().downcast_ref::<BooleanArray>().unwrap();
            let mut builder = BooleanBuilder::with_capacity(num_rows);
            for &idx in indices {
                if idx == null_sentinel {
                    builder.append_null();
                } else {
                    builder.append_value(source.value(idx as usize));
                }
            }
            Ok(Arc::new(builder.finish()))
        }
        _ => Err(DbxError::SqlExecution {
            message: format!("Unsupported type: {:?}", source_col.data_type()),
            context: "create_column_with_nulls".to_string(),
        }),
    }
}