uni-query 2.0.6

OpenCypher query parser, planner, and vectorized executor for Uni
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2024-2026 Dragonscale Team

//! Physical execution plans that dispatch graph reads against
//! plugin-registered `CatalogTable`s (M5 Batch 2 follow-up #6 — virtual
//! label-id allocation).
//!
//! When the planner encounters a `MATCH (n:External)` whose label is
//! not in the native schema, follow-up #5 consults registered
//! `CatalogProvider` / `ReplacementScanProvider`s for a claim. This
//! file implements the "what happens after the claim succeeds" leg:
//! a virtual `u16` label-id is allocated on `PluginRegistry`, the
//! claiming `CatalogTable` is stashed alongside, and at physical-plan
//! time `CatalogVertexScanExec` adapts that table's rows into the
//! graph-row schema convention every downstream operator expects
//! (`{var}._vid`, `{var}._labels`, `{var}.<prop>` columns).
//!
//! ## Adaptation contract
//!
//! - `_vid` (UInt64) is **synthesized** per row as
//!   `(virtual_label_id as u64) << 48 | row_offset`. The high-16-bit
//!   encoding makes virtual vids unambiguously distinguishable from
//!   native vids (sequentially allocated from 0, well below
//!   `0xFF00_0000_0000_0000`). Row offset increments across batches
//!   within a single `execute()` call via an `AtomicU64`.
//! - `_labels` is **synthesized** as a single-element `[label_name]`
//!   `List<Utf8>` per row.
//! - Property columns are projected from the catalog table's columns
//!   by name match (`prop` ↔ table column named `prop`), then renamed
//!   to the `{var}.{prop}` convention. Properties the table does not
//!   expose materialize as null columns of `Utf8` type (loose typing
//!   for now; tighten when the planner gains a property-type oracle
//!   for virtual labels).
//! - Reserved system column names (`_vid`, `_labels`, etc., and any
//!   name starting with `_`) on the catalog table are rejected at
//!   constructor time — they would collide with our synthesized
//!   columns and silently produce wrong results.
//!
//! ## Edges
//!
//! `CatalogEdgeScanExec` follows the same pattern but synthesizes
//! `_eid`, `_src_vid`, `_dst_vid` columns. The catalog table MUST
//! declare `src_id` and `dst_id` columns (Int64 or UInt64) so the
//! exec can populate `_src_vid`/`_dst_vid`. Without them the
//! constructor errors immediately.

use std::any::Any;
use std::collections::HashMap;
use std::fmt;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::task::{Context, Poll};

use arrow_array::builder::ListBuilder;
use arrow_array::{ArrayRef, RecordBatch, StringArray, UInt64Array};
use arrow_schema::{DataType, Field, Schema, SchemaRef};
use datafusion::common::Result as DFResult;
use datafusion::execution::{RecordBatchStream, SendableRecordBatchStream, TaskContext};
use datafusion::logical_expr::Expr as DfExpr;
use datafusion::physical_plan::metrics::{BaselineMetrics, ExecutionPlanMetricsSet, MetricsSet};
use datafusion::physical_plan::{DisplayAs, DisplayFormatType, ExecutionPlan, PlanProperties};
use futures::Stream;
use uni_plugin::traits::catalog::CatalogTable;

use crate::query::df_graph::common::{compute_plan_properties, labels_data_type};

/// Per-row virtual-vid base. Encodes the label id in the high 16 bits.
#[inline]
fn virtual_vid_base(virtual_label_id: u16) -> u64 {
    (virtual_label_id as u64) << 48
}

/// Verify the catalog table's schema has no reserved column names.
/// Returns the offending name on failure so the caller can surface it.
fn check_no_reserved_columns(schema: &SchemaRef) -> Result<(), String> {
    for field in schema.fields() {
        if field.name().starts_with('_') {
            return Err(field.name().clone());
        }
    }
    Ok(())
}

// ── Vertex scan ──────────────────────────────────────────────────────

/// Adapts a virtual-label `CatalogTable` into a graph-row-shaped
/// vertex scan. See module docs for the adaptation contract.
pub struct CatalogVertexScanExec {
    table: Arc<dyn CatalogTable>,
    virtual_label_id: u16,
    label_name: String,
    variable: String,
    /// Properties to project, in output order. Each must either match
    /// a catalog-table column name (case-sensitive) or be served as a
    /// nullable `Utf8` column of nulls.
    properties: Vec<String>,
    /// DataFusion filter expressions to pass to `table.scan(filters=)`.
    /// The catalog is free to ignore them; the planner re-applies the
    /// same predicates as a top-level `FilterExec` for safety.
    pushdown_filters: Vec<DfExpr>,
    /// Limit to pass to `table.scan(limit=)`; same "advisory" semantics.
    pushdown_limit: Option<usize>,
    /// Output schema with graph-row convention.
    schema: SchemaRef,
    properties_plan: Arc<PlanProperties>,
    metrics: ExecutionPlanMetricsSet,
}

impl fmt::Debug for CatalogVertexScanExec {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CatalogVertexScanExec")
            .field("label_name", &self.label_name)
            .field(
                "virtual_label_id",
                &format_args!("{:#x}", self.virtual_label_id),
            )
            .field("variable", &self.variable)
            .field("properties", &self.properties)
            .field("pushdown_filters", &self.pushdown_filters.len())
            .field("pushdown_limit", &self.pushdown_limit)
            .finish()
    }
}

impl CatalogVertexScanExec {
    /// Construct a new catalog-backed vertex scan.
    ///
    /// # Errors
    ///
    /// Returns an error if the catalog table's schema contains a column
    /// whose name starts with `_` (reserved for synthesized graph-row
    /// system columns).
    pub fn try_new(
        table: Arc<dyn CatalogTable>,
        virtual_label_id: u16,
        label_name: impl Into<String>,
        variable: impl Into<String>,
        properties: Vec<String>,
        pushdown_filters: Vec<DfExpr>,
        pushdown_limit: Option<usize>,
    ) -> anyhow::Result<Self> {
        let label_name = label_name.into();
        let variable = variable.into();
        let table_schema = table.schema();
        if let Err(bad) = check_no_reserved_columns(&table_schema) {
            return Err(anyhow::anyhow!(
                "CatalogTable for label `{label_name}` declares reserved column \
                 `{bad}` (names starting with `_` are synthesized by the graph-row \
                 adapter — rename it in the underlying table)"
            ));
        }
        let schema = Self::build_output_schema(&variable, &properties, &table_schema);
        let properties_plan = compute_plan_properties(schema.clone());
        Ok(Self {
            table,
            virtual_label_id,
            label_name,
            variable,
            properties,
            pushdown_filters,
            pushdown_limit,
            schema,
            properties_plan,
            metrics: ExecutionPlanMetricsSet::new(),
        })
    }

    fn build_output_schema(
        variable: &str,
        properties: &[String],
        table_schema: &SchemaRef,
    ) -> SchemaRef {
        let mut fields = vec![
            Field::new(format!("{variable}._vid"), DataType::UInt64, false),
            Field::new(format!("{variable}._labels"), labels_data_type(), false),
        ];
        let table_by_name: HashMap<&str, &Field> = table_schema
            .fields()
            .iter()
            .map(|f| (f.name().as_str(), f.as_ref()))
            .collect();
        for prop in properties {
            let col_name = format!("{variable}.{prop}");
            let (dtype, nullable) = match table_by_name.get(prop.as_str()) {
                Some(f) => (f.data_type().clone(), true),
                None => (DataType::Utf8, true),
            };
            fields.push(Field::new(&col_name, dtype, nullable));
        }
        Arc::new(Schema::new(fields))
    }
}

impl DisplayAs for CatalogVertexScanExec {
    fn fmt_as(&self, _t: DisplayFormatType, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "CatalogVertexScanExec: label={}, virtual_id={:#x}, variable={}, props={:?}",
            self.label_name, self.virtual_label_id, self.variable, self.properties
        )?;
        if !self.pushdown_filters.is_empty() {
            write!(f, ", filters={}", self.pushdown_filters.len())?;
        }
        if let Some(lim) = self.pushdown_limit {
            write!(f, ", limit={lim}")?;
        }
        Ok(())
    }
}

impl ExecutionPlan for CatalogVertexScanExec {
    fn name(&self) -> &str {
        "CatalogVertexScanExec"
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn schema(&self) -> SchemaRef {
        self.schema.clone()
    }

    fn properties(&self) -> &Arc<PlanProperties> {
        &self.properties_plan
    }

    fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
        vec![]
    }

    fn with_new_children(
        self: Arc<Self>,
        children: Vec<Arc<dyn ExecutionPlan>>,
    ) -> DFResult<Arc<dyn ExecutionPlan>> {
        if !children.is_empty() {
            return Err(datafusion::error::DataFusionError::Plan(
                "CatalogVertexScanExec has no children".into(),
            ));
        }
        Ok(self)
    }

    fn execute(
        &self,
        partition: usize,
        _context: Arc<TaskContext>,
    ) -> DFResult<SendableRecordBatchStream> {
        let metrics = BaselineMetrics::new(&self.metrics, partition);
        // Build projection: indices into the catalog table's schema for
        // every property name we actually want. Properties the table
        // doesn't expose are populated as null columns by the adapter
        // (no projection index).
        let table_schema = self.table.schema();
        let projection: Vec<usize> = self
            .properties
            .iter()
            .filter_map(|p| table_schema.index_of(p).ok())
            .collect();
        let projection_opt = if projection.is_empty() {
            None
        } else {
            Some(projection.as_slice())
        };
        let stream = self
            .table
            .scan(projection_opt, &self.pushdown_filters, self.pushdown_limit)
            .map_err(|e| {
                datafusion::error::DataFusionError::Execution(format!(
                    "CatalogTable::scan failed: {e}"
                ))
            })?;
        Ok(Box::pin(VertexAdapterStream {
            inner: stream,
            output_schema: self.schema.clone(),
            virtual_label_id: self.virtual_label_id,
            label_name: self.label_name.clone(),
            variable: self.variable.clone(),
            properties: self.properties.clone(),
            next_offset: AtomicU64::new(0),
            metrics,
        }))
    }

    fn metrics(&self) -> Option<MetricsSet> {
        Some(self.metrics.clone_inner())
    }
}

struct VertexAdapterStream {
    inner: SendableRecordBatchStream,
    output_schema: SchemaRef,
    virtual_label_id: u16,
    label_name: String,
    variable: String,
    properties: Vec<String>,
    next_offset: AtomicU64,
    metrics: BaselineMetrics,
}

impl RecordBatchStream for VertexAdapterStream {
    fn schema(&self) -> SchemaRef {
        self.output_schema.clone()
    }
}

impl Stream for VertexAdapterStream {
    type Item = DFResult<RecordBatch>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        match Pin::new(&mut self.inner).poll_next(cx) {
            Poll::Pending => Poll::Pending,
            Poll::Ready(None) => Poll::Ready(None),
            Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))),
            Poll::Ready(Some(Ok(batch))) => {
                let row_count = batch.num_rows();
                let base = virtual_vid_base(self.virtual_label_id)
                    | self
                        .next_offset
                        .fetch_add(row_count as u64, Ordering::SeqCst);
                let adapted = adapt_vertex_batch(
                    &batch,
                    &self.output_schema,
                    base,
                    &self.label_name,
                    &self.variable,
                    &self.properties,
                );
                self.metrics.record_output(row_count);
                Poll::Ready(Some(adapted))
            }
        }
    }
}

/// Build a graph-row-shaped batch from the catalog table's batch. The
/// `vid_start` is the value of `_vid` for the first row.
fn adapt_vertex_batch(
    in_batch: &RecordBatch,
    output_schema: &SchemaRef,
    vid_start: u64,
    label_name: &str,
    variable: &str,
    properties: &[String],
) -> DFResult<RecordBatch> {
    let n = in_batch.num_rows();
    let vid_array: ArrayRef = Arc::new(UInt64Array::from_iter_values(
        (0..n as u64).map(|i| vid_start + i),
    ));
    let labels_array: ArrayRef = {
        let mut b = ListBuilder::new(arrow_array::builder::StringBuilder::new());
        for _ in 0..n {
            b.values().append_value(label_name);
            b.append(true);
        }
        Arc::new(b.finish())
    };
    let in_schema = in_batch.schema();
    let in_by_name: HashMap<&str, ArrayRef> = in_schema
        .fields()
        .iter()
        .enumerate()
        .map(|(i, f)| (f.name().as_str(), in_batch.column(i).clone()))
        .collect();
    let _ = variable; // already embedded in output_schema field names
    let mut columns: Vec<ArrayRef> = Vec::with_capacity(output_schema.fields().len());
    columns.push(vid_array);
    columns.push(labels_array);
    for prop in properties {
        let col = in_by_name
            .get(prop.as_str())
            .cloned()
            .unwrap_or_else(|| Arc::new(StringArray::new_null(n)));
        columns.push(col);
    }
    RecordBatch::try_new(output_schema.clone(), columns).map_err(|e| {
        datafusion::error::DataFusionError::Execution(format!(
            "CatalogVertexScanExec: failed to assemble adapted batch: {e}"
        ))
    })
}

// ── Edge scan ────────────────────────────────────────────────────────

/// Adapts a virtual-edge-type `CatalogTable` into a graph-row-shaped
/// edge scan. The table MUST declare `src_id` and `dst_id` columns;
/// `_eid` is synthesized per row from the virtual edge-type id.
pub struct CatalogEdgeScanExec {
    table: Arc<dyn CatalogTable>,
    virtual_type_id: u32,
    type_name: String,
    variable: String,
    properties: Vec<String>,
    pushdown_filters: Vec<DfExpr>,
    pushdown_limit: Option<usize>,
    schema: SchemaRef,
    properties_plan: Arc<PlanProperties>,
    metrics: ExecutionPlanMetricsSet,
}

impl fmt::Debug for CatalogEdgeScanExec {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CatalogEdgeScanExec")
            .field("type_name", &self.type_name)
            .field(
                "virtual_type_id",
                &format_args!("{:#x}", self.virtual_type_id),
            )
            .field("variable", &self.variable)
            .field("properties", &self.properties)
            .finish()
    }
}

impl CatalogEdgeScanExec {
    /// # Errors
    /// Returns an error if the table's schema lacks `src_id`/`dst_id`,
    /// or if it declares a column whose name starts with `_`.
    pub fn try_new(
        table: Arc<dyn CatalogTable>,
        virtual_type_id: u32,
        type_name: impl Into<String>,
        variable: impl Into<String>,
        properties: Vec<String>,
        pushdown_filters: Vec<DfExpr>,
        pushdown_limit: Option<usize>,
    ) -> anyhow::Result<Self> {
        let type_name = type_name.into();
        let variable = variable.into();
        let table_schema = table.schema();
        if let Err(bad) = check_no_reserved_columns(&table_schema) {
            return Err(anyhow::anyhow!(
                "CatalogTable for edge type `{type_name}` declares reserved column \
                 `{bad}` (names starting with `_` are synthesized by the graph-row adapter)"
            ));
        }
        for required in ["src_id", "dst_id"] {
            if table_schema.index_of(required).is_err() {
                return Err(anyhow::anyhow!(
                    "CatalogTable for edge type `{type_name}` must declare a \
                     `{required}` column (mapped to `_{}_vid` in the graph-row \
                     adapter)",
                    if required == "src_id" { "src" } else { "dst" }
                ));
            }
        }
        let schema = Self::build_output_schema(&variable, &properties, &table_schema);
        let properties_plan = compute_plan_properties(schema.clone());
        Ok(Self {
            table,
            virtual_type_id,
            type_name,
            variable,
            properties,
            pushdown_filters,
            pushdown_limit,
            schema,
            properties_plan,
            metrics: ExecutionPlanMetricsSet::new(),
        })
    }

    fn build_output_schema(
        variable: &str,
        properties: &[String],
        table_schema: &SchemaRef,
    ) -> SchemaRef {
        let mut fields = vec![
            Field::new(format!("{variable}._eid"), DataType::UInt64, false),
            Field::new(format!("{variable}._src_vid"), DataType::UInt64, false),
            Field::new(format!("{variable}._dst_vid"), DataType::UInt64, false),
        ];
        let table_by_name: HashMap<&str, &Field> = table_schema
            .fields()
            .iter()
            .map(|f| (f.name().as_str(), f.as_ref()))
            .collect();
        for prop in properties {
            if prop == "src_id" || prop == "dst_id" {
                // These are surfaced via the synthesized `_src_vid` /
                // `_dst_vid` system columns; don't double-project.
                continue;
            }
            let col_name = format!("{variable}.{prop}");
            let (dtype, nullable) = match table_by_name.get(prop.as_str()) {
                Some(f) => (f.data_type().clone(), true),
                None => (DataType::Utf8, true),
            };
            fields.push(Field::new(&col_name, dtype, nullable));
        }
        Arc::new(Schema::new(fields))
    }
}

impl DisplayAs for CatalogEdgeScanExec {
    fn fmt_as(&self, _t: DisplayFormatType, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "CatalogEdgeScanExec: type={}, virtual_id={:#x}, variable={}, props={:?}",
            self.type_name, self.virtual_type_id, self.variable, self.properties
        )
    }
}

impl ExecutionPlan for CatalogEdgeScanExec {
    fn name(&self) -> &str {
        "CatalogEdgeScanExec"
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
    fn schema(&self) -> SchemaRef {
        self.schema.clone()
    }
    fn properties(&self) -> &Arc<PlanProperties> {
        &self.properties_plan
    }
    fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
        vec![]
    }
    fn with_new_children(
        self: Arc<Self>,
        children: Vec<Arc<dyn ExecutionPlan>>,
    ) -> DFResult<Arc<dyn ExecutionPlan>> {
        if !children.is_empty() {
            return Err(datafusion::error::DataFusionError::Plan(
                "CatalogEdgeScanExec has no children".into(),
            ));
        }
        Ok(self)
    }
    fn execute(
        &self,
        partition: usize,
        _context: Arc<TaskContext>,
    ) -> DFResult<SendableRecordBatchStream> {
        let metrics = BaselineMetrics::new(&self.metrics, partition);
        let table_schema = self.table.schema();
        // The projection must include src_id/dst_id (so the adapter
        // can populate _src_vid/_dst_vid) plus the requested
        // properties. Build the projection list deterministically.
        let mut wanted: Vec<&str> = vec!["src_id", "dst_id"];
        for p in &self.properties {
            if p != "src_id" && p != "dst_id" {
                wanted.push(p.as_str());
            }
        }
        let projection: Vec<usize> = wanted
            .iter()
            .filter_map(|p| table_schema.index_of(p).ok())
            .collect();
        let projection_opt = if projection.is_empty() {
            None
        } else {
            Some(projection.as_slice())
        };
        let stream = self
            .table
            .scan(projection_opt, &self.pushdown_filters, self.pushdown_limit)
            .map_err(|e| {
                datafusion::error::DataFusionError::Execution(format!(
                    "CatalogTable::scan failed: {e}"
                ))
            })?;
        Ok(Box::pin(EdgeAdapterStream {
            inner: stream,
            output_schema: self.schema.clone(),
            virtual_type_id: self.virtual_type_id,
            variable: self.variable.clone(),
            properties: self.properties.clone(),
            next_offset: AtomicU64::new(0),
            metrics,
        }))
    }
    fn metrics(&self) -> Option<MetricsSet> {
        Some(self.metrics.clone_inner())
    }
}

struct EdgeAdapterStream {
    inner: SendableRecordBatchStream,
    output_schema: SchemaRef,
    virtual_type_id: u32,
    variable: String,
    properties: Vec<String>,
    next_offset: AtomicU64,
    metrics: BaselineMetrics,
}

impl RecordBatchStream for EdgeAdapterStream {
    fn schema(&self) -> SchemaRef {
        self.output_schema.clone()
    }
}

impl Stream for EdgeAdapterStream {
    type Item = DFResult<RecordBatch>;
    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        match Pin::new(&mut self.inner).poll_next(cx) {
            Poll::Pending => Poll::Pending,
            Poll::Ready(None) => Poll::Ready(None),
            Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))),
            Poll::Ready(Some(Ok(batch))) => {
                let row_count = batch.num_rows();
                let base = ((self.virtual_type_id as u64) << 32)
                    | self
                        .next_offset
                        .fetch_add(row_count as u64, Ordering::SeqCst);
                let adapted = adapt_edge_batch(
                    &batch,
                    &self.output_schema,
                    base,
                    &self.variable,
                    &self.properties,
                );
                self.metrics.record_output(row_count);
                Poll::Ready(Some(adapted))
            }
        }
    }
}

fn adapt_edge_batch(
    in_batch: &RecordBatch,
    output_schema: &SchemaRef,
    eid_start: u64,
    variable: &str,
    properties: &[String],
) -> DFResult<RecordBatch> {
    use arrow_array::cast::AsArray;
    use arrow_array::types::Int64Type;
    let n = in_batch.num_rows();
    let eid: ArrayRef = Arc::new(UInt64Array::from_iter_values(
        (0..n as u64).map(|i| eid_start + i),
    ));
    let in_schema = in_batch.schema();
    let in_by_name: HashMap<&str, ArrayRef> = in_schema
        .fields()
        .iter()
        .enumerate()
        .map(|(i, f)| (f.name().as_str(), in_batch.column(i).clone()))
        .collect();
    let to_u64 = |arr: &ArrayRef| -> DFResult<ArrayRef> {
        match arr.data_type() {
            DataType::UInt64 => Ok(arr.clone()),
            DataType::Int64 => {
                let a = arr.as_primitive::<Int64Type>();
                Ok(Arc::new(UInt64Array::from_iter_values(
                    (0..a.len()).map(|i| a.value(i) as u64),
                )))
            }
            DataType::UInt32 => {
                let a = arr.as_primitive::<arrow_array::types::UInt32Type>();
                Ok(Arc::new(UInt64Array::from_iter_values(
                    (0..a.len()).map(|i| u64::from(a.value(i))),
                )))
            }
            other => Err(datafusion::error::DataFusionError::Execution(format!(
                "CatalogEdgeScanExec: src_id/dst_id must be Int64/UInt64/UInt32, got {other:?}"
            ))),
        }
    };
    let src_arr = in_by_name.get("src_id").ok_or_else(|| {
        datafusion::error::DataFusionError::Execution("missing src_id column".into())
    })?;
    let dst_arr = in_by_name.get("dst_id").ok_or_else(|| {
        datafusion::error::DataFusionError::Execution("missing dst_id column".into())
    })?;
    let src_vid = to_u64(src_arr)?;
    let dst_vid = to_u64(dst_arr)?;

    let _ = variable;
    let mut columns: Vec<ArrayRef> = Vec::with_capacity(output_schema.fields().len());
    columns.push(eid);
    columns.push(src_vid);
    columns.push(dst_vid);
    for prop in properties {
        if prop == "src_id" || prop == "dst_id" {
            continue;
        }
        let col = in_by_name
            .get(prop.as_str())
            .cloned()
            .unwrap_or_else(|| Arc::new(StringArray::new_null(n)));
        columns.push(col);
    }
    RecordBatch::try_new(output_schema.clone(), columns).map_err(|e| {
        datafusion::error::DataFusionError::Execution(format!(
            "CatalogEdgeScanExec: failed to assemble adapted batch: {e}"
        ))
    })
}