mirror of
https://github.com/gorules/zen.git
synced 2026-11-28 16:06:26 +00:00
refactor: split compiled/typed.rs into compiled/leaf modules
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use crate::compiled::data::{Data, Layer, Mask, Shape};
|
||||
use crate::compiled::shred::Normalizer;
|
||||
use crate::compiled::typed::{ColumnBuilder, Input, Leaf};
|
||||
use crate::compiled::leaf::{ColumnBuilder, Input, Leaf};
|
||||
use crate::compiled::{CompiledGraph, Source};
|
||||
use crate::decision_graph::cleaner::{VariableCleaner, ZEN_RESERVED_PROPERTIES};
|
||||
use crate::decision_graph::walker::GraphWalker;
|
||||
@@ -9,7 +9,7 @@ use crate::nodes::NodeHandlerExtensions;
|
||||
use crate::EvaluationError;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
use crate::compiled::typed::Bits;
|
||||
use crate::compiled::leaf::Bits;
|
||||
use zen_expression::lane::{Column, Columns, Dictionary, Values};
|
||||
use zen_expression::Variable;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::compiled::typed::{Bits, ColumnBuilder, Leaf};
|
||||
use crate::compiled::leaf::{Bits, ColumnBuilder, Leaf};
|
||||
use crate::decision_graph::walker::GraphWalker;
|
||||
use std::cell::{OnceCell, RefCell};
|
||||
use std::rc::Rc;
|
||||
|
||||
@@ -0,0 +1,460 @@
|
||||
use std::rc::Rc;
|
||||
use zen_expression::lane::{Column, Dictionary, Kind, Output, Shape, Values};
|
||||
use zen_expression::Variable;
|
||||
use super::{Bits, Dict};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum Store {
|
||||
Scaled { mant: Vec<i64>, scale: Vec<u8> },
|
||||
Bool(Vec<u64>),
|
||||
Text { offsets: Vec<i32>, data: String },
|
||||
Coded { codes: Rc<[i32]>, dict: Rc<Dict> },
|
||||
Any(Vec<Variable>),
|
||||
List { offsets: Vec<i32>, child: Box<Store> },
|
||||
}
|
||||
|
||||
impl Store {
|
||||
#[inline(always)]
|
||||
pub fn part(n: &rust_decimal::Decimal) -> Option<(i64, u8)> {
|
||||
let unpacked = n.unpack();
|
||||
let wide = u64::from(unpacked.mid) << 32 | u64::from(unpacked.lo);
|
||||
let scale = u8::try_from(unpacked.scale).ok()?;
|
||||
match (unpacked.hi, unpacked.negative) {
|
||||
(0, false) => Some((i64::try_from(wide).ok()?, scale)),
|
||||
(0, true) if wide != 0 => Some((0i64.checked_sub_unsigned(wide)?, scale)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unpacked(values: &[rust_decimal::Decimal]) -> Option<Store> {
|
||||
let mut mant = Vec::with_capacity(values.len());
|
||||
let mut scale = Vec::with_capacity(values.len());
|
||||
for value in values {
|
||||
let (m, s) = Self::part(value)?;
|
||||
mant.push(m);
|
||||
scale.push(s);
|
||||
}
|
||||
Some(Store::Scaled { mant, scale })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Array {
|
||||
pub(super) values: Store,
|
||||
pub(super) valid: Option<Vec<u64>>,
|
||||
pub(super) rows: usize,
|
||||
}
|
||||
|
||||
impl Array {
|
||||
fn any(out: &Output) -> Array {
|
||||
Array::from_values(
|
||||
(0..out.len())
|
||||
.map(|row| out.variable(row).and_then(Result::ok).unwrap_or(Variable::Null))
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn from_output(out: &mut Output) -> Array {
|
||||
let rows = out.len();
|
||||
let mut valid: Option<Vec<u64>> = None;
|
||||
if out.kind() != Kind::Dyn {
|
||||
for (row, value) in out.boxed_values() {
|
||||
match value {
|
||||
Variable::Null => Bits::set(valid.get_or_insert_with(|| Bits::ones(rows)), *row as usize, false),
|
||||
_ => return Self::any(out),
|
||||
}
|
||||
}
|
||||
}
|
||||
if out.failed().iter().any(|w| *w != 0) {
|
||||
let bits = valid.get_or_insert_with(|| Bits::ones(rows));
|
||||
bits.iter_mut().zip(out.failed()).for_each(|(v, failed)| *v &= !failed);
|
||||
}
|
||||
let scalar = matches!(out.shape(), Shape::Scalar);
|
||||
if let (true, Some((keys, offsets, data))) = (scalar, out.codes()) {
|
||||
let mut valid = valid.unwrap_or_else(|| Bits::ones(rows));
|
||||
keys.iter().enumerate().filter(|(_, code)| **code < 0).for_each(|(row, _)| Bits::set(&mut valid, row, false));
|
||||
let dict = Dict::Text {
|
||||
offsets: offsets.to_vec(),
|
||||
data: data.to_string(),
|
||||
};
|
||||
return Array::coded_with(keys.into(), Rc::new(dict), valid);
|
||||
}
|
||||
let values = match out.kind() {
|
||||
Kind::Num if scalar => {
|
||||
let (mant, scale) = out.take_numbers();
|
||||
Store::Scaled { mant, scale }
|
||||
}
|
||||
Kind::Bool if scalar => Store::Bool(out.take_bits()),
|
||||
Kind::Str if i32::try_from(out.data().len()).is_ok() && std::str::from_utf8(out.data()).is_ok() => {
|
||||
let (offsets, data) = out.take_text();
|
||||
Store::Text {
|
||||
offsets: offsets.into_iter().map(|o| o as i32).collect(),
|
||||
data: String::from_utf8(data).unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
Kind::Dyn if scalar && out.boxed_values().is_empty() => {
|
||||
let failed = out.failed().to_vec();
|
||||
let mut values = out.take_values();
|
||||
values.resize(rows, Variable::Null);
|
||||
for (row, value) in values.iter_mut().enumerate() {
|
||||
if Bits::get(&failed, row) {
|
||||
*value = Variable::Null;
|
||||
}
|
||||
}
|
||||
return Array::from_values(values);
|
||||
}
|
||||
_ => return Self::any(out),
|
||||
};
|
||||
Array { values, valid, rows }
|
||||
}
|
||||
|
||||
pub fn parts(values: Store, valid: Option<Vec<u64>>, rows: usize) -> Array {
|
||||
Array { values, valid, rows }
|
||||
}
|
||||
|
||||
pub fn from_values(values: Vec<Variable>) -> Array {
|
||||
Array {
|
||||
rows: values.len(),
|
||||
values: Store::Any(values),
|
||||
valid: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.rows
|
||||
}
|
||||
|
||||
pub fn store(&self) -> &Store {
|
||||
&self.values
|
||||
}
|
||||
|
||||
pub fn valid(&self, row: usize) -> bool {
|
||||
self.valid.as_deref().is_none_or(|bits| Bits::get(bits, row))
|
||||
}
|
||||
|
||||
pub fn validity_bits(&self) -> Option<&[u64]> {
|
||||
self.valid.as_deref()
|
||||
}
|
||||
|
||||
pub fn column(&self) -> Column<'_> {
|
||||
let values = match &self.values {
|
||||
Store::Scaled { mant, scale } => Values::Scaled { mant, scale },
|
||||
Store::Bool(bits) => Values::Bool { bits, offset: 0 },
|
||||
Store::Text { offsets, data } => Values::Text { offsets, data },
|
||||
Store::Coded { codes, dict } => Values::Dict {
|
||||
keys: codes,
|
||||
values: dict.dictionary(),
|
||||
},
|
||||
Store::Any(values) => Values::Any(values),
|
||||
Store::List { offsets, child } => Values::List {
|
||||
offsets,
|
||||
child: match child.as_ref() {
|
||||
Store::Text { offsets, data } => Dictionary::Text { offsets, data },
|
||||
Store::Scaled { mant, scale } => Dictionary::Scaled { mant, scale },
|
||||
Store::Bool(bits) => Dictionary::Bool { bits },
|
||||
_ => Dictionary::Any(&[]),
|
||||
},
|
||||
},
|
||||
};
|
||||
match &self.valid {
|
||||
Some(bits) => Column::with_validity(values, bits, 0),
|
||||
None => Column::new(values),
|
||||
}
|
||||
}
|
||||
|
||||
fn listed(offsets: &[i32], child: Dictionary, valid: Option<(&[u64], usize)>, positions: &[usize]) -> Option<Store> {
|
||||
let range = |p: usize| -> (usize, usize) {
|
||||
let live = valid.is_none_or(|(bits, at)| Bits::get(bits, at + p));
|
||||
match (live, offsets.get(p), offsets.get(p + 1)) {
|
||||
(true, Some(a), Some(b)) => (*a as usize, (*b as usize).max(*a as usize)),
|
||||
_ => (0, 0),
|
||||
}
|
||||
};
|
||||
let mut out = Vec::with_capacity(positions.len() + 1);
|
||||
out.push(0i32);
|
||||
let child = match child {
|
||||
Dictionary::Text { offsets: co, data } => {
|
||||
let (mut offsets, mut text) = (vec![0i32], String::new());
|
||||
for &p in positions {
|
||||
let (a, b) = range(p);
|
||||
for i in a..b {
|
||||
text.push_str(data.get(*co.get(i)? as usize..*co.get(i + 1)? as usize)?);
|
||||
offsets.push(i32::try_from(text.len()).ok()?);
|
||||
}
|
||||
out.push(i32::try_from(offsets.len() - 1).ok()?);
|
||||
}
|
||||
Store::Text { offsets, data: text }
|
||||
}
|
||||
Dictionary::Scaled { mant, scale } => {
|
||||
let (mut m, mut sc) = (Vec::new(), Vec::new());
|
||||
for &p in positions {
|
||||
let (a, b) = range(p);
|
||||
m.extend_from_slice(mant.get(a..b)?);
|
||||
sc.extend_from_slice(scale.get(a..b)?);
|
||||
out.push(i32::try_from(m.len()).ok()?);
|
||||
}
|
||||
Store::Scaled { mant: m, scale: sc }
|
||||
}
|
||||
Dictionary::Bool { bits } => {
|
||||
let mut flat: Vec<bool> = Vec::new();
|
||||
for &p in positions {
|
||||
let (a, b) = range(p);
|
||||
flat.extend((a..b).map(|i| Bits::get(bits, i)));
|
||||
out.push(i32::try_from(flat.len()).ok()?);
|
||||
}
|
||||
Store::Bool(Bits::of(flat.len(), |i| flat[i]))
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
Some(Store::List { offsets: out, child: Box::new(child) })
|
||||
}
|
||||
|
||||
fn coded_gather(keys: &[i32], values: Dictionary, valid: Option<(&[u64], usize)>, positions: &[usize]) -> Option<Array> {
|
||||
let size = values.column().len();
|
||||
if size > positions.len().max(64) {
|
||||
return None;
|
||||
}
|
||||
let dict = match values {
|
||||
Dictionary::Text { offsets, data } => Dict::Text {
|
||||
offsets: offsets.get(..=size)?.to_vec(),
|
||||
data: data.to_string(),
|
||||
},
|
||||
Dictionary::Scaled { mant, scale } => Dict::Scaled {
|
||||
mant: mant.get(..size)?.to_vec(),
|
||||
scale: scale.get(..size)?.to_vec(),
|
||||
},
|
||||
Dictionary::Bool { bits } => Dict::Bool(bits.to_vec()),
|
||||
_ => return None,
|
||||
};
|
||||
let codes: Rc<[i32]> = positions
|
||||
.iter()
|
||||
.map(|&p| match valid.is_none_or(|(bits, at)| Bits::get(bits, at + p)) {
|
||||
true => keys.get(p).copied().unwrap_or(-1),
|
||||
false => -1,
|
||||
})
|
||||
.collect();
|
||||
Some(Array::coded(codes, Rc::new(dict)))
|
||||
}
|
||||
|
||||
pub fn gather(column: &Column, positions: &[usize]) -> Array {
|
||||
let rows = positions.len();
|
||||
if let Values::Dict { keys, values } = column.values {
|
||||
if let Some(array) = Self::coded_gather(keys, values, column.validity, positions) {
|
||||
return array;
|
||||
}
|
||||
}
|
||||
if let Values::List { offsets, child } = column.values {
|
||||
if let Some(values) = Self::listed(offsets, child, column.validity, positions) {
|
||||
let valid = column
|
||||
.validity
|
||||
.map(|(bits, offset)| Bits::of(rows, |i| Bits::get(bits, offset + positions[i])));
|
||||
return Array { values, valid, rows };
|
||||
}
|
||||
}
|
||||
let valid = column
|
||||
.validity
|
||||
.filter(|(bits, offset)| !Bits::full(bits, *offset, positions.iter().max().map_or(0, |m| m + 1)))
|
||||
.map(|(bits, offset)| Bits::of(rows, |i| Bits::get(bits, offset + positions[i])));
|
||||
let values = match column.values {
|
||||
Values::Scaled { mant, scale } => Store::Scaled {
|
||||
mant: positions.iter().map(|&p| mant.get(p).copied().unwrap_or(0)).collect(),
|
||||
scale: positions.iter().map(|&p| scale.get(p).copied().unwrap_or(0)).collect(),
|
||||
},
|
||||
Values::I64(values) => Store::Scaled {
|
||||
mant: positions.iter().map(|&p| values.get(p).copied().unwrap_or(0)).collect(),
|
||||
scale: vec![0; rows],
|
||||
},
|
||||
Values::Dec(values) => {
|
||||
let mut mant = Vec::with_capacity(rows);
|
||||
let mut scale = Vec::with_capacity(rows);
|
||||
let parts = positions.iter().try_for_each(|&p| {
|
||||
let (m, s) = match values.get(p) {
|
||||
Some(d) => (i64::try_from(d.mantissa()).ok()?, u8::try_from(d.scale()).ok()?),
|
||||
None => (0, 0),
|
||||
};
|
||||
mant.push(m);
|
||||
scale.push(s);
|
||||
Some(())
|
||||
});
|
||||
match parts {
|
||||
Some(()) => Store::Scaled { mant, scale },
|
||||
None => {
|
||||
return Array {
|
||||
values: Store::Any(positions.iter().map(|&p| column.variable(p)).collect()),
|
||||
valid: None,
|
||||
rows,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Values::Bool { bits, offset } => Store::Bool(Bits::of(rows, |i| Bits::get(bits, offset + positions[i]))),
|
||||
Values::Text { .. } | Values::Utf8 { .. } => {
|
||||
let mut offsets = Vec::with_capacity(rows + 1);
|
||||
let mut data = String::new();
|
||||
offsets.push(0);
|
||||
for &p in positions {
|
||||
match column.text(p) {
|
||||
Some(text) => data.push_str(text),
|
||||
None if !column.valid(p) => {}
|
||||
None => {
|
||||
return Array {
|
||||
values: Store::Any(positions.iter().map(|&p| column.variable(p)).collect()),
|
||||
valid: None,
|
||||
rows,
|
||||
}
|
||||
}
|
||||
}
|
||||
offsets.push(data.len() as i32);
|
||||
}
|
||||
Store::Text { offsets, data }
|
||||
}
|
||||
_ => {
|
||||
return Array {
|
||||
values: Store::Any(positions.iter().map(|&p| column.variable(p)).collect()),
|
||||
valid: None,
|
||||
rows,
|
||||
}
|
||||
}
|
||||
};
|
||||
Array { values, valid, rows }
|
||||
}
|
||||
|
||||
pub fn coded(codes: Rc<[i32]>, dict: Rc<Dict>) -> Array {
|
||||
Array {
|
||||
rows: codes.len(),
|
||||
values: Store::Coded { codes, dict },
|
||||
valid: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn broadcast(&self, rows: usize) -> Option<Array> {
|
||||
let valid = self.valid.as_deref().is_none_or(|bits| Bits::get(bits, 0));
|
||||
let (dict, code) = match &self.values {
|
||||
Store::List { .. } => return Some(Self::gather(&self.column(), &vec![0; rows])),
|
||||
Store::Scaled { mant, scale } => (
|
||||
Rc::new(Dict::Scaled {
|
||||
mant: vec![*mant.first()?],
|
||||
scale: vec![*scale.first()?],
|
||||
}),
|
||||
0,
|
||||
),
|
||||
Store::Bool(bits) => (Rc::new(Dict::Bool(vec![bits.first()? & 1])), 0),
|
||||
Store::Text { offsets, data } => {
|
||||
let text = data.get(*offsets.first()? as usize..*offsets.get(1)? as usize)?;
|
||||
(
|
||||
Rc::new(Dict::Text {
|
||||
offsets: vec![0, i32::try_from(text.len()).ok()?],
|
||||
data: text.to_string(),
|
||||
}),
|
||||
0,
|
||||
)
|
||||
}
|
||||
Store::Coded { codes, dict } => (dict.clone(), *codes.first()?),
|
||||
Store::Any(values) => match values.first()? {
|
||||
Variable::Array(items)
|
||||
if items
|
||||
.borrow()
|
||||
.iter()
|
||||
.all(|item| matches!(item, Variable::Null | Variable::Bool(_) | Variable::Number(_) | Variable::String(_))) =>
|
||||
{
|
||||
(Rc::new(Dict::Any(vec![values.first()?.clone()])), 0)
|
||||
}
|
||||
value @ (Variable::Array(_) | Variable::Object(_) | Variable::Dynamic(_)) => {
|
||||
return Some(Array {
|
||||
rows,
|
||||
values: Store::Any((0..rows).map(|_| if valid { value.deep_clone() } else { Variable::Null }).collect()),
|
||||
valid: None,
|
||||
})
|
||||
}
|
||||
value => (Rc::new(Dict::Any(vec![value.clone()])), 0),
|
||||
},
|
||||
};
|
||||
let code = if valid { code } else { -1 };
|
||||
Some(Array {
|
||||
rows,
|
||||
values: Store::Coded {
|
||||
codes: vec![code; rows].into(),
|
||||
dict,
|
||||
},
|
||||
valid: None,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn coded_with(codes: Rc<[i32]>, dict: Rc<Dict>, valid: Vec<u64>) -> Array {
|
||||
Array {
|
||||
rows: codes.len(),
|
||||
values: Store::Coded { codes, dict },
|
||||
valid: Some(valid),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expand(&self, ids: &[u32]) -> Array {
|
||||
let code = |id: u32| match self.valid(id as usize) {
|
||||
true => id as i32,
|
||||
false => -1,
|
||||
};
|
||||
let shared = |value: &Variable| match value {
|
||||
Variable::Array(items) => items
|
||||
.borrow()
|
||||
.iter()
|
||||
.all(|item| matches!(item, Variable::Null | Variable::Bool(_) | Variable::Number(_) | Variable::String(_))),
|
||||
Variable::Object(_) | Variable::Dynamic(_) => false,
|
||||
_ => true,
|
||||
};
|
||||
let dict = match &self.values {
|
||||
Store::Scaled { mant, scale } => Dict::Scaled {
|
||||
mant: mant.clone(),
|
||||
scale: scale.clone(),
|
||||
},
|
||||
Store::Text { offsets, data } => Dict::Text {
|
||||
offsets: offsets.clone(),
|
||||
data: data.clone(),
|
||||
},
|
||||
Store::Coded { codes, dict } => {
|
||||
let codes = ids
|
||||
.iter()
|
||||
.map(|&id| match self.valid(id as usize) {
|
||||
true => codes.get(id as usize).copied().unwrap_or(-1),
|
||||
false => -1,
|
||||
})
|
||||
.collect();
|
||||
return Array::coded(codes, dict.clone());
|
||||
}
|
||||
Store::Any(values) if values.iter().all(shared) => Dict::Any(values.clone()),
|
||||
Store::Any(values) => {
|
||||
let values = ids
|
||||
.iter()
|
||||
.map(|&id| match (self.valid(id as usize), values.get(id as usize)) {
|
||||
(true, Some(value)) => value.deep_clone(),
|
||||
_ => Variable::Null,
|
||||
})
|
||||
.collect();
|
||||
return Array::from_values(values);
|
||||
}
|
||||
Store::Bool(_) | Store::List { .. } => {
|
||||
let positions: Vec<usize> = ids.iter().map(|&id| id as usize).collect();
|
||||
return self.pick(&positions);
|
||||
}
|
||||
};
|
||||
Array::coded(ids.iter().map(|&id| code(id)).collect(), Rc::new(dict))
|
||||
}
|
||||
|
||||
pub fn pick(&self, positions: &[usize]) -> Array {
|
||||
match &self.values {
|
||||
Store::Coded { codes, dict } => Array {
|
||||
values: Store::Coded {
|
||||
codes: positions.iter().map(|&p| codes[p]).collect(),
|
||||
dict: dict.clone(),
|
||||
},
|
||||
valid: self.valid.as_deref().map(|bits| Bits::gather(bits, positions)),
|
||||
rows: positions.len(),
|
||||
},
|
||||
Store::Any(values) => Array {
|
||||
values: Store::Any(positions.iter().map(|&p| values[p].clone()).collect()),
|
||||
valid: self.valid.as_deref().map(|bits| Bits::gather(bits, positions)),
|
||||
rows: positions.len(),
|
||||
},
|
||||
_ => Self::gather(&self.column(), positions),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
use zen_expression::lane::Column;
|
||||
|
||||
pub(crate) struct Bits;
|
||||
|
||||
impl Bits {
|
||||
#[inline]
|
||||
pub fn get(bits: &[u64], row: usize) -> bool {
|
||||
bits.get(row >> 6).is_some_and(|w| w >> (row & 63) & 1 == 1)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set(bits: &mut [u64], row: usize, on: bool) {
|
||||
if let Some(word) = bits.get_mut(row >> 6) {
|
||||
*word = (*word & !(1 << (row & 63))) | (u64::from(on) << (row & 63));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ones(rows: usize) -> Vec<u64> {
|
||||
let mut bits = vec![u64::MAX; rows.div_ceil(64)];
|
||||
Self::trim(&mut bits, rows);
|
||||
bits
|
||||
}
|
||||
|
||||
pub fn trim(bits: &mut [u64], rows: usize) {
|
||||
if let (Some(last), tail @ 1..) = (bits.last_mut(), rows & 63) {
|
||||
*last &= (1u64 << tail) - 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn of(rows: usize, mut f: impl FnMut(usize) -> bool) -> Vec<u64> {
|
||||
let mut bits = vec![0u64; rows.div_ceil(64)];
|
||||
for (w, word) in bits.iter_mut().enumerate() {
|
||||
let base = w << 6;
|
||||
let mut value = 0u64;
|
||||
for bit in 0..(rows - base).min(64) {
|
||||
value |= u64::from(f(base + bit)) << bit;
|
||||
}
|
||||
*word = value;
|
||||
}
|
||||
bits
|
||||
}
|
||||
|
||||
pub fn window(bits: &[u64], offset: usize, rows: usize) -> Vec<u64> {
|
||||
match offset {
|
||||
0 if bits.len() >= rows.div_ceil(64) => {
|
||||
let mut out = bits[..rows.div_ceil(64)].to_vec();
|
||||
Self::trim(&mut out, rows);
|
||||
out
|
||||
}
|
||||
_ => (0..rows.div_ceil(64))
|
||||
.map(|w| Column::word(bits, offset + (w << 6), (rows - (w << 6)).min(64)))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gather(bits: &[u64], positions: &[usize]) -> Vec<u64> {
|
||||
Self::of(positions.len(), |i| Self::get(bits, positions[i]))
|
||||
}
|
||||
|
||||
pub fn fill(bits: &mut [u64], start: usize, len: usize) {
|
||||
let end = start + len;
|
||||
let mut at = start;
|
||||
while at < end {
|
||||
let (word, bit) = (at >> 6, at & 63);
|
||||
let take = (64 - bit).min(end - at);
|
||||
let mask = match take {
|
||||
64 => u64::MAX,
|
||||
t => ((1u64 << t) - 1) << bit,
|
||||
};
|
||||
if let Some(w) = bits.get_mut(word) {
|
||||
*w |= mask;
|
||||
}
|
||||
at += take;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn splice(bits: &mut [u64], start: usize, source: &[u64], len: usize) {
|
||||
let mut at = 0;
|
||||
while at < len {
|
||||
let take = (len - at).min(64);
|
||||
let word = Column::word(source, at, take);
|
||||
let (dst, bit) = ((start + at) >> 6, (start + at) & 63);
|
||||
if let Some(w) = bits.get_mut(dst) {
|
||||
*w |= word << bit;
|
||||
}
|
||||
if bit > 0 {
|
||||
if let Some(w) = bits.get_mut(dst + 1) {
|
||||
*w |= word >> (64 - bit);
|
||||
}
|
||||
}
|
||||
at += take;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn full(bits: &[u64], offset: usize, len: usize) -> bool {
|
||||
let (mut at, end) = (offset, offset + len);
|
||||
while at < end {
|
||||
let Some(word) = bits.get(at >> 6) else {
|
||||
return false;
|
||||
};
|
||||
let (shift, take) = (at & 63, (64 - (at & 63)).min(end - at));
|
||||
let mask = match take {
|
||||
64 => u64::MAX,
|
||||
_ => (1u64 << take) - 1,
|
||||
};
|
||||
if (word >> shift) & mask != mask {
|
||||
return false;
|
||||
}
|
||||
at += take;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
pub fn rows(bits: &[u64]) -> impl Iterator<Item = usize> + '_ {
|
||||
bits.iter().enumerate().flat_map(|(w, &word)| {
|
||||
let mut word = word;
|
||||
std::iter::from_fn(move || {
|
||||
(word != 0).then(|| {
|
||||
let at = word.trailing_zeros() as usize;
|
||||
word &= word - 1;
|
||||
w * 64 + at
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
pub fn none(bits: &[u64]) -> bool {
|
||||
bits.iter().all(|w| *w == 0)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,285 @@
|
||||
use zen_expression::lane::{Column, Dictionary, Values};
|
||||
use zen_expression::Variable;
|
||||
use super::{Array, Bits, Literal, Store};
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct ColumnBuilder {
|
||||
kind: Option<u8>,
|
||||
mant: Vec<i64>,
|
||||
scale: Vec<u8>,
|
||||
bits: Vec<u64>,
|
||||
offsets: Vec<i32>,
|
||||
data: String,
|
||||
any: Vec<Variable>,
|
||||
valid: Vec<u64>,
|
||||
nulls: bool,
|
||||
rows: usize,
|
||||
capacity: usize,
|
||||
}
|
||||
|
||||
impl ColumnBuilder {
|
||||
const NUM: u8 = 0;
|
||||
const BOOL: u8 = 1;
|
||||
const TEXT: u8 = 2;
|
||||
const ANY: u8 = 3;
|
||||
|
||||
pub fn with_capacity(rows: usize) -> Self {
|
||||
Self {
|
||||
valid: Vec::with_capacity(rows.div_ceil(64)),
|
||||
capacity: rows,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn null_at(column: &Column, row: usize) -> bool {
|
||||
if !column.valid(row) {
|
||||
return true;
|
||||
}
|
||||
match column.values {
|
||||
Values::Any(values) => values.get(row).is_none_or(|v| matches!(v, Variable::Null)),
|
||||
Values::Dict { values, .. } => column.code(row).is_none_or(|code| {
|
||||
!values.valid(code)
|
||||
|| matches!(values, Dictionary::Any(v) if v.get(code).is_none_or(|v| matches!(v, Variable::Null)))
|
||||
}),
|
||||
Values::Dec(_) | Values::Scaled { .. } | Values::I64(_) | Values::F64(_) => column.number(row).is_none(),
|
||||
Values::Bool { .. } | Values::List { .. } => false,
|
||||
_ => column.text(row).is_none(),
|
||||
}
|
||||
}
|
||||
|
||||
fn open(&mut self) -> usize {
|
||||
let row = self.rows;
|
||||
self.rows += 1;
|
||||
if row.is_multiple_of(64) {
|
||||
self.valid.push(0);
|
||||
if self.kind == Some(Self::BOOL) {
|
||||
self.bits.push(0);
|
||||
}
|
||||
}
|
||||
row
|
||||
}
|
||||
|
||||
fn fill(&mut self) {
|
||||
match self.kind {
|
||||
Some(Self::NUM) => {
|
||||
self.mant.push(0);
|
||||
self.scale.push(0);
|
||||
}
|
||||
Some(Self::TEXT) => self.offsets.push(self.data.len() as i32),
|
||||
Some(Self::ANY) => self.any.push(Variable::Null),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn start(&mut self, kind: u8) {
|
||||
let rows = self.rows;
|
||||
let capacity = self.capacity.max(rows);
|
||||
self.kind = Some(kind);
|
||||
match kind {
|
||||
Self::NUM => {
|
||||
self.mant = Vec::with_capacity(capacity);
|
||||
self.mant.resize(rows, 0);
|
||||
self.scale = Vec::with_capacity(capacity);
|
||||
self.scale.resize(rows, 0);
|
||||
}
|
||||
Self::BOOL => self.bits = vec![0; rows.div_ceil(64)],
|
||||
Self::TEXT => self.offsets = vec![0; rows + 1],
|
||||
_ => self.any = vec![Variable::Null; rows],
|
||||
}
|
||||
}
|
||||
|
||||
fn degrade(&mut self) {
|
||||
let typed = Array {
|
||||
values: match self.kind {
|
||||
Some(Self::NUM) => Store::Scaled {
|
||||
mant: std::mem::take(&mut self.mant),
|
||||
scale: std::mem::take(&mut self.scale),
|
||||
},
|
||||
Some(Self::BOOL) => Store::Bool(std::mem::take(&mut self.bits)),
|
||||
Some(Self::TEXT) => Store::Text {
|
||||
offsets: std::mem::take(&mut self.offsets),
|
||||
data: std::mem::take(&mut self.data),
|
||||
},
|
||||
_ => Store::Any(Vec::new()),
|
||||
},
|
||||
valid: Some(self.valid.clone()),
|
||||
rows: self.rows,
|
||||
};
|
||||
let column = typed.column();
|
||||
self.any = (0..self.rows).map(|row| column.variable(row)).collect();
|
||||
self.kind = Some(Self::ANY);
|
||||
}
|
||||
|
||||
pub fn push_cell(&mut self, column: &Column, row: usize) {
|
||||
if !column.valid(row) {
|
||||
return self.push_null();
|
||||
}
|
||||
match column.values {
|
||||
Values::Scaled { mant, scale } => match (mant.get(row), scale.get(row)) {
|
||||
(Some(m), Some(s)) => self.push_scaled(*m, *s),
|
||||
_ => self.push_null(),
|
||||
},
|
||||
Values::I64(values) => match values.get(row) {
|
||||
Some(v) => self.push_scaled(*v, 0),
|
||||
None => self.push_null(),
|
||||
},
|
||||
Values::Dec(values) => match values.get(row) {
|
||||
Some(d) => match (i64::try_from(d.mantissa()), u8::try_from(d.scale())) {
|
||||
(Ok(m), Ok(s)) => self.push_scaled(m, s),
|
||||
_ => self.push_variable(Variable::Number(*d)),
|
||||
},
|
||||
None => self.push_null(),
|
||||
},
|
||||
Values::Bool { .. } => match column.boolean(row) {
|
||||
Some(b) => self.push_bool(b),
|
||||
None => self.push_null(),
|
||||
},
|
||||
Values::Text { .. } | Values::Utf8 { .. } => match column.text(row) {
|
||||
Some(text) => self.push_text(text),
|
||||
None => self.push_null(),
|
||||
},
|
||||
Values::Dict { keys, values } => match keys.get(row).and_then(|k| usize::try_from(*k).ok()) {
|
||||
None => self.push_null(),
|
||||
Some(code) => match values {
|
||||
Dictionary::Scaled { mant, scale } => match (mant.get(code), scale.get(code)) {
|
||||
(Some(m), Some(s)) => self.push_scaled(*m, *s),
|
||||
_ => self.push_null(),
|
||||
},
|
||||
Dictionary::Text { offsets, data } => {
|
||||
let text = offsets
|
||||
.get(code)
|
||||
.zip(offsets.get(code + 1))
|
||||
.and_then(|(a, b)| data.get(*a as usize..*b as usize));
|
||||
match text {
|
||||
Some(text) => self.push_text(text),
|
||||
None => self.push_null(),
|
||||
}
|
||||
}
|
||||
Dictionary::Bool { bits } => self.push_bool(Bits::get(bits, code)),
|
||||
Dictionary::Any(values) => self.push_variable(values.get(code).cloned().unwrap_or(Variable::Null)),
|
||||
Dictionary::Column(inner) => self.push_cell(inner, code),
|
||||
},
|
||||
},
|
||||
Values::Any(values) => self.push_variable(values.get(row).cloned().unwrap_or(Variable::Null)),
|
||||
_ => self.push_variable(column.variable(row)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_scaled(&mut self, mant: i64, scale: u8) {
|
||||
if self.kind.is_none() {
|
||||
self.start(Self::NUM);
|
||||
}
|
||||
if self.kind != Some(Self::NUM) {
|
||||
return self.push_variable(Variable::Number(rust_decimal::Decimal::new(mant, scale as u32)));
|
||||
}
|
||||
let row = self.open();
|
||||
self.mark(row);
|
||||
self.mant.push(mant);
|
||||
self.scale.push(scale);
|
||||
}
|
||||
|
||||
pub fn push_text(&mut self, text: &str) {
|
||||
if self.kind.is_none() {
|
||||
self.start(Self::TEXT);
|
||||
}
|
||||
if self.kind != Some(Self::TEXT) {
|
||||
return self.push_variable(Variable::String(text.into()));
|
||||
}
|
||||
let row = self.open();
|
||||
self.mark(row);
|
||||
self.data.push_str(text);
|
||||
self.offsets.push(self.data.len() as i32);
|
||||
}
|
||||
|
||||
pub fn push_null(&mut self) {
|
||||
self.open();
|
||||
self.nulls = true;
|
||||
self.fill();
|
||||
}
|
||||
|
||||
fn mark(&mut self, row: usize) {
|
||||
if let Some(word) = self.valid.get_mut(row / 64) {
|
||||
*word |= 1 << (row % 64);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_literal(&mut self, literal: &Literal) {
|
||||
match literal {
|
||||
Literal::Null => self.push_null(),
|
||||
Literal::Array(_) | Literal::Object(_) => self.push_any(literal.variable()),
|
||||
Literal::Num(n) => match (i64::try_from(n.mantissa()), u8::try_from(n.scale())) {
|
||||
(Ok(m), Ok(s)) => self.push_scaled(m, s),
|
||||
_ => self.push_any(Variable::Number(*n)),
|
||||
},
|
||||
Literal::Str(text) => self.push_text(text),
|
||||
Literal::Bool(b) => self.push_bool(*b),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_any(&mut self, value: Variable) {
|
||||
match self.kind {
|
||||
None => self.start(Self::ANY),
|
||||
Some(Self::ANY) => {}
|
||||
Some(_) => self.degrade(),
|
||||
}
|
||||
let row = self.open();
|
||||
self.mark(row);
|
||||
self.any.push(value);
|
||||
}
|
||||
|
||||
pub fn push_variable(&mut self, value: Variable) {
|
||||
let typed = |kind: u8| self.kind.is_none_or(|k| k == kind);
|
||||
match &value {
|
||||
Variable::Null => return self.push_null(),
|
||||
Variable::Number(n) if typed(Self::NUM) => {
|
||||
if let (Ok(m), Ok(s)) = (i64::try_from(n.mantissa()), u8::try_from(n.scale())) {
|
||||
return self.push_scaled(m, s);
|
||||
}
|
||||
}
|
||||
Variable::String(text) if typed(Self::TEXT) => return self.push_text(text),
|
||||
Variable::Bool(b) if typed(Self::BOOL) => return self.push_bool(*b),
|
||||
_ => {}
|
||||
}
|
||||
self.push_any(value)
|
||||
}
|
||||
|
||||
pub fn push_bool(&mut self, b: bool) {
|
||||
if self.kind.is_none() {
|
||||
self.start(Self::BOOL);
|
||||
}
|
||||
if self.kind != Some(Self::BOOL) {
|
||||
return self.push_any(Variable::Bool(b));
|
||||
}
|
||||
let row = self.open();
|
||||
self.mark(row);
|
||||
if b {
|
||||
if let Some(word) = self.bits.get_mut(row / 64) {
|
||||
*word |= 1 << (row % 64);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn finish(mut self) -> Array {
|
||||
let values = match self.kind {
|
||||
Some(Self::NUM) => Store::Scaled {
|
||||
mant: self.mant,
|
||||
scale: self.scale,
|
||||
},
|
||||
Some(Self::BOOL) => Store::Bool(self.bits),
|
||||
Some(Self::TEXT) => Store::Text {
|
||||
offsets: self.offsets,
|
||||
data: self.data,
|
||||
},
|
||||
Some(_) => Store::Any(self.any),
|
||||
None => {
|
||||
self.nulls = true;
|
||||
Store::Any(vec![Variable::Null; self.rows])
|
||||
}
|
||||
};
|
||||
Array {
|
||||
values,
|
||||
valid: self.nulls.then_some(self.valid),
|
||||
rows: self.rows,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,662 @@
|
||||
use std::cell::OnceCell;
|
||||
use std::rc::Rc;
|
||||
use zen_expression::lane::{Column, Dictionary, Values};
|
||||
use zen_expression::Variable;
|
||||
use super::{Array, Bits, ColumnBuilder, Dict, Input, Items, Literal, Masked, Passed, Picked, Records, Scattered, Stitched, Store, Templated};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) enum Leaf<'a> {
|
||||
Scattered(Rc<Scattered<'a>>),
|
||||
Masked(Rc<Masked<'a>>),
|
||||
Input(Rc<Input<'a>>),
|
||||
Any(Rc<[Variable]>),
|
||||
Typed(Rc<Array>),
|
||||
Picked(Rc<Picked<'a>>),
|
||||
Stitched(Rc<Stitched<'a>>),
|
||||
Records(Rc<Records<'a>>),
|
||||
Templated(Rc<Templated>),
|
||||
}
|
||||
|
||||
impl<'a> Leaf<'a> {
|
||||
pub fn items(&self) -> Option<Items<'a>> {
|
||||
match self {
|
||||
Leaf::Records(records) => Some(Items::Records(records.clone())),
|
||||
Leaf::Input(input) => Self::struct_list(input.column).then(|| Items::Input { column: input.column, mask: None }),
|
||||
Leaf::Masked(masked) => match &masked.inner {
|
||||
Leaf::Input(input) if Self::struct_list(input.column) => Some(Items::Input {
|
||||
column: input.column,
|
||||
mask: Some(masked.bits().clone()),
|
||||
}),
|
||||
Leaf::Records(records) => {
|
||||
let valid: Vec<u64> = records.valid.iter().zip(masked.bits().iter()).map(|(v, m)| v & m).collect();
|
||||
Some(Items::Records(Rc::new(Records::new(records.rows, records.offsets.clone(), valid, records.fields.clone(), records.nulled.clone()))))
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn records(&self) -> Option<Rc<Records<'a>>> {
|
||||
match self {
|
||||
Leaf::Records(records) => Some(records.clone()),
|
||||
Leaf::Picked(picked) => {
|
||||
let records = picked.source.records()?;
|
||||
match picked.dense.get_or_init(|| Leaf::Records(Rc::new(records.pick(&picked.positions)))) {
|
||||
Leaf::Records(records) => Some(records.clone()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
Leaf::Scattered(scattered) if scattered.inner.records().is_some() => match scattered.dense.get_or_init(|| Self::spread(scattered)) {
|
||||
Leaf::Records(records) => Some(records.clone()),
|
||||
_ => None,
|
||||
},
|
||||
Leaf::Masked(masked) if matches!(masked.inner, Leaf::Records(_)) => match self.items() {
|
||||
Some(Items::Records(records)) => Some(records),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn struct_list(column: Column) -> bool {
|
||||
matches!(column.values, Values::List { child: Dictionary::Column(child), .. } if matches!(child.values, Values::Struct { .. }))
|
||||
}
|
||||
|
||||
pub fn typed(array: Array) -> Leaf<'a> {
|
||||
Leaf::Typed(Rc::new(array))
|
||||
}
|
||||
|
||||
pub fn masked(inner: Leaf<'a>, mask: &Rc<[u64]>) -> Leaf<'a> {
|
||||
let rows = inner.len();
|
||||
let trimmed = mask.last().is_none_or(|w| rows.is_multiple_of(64) || w >> (rows % 64) == 0);
|
||||
let bits = OnceCell::new();
|
||||
if inner.all_valid() && trimmed && mask.len() == rows.div_ceil(64) {
|
||||
let _ = bits.set(mask.clone());
|
||||
}
|
||||
Leaf::Masked(Rc::new(Masked { inner, mask: mask.clone(), bits }))
|
||||
}
|
||||
|
||||
fn all_valid(&self) -> bool {
|
||||
match self {
|
||||
Leaf::Input(input) => input.column.validity.is_none(),
|
||||
Leaf::Typed(typed) => typed.valid.is_none(),
|
||||
Leaf::Any(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scattered(inner: Leaf<'a>, positions: Rc<[usize]>, rows: usize) -> Leaf<'a> {
|
||||
Leaf::Scattered(Rc::new(Scattered {
|
||||
inner,
|
||||
positions,
|
||||
rows,
|
||||
dense: OnceCell::new(),
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn coded_dict(&self) -> Option<&Rc<Dict>> {
|
||||
match self {
|
||||
Leaf::Typed(typed) => match &typed.values {
|
||||
Store::Coded { dict, .. } => Some(dict),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scatter_parts(&self) -> Option<(&Leaf<'a>, &Rc<[usize]>)> {
|
||||
match self {
|
||||
Leaf::Scattered(scattered) if scattered.dense.get().is_none() => Some((&scattered.inner, &scattered.positions)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn same(&self, other: &Leaf<'a>) -> bool {
|
||||
match (self, other) {
|
||||
(Leaf::Scattered(a), Leaf::Scattered(b)) => Rc::ptr_eq(a, b),
|
||||
(Leaf::Masked(a), Leaf::Masked(b)) => Rc::ptr_eq(a, b),
|
||||
(Leaf::Input(a), Leaf::Input(b)) => Rc::ptr_eq(a, b),
|
||||
(Leaf::Any(a), Leaf::Any(b)) => Rc::ptr_eq(a, b),
|
||||
(Leaf::Typed(a), Leaf::Typed(b)) => Rc::ptr_eq(a, b),
|
||||
(Leaf::Picked(a), Leaf::Picked(b)) => Rc::ptr_eq(a, b),
|
||||
(Leaf::Stitched(a), Leaf::Stitched(b)) => Rc::ptr_eq(a, b),
|
||||
(Leaf::Records(a), Leaf::Records(b)) => Rc::ptr_eq(a, b),
|
||||
(Leaf::Templated(a), Leaf::Templated(b)) => Rc::ptr_eq(a, b),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn spread(scattered: &Scattered<'a>) -> Leaf<'a> {
|
||||
let (rows, positions) = (scattered.rows, &scattered.positions);
|
||||
if let Some(spread) = scattered.inner.records().and_then(|records| records.spread(positions, rows)) {
|
||||
return Leaf::Records(Rc::new(spread));
|
||||
}
|
||||
if let Leaf::Templated(templated) = &scattered.inner {
|
||||
let mut codes = vec![-1i32; rows];
|
||||
for (at, &row) in positions.iter().enumerate() {
|
||||
if let (Some(slot), Some(code)) = (codes.get_mut(row), templated.codes.get(at)) {
|
||||
*slot = *code;
|
||||
}
|
||||
}
|
||||
return Templated::leaf(codes.into(), templated.templates.clone());
|
||||
}
|
||||
if let Leaf::Typed(typed) = &scattered.inner {
|
||||
if let Store::Coded { codes, dict } = &typed.values {
|
||||
let mut full = vec![-1i32; rows];
|
||||
for (i, &row) in positions.iter().enumerate() {
|
||||
if let (Some(slot), Some(code)) = (full.get_mut(row), codes.get(i)) {
|
||||
*slot = match typed.valid(i) {
|
||||
true => *code,
|
||||
false => -1,
|
||||
};
|
||||
}
|
||||
}
|
||||
return Leaf::typed(Array::coded(full.into(), dict.clone()));
|
||||
}
|
||||
}
|
||||
let column = scattered.inner.column();
|
||||
let mut valid = vec![0u64; rows.div_ceil(64)];
|
||||
for (i, &row) in positions.iter().enumerate() {
|
||||
if column.valid(i) {
|
||||
Bits::set(&mut valid, row, true);
|
||||
}
|
||||
}
|
||||
let values = match column.values {
|
||||
Values::Scaled { mant, scale } => {
|
||||
let (mut m, mut s) = (vec![0i64; rows], vec![0u8; rows]);
|
||||
for (i, &row) in positions.iter().enumerate() {
|
||||
m[row] = mant.get(i).copied().unwrap_or(0);
|
||||
s[row] = scale.get(i).copied().unwrap_or(0);
|
||||
}
|
||||
Store::Scaled { mant: m, scale: s }
|
||||
}
|
||||
Values::Bool { bits, offset } => {
|
||||
let mut out = vec![0u64; rows.div_ceil(64)];
|
||||
for (i, &row) in positions.iter().enumerate() {
|
||||
Bits::set(&mut out, row, Bits::get(bits, offset + i));
|
||||
}
|
||||
Store::Bool(out)
|
||||
}
|
||||
Values::Any(values) => {
|
||||
let mut out = vec![Variable::Null; rows];
|
||||
for (i, &row) in positions.iter().enumerate() {
|
||||
out[row] = values.get(i).cloned().unwrap_or(Variable::Null);
|
||||
}
|
||||
Store::Any(out)
|
||||
}
|
||||
Values::Text { .. } | Values::Utf8 { .. } => {
|
||||
return Self::spread_text(&column, positions, rows).map_or_else(|| Self::spread_cells(&column, positions, rows), Leaf::typed)
|
||||
}
|
||||
_ => return Self::spread_cells(&column, positions, rows),
|
||||
};
|
||||
Leaf::typed(Array {
|
||||
values,
|
||||
valid: Some(valid),
|
||||
rows,
|
||||
})
|
||||
}
|
||||
|
||||
fn spread_text(column: &Column, positions: &[usize], rows: usize) -> Option<Array> {
|
||||
let mut offsets = Vec::with_capacity(rows + 1);
|
||||
let mut data = String::new();
|
||||
let mut valid = vec![0u64; rows.div_ceil(64)];
|
||||
let (mut nulls, mut filled) = (false, false);
|
||||
let mut next = positions.iter().enumerate().peekable();
|
||||
offsets.push(0i32);
|
||||
for row in 0..rows {
|
||||
let text = match next.peek() {
|
||||
Some(&(i, &at)) if at == row => {
|
||||
next.next();
|
||||
match column.valid(i) {
|
||||
true => column.text(i),
|
||||
false => None,
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
match text {
|
||||
Some(text) => {
|
||||
data.push_str(text);
|
||||
valid[row >> 6] |= 1 << (row & 63);
|
||||
filled = true;
|
||||
}
|
||||
None => nulls = true,
|
||||
}
|
||||
offsets.push(data.len() as i32);
|
||||
}
|
||||
filled.then_some(Array {
|
||||
values: Store::Text { offsets, data },
|
||||
valid: nulls.then_some(valid),
|
||||
rows,
|
||||
})
|
||||
}
|
||||
|
||||
fn spread_cells(column: &Column, positions: &[usize], rows: usize) -> Leaf<'a> {
|
||||
let mut builder = ColumnBuilder::with_capacity(rows);
|
||||
let mut next = positions.iter().enumerate().peekable();
|
||||
for row in 0..rows {
|
||||
match next.peek() {
|
||||
Some(&(i, &at)) if at == row => {
|
||||
builder.push_cell(column, i);
|
||||
next.next();
|
||||
}
|
||||
_ => builder.push_null(),
|
||||
}
|
||||
}
|
||||
Leaf::typed(builder.finish())
|
||||
}
|
||||
|
||||
pub fn nulls(rows: usize) -> Leaf<'a> {
|
||||
Leaf::Any((0..rows).map(|_| Variable::Null).collect())
|
||||
}
|
||||
|
||||
pub fn input(column: Column<'a>, rows: usize) -> Leaf<'a> {
|
||||
Leaf::Input(Rc::new(Input {
|
||||
column,
|
||||
rows,
|
||||
valued: OnceCell::new(),
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn passed(&self) -> Option<Passed<'_, 'a>> {
|
||||
match self {
|
||||
Leaf::Input(input) => Some((input, None)),
|
||||
Leaf::Picked(picked) => match &picked.source {
|
||||
Leaf::Input(input) => Some((input, Some(&picked.positions))),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stitched(sources: Vec<Leaf<'a>>, located: Rc<[(usize, usize)]>) -> Leaf<'a> {
|
||||
Leaf::Stitched(Rc::new(Stitched {
|
||||
sources,
|
||||
located,
|
||||
dense: OnceCell::new(),
|
||||
}))
|
||||
}
|
||||
|
||||
fn source(&self, row: usize) -> Option<(&Leaf<'a>, usize)> {
|
||||
match self {
|
||||
Leaf::Stitched(stitched) => {
|
||||
let &(piece, at) = stitched.located.get(row)?;
|
||||
Some((stitched.sources.get(piece)?, at))
|
||||
}
|
||||
Leaf::Picked(picked) => Some((&picked.source, *picked.positions.get(row)?)),
|
||||
_ => Some((self, row)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, row: usize) -> Variable {
|
||||
match self {
|
||||
Leaf::Any(values) => values.get(row).cloned().unwrap_or(Variable::Null),
|
||||
Leaf::Picked(_) | Leaf::Stitched(_) => self.source(row).map_or(Variable::Null, |(leaf, at)| leaf.get(at)),
|
||||
Leaf::Records(records) => records.get(row),
|
||||
Leaf::Templated(templated) => templated.get(row),
|
||||
_ => self.column().variable(row),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn objects(&self) -> bool {
|
||||
if let Leaf::Masked(_) | Leaf::Scattered(_) = self {
|
||||
if let Some(found) = self.plain_objects() {
|
||||
return found;
|
||||
}
|
||||
}
|
||||
match self {
|
||||
Leaf::Any(values) => values.iter().any(|v| matches!(v, Variable::Object(_))),
|
||||
Leaf::Picked(picked) => picked.source.objects(),
|
||||
Leaf::Stitched(stitched) => stitched.sources.iter().any(Leaf::objects),
|
||||
Leaf::Records(records) => records.fields.iter().any(|(key, leaf, _)| key.as_str().starts_with('$') || leaf.objects()),
|
||||
Leaf::Templated(templated) => templated.templates.iter().any(|literal| matches!(literal, Literal::Object(_))),
|
||||
Leaf::Masked(masked) if matches!(masked.inner, Leaf::Records(_)) => masked.inner.objects(),
|
||||
Leaf::Scattered(scattered) if matches!(scattered.inner, Leaf::Records(_)) => scattered.inner.objects(),
|
||||
_ => match self.column().values {
|
||||
Values::Any(values) => values.iter().any(|v| matches!(v, Variable::Object(_))),
|
||||
Values::Dict {
|
||||
values: Dictionary::Any(values),
|
||||
..
|
||||
} => values.iter().any(|v| matches!(v, Variable::Object(_))),
|
||||
_ => false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn plain_objects(&self) -> Option<bool> {
|
||||
match self {
|
||||
_ if self.objectless() => Some(false),
|
||||
Leaf::Typed(_) | Leaf::Input(_) | Leaf::Any(_) => Some(self.objects()),
|
||||
Leaf::Masked(masked) => masked.inner.plain_objects(),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn objectless(&self) -> bool {
|
||||
let plain = |values: &[Variable]| !values.iter().any(|v| matches!(v, Variable::Object(_)));
|
||||
match self {
|
||||
_ if self.scalar() => true,
|
||||
Leaf::Any(values) => plain(values),
|
||||
Leaf::Typed(typed) => match &typed.values {
|
||||
Store::Any(values) => plain(values),
|
||||
Store::Coded { dict, .. } => matches!(&**dict, Dict::Any(values) if plain(values)),
|
||||
_ => false,
|
||||
},
|
||||
Leaf::Input(input) => match input.column.values {
|
||||
Values::Any(values) | Values::Dict { values: Dictionary::Any(values), .. } => plain(values),
|
||||
_ => false,
|
||||
},
|
||||
Leaf::Templated(templated) => !templated.templates.iter().any(|literal| matches!(literal, Literal::Object(_))),
|
||||
Leaf::Masked(masked) => masked.inner.objectless(),
|
||||
Leaf::Scattered(scattered) => scattered.inner.objectless(),
|
||||
Leaf::Picked(picked) => picked.source.objectless(),
|
||||
Leaf::Stitched(stitched) => stitched.sources.iter().all(Leaf::objectless),
|
||||
Leaf::Records(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn scalar(&self) -> bool {
|
||||
match self {
|
||||
Leaf::Typed(typed) => match &typed.values {
|
||||
Store::Scaled { .. } | Store::Bool(_) | Store::Text { .. } => true,
|
||||
Store::Coded { dict, .. } => !matches!(**dict, Dict::Any(_)),
|
||||
Store::Any(_) | Store::List { .. } => false,
|
||||
},
|
||||
Leaf::Masked(masked) => masked.inner.scalar(),
|
||||
Leaf::Scattered(scattered) => scattered.inner.scalar(),
|
||||
Leaf::Picked(picked) => picked.source.scalar(),
|
||||
Leaf::Stitched(stitched) => stitched.sources.iter().all(Leaf::scalar),
|
||||
Leaf::Input(input) => match input.column.values {
|
||||
Values::Dict { values, .. } => !matches!(values, Dictionary::Any(_) | Dictionary::Column(_)),
|
||||
Values::List { .. } | Values::Struct { .. } | Values::Any(_) => false,
|
||||
_ => true,
|
||||
},
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn dense(&self, positions: &[usize]) -> Leaf<'a> {
|
||||
match self {
|
||||
Leaf::Any(values) => Leaf::Any(positions.iter().map(|&i| values[i].clone()).collect()),
|
||||
Leaf::Typed(typed) => Leaf::typed(typed.pick(positions)),
|
||||
Leaf::Input(input) => Leaf::typed(Array::gather(&input.column, positions)),
|
||||
Leaf::Masked(_) | Leaf::Scattered(_) => Leaf::typed(Array::gather(&self.column(), positions)),
|
||||
Leaf::Records(records) => Leaf::Records(Rc::new(records.pick(positions))),
|
||||
Leaf::Templated(templated) => templated.mapped(positions.iter().copied()),
|
||||
Leaf::Picked(picked) => {
|
||||
let composed: Vec<usize> = positions.iter().map(|&i| picked.positions[i]).collect();
|
||||
picked.source.dense(&composed)
|
||||
}
|
||||
Leaf::Stitched(stitched) => {
|
||||
let views: Vec<Column> = stitched.sources.iter().map(Leaf::column).collect();
|
||||
let mut builder = ColumnBuilder::with_capacity(positions.len());
|
||||
for &row in positions {
|
||||
match stitched.located.get(row).and_then(|&(piece, at)| Some((views.get(piece)?, at))) {
|
||||
Some((view, at)) => builder.push_cell(view, at),
|
||||
None => builder.push_null(),
|
||||
}
|
||||
}
|
||||
Leaf::typed(builder.finish())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expand(&self, ids: &[u32]) -> Leaf<'a> {
|
||||
match self {
|
||||
Leaf::Typed(typed) => Leaf::typed(typed.expand(ids)),
|
||||
Leaf::Any(values) => Leaf::typed(Array::from_values(values.to_vec()).expand(ids)),
|
||||
Leaf::Templated(templated) => templated.mapped(ids.iter().map(|&id| id as usize)),
|
||||
other => {
|
||||
let column = other.column();
|
||||
Leaf::typed(Array::from_values((0..other.len()).map(|row| column.variable(row)).collect()).expand(ids))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn regroup(&self, positions: &Rc<[usize]>) -> Leaf<'a> {
|
||||
match self.records() {
|
||||
Some(records) => Leaf::Records(Rc::new(records.pick(positions))),
|
||||
None => self.pick(positions),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pick(&self, positions: &Rc<[usize]>) -> Leaf<'a> {
|
||||
match self {
|
||||
Leaf::Picked(picked) => Leaf::Picked(Rc::new(Picked {
|
||||
source: picked.source.clone(),
|
||||
positions: positions.iter().map(|&i| picked.positions[i]).collect(),
|
||||
dense: OnceCell::new(),
|
||||
})),
|
||||
other => Leaf::Picked(Rc::new(Picked {
|
||||
source: other.clone(),
|
||||
positions: positions.clone(),
|
||||
dense: OnceCell::new(),
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn column(&self) -> Column<'_> {
|
||||
match self {
|
||||
Leaf::Input(input) => input.column,
|
||||
Leaf::Scattered(scattered) => scattered.dense.get_or_init(|| Self::spread(scattered)).column(),
|
||||
Leaf::Masked(masked) => Column {
|
||||
values: masked.inner.column().values,
|
||||
validity: Some((masked.bits(), 0)),
|
||||
},
|
||||
Leaf::Any(values) => Column::new(Values::Any(values)),
|
||||
Leaf::Typed(typed) => typed.column(),
|
||||
Leaf::Picked(picked) => picked
|
||||
.dense
|
||||
.get_or_init(|| picked.source.dense(&picked.positions))
|
||||
.column(),
|
||||
Leaf::Stitched(stitched) => stitched
|
||||
.dense
|
||||
.get_or_init(|| self.dense(&(0..stitched.located.len()).collect::<Vec<_>>()))
|
||||
.column(),
|
||||
Leaf::Records(records) => Column::new(Values::Any(records.dense())),
|
||||
Leaf::Templated(templated) => Column::with_validity(Values::Any(templated.dense()), &templated.valid, 0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn null(&self, row: usize) -> bool {
|
||||
match self {
|
||||
Leaf::Any(values) => values.get(row).is_none_or(|v| matches!(v, Variable::Null)),
|
||||
Leaf::Picked(_) | Leaf::Stitched(_) => self.source(row).is_none_or(|(leaf, at)| leaf.null(at)),
|
||||
Leaf::Records(records) => !records.valid(row),
|
||||
Leaf::Templated(templated) => templated.template(row).is_none(),
|
||||
_ => ColumnBuilder::null_at(&self.column(), row),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn valid_at(&self, row: usize) -> bool {
|
||||
match self {
|
||||
Leaf::Input(input) => input.column.valid(row),
|
||||
Leaf::Masked(masked) => Bits::get(masked.bits(), row),
|
||||
Leaf::Scattered(_) => self.column().valid(row),
|
||||
Leaf::Typed(typed) => typed.valid(row),
|
||||
Leaf::Any(_) | Leaf::Records(_) => true,
|
||||
Leaf::Templated(templated) => templated.template(row).is_some(),
|
||||
Leaf::Picked(_) => self.source(row).is_some_and(|(leaf, at)| leaf.valid_at(at)),
|
||||
Leaf::Stitched(_) => self.column().valid(row),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn validity(&self, rows: usize) -> Vec<u64> {
|
||||
match self {
|
||||
Leaf::Input(input) => match input.column.validity {
|
||||
Some((bits, offset)) => Bits::window(bits, offset, rows),
|
||||
None => Bits::ones(rows),
|
||||
},
|
||||
Leaf::Typed(typed) => match &typed.valid {
|
||||
Some(bits) => Bits::window(bits, 0, rows),
|
||||
None => Bits::ones(rows),
|
||||
},
|
||||
Leaf::Any(_) | Leaf::Records(_) => Bits::ones(rows),
|
||||
Leaf::Templated(templated) => Bits::window(&templated.valid, 0, rows),
|
||||
Leaf::Masked(masked) => Bits::window(masked.bits(), 0, rows),
|
||||
Leaf::Scattered(_) => match self.column().validity {
|
||||
Some((bits, offset)) => Bits::window(bits, offset, rows),
|
||||
None => Bits::ones(rows),
|
||||
},
|
||||
Leaf::Picked(picked) => match picked.dense.get() {
|
||||
Some(dense) => dense.validity(rows),
|
||||
None => Bits::gather(&picked.source.validity(picked.source.len()), &picked.positions),
|
||||
},
|
||||
Leaf::Stitched(_) => match self.column().validity {
|
||||
Some((bits, offset)) => Bits::window(bits, offset, rows),
|
||||
None => Bits::ones(rows),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn valued(&self, rows: usize) -> Vec<u64> {
|
||||
match self {
|
||||
Leaf::Input(input) if input.rows == rows => input
|
||||
.valued
|
||||
.get_or_init(|| Self::column_valued(input.column, rows))
|
||||
.clone(),
|
||||
Leaf::Any(values) => Bits::of(rows, |row| values.get(row).is_some_and(|v| !matches!(v, Variable::Null))),
|
||||
Leaf::Picked(picked) => match picked.dense.get() {
|
||||
Some(dense) => dense.valued(rows),
|
||||
None => Bits::gather(&picked.source.valued(picked.source.len()), &picked.positions),
|
||||
},
|
||||
Leaf::Stitched(stitched) => {
|
||||
let parts: Vec<Vec<u64>> = stitched.sources.iter().map(|leaf| leaf.valued(leaf.len())).collect();
|
||||
Bits::of(rows, |row| stitched.located.get(row).is_some_and(|&(piece, at)| parts.get(piece).is_some_and(|bits| Bits::get(bits, at))))
|
||||
}
|
||||
Leaf::Records(_) | Leaf::Templated(_) => Bits::of(rows, |row| !self.null(row)),
|
||||
Leaf::Typed(typed) if matches!(typed.values, Store::Coded { ref dict, .. } if !matches!(**dict, Dict::Any(_))) => {
|
||||
let Store::Coded { codes, .. } = &typed.values else {
|
||||
return Bits::of(rows, |row| !self.null(row));
|
||||
};
|
||||
let mut bits: Vec<u64> = codes
|
||||
.get(..rows)
|
||||
.unwrap_or(codes)
|
||||
.chunks(64)
|
||||
.map(|chunk| chunk.iter().enumerate().fold(0u64, |word, (at, c)| word | (u64::from(*c >= 0) << at)))
|
||||
.collect();
|
||||
bits.resize(rows.div_ceil(64), 0);
|
||||
if let Some(valid) = &typed.valid {
|
||||
bits.iter_mut().zip(valid).for_each(|(b, v)| *b &= v);
|
||||
}
|
||||
bits
|
||||
}
|
||||
_ => Self::column_valued(self.column(), rows),
|
||||
}
|
||||
}
|
||||
|
||||
fn column_valued(column: Column, rows: usize) -> Vec<u64> {
|
||||
match column.values {
|
||||
Values::Dict { keys, values } => {
|
||||
let size = values.column().len();
|
||||
if size > rows {
|
||||
return Bits::of(rows, |row| !ColumnBuilder::null_at(&column, row));
|
||||
}
|
||||
let present: Vec<bool> = (0..size)
|
||||
.map(|code| {
|
||||
values.valid(code)
|
||||
&& !matches!(values, Dictionary::Any(v) if v.get(code).is_none_or(|v| matches!(v, Variable::Null)))
|
||||
})
|
||||
.collect();
|
||||
let mut bits = Bits::of(rows, |row| {
|
||||
keys.get(row)
|
||||
.and_then(|k| usize::try_from(*k).ok())
|
||||
.and_then(|code| present.get(code))
|
||||
.is_some_and(|p| *p)
|
||||
});
|
||||
if let Some((valid, offset)) = column.validity {
|
||||
bits.iter_mut().zip(Bits::window(valid, offset, rows)).for_each(|(b, v)| *b &= v);
|
||||
}
|
||||
bits
|
||||
}
|
||||
Values::Text { offsets, data } => {
|
||||
let bytes = data.as_bytes();
|
||||
let boundary = |o: &i32| usize::try_from(*o).is_ok_and(|o| o == bytes.len() || bytes.get(o).is_some_and(|b| (*b as i8) >= -0x40));
|
||||
let formed = offsets
|
||||
.get(..=rows)
|
||||
.is_some_and(|window| window.windows(2).all(|w| w[0] <= w[1]) && window.iter().all(boundary));
|
||||
if formed {
|
||||
return match column.validity {
|
||||
None => Bits::ones(rows),
|
||||
Some((bits, offset)) => Bits::window(bits, offset, rows),
|
||||
};
|
||||
}
|
||||
let mut bits = Bits::of(rows, |row| {
|
||||
offsets
|
||||
.get(row)
|
||||
.zip(offsets.get(row + 1))
|
||||
.is_some_and(|(a, b)| data.get(*a as usize..*b as usize).is_some())
|
||||
});
|
||||
if let Some((valid, offset)) = column.validity {
|
||||
bits.iter_mut().zip(Bits::window(valid, offset, rows)).for_each(|(b, v)| *b &= v);
|
||||
}
|
||||
bits
|
||||
}
|
||||
Values::Any(_) | Values::List { .. } | Values::Strs(_) | Values::Utf8 { .. } | Values::LargeUtf8 { .. } => {
|
||||
Bits::of(rows, |row| !ColumnBuilder::null_at(&column, row))
|
||||
}
|
||||
_ => match column.validity {
|
||||
None => Bits::ones(rows),
|
||||
Some((bits, offset)) => Bits::window(bits, offset, rows),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn truthy(&self, row: usize) -> bool {
|
||||
match self {
|
||||
Leaf::Any(values) => matches!(values.get(row), Some(Variable::Bool(true))),
|
||||
Leaf::Picked(_) | Leaf::Stitched(_) => self.source(row).is_some_and(|(leaf, at)| leaf.truthy(at)),
|
||||
Leaf::Records(_) => false,
|
||||
Leaf::Templated(templated) => matches!(templated.template(row), Some(Literal::Bool(true))),
|
||||
_ => {
|
||||
let column = self.column();
|
||||
match column.values {
|
||||
Values::Any(values) => column.valid(row) && matches!(values.get(row), Some(Variable::Bool(true))),
|
||||
_ => column.valid(row) && column.boolean(row) == Some(true),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn truths(&self, rows: usize) -> Vec<u64> {
|
||||
if let Leaf::Scattered(scattered) = self {
|
||||
if scattered.dense.get().is_none() {
|
||||
let inner = scattered.inner.truths(scattered.positions.len());
|
||||
let mut truths = vec![0u64; rows.div_ceil(64)];
|
||||
scattered
|
||||
.positions
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(i, _)| Bits::get(&inner, *i))
|
||||
.for_each(|(_, &row)| Bits::set(&mut truths, row, true));
|
||||
return truths;
|
||||
}
|
||||
}
|
||||
if let Leaf::Typed(_) | Leaf::Input(..) | Leaf::Masked(_) | Leaf::Scattered(_) = self {
|
||||
let column = self.column();
|
||||
if let Values::Bool { bits, offset } = column.values {
|
||||
let mut truths = Bits::window(bits, offset, rows);
|
||||
if let Some((valid, at)) = column.validity {
|
||||
truths.iter_mut().zip(Bits::window(valid, at, rows)).for_each(|(t, v)| *t &= v);
|
||||
}
|
||||
return truths;
|
||||
}
|
||||
}
|
||||
Bits::of(rows, |row| self.truthy(row))
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
match self {
|
||||
Leaf::Input(input) => input.rows,
|
||||
Leaf::Masked(masked) => masked.inner.len(),
|
||||
Leaf::Scattered(scattered) => scattered.rows,
|
||||
Leaf::Any(values) => values.len(),
|
||||
Leaf::Typed(typed) => typed.len(),
|
||||
Leaf::Picked(picked) => picked.positions.len(),
|
||||
Leaf::Stitched(stitched) => stitched.located.len(),
|
||||
Leaf::Records(records) => records.rows,
|
||||
Leaf::Templated(templated) => templated.codes.len(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
use std::rc::Rc;
|
||||
use zen_expression::lane::Dictionary;
|
||||
use zen_expression::Variable;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum Dict {
|
||||
Text { offsets: Vec<i32>, data: String },
|
||||
Scaled { mant: Vec<i64>, scale: Vec<u8> },
|
||||
Bool(Vec<u64>),
|
||||
Any(Vec<Variable>),
|
||||
}
|
||||
|
||||
impl Dict {
|
||||
fn len(&self) -> Option<usize> {
|
||||
match self {
|
||||
Dict::Text { offsets, .. } => Some(offsets.len().saturating_sub(1)),
|
||||
Dict::Scaled { mant, .. } => Some(mant.len()),
|
||||
Dict::Any(values) => Some(values.len()),
|
||||
Dict::Bool(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn concat(dicts: &[&Rc<Dict>]) -> Option<(Rc<Dict>, Vec<i32>)> {
|
||||
if let [single] = dicts {
|
||||
return Some(((*single).clone(), vec![0]));
|
||||
}
|
||||
let mut bases = Vec::with_capacity(dicts.len());
|
||||
let mut total = 0usize;
|
||||
for dict in dicts {
|
||||
bases.push(i32::try_from(total).ok()?);
|
||||
total += dict.len()?;
|
||||
}
|
||||
i32::try_from(total).ok()?;
|
||||
let merged = match dicts.first().map(|d| &***d)? {
|
||||
Dict::Text { .. } => {
|
||||
let mut offsets = vec![0i32];
|
||||
let mut data = String::new();
|
||||
for dict in dicts {
|
||||
let Dict::Text { offsets: o, data: d } = &***dict else {
|
||||
return None;
|
||||
};
|
||||
let base = i32::try_from(data.len()).ok()?;
|
||||
offsets.extend(o.iter().skip(1).map(|x| x + base));
|
||||
data.push_str(d);
|
||||
}
|
||||
i32::try_from(data.len()).ok()?;
|
||||
Dict::Text { offsets, data }
|
||||
}
|
||||
Dict::Scaled { .. } => {
|
||||
let (mut mant, mut scale) = (Vec::with_capacity(total), Vec::with_capacity(total));
|
||||
for dict in dicts {
|
||||
let Dict::Scaled { mant: m, scale: s } = &***dict else {
|
||||
return None;
|
||||
};
|
||||
mant.extend_from_slice(m);
|
||||
scale.extend_from_slice(s);
|
||||
}
|
||||
Dict::Scaled { mant, scale }
|
||||
}
|
||||
Dict::Any(_) => {
|
||||
let mut values = Vec::with_capacity(total);
|
||||
for dict in dicts {
|
||||
let Dict::Any(v) = &***dict else {
|
||||
return None;
|
||||
};
|
||||
values.extend(v.iter().cloned());
|
||||
}
|
||||
Dict::Any(values)
|
||||
}
|
||||
Dict::Bool(_) => return None,
|
||||
};
|
||||
Some((Rc::new(merged), bases))
|
||||
}
|
||||
|
||||
pub(super) fn dictionary(&self) -> Dictionary<'_> {
|
||||
match self {
|
||||
Dict::Text { offsets, data } => Dictionary::Text { offsets, data },
|
||||
Dict::Scaled { mant, scale } => Dictionary::Scaled { mant, scale },
|
||||
Dict::Bool(bits) => Dictionary::Bool { bits },
|
||||
Dict::Any(values) => Dictionary::Any(values),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
use zen_expression::Variable;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) enum Literal {
|
||||
Null,
|
||||
Bool(bool),
|
||||
Num(rust_decimal::Decimal),
|
||||
Str(std::sync::Arc<str>),
|
||||
Array(Vec<Literal>),
|
||||
Object(Vec<(std::sync::Arc<str>, Literal)>),
|
||||
}
|
||||
|
||||
impl Literal {
|
||||
pub fn variable(&self) -> Variable {
|
||||
match self {
|
||||
Literal::Null => Variable::Null,
|
||||
Literal::Bool(b) => Variable::Bool(*b),
|
||||
Literal::Num(n) => Variable::Number(*n),
|
||||
Literal::Str(s) => Variable::String(s.as_ref().into()),
|
||||
Literal::Array(items) => Variable::from_array(items.iter().map(Literal::variable).collect()),
|
||||
Literal::Object(fields) => {
|
||||
let mut map = zen_types::variable::VariableMap::with_capacity(fields.len());
|
||||
for (key, value) in fields {
|
||||
map.insert(key.as_ref().into(), value.variable());
|
||||
}
|
||||
Variable::from_object(map)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deep(&self) -> bool {
|
||||
match self {
|
||||
Literal::Object(_) => true,
|
||||
Literal::Array(items) => items.iter().any(Literal::deep),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scaled(&self) -> Option<(i64, u8)> {
|
||||
match self {
|
||||
Literal::Num(n) => Some((i64::try_from(n.mantissa()).ok()?, u8::try_from(n.scale()).ok()?)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn of(value: &Variable) -> Option<Literal> {
|
||||
match value {
|
||||
Variable::Null => Some(Literal::Null),
|
||||
Variable::Bool(b) => Some(Literal::Bool(*b)),
|
||||
Variable::Number(n) => Some(Literal::Num(*n)),
|
||||
Variable::String(s) => Some(Literal::Str(std::sync::Arc::from(s.as_ref() as &str))),
|
||||
Variable::Array(items) => items.borrow().iter().map(Literal::of).collect::<Option<Vec<_>>>().map(Literal::Array),
|
||||
Variable::Object(fields) => fields
|
||||
.borrow()
|
||||
.iter()
|
||||
.map(|(key, value)| Some((std::sync::Arc::from(key.as_ref() as &str), Literal::of(value)?)))
|
||||
.collect::<Option<Vec<_>>>()
|
||||
.map(Literal::Object),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
mod array;
|
||||
mod bits;
|
||||
mod builder;
|
||||
mod column;
|
||||
mod dict;
|
||||
mod literal;
|
||||
mod records;
|
||||
mod views;
|
||||
|
||||
pub(crate) use array::{Array, Store};
|
||||
pub(crate) use bits::Bits;
|
||||
pub(crate) use builder::ColumnBuilder;
|
||||
pub(crate) use column::Leaf;
|
||||
pub(crate) use dict::Dict;
|
||||
pub(crate) use literal::Literal;
|
||||
pub(crate) use records::{Field, Items, Records};
|
||||
pub(crate) use views::{Input, Masked, Passed, Picked, Scattered, Stitched, Templated};
|
||||
@@ -0,0 +1,345 @@
|
||||
use std::cell::OnceCell;
|
||||
use std::rc::Rc;
|
||||
use zen_types::symbol::Symbol;
|
||||
use zen_types::variable::VariableMap;
|
||||
use zen_expression::lane::{Column, Dictionary, Values};
|
||||
use zen_expression::Variable;
|
||||
use super::{Bits, Dict, Leaf, Store};
|
||||
|
||||
pub(crate) type Field<'a> = (Symbol, Leaf<'a>, Rc<[u64]>);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Records<'a> {
|
||||
pub(super) rows: usize,
|
||||
pub(super) offsets: Vec<i32>,
|
||||
pub(super) valid: Vec<u64>,
|
||||
pub(super) fields: Vec<Field<'a>>,
|
||||
pub(super) nulled: Option<Rc<[u64]>>,
|
||||
dense: OnceCell<Rc<[Variable]>>,
|
||||
lanes: OnceCell<(Vec<Vec<u64>>, Option<Vec<u64>>)>,
|
||||
maker: OnceCell<Option<Maker>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Maker {
|
||||
shapes: std::cell::RefCell<Vec<(u64, Option<Rc<zen_types::variable::Shape>>)>>,
|
||||
cached: Vec<Option<Decoded>>,
|
||||
}
|
||||
|
||||
type Decoded = (Rc<[i32]>, Vec<Variable>);
|
||||
|
||||
impl<'a> Records<'a> {
|
||||
pub const LANE_VIEW: bool = true;
|
||||
|
||||
pub fn spread(&self, positions: &[usize], rows: usize) -> Option<Records<'a>> {
|
||||
if positions.len() != self.rows || positions.windows(2).any(|w| w[0] >= w[1]) || positions.last().is_some_and(|&last| last >= rows) {
|
||||
return None;
|
||||
}
|
||||
let mut offsets = Vec::with_capacity(rows + 1);
|
||||
let mut valid = vec![0u64; rows.div_ceil(64)];
|
||||
offsets.push(0i32);
|
||||
for (at, &row) in positions.iter().enumerate() {
|
||||
let start = *self.offsets.get(at)?;
|
||||
offsets.resize(row + 1, start);
|
||||
offsets.push(*self.offsets.get(at + 1)?);
|
||||
Bits::set(&mut valid, row, Bits::get(&self.valid, at));
|
||||
}
|
||||
let end = self.offsets.last().copied().unwrap_or(0);
|
||||
offsets.resize(rows + 1, end);
|
||||
Some(Records::new(rows, offsets, valid, self.fields.clone(), self.nulled.clone()))
|
||||
}
|
||||
|
||||
pub fn pick(&self, positions: &[usize]) -> Records<'a> {
|
||||
let rows = positions.len();
|
||||
let mut offsets = Vec::with_capacity(rows + 1);
|
||||
let mut valid = vec![0u64; rows.div_ceil(64)];
|
||||
let mut items: Vec<usize> = Vec::new();
|
||||
offsets.push(0i32);
|
||||
for (at, &row) in positions.iter().enumerate() {
|
||||
if let Some((start, end)) = self.range(row) {
|
||||
Bits::set(&mut valid, at, true);
|
||||
items.extend(start..end);
|
||||
}
|
||||
offsets.push(i32::try_from(items.len()).unwrap_or(i32::MAX));
|
||||
}
|
||||
let items: Rc<[usize]> = items.into();
|
||||
let fields = self
|
||||
.fields
|
||||
.iter()
|
||||
.map(|(key, leaf, present)| (key.clone(), leaf.pick(&items), Rc::from(Bits::gather(present, &items))))
|
||||
.collect();
|
||||
let nulled = self.nulled.as_ref().map(|nulled| Rc::from(Bits::gather(nulled, &items)));
|
||||
Records::new(rows, offsets, valid, fields, nulled)
|
||||
}
|
||||
|
||||
pub fn plain(&self) -> bool {
|
||||
self.nulled.is_none()
|
||||
}
|
||||
|
||||
pub fn alive(&self, rows: usize) -> bool {
|
||||
(0..rows.min(self.rows)).any(|row| self.valid(row))
|
||||
}
|
||||
|
||||
pub fn new(rows: usize, offsets: Vec<i32>, valid: Vec<u64>, fields: Vec<Field<'a>>, nulled: Option<Rc<[u64]>>) -> Records<'a> {
|
||||
Records {
|
||||
rows,
|
||||
offsets,
|
||||
valid,
|
||||
fields,
|
||||
nulled,
|
||||
dense: OnceCell::new(),
|
||||
lanes: OnceCell::new(),
|
||||
maker: OnceCell::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn maker(&self) -> Option<&Maker> {
|
||||
self.maker
|
||||
.get_or_init(|| {
|
||||
if self.nulled.is_some() || self.fields.len() > 64 || self.fields.iter().any(|(key, _, _)| key.as_str().contains('.')) {
|
||||
return None;
|
||||
}
|
||||
let cached = self
|
||||
.fields
|
||||
.iter()
|
||||
.map(|(_, leaf, _)| match leaf {
|
||||
Leaf::Typed(array) => match (&array.values, &array.valid) {
|
||||
(Store::Coded { codes, dict }, None) if !matches!(**dict, Dict::Any(_)) => {
|
||||
let values = dict.dictionary();
|
||||
Some((codes.clone(), (0..values.len()).map(|code| values.variable(code)).collect()))
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
Some(Maker {
|
||||
shapes: std::cell::RefCell::new(Vec::new()),
|
||||
cached,
|
||||
})
|
||||
})
|
||||
.as_ref()
|
||||
}
|
||||
|
||||
fn made(&self, maker: &Maker, item: usize) -> Variable {
|
||||
let value = |(at, (_, leaf, _)): (usize, &Field<'a>)| match maker.cached.get(at).and_then(Option::as_ref) {
|
||||
Some((codes, values)) => codes
|
||||
.get(item)
|
||||
.and_then(|&code| usize::try_from(code).ok())
|
||||
.and_then(|code| values.get(code))
|
||||
.cloned()
|
||||
.unwrap_or(Variable::Null),
|
||||
None => leaf.get(item),
|
||||
};
|
||||
let pattern = self.fields.iter().enumerate().fold(0u64, |bits, (at, (_, _, present))| bits | u64::from(Bits::get(present, item)) << at);
|
||||
let found = maker.shapes.borrow().iter().find(|(p, _)| *p == pattern).map(|(_, shape)| shape.clone());
|
||||
let shape = match found {
|
||||
Some(shape) => shape,
|
||||
None => {
|
||||
let mut map = VariableMap::with_capacity(self.fields.len());
|
||||
self.fields.iter().enumerate().filter(|(at, _)| pattern >> at & 1 == 1).for_each(|(_, (key, _, _))| {
|
||||
map.insert(key.clone(), Variable::Null);
|
||||
});
|
||||
let shape = map.shape().filter(|_| map.len() == pattern.count_ones() as usize).cloned();
|
||||
maker.shapes.borrow_mut().push((pattern, shape.clone()));
|
||||
shape
|
||||
}
|
||||
};
|
||||
let present = self.fields.iter().enumerate().filter(|(at, _)| pattern >> at & 1 == 1);
|
||||
match shape {
|
||||
Some(shape) => Variable::from_object(VariableMap::from_shape(shape, present.map(value))),
|
||||
None => {
|
||||
let mut map = VariableMap::with_capacity(self.fields.len());
|
||||
for (at, field) in present {
|
||||
map.insert(field.0.clone(), value((at, field)));
|
||||
}
|
||||
Variable::from_object(map)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn lanes(&self) -> &(Vec<Vec<u64>>, Option<Vec<u64>>) {
|
||||
self.lanes.get_or_init(|| {
|
||||
let count = self.count();
|
||||
let fields = self
|
||||
.fields
|
||||
.iter()
|
||||
.map(|(_, leaf, present)| leaf.validity(count).iter().zip(present.iter()).map(|(v, p)| v & p).collect())
|
||||
.collect();
|
||||
let alive = self.nulled.as_ref().map(|nulled| {
|
||||
let mut bits: Vec<u64> = nulled.iter().map(|w| !w).collect();
|
||||
bits.resize(count.div_ceil(64), u64::MAX);
|
||||
Bits::trim(&mut bits, count);
|
||||
bits
|
||||
});
|
||||
(fields, alive)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn parts(&self) -> Vec<(&str, Column<'_>)> {
|
||||
let (valid, _) = self.lanes();
|
||||
self.fields
|
||||
.iter()
|
||||
.zip(valid)
|
||||
.map(|((key, leaf, _), bits)| {
|
||||
(
|
||||
key.as_str(),
|
||||
Column {
|
||||
values: leaf.column().values,
|
||||
validity: Some((bits.as_slice(), 0)),
|
||||
},
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn item<'c>(&'c self, fields: &'c [(&'c str, Column<'c>)]) -> Column<'c> {
|
||||
let (_, alive) = self.lanes();
|
||||
Column {
|
||||
values: Values::Struct { fields, len: self.count() },
|
||||
validity: alive.as_ref().map(|bits| (bits.as_slice(), 0)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn list<'c>(&'c self, item: &'c Column<'c>) -> Column<'c> {
|
||||
Column {
|
||||
values: Values::List {
|
||||
offsets: &self.offsets,
|
||||
child: Dictionary::Column(item),
|
||||
},
|
||||
validity: Some((&self.valid, 0)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn valid(&self, row: usize) -> bool {
|
||||
Bits::get(&self.valid, row)
|
||||
}
|
||||
|
||||
pub fn range(&self, row: usize) -> Option<(usize, usize)> {
|
||||
match self.valid(row) {
|
||||
true => Some((usize::try_from(*self.offsets.get(row)?).ok()?, usize::try_from(*self.offsets.get(row + 1)?).ok()?)),
|
||||
false => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn count(&self) -> usize {
|
||||
self.offsets.last().and_then(|o| usize::try_from(*o).ok()).unwrap_or(0)
|
||||
}
|
||||
|
||||
pub fn fields(&self) -> &[Field<'a>] {
|
||||
&self.fields
|
||||
}
|
||||
|
||||
pub fn offsets(&self) -> &[i32] {
|
||||
&self.offsets
|
||||
}
|
||||
|
||||
pub fn validity(&self) -> &[u64] {
|
||||
&self.valid
|
||||
}
|
||||
|
||||
pub fn object(fields: &[Field<'a>], item: usize) -> Variable {
|
||||
if fields.iter().any(|(key, _, _)| key.as_str().contains('.')) {
|
||||
let object = Variable::empty_object();
|
||||
for (key, leaf, present) in fields {
|
||||
if Bits::get(present, item) {
|
||||
object.dot_insert(key.as_str(), leaf.get(item));
|
||||
}
|
||||
}
|
||||
return object;
|
||||
}
|
||||
let mut map = VariableMap::with_capacity(fields.len());
|
||||
for (key, leaf, present) in fields {
|
||||
if Bits::get(present, item) {
|
||||
map.insert(key.clone(), leaf.get(item));
|
||||
}
|
||||
}
|
||||
Variable::from_object(map)
|
||||
}
|
||||
|
||||
pub fn get(&self, row: usize) -> Variable {
|
||||
if let (Some(maker), Some((start, end))) = (self.maker(), self.range(row)) {
|
||||
return Variable::from_array((start..end).map(|item| self.made(maker, item)).collect());
|
||||
}
|
||||
match self.range(row) {
|
||||
Some((start, end)) => Variable::from_array(
|
||||
(start..end)
|
||||
.map(|item| match self.nulled.as_ref().is_some_and(|n| Bits::get(n, item)) {
|
||||
true => Variable::Null,
|
||||
false => Self::object(&self.fields, item),
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
None => Variable::Null,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn dense(&self) -> &Rc<[Variable]> {
|
||||
self.dense.get_or_init(|| (0..self.rows).map(|row| self.get(row)).collect())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) enum Items<'a> {
|
||||
Input { column: Column<'a>, mask: Option<Rc<[u64]>> },
|
||||
Records(Rc<Records<'a>>),
|
||||
}
|
||||
|
||||
impl<'a> Items<'a> {
|
||||
pub fn range(&self, row: usize) -> Option<(usize, usize)> {
|
||||
match self {
|
||||
Items::Input { column, mask } => match mask.as_ref().is_none_or(|m| Bits::get(m, row)) {
|
||||
true => column.range(row),
|
||||
false => None,
|
||||
},
|
||||
Items::Records(records) => records.range(row),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn count(&self) -> usize {
|
||||
match self {
|
||||
Items::Input { column, .. } => match column.values {
|
||||
Values::List { child, .. } => child.column().len(),
|
||||
_ => 0,
|
||||
},
|
||||
Items::Records(records) => records.count(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fields(&self) -> Option<Vec<Field<'a>>> {
|
||||
match self {
|
||||
Items::Records(records) if records.nulled.is_some() => None,
|
||||
Items::Records(records) => Some(records.fields.clone()),
|
||||
Items::Input { column, .. } => {
|
||||
let Values::List {
|
||||
child: Dictionary::Column(child),
|
||||
..
|
||||
} = column.values
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
let Values::Struct { fields, len } = child.values else {
|
||||
return None;
|
||||
};
|
||||
let rows = match child.validity {
|
||||
Some((bits, offset)) => Some(Bits::window(bits, offset, len)),
|
||||
None => None,
|
||||
};
|
||||
Some(
|
||||
fields
|
||||
.iter()
|
||||
.map(|(name, field)| {
|
||||
let mut present = match field.validity {
|
||||
Some((bits, offset)) => Bits::window(bits, offset, len),
|
||||
None => Bits::ones(len),
|
||||
};
|
||||
if let Some(rows) = &rows {
|
||||
present.iter_mut().zip(rows).for_each(|(p, r)| *p &= r);
|
||||
}
|
||||
(Symbol::from(*name), Leaf::input(*field, len), Rc::from(present))
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
use std::cell::OnceCell;
|
||||
use std::rc::Rc;
|
||||
use zen_expression::lane::Column;
|
||||
use zen_expression::Variable;
|
||||
use super::{Bits, Leaf, Literal};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Picked<'a> {
|
||||
pub(super) source: Leaf<'a>,
|
||||
pub(super) positions: Rc<[usize]>,
|
||||
pub(super) dense: OnceCell<Leaf<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Stitched<'a> {
|
||||
pub(super) sources: Vec<Leaf<'a>>,
|
||||
pub(super) located: Rc<[(usize, usize)]>,
|
||||
pub(super) dense: OnceCell<Leaf<'a>>,
|
||||
}
|
||||
|
||||
pub(crate) type Passed<'l, 'a> = (&'l Rc<Input<'a>>, Option<&'l Rc<[usize]>>);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Input<'a> {
|
||||
pub column: Column<'a>,
|
||||
pub rows: usize,
|
||||
pub(super) valued: OnceCell<Vec<u64>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Masked<'a> {
|
||||
pub(super) inner: Leaf<'a>,
|
||||
pub(super) mask: Rc<[u64]>,
|
||||
pub(super) bits: OnceCell<Rc<[u64]>>,
|
||||
}
|
||||
|
||||
impl Masked<'_> {
|
||||
pub(super) fn bits(&self) -> &Rc<[u64]> {
|
||||
self.bits.get_or_init(|| {
|
||||
let rows = self.inner.len();
|
||||
let mut bits = self.inner.validity(rows);
|
||||
bits.iter_mut().zip(self.mask.iter()).for_each(|(b, m)| *b &= m);
|
||||
bits.iter_mut().skip(self.mask.len()).for_each(|b| *b = 0);
|
||||
bits.into()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Scattered<'a> {
|
||||
pub(super) inner: Leaf<'a>,
|
||||
pub(super) positions: Rc<[usize]>,
|
||||
pub(super) rows: usize,
|
||||
pub(super) dense: OnceCell<Leaf<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Templated {
|
||||
pub(super) codes: Rc<[i32]>,
|
||||
pub(super) templates: Rc<[Literal]>,
|
||||
pub(super) valid: Vec<u64>,
|
||||
dense: OnceCell<Rc<[Variable]>>,
|
||||
}
|
||||
|
||||
impl Templated {
|
||||
pub fn leaf<'a>(codes: Rc<[i32]>, templates: Rc<[Literal]>) -> Leaf<'a> {
|
||||
let valid = Bits::of(codes.len(), |row| {
|
||||
usize::try_from(codes[row]).ok().and_then(|code| templates.get(code)).is_some_and(|literal| !matches!(literal, Literal::Null))
|
||||
});
|
||||
Leaf::Templated(Rc::new(Templated {
|
||||
codes,
|
||||
templates,
|
||||
valid,
|
||||
dense: OnceCell::new(),
|
||||
}))
|
||||
}
|
||||
|
||||
pub(super) fn template(&self, row: usize) -> Option<&Literal> {
|
||||
let code = usize::try_from(*self.codes.get(row)?).ok()?;
|
||||
self.templates.get(code).filter(|literal| !matches!(literal, Literal::Null))
|
||||
}
|
||||
|
||||
pub(super) fn get(&self, row: usize) -> Variable {
|
||||
self.template(row).map_or(Variable::Null, Literal::variable)
|
||||
}
|
||||
|
||||
pub(super) fn mapped<'a>(&self, rows: impl Iterator<Item = usize>) -> Leaf<'a> {
|
||||
Templated::leaf(rows.map(|row| self.codes.get(row).copied().unwrap_or(-1)).collect(), self.templates.clone())
|
||||
}
|
||||
|
||||
pub(super) fn dense(&self) -> &Rc<[Variable]> {
|
||||
self.dense.get_or_init(|| (0..self.codes.len()).map(|row| self.get(row)).collect())
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::compiled::typed::{Array, Bits, Dict, Leaf, Store};
|
||||
use crate::compiled::leaf::{Array, Bits, Dict, Leaf, Store};
|
||||
use std::rc::Rc;
|
||||
use std::sync::OnceLock;
|
||||
use zen_expression::typed::{Data, Hir, Outputs, Slot};
|
||||
|
||||
@@ -13,7 +13,7 @@ mod data;
|
||||
mod lowered;
|
||||
mod schema;
|
||||
mod shred;
|
||||
mod typed;
|
||||
mod leaf;
|
||||
|
||||
use crate::decision_graph::cleaner::VariableCleaner;
|
||||
use crate::decision_graph::graph::{DecisionGraph, DecisionGraphResponse};
|
||||
@@ -44,7 +44,7 @@ use std::time::Instant;
|
||||
use table::{Table, TableOut};
|
||||
use bound::{Bound, Scopes};
|
||||
use data::{Col, Data, Layer, Mask};
|
||||
use typed::{Array, ColumnBuilder, Leaf};
|
||||
use leaf::{Array, ColumnBuilder, Leaf};
|
||||
use std::rc::Rc;
|
||||
use zen_types::symbol::Symbol;
|
||||
use zen_expression::lane::{Binding, Columns};
|
||||
@@ -1635,7 +1635,7 @@ impl CompiledGraph {
|
||||
Some(
|
||||
present
|
||||
.into_iter()
|
||||
.map(|p| Mask::Bits(typed::Bits::of(count, |row| p[row]).into()))
|
||||
.map(|p| Mask::Bits(leaf::Bits::of(count, |row| p[row]).into()))
|
||||
.collect(),
|
||||
),
|
||||
);
|
||||
@@ -2132,7 +2132,7 @@ impl CompiledGraph {
|
||||
builder.push_variable(value.unwrap_or(Variable::Null));
|
||||
}
|
||||
leaves.push(Leaf::typed(builder.finish()));
|
||||
present.push(Mask::Bits(typed::Bits::of(mask.len(), |row| mask[row]).into()));
|
||||
present.push(Mask::Bits(leaf::Bits::of(mask.len(), |row| mask[row]).into()));
|
||||
}
|
||||
let rows: Rc<[usize]> = (0..inputs.len()).collect();
|
||||
Some(Data::record(&rows, Layer::new(keys.into(), leaves, Some(present)), None))
|
||||
|
||||
@@ -5,7 +5,7 @@ mod view;
|
||||
|
||||
use crate::compiled::schedule::{End, Outcome, Segment};
|
||||
use crate::compiled::table::Table;
|
||||
use crate::compiled::typed::Literal;
|
||||
use crate::compiled::leaf::Literal;
|
||||
use crate::compiled::{Body, CompiledGraph, Condition, Kind, Step};
|
||||
use petgraph::prelude::NodeIndex;
|
||||
use std::sync::{Arc, OnceLock, RwLock};
|
||||
|
||||
@@ -13,7 +13,7 @@ use crate::decision_graph::walker::GraphWalker;
|
||||
use crate::nodes::{NodeError, NodeHandlerExtensions};
|
||||
use crate::compiled::bound::{Bound, Outs};
|
||||
use crate::compiled::shred::{Listing, Normalizer, Seen, Shred, Shredder};
|
||||
use crate::compiled::typed::{Array, Bits, ColumnBuilder, Dict, Field, Items, Leaf, Literal, Records, Store as Buffer, Templated};
|
||||
use crate::compiled::leaf::{Array, Bits, ColumnBuilder, Dict, Field, Items, Leaf, Literal, Records, Store as Buffer, Templated};
|
||||
use crate::compiled::data::Shape as DataShape;
|
||||
use crate::compiled::{Body, ColumnarOutput, CompiledGraph, Condition, Kind, OutputColumn};
|
||||
use crate::model::{DecisionNodeKind, GraphContent};
|
||||
|
||||
@@ -9,7 +9,7 @@ use super::{Fixed, Native, PolicyPlan, Rows};
|
||||
use crate::compiled::bound::Outs;
|
||||
use crate::compiled::columnar::OutputColumn;
|
||||
use crate::compiled::shred::{Listing, Normalizer, Shred, Shredder};
|
||||
use crate::compiled::typed::{Array, Bits, ColumnBuilder, Dict, Leaf};
|
||||
use crate::compiled::leaf::{Array, Bits, ColumnBuilder, Dict, Leaf};
|
||||
use crate::policy::blocks::{AssertionIr, MatchIr, MatchSelection, TableSelection};
|
||||
use super::Host;
|
||||
use crate::policy::evaluator::{EvalArtifact, Iteration};
|
||||
@@ -617,7 +617,7 @@ impl<'s, 'a> Pass<'s, 'a> {
|
||||
truths.push(leaf.truths(positions.len()));
|
||||
}
|
||||
let bits = Self::folded(ir, &truths, positions.len());
|
||||
let leaf = Leaf::typed(Array::parts(crate::compiled::typed::Store::Bool(bits), None, positions.len()));
|
||||
let leaf = Leaf::typed(Array::parts(crate::compiled::leaf::Store::Bool(bits), None, positions.len()));
|
||||
self.write(&ir.output, leaf, positions);
|
||||
}
|
||||
|
||||
@@ -644,8 +644,8 @@ impl<'s, 'a> Pass<'s, 'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn dict(values: &[Variable]) -> crate::compiled::typed::Dict {
|
||||
use crate::compiled::typed::Dict;
|
||||
fn dict(values: &[Variable]) -> crate::compiled::leaf::Dict {
|
||||
use crate::compiled::leaf::Dict;
|
||||
if values.iter().all(|v| matches!(v, Variable::String(_))) {
|
||||
let mut offsets = vec![0i32];
|
||||
let mut data = String::new();
|
||||
@@ -1302,7 +1302,7 @@ impl<'s, 'a> Pass<'s, 'a> {
|
||||
truths.push(leaf.truths(kids.len()));
|
||||
}
|
||||
let bits = Self::folded(ir, &truths, kids.len());
|
||||
let leaf = Leaf::typed(Array::parts(crate::compiled::typed::Store::Bool(bits), None, kids.len()));
|
||||
let leaf = Leaf::typed(Array::parts(crate::compiled::leaf::Store::Bool(bits), None, kids.len()));
|
||||
self.member_write(at, &ir.output, leaf, &kids);
|
||||
}
|
||||
(Event::Select(_), Some(Native::Match { pick: Some(pick), .. })) => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::{Pass, Sheet};
|
||||
use crate::compiled::policy::schedule::{End, Event, Segment};
|
||||
use crate::compiled::policy::{Native, PolicyPlan};
|
||||
use crate::compiled::typed::{Array, Bits, Leaf, Store};
|
||||
use crate::compiled::leaf::{Array, Bits, Leaf, Store};
|
||||
use crate::policy::blocks::{AssertionIr, BlockKind, ConditionOperator, DecisionTableIr, MatchIr};
|
||||
use std::cmp::Ordering;
|
||||
use std::rc::Rc;
|
||||
|
||||
@@ -2,7 +2,7 @@ use super::fuse::{Fused, Fusions, Sink};
|
||||
use super::{Bind, Member, Pass, Sheet};
|
||||
use crate::compiled::lowered::Exports;
|
||||
use crate::compiled::shred::{Listing, Normalizer};
|
||||
use crate::compiled::typed::{Bits, Leaf};
|
||||
use crate::compiled::leaf::{Bits, Leaf};
|
||||
use crate::policy::blocks::BlockKind;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::compiled::shred::{Normalizer, Shred};
|
||||
use crate::policy::evaluator::EvalArtifact;
|
||||
use crate::compiled::typed::{Bits, ColumnBuilder, Leaf};
|
||||
use crate::compiled::leaf::{Bits, ColumnBuilder, Leaf};
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
use zen_expression::lane::{Column, Columns, Dictionary, Values};
|
||||
@@ -238,7 +238,7 @@ impl<'a> Entity<'a> {
|
||||
(Symbol::from(field.name.as_ref()), field.leaf.clone(), present)
|
||||
})
|
||||
.collect();
|
||||
Leaf::Records(Rc::new(crate::compiled::typed::Records::new(rows, offsets, valid, fields, None)))
|
||||
Leaf::Records(Rc::new(crate::compiled::leaf::Records::new(rows, offsets, valid, fields, None)))
|
||||
}
|
||||
|
||||
pub fn compose(&self, row: usize) -> Option<Variable> {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::compiled::data::{Data, Shape};
|
||||
use crate::compiled::typed::{Bits, Leaf};
|
||||
use crate::compiled::leaf::{Bits, Leaf};
|
||||
use rust_decimal::prelude::ToPrimitive;
|
||||
use rust_decimal::Decimal;
|
||||
use serde_json::Value;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::compiled::typed::{Array, Bits, Dict, Leaf, Records, Store};
|
||||
use crate::compiled::leaf::{Array, Bits, Dict, Leaf, Records, Store};
|
||||
use crate::nodes::input::dates::DeclaredDates;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::compiled::bound::{Bound, Outs};
|
||||
use crate::compiled::data::Col;
|
||||
use crate::compiled::data::Mask;
|
||||
use crate::compiled::typed::{Array, Bits, ColumnBuilder, Dict, Leaf, Literal, Records, Store, Templated};
|
||||
use crate::compiled::leaf::{Array, Bits, ColumnBuilder, Dict, Leaf, Literal, Records, Store, Templated};
|
||||
use std::borrow::Cow;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user