numbarrow.core.is_null
Overview
Arrow marks the null rows of an array in a validity bitmap: one bit per row, packed into bytes with the least
significant bit first, set for a valid row and clear for a null one.
arrow_array_adapter() returns that bitmap as a uint8 array, and the functions of
this module read it inside @njit code. Python code can call them too.
is_null(i, bitmap)returnsTruewhen rowiis null. The function does not acceptNoneforbitmap, so check a bitmap forNonebefore the call.is_null_struct(i, struct_bitmap, field_bitmap)returnsTruewhen the struct’s bitmap or the field’s bitmap marks rowinull, and it acceptsNonefor either. Use it with the 3-tuple thatarrow_array_adapterreturns for a struct, because a row that is null as a whole is not always null in its fields. The field bitmaps thatmake_mapinarrow_func()passes already include the struct’s nulls, sois_nullis enough for them.unpack_booleans(offset, length, packed_data)expands bit-packed booleans into abool_array. Arrow packs the values of aBooleanArrayin the same layout as a validity bitmap, and the adapter uses this function to unpack them.
In the example, the struct has no bitmap, so is_null_struct receives None for it and bitmap as the field’s:
import numpy as np
from numbarrow.core.is_null import is_null, is_null_struct
bitmap = np.packbits([True, False, True], bitorder="little") # rows 0 and 2 valid
print(bitmap, is_null(1, bitmap), is_null_struct(1, None, bitmap)) # [5] True True
numba compiles the three functions when this module is first imported, each for one signature. A call converts an
index of another integer type to int64, and it accepts a writable bitmap as well as a read-only one. None as
the bitmap of is_null raises TypeError in a call from Python, and a jitted function that passes it fails to
compile.
By default, the functions do not check an index against the length of the bitmap. An index past the end reads memory
outside the bitmap: the answer is meaningless, and the read can crash the process.
NUMBARROW_JIT_OPTIONS='{"boundscheck": true}' makes such a read raise IndexError. The README section on
compilation and the cache explains the variable, and how to keep numba from loading cached code that was compiled
without bounds checks.
Module
Functions, compiled with numba, that read Arrow validity bitmaps and bit-packed boolean values.
An Arrow validity bitmap holds one bit for each element of an array: 1 if the element is valid, 0 if it is null. The
bits are packed into uint8 bytes from the least significant bit up, so element i is bit i % 8 of byte
i // 8. is_null() reads the bit of one element, and is_null_struct() reads the two bitmaps that
together decide whether a value of a struct’s field is null. Arrow packs the values of a boolean array in the same
way, and unpack_booleans() expands them into a numpy boolean array. numba compiles all three functions when this
module is imported, with the options of numbarrow.core.configurations. Python code and jitted code can both
call them.
- numbarrow.core.is_null.is_null(index_: int, bitmap: ndarray) bool[source]
Return True if element
index_is null in the validity bitmapbitmap, and False if it is valid.bitmapmust be a one-dimensional, C-contiguous uint8 array, read-only or writable, as the adapters return it. Called from Python with anything else, None included, the function raisesTypeError. An adapter returns None for an array without a validity buffer, whose elements are all valid, so test for None before the call.index_must satisfy0 <= index_ < 8 * len(bitmap). numba compiles without bounds checks by default, so an index past the end reads memory outside the bitmap: the answer is meaningless, and the read can crash the process. With bounds checks, the function raisesIndexErrorinstead.NUMBARROW_JIT_OPTIONS='{"boundscheck": true}'turns them on, and caching off, because the object replaces the default{"cache": true}. With caching on, through{"cache": true, "boundscheck": true}or numba’sNUMBA_BOUNDSCHECK=1, numba still loads code that it cached without bounds checks, so pointNUMBA_CACHE_DIRat a new directory as well.A negative index down to
-8 * len(bitmap)counts back from the end, as a numpy index does: it reads the bit of element8 * len(bitmap) + index_. That read stays inside the bitmap, so bounds checks do not catch it. A lower index reads outside the bitmap, and bounds checks catch it.- Parameters:
index_ – the zero-based index of the element
bitmap – the packed validity bitmap
- Returns:
True if the element’s bit is 0, False if it is 1
- numbarrow.core.is_null.is_null_struct(index_, struct_bitmap, field_bitmap)[source]
Return True if element
index_of a struct’s field is null in the struct’s bitmap or in the field’s.A struct array has a validity bitmap of its own, which marks the rows that are null as a whole, and each field has one for its own values. A field’s bitmap need not mark the null rows of the struct, so a value is null if either bitmap marks it. Pass None for a bitmap that the array does not have; if both are None, the value is valid.
numba compiles the function once, when the module is imported, for an
int64index and, for each bitmap, a read-only uint8 array or None. Every call uses that compilation: an index of another integer type converts toint64, and a writable bitmap is accepted as well. The bounds onindex_are those ofis_null().- Parameters:
index_ – the zero-based index of the element, converted to
int64struct_bitmap – the struct array’s own validity bitmap, or None
field_bitmap – the field’s validity bitmap, or None
- Returns:
True if either bitmap marks the value null
- numbarrow.core.is_null.unpack_booleans(offset: int, length: int, packed_data: ndarray) ndarray[source]
Return a new array of the
lengthbooleans packed inpacked_data, from bitoffseton.Arrow packs the values of a boolean array as it packs a validity bitmap: one bit per value, from the least significant bit of each byte up.
packed_datais a one-dimensional, C-contiguous uint8 array, read-only or writable.offset + lengthmust not exceed8 * len(packed_data). numba compiles without bounds checks by default, so the bits past that end come from memory outsidepacked_data, and the read can crash the process. With bounds checks, whichNUMBARROW_JIT_OPTIONS='{"boundscheck": true}'or numba’sNUMBA_BOUNDSCHECK=1turns on, the function raisesIndexErrorinstead. With caching on, numba still loads code that it cached without bounds checks, so pointNUMBA_CACHE_DIRat a new directory as well.- Parameters:
offset – the position of the first bit to read, counted from the first bit of
packed_datalength – the number of booleans to return
packed_data – the bytes that hold the packed bits
- Returns:
a new array of
lengthbooleans