use std::sync::Arc;
use arrow::array::{Array, ArrayRef, RecordBatch, UInt32Array, new_empty_array};
use arrow::compute::{TakeOptions, concat, concat_batches, take};
use arrow::datatypes::SchemaRef;
use datafusion_common::{Result, exec_datafusion_err, exec_err};
#[derive(Debug, Clone)]
pub(crate) struct LogicalBatch {
schema: SchemaRef,
segments: Vec<RecordBatch>,
offsets: Vec<usize>,
}
impl LogicalBatch {
pub(crate) fn new(schema: SchemaRef, batches: Vec<RecordBatch>) -> Result<Self> {
if batches.iter().any(|batch| batch.schema() != schema) {
return exec_err!("LogicalBatch input batches must have the same schema");
}
let segments: Vec<RecordBatch> = batches
.into_iter()
.filter(|batch| batch.num_rows() > 0)
.collect();
let mut offsets = Vec::with_capacity(segments.len() + 1);
let mut num_rows: usize = 0;
offsets.push(num_rows);
for segment in &segments {
num_rows = num_rows.checked_add(segment.num_rows()).ok_or_else(|| {
exec_datafusion_err!("LogicalBatch total row count exceeds usize::MAX")
})?;
offsets.push(num_rows);
}
Ok(Self {
schema,
segments,
offsets,
})
}
pub(crate) fn new_empty(schema: SchemaRef) -> Self {
Self {
schema,
segments: vec![],
offsets: vec![0],
}
}
pub(crate) fn schema(&self) -> SchemaRef {
Arc::clone(&self.schema)
}
pub(crate) fn num_rows(&self) -> usize {
self.offsets[self.offsets.len() - 1]
}
pub(crate) fn into_batches(self) -> Vec<RecordBatch> {
self.segments
}
pub(crate) fn row(&self, index: usize) -> Result<BatchRow<'_>> {
if index >= self.num_rows() {
return exec_err!(
"row index {index} out of bounds for a batch of {} rows",
self.num_rows()
);
}
let segment = self.segment_of(index, 0);
Ok(BatchRow {
batch: &self.segments[segment],
index: index - self.offsets[segment],
})
}
pub(crate) fn row_indices(
&self,
rows: impl IntoIterator<Item = usize>,
) -> Result<RowIndices> {
let rows = rows.into_iter();
let mut groups = Vec::new();
let mut indices = Vec::with_capacity(rows.size_hint().0);
let mut segment = 0;
for row in rows {
if row >= self.num_rows() {
return exec_err!(
"row index {row} out of bounds for a batch of {} rows",
self.num_rows()
);
}
let next_segment = self.segment_of(row, segment);
if next_segment != segment && !indices.is_empty() {
groups.push(Single {
segment,
indices: UInt32Array::from(std::mem::take(&mut indices)),
});
}
segment = next_segment;
let index = u32::try_from(row - self.offsets[segment]).map_err(|_| {
exec_datafusion_err!(
"row index {row} within segment {segment} exceeds u32::MAX"
)
})?;
indices.push(index);
}
if !indices.is_empty() {
groups.push(Single {
segment,
indices: UInt32Array::from(indices),
});
}
Ok(RowIndices(match groups.len() {
0 => IndicesVariant::Empty,
1 => IndicesVariant::Single(groups.pop().unwrap()),
_ => IndicesVariant::Multi(groups),
}))
}
pub(crate) fn take_column(
&self,
column: usize,
indices: &RowIndices,
) -> Result<ArrayRef> {
let field = self.schema.fields().get(column).ok_or_else(|| {
exec_datafusion_err!(
"column index {column} out of bounds for a batch of {} columns",
self.schema.fields().len()
)
})?;
let take_single = |single: &Single| -> Result<ArrayRef> {
let batch = self.segments.get(single.segment).ok_or_else(|| {
exec_datafusion_err!(
"segment index {} out of bounds for a batch of {} segments",
single.segment,
self.segments.len()
)
})?;
let values = batch.columns().get(column).ok_or_else(|| {
exec_datafusion_err!(
"column index {column} out of bounds for a batch of {} columns",
batch.num_columns()
)
})?;
take(
values.as_ref(),
&single.indices,
Some(TakeOptions { check_bounds: true }),
)
.map_err(|error| exec_datafusion_err!("Failed to gather rows: {error}"))
};
Ok(match &indices.0 {
IndicesVariant::Empty => new_empty_array(field.data_type()),
IndicesVariant::Single(single) => take_single(single)?,
IndicesVariant::Multi(groups) => {
let arrays = groups
.iter()
.map(take_single)
.collect::<Result<Vec<_>, _>>()?;
let arrays: Vec<&dyn Array> =
arrays.iter().map(|array| array.as_ref()).collect();
concat(&arrays)?
}
})
}
pub(crate) fn slice(&self, offset: usize, length: usize) -> Result<RecordBatch> {
let end = offset
.checked_add(length)
.filter(|&end| end <= self.num_rows())
.ok_or_else(|| {
exec_datafusion_err!(
"slice with offset {offset} and length {length} out of bounds for a batch of {} rows",
self.num_rows()
)
})?;
if length == 0 {
return Ok(RecordBatch::new_empty(self.schema()));
}
let first = self.segment_of(offset, 0);
let last = self.segment_of(end - 1, first);
let head = &self.segments[first];
let head_offset = offset - self.offsets[first];
if first == last {
return Ok(head.slice(head_offset, length));
}
let head = head.slice(head_offset, head.num_rows() - head_offset);
let tail = self.segments[last].slice(0, end - self.offsets[last]);
let pieces = std::iter::once(&head)
.chain(&self.segments[first + 1..last])
.chain(std::iter::once(&tail));
Ok(concat_batches(&self.schema, pieces)?)
}
fn segment_of(&self, row: usize, hint: usize) -> usize {
debug_assert!(row < self.num_rows());
if hint + 1 < self.offsets.len()
&& (self.offsets[hint]..self.offsets[hint + 1]).contains(&row)
{
return hint;
}
self.offsets.partition_point(|&start| start <= row) - 1
}
}
impl From<RecordBatch> for LogicalBatch {
fn from(batch: RecordBatch) -> Self {
let num_rows = batch.num_rows();
if num_rows == 0 {
return Self::new_empty(batch.schema());
}
Self {
schema: batch.schema(),
segments: vec![batch],
offsets: vec![0, num_rows],
}
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct BatchRow<'a> {
batch: &'a RecordBatch,
index: usize,
}
impl<'a> BatchRow<'a> {
pub(crate) fn new(batch: &'a RecordBatch, index: usize) -> Result<Self> {
if index >= batch.num_rows() {
return exec_err!(
"row index {index} out of bounds for a batch of {} rows",
batch.num_rows()
);
}
Ok(Self { batch, index })
}
pub(crate) fn column(&self, column: usize) -> Result<&'a ArrayRef> {
self.batch.columns().get(column).ok_or_else(|| {
exec_datafusion_err!(
"column index {column} out of bounds for a batch of {} columns",
self.batch.num_columns()
)
})
}
pub(crate) fn index(&self) -> usize {
self.index
}
}
#[derive(Debug)]
pub(crate) struct RowIndices(IndicesVariant);
impl RowIndices {
pub(crate) fn len(&self) -> usize {
match &self.0 {
IndicesVariant::Empty => 0,
IndicesVariant::Single(single) => single.indices.len(),
IndicesVariant::Multi(groups) => {
groups.iter().map(|single| single.indices.len()).sum()
}
}
}
}
#[derive(Debug)]
enum IndicesVariant {
Empty,
Single(Single),
Multi(Vec<Single>),
}
#[derive(Debug)]
struct Single {
segment: usize,
indices: UInt32Array,
}
#[cfg(test)]
mod tests {
use super::*;
use std::ops::Range;
use arrow::array::{AsArray, Int32Array, StringArray};
use arrow::datatypes::{DataType, Field, Int32Type, Schema};
use datafusion_common::DataFusionError;
fn schema() -> SchemaRef {
Arc::new(Schema::new(vec![
Field::new("a", DataType::Int32, false),
Field::new("b", DataType::Utf8, true),
]))
}
fn batch(rows: Range<i32>) -> RecordBatch {
let a = Int32Array::from_iter_values(rows.clone());
let b =
StringArray::from_iter(rows.map(|i| (i % 2 == 0).then(|| format!("s{i}"))));
RecordBatch::try_new(schema(), vec![Arc::new(a), Arc::new(b)]).unwrap()
}
fn logical_batch() -> (LogicalBatch, RecordBatch) {
let segments = vec![
RecordBatch::new_empty(schema()),
batch(0..3),
batch(3..5),
RecordBatch::new_empty(schema()),
batch(5..9),
RecordBatch::new_empty(schema()),
];
let expected = concat_batches(&schema(), &segments).unwrap();
(LogicalBatch::new(schema(), segments).unwrap(), expected)
}
#[test]
fn construction() {
let (logical_batch, expected) = logical_batch();
assert_eq!(logical_batch.num_rows(), 9);
assert_eq!(logical_batch.offsets, vec![0, 3, 5, 9]);
assert_eq!(logical_batch.schema(), schema());
let segments = logical_batch.into_batches();
assert_eq!(segments.len(), 3);
assert_eq!(concat_batches(&schema(), &segments).unwrap(), expected);
let empty = LogicalBatch::new_empty(schema());
assert_eq!(empty.num_rows(), 0);
assert_eq!(empty.slice(0, 0).unwrap(), RecordBatch::new_empty(schema()));
assert_eq!(
empty
.take_column(0, &empty.row_indices([]).unwrap())
.unwrap()
.len(),
0
);
let single = LogicalBatch::from(batch(0..4));
assert_eq!(single.num_rows(), 4);
assert_eq!(single.slice(0, 4).unwrap(), batch(0..4));
}
#[test]
fn row_lookup() {
let (logical_batch, _) = logical_batch();
for i in 0..9 {
let row = logical_batch.row(i).unwrap();
let a = row.column(0).unwrap().as_primitive::<Int32Type>();
assert_eq!(a.value(row.index()), i as i32, "row {i}");
}
assert_eq!(logical_batch.row(0).unwrap().index(), 0);
assert_eq!(logical_batch.row(2).unwrap().index(), 2);
assert_eq!(logical_batch.row(3).unwrap().index(), 0);
assert_eq!(logical_batch.row(4).unwrap().index(), 1);
assert_eq!(logical_batch.row(5).unwrap().index(), 0);
assert_eq!(logical_batch.row(8).unwrap().index(), 3);
}
#[test]
fn row_lookup_out_of_bounds() {
let (logical_batch, _) = logical_batch();
for index in [9, usize::MAX] {
assert!(matches!(
logical_batch.row(index),
Err(DataFusionError::Execution(_))
));
}
assert!(matches!(
LogicalBatch::new_empty(schema()).row(0),
Err(DataFusionError::Execution(_))
));
}
#[test]
fn row_indices_out_of_bounds() {
let (logical_batch, _) = logical_batch();
for rows in [[0, 9], [3, usize::MAX]] {
assert!(matches!(
logical_batch.row_indices(rows),
Err(DataFusionError::Execution(_))
));
}
assert!(matches!(
LogicalBatch::new_empty(schema()).row_indices([0]),
Err(DataFusionError::Execution(_))
));
}
#[test]
fn batch_row_out_of_bounds() {
let batch = batch(0..3);
for index in [3, usize::MAX] {
assert!(matches!(
BatchRow::new(&batch, index),
Err(DataFusionError::Execution(_))
));
}
let row = BatchRow::new(&batch, 2).unwrap();
assert_eq!(row.index(), 2);
assert_eq!(row.column(0).unwrap(), batch.column(0));
assert!(matches!(row.column(2), Err(DataFusionError::Execution(_))));
assert!(matches!(
BatchRow::new(&RecordBatch::new_empty(schema()), 0),
Err(DataFusionError::Execution(_))
));
}
fn assert_take(logical_batch: &LogicalBatch, expected: &RecordBatch, rows: &[usize]) {
let indices = logical_batch.row_indices(rows.iter().copied()).unwrap();
assert_eq!(indices.len(), rows.len());
let plain_indices = UInt32Array::from_iter_values(rows.iter().map(|i| *i as u32));
for column in 0..2 {
let actual = logical_batch.take_column(column, &indices).unwrap();
let expected = take(expected.column(column), &plain_indices, None).unwrap();
assert_eq!(&actual, &expected, "column {column}, rows {rows:?}");
}
}
#[test]
fn take_within_one_segment() {
let (logical_batch, expected) = logical_batch();
let indices = logical_batch.row_indices([3, 4, 4, 3]).unwrap();
assert!(matches!(
indices.0,
IndicesVariant::Single(Single { segment: 1, .. })
));
assert_take(&logical_batch, &expected, &[3, 4, 4, 3]);
assert_take(&logical_batch, &expected, &[8, 5, 6, 7]);
assert_take(&logical_batch, &expected, &[0, 0, 0]);
}
#[test]
fn take_across_segments() {
let (logical_batch, expected) = logical_batch();
let indices = logical_batch.row_indices([2, 3, 2, 3]).unwrap();
assert!(matches!(indices.0, IndicesVariant::Multi(_)));
assert_take(&logical_batch, &expected, &[2, 3, 2, 3]);
assert_take(&logical_batch, &expected, &[8, 0, 4, 5]);
assert_take(&logical_batch, &expected, &(0..9).collect::<Vec<_>>());
assert_take(&logical_batch, &expected, &[3, 4, 4, 7, 5, 3]);
assert_take(&logical_batch, &expected, &[0, 8]);
}
#[test]
fn take_nothing() {
let (logical_batch, _) = logical_batch();
let indices = logical_batch.row_indices([]).unwrap();
assert_eq!(indices.len(), 0);
let actual = logical_batch.take_column(1, &indices).unwrap();
assert_eq!(actual.len(), 0);
assert_eq!(actual.data_type(), &DataType::Utf8);
}
#[test]
fn take_out_of_bounds() {
let (logical_batch, _) = logical_batch();
for rows in [vec![], vec![0], vec![0, 3]] {
let indices = logical_batch.row_indices(rows).unwrap();
assert!(matches!(
logical_batch.take_column(2, &indices),
Err(DataFusionError::Execution(_))
));
}
let smaller = LogicalBatch::from(batch(0..1));
for row in [2, 8] {
let indices = logical_batch.row_indices([row]).unwrap();
assert!(matches!(
smaller.take_column(0, &indices),
Err(DataFusionError::Execution(_))
));
}
}
#[test]
fn slice_within_one_segment() {
let (logical_batch, expected) = logical_batch();
for (offset, length) in [
(0, 3),
(1, 2),
(3, 2),
(5, 4),
(6, 1),
(8, 1),
(4, 0),
(9, 0),
] {
let actual = logical_batch.slice(offset, length).unwrap();
assert_eq!(
actual,
expected.slice(offset, length),
"{offset}..{}",
offset + length
);
}
}
#[test]
fn slice_across_segments() {
let (logical_batch, expected) = logical_batch();
for (offset, length) in [(0, 9), (2, 2), (2, 4), (1, 7), (4, 5), (3, 6)] {
let actual = logical_batch.slice(offset, length).unwrap();
assert_eq!(
actual,
expected.slice(offset, length),
"{offset}..{}",
offset + length
);
}
}
#[test]
fn slice_out_of_bounds() {
let (logical_batch, _) = logical_batch();
for (offset, length) in [
(7, 3),
(10, 0),
(usize::MAX, 1),
(1, usize::MAX),
(usize::MAX, usize::MAX),
] {
assert!(matches!(
logical_batch.slice(offset, length),
Err(DataFusionError::Execution(_))
));
}
}
#[test]
fn construction_errors() {
let empty_schema = Arc::new(Schema::empty());
assert!(matches!(
LogicalBatch::new(Arc::clone(&empty_schema), vec![batch(0..1)]),
Err(DataFusionError::Execution(_))
));
let batch = |rows| {
RecordBatch::try_new_with_options(
Arc::clone(&empty_schema),
vec![],
&arrow::array::RecordBatchOptions::new().with_row_count(Some(rows)),
)
.unwrap()
};
assert!(matches!(
LogicalBatch::new(
Arc::clone(&empty_schema),
vec![batch(usize::MAX), batch(1)]
),
Err(DataFusionError::Execution(_))
));
if let Some(index) = (u32::MAX as usize).checked_add(1) {
let large = LogicalBatch::from(batch(usize::MAX));
assert!(matches!(
large.row_indices([index]),
Err(DataFusionError::Execution(_))
));
}
}
#[test]
fn empty_schema_counts_rows() {
let schema = Arc::new(Schema::empty());
let batch = |rows| {
RecordBatch::try_new_with_options(
Arc::clone(&schema),
vec![],
&arrow::array::RecordBatchOptions::new().with_row_count(Some(rows)),
)
.unwrap()
};
let logical_batch =
LogicalBatch::new(Arc::clone(&schema), vec![batch(2), batch(3)]).unwrap();
assert_eq!(logical_batch.num_rows(), 5);
assert_eq!(logical_batch.row(4).unwrap().index(), 2);
assert_eq!(logical_batch.slice(1, 3).unwrap().num_rows(), 3);
assert_eq!(logical_batch.slice(3, 2).unwrap().num_rows(), 2);
}
}