Skip to content

Commit

Permalink
lifetimes ¯\_(ツ)_/¯
Browse files Browse the repository at this point in the history
  • Loading branch information
jdcasale committed May 8, 2024
1 parent 2bfcb46 commit 4667b2d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 55 deletions.
49 changes: 49 additions & 0 deletions vortex-array/src/stats/flatbuffers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use flatbuffers::{FlatBufferBuilder, WIPOffset};
use itertools::Itertools;
use vortex_flatbuffers::WriteFlatBuffer;

use crate::stats::{Stat, Statistics};

impl WriteFlatBuffer for &dyn Statistics {
type Target<'t> = crate::flatbuffers::ArrayStats<'t>;

fn write_flatbuffer<'fb>(
&self,
fbb: &mut FlatBufferBuilder<'fb>,
) -> WIPOffset<Self::Target<'fb>> {
let trailing_zero_freq = self
.get_as::<Vec<u64>>(Stat::TrailingZeroFreq)
.ok()
.map(|v| v.iter().copied().collect_vec())
.map(|v| fbb.create_vector(v.as_slice()));

let bit_width_freq = self
.get_as::<Vec<u64>>(Stat::BitWidthFreq)
.ok()
.map(|v| v.iter().copied().collect_vec())
.map(|v| fbb.create_vector(v.as_slice()));

let min = self
.get(Stat::Min)
.map(|min| min.value().write_flatbuffer(fbb));

let max = self
.get(Stat::Max)
.map(|max| max.value().write_flatbuffer(fbb));

let stat_args = &crate::flatbuffers::ArrayStatsArgs {
min,
max,
is_sorted: self.get_as::<bool>(Stat::IsSorted).ok(),
is_strict_sorted: self.get_as::<bool>(Stat::IsStrictSorted).ok(),
is_constant: self.get_as::<bool>(Stat::IsConstant).ok(),
run_count: self.get_as::<u64>(Stat::RunCount).ok(),
true_count: self.get_as::<u64>(Stat::TrueCount).ok(),
null_count: self.get_as::<u64>(Stat::NullCount).ok(),
bit_width_freq,
trailing_zero_freq,
};

crate::flatbuffers::ArrayStats::create(fbb, stat_args)
}
}
1 change: 1 addition & 0 deletions vortex-array/src/stats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use vortex_dtype::{DType, NativePType};
use vortex_error::{vortex_err, VortexError, VortexResult};
use vortex_scalar::Scalar;

pub mod flatbuffers;
mod statsset;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Sequence)]
Expand Down
6 changes: 3 additions & 3 deletions vortex-array/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ impl Statistics for ArrayView<'_> {

self.to_array()
.with_dyn(|a| a.compute_statistics(stat))
.ok()?;

self.get(stat)
.ok()?
.get(stat)
.cloned()
}
}

Expand Down
58 changes: 6 additions & 52 deletions vortex-ipc/src/messages.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use flatbuffers::{FlatBufferBuilder, WIPOffset};
use itertools::Itertools;
use vortex::flatbuffers as vfb;
use vortex::stats::Stat;
use vortex::flatbuffers as fba;
use vortex::{ArrayData, Context, ViewContext};
use vortex_dtype::DType;
use vortex_error::{vortex_err, VortexError};
Expand Down Expand Up @@ -159,7 +158,7 @@ impl<'a> WriteFlatBuffer for IPCChunk<'a> {
}

impl<'a> WriteFlatBuffer for IPCArray<'a> {
type Target<'t> = vfb::Array<'t>;
type Target<'t> = fba::Array<'t>;

fn write_flatbuffer<'fb>(
&self,
Expand Down Expand Up @@ -191,63 +190,18 @@ impl<'a> WriteFlatBuffer for IPCArray<'a> {
.collect_vec();
let children = Some(fbb.create_vector(&children));

let stats = collect_array_stats(fbb, self.1);
let stats = Some(self.1.statistics().write_flatbuffer(fbb));

vfb::Array::create(
fba::Array::create(
fbb,
&vfb::ArrayArgs {
&fba::ArrayArgs {
version: Default::default(),
has_buffer: column_data.buffer().is_some(),
encoding,
metadata,
stats: Some(stats),
stats,
children,
},
)
}
}

/// Computes all stats and uses the results to create an ArrayStats table for the flatbuffer message
fn collect_array_stats<'a>(
fbb: &'_ mut FlatBufferBuilder<'a>,
array: &'_ ArrayData,
) -> WIPOffset<vfb::ArrayStats<'a>> {
let trailing_zero_freq = array
.statistics()
.get_as::<Vec<u64>>(Stat::TrailingZeroFreq)
.ok()
.map(|v| v.iter().copied().collect_vec())
.map(|v| fbb.create_vector(v.as_slice()));

let bit_width_freq = array
.statistics()
.get_as::<Vec<u64>>(Stat::BitWidthFreq)
.ok()
.map(|v| v.iter().copied().collect_vec())
.map(|v| fbb.create_vector(v.as_slice()));

let min = array
.statistics()
.get(Stat::Min)
.map(|min| min.value().write_flatbuffer(fbb));

let max = array
.statistics()
.get(Stat::Max)
.map(|max| max.value().write_flatbuffer(fbb));

let stat_args = &vfb::ArrayStatsArgs {
min,
max,
is_sorted: array.statistics().get_as::<bool>(Stat::IsSorted).ok(),
is_strict_sorted: array.statistics().get_as::<bool>(Stat::IsStrictSorted).ok(),
is_constant: array.statistics().get_as::<bool>(Stat::IsConstant).ok(),
run_count: array.statistics().get_as::<u64>(Stat::RunCount).ok(),
true_count: array.statistics().get_as::<u64>(Stat::TrueCount).ok(),
null_count: array.statistics().get_as::<u64>(Stat::NullCount).ok(),
bit_width_freq,
trailing_zero_freq,
};

vfb::ArrayStats::create(fbb, stat_args)
}

0 comments on commit 4667b2d

Please sign in to comment.