File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -27,7 +27,8 @@ extra-source-files: cbits/arrow_abi.h
2727 tests/data/*.csv
2828 tests/data/*.tsv
2929 tests/data/*.parquet
30- -- unstable_csv test data moved to dataframe-fastcsv
30+ tests/data/unstable_csv/*.csv
31+ tests/data/unstable_csv/*.tsv
3132 -- tests/data/*.md
3233 -- tests/data/*.bin
3334 -- tests/data/*.json
Original file line number Diff line number Diff line change @@ -187,6 +187,7 @@ module DataFrame.Typed (
187187
188188 -- * Schema type families (for advanced use)
189189 Lookup ,
190+ SafeLookup ,
190191 HasName ,
191192 SubsetSchema ,
192193 ExcludeSchema ,
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ import GHC.TypeLits (KnownSymbol, symbolVal)
2121import DataFrame.Internal.Column (Columnable )
2222import DataFrame.Internal.Expression (Expr (Col ))
2323import qualified DataFrame.Operations.Core as D
24- import DataFrame.Typed.Schema (AssertPresent , Lookup )
24+ import DataFrame.Typed.Schema (AssertPresent , SafeLookup )
2525import DataFrame.Typed.Types (TypedDataFrame (.. ))
2626
2727{- | Retrieve a column as a boxed 'Vector', with the type determined by
@@ -30,7 +30,7 @@ the schema. The column must exist (enforced at compile time).
3030columnAsVector ::
3131 forall name cols a .
3232 ( KnownSymbol name
33- , a ~ Lookup name cols
33+ , a ~ SafeLookup name cols
3434 , Columnable a
3535 , AssertPresent name cols
3636 ) =>
@@ -44,7 +44,7 @@ columnAsVector (TDF df) =
4444columnAsList ::
4545 forall name cols a .
4646 ( KnownSymbol name
47- , a ~ Lookup name cols
47+ , a ~ SafeLookup name cols
4848 , Columnable a
4949 , AssertPresent name cols
5050 ) =>
Original file line number Diff line number Diff line change @@ -155,7 +155,7 @@ import DataFrame.Internal.Nullable (
155155 )
156156import DataFrame.Internal.Types (Promote , PromoteDiv )
157157
158- import DataFrame.Typed.Schema (AssertPresent , Lookup )
158+ import DataFrame.Typed.Schema (AssertPresent , SafeLookup )
159159import DataFrame.Typed.Types (TExpr (.. ), TSortOrder (.. ))
160160import Prelude hiding (maximum , minimum , sum )
161161
@@ -172,7 +172,7 @@ salary = col \@\"salary\"
172172col ::
173173 forall (name :: Symbol ) cols a .
174174 ( KnownSymbol name
175- , a ~ Lookup name cols
175+ , a ~ SafeLookup name cols
176176 , Columnable a
177177 , AssertPresent name cols
178178 ) =>
Original file line number Diff line number Diff line change @@ -328,7 +328,7 @@ replaceColumn ::
328328 forall name a cols .
329329 ( KnownSymbol name
330330 , Columnable a
331- , a ~ Lookup name cols
331+ , a ~ SafeLookup name cols
332332 , AssertPresent name cols
333333 ) =>
334334 TExpr cols a -> TypedDataFrame cols -> TypedDataFrame cols
Original file line number Diff line number Diff line change 1616module DataFrame.Typed.Schema (
1717 -- * Type families for schema manipulation
1818 Lookup ,
19+ SafeLookup ,
1920 HasName ,
2021 RemoveColumn ,
2122 Impute ,
@@ -76,6 +77,14 @@ type family Lookup (name :: Symbol) (cols :: [Type]) :: Type where
7677 TypeError
7778 ('Text " Column '" ':<>: 'Text name ':<>: 'Text " ' not found in schema" )
7879
80+ -- | Like 'Lookup', but returns a harmless fallback ('Int') instead of
81+ -- 'TypeError' when the column is not found. Use together with
82+ -- 'AssertPresent' so the error fires exactly once.
83+ type family SafeLookup (name :: Symbol ) (cols :: [Type ]) :: Type where
84+ SafeLookup name (Column name a ': _ ) = a
85+ SafeLookup name (Column _ _ ': rest ) = SafeLookup name rest
86+ SafeLookup name '[] = Int
87+
7988-- | Unwrap a Maybe from a type after we impute values.
8089type family Impute (name :: Symbol ) (cols :: [Type ]) :: [Type ] where
8190 Impute name (Column name (Maybe a ) ': rest ) = Column name a ': rest
You can’t perform that action at this time.
0 commit comments