Skip to content

Commit 88d6163

Browse files
committed
fix: Single column not found error when using typed dataframe.
1 parent 215dec8 commit 88d6163

6 files changed

Lines changed: 18 additions & 7 deletions

File tree

dataframe.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff 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

src/DataFrame/Typed.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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,

src/DataFrame/Typed/Access.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import GHC.TypeLits (KnownSymbol, symbolVal)
2121
import DataFrame.Internal.Column (Columnable)
2222
import DataFrame.Internal.Expression (Expr (Col))
2323
import qualified DataFrame.Operations.Core as D
24-
import DataFrame.Typed.Schema (AssertPresent, Lookup)
24+
import DataFrame.Typed.Schema (AssertPresent, SafeLookup)
2525
import 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).
3030
columnAsVector ::
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) =
4444
columnAsList ::
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
) =>

src/DataFrame/Typed/Expr.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ import DataFrame.Internal.Nullable (
155155
)
156156
import DataFrame.Internal.Types (Promote, PromoteDiv)
157157

158-
import DataFrame.Typed.Schema (AssertPresent, Lookup)
158+
import DataFrame.Typed.Schema (AssertPresent, SafeLookup)
159159
import DataFrame.Typed.Types (TExpr (..), TSortOrder (..))
160160
import Prelude hiding (maximum, minimum, sum)
161161

@@ -172,7 +172,7 @@ salary = col \@\"salary\"
172172
col ::
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
) =>

src/DataFrame/Typed/Operations.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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

src/DataFrame/Typed/Schema.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
module 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.
8089
type family Impute (name :: Symbol) (cols :: [Type]) :: [Type] where
8190
Impute name (Column name (Maybe a) ': rest) = Column name a ': rest

0 commit comments

Comments
 (0)