module Hasura.Backends.BigQuery.DDL
  ( fetchAndValidateEnumValues,
    buildFunctionInfo,
    updateColumnInEventTrigger,
    parseBoolExpOperations,
    parseCollectableType,
    module M,
  )
where

import Data.Aeson
import Hasura.Backends.BigQuery.DDL.BoolExp
import Hasura.Backends.BigQuery.DDL.ComputedField as M
import Hasura.Backends.BigQuery.DDL.Source as M
import Hasura.Backends.BigQuery.Types qualified as BigQuery
import Hasura.Base.Error
import Hasura.GraphQL.Schema.NamingCase
import Hasura.Prelude
import Hasura.RQL.IR.BoolExp
import Hasura.RQL.Types.Backend
import Hasura.RQL.Types.Column
import Hasura.RQL.Types.Common
import Hasura.RQL.Types.EventTrigger
import Hasura.RQL.Types.Function
import Hasura.RQL.Types.SchemaCache
import Hasura.RQL.Types.Table
import Hasura.SQL.Backend
import Hasura.SQL.Types
import Hasura.Server.Utils
import Hasura.Session

fetchAndValidateEnumValues ::
  (Monad 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)
fetchAndValidateEnumValues SourceConfig 'BigQuery
_ TableName 'BigQuery
_ Maybe (PrimaryKey 'BigQuery (RawColumnInfo 'BigQuery))
_ [RawColumnInfo 'BigQuery]
_ =
  ExceptT QErr m EnumValues -> m (Either QErr EnumValues)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT QErr m EnumValues -> m (Either QErr EnumValues))
-> ExceptT QErr m EnumValues -> m (Either QErr EnumValues)
forall a b. (a -> b) -> a -> b
$
    Code -> Text -> ExceptT QErr m EnumValues
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotSupported Text
"Enum tables are not supported for BigQuery sources"

buildFunctionInfo ::
  (MonadError QErr m) =>
  SourceName ->
  FunctionName 'BigQuery ->
  SystemDefined ->
  FunctionConfig ->
  FunctionPermissionsMap ->
  RawFunctionInfo 'BigQuery ->
  Maybe Text ->
  NamingCase ->
  m (FunctionInfo 'BigQuery, SchemaDependency)
buildFunctionInfo :: SourceName
-> FunctionName 'BigQuery
-> SystemDefined
-> FunctionConfig
-> FunctionPermissionsMap
-> RawFunctionInfo 'BigQuery
-> Maybe Text
-> NamingCase
-> m (FunctionInfo 'BigQuery, SchemaDependency)
buildFunctionInfo SourceName
_ FunctionName 'BigQuery
_ SystemDefined
_ FunctionConfig
_ FunctionPermissionsMap
_ RawFunctionInfo 'BigQuery
_ Maybe Text
_ NamingCase
_ =
  Code -> Text -> m (FunctionInfo 'BigQuery, SchemaDependency)
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotSupported Text
"SQL Functions are not supported for BigQuery source"

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
updateColumnInEventTrigger TableName 'BigQuery
_ Column 'BigQuery
_ Column 'BigQuery
_ TableName 'BigQuery
_ = EventTriggerConf 'BigQuery -> EventTriggerConf 'BigQuery
forall a. a -> a
id

parseCollectableType ::
  (MonadError QErr m) =>
  CollectableType (ColumnType 'BigQuery) ->
  Value ->
  m (PartialSQLExp 'BigQuery)
parseCollectableType :: CollectableType (ColumnType 'BigQuery)
-> Value -> m (PartialSQLExp 'BigQuery)
parseCollectableType CollectableType (ColumnType 'BigQuery)
collectableType = \case
  String Text
t
    | Text -> Bool
isSessionVariable Text
t -> PartialSQLExp 'BigQuery -> m (PartialSQLExp 'BigQuery)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PartialSQLExp 'BigQuery -> m (PartialSQLExp 'BigQuery))
-> PartialSQLExp 'BigQuery -> m (PartialSQLExp 'BigQuery)
forall a b. (a -> b) -> a -> b
$ CollectableType (ColumnType 'BigQuery)
-> SessionVariable -> PartialSQLExp 'BigQuery
mkTypedSessionVar CollectableType (ColumnType 'BigQuery)
collectableType (SessionVariable -> PartialSQLExp 'BigQuery)
-> SessionVariable -> PartialSQLExp 'BigQuery
forall a b. (a -> b) -> a -> b
$ Text -> SessionVariable
mkSessionVariable Text
t
    | Text -> Bool
isReqUserId Text
t -> PartialSQLExp 'BigQuery -> m (PartialSQLExp 'BigQuery)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PartialSQLExp 'BigQuery -> m (PartialSQLExp 'BigQuery))
-> PartialSQLExp 'BigQuery -> m (PartialSQLExp 'BigQuery)
forall a b. (a -> b) -> a -> b
$ CollectableType (ColumnType 'BigQuery)
-> SessionVariable -> PartialSQLExp 'BigQuery
mkTypedSessionVar CollectableType (ColumnType 'BigQuery)
collectableType SessionVariable
forall a. IsString a => a
userIdHeader
  Value
val -> case CollectableType (ColumnType 'BigQuery)
collectableType of
    CollectableTypeScalar ColumnType 'BigQuery
scalarType ->
      Expression -> PartialSQLExp 'BigQuery
forall (backend :: BackendType).
SQLExpression backend -> PartialSQLExp backend
PSESQLExp (Expression -> PartialSQLExp 'BigQuery)
-> (Value -> Expression) -> Value -> PartialSQLExp 'BigQuery
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Expression
BigQuery.ValueExpression (Value -> PartialSQLExp 'BigQuery)
-> m Value -> m (PartialSQLExp 'BigQuery)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ColumnType 'BigQuery -> Value -> m (ScalarValue 'BigQuery)
forall (m :: * -> *) (b :: BackendType).
(MonadError QErr m, Backend b) =>
ColumnType b -> Value -> m (ScalarValue b)
parseScalarValueColumnType ColumnType 'BigQuery
scalarType Value
val
    CollectableTypeArray ColumnType 'BigQuery
_ ->
      Code -> Text -> m (PartialSQLExp 'BigQuery)
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotSupported Text
"Array types are not supported in BigQuery backend"

mkTypedSessionVar ::
  CollectableType (ColumnType 'BigQuery) ->
  SessionVariable ->
  PartialSQLExp 'BigQuery
mkTypedSessionVar :: CollectableType (ColumnType 'BigQuery)
-> SessionVariable -> PartialSQLExp 'BigQuery
mkTypedSessionVar CollectableType (ColumnType 'BigQuery)
columnType =
  SessionVarType 'BigQuery
-> SessionVariable -> PartialSQLExp 'BigQuery
forall (backend :: BackendType).
SessionVarType backend -> SessionVariable -> PartialSQLExp backend
PSESessVar (ColumnType 'BigQuery -> ScalarType 'BigQuery
ColumnType 'BigQuery -> ScalarType
msColumnTypeToScalarType (ColumnType 'BigQuery -> ScalarType)
-> CollectableType (ColumnType 'BigQuery)
-> CollectableType ScalarType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CollectableType (ColumnType 'BigQuery)
columnType)

msColumnTypeToScalarType :: ColumnType 'BigQuery -> ScalarType 'BigQuery
msColumnTypeToScalarType :: ColumnType 'BigQuery -> ScalarType 'BigQuery
msColumnTypeToScalarType = \case
  ColumnScalar ScalarType 'BigQuery
scalarType -> ScalarType 'BigQuery
scalarType
  ColumnEnumReference EnumReference 'BigQuery
_ -> ScalarType 'BigQuery
ScalarType
BigQuery.StringScalarType