dataframe-core-2.4.0.0: Core data structures for the dataframe library.
Safe HaskellNone
LanguageHaskell2010

DataFrame.Internal.Column

Synopsis

Documentation

type Bitmap = Vector Word8 Source #

A bit-packed validity bitmap. Bit i = 1 means row i is valid (not null).

data Column where Source #

Type-erased column GADT. Pattern-matching on the constructor recovers the representation; nullability is an optional bit-packed Bitmap (Nothing = no nulls, Just bm = bit i set iff row i is valid).

Constructors

BoxedColumn :: forall a. Columnable a => Maybe Bitmap -> Vector a -> Column 
UnboxedColumn :: forall a. (Columnable a, Unbox a) => Maybe Bitmap -> Vector a -> Column 
PackedText :: Maybe Bitmap -> !PackedTextData -> Column 
MergedColumn :: !Column -> !Column -> Column 

Instances

Instances details
Show Column Source # 
Instance details

Defined in DataFrame.Internal.Column

Eq Column Source # 
Instance details

Defined in DataFrame.Internal.Column

Methods

(==) :: Column -> Column -> Bool #

(/=) :: Column -> Column -> Bool #

data MutableColumn where Source #

A mutable companion struct to dataframe columns.

Used mostly as an intermediate structure for I/O.

Constructors

MBoxedColumn :: forall a. Columnable a => IOVector a -> MutableColumn 
MUnboxedColumn :: forall a. (Columnable a, Unbox a) => IOVector a -> MutableColumn 

bitmapTestBit :: Bitmap -> Int -> Bool Source #

Test whether row i is valid (not null) in a bitmap.

allValidBitmap :: Int -> Bitmap Source #

Build a fully-valid bitmap for n rows (all bits set).

buildBitmapFromValid :: Vector Word8 -> Bitmap Source #

Build a bitmap from a VU.Vector Word8 validity vector (1 = valid, 0 = null), as produced by Arrow / Parquet decoders.

buildBitmapFromNulls :: Int -> [Int] -> Bitmap Source #

Build a bitmap from a list of null-row indices. nullIdxs are the positions that are NULL.

bitmapSlice :: Int -> Int -> Bitmap -> Bitmap Source #

Slice a bitmap for rows [start .. start+len-1].

bitmapConcat :: Int -> Bitmap -> Int -> Bitmap -> Bitmap Source #

Concatenate two bitmaps covering n1 and n2 rows respectively.

mergeBitmaps :: Bitmap -> Bitmap -> Bitmap Source #

Combine two bitmaps with AND (both must be valid for result to be valid).

fromMaybeVec :: Columnable a => Vector (Maybe a) -> Column Source #

Materialize a nullable column from VB.Vector (Maybe a); picks UnboxedColumn when a is unboxable, else BoxedColumn. Always attaches a bitmap so the column reads as nullable even with no Nothing values.

fromMaybeVecUnboxed :: (Columnable a, Unbox a) => Vector (Maybe a) -> Column Source #

Materialize a nullable UnboxedColumn to VB.Vector (Maybe a) using runST. Always attaches a bitmap so the column is recognized as nullable even when no Nothing values are present (preserves the Maybe type marker).

columnElemIsNull :: Column -> Int -> Bool Source #

Whether row i is null, respecting the bitmap.

columnBitmap :: Column -> Maybe Bitmap Source #

Return the 'Maybe Bitmap' from a column.

materializePacked :: Column -> Column Source #

Decode a PackedText into a BoxedColumn Text (bit-identical to materializing at freeze). Identity on every other column.

isPackedText :: Column -> Bool Source #

Whether a column is a PackedText.

isMergedColumn :: Column -> Bool Source #

Whether a column is a MergedColumn.

checkMergedNoBothNull :: Column -> Column -> () Source #

MergedColumn defers element construction, so forcing must still surface the one deferred error — a row null on both sides — inside strict IO/executor boundaries. O(rows) bitmap walk, no allocation; both-null needs a bitmap on each side, so anything else passes immediately.

data TypedColumn a where Source #

A wrapper around the type-erased Column carrying a phantom element type, used to type-check expressions. The phantom is not guaranteed to match the underlying vector's type.

Constructors

TColumn :: forall a. Columnable a => Column -> TypedColumn a 

Instances

Instances details
Show a => Show (TypedColumn a) Source # 
Instance details

Defined in DataFrame.Internal.Column

Eq a => Eq (TypedColumn a) Source # 
Instance details

Defined in DataFrame.Internal.Column

unwrapTypedColumn :: TypedColumn a -> Column Source #

Gets the underlying value from a TypedColumn.

vectorFromTypedColumn :: TypedColumn a -> Vector a Source #

Gets the underlying vector from a TypedColumn.

hasMissing :: Column -> Bool Source #

Checks if a column contains missing values (has a bitmap).

allMissing :: Column -> Bool Source #

Checks if a column contains only missing values.

isNumeric :: Column -> Bool Source #

Checks if a column contains numeric values.

hasElemType :: Columnable a => Column -> Bool Source #

Whether the column stores element type a. For nullable columns, also True when a = Maybe b and the column stores b internally.

columnVersionString :: Column -> String Source #

An internal/debugging function to get the column type of a column.

columnTypeString :: Column -> String Source #

An internal/debugging function to get the type stored in the outermost vector of a column.

forceColumn :: Column -> () Source #

Force evaluation of all elements in a column. Replacement for the removed instance NFData Column; used by the IO and lazy-executor strict paths.

eqBoxedCols :: Eq a => Maybe Bitmap -> Vector a -> Maybe Bitmap -> Vector a -> Bool Source #

Compare two nullable boxed columns element by element, skipping null slots. Uses a manual loop to avoid stream fusion forcing null-slot error thunks.

eqPackedCols :: Maybe Bitmap -> PackedTextData -> Maybe Bitmap -> PackedTextData -> Bool Source #

Byte-slice equality of two packed-text columns, skipping null slots (a null compares equal only to a null), mirroring eqBoxedCols.

class ColumnifyRep (r :: Rep) a where Source #

A class for converting a vector to a column of the appropriate type. Given each Rep we tell the toColumnRep function which Column type to pick.

Methods

toColumnRep :: Vector a -> Column Source #

Instances

Instances details
Columnable a => ColumnifyRep 'RBoxed a Source # 
Instance details

Defined in DataFrame.Internal.Column

Methods

toColumnRep :: Vector a -> Column Source #

(Columnable a, Unbox a) => ColumnifyRep 'RUnboxed a Source # 
Instance details

Defined in DataFrame.Internal.Column

Methods

toColumnRep :: Vector a -> Column Source #

Columnable a => ColumnifyRep 'RNullableBoxed (Maybe a) Source # 
Instance details

Defined in DataFrame.Internal.Column

Methods

toColumnRep :: Vector (Maybe a) -> Column Source #

type Columnable a = (Columnable' a, ColumnifyRep (KindOf a) a, UnboxIf a, IntegralIf a, FloatingIf a, SBoolI (Unboxable a), SBoolI (Numeric a), SBoolI (IntegralTypes a), SBoolI (FloatingTypes a)) Source #

Constraint synonym for what we can put into columns.

fromVector :: (Columnable a, ColumnifyRep (KindOf a) a) => Vector a -> Column Source #

O(n) Convert a vector to a column. Automatically picks the best representation of a vector to store the underlying data in.

Examples:

> import qualified Data.Vector as V
> fromVector (VB.fromList [(1 :: Int), 2, 3, 4])
[1,2,3,4]

fromUnboxedVector :: (Columnable a, Unbox a) => Vector a -> Column Source #

O(n) Convert an unboxed vector to a column. This avoids the extra conversion if you already have the data in an unboxed vector.

Examples:

> import qualified Data.Vector.Unboxed as V
> fromUnboxedVector (VB.fromList [(1 :: Int), 2, 3, 4])
[1,2,3,4]

fromList :: (Columnable a, ColumnifyRep (KindOf a) a) => [a] -> Column Source #

O(n) Convert a list to a column. Automatically picks the best representation of a vector to store the underlying data in.

Examples:

> fromList [(1 :: Int), 2, 3, 4]
[1,2,3,4]

mkRandom :: (RandomGen g, Columnable a, ColumnifyRep (KindOf a) a, UniformRange a) => g -> Int -> a -> a -> Column Source #

O(n) Create a column of random elements within a range.

Takes a random number generator, a length, and a lower and upper bound for the random values.

Examples:

> import System.Random (mkStdGen)
> mkRandom (mkStdGen 42) 4 0 10
[4,2,6,5]

mapColumn :: (Columnable b, Columnable c) => (b -> c) -> Column -> Either DataFrameException Column Source #

An internal function to map a function over the values of a column.

imapColumn :: (Columnable b, Columnable c) => (Int -> b -> c) -> Column -> Either DataFrameException Column Source #

Applies a function that returns an unboxed result to an unboxed vector, storing the result in a column.

columnLength :: Column -> Int Source #

O(1) Gets the number of elements in the column.

numElements :: Column -> Int Source #

O(n) Gets the number of non-null elements in the column.

takeColumn :: Int -> Column -> Column Source #

O(n) Takes the first n values of a column.

takeLastColumn :: Int -> Column -> Column Source #

O(n) Takes the last n values of a column.

sliceColumn :: Int -> Int -> Column -> Column Source #

O(n) Takes n values after a given column index.

atIndicesStable :: Vector Int -> Column -> Column Source #

O(n) Selects the elements at a given set of indices. Does not change the order.

gatherWithSentinel :: Vector Int -> Column -> Column Source #

Like atIndicesStable but treats negative indices as null. Keeps the index vector fully unboxed (no VB.Vector (Maybe Int)).

getIndices :: Vector Int -> Vector a -> Vector a Source #

Internal helper to get indices in a boxed vector.

getIndicesUnboxed :: Unbox a => Vector Int -> Vector a -> Vector a Source #

Internal helper to get indices in an unboxed vector.

ifoldrColumn :: (Columnable a, Columnable b) => (Int -> a -> b -> b) -> b -> Column -> Either DataFrameException b Source #

Fold (right) column with index.

foldlColumn :: (Columnable a, Columnable b) => (b -> a -> b) -> b -> Column -> Either DataFrameException b Source #

foldl1DirectGroups :: Columnable a => (a -> a -> a) -> Column -> Vector Int -> Vector Int -> Either DataFrameException Column Source #

O(n) Seedless fold over groups using the first element of each group as seed. Like foldDirectGroups but for the case where no initial accumulator is available.

foldLinearGroups :: (Columnable b, Columnable acc) => (acc -> b -> acc) -> acc -> Column -> Vector Int -> Int -> Either DataFrameException Column Source #

O(n) fold over groups by scanning the column linearly (rowToGroup[i] = group of row i). Random writes hit the small per-group accumulator array; when acc is unboxable that array is unboxed, avoiding pointer indirection.

zipColumns :: Column -> Column -> Column Source #

An internal, column version of zip.

mergeColumns :: Column -> Column -> Column Source #

Merge two columns using These. O(1): the sides are kept in their native representation and These values materialize on element access.

materializeMerged :: Column -> Column Source #

Decode a MergedColumn into the eager BoxedColumn (These a b) form.

mergeEager :: Column -> Column -> Column Source #

The eager element-wise merge (These per row, boxed). Bitmaps are honored for every representation pair: a null side yields This/That, both-null is an error (the join kernels never produce such a row).

zipWithColumns :: (Columnable a, Columnable b, Columnable c) => (a -> b -> c) -> Column -> Column -> Either DataFrameException Column Source #

An internal, column version of zipWith.

freezeColumnEither :: [(Int, Text)] -> MutableColumn -> IO Column Source #

Freeze a mutable column into an Either Text a column: every recorded null position becomes Left rawText (preserving the original input), every other position becomes Right v. Used by CSV readers under EitherRead mode.

ensureOptional :: Column -> Column Source #

Promote a non-nullable column to a nullable one (add an all-valid bitmap). No-op when already nullable.

expandColumn :: Int -> Column -> Column Source #

Fills the end of a column, up to n, with null rows. Does nothing if column has length >= n.

leftExpandColumn :: Int -> Column -> Column Source #

Fills the beginning of a column, up to n, with null rows. Does nothing if column has length >= n.

concatColumns :: Column -> Column -> Either DataFrameException Column Source #

Concatenates two columns. Returns Nothing if the columns are of different types.

concatManyColumns :: [Column] -> Column Source #

Like concatColumns but also combines columns of different types by wrapping values in Either (e.g. [1,2] and ["a","b"] become [Left 1, Left 2, Right "a", Right "b"]).

O(n) Concatenate a list of same-type columns in a single allocation. All columns must have the same constructor and element type (as they will within a single Parquet column). Calls error on mismatch.

newMutableColumn :: Int -> Column -> IO MutableColumn Source #

Allocate a mutable column of size n matching the constructor/type of the given column.

copyIntoMutableColumn :: MutableColumn -> Int -> Column -> IO () Source #

Copy a column chunk into a mutable column starting at offset off.

freezeMutableColumn :: MutableColumn -> IO Column Source #

Freeze a mutable column into an immutable column.

toList :: Columnable a => Column -> [a] Source #

O(n) Converts a column to a list. Throws an exception if the wrong type is specified.

Examples:

> column = fromList [(1 :: Int), 2, 3, 4]
> toList Int column
[1,2,3,4]
> toList Double column
exception: ...

toVector :: forall a v. (Vector v a, Columnable a) => Column -> Either DataFrameException (v a) Source #

Type-safe conversion of a column to a vector of element type a (specify via type application); Left TypeMismatchException when the column's type differs.

>>> toVector @Int @VU.Vector column
Right (unboxed vector of Ints)
>>> toVector @Text @VB.Vector column
Right (boxed vector of Text)

toDoubleVector :: Column -> Either DataFrameException (Vector Double) Source #

Convert a column to an unboxed Double vector, coercing numeric types (realToFrac for floats, fromIntegral for integrals; nulls become NaN). Left TypeMismatchException when the column is not numeric.

toFloatVector :: Column -> Either DataFrameException (Vector Float) Source #

Convert a column to an unboxed Float vector, coercing numeric types (nulls become NaN); Left TypeMismatchException when not numeric. Converting from Double may lose precision.

toIntVector :: Column -> Either DataFrameException (Vector Int) Source #

Convert a column to an unboxed Int vector, coercing numeric types (floats are rounded via banker's rounding); Left TypeMismatchException when the column is not numeric. Does not support nullable columns.

finalizeParseResult :: Unbox a => STVector s a -> STVector s Word8 -> Bool -> ST s (Maybe (Maybe Bitmap, Vector a)) Source #