{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module DataFrame.Typed.IO.Parquet (
readParquet,
readParquetWithError,
readParquetWithOpts,
readParquetFiles,
) where
import Control.Applicative ((<|>))
import Control.Exception (SomeException, try)
import qualified Data.Text as T
import DataFrame.IO.Parquet (ParquetReadOptions (..), defaultParquetReadOptions)
import qualified DataFrame.IO.Parquet as Parquet
import DataFrame.Typed.Freeze (freezeOrThrow, freezeWithError)
import DataFrame.Typed.Schema (KnownSchema, schemaColumnNames)
import DataFrame.Typed.Types (TypedDataFrame)
schemaOptions ::
forall cols. (KnownSchema cols) => ParquetReadOptions -> ParquetReadOptions
schemaOptions :: forall (cols :: [(Symbol, *)]).
KnownSchema cols =>
ParquetReadOptions -> ParquetReadOptions
schemaOptions ParquetReadOptions
opts =
ParquetReadOptions
opts
{ selectedColumns = selectedColumns opts <|> Just (schemaColumnNames @cols)
}
readParquet ::
forall cols. (KnownSchema cols) => FilePath -> IO (TypedDataFrame cols)
readParquet :: forall (cols :: [(Symbol, *)]).
KnownSchema cols =>
FilePath -> IO (TypedDataFrame cols)
readParquet = forall (cols :: [(Symbol, *)]).
KnownSchema cols =>
ParquetReadOptions -> FilePath -> IO (TypedDataFrame cols)
readParquetWithOpts @cols ParquetReadOptions
defaultParquetReadOptions
readParquetWithError ::
forall cols.
(KnownSchema cols) =>
FilePath -> IO (Either T.Text (TypedDataFrame cols))
readParquetWithError :: forall (cols :: [(Symbol, *)]).
KnownSchema cols =>
FilePath -> IO (Either Text (TypedDataFrame cols))
readParquetWithError FilePath
path = do
Either SomeException DataFrame
r <-
IO DataFrame -> IO (Either SomeException DataFrame)
forall e a. Exception e => IO a -> IO (Either e a)
try
(ParquetReadOptions -> FilePath -> IO DataFrame
Parquet.readParquetWithOpts (forall (cols :: [(Symbol, *)]).
KnownSchema cols =>
ParquetReadOptions -> ParquetReadOptions
schemaOptions @cols ParquetReadOptions
defaultParquetReadOptions) FilePath
path)
Either Text (TypedDataFrame cols)
-> IO (Either Text (TypedDataFrame cols))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Text (TypedDataFrame cols)
-> IO (Either Text (TypedDataFrame cols)))
-> Either Text (TypedDataFrame cols)
-> IO (Either Text (TypedDataFrame cols))
forall a b. (a -> b) -> a -> b
$ case Either SomeException DataFrame
r of
Left (SomeException
e :: SomeException) -> Text -> Either Text (TypedDataFrame cols)
forall a b. a -> Either a b
Left (FilePath -> Text
T.pack (SomeException -> FilePath
forall a. Show a => a -> FilePath
show SomeException
e))
Right DataFrame
df -> DataFrame -> Either Text (TypedDataFrame cols)
forall (cols :: [(Symbol, *)]).
KnownSchema cols =>
DataFrame -> Either Text (TypedDataFrame cols)
freezeWithError DataFrame
df
readParquetWithOpts ::
forall cols.
(KnownSchema cols) =>
ParquetReadOptions -> FilePath -> IO (TypedDataFrame cols)
readParquetWithOpts :: forall (cols :: [(Symbol, *)]).
KnownSchema cols =>
ParquetReadOptions -> FilePath -> IO (TypedDataFrame cols)
readParquetWithOpts ParquetReadOptions
opts FilePath
path =
ParquetReadOptions -> FilePath -> IO DataFrame
Parquet.readParquetWithOpts (forall (cols :: [(Symbol, *)]).
KnownSchema cols =>
ParquetReadOptions -> ParquetReadOptions
schemaOptions @cols ParquetReadOptions
opts) FilePath
path
IO DataFrame
-> (DataFrame -> IO (TypedDataFrame cols))
-> IO (TypedDataFrame cols)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (cols :: [(Symbol, *)]).
KnownSchema cols =>
DataFrame -> IO (TypedDataFrame cols)
freezeOrThrow @cols
readParquetFiles ::
forall cols. (KnownSchema cols) => FilePath -> IO (TypedDataFrame cols)
readParquetFiles :: forall (cols :: [(Symbol, *)]).
KnownSchema cols =>
FilePath -> IO (TypedDataFrame cols)
readParquetFiles FilePath
path =
ParquetReadOptions -> FilePath -> IO DataFrame
Parquet.readParquetFilesWithOpts
(forall (cols :: [(Symbol, *)]).
KnownSchema cols =>
ParquetReadOptions -> ParquetReadOptions
schemaOptions @cols ParquetReadOptions
defaultParquetReadOptions)
FilePath
path
IO DataFrame
-> (DataFrame -> IO (TypedDataFrame cols))
-> IO (TypedDataFrame cols)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (cols :: [(Symbol, *)]).
KnownSchema cols =>
DataFrame -> IO (TypedDataFrame cols)
freezeOrThrow @cols