dataframe-1.0.0.1: A fast, safe, and intuitive DataFrame library.
Safe HaskellNone
LanguageHaskell2010

DataFrame.Typed.Expr

Description

Type-safe expression construction for typed DataFrames.

Unlike the untyped Expr a where column references are unchecked strings, TExpr ensures at compile time that:

  • Referenced columns exist in the schema
  • Column types match the expression type

Example

type Schema = '[Column "age" Int, Column "salary" Double]

-- This compiles:
goodExpr :: TExpr Schema Double
goodExpr = col @"salary"

-- This gives a compile-time error (column not found):
badExpr :: TExpr Schema Double
badExpr = col @"nonexistent"

-- This gives a compile-time error (type mismatch):
wrongType :: TExpr Schema Int
wrongType = col @"salary"  -- salary is Double, not Int
Synopsis

Core typed expression type (re-exported from Types)

newtype TExpr (cols :: [Type]) a Source #

A typed expression validated against schema cols, producing values of type a.

Unlike the untyped 'Expr a', a TExpr can only be constructed through type-safe combinators (col, lit, arithmetic operations) that verify column references exist in the schema with the correct type.

Use unTExpr to extract the underlying Expr for delegation to the untyped API.

Constructors

TExpr 

Fields

Instances

Instances details
(IsString a, Columnable a) => IsString (TExpr cols a) Source # 
Instance details

Defined in DataFrame.Typed.Expr

Methods

fromString :: String -> TExpr cols a #

(Floating a, Columnable a) => Floating (TExpr cols a) Source # 
Instance details

Defined in DataFrame.Typed.Expr

Methods

pi :: TExpr cols a #

exp :: TExpr cols a -> TExpr cols a #

log :: TExpr cols a -> TExpr cols a #

sqrt :: TExpr cols a -> TExpr cols a #

(**) :: TExpr cols a -> TExpr cols a -> TExpr cols a #

logBase :: TExpr cols a -> TExpr cols a -> TExpr cols a #

sin :: TExpr cols a -> TExpr cols a #

cos :: TExpr cols a -> TExpr cols a #

tan :: TExpr cols a -> TExpr cols a #

asin :: TExpr cols a -> TExpr cols a #

acos :: TExpr cols a -> TExpr cols a #

atan :: TExpr cols a -> TExpr cols a #

sinh :: TExpr cols a -> TExpr cols a #

cosh :: TExpr cols a -> TExpr cols a #

tanh :: TExpr cols a -> TExpr cols a #

asinh :: TExpr cols a -> TExpr cols a #

acosh :: TExpr cols a -> TExpr cols a #

atanh :: TExpr cols a -> TExpr cols a #

log1p :: TExpr cols a -> TExpr cols a #

expm1 :: TExpr cols a -> TExpr cols a #

log1pexp :: TExpr cols a -> TExpr cols a #

log1mexp :: TExpr cols a -> TExpr cols a #

(Num a, Columnable a) => Num (TExpr cols a) Source # 
Instance details

Defined in DataFrame.Typed.Expr

Methods

(+) :: TExpr cols a -> TExpr cols a -> TExpr cols a #

(-) :: TExpr cols a -> TExpr cols a -> TExpr cols a #

(*) :: TExpr cols a -> TExpr cols a -> TExpr cols a #

negate :: TExpr cols a -> TExpr cols a #

abs :: TExpr cols a -> TExpr cols a #

signum :: TExpr cols a -> TExpr cols a #

fromInteger :: Integer -> TExpr cols a #

(Fractional a, Columnable a) => Fractional (TExpr cols a) Source # 
Instance details

Defined in DataFrame.Typed.Expr

Methods

(/) :: TExpr cols a -> TExpr cols a -> TExpr cols a #

recip :: TExpr cols a -> TExpr cols a #

fromRational :: Rational -> TExpr cols a #

Column reference (schema-checked)

col :: forall (name :: Symbol) (cols :: [Type]) a. (KnownSymbol name, a ~ Lookup name cols, Columnable a, AssertPresent name cols) => TExpr cols a Source #

Create a typed column reference. This is the key type-safety entry point.

The column name must exist in cols and its type must match a. Both checks happen at compile time via type families.

salary :: TExpr '[Column "salary" Double] Double
salary = col @"salary"

Literals

lit :: forall a (cols :: [Type]). Columnable a => a -> TExpr cols a Source #

Create a literal expression. Valid for any schema since it references no columns.

Conditional

ifThenElse :: forall a (cols :: [Type]). Columnable a => TExpr cols Bool -> TExpr cols a -> TExpr cols a -> TExpr cols a Source #

Conditional expression.

Unary / binary lifting

lift :: forall a b (cols :: [Type]). (Columnable a, Columnable b) => (a -> b) -> TExpr cols a -> TExpr cols b Source #

Lift a unary function into a typed expression.

lift2 :: forall a b c (cols :: [Type]). (Columnable a, Columnable b, Columnable c) => (a -> b -> c) -> TExpr cols a -> TExpr cols b -> TExpr cols c Source #

Lift a binary function into typed expressions.

nullLift :: forall a r (cols :: [Type]). (NullLift1Op a r (NullLift1Result a r), Columnable (NullLift1Result a r)) => (BaseType a -> r) -> TExpr cols a -> TExpr cols (NullLift1Result a r) Source #

Typed nullLift: lift a unary function with nullable propagation. When the input is Maybe a, Nothing short-circuits; when plain a, applies directly. The return type is inferred via NullLift1Result: no annotation needed.

nullLift2 :: forall a b r (cols :: [Type]). (NullLift2Op a b r (NullLift2Result a b r), Columnable (NullLift2Result a b r)) => (BaseType a -> BaseType b -> r) -> TExpr cols a -> TExpr cols b -> TExpr cols (NullLift2Result a b r) Source #

Typed nullLift2: lift a binary function with nullable propagation. Any Nothing operand short-circuits to Nothing in the result. The return type is inferred via NullLift2Result: no annotation needed.

Same-type comparison operators

(.==.) :: forall a (cols :: [Type]). (Columnable a, Eq a) => TExpr cols a -> TExpr cols a -> TExpr cols Bool infixl 4 Source #

(./=.) :: forall a (cols :: [Type]). (Columnable a, Eq a) => TExpr cols a -> TExpr cols a -> TExpr cols Bool infixl 4 Source #

(.<.) :: forall a (cols :: [Type]). (Columnable a, Ord a) => TExpr cols a -> TExpr cols a -> TExpr cols Bool infixl 4 Source #

(.<=.) :: forall a (cols :: [Type]). (Columnable a, Ord a) => TExpr cols a -> TExpr cols a -> TExpr cols Bool infixl 4 Source #

(.>=.) :: forall a (cols :: [Type]). (Columnable a, Ord a) => TExpr cols a -> TExpr cols a -> TExpr cols Bool infixl 4 Source #

(.>.) :: forall a (cols :: [Type]). (Columnable a, Ord a) => TExpr cols a -> TExpr cols a -> TExpr cols Bool infixl 4 Source #

Same-type arithmetic operators

(.+.) :: forall a (cols :: [Type]). (Columnable a, Num a) => TExpr cols a -> TExpr cols a -> TExpr cols a infixl 6 Source #

(.-.) :: forall a (cols :: [Type]). (Columnable a, Num a) => TExpr cols a -> TExpr cols a -> TExpr cols a infixl 6 Source #

(.*.) :: forall a (cols :: [Type]). (Columnable a, Num a) => TExpr cols a -> TExpr cols a -> TExpr cols a infixl 7 Source #

(./.) :: forall a (cols :: [Type]). (Columnable a, Fractional a) => TExpr cols a -> TExpr cols a -> TExpr cols a infixl 7 Source #

Same-type exponentiation operators

(.^^.) :: forall a b (cols :: [Type]). (Columnable a, Columnable b, Fractional a, Integral b) => TExpr cols a -> TExpr cols b -> TExpr cols a infixr 8 Source #

(.^.) :: forall a b (cols :: [Type]). (Columnable a, Columnable b, Num a, Integral b) => TExpr cols a -> TExpr cols b -> TExpr cols a infixr 8 Source #

Nullable-aware arithmetic operators

(.+) :: forall a b (cols :: [Type]). (NumericWidenOp (BaseType a) (BaseType b), NullLift2Op a b (Promote (BaseType a) (BaseType b)) (WidenResult a b), Num (Promote (BaseType a) (BaseType b))) => TExpr cols a -> TExpr cols b -> TExpr cols (WidenResult a b) infixl 6 Source #

Nullable-aware addition. Works for all combinations of nullable/non-nullable operands. col @"x" .+ col @"y" -- :: TExpr cols (Maybe Int) when y :: Maybe Int

(.-) :: forall a b (cols :: [Type]). (NumericWidenOp (BaseType a) (BaseType b), NullLift2Op a b (Promote (BaseType a) (BaseType b)) (WidenResult a b), Num (Promote (BaseType a) (BaseType b))) => TExpr cols a -> TExpr cols b -> TExpr cols (WidenResult a b) infixl 6 Source #

Nullable-aware subtraction.

(.*) :: forall a b (cols :: [Type]). (NumericWidenOp (BaseType a) (BaseType b), NullLift2Op a b (Promote (BaseType a) (BaseType b)) (WidenResult a b), Num (Promote (BaseType a) (BaseType b))) => TExpr cols a -> TExpr cols b -> TExpr cols (WidenResult a b) infixl 7 Source #

Nullable-aware multiplication.

(./) :: forall a b (cols :: [Type]). (DivWidenOp (BaseType a) (BaseType b), NullLift2Op a b (PromoteDiv (BaseType a) (BaseType b)) (WidenResultDiv a b), Fractional (PromoteDiv (BaseType a) (BaseType b))) => TExpr cols a -> TExpr cols b -> TExpr cols (WidenResultDiv a b) infixl 7 Source #

Nullable-aware division. Integral operands are promoted to Double.

Nullable-aware exponentiation operators

(.^^) :: forall a b (cols :: [Type]). (Columnable (BaseType a), Columnable (BaseType b), Fractional (BaseType a), Integral (BaseType b), NumericWidenOp (BaseType a) (BaseType b), NullLift2Op a b (BaseType a) a, Num (Promote (BaseType a) (BaseType b))) => TExpr cols a -> TExpr cols b -> TExpr cols a infixr 8 Source #

Nullable-aware exponentiation (fractional base, integral exponent).

(.^) :: forall a b (cols :: [Type]). (Columnable (BaseType a), Columnable (BaseType b), Num (BaseType a), Integral (BaseType b), NumericWidenOp (BaseType a) (BaseType b), NullLift2Op a b (BaseType a) a, Num (Promote (BaseType a) (BaseType b))) => TExpr cols a -> TExpr cols b -> TExpr cols a infixr 8 Source #

Nullable-aware exponentiation (num base, integral exponent).

Nullable-aware comparison operators (three-valued logic)

(.==) :: forall a b (cols :: [Type]). (NullableCmpOp a b (NullCmpResult a b), Eq (BaseType a)) => TExpr cols a -> TExpr cols b -> TExpr cols (NullCmpResult a b) infix 4 Source #

Nullable-aware equality. Returns Maybe Bool when either operand is nullable.

(./=) :: forall a b (cols :: [Type]). (NullableCmpOp a b (NullCmpResult a b), Eq (BaseType a)) => TExpr cols a -> TExpr cols b -> TExpr cols (NullCmpResult a b) infix 4 Source #

Nullable-aware inequality.

(.<) :: forall a b (cols :: [Type]). (NullableCmpOp a b (NullCmpResult a b), Ord (BaseType a)) => TExpr cols a -> TExpr cols b -> TExpr cols (NullCmpResult a b) infix 4 Source #

Nullable-aware less-than.

(.<=) :: forall a b (cols :: [Type]). (NullableCmpOp a b (NullCmpResult a b), Ord (BaseType a)) => TExpr cols a -> TExpr cols b -> TExpr cols (NullCmpResult a b) infix 4 Source #

Nullable-aware less-than-or-equal.

(.>=) :: forall a b (cols :: [Type]). (NullableCmpOp a b (NullCmpResult a b), Ord (BaseType a)) => TExpr cols a -> TExpr cols b -> TExpr cols (NullCmpResult a b) infix 4 Source #

Nullable-aware greater-than-or-equal.

(.>) :: forall a b (cols :: [Type]). (NullableCmpOp a b (NullCmpResult a b), Ord (BaseType a)) => TExpr cols a -> TExpr cols b -> TExpr cols (NullCmpResult a b) infix 4 Source #

Nullable-aware greater-than.

Logical operators

(.&&.) :: forall (cols :: [Type]). TExpr cols Bool -> TExpr cols Bool -> TExpr cols Bool infixr 3 Source #

(.||.) :: forall (cols :: [Type]). TExpr cols Bool -> TExpr cols Bool -> TExpr cols Bool infixr 2 Source #

(.&&) :: forall a b (cols :: [Type]). (NullableCmpOp a b (NullCmpResult a b), BaseType a ~ Bool) => TExpr cols a -> TExpr cols b -> TExpr cols (NullCmpResult a b) infixr 3 Source #

Nullable-aware logical AND. Returns Maybe Bool when either operand is nullable.

(.||) :: forall a b (cols :: [Type]). (NullableCmpOp a b (NullCmpResult a b), BaseType a ~ Bool) => TExpr cols a -> TExpr cols b -> TExpr cols (NullCmpResult a b) infixr 2 Source #

Nullable-aware logical OR. Returns Maybe Bool when either operand is nullable.

not :: forall (cols :: [Type]). TExpr cols Bool -> TExpr cols Bool Source #

Aggregation combinators

sum :: forall a (cols :: [Type]). (Columnable a, Num a) => TExpr cols a -> TExpr cols a Source #

mean :: forall a (cols :: [Type]). (Columnable a, Real a) => TExpr cols a -> TExpr cols Double Source #

count :: forall a (cols :: [Type]). Columnable a => TExpr cols a -> TExpr cols Int Source #

minimum :: forall a (cols :: [Type]). (Columnable a, Ord a) => TExpr cols a -> TExpr cols a Source #

maximum :: forall a (cols :: [Type]). (Columnable a, Ord a) => TExpr cols a -> TExpr cols a Source #

collect :: forall a (cols :: [Type]). Columnable a => TExpr cols a -> TExpr cols [a] Source #

Cast / coercion expressions

castExpr :: forall b (cols :: [Type]) src. (Columnable b, Columnable src) => TExpr cols src -> TExpr cols (Maybe b) Source #

castExprWithDefault :: forall b (cols :: [Type]) src. (Columnable b, Columnable src) => b -> TExpr cols src -> TExpr cols b Source #

castExprEither :: forall b (cols :: [Type]) src. (Columnable b, Columnable src) => TExpr cols src -> TExpr cols (Either Text b) Source #

unsafeCastExpr :: forall b (cols :: [Type]) src. (Columnable b, Columnable src) => TExpr cols src -> TExpr cols b Source #

Named expression helper

as :: forall a (cols :: [Type]). Columnable a => TExpr cols a -> Text -> NamedExpr Source #

Create a NamedExpr for use with aggregateUntyped.

Sort helpers

asc :: forall a (cols :: [Type]). Columnable a => TExpr cols a -> TSortOrder cols Source #

Create an ascending sort order from a typed expression.

desc :: forall a (cols :: [Type]). Columnable a => TExpr cols a -> TSortOrder cols Source #

Create a descending sort order from a typed expression.

Orphan instances

(IsString a, Columnable a) => IsString (TExpr cols a) Source # 
Instance details

Methods

fromString :: String -> TExpr cols a #

(Floating a, Columnable a) => Floating (TExpr cols a) Source # 
Instance details

Methods

pi :: TExpr cols a #

exp :: TExpr cols a -> TExpr cols a #

log :: TExpr cols a -> TExpr cols a #

sqrt :: TExpr cols a -> TExpr cols a #

(**) :: TExpr cols a -> TExpr cols a -> TExpr cols a #

logBase :: TExpr cols a -> TExpr cols a -> TExpr cols a #

sin :: TExpr cols a -> TExpr cols a #

cos :: TExpr cols a -> TExpr cols a #

tan :: TExpr cols a -> TExpr cols a #

asin :: TExpr cols a -> TExpr cols a #

acos :: TExpr cols a -> TExpr cols a #

atan :: TExpr cols a -> TExpr cols a #

sinh :: TExpr cols a -> TExpr cols a #

cosh :: TExpr cols a -> TExpr cols a #

tanh :: TExpr cols a -> TExpr cols a #

asinh :: TExpr cols a -> TExpr cols a #

acosh :: TExpr cols a -> TExpr cols a #

atanh :: TExpr cols a -> TExpr cols a #

log1p :: TExpr cols a -> TExpr cols a #

expm1 :: TExpr cols a -> TExpr cols a #

log1pexp :: TExpr cols a -> TExpr cols a #

log1mexp :: TExpr cols a -> TExpr cols a #

(Num a, Columnable a) => Num (TExpr cols a) Source # 
Instance details

Methods

(+) :: TExpr cols a -> TExpr cols a -> TExpr cols a #

(-) :: TExpr cols a -> TExpr cols a -> TExpr cols a #

(*) :: TExpr cols a -> TExpr cols a -> TExpr cols a #

negate :: TExpr cols a -> TExpr cols a #

abs :: TExpr cols a -> TExpr cols a #

signum :: TExpr cols a -> TExpr cols a #

fromInteger :: Integer -> TExpr cols a #

(Fractional a, Columnable a) => Fractional (TExpr cols a) Source # 
Instance details

Methods

(/) :: TExpr cols a -> TExpr cols a -> TExpr cols a #

recip :: TExpr cols a -> TExpr cols a #

fromRational :: Rational -> TExpr cols a #