{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module DataFrame.Operations.Typing where

import qualified Data.Map as M
import qualified Data.Text as T
import qualified Data.Vector as V

import Control.Applicative (asum)
import Control.Monad (join)
import Data.Maybe (fromMaybe)
import qualified Data.Proxy as P
import Data.Time
import Data.Type.Equality (TestEquality (..))
import DataFrame.Internal.Column (Column (..), fromVector)
import DataFrame.Internal.DataFrame (DataFrame (..), unsafeGetColumn)
import DataFrame.Internal.Parsing
import DataFrame.Internal.Schema
import DataFrame.Operations.Core
import Text.Read
import Type.Reflection

type DateFormat = String


data ParseOptions = ParseOptions
    { ParseOptions -> [Text]
missingValues :: [T.Text]
    
    , ParseOptions -> Int
sampleSize :: Int
    
    , ParseOptions -> Bool
parseSafe :: Bool
    
    , ParseOptions -> DateFormat
parseDateFormat :: DateFormat
    
    }


defaultParseOptions :: ParseOptions
defaultParseOptions :: ParseOptions
defaultParseOptions =
    ParseOptions
        { missingValues :: [Text]
missingValues = []
        , sampleSize :: Int
sampleSize = Int
100
        , parseSafe :: Bool
parseSafe = Bool
True
        , parseDateFormat :: DateFormat
parseDateFormat = DateFormat
"%Y-%m-%d"
        }

parseDefaults :: ParseOptions -> DataFrame -> DataFrame
parseDefaults :: ParseOptions -> DataFrame -> DataFrame
parseDefaults ParseOptions
opts DataFrame
df = DataFrame
df{columns = V.map (parseDefault opts) (columns df)}

parseDefault :: ParseOptions -> Column -> Column
parseDefault :: ParseOptions -> Column -> Column
parseDefault ParseOptions
opts (BoxedColumn (Vector a
c :: V.Vector a)) =
    case (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @a) TypeRep a -> TypeRep Text -> Maybe (a :~: Text)
forall a b. TypeRep a -> TypeRep b -> Maybe (a :~: b)
forall {k} (f :: k -> *) (a :: k) (b :: k).
TestEquality f =>
f a -> f b -> Maybe (a :~: b)
`testEquality` (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @T.Text) of
        Maybe (a :~: Text)
Nothing -> case (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @a) TypeRep a -> TypeRep DateFormat -> Maybe (a :~: DateFormat)
forall a b. TypeRep a -> TypeRep b -> Maybe (a :~: b)
forall {k} (f :: k -> *) (a :: k) (b :: k).
TestEquality f =>
f a -> f b -> Maybe (a :~: b)
`testEquality` (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @String) of
            Just a :~: DateFormat
Refl -> ParseOptions -> Vector Text -> Column
parseFromExamples ParseOptions
opts ((DateFormat -> Text) -> Vector DateFormat -> Vector Text
forall a b. (a -> b) -> Vector a -> Vector b
V.map DateFormat -> Text
T.pack Vector a
Vector DateFormat
c)
            Maybe (a :~: DateFormat)
Nothing -> Vector a -> Column
forall a. Columnable a => Vector a -> Column
BoxedColumn Vector a
c
        Just a :~: Text
Refl -> ParseOptions -> Vector Text -> Column
parseFromExamples ParseOptions
opts Vector a
Vector Text
c
parseDefault ParseOptions
opts (OptionalColumn (Vector (Maybe a)
c :: V.Vector (Maybe a))) =
    case (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @a) TypeRep a -> TypeRep Text -> Maybe (a :~: Text)
forall a b. TypeRep a -> TypeRep b -> Maybe (a :~: b)
forall {k} (f :: k -> *) (a :: k) (b :: k).
TestEquality f =>
f a -> f b -> Maybe (a :~: b)
`testEquality` (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @T.Text) of
        Maybe (a :~: Text)
Nothing -> case (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @a) TypeRep a -> TypeRep DateFormat -> Maybe (a :~: DateFormat)
forall a b. TypeRep a -> TypeRep b -> Maybe (a :~: b)
forall {k} (f :: k -> *) (a :: k) (b :: k).
TestEquality f =>
f a -> f b -> Maybe (a :~: b)
`testEquality` (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @String) of
            Just a :~: DateFormat
Refl ->
                ParseOptions -> Vector Text -> Column
parseFromExamples ParseOptions
opts ((Maybe DateFormat -> Text)
-> Vector (Maybe DateFormat) -> Vector Text
forall a b. (a -> b) -> Vector a -> Vector b
V.map (DateFormat -> Text
T.pack (DateFormat -> Text)
-> (Maybe DateFormat -> DateFormat) -> Maybe DateFormat -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DateFormat -> Maybe DateFormat -> DateFormat
forall a. a -> Maybe a -> a
fromMaybe DateFormat
"") Vector (Maybe a)
Vector (Maybe DateFormat)
c)
            Maybe (a :~: DateFormat)
Nothing -> Vector (Maybe a) -> Column
forall a. Columnable a => Vector a -> Column
BoxedColumn Vector (Maybe a)
c
        Just a :~: Text
Refl -> ParseOptions -> Vector Text -> Column
parseFromExamples ParseOptions
opts ((Maybe Text -> Text) -> Vector (Maybe Text) -> Vector Text
forall a b. (a -> b) -> Vector a -> Vector b
V.map (Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"") Vector (Maybe a)
Vector (Maybe Text)
c)
parseDefault ParseOptions
_ Column
column = Column
column

parseFromExamples :: ParseOptions -> V.Vector T.Text -> Column
parseFromExamples :: ParseOptions -> Vector Text -> Column
parseFromExamples ParseOptions
opts Vector Text
cols =
    let
        converter :: Text -> Maybe Text
converter =
            if ParseOptions -> Bool
parseSafe ParseOptions
opts then [Text] -> Text -> Maybe Text
convertNullish (ParseOptions -> [Text]
missingValues ParseOptions
opts) else Text -> Maybe Text
convertOnlyEmpty
        examples :: Vector (Maybe Text)
examples = (Text -> Maybe Text) -> Vector Text -> Vector (Maybe Text)
forall a b. (a -> b) -> Vector a -> Vector b
V.map Text -> Maybe Text
converter (Int -> Vector Text -> Vector Text
forall a. Int -> Vector a -> Vector a
V.take (ParseOptions -> Int
sampleSize ParseOptions
opts) Vector Text
cols)
        asMaybeText :: Vector (Maybe Text)
asMaybeText = (Text -> Maybe Text) -> Vector Text -> Vector (Maybe Text)
forall a b. (a -> b) -> Vector a -> Vector b
V.map Text -> Maybe Text
converter Vector Text
cols
        dfmt :: DateFormat
dfmt = ParseOptions -> DateFormat
parseDateFormat ParseOptions
opts
     in
        case DateFormat -> Vector (Maybe Text) -> ParsingAssumption
makeParsingAssumption DateFormat
dfmt Vector (Maybe Text)
examples of
            ParsingAssumption
BoolAssumption -> Vector (Maybe Text) -> Column
handleBoolAssumption Vector (Maybe Text)
asMaybeText
            ParsingAssumption
IntAssumption -> Vector (Maybe Text) -> Column
handleIntAssumption Vector (Maybe Text)
asMaybeText
            ParsingAssumption
DoubleAssumption -> Vector (Maybe Text) -> Column
handleDoubleAssumption Vector (Maybe Text)
asMaybeText
            ParsingAssumption
TextAssumption -> Vector (Maybe Text) -> Column
handleTextAssumption Vector (Maybe Text)
asMaybeText
            ParsingAssumption
DateAssumption -> DateFormat -> Vector (Maybe Text) -> Column
handleDateAssumption DateFormat
dfmt Vector (Maybe Text)
asMaybeText
            ParsingAssumption
NoAssumption -> DateFormat -> Vector (Maybe Text) -> Column
handleNoAssumption DateFormat
dfmt Vector (Maybe Text)
asMaybeText

handleBoolAssumption :: V.Vector (Maybe T.Text) -> Column
handleBoolAssumption :: Vector (Maybe Text) -> Column
handleBoolAssumption Vector (Maybe Text)
asMaybeText
    | Bool
parsableAsBool =
        Column -> (Vector Bool -> Column) -> Maybe (Vector Bool) -> Column
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector (Maybe Bool) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector Vector (Maybe Bool)
asMaybeBool) Vector Bool -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector (Vector (Maybe Bool) -> Maybe (Vector Bool)
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a.
Applicative f =>
Vector (f a) -> f (Vector a)
sequenceA Vector (Maybe Bool)
asMaybeBool)
    | Bool
otherwise = Column -> (Vector Text -> Column) -> Maybe (Vector Text) -> Column
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector (Maybe Text) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector Vector (Maybe Text)
asMaybeText) Vector Text -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector (Vector (Maybe Text) -> Maybe (Vector Text)
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a.
Applicative f =>
Vector (f a) -> f (Vector a)
sequenceA Vector (Maybe Text)
asMaybeText)
  where
    asMaybeBool :: Vector (Maybe Bool)
asMaybeBool = (Maybe Text -> Maybe Bool)
-> Vector (Maybe Text) -> Vector (Maybe Bool)
forall a b. (a -> b) -> Vector a -> Vector b
V.map (Maybe Text -> (Text -> Maybe Bool) -> Maybe Bool
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= HasCallStack => Text -> Maybe Bool
Text -> Maybe Bool
readBool) Vector (Maybe Text)
asMaybeText
    parsableAsBool :: Bool
parsableAsBool = Vector (Maybe Text) -> Vector (Maybe Bool) -> Bool
forall a b. Vector (Maybe a) -> Vector (Maybe b) -> Bool
vecSameConstructor Vector (Maybe Text)
asMaybeText Vector (Maybe Bool)
asMaybeBool

handleIntAssumption :: V.Vector (Maybe T.Text) -> Column
handleIntAssumption :: Vector (Maybe Text) -> Column
handleIntAssumption Vector (Maybe Text)
asMaybeText
    | Bool
parsableAsInt =
        Column -> (Vector Int -> Column) -> Maybe (Vector Int) -> Column
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector (Maybe Int) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector Vector (Maybe Int)
asMaybeInt) Vector Int -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector (Vector (Maybe Int) -> Maybe (Vector Int)
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a.
Applicative f =>
Vector (f a) -> f (Vector a)
sequenceA Vector (Maybe Int)
asMaybeInt)
    | Bool
parsableAsDouble =
        Column
-> (Vector Double -> Column) -> Maybe (Vector Double) -> Column
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector (Maybe Double) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector Vector (Maybe Double)
asMaybeDouble) Vector Double -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector (Vector (Maybe Double) -> Maybe (Vector Double)
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a.
Applicative f =>
Vector (f a) -> f (Vector a)
sequenceA Vector (Maybe Double)
asMaybeDouble)
    | Bool
otherwise = Column -> (Vector Text -> Column) -> Maybe (Vector Text) -> Column
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector (Maybe Text) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector Vector (Maybe Text)
asMaybeText) Vector Text -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector (Vector (Maybe Text) -> Maybe (Vector Text)
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a.
Applicative f =>
Vector (f a) -> f (Vector a)
sequenceA Vector (Maybe Text)
asMaybeText)
  where
    asMaybeInt :: Vector (Maybe Int)
asMaybeInt = (Maybe Text -> Maybe Int)
-> Vector (Maybe Text) -> Vector (Maybe Int)
forall a b. (a -> b) -> Vector a -> Vector b
V.map (Maybe Text -> (Text -> Maybe Int) -> Maybe Int
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= HasCallStack => Text -> Maybe Int
Text -> Maybe Int
readInt) Vector (Maybe Text)
asMaybeText
    asMaybeDouble :: Vector (Maybe Double)
asMaybeDouble = (Maybe Text -> Maybe Double)
-> Vector (Maybe Text) -> Vector (Maybe Double)
forall a b. (a -> b) -> Vector a -> Vector b
V.map (Maybe Text -> (Text -> Maybe Double) -> Maybe Double
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= HasCallStack => Text -> Maybe Double
Text -> Maybe Double
readDouble) Vector (Maybe Text)
asMaybeText
    parsableAsInt :: Bool
parsableAsInt =
        Vector (Maybe Text) -> Vector (Maybe Int) -> Bool
forall a b. Vector (Maybe a) -> Vector (Maybe b) -> Bool
vecSameConstructor Vector (Maybe Text)
asMaybeText Vector (Maybe Int)
asMaybeInt
            Bool -> Bool -> Bool
&& Vector (Maybe Text) -> Vector (Maybe Double) -> Bool
forall a b. Vector (Maybe a) -> Vector (Maybe b) -> Bool
vecSameConstructor Vector (Maybe Text)
asMaybeText Vector (Maybe Double)
asMaybeDouble
    parsableAsDouble :: Bool
parsableAsDouble = Vector (Maybe Text) -> Vector (Maybe Double) -> Bool
forall a b. Vector (Maybe a) -> Vector (Maybe b) -> Bool
vecSameConstructor Vector (Maybe Text)
asMaybeText Vector (Maybe Double)
asMaybeDouble

handleDoubleAssumption :: V.Vector (Maybe T.Text) -> Column
handleDoubleAssumption :: Vector (Maybe Text) -> Column
handleDoubleAssumption Vector (Maybe Text)
asMaybeText
    | Bool
parsableAsDouble =
        Column
-> (Vector Double -> Column) -> Maybe (Vector Double) -> Column
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector (Maybe Double) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector Vector (Maybe Double)
asMaybeDouble) Vector Double -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector (Vector (Maybe Double) -> Maybe (Vector Double)
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a.
Applicative f =>
Vector (f a) -> f (Vector a)
sequenceA Vector (Maybe Double)
asMaybeDouble)
    | Bool
otherwise = Column -> (Vector Text -> Column) -> Maybe (Vector Text) -> Column
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector (Maybe Text) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector Vector (Maybe Text)
asMaybeText) Vector Text -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector (Vector (Maybe Text) -> Maybe (Vector Text)
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a.
Applicative f =>
Vector (f a) -> f (Vector a)
sequenceA Vector (Maybe Text)
asMaybeText)
  where
    asMaybeDouble :: Vector (Maybe Double)
asMaybeDouble = (Maybe Text -> Maybe Double)
-> Vector (Maybe Text) -> Vector (Maybe Double)
forall a b. (a -> b) -> Vector a -> Vector b
V.map (Maybe Text -> (Text -> Maybe Double) -> Maybe Double
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= HasCallStack => Text -> Maybe Double
Text -> Maybe Double
readDouble) Vector (Maybe Text)
asMaybeText
    parsableAsDouble :: Bool
parsableAsDouble = Vector (Maybe Text) -> Vector (Maybe Double) -> Bool
forall a b. Vector (Maybe a) -> Vector (Maybe b) -> Bool
vecSameConstructor Vector (Maybe Text)
asMaybeText Vector (Maybe Double)
asMaybeDouble

handleDateAssumption :: DateFormat -> V.Vector (Maybe T.Text) -> Column
handleDateAssumption :: DateFormat -> Vector (Maybe Text) -> Column
handleDateAssumption DateFormat
dateFormat Vector (Maybe Text)
asMaybeText
    | Bool
parsableAsDate =
        Column -> (Vector Day -> Column) -> Maybe (Vector Day) -> Column
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector (Maybe Day) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector Vector (Maybe Day)
asMaybeDate) Vector Day -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector (Vector (Maybe Day) -> Maybe (Vector Day)
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a.
Applicative f =>
Vector (f a) -> f (Vector a)
sequenceA Vector (Maybe Day)
asMaybeDate)
    | Bool
otherwise = Column -> (Vector Text -> Column) -> Maybe (Vector Text) -> Column
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector (Maybe Text) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector Vector (Maybe Text)
asMaybeText) Vector Text -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector (Vector (Maybe Text) -> Maybe (Vector Text)
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a.
Applicative f =>
Vector (f a) -> f (Vector a)
sequenceA Vector (Maybe Text)
asMaybeText)
  where
    asMaybeDate :: Vector (Maybe Day)
asMaybeDate = (Maybe Text -> Maybe Day)
-> Vector (Maybe Text) -> Vector (Maybe Day)
forall a b. (a -> b) -> Vector a -> Vector b
V.map (Maybe Text -> (Text -> Maybe Day) -> Maybe Day
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= DateFormat -> Text -> Maybe Day
parseTimeOpt DateFormat
dateFormat) Vector (Maybe Text)
asMaybeText
    parsableAsDate :: Bool
parsableAsDate = Vector (Maybe Text) -> Vector (Maybe Day) -> Bool
forall a b. Vector (Maybe a) -> Vector (Maybe b) -> Bool
vecSameConstructor Vector (Maybe Text)
asMaybeText Vector (Maybe Day)
asMaybeDate

handleTextAssumption :: V.Vector (Maybe T.Text) -> Column
handleTextAssumption :: Vector (Maybe Text) -> Column
handleTextAssumption Vector (Maybe Text)
asMaybeText = Column -> (Vector Text -> Column) -> Maybe (Vector Text) -> Column
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Vector (Maybe Text) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector Vector (Maybe Text)
asMaybeText) Vector Text -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector (Vector (Maybe Text) -> Maybe (Vector Text)
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a.
Applicative f =>
Vector (f a) -> f (Vector a)
sequenceA Vector (Maybe Text)
asMaybeText)

handleNoAssumption :: DateFormat -> V.Vector (Maybe T.Text) -> Column
handleNoAssumption :: DateFormat -> Vector (Maybe Text) -> Column
handleNoAssumption DateFormat
dateFormat Vector (Maybe Text)
asMaybeText
    
    
    
    | (Maybe Text -> Bool) -> Vector (Maybe Text) -> Bool
forall a. (a -> Bool) -> Vector a -> Bool
V.all (Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe Text
forall a. Maybe a
Nothing) Vector (Maybe Text)
asMaybeText = Vector (Maybe Text) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector Vector (Maybe Text)
asMaybeText
    | Bool
parsableAsBool = Vector (Maybe Bool) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector Vector (Maybe Bool)
asMaybeBool
    | Bool
parsableAsInt = Vector (Maybe Int) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector Vector (Maybe Int)
asMaybeInt
    | Bool
parsableAsDouble = Vector (Maybe Double) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector Vector (Maybe Double)
asMaybeDouble
    | Bool
parsableAsDate = Vector (Maybe Day) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector Vector (Maybe Day)
asMaybeDate
    | Bool
otherwise = Vector (Maybe Text) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector Vector (Maybe Text)
asMaybeText
  where
    asMaybeBool :: Vector (Maybe Bool)
asMaybeBool = (Maybe Text -> Maybe Bool)
-> Vector (Maybe Text) -> Vector (Maybe Bool)
forall a b. (a -> b) -> Vector a -> Vector b
V.map (Maybe Text -> (Text -> Maybe Bool) -> Maybe Bool
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= HasCallStack => Text -> Maybe Bool
Text -> Maybe Bool
readBool) Vector (Maybe Text)
asMaybeText
    asMaybeInt :: Vector (Maybe Int)
asMaybeInt = (Maybe Text -> Maybe Int)
-> Vector (Maybe Text) -> Vector (Maybe Int)
forall a b. (a -> b) -> Vector a -> Vector b
V.map (Maybe Text -> (Text -> Maybe Int) -> Maybe Int
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= HasCallStack => Text -> Maybe Int
Text -> Maybe Int
readInt) Vector (Maybe Text)
asMaybeText
    asMaybeDouble :: Vector (Maybe Double)
asMaybeDouble = (Maybe Text -> Maybe Double)
-> Vector (Maybe Text) -> Vector (Maybe Double)
forall a b. (a -> b) -> Vector a -> Vector b
V.map (Maybe Text -> (Text -> Maybe Double) -> Maybe Double
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= HasCallStack => Text -> Maybe Double
Text -> Maybe Double
readDouble) Vector (Maybe Text)
asMaybeText
    asMaybeDate :: Vector (Maybe Day)
asMaybeDate = (Maybe Text -> Maybe Day)
-> Vector (Maybe Text) -> Vector (Maybe Day)
forall a b. (a -> b) -> Vector a -> Vector b
V.map (Maybe Text -> (Text -> Maybe Day) -> Maybe Day
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= DateFormat -> Text -> Maybe Day
parseTimeOpt DateFormat
dateFormat) Vector (Maybe Text)
asMaybeText
    parsableAsBool :: Bool
parsableAsBool = Vector (Maybe Text) -> Vector (Maybe Bool) -> Bool
forall a b. Vector (Maybe a) -> Vector (Maybe b) -> Bool
vecSameConstructor Vector (Maybe Text)
asMaybeText Vector (Maybe Bool)
asMaybeBool
    parsableAsInt :: Bool
parsableAsInt =
        Vector (Maybe Text) -> Vector (Maybe Int) -> Bool
forall a b. Vector (Maybe a) -> Vector (Maybe b) -> Bool
vecSameConstructor Vector (Maybe Text)
asMaybeText Vector (Maybe Int)
asMaybeInt
            Bool -> Bool -> Bool
&& Vector (Maybe Text) -> Vector (Maybe Double) -> Bool
forall a b. Vector (Maybe a) -> Vector (Maybe b) -> Bool
vecSameConstructor Vector (Maybe Text)
asMaybeText Vector (Maybe Double)
asMaybeDouble
    parsableAsDouble :: Bool
parsableAsDouble = Vector (Maybe Text) -> Vector (Maybe Double) -> Bool
forall a b. Vector (Maybe a) -> Vector (Maybe b) -> Bool
vecSameConstructor Vector (Maybe Text)
asMaybeText Vector (Maybe Double)
asMaybeDouble
    parsableAsDate :: Bool
parsableAsDate = Vector (Maybe Text) -> Vector (Maybe Day) -> Bool
forall a b. Vector (Maybe a) -> Vector (Maybe b) -> Bool
vecSameConstructor Vector (Maybe Text)
asMaybeText Vector (Maybe Day)
asMaybeDate

convertNullish :: [T.Text] -> T.Text -> Maybe T.Text
convertNullish :: [Text] -> Text -> Maybe Text
convertNullish [Text]
missing Text
v = if Text -> Bool
isNullish Text
v Bool -> Bool -> Bool
|| Text
v Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
missing then Maybe Text
forall a. Maybe a
Nothing else Text -> Maybe Text
forall a. a -> Maybe a
Just Text
v

convertOnlyEmpty :: T.Text -> Maybe T.Text
convertOnlyEmpty :: Text -> Maybe Text
convertOnlyEmpty Text
v = if Text
v Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"" then Maybe Text
forall a. Maybe a
Nothing else Text -> Maybe Text
forall a. a -> Maybe a
Just Text
v

parseTimeOpt :: DateFormat -> T.Text -> Maybe Day
parseTimeOpt :: DateFormat -> Text -> Maybe Day
parseTimeOpt DateFormat
dateFormat Text
s =
    Bool -> TimeLocale -> DateFormat -> DateFormat -> Maybe Day
forall (m :: * -> *) t.
(MonadFail m, ParseTime t) =>
Bool -> TimeLocale -> DateFormat -> DateFormat -> m t
parseTimeM 
        Bool
True
        TimeLocale
defaultTimeLocale
        DateFormat
dateFormat
        (Text -> DateFormat
T.unpack Text
s)

unsafeParseTime :: DateFormat -> T.Text -> Day
unsafeParseTime :: DateFormat -> Text -> Day
unsafeParseTime DateFormat
dateFormat Text
s =
    Bool -> TimeLocale -> DateFormat -> DateFormat -> Day
forall t.
ParseTime t =>
Bool -> TimeLocale -> DateFormat -> DateFormat -> t
parseTimeOrError 
        Bool
True
        TimeLocale
defaultTimeLocale
        DateFormat
dateFormat
        (Text -> DateFormat
T.unpack Text
s)

hasNullValues :: (Eq a) => V.Vector (Maybe a) -> Bool
hasNullValues :: forall a. Eq a => Vector (Maybe a) -> Bool
hasNullValues = (Maybe a -> Bool) -> Vector (Maybe a) -> Bool
forall a. (a -> Bool) -> Vector a -> Bool
V.any (Maybe a -> Maybe a -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe a
forall a. Maybe a
Nothing)

vecSameConstructor :: V.Vector (Maybe a) -> V.Vector (Maybe b) -> Bool
vecSameConstructor :: forall a b. Vector (Maybe a) -> Vector (Maybe b) -> Bool
vecSameConstructor Vector (Maybe a)
xs Vector (Maybe b)
ys = (Vector (Maybe a) -> Int
forall a. Vector a -> Int
V.length Vector (Maybe a)
xs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Vector (Maybe b) -> Int
forall a. Vector a -> Int
V.length Vector (Maybe b)
ys) Bool -> Bool -> Bool
&& Vector Bool -> Bool
V.and ((Maybe a -> Maybe b -> Bool)
-> Vector (Maybe a) -> Vector (Maybe b) -> Vector Bool
forall a b c. (a -> b -> c) -> Vector a -> Vector b -> Vector c
V.zipWith Maybe a -> Maybe b -> Bool
forall a b. Maybe a -> Maybe b -> Bool
hasSameConstructor Vector (Maybe a)
xs Vector (Maybe b)
ys)
  where
    hasSameConstructor :: Maybe a -> Maybe b -> Bool
    hasSameConstructor :: forall a b. Maybe a -> Maybe b -> Bool
hasSameConstructor (Just a
_) (Just b
_) = Bool
True
    hasSameConstructor Maybe a
Nothing Maybe b
Nothing = Bool
True
    hasSameConstructor Maybe a
_ Maybe b
_ = Bool
False

makeParsingAssumption ::
    DateFormat -> V.Vector (Maybe T.Text) -> ParsingAssumption
makeParsingAssumption :: DateFormat -> Vector (Maybe Text) -> ParsingAssumption
makeParsingAssumption DateFormat
dateFormat Vector (Maybe Text)
asMaybeText
    
    
    | (Maybe Text -> Bool) -> Vector (Maybe Text) -> Bool
forall a. (a -> Bool) -> Vector a -> Bool
V.all (Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe Text
forall a. Maybe a
Nothing) Vector (Maybe Text)
asMaybeText = ParsingAssumption
NoAssumption
    
    
    
    | Vector (Maybe Text) -> Vector (Maybe Bool) -> Bool
forall a b. Vector (Maybe a) -> Vector (Maybe b) -> Bool
vecSameConstructor Vector (Maybe Text)
asMaybeText Vector (Maybe Bool)
asMaybeBool = ParsingAssumption
BoolAssumption
    | Vector (Maybe Text) -> Vector (Maybe Int) -> Bool
forall a b. Vector (Maybe a) -> Vector (Maybe b) -> Bool
vecSameConstructor Vector (Maybe Text)
asMaybeText Vector (Maybe Int)
asMaybeInt
        Bool -> Bool -> Bool
&& Vector (Maybe Text) -> Vector (Maybe Double) -> Bool
forall a b. Vector (Maybe a) -> Vector (Maybe b) -> Bool
vecSameConstructor Vector (Maybe Text)
asMaybeText Vector (Maybe Double)
asMaybeDouble =
        ParsingAssumption
IntAssumption
    
    
    | Vector (Maybe Text) -> Vector (Maybe Double) -> Bool
forall a b. Vector (Maybe a) -> Vector (Maybe b) -> Bool
vecSameConstructor Vector (Maybe Text)
asMaybeText Vector (Maybe Double)
asMaybeDouble = ParsingAssumption
DoubleAssumption
    
    
    | Vector (Maybe Text) -> Vector (Maybe Day) -> Bool
forall a b. Vector (Maybe a) -> Vector (Maybe b) -> Bool
vecSameConstructor Vector (Maybe Text)
asMaybeText Vector (Maybe Day)
asMaybeDate = ParsingAssumption
DateAssumption
    | Bool
otherwise = ParsingAssumption
TextAssumption
  where
    asMaybeBool :: Vector (Maybe Bool)
asMaybeBool = (Maybe Text -> Maybe Bool)
-> Vector (Maybe Text) -> Vector (Maybe Bool)
forall a b. (a -> b) -> Vector a -> Vector b
V.map (Maybe Text -> (Text -> Maybe Bool) -> Maybe Bool
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= HasCallStack => Text -> Maybe Bool
Text -> Maybe Bool
readBool) Vector (Maybe Text)
asMaybeText
    asMaybeInt :: Vector (Maybe Int)
asMaybeInt = (Maybe Text -> Maybe Int)
-> Vector (Maybe Text) -> Vector (Maybe Int)
forall a b. (a -> b) -> Vector a -> Vector b
V.map (Maybe Text -> (Text -> Maybe Int) -> Maybe Int
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= HasCallStack => Text -> Maybe Int
Text -> Maybe Int
readInt) Vector (Maybe Text)
asMaybeText
    asMaybeDouble :: Vector (Maybe Double)
asMaybeDouble = (Maybe Text -> Maybe Double)
-> Vector (Maybe Text) -> Vector (Maybe Double)
forall a b. (a -> b) -> Vector a -> Vector b
V.map (Maybe Text -> (Text -> Maybe Double) -> Maybe Double
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= HasCallStack => Text -> Maybe Double
Text -> Maybe Double
readDouble) Vector (Maybe Text)
asMaybeText
    asMaybeDate :: Vector (Maybe Day)
asMaybeDate = (Maybe Text -> Maybe Day)
-> Vector (Maybe Text) -> Vector (Maybe Day)
forall a b. (a -> b) -> Vector a -> Vector b
V.map (Maybe Text -> (Text -> Maybe Day) -> Maybe Day
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= DateFormat -> Text -> Maybe Day
parseTimeOpt DateFormat
dateFormat) Vector (Maybe Text)
asMaybeText

data ParsingAssumption
    = BoolAssumption
    | IntAssumption
    | DoubleAssumption
    | DateAssumption
    | NoAssumption
    | TextAssumption

parseWithTypes :: Bool -> M.Map T.Text SchemaType -> DataFrame -> DataFrame
parseWithTypes :: Bool -> Map Text SchemaType -> DataFrame -> DataFrame
parseWithTypes Bool
safe Map Text SchemaType
ts DataFrame
df
    | Map Text SchemaType -> Bool
forall k a. Map k a -> Bool
M.null Map Text SchemaType
ts = DataFrame
df
    | Bool
otherwise =
        (Text -> SchemaType -> DataFrame -> DataFrame)
-> DataFrame -> Map Text SchemaType -> DataFrame
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
M.foldrWithKey
            (\Text
k SchemaType
v DataFrame
d -> Text -> Column -> DataFrame -> DataFrame
insertColumn Text
k (SchemaType -> Column -> Column
asType SchemaType
v (Text -> DataFrame -> Column
unsafeGetColumn Text
k DataFrame
d)) DataFrame
d)
            DataFrame
df
            Map Text SchemaType
ts
  where
    asType :: SchemaType -> Column -> Column
    asType :: SchemaType -> Column -> Column
asType (SType (Proxy a
_ :: P.Proxy a)) c :: Column
c@(BoxedColumn (Vector a
col :: V.Vector b)) = case forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @a of
        App TypeRep a
t1 TypeRep b
t2 -> case TypeRep a -> TypeRep Maybe -> Maybe (a :~~: Maybe)
forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
eqTypeRep TypeRep a
t1 (forall {k} (a :: k). Typeable a => TypeRep a
forall (a :: * -> *). Typeable a => TypeRep a
typeRep @Maybe) of
            Just a :~~: Maybe
HRefl -> case TypeRep a -> TypeRep a -> Maybe (a :~: a)
forall a b. TypeRep a -> TypeRep b -> Maybe (a :~: b)
forall {k} (f :: k -> *) (a :: k) (b :: k).
TestEquality f =>
f a -> f b -> Maybe (a :~: b)
testEquality (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @a) (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @b) of
                Just a :~: a
Refl -> Column
c
                Maybe (a :~: a)
Nothing -> case TypeRep Text -> TypeRep a -> Maybe (Text :~: a)
forall a b. TypeRep a -> TypeRep b -> Maybe (a :~: b)
forall {k} (f :: k -> *) (a :: k) (b :: k).
TestEquality f =>
f a -> f b -> Maybe (a :~: b)
testEquality (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @T.Text) (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @b) of
                    Just Text :~: a
Refl -> Vector (Maybe b) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector ((Text -> Maybe b) -> Vector Text -> Vector (Maybe b)
forall a b. (a -> b) -> Vector a -> Vector b
V.map (Maybe (Maybe b) -> Maybe b
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Maybe (Maybe b) -> Maybe b)
-> (Text -> Maybe (Maybe b)) -> Text -> Maybe b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Read a => DateFormat -> Maybe a
readAsMaybe @a) (DateFormat -> Maybe (Maybe b))
-> (Text -> DateFormat) -> Text -> Maybe (Maybe b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> DateFormat
T.unpack) Vector a
Vector Text
col)
                    Maybe (Text :~: a)
Nothing -> Vector (Maybe b) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector ((a -> Maybe b) -> Vector a -> Vector (Maybe b)
forall a b. (a -> b) -> Vector a -> Vector b
V.map (Maybe (Maybe b) -> Maybe b
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Maybe (Maybe b) -> Maybe b)
-> (a -> Maybe (Maybe b)) -> a -> Maybe b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Read a => DateFormat -> Maybe a
readAsMaybe @a) (DateFormat -> Maybe (Maybe b))
-> (a -> DateFormat) -> a -> Maybe (Maybe b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> DateFormat
forall a. Show a => a -> DateFormat
show) Vector a
col)
            Maybe (a :~~: Maybe)
Nothing -> case TypeRep a
t1 of
                App TypeRep a
t1' TypeRep b
t2' -> case TypeRep a -> TypeRep Either -> Maybe (a :~~: Either)
forall k1 k2 (a :: k1) (b :: k2).
TypeRep a -> TypeRep b -> Maybe (a :~~: b)
eqTypeRep TypeRep a
t1' (forall {k} (a :: k). Typeable a => TypeRep a
forall (a :: * -> * -> *). Typeable a => TypeRep a
typeRep @Either) of
                    Just a :~~: Either
HRefl -> case TypeRep a -> TypeRep a -> Maybe (a :~: a)
forall a b. TypeRep a -> TypeRep b -> Maybe (a :~: b)
forall {k} (f :: k -> *) (a :: k) (b :: k).
TestEquality f =>
f a -> f b -> Maybe (a :~: b)
testEquality (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @a) (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @b) of
                        Just a :~: a
Refl -> Column
c
                        Maybe (a :~: a)
Nothing -> case TypeRep Text -> TypeRep a -> Maybe (Text :~: a)
forall a b. TypeRep a -> TypeRep b -> Maybe (a :~: b)
forall {k} (f :: k -> *) (a :: k) (b :: k).
TestEquality f =>
f a -> f b -> Maybe (a :~: b)
testEquality (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @T.Text) (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @b) of
                            Just Text :~: a
Refl -> Vector a -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector ((Text -> a) -> Vector Text -> Vector a
forall a b. (a -> b) -> Vector a -> Vector b
V.map ((forall a. Read a => DateFormat -> a
readAsEither @a) (DateFormat -> a) -> (Text -> DateFormat) -> Text -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> DateFormat
T.unpack) Vector a
Vector Text
col)
                            Maybe (Text :~: a)
Nothing -> Vector a -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector ((a -> a) -> Vector a -> Vector a
forall a b. (a -> b) -> Vector a -> Vector b
V.map ((forall a. Read a => DateFormat -> a
readAsEither @a) (DateFormat -> a) -> (a -> DateFormat) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> DateFormat
forall a. Show a => a -> DateFormat
show) Vector a
col)
                    Maybe (a :~~: Either)
Nothing -> case TypeRep a -> TypeRep a -> Maybe (a :~: a)
forall a b. TypeRep a -> TypeRep b -> Maybe (a :~: b)
forall {k} (f :: k -> *) (a :: k) (b :: k).
TestEquality f =>
f a -> f b -> Maybe (a :~: b)
testEquality (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @a) (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @b) of
                        Just a :~: a
Refl -> Column
c
                        Maybe (a :~: a)
Nothing -> case TypeRep Text -> TypeRep a -> Maybe (Text :~: a)
forall a b. TypeRep a -> TypeRep b -> Maybe (a :~: b)
forall {k} (f :: k -> *) (a :: k) (b :: k).
TestEquality f =>
f a -> f b -> Maybe (a :~: b)
testEquality (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @T.Text) (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @b) of
                            Just Text :~: a
Refl ->
                                if Bool
safe
                                    then Vector (Maybe a) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector ((Text -> Maybe a) -> Vector Text -> Vector (Maybe a)
forall a b. (a -> b) -> Vector a -> Vector b
V.map ((forall a. Read a => DateFormat -> Maybe a
readMaybe @a) (DateFormat -> Maybe a) -> (Text -> DateFormat) -> Text -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> DateFormat
T.unpack) Vector a
Vector Text
col)
                                    else Vector a -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector ((Text -> a) -> Vector Text -> Vector a
forall a b. (a -> b) -> Vector a -> Vector b
V.map ((forall a. Read a => DateFormat -> a
read @a) (DateFormat -> a) -> (Text -> DateFormat) -> Text -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> DateFormat
T.unpack) Vector a
Vector Text
col)
                            Maybe (Text :~: a)
Nothing ->
                                if Bool
safe
                                    then Vector (Maybe a) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector ((a -> Maybe a) -> Vector a -> Vector (Maybe a)
forall a b. (a -> b) -> Vector a -> Vector b
V.map ((forall a. Read a => DateFormat -> Maybe a
readMaybe @a) (DateFormat -> Maybe a) -> (a -> DateFormat) -> a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> DateFormat
forall a. Show a => a -> DateFormat
show) Vector a
col)
                                    else Vector a -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector ((a -> a) -> Vector a -> Vector a
forall a b. (a -> b) -> Vector a -> Vector b
V.map ((forall a. Read a => DateFormat -> a
read @a) (DateFormat -> a) -> (a -> DateFormat) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> DateFormat
forall a. Show a => a -> DateFormat
show) Vector a
col)
                TypeRep a
_ -> Column
c
        TypeRep a
_ -> case TypeRep a -> TypeRep a -> Maybe (a :~: a)
forall a b. TypeRep a -> TypeRep b -> Maybe (a :~: b)
forall {k} (f :: k -> *) (a :: k) (b :: k).
TestEquality f =>
f a -> f b -> Maybe (a :~: b)
testEquality (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @a) (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @b) of
            Just a :~: a
Refl -> Column
c
            Maybe (a :~: a)
Nothing -> case TypeRep Text -> TypeRep a -> Maybe (Text :~: a)
forall a b. TypeRep a -> TypeRep b -> Maybe (a :~: b)
forall {k} (f :: k -> *) (a :: k) (b :: k).
TestEquality f =>
f a -> f b -> Maybe (a :~: b)
testEquality (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @T.Text) (forall a. Typeable a => TypeRep a
forall {k} (a :: k). Typeable a => TypeRep a
typeRep @b) of
                Just Text :~: a
Refl ->
                    if Bool
safe
                        then Vector (Maybe a) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector ((Text -> Maybe a) -> Vector Text -> Vector (Maybe a)
forall a b. (a -> b) -> Vector a -> Vector b
V.map ((forall a. Read a => DateFormat -> Maybe a
readMaybe @a) (DateFormat -> Maybe a) -> (Text -> DateFormat) -> Text -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> DateFormat
T.unpack) Vector a
Vector Text
col)
                        else Vector a -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector ((Text -> a) -> Vector Text -> Vector a
forall a b. (a -> b) -> Vector a -> Vector b
V.map ((forall a. Read a => DateFormat -> a
read @a) (DateFormat -> a) -> (Text -> DateFormat) -> Text -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> DateFormat
T.unpack) Vector a
Vector Text
col)
                Maybe (Text :~: a)
Nothing ->
                    if Bool
safe
                        then Vector (Maybe a) -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector ((a -> Maybe a) -> Vector a -> Vector (Maybe a)
forall a b. (a -> b) -> Vector a -> Vector b
V.map ((forall a. Read a => DateFormat -> Maybe a
readMaybe @a) (DateFormat -> Maybe a) -> (a -> DateFormat) -> a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> DateFormat
forall a. Show a => a -> DateFormat
show) Vector a
col)
                        else Vector a -> Column
forall a.
(Columnable a, ColumnifyRep (KindOf a) a) =>
Vector a -> Column
fromVector ((a -> a) -> Vector a -> Vector a
forall a b. (a -> b) -> Vector a -> Vector b
V.map ((forall a. Read a => DateFormat -> a
read @a) (DateFormat -> a) -> (a -> DateFormat) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> DateFormat
forall a. Show a => a -> DateFormat
show) Vector a
col)
    asType SchemaType
_ Column
c = Column
c

readAsMaybe :: (Read a) => String -> Maybe a
readAsMaybe :: forall a. Read a => DateFormat -> Maybe a
readAsMaybe DateFormat
s
    | DateFormat -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null DateFormat
s = Maybe a
forall a. Maybe a
Nothing
    | Bool
otherwise = DateFormat -> Maybe a
forall a. Read a => DateFormat -> Maybe a
readMaybe (DateFormat -> Maybe a) -> DateFormat -> Maybe a
forall a b. (a -> b) -> a -> b
$ DateFormat
"Just " DateFormat -> DateFormat -> DateFormat
forall a. Semigroup a => a -> a -> a
<> DateFormat
s

readAsEither :: (Read a) => String -> a
readAsEither :: forall a. Read a => DateFormat -> a
readAsEither DateFormat
v = case [Maybe a] -> Maybe a
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum [DateFormat -> Maybe a
forall a. Read a => DateFormat -> Maybe a
readMaybe (DateFormat -> Maybe a) -> DateFormat -> Maybe a
forall a b. (a -> b) -> a -> b
$ DateFormat
"Left " DateFormat -> DateFormat -> DateFormat
forall a. Semigroup a => a -> a -> a
<> DateFormat
s, DateFormat -> Maybe a
forall a. Read a => DateFormat -> Maybe a
readMaybe (DateFormat -> Maybe a) -> DateFormat -> Maybe a
forall a b. (a -> b) -> a -> b
$ DateFormat
"Right " DateFormat -> DateFormat -> DateFormat
forall a. Semigroup a => a -> a -> a
<> DateFormat
s] of
    Maybe a
Nothing -> DateFormat -> a
forall a. HasCallStack => DateFormat -> a
error (DateFormat -> a) -> DateFormat -> a
forall a b. (a -> b) -> a -> b
$ DateFormat
"Couldn't read value: " DateFormat -> DateFormat -> DateFormat
forall a. Semigroup a => a -> a -> a
<> DateFormat
s
    Just a
v -> a
v
  where
    s :: DateFormat
s = if DateFormat -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null DateFormat
v then DateFormat
"\"\"" else DateFormat
v