Skip to content

Commit

Permalink
add docs and actually create scalar columns
Browse files Browse the repository at this point in the history
  • Loading branch information
coastalwhite committed Sep 12, 2024
1 parent c0bb959 commit 4bda43a
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 137 deletions.
51 changes: 51 additions & 0 deletions crates/polars-core/src/chunked_array/builder/binary_offset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use super::*;

pub struct BinaryOffsetChunkedBuilder {
pub(crate) chunk_builder: MutableBinaryArray<i64>,
pub(crate) field: FieldRef,
}

impl Clone for BinaryOffsetChunkedBuilder {
fn clone(&self) -> Self {
Self {
chunk_builder: self.chunk_builder.clone(),
field: self.field.clone(),
}
}
}

impl BinaryOffsetChunkedBuilder {
/// Create a new [`BinaryOffsetChunkedBuilder`]
///
/// # Arguments
///
/// * `capacity` - Number of string elements in the final array.
pub fn new(name: PlSmallStr, capacity: usize) -> Self {
Self {
chunk_builder: MutableBinaryArray::with_capacity(capacity),
field: Arc::new(Field::new(name, DataType::BinaryOffset)),
}
}

/// Appends a value of type `T` into the builder
#[inline]
pub fn append_value(&mut self, v: &[u8]) {
self.chunk_builder.push(Some(v));
}

/// Appends a null slot into the builder
#[inline]
pub fn append_null(&mut self) {
self.chunk_builder.push_null()
}

#[inline]
pub fn append_option(&mut self, opt: Option<&[u8]>) {
self.chunk_builder.push(opt);
}

pub fn finish(mut self) -> BinaryOffsetChunked {
let arr = self.chunk_builder.as_box();
ChunkedArray::new_with_compute_len(self.field, vec![arr])
}
}
2 changes: 2 additions & 0 deletions crates/polars-core/src/chunked_array/builder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod binary_offset;
mod boolean;
#[cfg(feature = "dtype-array")]
pub mod fixed_size_list;
Expand All @@ -10,6 +11,7 @@ use std::sync::Arc;

use arrow::array::*;
use arrow::bitmap::Bitmap;
pub use binary_offset::*;
pub use boolean::*;
#[cfg(feature = "dtype-array")]
pub(crate) use fixed_size_list::*;
Expand Down
12 changes: 8 additions & 4 deletions crates/polars-core/src/datatypes/any_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ impl<'a> AnyValue<'a> {
Object(v) => ObjectOwned(OwnedObject(v.to_boxed())),
#[cfg(feature = "dtype-struct")]
Struct(idx, arr, fields) => {
let avs = struct_to_avs_static(idx, arr, fields);
let avs = struct_to_avs_static(idx, arr, fields)?;
StructOwned(Box::new((avs, fields.to_vec())))
},
#[cfg(feature = "dtype-struct")]
Expand Down Expand Up @@ -1222,7 +1222,11 @@ impl TotalEq for AnyValue<'_> {
}

#[cfg(feature = "dtype-struct")]
fn struct_to_avs_static(idx: usize, arr: &StructArray, fields: &[Field]) -> Vec<AnyValue<'static>> {
fn struct_to_avs_static(
idx: usize,
arr: &StructArray,
fields: &[Field],
) -> PolarsResult<Vec<AnyValue<'static>>> {
let arrs = arr.values();
let mut avs = Vec::with_capacity(arrs.len());
// amortize loop counter
Expand All @@ -1231,10 +1235,10 @@ fn struct_to_avs_static(idx: usize, arr: &StructArray, fields: &[Field]) -> Vec<
let arr = &**arrs.get_unchecked_release(i);
let field = fields.get_unchecked_release(i);
let av = arr_to_any_value(arr, idx, &field.dtype);
avs.push_unchecked(av.into_static().unwrap());
avs.push_unchecked(av.into_static()?);
}
}
avs
Ok(avs)
}

#[cfg(feature = "dtype-categorical")]
Expand Down
Loading

0 comments on commit 4bda43a

Please sign in to comment.