Skip to content

Commit

Permalink
implement :null dtype (#805)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkarthee authored Jan 9, 2024
1 parent cc0e5a1 commit 00258f5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/explorer/shared.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ defmodule Explorer.Shared do
def normalise_dtype(:u16), do: {:u, 16}
def normalise_dtype(:u32), do: {:u, 32}
def normalise_dtype(:u64), do: {:u, 64}
def normalise_dtype(:null), do: :null
def normalise_dtype(_dtype), do: nil

@doc """
Expand Down
3 changes: 3 additions & 0 deletions native/explorer/src/datatypes/ex_dtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl TryFrom<&ExTimeUnit> for TimeUnit {

#[derive(NifTaggedEnum)]
pub enum ExSeriesDtype {
Null,
Binary,
Boolean,
Category,
Expand All @@ -63,6 +64,7 @@ impl TryFrom<&DataType> for ExSeriesDtype {

fn try_from(value: &DataType) -> Result<Self, Self::Error> {
match value {
DataType::Null => Ok(ExSeriesDtype::Null),
DataType::Binary => Ok(ExSeriesDtype::Binary),
DataType::Boolean => Ok(ExSeriesDtype::Boolean),
DataType::Categorical(_, _) => Ok(ExSeriesDtype::Category),
Expand Down Expand Up @@ -128,6 +130,7 @@ impl TryFrom<&ExSeriesDtype> for DataType {

fn try_from(value: &ExSeriesDtype) -> Result<Self, Self::Error> {
match value {
ExSeriesDtype::Null => Ok(DataType::Null),
ExSeriesDtype::Binary => Ok(DataType::Binary),
ExSeriesDtype::Boolean => Ok(DataType::Boolean),
ExSeriesDtype::Category => {
Expand Down
12 changes: 12 additions & 0 deletions native/explorer/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,17 @@ macro_rules! series_to_list {
};
}

#[inline]
fn null_series_to_list<'b>(s: &Series, env: Env<'b>) -> Result<Term<'b>, ExplorerError> {
let nil_as_c_arg = atom::nil().to_term(env).as_c_arg();
let env_as_c_arg = env.as_c_arg();
let mut list = unsafe { list::make_list(env_as_c_arg, &[]) };
for _n in 0..s.len() {
list = unsafe { list::make_list_cell(env_as_c_arg, nil_as_c_arg, list) }
}
Ok(unsafe { Term::new(env, list) })
}

macro_rules! series_to_iovec {
($resource:ident, $s:ident, $env:ident, $convert_function:ident, $in_type:ty) => {{
Ok(unsafe_iterator_series_to_list!(
Expand Down Expand Up @@ -593,6 +604,7 @@ pub fn term_from_value<'b>(v: AnyValue, env: Env<'b>) -> Result<Term<'b>, Explor

pub fn list_from_series(s: ExSeries, env: Env) -> Result<Term, ExplorerError> {
match s.dtype() {
DataType::Null => null_series_to_list(&s, env),
DataType::Boolean => series_to_list!(s, env, bool),

DataType::Int8 => series_to_list!(s, env, i8),
Expand Down
14 changes: 14 additions & 0 deletions test/explorer/series_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3428,6 +3428,20 @@ defmodule Explorer.SeriesTest do
end

describe "cast/2" do
test "integer series to null" do
s = Series.from_list([1, 2, 3])
s1 = Series.cast(s, :null)
assert Series.to_list(s1) == [nil, nil, nil]
assert Series.dtype(s1) == :null
end

test "string series to null" do
s = Series.from_list(["a", "b", "c"])
s1 = Series.cast(s, :null)
assert Series.to_list(s1) == [nil, nil, nil]
assert Series.dtype(s1) == :null
end

test "integer series to string" do
s = Series.from_list([1, 2, 3])
s1 = Series.cast(s, :string)
Expand Down

0 comments on commit 00258f5

Please sign in to comment.