{-# LANGUAGE DuplicateRecordFields #-}

module Hasura.Backends.BigQuery.DDL.Source
  ( resolveSource,
    postDropSourceHook,
    resolveSourceConfig,
    restTypeToScalarType,
  )
where

import Data.Aeson qualified as J
import Data.ByteString.Lazy qualified as L
import Data.Environment qualified as Env
import Data.HashMap.Strict.Extended qualified as HashMap
import Data.Int qualified as Int
import Data.Text qualified as T
import Data.Text.Encoding qualified as T
import Data.Time.Clock.System
import Hasura.Backends.BigQuery.Connection
import Hasura.Backends.BigQuery.Meta
import Hasura.Backends.BigQuery.Source
import Hasura.Backends.BigQuery.Types
import Hasura.Base.Error
import Hasura.Function.Cache (FunctionOverloads (..))
import Hasura.Prelude
import Hasura.RQL.Types.Backend (BackendConfig)
import Hasura.RQL.Types.BackendType
import Hasura.RQL.Types.Column
import Hasura.RQL.Types.Common
import Hasura.RQL.Types.Source
import Hasura.Table.Cache

defaultGlobalSelectLimit :: Int.Int64
defaultGlobalSelectLimit :: Int64
defaultGlobalSelectLimit = Int64
1000

defaultRetryLimit :: Int
defaultRetryLimit :: Int
defaultRetryLimit = Int
5

defaultRetryBaseDelay :: Microseconds
defaultRetryBaseDelay :: Microseconds
defaultRetryBaseDelay = Microseconds
500000

resolveSourceConfig ::
  (MonadIO m) =>
  SourceName ->
  BigQueryConnSourceConfig ->
  BackendSourceKind 'BigQuery ->
  BackendConfig 'BigQuery ->
  Env.Environment ->
  manager ->
  m (Either QErr BigQuerySourceConfig)
resolveSourceConfig :: forall (m :: * -> *) manager.
MonadIO m =>
SourceName
-> BigQueryConnSourceConfig
-> BackendSourceKind 'BigQuery
-> BackendConfig 'BigQuery
-> Environment
-> manager
-> m (Either QErr BigQuerySourceConfig)
resolveSourceConfig SourceName
_name BigQueryConnSourceConfig {Maybe ConfigurationInput
ConfigurationInput
ConfigurationInputs
ConfigurationJSON ServiceAccount
_cscServiceAccount :: ConfigurationJSON ServiceAccount
_cscDatasets :: ConfigurationInputs
_cscProjectId :: ConfigurationInput
_cscGlobalSelectLimit :: Maybe ConfigurationInput
_cscRetryBaseDelay :: Maybe ConfigurationInput
_cscRetryLimit :: Maybe ConfigurationInput
_cscServiceAccount :: BigQueryConnSourceConfig -> ConfigurationJSON ServiceAccount
_cscDatasets :: BigQueryConnSourceConfig -> ConfigurationInputs
_cscProjectId :: BigQueryConnSourceConfig -> ConfigurationInput
_cscGlobalSelectLimit :: BigQueryConnSourceConfig -> Maybe ConfigurationInput
_cscRetryBaseDelay :: BigQueryConnSourceConfig -> Maybe ConfigurationInput
_cscRetryLimit :: BigQueryConnSourceConfig -> Maybe ConfigurationInput
..} BackendSourceKind 'BigQuery
_backendKind BackendConfig 'BigQuery
_backendConfig Environment
env manager
_manager = ExceptT QErr m BigQuerySourceConfig
-> m (Either QErr BigQuerySourceConfig)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT QErr m BigQuerySourceConfig
 -> m (Either QErr BigQuerySourceConfig))
-> ExceptT QErr m BigQuerySourceConfig
-> m (Either QErr BigQuerySourceConfig)
forall a b. (a -> b) -> a -> b
$ do
  Either String ServiceAccount
eSA <- Environment
-> ConfigurationJSON ServiceAccount
-> ExceptT QErr m (Either String ServiceAccount)
forall (m :: * -> *) a.
(QErrM m, FromJSON a) =>
Environment -> ConfigurationJSON a -> m (Either String a)
resolveConfigurationJson Environment
env ConfigurationJSON ServiceAccount
_cscServiceAccount
  case Either String ServiceAccount
eSA of
    Left String
e -> Code -> Text -> ExceptT QErr m BigQuerySourceConfig
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
Unexpected (Text -> ExceptT QErr m BigQuerySourceConfig)
-> Text -> ExceptT QErr m BigQuerySourceConfig
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack String
e
    Right ServiceAccount
serviceAccount -> do
      BigQueryProjectId
projectId <- Text -> BigQueryProjectId
BigQueryProjectId (Text -> BigQueryProjectId)
-> ExceptT QErr m Text -> ExceptT QErr m BigQueryProjectId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Environment -> ConfigurationInput -> ExceptT QErr m Text
forall (m :: * -> *).
QErrM m =>
Environment -> ConfigurationInput -> m Text
resolveConfigurationInput Environment
env ConfigurationInput
_cscProjectId
      Maybe RetryOptions
retryOptions <- do
        Int
numRetries <-
          Environment -> ConfigurationInput -> ExceptT QErr m Text
forall (m :: * -> *).
QErrM m =>
Environment -> ConfigurationInput -> m Text
resolveConfigurationInput Environment
env (ConfigurationInput -> ExceptT QErr m Text)
-> Maybe ConfigurationInput -> ExceptT QErr m (Maybe Text)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Maybe a -> m (Maybe b)
`mapM` Maybe ConfigurationInput
_cscRetryLimit ExceptT QErr m (Maybe Text)
-> (Maybe Text -> ExceptT QErr m Int) -> ExceptT QErr m Int
forall a b.
ExceptT QErr m a -> (a -> ExceptT QErr m b) -> ExceptT QErr m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
            Maybe Text
Nothing -> Int -> ExceptT QErr m Int
forall a. a -> ExceptT QErr m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
defaultRetryLimit
            Just Text
v -> Text -> Text -> ExceptT QErr m Int
forall (m :: * -> *) a.
(MonadError QErr m, Num a, Ord a, FromJSON a, Read a) =>
Text -> Text -> m a
readNonNegative Text
v Text
"retry limit"
        if Int
numRetries Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
          then Maybe RetryOptions -> ExceptT QErr m (Maybe RetryOptions)
forall a. a -> ExceptT QErr m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe RetryOptions
forall a. Maybe a
Nothing
          else do
            let _retryNumRetries :: Int
_retryNumRetries = Int
numRetries
            Microseconds
_retryBaseDelay <-
              Environment -> ConfigurationInput -> ExceptT QErr m Text
forall (m :: * -> *).
QErrM m =>
Environment -> ConfigurationInput -> m Text
resolveConfigurationInput Environment
env (ConfigurationInput -> ExceptT QErr m Text)
-> Maybe ConfigurationInput -> ExceptT QErr m (Maybe Text)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Maybe a -> m (Maybe b)
`mapM` Maybe ConfigurationInput
_cscRetryBaseDelay ExceptT QErr m (Maybe Text)
-> (Maybe Text -> ExceptT QErr m Microseconds)
-> ExceptT QErr m Microseconds
forall a b.
ExceptT QErr m a -> (a -> ExceptT QErr m b) -> ExceptT QErr m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                Maybe Text
Nothing -> Microseconds -> ExceptT QErr m Microseconds
forall a. a -> ExceptT QErr m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Microseconds
defaultRetryBaseDelay
                Just Text
v -> Integer -> Microseconds
forall a. Num a => Integer -> a
fromInteger (Integer -> Microseconds)
-> ExceptT QErr m Integer -> ExceptT QErr m Microseconds
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Text -> ExceptT QErr m Integer
forall (m :: * -> *) a.
(MonadError QErr m, Num a, Ord a, FromJSON a, Read a) =>
Text -> Text -> m a
readNonNegative Text
v Text
"retry base delay"
            Maybe RetryOptions -> ExceptT QErr m (Maybe RetryOptions)
forall a. a -> ExceptT QErr m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe RetryOptions -> ExceptT QErr m (Maybe RetryOptions))
-> Maybe RetryOptions -> ExceptT QErr m (Maybe RetryOptions)
forall a b. (a -> b) -> a -> b
$ RetryOptions -> Maybe RetryOptions
forall a. a -> Maybe a
Just RetryOptions {Int
Microseconds
_retryNumRetries :: Int
_retryBaseDelay :: Microseconds
_retryBaseDelay :: Microseconds
_retryNumRetries :: Int
..}
      BigQueryConnection
_scConnection <- ServiceAccount
-> BigQueryProjectId
-> Maybe RetryOptions
-> ExceptT QErr m BigQueryConnection
forall (m :: * -> *).
MonadIO m =>
ServiceAccount
-> BigQueryProjectId -> Maybe RetryOptions -> m BigQueryConnection
initConnection ServiceAccount
serviceAccount BigQueryProjectId
projectId Maybe RetryOptions
retryOptions
      [BigQueryDataset]
_scDatasets <- (Text -> BigQueryDataset) -> [Text] -> [BigQueryDataset]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> BigQueryDataset
BigQueryDataset ([Text] -> [BigQueryDataset])
-> ExceptT QErr m [Text] -> ExceptT QErr m [BigQueryDataset]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Environment -> ConfigurationInputs -> ExceptT QErr m [Text]
forall (m :: * -> *).
QErrM m =>
Environment -> ConfigurationInputs -> m [Text]
resolveConfigurationInputs Environment
env ConfigurationInputs
_cscDatasets
      Int64
_scGlobalSelectLimit <-
        Environment -> ConfigurationInput -> ExceptT QErr m Text
forall (m :: * -> *).
QErrM m =>
Environment -> ConfigurationInput -> m Text
resolveConfigurationInput Environment
env (ConfigurationInput -> ExceptT QErr m Text)
-> Maybe ConfigurationInput -> ExceptT QErr m (Maybe Text)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Maybe a -> m (Maybe b)
`mapM` Maybe ConfigurationInput
_cscGlobalSelectLimit ExceptT QErr m (Maybe Text)
-> (Maybe Text -> ExceptT QErr m Int64) -> ExceptT QErr m Int64
forall a b.
ExceptT QErr m a -> (a -> ExceptT QErr m b) -> ExceptT QErr m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
          Maybe Text
Nothing -> Int64 -> ExceptT QErr m Int64
forall a. a -> ExceptT QErr m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int64
defaultGlobalSelectLimit
          Just Text
v -> Text -> Text -> ExceptT QErr m Int64
forall (m :: * -> *) a.
(MonadError QErr m, Num a, Ord a, FromJSON a, Read a) =>
Text -> Text -> m a
readNonNegative Text
v Text
"global select limit"
      BigQuerySourceConfig -> ExceptT QErr m BigQuerySourceConfig
forall a. a -> ExceptT QErr m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure BigQuerySourceConfig {Int64
[BigQueryDataset]
BigQueryConnection
_scConnection :: BigQueryConnection
_scDatasets :: [BigQueryDataset]
_scGlobalSelectLimit :: Int64
_scConnection :: BigQueryConnection
_scDatasets :: [BigQueryDataset]
_scGlobalSelectLimit :: Int64
..}

readNonNegative :: (MonadError QErr m, Num a, Ord a, J.FromJSON a, Read a) => Text -> Text -> m a
readNonNegative :: forall (m :: * -> *) a.
(MonadError QErr m, Num a, Ord a, FromJSON a, Read a) =>
Text -> Text -> m a
readNonNegative Text
i Text
paramName =
  -- This works around the inconsistency between JSON and
  -- environment variables. The config handling module should be
  -- reworked to handle non-text values better.
  case String -> Maybe a
forall a. Read a => String -> Maybe a
readMaybe (Text -> String
T.unpack Text
i) Maybe a -> Maybe a -> Maybe a
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ByteString -> Maybe a
forall a. FromJSON a => ByteString -> Maybe a
J.decode (ByteString -> ByteString
L.fromStrict (Text -> ByteString
T.encodeUtf8 Text
i)) of
    Maybe a
Nothing -> Code -> Text -> m a
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
Unexpected (Text -> m a) -> Text -> m a
forall a b. (a -> b) -> a -> b
$ Text
"Need a non-negative integer for " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
paramName
    Just a
i' -> do
      Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (a
i' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Code -> Text -> m ()
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
Unexpected (Text -> m ()) -> Text -> m ()
forall a b. (a -> b) -> a -> b
$ Text
"Need the integer for the " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
paramName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" to be non-negative"
      a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
i'

resolveSource ::
  (MonadIO m) =>
  BigQuerySourceConfig ->
  m (Either QErr (DBObjectsIntrospection 'BigQuery))
resolveSource :: forall (m :: * -> *).
MonadIO m =>
BigQuerySourceConfig
-> m (Either QErr (DBObjectsIntrospection 'BigQuery))
resolveSource BigQuerySourceConfig
sourceConfig =
  ExceptT QErr m (DBObjectsIntrospection 'BigQuery)
-> m (Either QErr (DBObjectsIntrospection 'BigQuery))
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT QErr m (DBObjectsIntrospection 'BigQuery)
 -> m (Either QErr (DBObjectsIntrospection 'BigQuery)))
-> ExceptT QErr m (DBObjectsIntrospection 'BigQuery)
-> m (Either QErr (DBObjectsIntrospection 'BigQuery))
forall a b. (a -> b) -> a -> b
$ do
    Either RestProblem [RestTable]
tables <- BigQuerySourceConfig
-> ExceptT QErr m (Either RestProblem [RestTable])
forall (m :: * -> *).
MonadIO m =>
BigQuerySourceConfig -> m (Either RestProblem [RestTable])
getTables BigQuerySourceConfig
sourceConfig
    Either RestProblem [RestRoutine]
routines <- BigQuerySourceConfig
-> ExceptT QErr m (Either RestProblem [RestRoutine])
forall (m :: * -> *).
MonadIO m =>
BigQuerySourceConfig -> m (Either RestProblem [RestRoutine])
getRoutines BigQuerySourceConfig
sourceConfig
    let result :: Either RestProblem ([RestTable], [RestRoutine])
result = (,) ([RestTable] -> [RestRoutine] -> ([RestTable], [RestRoutine]))
-> Either RestProblem [RestTable]
-> Either
     RestProblem ([RestRoutine] -> ([RestTable], [RestRoutine]))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either RestProblem [RestTable]
tables Either RestProblem ([RestRoutine] -> ([RestTable], [RestRoutine]))
-> Either RestProblem [RestRoutine]
-> Either RestProblem ([RestTable], [RestRoutine])
forall a b.
Either RestProblem (a -> b)
-> Either RestProblem a -> Either RestProblem b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Either RestProblem [RestRoutine]
routines
    case Either RestProblem ([RestTable], [RestRoutine])
result of
      Left RestProblem
err ->
        Code -> Text -> ExceptT QErr m (DBObjectsIntrospection 'BigQuery)
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
Unexpected
          (Text -> ExceptT QErr m (DBObjectsIntrospection 'BigQuery))
-> Text -> ExceptT QErr m (DBObjectsIntrospection 'BigQuery)
forall a b. (a -> b) -> a -> b
$ Text
"unexpected exception while connecting to database: "
          Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> RestProblem -> Text
forall a. Show a => a -> Text
tshow RestProblem
err
      Right ([RestTable]
restTables, [RestRoutine]
restRoutines) -> do
        Int64
seconds <- IO Int64 -> ExceptT QErr m Int64
forall a. IO a -> ExceptT QErr m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Int64 -> ExceptT QErr m Int64)
-> IO Int64 -> ExceptT QErr m Int64
forall a b. (a -> b) -> a -> b
$ (SystemTime -> Int64) -> IO SystemTime -> IO Int64
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SystemTime -> Int64
systemSeconds IO SystemTime
getSystemTime
        let functions :: HashMap FunctionName (FunctionOverloads 'BigQuery)
functions = NonEmpty (RawFunctionInfo 'BigQuery) -> FunctionOverloads 'BigQuery
NonEmpty RestRoutine -> FunctionOverloads 'BigQuery
forall (b :: BackendType).
NonEmpty (RawFunctionInfo b) -> FunctionOverloads b
FunctionOverloads (NonEmpty RestRoutine -> FunctionOverloads 'BigQuery)
-> HashMap FunctionName (NonEmpty RestRoutine)
-> HashMap FunctionName (FunctionOverloads 'BigQuery)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (RestRoutine -> FunctionName)
-> [RestRoutine] -> HashMap FunctionName (NonEmpty RestRoutine)
forall k (t :: * -> *) v.
(Hashable k, Foldable t) =>
(v -> k) -> t v -> HashMap k (NonEmpty v)
HashMap.groupOnNE (RestRoutineReference -> FunctionName
routineReferenceToFunctionName (RestRoutineReference -> FunctionName)
-> (RestRoutine -> RestRoutineReference)
-> RestRoutine
-> FunctionName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RestRoutine -> RestRoutineReference
routineReference) [RestRoutine]
restRoutines
        DBObjectsIntrospection 'BigQuery
-> ExceptT QErr m (DBObjectsIntrospection 'BigQuery)
forall a. a -> ExceptT QErr m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
          ( DBObjectsIntrospection
              { _rsTables :: DBTablesMetadata 'BigQuery
_rsTables =
                  [(TableName, DBTableMetadata 'BigQuery)]
-> HashMap TableName (DBTableMetadata 'BigQuery)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
                    [ ( RestTableReference -> TableName
restTableReferenceToTableName RestTableReference
tableReference,
                        DBTableMetadata
                          { _ptmiOid :: OID
_ptmiOid = Int -> OID
OID (Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
seconds Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
index :: Int), -- TODO: The seconds are used for uniqueness. BigQuery doesn't support a "stable" ID for a table.
                            _ptmiColumns :: [RawColumnInfo 'BigQuery]
_ptmiColumns =
                              [ RawColumnInfo
                                  { rciName :: Column 'BigQuery
rciName = Text -> ColumnName
ColumnName Text
name,
                                    rciPosition :: Int
rciPosition = Int
position,
                                    rciType :: RawColumnType 'BigQuery
rciType = ScalarType 'BigQuery -> RawColumnType 'BigQuery
forall (b :: BackendType). ScalarType b -> RawColumnType b
RawColumnTypeScalar (ScalarType 'BigQuery -> RawColumnType 'BigQuery)
-> ScalarType 'BigQuery -> RawColumnType 'BigQuery
forall a b. (a -> b) -> a -> b
$ RestType -> ScalarType
restTypeToScalarType RestType
type',
                                    rciIsNullable :: Bool
rciIsNullable =
                                      case Mode
mode of
                                        Mode
Nullable -> Bool
True
                                        Mode
_ -> Bool
False,
                                    rciDescription :: Maybe Description
rciDescription = Maybe Description
forall a. Maybe a
Nothing,
                                    rciMutability :: ColumnMutability
rciMutability = ColumnMutability {_cmIsInsertable :: Bool
_cmIsInsertable = Bool
True, _cmIsUpdatable :: Bool
_cmIsUpdatable = Bool
True}
                                  }
                                | (Int
position, RestFieldSchema {Text
name :: Text
$sel:name:RestFieldSchema :: RestFieldSchema -> Text
name, RestType
type' :: RestType
$sel:type':RestFieldSchema :: RestFieldSchema -> RestType
type', Mode
mode :: Mode
$sel:mode:RestFieldSchema :: RestFieldSchema -> Mode
mode}) <-
                                    [Int] -> [RestFieldSchema] -> [(Int, RestFieldSchema)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
1 ..] [RestFieldSchema]
fields -- TODO: Same trouble as Oid above.
                              ],
                            _ptmiPrimaryKey :: Maybe (PrimaryKey 'BigQuery (Column 'BigQuery))
_ptmiPrimaryKey = Maybe (PrimaryKey 'BigQuery (Column 'BigQuery))
Maybe (PrimaryKey 'BigQuery ColumnName)
forall a. Maybe a
Nothing,
                            _ptmiUniqueConstraints :: HashSet (UniqueConstraint 'BigQuery)
_ptmiUniqueConstraints = HashSet (UniqueConstraint 'BigQuery)
forall a. Monoid a => a
mempty,
                            _ptmiForeignKeys :: HashSet (ForeignKeyMetadata 'BigQuery)
_ptmiForeignKeys = HashSet (ForeignKeyMetadata 'BigQuery)
forall a. Monoid a => a
mempty,
                            _ptmiViewInfo :: Maybe ViewInfo
_ptmiViewInfo = ViewInfo -> Maybe ViewInfo
forall a. a -> Maybe a
Just (ViewInfo -> Maybe ViewInfo) -> ViewInfo -> Maybe ViewInfo
forall a b. (a -> b) -> a -> b
$ Bool -> Bool -> Bool -> ViewInfo
ViewInfo Bool
False Bool
False Bool
False,
                            _ptmiDescription :: Maybe PGDescription
_ptmiDescription = Maybe PGDescription
forall a. Maybe a
Nothing,
                            _ptmiExtraTableMetadata :: ExtraTableMetadata 'BigQuery
_ptmiExtraTableMetadata = ()
                          }
                      )
                      | (Int
index, RestTable {RestTableReference
tableReference :: RestTableReference
$sel:tableReference:RestTable :: RestTable -> RestTableReference
tableReference, RestTableSchema
schema :: RestTableSchema
$sel:schema:RestTable :: RestTable -> RestTableSchema
schema}) <-
                          [Int] -> [RestTable] -> [(Int, RestTable)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] [RestTable]
restTables,
                        let RestTableSchema [RestFieldSchema]
fields = RestTableSchema
schema
                    ],
                _rsFunctions :: DBFunctionsMetadata 'BigQuery
_rsFunctions = DBFunctionsMetadata 'BigQuery
HashMap FunctionName (FunctionOverloads 'BigQuery)
functions,
                _rsScalars :: ScalarMap 'BigQuery
_rsScalars = ScalarMap 'BigQuery
forall a. Monoid a => a
mempty,
                _rsLogicalModels :: LogicalModels 'BigQuery
_rsLogicalModels = LogicalModels 'BigQuery
forall a. Monoid a => a
mempty
              }
          )

restTypeToScalarType :: RestType -> ScalarType
restTypeToScalarType :: RestType -> ScalarType
restTypeToScalarType =
  \case
    RestType
STRING -> ScalarType
StringScalarType
    RestType
BYTES -> ScalarType
BytesScalarType
    RestType
INTEGER -> ScalarType
IntegerScalarType
    RestType
FLOAT -> ScalarType
FloatScalarType
    RestType
BOOL -> ScalarType
BoolScalarType
    RestType
TIMESTAMP -> ScalarType
TimestampScalarType
    RestType
DATE -> ScalarType
DateScalarType
    RestType
TIME -> ScalarType
TimeScalarType
    RestType
DATETIME -> ScalarType
DatetimeScalarType
    RestType
GEOGRAPHY -> ScalarType
GeographyScalarType
    RestType
STRUCT -> ScalarType
StructScalarType
    RestType
BIGDECIMAL -> ScalarType
BigDecimalScalarType
    RestType
DECIMAL -> ScalarType
DecimalScalarType
    RestType
JSON -> ScalarType
JsonScalarType

-- Hierarchy: Project / Dataset / Table
-- see <https://cloud.google.com/bigquery/docs/datasets-intro>
restTableReferenceToTableName :: RestTableReference -> TableName
restTableReferenceToTableName :: RestTableReference -> TableName
restTableReferenceToTableName RestTableReference {Text
datasetId :: Text
projectId :: Text
tableId :: Text
$sel:datasetId:RestTableReference :: RestTableReference -> Text
$sel:projectId:RestTableReference :: RestTableReference -> Text
$sel:tableId:RestTableReference :: RestTableReference -> Text
..} =
  TableName {$sel:tableName:TableName :: Text
tableName = Text
tableId, $sel:tableNameSchema:TableName :: Text
tableNameSchema = Text
datasetId}

-- We ignore project id and push that requirement up to the top to
-- the data source level.

postDropSourceHook ::
  (MonadIO m) =>
  BigQuerySourceConfig ->
  TableEventTriggers 'BigQuery ->
  m ()
postDropSourceHook :: forall (m :: * -> *).
MonadIO m =>
BigQuerySourceConfig -> TableEventTriggers 'BigQuery -> m ()
postDropSourceHook BigQuerySourceConfig
_ TableEventTriggers 'BigQuery
_ =
  -- On BigQuery we don't keep connections open.
  () -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()