module Hasura.Backends.BigQuery.DDL
  ( fetchAndValidateEnumValues,
    buildFunctionInfo,
    updateColumnInEventTrigger,
    parseBoolExpOperations,
    parseCollectableType,
    scalarTypeFromColumnType,
    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.Function.Cache
import Hasura.Prelude
import Hasura.RQL.IR.BoolExp
import Hasura.RQL.Types.Backend
import Hasura.RQL.Types.BackendType
import Hasura.RQL.Types.Column
import Hasura.RQL.Types.Common
import Hasura.RQL.Types.EventTrigger
import Hasura.RQL.Types.NamingCase
import Hasura.RQL.Types.SchemaCache
import Hasura.SQL.Types
import Hasura.Server.Utils
import Hasura.Session
import Hasura.Table.Cache

fetchAndValidateEnumValues ::
  (Monad m) =>
  SourceConfig 'BigQuery ->
  TableName 'BigQuery ->
  Maybe (PrimaryKey 'BigQuery (RawColumnInfo 'BigQuery)) ->
  [RawColumnInfo 'BigQuery] ->
  m (Either QErr EnumValues)
fetchAndValidateEnumValues :: forall (m :: * -> *).
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]
_ =
  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 'BigQuery ->
  FunctionPermissionsMap ->
  RawFunctionInfo 'BigQuery ->
  Maybe Text ->
  NamingCase ->
  m (FunctionInfo 'BigQuery, SchemaDependency)
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
_ =
  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 :: forall (m :: * -> *).
MonadError QErr m =>
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 a. a -> m a
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 a. a -> m a
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 ->
      SQLExpression 'BigQuery -> PartialSQLExp 'BigQuery
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
. TypedValue -> Expression
BigQuery.ValueExpression (TypedValue -> Expression)
-> (Value -> TypedValue) -> Value -> Expression
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScalarType -> Value -> TypedValue
BigQuery.TypedValue (ColumnType 'BigQuery -> ScalarType
scalarTypeFromColumnType ColumnType 'BigQuery
scalarType) (Value -> PartialSQLExp 'BigQuery)
-> m Value -> m (PartialSQLExp 'BigQuery)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ScalarTypeParsingContext 'BigQuery
-> ColumnType 'BigQuery -> Value -> m (ScalarValue 'BigQuery)
forall (m :: * -> *) (b :: BackendType).
(MonadError QErr m, Backend b) =>
ScalarTypeParsingContext b
-> ColumnType b -> Value -> m (ScalarValue b)
parseScalarValueColumnTypeWithContext () 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"

scalarTypeFromColumnType :: ColumnType 'BigQuery -> BigQuery.ScalarType
scalarTypeFromColumnType :: ColumnType 'BigQuery -> ScalarType
scalarTypeFromColumnType (ColumnEnumReference EnumReference 'BigQuery
_) = ScalarType
BigQuery.StringScalarType
scalarTypeFromColumnType (ColumnScalar ScalarType 'BigQuery
scalar) = ScalarType 'BigQuery
ScalarType
scalar

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