use std::{collections::VecDeque, fmt::Debug, iter, ops::Range, sync::Arc, vec};
use arrow::array::AsArray;
use arrow_array::{make_array, types::UInt64Type, Array, ArrayRef, PrimitiveArray};
use arrow_buffer::{bit_util, BooleanBuffer, NullBuffer};
use arrow_schema::{DataType, Field as ArrowField};
use futures::{future::BoxFuture, stream::FuturesUnordered, FutureExt, TryStreamExt};
use lance_arrow::deepcopy::deep_copy_array;
use lance_core::utils::bit::pad_bytes;
use log::{debug, trace};
use snafu::{location, Location};
use crate::data::{AllNullDataBlock, DataBlock, VariableWidthBlock};
use crate::decoder::PerValueDecompressor;
use crate::encoder::PerValueDataBlock;
use crate::repdef::{build_control_word_iterator, ControlWordIterator, ControlWordParser};
use crate::statistics::{GetStat, Stat};
use lance_core::{datatypes::Field, utils::tokio::spawn_cpu, Result};
use crate::{
buffer::LanceBuffer,
data::{BlockInfo, DataBlockBuilder, FixedWidthDataBlock, UsedEncoding},
decoder::{
BlockDecompressor, ColumnInfo, DecodeArrayTask, DecodePageTask, DecodedArray, DecodedPage,
DecompressorStrategy, FieldScheduler, FilterExpression, LoadedPage, LogicalPageDecoder,
MessageType, MiniBlockDecompressor, NextDecodeTask, PageEncoding, PageInfo, PageScheduler,
PrimitivePageDecoder, PriorityRange, ScheduledScanLine, SchedulerContext, SchedulingJob,
StructuralDecodeArrayTask, StructuralFieldDecoder, StructuralFieldScheduler,
StructuralPageDecoder, StructuralSchedulingJob, UnloadedPage,
},
encoder::{
ArrayEncodingStrategy, CompressionStrategy, EncodeTask, EncodedColumn, EncodedPage,
EncodingOptions, FieldEncoder, MiniBlockChunk, MiniBlockCompressed, OutOfLineBuffers,
},
encodings::physical::{decoder_from_array_encoding, ColumnBuffers, PageBuffers},
format::{pb, ProtobufUtils},
repdef::{LevelBuffer, RepDefBuilder, RepDefUnraveler},
EncodingsIo,
};
#[derive(Debug)]
struct PrimitivePage {
scheduler: Box<dyn PageScheduler>,
num_rows: u64,
page_index: u32,
}
#[derive(Debug)]
pub struct PrimitiveFieldScheduler {
data_type: DataType,
page_schedulers: Vec<PrimitivePage>,
num_rows: u64,
should_validate: bool,
column_index: u32,
}
impl PrimitiveFieldScheduler {
pub fn new(
column_index: u32,
data_type: DataType,
pages: Arc<[PageInfo]>,
buffers: ColumnBuffers,
should_validate: bool,
) -> Self {
let page_schedulers = pages
.iter()
.enumerate()
.filter(|(page_index, page)| {
log::trace!("Skipping empty page with index {}", page_index);
page.num_rows > 0
})
.map(|(page_index, page)| {
let page_buffers = PageBuffers {
column_buffers: buffers,
positions_and_sizes: &page.buffer_offsets_and_sizes,
};
let scheduler = decoder_from_array_encoding(
page.encoding.as_legacy(),
&page_buffers,
&data_type,
);
PrimitivePage {
scheduler,
num_rows: page.num_rows,
page_index: page_index as u32,
}
})
.collect::<Vec<_>>();
let num_rows = page_schedulers.iter().map(|p| p.num_rows).sum();
Self {
data_type,
page_schedulers,
num_rows,
should_validate,
column_index,
}
}
}
#[derive(Debug)]
struct PrimitiveFieldSchedulingJob<'a> {
scheduler: &'a PrimitiveFieldScheduler,
ranges: Vec<Range<u64>>,
page_idx: usize,
range_idx: usize,
range_offset: u64,
global_row_offset: u64,
}
impl<'a> PrimitiveFieldSchedulingJob<'a> {
pub fn new(scheduler: &'a PrimitiveFieldScheduler, ranges: Vec<Range<u64>>) -> Self {
Self {
scheduler,
ranges,
page_idx: 0,
range_idx: 0,
range_offset: 0,
global_row_offset: 0,
}
}
}
impl<'a> SchedulingJob for PrimitiveFieldSchedulingJob<'a> {
fn schedule_next(
&mut self,
context: &mut SchedulerContext,
priority: &dyn PriorityRange,
) -> Result<ScheduledScanLine> {
debug_assert!(self.range_idx < self.ranges.len());
let mut range = self.ranges[self.range_idx].clone();
range.start += self.range_offset;
let mut cur_page = &self.scheduler.page_schedulers[self.page_idx];
trace!(
"Current range is {:?} and current page has {} rows",
range,
cur_page.num_rows
);
while cur_page.num_rows + self.global_row_offset <= range.start {
self.global_row_offset += cur_page.num_rows;
self.page_idx += 1;
trace!("Skipping entire page of {} rows", cur_page.num_rows);
cur_page = &self.scheduler.page_schedulers[self.page_idx];
}
let mut ranges_in_page = Vec::new();
while cur_page.num_rows + self.global_row_offset > range.start {
range.start = range.start.max(self.global_row_offset);
let start_in_page = range.start - self.global_row_offset;
let end_in_page = start_in_page + (range.end - range.start);
let end_in_page = end_in_page.min(cur_page.num_rows);
let last_in_range = (end_in_page + self.global_row_offset) >= range.end;
ranges_in_page.push(start_in_page..end_in_page);
if last_in_range {
self.range_idx += 1;
if self.range_idx == self.ranges.len() {
break;
}
range = self.ranges[self.range_idx].clone();
} else {
break;
}
}
let num_rows_in_next = ranges_in_page.iter().map(|r| r.end - r.start).sum();
trace!(
"Scheduling {} rows across {} ranges from page with {} rows (priority={}, column_index={}, page_index={})",
num_rows_in_next,
ranges_in_page.len(),
cur_page.num_rows,
priority.current_priority(),
self.scheduler.column_index,
cur_page.page_index,
);
self.global_row_offset += cur_page.num_rows;
self.page_idx += 1;
let physical_decoder = cur_page.scheduler.schedule_ranges(
&ranges_in_page,
context.io(),
priority.current_priority(),
);
let logical_decoder = PrimitiveFieldDecoder {
data_type: self.scheduler.data_type.clone(),
column_index: self.scheduler.column_index,
unloaded_physical_decoder: Some(physical_decoder),
physical_decoder: None,
rows_drained: 0,
num_rows: num_rows_in_next,
should_validate: self.scheduler.should_validate,
page_index: cur_page.page_index,
};
let decoder = Box::new(logical_decoder);
let decoder_ready = context.locate_decoder(decoder);
Ok(ScheduledScanLine {
decoders: vec![MessageType::DecoderReady(decoder_ready)],
rows_scheduled: num_rows_in_next,
})
}
fn num_rows(&self) -> u64 {
self.ranges.iter().map(|r| r.end - r.start).sum()
}
}
impl FieldScheduler for PrimitiveFieldScheduler {
fn num_rows(&self) -> u64 {
self.num_rows
}
fn schedule_ranges<'a>(
&'a self,
ranges: &[std::ops::Range<u64>],
_filter: &FilterExpression,
) -> Result<Box<dyn SchedulingJob + 'a>> {
Ok(Box::new(PrimitiveFieldSchedulingJob::new(
self,
ranges.to_vec(),
)))
}
fn initialize<'a>(
&'a self,
_filter: &'a FilterExpression,
_context: &'a SchedulerContext,
) -> BoxFuture<'a, Result<()>> {
std::future::ready(Ok(())).boxed()
}
}
trait StructuralPageScheduler: std::fmt::Debug + Send {
fn initialize<'a>(&'a mut self, io: &Arc<dyn EncodingsIo>) -> BoxFuture<'a, Result<()>>;
fn schedule_ranges(
&self,
ranges: &[Range<u64>],
io: &dyn EncodingsIo,
) -> Result<BoxFuture<'static, Result<Box<dyn StructuralPageDecoder>>>>;
}
#[derive(Debug)]
struct ChunkMeta {
num_values: u64,
chunk_size_bytes: u64,
}
#[derive(Debug)]
struct DecodeMiniBlockTask {
rep_decompressor: Arc<dyn BlockDecompressor>,
def_decompressor: Arc<dyn BlockDecompressor>,
value_decompressor: Arc<dyn MiniBlockDecompressor>,
chunks: Vec<ScheduledChunk>,
offset_into_first_chunk: u64,
num_rows: u64,
}
impl DecodeMiniBlockTask {
fn decode_levels(
rep_decompressor: &dyn BlockDecompressor,
levels: LanceBuffer,
) -> Result<Option<impl AsRef<[u16]>>> {
let rep = rep_decompressor.decompress(levels)?;
match rep {
DataBlock::FixedWidth(mut rep) => Ok(Some(rep.data.borrow_to_typed_slice::<u16>())),
DataBlock::Constant(constant) => {
assert_eq!(constant.data.len(), 2);
if constant.data[0] == 0 && constant.data[1] == 0 {
Ok(None)
} else {
todo!()
}
}
_ => unreachable!(),
}
}
fn extend_levels(
offset: usize,
range: Range<u64>,
levels: &mut Option<LevelBuffer>,
level_buf: &Option<impl AsRef<[u16]>>,
dest_offset: usize,
) {
if let Some(level_buf) = level_buf {
if levels.is_none() {
let mut new_levels_vec =
LevelBuffer::with_capacity(offset + (range.end - range.start) as usize);
new_levels_vec.extend(iter::repeat(0).take(dest_offset));
*levels = Some(new_levels_vec);
}
levels.as_mut().unwrap().extend(
level_buf.as_ref()[range.start as usize..range.end as usize]
.iter()
.copied(),
);
} else if let Some(levels) = levels {
let num_values = (range.end - range.start) as usize;
levels.extend(iter::repeat(0).take(num_values));
}
}
}
impl DecodePageTask for DecodeMiniBlockTask {
fn decode(self: Box<Self>) -> Result<DecodedPage> {
let mut repbuf: Option<LevelBuffer> = None;
let mut defbuf: Option<LevelBuffer> = None;
let rep_decompressor = self.rep_decompressor;
let def_decompressor = self.def_decompressor;
let mut remaining = self.num_rows;
let estimated_size_bytes = self
.chunks
.iter()
.map(|chunk| chunk.data.len())
.sum::<usize>()
* 2;
let mut data_builder =
DataBlockBuilder::with_capacity_estimate(estimated_size_bytes as u64);
let mut to_skip = self.offset_into_first_chunk;
let mut level_offset = 0;
for chunk in self.chunks.into_iter() {
let buf = chunk.data.into_buffer();
let bytes_rep = u16::from_le_bytes([buf[0], buf[1]]) as usize;
let bytes_def = u16::from_le_bytes([buf[2], buf[3]]) as usize;
let bytes_val = u16::from_le_bytes([buf[4], buf[5]]) as usize;
debug_assert!(buf.len() >= bytes_rep + bytes_def + bytes_val + 6);
debug_assert!(
buf.len()
<= bytes_rep
+ bytes_def
+ bytes_val
+ 6
+ 1 + (2 * MINIBLOCK_MAX_PADDING) );
let p1 = bytes_rep % 2;
let rep = buf.slice_with_length(6, bytes_rep);
let def = buf.slice_with_length(6 + bytes_rep + p1, bytes_def);
let p2 = pad_bytes::<MINIBLOCK_ALIGNMENT>(6 + bytes_rep + p1 + bytes_def);
let values = buf.slice_with_length(6 + bytes_rep + bytes_def + p2, bytes_val);
let mut values = self
.value_decompressor
.decompress(LanceBuffer::Borrowed(values), chunk.vals_in_chunk)?;
let rep = Self::decode_levels(rep_decompressor.as_ref(), LanceBuffer::Borrowed(rep))?;
let def = Self::decode_levels(def_decompressor.as_ref(), LanceBuffer::Borrowed(def))?;
let mut offset = to_skip;
for range in chunk.ranges {
if to_skip > range.end - range.start {
to_skip -= range.end - range.start;
continue;
}
let range = range.start + to_skip..range.end;
to_skip = 0;
let range_len = range.end - range.start;
let to_take = range_len.min(remaining);
let range = range.start..range.start + to_take;
Self::extend_levels(
offset as usize,
range.clone(),
&mut repbuf,
&rep,
level_offset,
);
Self::extend_levels(
offset as usize,
range.clone(),
&mut defbuf,
&def,
level_offset,
);
data_builder.append(&mut values, range);
remaining -= to_take;
offset += to_take;
level_offset += to_take as usize;
}
}
debug_assert_eq!(remaining, 0);
let data = data_builder.finish();
Ok(DecodedPage {
data,
repetition: repbuf,
definition: defbuf,
})
}
}
#[derive(Debug)]
struct MiniBlockDecoder {
rep_decompressor: Arc<dyn BlockDecompressor>,
def_decompressor: Arc<dyn BlockDecompressor>,
value_decompressor: Arc<dyn MiniBlockDecompressor>,
data: VecDeque<ScheduledChunk>,
offset_in_current_chunk: u64,
num_rows: u64,
}
impl StructuralPageDecoder for MiniBlockDecoder {
fn drain(&mut self, num_rows: u64) -> Result<Box<dyn DecodePageTask>> {
let mut remaining = num_rows;
let mut chunks = Vec::new();
let offset_into_first_chunk = self.offset_in_current_chunk;
while remaining > 0 {
if remaining >= self.data.front().unwrap().vals_targeted - self.offset_in_current_chunk
{
let chunk = self.data.pop_front().unwrap();
remaining -= chunk.vals_targeted - self.offset_in_current_chunk;
chunks.push(chunk);
self.offset_in_current_chunk = 0;
} else {
let chunk = self.data.front().unwrap().clone();
self.offset_in_current_chunk += remaining;
debug_assert!(self.offset_in_current_chunk > 0);
remaining = 0;
chunks.push(chunk);
}
}
Ok(Box::new(DecodeMiniBlockTask {
chunks,
rep_decompressor: self.rep_decompressor.clone(),
def_decompressor: self.def_decompressor.clone(),
value_decompressor: self.value_decompressor.clone(),
num_rows,
offset_into_first_chunk,
}))
}
fn num_rows(&self) -> u64 {
self.num_rows
}
}
#[derive(Debug, Default)]
pub struct SimpleAllNullScheduler {}
impl StructuralPageScheduler for SimpleAllNullScheduler {
fn initialize<'a>(&'a mut self, _io: &Arc<dyn EncodingsIo>) -> BoxFuture<'a, Result<()>> {
std::future::ready(Ok(())).boxed()
}
fn schedule_ranges(
&self,
ranges: &[Range<u64>],
_io: &dyn EncodingsIo,
) -> Result<BoxFuture<'static, Result<Box<dyn StructuralPageDecoder>>>> {
let num_rows = ranges.iter().map(|r| r.end - r.start).sum::<u64>();
Ok(std::future::ready(Ok(
Box::new(SimpleAllNullPageDecoder { num_rows }) as Box<dyn StructuralPageDecoder>
))
.boxed())
}
}
#[derive(Debug)]
struct SimpleAllNullDecodePageTask {
num_values: u64,
}
impl DecodePageTask for SimpleAllNullDecodePageTask {
fn decode(self: Box<Self>) -> Result<DecodedPage> {
Ok(DecodedPage {
data: DataBlock::AllNull(AllNullDataBlock {
num_values: self.num_values,
}),
repetition: None,
definition: Some(vec![1; self.num_values as usize]),
})
}
}
#[derive(Debug)]
pub struct SimpleAllNullPageDecoder {
num_rows: u64,
}
impl StructuralPageDecoder for SimpleAllNullPageDecoder {
fn drain(&mut self, num_rows: u64) -> Result<Box<dyn DecodePageTask>> {
Ok(Box::new(SimpleAllNullDecodePageTask {
num_values: num_rows,
}))
}
fn num_rows(&self) -> u64 {
self.num_rows
}
}
#[derive(Debug)]
pub struct MiniBlockScheduler {
meta_buf_position: u64,
meta_buf_size: u64,
data_buf_position: u64,
priority: u64,
rows_in_page: u64,
rep_decompressor: Arc<dyn BlockDecompressor>,
def_decompressor: Arc<dyn BlockDecompressor>,
value_decompressor: Arc<dyn MiniBlockDecompressor>,
chunk_meta: Vec<ChunkMeta>,
}
impl MiniBlockScheduler {
fn try_new(
buffer_offsets_and_sizes: &[(u64, u64)],
priority: u64,
rows_in_page: u64,
layout: &pb::MiniBlockLayout,
decompressors: &dyn DecompressorStrategy,
) -> Result<Self> {
let (meta_buf_position, meta_buf_size) = buffer_offsets_and_sizes[0];
let (data_buf_position, _) = buffer_offsets_and_sizes[1];
let rep_decompressor =
decompressors.create_block_decompressor(layout.rep_compression.as_ref().unwrap())?;
let def_decompressor =
decompressors.create_block_decompressor(layout.def_compression.as_ref().unwrap())?;
let value_decompressor = decompressors
.create_miniblock_decompressor(layout.value_compression.as_ref().unwrap())?;
Ok(Self {
meta_buf_position,
meta_buf_size,
data_buf_position,
rep_decompressor: rep_decompressor.into(),
def_decompressor: def_decompressor.into(),
value_decompressor: value_decompressor.into(),
priority,
rows_in_page,
chunk_meta: Vec::new(),
})
}
fn calc_overlap(
range: &mut Range<u64>,
chunk: &ChunkMeta,
rows_offset: u64,
dst: &mut ScheduledChunk,
) -> ChunkOverlap {
if range.start > chunk.num_values + rows_offset {
ChunkOverlap::RangeAfterChunk
} else {
let start_in_chunk = range.start - rows_offset;
let end_in_chunk = (start_in_chunk + range.end - range.start).min(chunk.num_values);
let rows_in_chunk = end_in_chunk - start_in_chunk;
range.start += rows_in_chunk;
dst.ranges.push(start_in_chunk..end_in_chunk);
ChunkOverlap::Overlap
}
}
}
#[derive(Debug)]
struct ScheduledChunk {
data: LanceBuffer,
vals_in_chunk: u64,
vals_targeted: u64,
ranges: Vec<Range<u64>>,
}
impl Clone for ScheduledChunk {
fn clone(&self) -> Self {
Self {
data: self.data.try_clone().unwrap(),
vals_in_chunk: self.vals_in_chunk,
ranges: self.ranges.clone(),
vals_targeted: self.vals_targeted,
}
}
}
pub enum ChunkOverlap {
RangeAfterChunk,
Overlap,
}
impl StructuralPageScheduler for MiniBlockScheduler {
fn initialize<'a>(&'a mut self, io: &Arc<dyn EncodingsIo>) -> BoxFuture<'a, Result<()>> {
let metadata = io.submit_single(
self.meta_buf_position..self.meta_buf_position + self.meta_buf_size,
0,
);
async move {
let bytes = metadata.await?;
assert!(bytes.len() % 2 == 0);
let mut bytes = LanceBuffer::from_bytes(bytes, 2);
let words = bytes.borrow_to_typed_slice::<u16>();
let words = words.as_ref();
self.chunk_meta.reserve(words.len());
let mut rows_counter = 0;
for (word_idx, word) in words.iter().enumerate() {
let log_num_values = word & 0x0F;
let divided_bytes = word >> 4;
let num_bytes = (divided_bytes as usize + 1) * MINIBLOCK_ALIGNMENT;
debug_assert!(num_bytes > 0);
let num_values = if word_idx < words.len() - 1 {
debug_assert!(log_num_values > 0);
1 << log_num_values
} else {
debug_assert_eq!(log_num_values, 0);
self.rows_in_page - rows_counter
};
rows_counter += num_values;
self.chunk_meta.push(ChunkMeta {
num_values,
chunk_size_bytes: num_bytes as u64,
});
}
Ok(())
}
.boxed()
}
fn schedule_ranges(
&self,
ranges: &[Range<u64>],
io: &dyn EncodingsIo,
) -> Result<BoxFuture<'static, Result<Box<dyn StructuralPageDecoder>>>> {
let mut chunk_meta_iter = self.chunk_meta.iter();
let mut current_chunk = chunk_meta_iter.next().unwrap();
let mut row_offset = 0;
let mut bytes_offset = 0;
let mut scheduled_chunks = VecDeque::with_capacity(self.chunk_meta.len());
let mut ranges_to_req = Vec::with_capacity(self.chunk_meta.len());
let mut num_rows = 0;
let mut current_scheduled_chunk = ScheduledChunk {
data: LanceBuffer::empty(),
ranges: Vec::new(),
vals_in_chunk: current_chunk.num_values,
vals_targeted: 0,
};
for range in ranges {
num_rows += range.end - range.start;
let mut range = range.clone();
while !range.is_empty() {
Self::calc_overlap(
&mut range,
current_chunk,
row_offset,
&mut current_scheduled_chunk,
);
if !range.is_empty() {
if !current_scheduled_chunk.ranges.is_empty() {
scheduled_chunks.push_back(current_scheduled_chunk);
ranges_to_req.push(
(self.data_buf_position + bytes_offset)
..(self.data_buf_position
+ bytes_offset
+ current_chunk.chunk_size_bytes),
);
}
row_offset += current_chunk.num_values;
bytes_offset += current_chunk.chunk_size_bytes;
if let Some(next_chunk) = chunk_meta_iter.next() {
current_chunk = next_chunk;
}
current_scheduled_chunk = ScheduledChunk {
data: LanceBuffer::empty(),
ranges: Vec::new(),
vals_in_chunk: current_chunk.num_values,
vals_targeted: 0,
};
}
}
}
if !current_scheduled_chunk.ranges.is_empty() {
scheduled_chunks.push_back(current_scheduled_chunk);
ranges_to_req.push(
(self.data_buf_position + bytes_offset)
..(self.data_buf_position + bytes_offset + current_chunk.chunk_size_bytes),
);
}
let data = io.submit_request(ranges_to_req, self.priority);
let rep_decompressor = self.rep_decompressor.clone();
let def_decompressor = self.def_decompressor.clone();
let value_decompressor = self.value_decompressor.clone();
for scheduled_chunk in scheduled_chunks.iter_mut() {
scheduled_chunk.vals_targeted =
scheduled_chunk.ranges.iter().map(|r| r.end - r.start).sum();
}
Ok(async move {
let data = data.await?;
for (chunk, data) in scheduled_chunks.iter_mut().zip(data) {
chunk.data = LanceBuffer::from_bytes(data, 1);
}
Ok(Box::new(MiniBlockDecoder {
rep_decompressor,
def_decompressor,
value_decompressor,
data: scheduled_chunks,
offset_in_current_chunk: 0,
num_rows,
}) as Box<dyn StructuralPageDecoder>)
}
.boxed())
}
}
#[derive(Debug)]
pub struct FullZipScheduler {
data_buf_position: u64,
priority: u64,
rows_in_page: u64,
value_decompressor: Arc<dyn PerValueDecompressor>,
ctrl_word_parser: ControlWordParser,
}
impl FullZipScheduler {
fn try_new(
buffer_offsets_and_sizes: &[(u64, u64)],
priority: u64,
rows_in_page: u64,
layout: &pb::FullZipLayout,
decompressors: &dyn DecompressorStrategy,
) -> Result<Self> {
let (data_buf_position, _) = buffer_offsets_and_sizes[0];
let value_decompressor = decompressors
.create_per_value_decompressor(layout.value_compression.as_ref().unwrap())?;
let ctrl_word_parser = ControlWordParser::new(
layout.bits_rep.try_into().unwrap(),
layout.bits_def.try_into().unwrap(),
);
Ok(Self {
data_buf_position,
value_decompressor: value_decompressor.into(),
priority,
rows_in_page,
ctrl_word_parser,
})
}
}
impl StructuralPageScheduler for FullZipScheduler {
fn initialize<'a>(&'a mut self, _io: &Arc<dyn EncodingsIo>) -> BoxFuture<'a, Result<()>> {
std::future::ready(Ok(())).boxed()
}
fn schedule_ranges(
&self,
ranges: &[Range<u64>],
io: &dyn EncodingsIo,
) -> Result<BoxFuture<'static, Result<Box<dyn StructuralPageDecoder>>>> {
let bits_per_value = self.value_decompressor.bits_per_value();
assert_eq!(bits_per_value % 8, 0);
let bytes_per_value = bits_per_value / 8;
let bytes_per_cw = self.ctrl_word_parser.bytes_per_word();
let total_bytes_per_value = bytes_per_value + bytes_per_cw as u64;
let byte_ranges = ranges.iter().map(|r| {
debug_assert!(r.end <= self.rows_in_page);
let start = self.data_buf_position + r.start * total_bytes_per_value;
let end = self.data_buf_position + r.end * total_bytes_per_value;
start..end
});
let data = io.submit_request(byte_ranges.collect(), self.priority);
let value_decompressor = self.value_decompressor.clone();
let num_rows = ranges.iter().map(|r| r.end - r.start).sum();
let ctrl_word_parser = self.ctrl_word_parser;
Ok(async move {
let data = data.await?;
let data = data
.into_iter()
.map(|d| LanceBuffer::from_bytes(d, 1))
.collect();
Ok(Box::new(FixedFullZipDecoder {
value_decompressor,
data,
num_rows,
ctrl_word_parser,
offset_in_current: 0,
bytes_per_value: bytes_per_value as usize,
total_bytes_per_value: total_bytes_per_value as usize,
}) as Box<dyn StructuralPageDecoder>)
}
.boxed())
}
}
#[derive(Debug)]
struct FixedFullZipDecoder {
value_decompressor: Arc<dyn PerValueDecompressor>,
ctrl_word_parser: ControlWordParser,
data: VecDeque<LanceBuffer>,
offset_in_current: usize,
bytes_per_value: usize,
total_bytes_per_value: usize,
num_rows: u64,
}
impl StructuralPageDecoder for FixedFullZipDecoder {
fn drain(&mut self, num_rows: u64) -> Result<Box<dyn DecodePageTask>> {
let mut task_data = Vec::with_capacity(self.data.len());
let mut remaining = num_rows;
while remaining > 0 {
let cur_buf = self.data.front_mut().unwrap();
let bytes_avail = cur_buf.len() - self.offset_in_current;
let bytes_needed = remaining as usize * self.total_bytes_per_value;
let bytes_to_take = bytes_needed.min(bytes_avail);
let task_slice = cur_buf.slice_with_length(self.offset_in_current, bytes_to_take);
let rows_in_task = (bytes_to_take / self.total_bytes_per_value) as u64;
task_data.push((task_slice, rows_in_task));
remaining -= rows_in_task;
if bytes_to_take + self.offset_in_current == cur_buf.len() {
self.data.pop_front();
self.offset_in_current = 0;
} else {
self.offset_in_current += bytes_to_take;
}
}
let num_rows = task_data.iter().map(|td| td.1).sum::<u64>() as usize;
Ok(Box::new(FixedFullZipDecodeTask {
value_decompressor: self.value_decompressor.clone(),
ctrl_word_parser: self.ctrl_word_parser,
data: task_data,
bytes_per_value: self.bytes_per_value,
num_rows,
}))
}
fn num_rows(&self) -> u64 {
self.num_rows
}
}
#[derive(Debug)]
struct FixedFullZipDecodeTask {
value_decompressor: Arc<dyn PerValueDecompressor>,
ctrl_word_parser: ControlWordParser,
data: Vec<(LanceBuffer, u64)>,
num_rows: usize,
bytes_per_value: usize,
}
impl DecodePageTask for FixedFullZipDecodeTask {
fn decode(self: Box<Self>) -> Result<DecodedPage> {
let estimated_size_bytes = self.data.iter().map(|data| data.0.len()).sum::<usize>() * 2;
let mut data_builder =
DataBlockBuilder::with_capacity_estimate(estimated_size_bytes as u64);
if self.ctrl_word_parser.bytes_per_word() == 0 {
for (buf, rows_in_buf) in self.data.into_iter() {
let mut decompressed = self.value_decompressor.decompress(buf, rows_in_buf)?;
data_builder.append(&mut decompressed, 0..rows_in_buf);
}
Ok(DecodedPage {
data: data_builder.finish(),
repetition: None,
definition: None,
})
} else {
let mut rep = Vec::with_capacity(self.num_rows);
let mut def = Vec::with_capacity(self.num_rows);
for (buf, rows_in_buf) in self.data.into_iter() {
let mut buf_slice = buf.as_ref();
let mut values = Vec::with_capacity(
buf.len() - (self.ctrl_word_parser.bytes_per_word() * rows_in_buf as usize),
);
for _ in 0..rows_in_buf {
self.ctrl_word_parser.parse(buf_slice, &mut rep, &mut def);
buf_slice = &buf_slice[self.ctrl_word_parser.bytes_per_word()..];
values.extend_from_slice(buf_slice[..self.bytes_per_value].as_ref());
buf_slice = &buf_slice[self.bytes_per_value..];
}
let values_buf = LanceBuffer::Owned(values);
let mut decompressed = self
.value_decompressor
.decompress(values_buf, rows_in_buf)?;
data_builder.append(&mut decompressed, 0..rows_in_buf);
}
let repetition = if rep.is_empty() { None } else { Some(rep) };
let definition = if def.is_empty() { None } else { Some(def) };
Ok(DecodedPage {
data: data_builder.finish(),
repetition,
definition,
})
}
}
}
#[derive(Debug)]
struct StructuralPrimitiveFieldSchedulingJob<'a> {
scheduler: &'a StructuralPrimitiveFieldScheduler,
ranges: Vec<Range<u64>>,
page_idx: usize,
range_idx: usize,
range_offset: u64,
global_row_offset: u64,
}
impl<'a> StructuralPrimitiveFieldSchedulingJob<'a> {
pub fn new(scheduler: &'a StructuralPrimitiveFieldScheduler, ranges: Vec<Range<u64>>) -> Self {
Self {
scheduler,
ranges,
page_idx: 0,
range_idx: 0,
range_offset: 0,
global_row_offset: 0,
}
}
}
impl<'a> StructuralSchedulingJob for StructuralPrimitiveFieldSchedulingJob<'a> {
fn schedule_next(
&mut self,
context: &mut SchedulerContext,
) -> Result<Option<ScheduledScanLine>> {
if self.range_idx >= self.ranges.len() {
return Ok(None);
}
let mut range = self.ranges[self.range_idx].clone();
range.start += self.range_offset;
let priority = range.start;
let mut cur_page = &self.scheduler.page_schedulers[self.page_idx];
trace!(
"Current range is {:?} and current page has {} rows",
range,
cur_page.num_rows
);
while cur_page.num_rows + self.global_row_offset <= range.start {
self.global_row_offset += cur_page.num_rows;
self.page_idx += 1;
trace!("Skipping entire page of {} rows", cur_page.num_rows);
cur_page = &self.scheduler.page_schedulers[self.page_idx];
}
let mut ranges_in_page = Vec::new();
while cur_page.num_rows + self.global_row_offset > range.start {
range.start = range.start.max(self.global_row_offset);
let start_in_page = range.start - self.global_row_offset;
let end_in_page = start_in_page + (range.end - range.start);
let end_in_page = end_in_page.min(cur_page.num_rows);
let last_in_range = (end_in_page + self.global_row_offset) >= range.end;
ranges_in_page.push(start_in_page..end_in_page);
if last_in_range {
self.range_idx += 1;
if self.range_idx == self.ranges.len() {
break;
}
range = self.ranges[self.range_idx].clone();
} else {
break;
}
}
let num_rows_in_next = ranges_in_page.iter().map(|r| r.end - r.start).sum();
trace!(
"Scheduling {} rows across {} ranges from page with {} rows (priority={}, column_index={}, page_index={})",
num_rows_in_next,
ranges_in_page.len(),
cur_page.num_rows,
priority,
self.scheduler.column_index,
cur_page.page_index,
);
self.global_row_offset += cur_page.num_rows;
self.page_idx += 1;
let page_decoder = cur_page
.scheduler
.schedule_ranges(&ranges_in_page, context.io().as_ref())?;
let cur_path = context.current_path();
let page_index = cur_page.page_index;
let unloaded_page = async move {
let page_decoder = page_decoder.await?;
Ok(LoadedPage {
decoder: page_decoder,
path: cur_path,
page_index,
})
}
.boxed();
Ok(Some(ScheduledScanLine {
decoders: vec![MessageType::UnloadedPage(UnloadedPage(unloaded_page))],
rows_scheduled: num_rows_in_next,
}))
}
}
#[derive(Debug)]
struct PageInfoAndScheduler {
page_index: usize,
num_rows: u64,
scheduler: Box<dyn StructuralPageScheduler>,
}
#[derive(Debug)]
pub struct StructuralPrimitiveFieldScheduler {
page_schedulers: Vec<PageInfoAndScheduler>,
column_index: u32,
}
impl StructuralPrimitiveFieldScheduler {
pub fn try_new(
column_info: &ColumnInfo,
decompressors: &dyn DecompressorStrategy,
) -> Result<Self> {
let page_schedulers = column_info
.page_infos
.iter()
.enumerate()
.map(|(page_index, page_info)| {
Self::page_info_to_scheduler(page_info, page_index, decompressors)
})
.collect::<Result<Vec<_>>>()?;
Ok(Self {
page_schedulers,
column_index: column_info.index,
})
}
fn page_info_to_scheduler(
page_info: &PageInfo,
page_index: usize,
decompressors: &dyn DecompressorStrategy,
) -> Result<PageInfoAndScheduler> {
let scheduler: Box<dyn StructuralPageScheduler> =
match page_info.encoding.as_structural().layout.as_ref() {
Some(pb::page_layout::Layout::MiniBlockLayout(mini_block)) => {
Box::new(MiniBlockScheduler::try_new(
&page_info.buffer_offsets_and_sizes,
page_info.priority,
page_info.num_rows,
mini_block,
decompressors,
)?)
}
Some(pb::page_layout::Layout::FullZipLayout(full_zip)) => {
Box::new(FullZipScheduler::try_new(
&page_info.buffer_offsets_and_sizes,
page_info.priority,
page_info.num_rows,
full_zip,
decompressors,
)?)
}
Some(pb::page_layout::Layout::AllNullLayout(_)) => {
Box::new(SimpleAllNullScheduler::default()) as Box<dyn StructuralPageScheduler>
}
_ => todo!(),
};
Ok(PageInfoAndScheduler {
page_index,
num_rows: page_info.num_rows,
scheduler,
})
}
}
impl StructuralFieldScheduler for StructuralPrimitiveFieldScheduler {
fn initialize<'a>(
&'a mut self,
_filter: &'a FilterExpression,
context: &'a SchedulerContext,
) -> BoxFuture<'a, Result<()>> {
let page_init = self
.page_schedulers
.iter_mut()
.map(|s| s.scheduler.initialize(context.io()))
.collect::<FuturesUnordered<_>>();
async move {
page_init.try_collect::<Vec<_>>().await?;
Ok(())
}
.boxed()
}
fn schedule_ranges<'a>(
&'a self,
ranges: &[Range<u64>],
_filter: &FilterExpression,
) -> Result<Box<dyn StructuralSchedulingJob + 'a>> {
let ranges = ranges.to_vec();
Ok(Box::new(StructuralPrimitiveFieldSchedulingJob::new(
self, ranges,
)))
}
}
pub struct PrimitiveFieldDecoder {
data_type: DataType,
unloaded_physical_decoder: Option<BoxFuture<'static, Result<Box<dyn PrimitivePageDecoder>>>>,
physical_decoder: Option<Arc<dyn PrimitivePageDecoder>>,
should_validate: bool,
num_rows: u64,
rows_drained: u64,
column_index: u32,
page_index: u32,
}
impl PrimitiveFieldDecoder {
pub fn new_from_data(
physical_decoder: Arc<dyn PrimitivePageDecoder>,
data_type: DataType,
num_rows: u64,
should_validate: bool,
) -> Self {
Self {
data_type,
unloaded_physical_decoder: None,
physical_decoder: Some(physical_decoder),
should_validate,
num_rows,
rows_drained: 0,
column_index: u32::MAX,
page_index: u32::MAX,
}
}
}
impl Debug for PrimitiveFieldDecoder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PrimitiveFieldDecoder")
.field("data_type", &self.data_type)
.field("num_rows", &self.num_rows)
.field("rows_drained", &self.rows_drained)
.finish()
}
}
struct PrimitiveFieldDecodeTask {
rows_to_skip: u64,
rows_to_take: u64,
should_validate: bool,
physical_decoder: Arc<dyn PrimitivePageDecoder>,
data_type: DataType,
}
impl DecodeArrayTask for PrimitiveFieldDecodeTask {
fn decode(self: Box<Self>) -> Result<ArrayRef> {
let block = self
.physical_decoder
.decode(self.rows_to_skip, self.rows_to_take)?;
let array = make_array(block.into_arrow(self.data_type.clone(), self.should_validate)?);
if let DataType::Dictionary(_, _) = self.data_type {
let dict = array.as_any_dictionary();
if let Some(nulls) = array.logical_nulls() {
let new_indices = dict.keys().to_data();
let new_array = make_array(
new_indices
.into_builder()
.nulls(Some(nulls))
.add_child_data(dict.values().to_data())
.data_type(dict.data_type().clone())
.build()?,
);
return Ok(new_array);
}
}
Ok(array)
}
}
impl LogicalPageDecoder for PrimitiveFieldDecoder {
fn wait_for_loaded(&mut self, loaded_need: u64) -> BoxFuture<Result<()>> {
log::trace!(
"primitive wait for more than {} rows on column {} and page {} (page has {} rows)",
loaded_need,
self.column_index,
self.page_index,
self.num_rows
);
async move {
let physical_decoder = self.unloaded_physical_decoder.take().unwrap().await?;
self.physical_decoder = Some(Arc::from(physical_decoder));
Ok(())
}
.boxed()
}
fn drain(&mut self, num_rows: u64) -> Result<NextDecodeTask> {
if self.physical_decoder.as_ref().is_none() {
return Err(lance_core::Error::Internal {
message: format!("drain was called on primitive field decoder for data type {} on column {} but the decoder was never awaited", self.data_type, self.column_index),
location: location!(),
});
}
let rows_to_skip = self.rows_drained;
let rows_to_take = num_rows;
self.rows_drained += rows_to_take;
let task = Box::new(PrimitiveFieldDecodeTask {
rows_to_skip,
rows_to_take,
should_validate: self.should_validate,
physical_decoder: self.physical_decoder.as_ref().unwrap().clone(),
data_type: self.data_type.clone(),
});
Ok(NextDecodeTask {
task,
num_rows: rows_to_take,
has_more: self.rows_drained != self.num_rows,
})
}
fn rows_loaded(&self) -> u64 {
if self.unloaded_physical_decoder.is_some() {
0
} else {
self.num_rows
}
}
fn rows_drained(&self) -> u64 {
if self.unloaded_physical_decoder.is_some() {
0
} else {
self.rows_drained
}
}
fn num_rows(&self) -> u64 {
self.num_rows
}
fn data_type(&self) -> &DataType {
&self.data_type
}
}
#[derive(Debug)]
pub struct StructuralCompositeDecodeArrayTask {
tasks: Vec<Box<dyn DecodePageTask>>,
num_values: u64,
data_type: DataType,
should_validate: bool,
}
impl StructuralDecodeArrayTask for StructuralCompositeDecodeArrayTask {
fn decode(self: Box<Self>) -> Result<DecodedArray> {
let mut arrays = Vec::with_capacity(self.tasks.len());
let mut all_rep = LevelBuffer::with_capacity(self.num_values as usize);
let mut all_def = LevelBuffer::with_capacity(self.num_values as usize);
let mut offset = 0;
let mut has_def = false;
for task in self.tasks {
let decoded = task.decode()?;
if let Some(rep) = &decoded.repetition {
all_rep.extend(rep);
}
if let Some(def) = &decoded.definition {
if !has_def {
has_def = true;
all_def.extend(iter::repeat(0).take(offset));
}
all_def.extend(def);
}
let array = make_array(
decoded
.data
.into_arrow(self.data_type.clone(), self.should_validate)?,
);
offset += array.len();
arrays.push(array);
}
let array_refs = arrays.iter().map(|arr| arr.as_ref()).collect::<Vec<_>>();
let array = arrow_select::concat::concat(&array_refs)?;
let all_rep = if all_rep.is_empty() {
None
} else {
Some(all_rep)
};
let all_def = if all_def.is_empty() {
None
} else {
Some(all_def)
};
let mut repdef = RepDefUnraveler::new(all_rep, all_def);
let mut validity = repdef.unravel_validity();
if matches!(self.data_type, DataType::Null) {
validity = None;
}
if let Some(validity) = validity.as_ref() {
assert!(validity.len() == array.len());
}
let array = make_array(unsafe {
array
.to_data()
.into_builder()
.nulls(validity)
.build_unchecked()
});
Ok(DecodedArray { array, repdef })
}
}
#[derive(Debug)]
pub struct StructuralPrimitiveFieldDecoder {
field: Arc<ArrowField>,
page_decoders: VecDeque<Box<dyn StructuralPageDecoder>>,
should_validate: bool,
rows_drained_in_current: u64,
}
impl StructuralPrimitiveFieldDecoder {
pub fn new(field: &Arc<ArrowField>, should_validate: bool) -> Self {
Self {
field: field.clone(),
page_decoders: VecDeque::new(),
should_validate,
rows_drained_in_current: 0,
}
}
}
impl StructuralFieldDecoder for StructuralPrimitiveFieldDecoder {
fn accept_page(&mut self, child: LoadedPage) -> Result<()> {
assert!(child.path.is_empty());
self.page_decoders.push_back(child.decoder);
Ok(())
}
fn drain(&mut self, num_rows: u64) -> Result<Box<dyn StructuralDecodeArrayTask>> {
let mut remaining = num_rows;
let mut tasks = Vec::new();
while remaining > 0 {
let cur_page = self.page_decoders.front_mut().unwrap();
let num_in_page = cur_page.num_rows() - self.rows_drained_in_current;
let to_take = num_in_page.min(remaining);
let task = cur_page.drain(to_take)?;
tasks.push(task);
if to_take == num_in_page {
self.page_decoders.pop_front();
self.rows_drained_in_current = 0;
} else {
self.rows_drained_in_current += to_take;
}
remaining -= to_take;
}
Ok(Box::new(StructuralCompositeDecodeArrayTask {
tasks,
data_type: self.field.data_type().clone(),
should_validate: self.should_validate,
num_values: num_rows,
}))
}
fn data_type(&self) -> &DataType {
self.field.data_type()
}
}
#[derive(Debug)]
pub struct AccumulationQueue {
cache_bytes: u64,
keep_original_array: bool,
buffered_arrays: Vec<ArrayRef>,
current_bytes: u64,
row_number: u64,
column_index: u32,
}
impl AccumulationQueue {
pub fn new(cache_bytes: u64, column_index: u32, keep_original_array: bool) -> Self {
Self {
cache_bytes,
buffered_arrays: Vec::new(),
current_bytes: 0,
column_index,
keep_original_array,
row_number: u64::MAX,
}
}
pub fn insert(&mut self, array: ArrayRef, row_number: u64) -> Option<(Vec<ArrayRef>, u64)> {
if self.row_number == u64::MAX {
self.row_number = row_number;
}
self.current_bytes += array.get_array_memory_size() as u64;
if self.current_bytes > self.cache_bytes {
debug!(
"Flushing column {} page of size {} bytes (unencoded)",
self.column_index, self.current_bytes
);
self.buffered_arrays.push(array);
self.current_bytes = 0;
let row_number = self.row_number;
self.row_number = u64::MAX;
Some((std::mem::take(&mut self.buffered_arrays), row_number))
} else {
trace!(
"Accumulating data for column {}. Now at {} bytes",
self.column_index,
self.current_bytes
);
if self.keep_original_array {
self.buffered_arrays.push(array);
} else {
self.buffered_arrays.push(deep_copy_array(array.as_ref()))
}
None
}
}
pub fn flush(&mut self) -> Option<(Vec<ArrayRef>, u64)> {
if self.buffered_arrays.is_empty() {
trace!(
"No final flush since no data at column {}",
self.column_index
);
None
} else {
trace!(
"Final flush of column {} which has {} bytes",
self.column_index,
self.current_bytes
);
self.current_bytes = 0;
let row_number = self.row_number;
self.row_number = 0;
Some((std::mem::take(&mut self.buffered_arrays), row_number))
}
}
}
pub struct PrimitiveFieldEncoder {
accumulation_queue: AccumulationQueue,
array_encoding_strategy: Arc<dyn ArrayEncodingStrategy>,
column_index: u32,
field: Field,
max_page_bytes: u64,
}
impl PrimitiveFieldEncoder {
pub fn try_new(
options: &EncodingOptions,
array_encoding_strategy: Arc<dyn ArrayEncodingStrategy>,
column_index: u32,
field: Field,
) -> Result<Self> {
Ok(Self {
accumulation_queue: AccumulationQueue::new(
options.cache_bytes_per_column,
column_index,
options.keep_original_array,
),
column_index,
max_page_bytes: options.max_page_bytes,
array_encoding_strategy,
field,
})
}
fn create_encode_task(&mut self, arrays: Vec<ArrayRef>) -> Result<EncodeTask> {
let encoder = self
.array_encoding_strategy
.create_array_encoder(&arrays, &self.field)?;
let column_idx = self.column_index;
let data_type = self.field.data_type();
Ok(tokio::task::spawn(async move {
let num_values = arrays.iter().map(|arr| arr.len() as u64).sum();
let data = DataBlock::from_arrays(&arrays, num_values);
let mut buffer_index = 0;
let array = encoder.encode(data, &data_type, &mut buffer_index)?;
let (data, description) = array.into_buffers();
Ok(EncodedPage {
data,
description: PageEncoding::Legacy(description),
num_rows: num_values,
column_idx,
row_number: 0, })
})
.map(|res_res| res_res.unwrap())
.boxed())
}
fn do_flush(&mut self, arrays: Vec<ArrayRef>) -> Result<Vec<EncodeTask>> {
if arrays.len() == 1 {
let array = arrays.into_iter().next().unwrap();
let size_bytes = array.get_buffer_memory_size();
let num_parts = bit_util::ceil(size_bytes, self.max_page_bytes as usize);
let num_parts = num_parts.min(array.len());
if num_parts <= 1 {
Ok(vec![self.create_encode_task(vec![array])?])
} else {
let mut tasks = Vec::with_capacity(num_parts);
let mut offset = 0;
let part_size = bit_util::ceil(array.len(), num_parts);
for _ in 0..num_parts {
let avail = array.len() - offset;
let chunk_size = avail.min(part_size);
let part = array.slice(offset, chunk_size);
let task = self.create_encode_task(vec![part])?;
tasks.push(task);
offset += chunk_size;
}
Ok(tasks)
}
} else {
Ok(vec![self.create_encode_task(arrays)?])
}
}
}
impl FieldEncoder for PrimitiveFieldEncoder {
fn maybe_encode(
&mut self,
array: ArrayRef,
_external_buffers: &mut OutOfLineBuffers,
_repdef: RepDefBuilder,
_row_number: u64,
) -> Result<Vec<EncodeTask>> {
if let Some(arrays) = self.accumulation_queue.insert(array, 0) {
Ok(self.do_flush(arrays.0)?)
} else {
Ok(vec![])
}
}
fn flush(&mut self, _external_buffers: &mut OutOfLineBuffers) -> Result<Vec<EncodeTask>> {
if let Some(arrays) = self.accumulation_queue.flush() {
Ok(self.do_flush(arrays.0)?)
} else {
Ok(vec![])
}
}
fn num_columns(&self) -> u32 {
1
}
fn finish(
&mut self,
_external_buffers: &mut OutOfLineBuffers,
) -> BoxFuture<'_, Result<Vec<crate::encoder::EncodedColumn>>> {
std::future::ready(Ok(vec![EncodedColumn::default()])).boxed()
}
}
const MINIBLOCK_ALIGNMENT: usize = 8;
const MINIBLOCK_MAX_PADDING: usize = MINIBLOCK_ALIGNMENT - 1;
pub struct PrimitiveStructuralEncoder {
accumulation_queue: AccumulationQueue,
accumulated_repdefs: Vec<RepDefBuilder>,
compression_strategy: Arc<dyn CompressionStrategy>,
column_index: u32,
field: Field,
}
impl PrimitiveStructuralEncoder {
pub fn try_new(
options: &EncodingOptions,
compression_strategy: Arc<dyn CompressionStrategy>,
column_index: u32,
field: Field,
) -> Result<Self> {
Ok(Self {
accumulation_queue: AccumulationQueue::new(
options.cache_bytes_per_column,
column_index,
options.keep_original_array,
),
accumulated_repdefs: Vec::new(),
column_index,
compression_strategy,
field,
})
}
fn is_narrow(data_block: &DataBlock) -> bool {
const MINIBLOCK_MAX_BYTE_LENGTH_PER_VALUE: u64 = 256;
if let Some(max_len_array) = data_block.get_stat(Stat::MaxLength) {
let max_len_array = max_len_array
.as_any()
.downcast_ref::<PrimitiveArray<UInt64Type>>()
.unwrap();
if max_len_array.value(0) < MINIBLOCK_MAX_BYTE_LENGTH_PER_VALUE {
return true;
}
}
false
}
fn serialize_miniblocks(
miniblocks: MiniBlockCompressed,
rep: Vec<LanceBuffer>,
def: Vec<LanceBuffer>,
) -> (LanceBuffer, LanceBuffer) {
let bytes_rep = rep.iter().map(|r| r.len()).sum::<usize>();
let bytes_def = def.iter().map(|d| d.len()).sum::<usize>();
let max_bytes_repdef_len = rep.len() * 4;
let max_padding = miniblocks.chunks.len() * (1 + (2 * MINIBLOCK_MAX_PADDING));
let mut data_buffer = Vec::with_capacity(
miniblocks.data.len() + bytes_rep + bytes_def + max_bytes_repdef_len + max_padding, );
let mut meta_buffer = Vec::with_capacity(miniblocks.data.len() * 2);
let mut value_offset = 0;
for ((chunk, rep), def) in miniblocks.chunks.into_iter().zip(rep).zip(def) {
let start_len = data_buffer.len();
debug_assert_eq!(start_len % MINIBLOCK_ALIGNMENT, 0);
assert!(rep.len() < u16::MAX as usize);
assert!(def.len() < u16::MAX as usize);
let bytes_rep = rep.len() as u16;
let bytes_def = def.len() as u16;
let bytes_val = chunk.num_bytes;
data_buffer.extend_from_slice(&bytes_rep.to_le_bytes());
data_buffer.extend_from_slice(&bytes_def.to_le_bytes());
data_buffer.extend_from_slice(&bytes_val.to_le_bytes());
data_buffer.extend_from_slice(&rep);
debug_assert_eq!(data_buffer.len() % 2, 0);
data_buffer.extend_from_slice(&def);
let p2 = pad_bytes::<MINIBLOCK_ALIGNMENT>(data_buffer.len());
data_buffer.extend(iter::repeat(0).take(p2));
let num_value_bytes = chunk.num_bytes as usize;
let values =
&miniblocks.data[value_offset as usize..value_offset as usize + num_value_bytes];
debug_assert_eq!(data_buffer.len() % MINIBLOCK_ALIGNMENT, 0);
data_buffer.extend_from_slice(values);
let p3 = pad_bytes::<MINIBLOCK_ALIGNMENT>(data_buffer.len());
data_buffer.extend(iter::repeat(0).take(p3));
value_offset += num_value_bytes as u64;
let chunk_bytes = data_buffer.len() - start_len;
assert!(chunk_bytes <= 16 * 1024);
assert!(chunk_bytes > 0);
assert_eq!(chunk_bytes % 8, 0);
let divided_bytes = chunk_bytes / MINIBLOCK_ALIGNMENT;
let divided_bytes_minus_one = (divided_bytes - 1) as u64;
let metadata = ((divided_bytes_minus_one << 4) | chunk.log_num_values as u64) as u16;
meta_buffer.extend_from_slice(&metadata.to_le_bytes());
}
(
LanceBuffer::Owned(data_buffer),
LanceBuffer::Owned(meta_buffer),
)
}
fn compress_levels(
levels: Option<LevelBuffer>,
num_values: u64,
compression_strategy: &dyn CompressionStrategy,
chunks: &[MiniBlockChunk],
) -> Result<(Vec<LanceBuffer>, pb::ArrayEncoding)> {
if let Some(levels) = levels {
debug_assert_eq!(num_values as usize, levels.len());
let mut levels_buf = LanceBuffer::reinterpret_vec(levels);
let levels_block = DataBlock::FixedWidth(FixedWidthDataBlock {
data: levels_buf.borrow_and_clone(),
bits_per_value: 16,
num_values,
block_info: BlockInfo::new(),
used_encoding: UsedEncoding::new(),
});
let levels_field = Field::new_arrow("", DataType::UInt16, false)?;
let (compressor, compressor_desc) =
compression_strategy.create_block_compressor(&levels_field, &levels_block)?;
let mut buffers = Vec::with_capacity(chunks.len());
let mut off = 0;
let mut values_counter = 0;
for chunk in chunks {
let chunk_num_values = chunk.num_values(values_counter, num_values);
values_counter += chunk_num_values;
let level_bytes = chunk_num_values as usize * 2;
let chunk_levels = levels_buf.slice_with_length(off, level_bytes);
let chunk_levels_block = DataBlock::FixedWidth(FixedWidthDataBlock {
data: chunk_levels,
bits_per_value: 16,
num_values: chunk_num_values,
block_info: BlockInfo::new(),
used_encoding: UsedEncoding::new(),
});
let compressed_levels = compressor.compress(chunk_levels_block)?;
off += level_bytes;
buffers.push(compressed_levels);
}
Ok((buffers, compressor_desc))
} else {
let data = chunks.iter().map(|_| LanceBuffer::empty()).collect();
let scalar = 0_u16.to_le_bytes().to_vec();
let encoding = ProtobufUtils::constant(scalar, num_values);
Ok((data, encoding))
}
}
fn encode_simple_all_null(
column_idx: u32,
num_rows: u64,
row_number: u64,
) -> Result<EncodedPage> {
let description = ProtobufUtils::simple_all_null_layout();
Ok(EncodedPage {
column_idx,
data: vec![],
description: PageEncoding::Structural(description),
num_rows,
row_number,
})
}
fn encode_miniblock(
column_idx: u32,
field: &Field,
compression_strategy: &dyn CompressionStrategy,
data: DataBlock,
repdefs: Vec<RepDefBuilder>,
row_number: u64,
) -> Result<EncodedPage> {
let repdef = RepDefBuilder::serialize(repdefs);
if let DataBlock::AllNull(_null_block) = data {
todo!()
}
let num_values = data.num_values();
let data = data.remove_validity();
let compressor = compression_strategy.create_miniblock_compressor(field, &data)?;
let (compressed_data, value_encoding) = compressor.compress(data)?;
let (compressed_rep, rep_encoding) = Self::compress_levels(
repdef.repetition_levels,
num_values,
compression_strategy,
&compressed_data.chunks,
)?;
let (compressed_def, def_encoding) = Self::compress_levels(
repdef.definition_levels,
num_values,
compression_strategy,
&compressed_data.chunks,
)?;
let (block_value_buffer, block_meta_buffer) =
Self::serialize_miniblocks(compressed_data, compressed_rep, compressed_def);
let description =
ProtobufUtils::miniblock_layout(rep_encoding, def_encoding, value_encoding);
Ok(EncodedPage {
num_rows: num_values,
column_idx,
data: vec![block_meta_buffer, block_value_buffer],
description: PageEncoding::Structural(description),
row_number,
})
}
fn serialize_full_zip_fixed(
fixed: FixedWidthDataBlock,
mut repdef: ControlWordIterator,
) -> LanceBuffer {
let len = fixed.data.len() + repdef.bytes_per_word() * fixed.num_values as usize;
let mut buf = Vec::with_capacity(len);
assert_eq!(
fixed.bits_per_value % 8,
0,
"Non-byte aligned full-zip compression not yet supported"
);
let bytes_per_value = fixed.bits_per_value as usize / 8;
for value in fixed.data.chunks_exact(bytes_per_value) {
repdef.append_next(&mut buf);
buf.extend_from_slice(value);
}
LanceBuffer::Owned(buf)
}
fn serialize_full_zip_variable(
mut variable: VariableWidthBlock,
mut repdef: ControlWordIterator,
) -> LanceBuffer {
let bytes_per_offset = variable.bits_per_offset as usize / 8;
assert_eq!(
variable.bits_per_offset % 8,
0,
"Only byte-aligned offsets supported"
);
let len = variable.data.len()
+ repdef.bytes_per_word() * variable.num_values as usize
+ bytes_per_offset * variable.num_values as usize;
let mut buf = Vec::with_capacity(len);
match bytes_per_offset {
4 => {
let offs = variable.offsets.borrow_to_typed_slice::<u32>();
for offsets in offs.as_ref().windows(2) {
repdef.append_next(&mut buf);
buf.extend_from_slice(&(offsets[1] - offsets[0]).to_le_bytes());
buf.extend_from_slice(&variable.data[offsets[0] as usize..offsets[1] as usize]);
}
}
8 => {
let offs = variable.offsets.borrow_to_typed_slice::<u64>();
for offsets in offs.as_ref().windows(2) {
repdef.append_next(&mut buf);
buf.extend_from_slice(&(offsets[1] - offsets[0]).to_le_bytes());
buf.extend_from_slice(&variable.data[offsets[0] as usize..offsets[1] as usize]);
}
}
_ => panic!("Unsupported offset size"),
}
LanceBuffer::Owned(buf)
}
fn serialize_full_zip(
compressed_data: PerValueDataBlock,
repdef: ControlWordIterator,
) -> LanceBuffer {
match compressed_data {
PerValueDataBlock::Fixed(fixed) => Self::serialize_full_zip_fixed(fixed, repdef),
PerValueDataBlock::Variable(var) => Self::serialize_full_zip_variable(var, repdef),
}
}
fn encode_full_zip(
column_idx: u32,
field: &Field,
compression_strategy: &dyn CompressionStrategy,
data: DataBlock,
repdefs: Vec<RepDefBuilder>,
row_number: u64,
) -> Result<EncodedPage> {
let repdef = RepDefBuilder::serialize(repdefs);
let max_rep = repdef
.repetition_levels
.as_ref()
.map_or(0, |r| r.iter().max().copied().unwrap_or(0));
let max_def = repdef
.definition_levels
.as_ref()
.map_or(0, |d| d.iter().max().copied().unwrap_or(0));
let repdef_iter = build_control_word_iterator(
repdef.repetition_levels,
max_rep,
repdef.definition_levels,
max_def,
);
let bits_rep = repdef_iter.bits_rep();
let bits_def = repdef_iter.bits_def();
let num_values = data.num_values();
let data = data.remove_validity();
let compressor = compression_strategy.create_per_value(field, &data)?;
let (compressed_data, value_encoding) = compressor.compress(data)?;
let zipped = Self::serialize_full_zip(compressed_data, repdef_iter);
let description = ProtobufUtils::full_zip_layout(bits_rep, bits_def, value_encoding);
Ok(EncodedPage {
num_rows: num_values,
column_idx,
data: vec![zipped],
description: PageEncoding::Structural(description),
row_number,
})
}
fn do_flush(
&mut self,
arrays: Vec<ArrayRef>,
repdefs: Vec<RepDefBuilder>,
row_number: u64,
) -> Result<Vec<EncodeTask>> {
let column_idx = self.column_index;
let compression_strategy = self.compression_strategy.clone();
let field = self.field.clone();
let task = spawn_cpu(move || {
let num_values = arrays.iter().map(|arr| arr.len() as u64).sum();
let num_nulls = arrays
.iter()
.map(|arr| arr.logical_nulls().map(|n| n.null_count()).unwrap_or(0) as u64)
.sum::<u64>();
if num_values == num_nulls && repdefs.iter().all(|rd| rd.is_simple_validity()) {
log::debug!(
"Encoding column {} with {} rows using simple-null layout",
column_idx,
num_values
);
Self::encode_simple_all_null(column_idx, num_values, row_number)
} else {
let data_block = DataBlock::from_arrays(&arrays, num_values);
if Self::is_narrow(&data_block) {
log::debug!(
"Encoding column {} with {} rows using mini-block layout",
column_idx,
num_values
);
Self::encode_miniblock(
column_idx,
&field,
compression_strategy.as_ref(),
data_block,
repdefs,
row_number,
)
} else {
log::debug!(
"Encoding column {} with {} rows using full-zip layout",
column_idx,
num_values
);
Self::encode_full_zip(
column_idx,
&field,
compression_strategy.as_ref(),
data_block,
repdefs,
row_number,
)
}
}
})
.boxed();
Ok(vec![task])
}
fn extract_validity_buf(array: &dyn Array, repdef: &mut RepDefBuilder) {
if let Some(validity) = array.nulls() {
repdef.add_validity_bitmap(validity.clone());
} else {
repdef.add_no_null(array.len());
}
}
fn extract_validity(array: &dyn Array, repdef: &mut RepDefBuilder) {
match array.data_type() {
DataType::Null => {
repdef.add_validity_bitmap(NullBuffer::new(BooleanBuffer::new_unset(array.len())));
}
DataType::Dictionary(_, _) => {
unreachable!()
}
_ => Self::extract_validity_buf(array, repdef),
}
}
}
impl FieldEncoder for PrimitiveStructuralEncoder {
fn maybe_encode(
&mut self,
array: ArrayRef,
_external_buffers: &mut OutOfLineBuffers,
mut repdef: RepDefBuilder,
row_number: u64,
) -> Result<Vec<EncodeTask>> {
Self::extract_validity(array.as_ref(), &mut repdef);
self.accumulated_repdefs.push(repdef);
if let Some((arrays, row_number)) = self.accumulation_queue.insert(array, row_number) {
let accumulated_repdefs = std::mem::take(&mut self.accumulated_repdefs);
Ok(self.do_flush(arrays, accumulated_repdefs, row_number)?)
} else {
Ok(vec![])
}
}
fn flush(&mut self, _external_buffers: &mut OutOfLineBuffers) -> Result<Vec<EncodeTask>> {
if let Some((arrays, row_number)) = self.accumulation_queue.flush() {
let accumulated_repdefs = std::mem::take(&mut self.accumulated_repdefs);
Ok(self.do_flush(arrays, accumulated_repdefs, row_number)?)
} else {
Ok(vec![])
}
}
fn num_columns(&self) -> u32 {
1
}
fn finish(
&mut self,
_external_buffers: &mut OutOfLineBuffers,
) -> BoxFuture<'_, Result<Vec<crate::encoder::EncodedColumn>>> {
std::future::ready(Ok(vec![EncodedColumn::default()])).boxed()
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use arrow_array::{ArrayRef, Int8Array, StringArray};
use crate::encodings::logical::primitive::PrimitiveStructuralEncoder;
use super::DataBlock;
#[test]
fn test_is_narrow() {
let int8_array = Int8Array::from(vec![1, 2, 3]);
let array_ref: ArrayRef = Arc::new(int8_array);
let block = DataBlock::from_array(array_ref);
assert!(PrimitiveStructuralEncoder::is_narrow(&block));
let string_array = StringArray::from(vec![Some("hello"), Some("world")]);
let block = DataBlock::from_array(string_array);
assert!(PrimitiveStructuralEncoder::is_narrow(&block));
let string_array = StringArray::from(vec![
Some("hello world".repeat(100)),
Some("world".to_string()),
]);
let block = DataBlock::from_array(string_array);
assert!((!PrimitiveStructuralEncoder::is_narrow(&block)));
}
}