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) returns True when row i is null. The function does not accept None for bitmap, so check a bitmap for None before the call.

  • is_null_struct(i, struct_bitmap, field_bitmap) returns True when the struct’s bitmap or the field’s bitmap marks row i null, and it accepts None for either. Use it with the 3-tuple that arrow_array_adapter returns for a struct, because a row that is null as a whole is not always null in its fields. The field bitmaps that make_mapinarrow_func() passes already include the struct’s nulls, so is_null is enough for them.

  • unpack_booleans(offset, length, packed_data) expands bit-packed booleans into a bool_ array. Arrow packs the values of a BooleanArray in 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 bitmap bitmap, and False if it is valid.

bitmap must 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 raises TypeError. 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 satisfy 0 <= 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 raises IndexError instead. 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’s NUMBA_BOUNDSCHECK=1, numba still loads code that it cached without bounds checks, so point NUMBA_CACHE_DIR at 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 element 8 * 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 int64 index and, for each bitmap, a read-only uint8 array or None. Every call uses that compilation: an index of another integer type converts to int64, and a writable bitmap is accepted as well. The bounds on index_ are those of is_null().

Parameters:
  • index_ – the zero-based index of the element, converted to int64

  • struct_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 length booleans packed in packed_data, from bit offset on.

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_data is a one-dimensional, C-contiguous uint8 array, read-only or writable.

offset + length must not exceed 8 * len(packed_data). numba compiles without bounds checks by default, so the bits past that end come from memory outside packed_data, and the read can crash the process. With bounds checks, which NUMBARROW_JIT_OPTIONS='{"boundscheck": true}' or numba’s NUMBA_BOUNDSCHECK=1 turns on, the function raises IndexError instead. With caching on, numba still loads code that it cached without bounds checks, so point NUMBA_CACHE_DIR at a new directory as well.

Parameters:
  • offset – the position of the first bit to read, counted from the first bit of packed_data

  • length – the number of booleans to return

  • packed_data – the bytes that hold the packed bits

Returns:

a new array of length booleans