Skip to content

Commit b83b451

Browse files
committed
feat: Remove constraints from Columnable' and add them to the call site unless absolutely necessary.
1 parent c121c8f commit b83b451

File tree

20 files changed

+316
-230
lines changed

20 files changed

+316
-230
lines changed

ffi/DataFrame/IR.hs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE ExplicitNamespaces #-}
2+
{-# LANGUAGE FlexibleContexts #-}
23
{-# LANGUAGE GADTs #-}
34
{-# LANGUAGE OverloadedStrings #-}
45
{-# LANGUAGE ScopedTypeVariables #-}
@@ -15,16 +16,19 @@ module DataFrame.IR (
1516

1617
import Data.Aeson (FromJSON (..), withObject, (.:))
1718
import Data.Aeson.Types (Parser)
19+
import qualified Data.ByteString as BS
20+
import Data.Int (Int16, Int32, Int64, Int8)
1821
import qualified Data.Text as T
1922
import Data.Type.Equality (
2023
TestEquality (testEquality),
2124
type (:~:) (Refl),
25+
type (:~~:) (HRefl),
2226
)
2327
import qualified Data.Vector as V
2428
import qualified Data.Vector.Unboxed as VU
25-
import Data.Word (Word64)
29+
import Data.Word (Word, Word16, Word32, Word64, Word8)
2630
import Foreign (wordPtrToPtr)
27-
import Type.Reflection (typeRep)
31+
import Type.Reflection (SomeTypeRep (..), eqTypeRep, typeRep)
2832

2933
import DataFrame.Functions (count, mean, meanMaybe, sumMaybe)
3034
import qualified DataFrame.Functions as Functions
@@ -34,7 +38,7 @@ import DataFrame.IO.CSV (
3438
readSeparated,
3539
readTsv,
3640
)
37-
import DataFrame.Internal.Column (Column (..))
41+
import DataFrame.Internal.Column (Column (..), Columnable)
3842
import DataFrame.Internal.DataFrame (DataFrame, unsafeGetColumn)
3943
import DataFrame.Internal.Expression (Expr (..), NamedExpr)
4044
import DataFrame.Operations.Aggregation (aggregate, groupBy)
@@ -111,16 +115,38 @@ executePlan (Sort cols ascending node) = do
111115
executePlan (Limit k node) =
112116
Subset.take k <$> executePlan node
113117

114-
-- | Build a SortOrder from a column's runtime type.
118+
{- | Build a SortOrder from a column's runtime type.
119+
Uses type dispatch to recover Ord for known column types.
120+
-}
115121
mkSortOrder :: Bool -> T.Text -> Column -> SortOrder
116-
mkSortOrder True n (UnboxedColumn Nothing (_ :: VU.Vector a)) = Asc (Col @a n)
117-
mkSortOrder False n (UnboxedColumn Nothing (_ :: VU.Vector a)) = Desc (Col @a n)
118-
mkSortOrder True n (UnboxedColumn (Just _) (_ :: VU.Vector a)) = Asc (Col @(Maybe a) n)
119-
mkSortOrder False n (UnboxedColumn (Just _) (_ :: VU.Vector a)) = Desc (Col @(Maybe a) n)
120-
mkSortOrder True n (BoxedColumn Nothing (_ :: V.Vector a)) = Asc (Col @a n)
121-
mkSortOrder False n (BoxedColumn Nothing (_ :: V.Vector a)) = Desc (Col @a n)
122-
mkSortOrder True n (BoxedColumn (Just _) (_ :: V.Vector a)) = Asc (Col @(Maybe a) n)
123-
mkSortOrder False n (BoxedColumn (Just _) (_ :: V.Vector a)) = Desc (Col @(Maybe a) n)
122+
mkSortOrder isAsc name col = dispatchType (columnTypeRep col)
123+
where
124+
columnTypeRep :: Column -> SomeTypeRep
125+
columnTypeRep (UnboxedColumn _ (_ :: VU.Vector a)) = SomeTypeRep (typeRep @a)
126+
columnTypeRep (BoxedColumn _ (_ :: V.Vector a)) = SomeTypeRep (typeRep @a)
127+
mk :: (Columnable a, Ord a) => Expr a -> SortOrder
128+
mk = if isAsc then Asc else Desc
129+
dispatchType (SomeTypeRep tr)
130+
| Just HRefl <- eqTypeRep tr (typeRep @Int) = mk (Col @Int name)
131+
| Just HRefl <- eqTypeRep tr (typeRep @Int8) = mk (Col @Int8 name)
132+
| Just HRefl <- eqTypeRep tr (typeRep @Int16) = mk (Col @Int16 name)
133+
| Just HRefl <- eqTypeRep tr (typeRep @Int32) = mk (Col @Int32 name)
134+
| Just HRefl <- eqTypeRep tr (typeRep @Int64) = mk (Col @Int64 name)
135+
| Just HRefl <- eqTypeRep tr (typeRep @Word) = mk (Col @Word name)
136+
| Just HRefl <- eqTypeRep tr (typeRep @Word8) = mk (Col @Word8 name)
137+
| Just HRefl <- eqTypeRep tr (typeRep @Word16) = mk (Col @Word16 name)
138+
| Just HRefl <- eqTypeRep tr (typeRep @Word32) = mk (Col @Word32 name)
139+
| Just HRefl <- eqTypeRep tr (typeRep @Word64) = mk (Col @Word64 name)
140+
| Just HRefl <- eqTypeRep tr (typeRep @Integer) = mk (Col @Integer name)
141+
| Just HRefl <- eqTypeRep tr (typeRep @Double) = mk (Col @Double name)
142+
| Just HRefl <- eqTypeRep tr (typeRep @Float) = mk (Col @Float name)
143+
| Just HRefl <- eqTypeRep tr (typeRep @Bool) = mk (Col @Bool name)
144+
| Just HRefl <- eqTypeRep tr (typeRep @Char) = mk (Col @Char name)
145+
| Just HRefl <- eqTypeRep tr (typeRep @T.Text) = mk (Col @T.Text name)
146+
| Just HRefl <- eqTypeRep tr (typeRep @String) = mk (Col @String name)
147+
| Just HRefl <- eqTypeRep tr (typeRep @BS.ByteString) =
148+
mk (Col @BS.ByteString name)
149+
| otherwise = error $ "mkSortOrder: unsupported column type: " ++ show tr
124150

125151
-- | Dispatch aggregation by fn name and runtime column type.
126152
buildNamedExpr :: DataFrame -> AggSpec -> IO NamedExpr

src/DataFrame.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ import DataFrame.Internal.DataFrame as Dataframe (
282282
empty,
283283
null,
284284
toCsv,
285+
toCsv',
285286
toMarkdown,
286287
toMarkdown',
287288
toSeparated,

0 commit comments

Comments
 (0)