{-# OPTIONS_GHC -fno-warn-orphans #-}

module Hasura.Backends.BigQuery.Instances.Metadata () where

import Hasura.Backends.BigQuery.DDL qualified as BigQuery
import Hasura.Backends.BigQuery.Schema.Introspection qualified as BigQuery (listAllTables)
import Hasura.Base.Error (Code (UnexpectedPayload), throw400)
import Hasura.NativeQuery.InterpolatedQuery (trimQueryEnd)
import Hasura.NativeQuery.Metadata (NativeQueryMetadata (..))
import Hasura.NativeQuery.Validation (validateArgumentDeclaration)
import Hasura.Prelude
import Hasura.RQL.DDL.Relationship (defaultBuildArrayRelationshipInfo, defaultBuildObjectRelationshipInfo)
import Hasura.RQL.Types.BackendType
import Hasura.RQL.Types.EventTrigger (RecreateEventTriggers (RETDoNothing))
import Hasura.RQL.Types.Metadata.Backend
import Hasura.Server.Migrate.Version (SourceCatalogMigrationState (SCMSNotSupported))

instance BackendMetadata 'BigQuery where
  prepareCatalog :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m) =>
SourceConfig 'BigQuery
-> ExceptT
     QErr m (RecreateEventTriggers, SourceCatalogMigrationState)
prepareCatalog SourceConfig 'BigQuery
_ = (RecreateEventTriggers, SourceCatalogMigrationState)
-> ExceptT
     QErr m (RecreateEventTriggers, SourceCatalogMigrationState)
forall a. a -> ExceptT QErr m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (RecreateEventTriggers
RETDoNothing, SourceCatalogMigrationState
SCMSNotSupported)
  buildComputedFieldInfo :: forall (m :: * -> *).
MonadError QErr m =>
HashSet (TableName 'BigQuery)
-> TableName 'BigQuery
-> HashSet (Column 'BigQuery)
-> ComputedFieldName
-> ComputedFieldDefinition 'BigQuery
-> RawFunctionInfo 'BigQuery
-> Comment
-> m (ComputedFieldInfo 'BigQuery)
buildComputedFieldInfo = HashSet (TableName 'BigQuery)
-> TableName 'BigQuery
-> HashSet (Column 'BigQuery)
-> ComputedFieldName
-> ComputedFieldDefinition 'BigQuery
-> RawFunctionInfo 'BigQuery
-> Comment
-> m (ComputedFieldInfo 'BigQuery)
HashSet TableName
-> TableName
-> HashSet ColumnName
-> ComputedFieldName
-> ComputedFieldDefinition
-> RestRoutine
-> Comment
-> m (ComputedFieldInfo 'BigQuery)
forall (m :: * -> *).
MonadError QErr m =>
HashSet TableName
-> TableName
-> HashSet ColumnName
-> ComputedFieldName
-> ComputedFieldDefinition
-> RestRoutine
-> Comment
-> m (ComputedFieldInfo 'BigQuery)
BigQuery.buildComputedFieldInfo
  fetchAndValidateEnumValues :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m) =>
SourceConfig 'BigQuery
-> TableName 'BigQuery
-> Maybe (PrimaryKey 'BigQuery (RawColumnInfo 'BigQuery))
-> [RawColumnInfo 'BigQuery]
-> m (Either QErr EnumValues)
fetchAndValidateEnumValues = SourceConfig 'BigQuery
-> TableName 'BigQuery
-> Maybe (PrimaryKey 'BigQuery (RawColumnInfo 'BigQuery))
-> [RawColumnInfo 'BigQuery]
-> m (Either QErr EnumValues)
forall (m :: * -> *).
Monad m =>
SourceConfig 'BigQuery
-> TableName 'BigQuery
-> Maybe (PrimaryKey 'BigQuery (RawColumnInfo 'BigQuery))
-> [RawColumnInfo 'BigQuery]
-> m (Either QErr EnumValues)
BigQuery.fetchAndValidateEnumValues
  resolveSourceConfig :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m, MonadResolveSource m) =>
SourceName
-> SourceConnConfiguration 'BigQuery
-> BackendSourceKind 'BigQuery
-> BackendInfo 'BigQuery
-> Environment
-> Manager
-> m (Either QErr (SourceConfig 'BigQuery))
resolveSourceConfig = SourceName
-> BigQueryConnSourceConfig
-> BackendSourceKind 'BigQuery
-> BackendConfig 'BigQuery
-> Environment
-> Manager
-> m (Either QErr BigQuerySourceConfig)
SourceName
-> SourceConnConfiguration 'BigQuery
-> BackendSourceKind 'BigQuery
-> BackendInfo 'BigQuery
-> Environment
-> Manager
-> m (Either QErr (SourceConfig 'BigQuery))
forall (m :: * -> *) manager.
MonadIO m =>
SourceName
-> BigQueryConnSourceConfig
-> BackendSourceKind 'BigQuery
-> BackendConfig 'BigQuery
-> Environment
-> manager
-> m (Either QErr BigQuerySourceConfig)
BigQuery.resolveSourceConfig
  resolveDatabaseMetadata :: forall (m :: * -> *).
(MonadIO m, MonadBaseControl IO m, MonadResolveSource m) =>
Logger Hasura
-> SourceMetadata 'BigQuery
-> SourceConfig 'BigQuery
-> m (Either QErr (DBObjectsIntrospection 'BigQuery))
resolveDatabaseMetadata Logger Hasura
_ SourceMetadata 'BigQuery
_ = BigQuerySourceConfig
-> m (Either QErr (DBObjectsIntrospection 'BigQuery))
SourceConfig 'BigQuery
-> m (Either QErr (DBObjectsIntrospection 'BigQuery))
forall (m :: * -> *).
MonadIO m =>
BigQuerySourceConfig
-> m (Either QErr (DBObjectsIntrospection 'BigQuery))
BigQuery.resolveSource
  parseBoolExpOperations :: forall (m :: * -> *) v.
MonadError QErr m =>
ValueParser 'BigQuery m v
-> FieldInfoMap (FieldInfo 'BigQuery)
-> FieldInfoMap (FieldInfo 'BigQuery)
-> ColumnReference 'BigQuery
-> Value
-> m [OpExpG 'BigQuery v]
parseBoolExpOperations = ValueParser 'BigQuery m v
-> FieldInfoMap (FieldInfo 'BigQuery)
-> FieldInfoMap (FieldInfo 'BigQuery)
-> ColumnReference 'BigQuery
-> Value
-> m [OpExpG 'BigQuery v]
forall (m :: * -> *) v.
MonadError QErr m =>
ValueParser 'BigQuery m v
-> FieldInfoMap (FieldInfo 'BigQuery)
-> FieldInfoMap (FieldInfo 'BigQuery)
-> ColumnReference 'BigQuery
-> Value
-> m [OpExpG 'BigQuery v]
BigQuery.parseBoolExpOperations
  buildArrayRelationshipInfo :: forall (m :: * -> *).
MonadError QErr m =>
SourceConfig 'BigQuery
-> SourceName
-> HashMap (TableName 'BigQuery) (HashSet (ForeignKey 'BigQuery))
-> TableName 'BigQuery
-> ArrRelDef 'BigQuery
-> m (RelInfo 'BigQuery, Seq SchemaDependency)
buildArrayRelationshipInfo SourceConfig 'BigQuery
_ = SourceName
-> HashMap (TableName 'BigQuery) (HashSet (ForeignKey 'BigQuery))
-> TableName 'BigQuery
-> ArrRelDef 'BigQuery
-> m (RelInfo 'BigQuery, Seq SchemaDependency)
forall (b :: BackendType) (m :: * -> *).
(QErrM m, Backend b) =>
SourceName
-> HashMap (TableName b) (HashSet (ForeignKey b))
-> TableName b
-> ArrRelDef b
-> m (RelInfo b, Seq SchemaDependency)
defaultBuildArrayRelationshipInfo
  buildObjectRelationshipInfo :: forall (m :: * -> *).
MonadError QErr m =>
SourceConfig 'BigQuery
-> SourceName
-> HashMap (TableName 'BigQuery) (HashSet (ForeignKey 'BigQuery))
-> TableName 'BigQuery
-> ObjRelDef 'BigQuery
-> m (RelInfo 'BigQuery, Seq SchemaDependency)
buildObjectRelationshipInfo SourceConfig 'BigQuery
_ = SourceName
-> HashMap (TableName 'BigQuery) (HashSet (ForeignKey 'BigQuery))
-> TableName 'BigQuery
-> ObjRelDef 'BigQuery
-> m (RelInfo 'BigQuery, Seq SchemaDependency)
forall (b :: BackendType) (m :: * -> *).
(QErrM m, Backend b) =>
SourceName
-> HashMap (TableName b) (HashSet (ForeignKey b))
-> TableName b
-> ObjRelDef b
-> m (RelInfo b, Seq SchemaDependency)
defaultBuildObjectRelationshipInfo
  buildFunctionInfo :: forall (m :: * -> *).
MonadError QErr m =>
SourceName
-> FunctionName 'BigQuery
-> SystemDefined
-> FunctionConfig 'BigQuery
-> FunctionPermissionsMap
-> RawFunctionInfo 'BigQuery
-> Maybe Text
-> NamingCase
-> m (FunctionInfo 'BigQuery, SchemaDependency)
buildFunctionInfo = SourceName
-> FunctionName 'BigQuery
-> SystemDefined
-> FunctionConfig 'BigQuery
-> FunctionPermissionsMap
-> RawFunctionInfo 'BigQuery
-> Maybe Text
-> NamingCase
-> m (FunctionInfo 'BigQuery, SchemaDependency)
forall (m :: * -> *).
MonadError QErr m =>
SourceName
-> FunctionName 'BigQuery
-> SystemDefined
-> FunctionConfig 'BigQuery
-> FunctionPermissionsMap
-> RawFunctionInfo 'BigQuery
-> Maybe Text
-> NamingCase
-> m (FunctionInfo 'BigQuery, SchemaDependency)
BigQuery.buildFunctionInfo
  updateColumnInEventTrigger :: TableName 'BigQuery
-> Column 'BigQuery
-> Column 'BigQuery
-> TableName 'BigQuery
-> EventTriggerConf 'BigQuery
-> EventTriggerConf 'BigQuery
updateColumnInEventTrigger = TableName 'BigQuery
-> Column 'BigQuery
-> Column 'BigQuery
-> TableName 'BigQuery
-> EventTriggerConf 'BigQuery
-> EventTriggerConf 'BigQuery
BigQuery.updateColumnInEventTrigger
  parseCollectableType :: forall (m :: * -> *) r.
(MonadError QErr m, MonadReader r m,
 Has (ScalarTypeParsingContext 'BigQuery) r) =>
CollectableType (ColumnType 'BigQuery)
-> Value -> m (PartialSQLExp 'BigQuery)
parseCollectableType = CollectableType (ColumnType 'BigQuery)
-> Value -> m (PartialSQLExp 'BigQuery)
forall (m :: * -> *).
MonadError QErr m =>
CollectableType (ColumnType 'BigQuery)
-> Value -> m (PartialSQLExp 'BigQuery)
BigQuery.parseCollectableType
  postDropSourceHook :: forall (m :: * -> *).
(MonadError QErr m, MonadIO m, MonadBaseControl IO m) =>
SourceConfig 'BigQuery -> TableEventTriggers 'BigQuery -> m ()
postDropSourceHook = BigQuerySourceConfig -> TableEventTriggers 'BigQuery -> m ()
SourceConfig 'BigQuery -> TableEventTriggers 'BigQuery -> m ()
forall (m :: * -> *).
MonadIO m =>
BigQuerySourceConfig -> TableEventTriggers 'BigQuery -> m ()
BigQuery.postDropSourceHook
  buildComputedFieldBooleanExp :: forall (m :: * -> *) v.
(MonadError QErr m, TableCoreInfoRM 'BigQuery m) =>
BoolExpResolver 'BigQuery m v
-> BoolExpRHSParser 'BigQuery m v
-> FieldInfoMap (FieldInfo 'BigQuery)
-> FieldInfoMap (FieldInfo 'BigQuery)
-> ComputedFieldInfo 'BigQuery
-> Value
-> m (AnnComputedFieldBoolExp 'BigQuery v)
buildComputedFieldBooleanExp BoolExpResolver 'BigQuery m v
_ BoolExpRHSParser 'BigQuery m v
_ FieldInfoMap (FieldInfo 'BigQuery)
_ FieldInfoMap (FieldInfo 'BigQuery)
_ ComputedFieldInfo 'BigQuery
_ Value
_ =
    Code -> Text -> m (AnnComputedFieldBoolExp 'BigQuery v)
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
UnexpectedPayload Text
"Computed fields are not supported in boolean expressions"
  supportsBeingRemoteRelationshipTarget :: SourceConfig 'BigQuery -> Bool
supportsBeingRemoteRelationshipTarget SourceConfig 'BigQuery
_ = Bool
True
  listAllTables :: forall (m :: * -> *) r.
(CacheRM m, MonadBaseControl IO m, MetadataM m, MonadError QErr m,
 MonadIO m, MonadReader r m, Has (Logger Hasura) r,
 ProvidesNetwork m) =>
SourceName -> m [TableName 'BigQuery]
listAllTables = SourceName -> m [TableName 'BigQuery]
SourceName -> m [TableName]
forall (m :: * -> *).
(CacheRM m, MetadataM m, MonadError QErr m, MonadIO m) =>
SourceName -> m [TableName]
BigQuery.listAllTables
  listAllTrackables :: forall (m :: * -> *) r.
(CacheRM m, MonadBaseControl IO m, MetadataM m, MonadError QErr m,
 MonadIO m, MonadReader r m, Has (Logger Hasura) r,
 ProvidesNetwork m) =>
SourceName -> m (TrackableInfo 'BigQuery)
listAllTrackables SourceName
_ = Code -> Text -> m (TrackableInfo 'BigQuery)
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
UnexpectedPayload Text
"listAllTrackables not supported by BigQuery!"
  getTableInfo :: forall (m :: * -> *).
(CacheRM m, MetadataM m, MonadError QErr m, MonadBaseControl IO m,
 MonadIO m) =>
SourceName
-> TableName 'BigQuery -> m (Maybe (SourceTableInfo 'BigQuery))
getTableInfo SourceName
_ TableName 'BigQuery
_ = Code -> Text -> m (Maybe (SourceTableInfo 'BigQuery))
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
UnexpectedPayload Text
"get_table_info not yet supported in BigQuery!"
  validateNativeQuery :: forall (m :: * -> *).
(MonadIO m, MonadError QErr m) =>
Environment
-> SourceConnConfiguration 'BigQuery
-> LogicalModelInfo 'BigQuery
-> NativeQueryMetadata 'BigQuery
-> m (InterpolatedQuery ArgumentName)
validateNativeQuery Environment
_ SourceConnConfiguration 'BigQuery
_ LogicalModelInfo 'BigQuery
_ NativeQueryMetadata 'BigQuery
nq = do
    NativeQueryMetadata 'BigQuery -> m ()
forall (m :: * -> *) (b :: BackendType).
(MonadIO m, MonadError QErr m) =>
NativeQueryMetadata b -> m ()
validateArgumentDeclaration NativeQueryMetadata 'BigQuery
nq
    InterpolatedQuery ArgumentName
-> m (InterpolatedQuery ArgumentName)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InterpolatedQuery ArgumentName -> InterpolatedQuery ArgumentName
forall var. InterpolatedQuery var -> InterpolatedQuery var
trimQueryEnd (NativeQueryMetadata 'BigQuery -> InterpolatedQuery ArgumentName
forall (b :: BackendType).
NativeQueryMetadata b -> InterpolatedQuery ArgumentName
_nqmCode NativeQueryMetadata 'BigQuery
nq)) -- for now, all queries are valid