2022-03-16 03:39:21 +03:00
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
|
2021-06-11 06:26:50 +03:00
|
|
|
-- |
|
|
|
|
-- Working example:
|
2021-09-24 01:56:37 +03:00
|
|
|
--
|
2021-06-11 06:26:50 +03:00
|
|
|
-- \$ curl -XPOST http://localhost:8080/v2/query -d @- <<EOF
|
|
|
|
-- {
|
|
|
|
-- "type":"bigquery_run_sql",
|
|
|
|
-- "args": {
|
|
|
|
-- "sql":"select 3 * 4 as foo, \"Hello, World!\" as bar",
|
|
|
|
-- "source":"chinook"
|
|
|
|
-- }
|
2021-09-24 01:56:37 +03:00
|
|
|
-- }
|
2021-06-11 06:26:50 +03:00
|
|
|
-- EOF
|
|
|
|
-- {"result_type":"TuplesOk","result":[["foo","bar"],["12","Hello, World!"]]}
|
2021-04-12 13:18:29 +03:00
|
|
|
module Hasura.Backends.BigQuery.DDL.RunSQL
|
|
|
|
( runSQL,
|
|
|
|
runDatabaseInspection,
|
|
|
|
BigQueryRunSQL,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2021-06-28 16:29:48 +03:00
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Data.Aeson.TH (deriveJSON)
|
|
|
|
import Data.Aeson.Text (encodeToLazyText)
|
|
|
|
import Data.HashMap.Strict.InsOrd qualified as OMap
|
|
|
|
import Data.Text qualified as T
|
|
|
|
import Data.Text.Lazy qualified as LT
|
|
|
|
import Data.Vector qualified as V
|
|
|
|
import Hasura.Backends.BigQuery.Execute qualified as Execute
|
2022-12-16 16:17:41 +03:00
|
|
|
import Hasura.Backends.BigQuery.Source (BigQueryDataset (..), BigQuerySourceConfig (..))
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Base.Error
|
2021-04-12 13:18:29 +03:00
|
|
|
import Hasura.EncJSON
|
2021-04-22 00:44:37 +03:00
|
|
|
import Hasura.Prelude
|
2021-06-28 16:29:48 +03:00
|
|
|
import Hasura.RQL.DDL.Schema (RunSQLRes (..))
|
2023-04-24 21:35:48 +03:00
|
|
|
import Hasura.RQL.Types.BackendType
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
import Hasura.RQL.Types.Metadata
|
|
|
|
import Hasura.RQL.Types.SchemaCache
|
|
|
|
import Hasura.RQL.Types.SchemaCache.Build
|
2021-04-22 00:44:37 +03:00
|
|
|
|
2021-04-12 13:18:29 +03:00
|
|
|
data BigQueryRunSQL = BigQueryRunSQL
|
|
|
|
{ _mrsSql :: Text,
|
2022-07-29 17:05:03 +03:00
|
|
|
_mrsSource :: SourceName
|
2021-09-24 01:56:37 +03:00
|
|
|
}
|
2021-04-12 13:18:29 +03:00
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
|
|
$(deriveJSON hasuraJSON ''BigQueryRunSQL)
|
|
|
|
|
|
|
|
runSQL ::
|
|
|
|
(MonadIO m, CacheRWM m, MonadError QErr m, MetadataM m) =>
|
|
|
|
BigQueryRunSQL ->
|
|
|
|
m EncJSON
|
|
|
|
runSQL = runSQL_ recordSetAsHeaderAndRows
|
|
|
|
|
|
|
|
-- | The SQL query in the request is ignored
|
|
|
|
runDatabaseInspection ::
|
|
|
|
(MonadIO m, CacheRWM m, MonadError QErr m, MetadataM m) =>
|
|
|
|
BigQueryRunSQL ->
|
|
|
|
m EncJSON
|
|
|
|
runDatabaseInspection (BigQueryRunSQL _query source) = do
|
2021-04-22 00:44:37 +03:00
|
|
|
BigQuerySourceConfig {_scDatasets = dataSets} <- askSourceConfig @'BigQuery source
|
2021-04-12 13:18:29 +03:00
|
|
|
let queries =
|
|
|
|
[ "SELECT *, ARRAY(SELECT as STRUCT * from "
|
2022-12-16 16:17:41 +03:00
|
|
|
<> getBigQueryDataset dataSet
|
2021-04-12 13:18:29 +03:00
|
|
|
<> ".INFORMATION_SCHEMA.COLUMNS WHERE table_name = t.table_name) as columns from "
|
2022-12-16 16:17:41 +03:00
|
|
|
<> getBigQueryDataset dataSet
|
2021-04-12 13:18:29 +03:00
|
|
|
<> ".INFORMATION_SCHEMA.TABLES as t"
|
|
|
|
| dataSet <- dataSets
|
|
|
|
]
|
|
|
|
query' = T.intercalate " UNION ALL " queries
|
|
|
|
runSQL_ recordSetAsSchema (BigQueryRunSQL query' source)
|
|
|
|
|
|
|
|
runSQL_ ::
|
|
|
|
(MonadIO m, CacheRWM m, MonadError QErr m, MetadataM m) =>
|
2021-04-22 00:44:37 +03:00
|
|
|
(Execute.RecordSet -> J.Value) ->
|
2021-04-12 13:18:29 +03:00
|
|
|
BigQueryRunSQL ->
|
|
|
|
m EncJSON
|
|
|
|
runSQL_ f (BigQueryRunSQL query source) = do
|
2021-04-22 00:44:37 +03:00
|
|
|
sourceConfig <- askSourceConfig @'BigQuery source
|
2021-04-12 13:18:29 +03:00
|
|
|
result <-
|
2021-04-22 00:44:37 +03:00
|
|
|
Execute.streamBigQuery
|
2022-02-09 18:26:14 +03:00
|
|
|
(_scConnection sourceConfig)
|
|
|
|
Execute.BigQuery {query = LT.fromStrict query, parameters = mempty}
|
2021-04-12 13:18:29 +03:00
|
|
|
case result of
|
2022-03-30 16:53:14 +03:00
|
|
|
Left executeProblem -> do
|
2022-06-27 17:32:31 +03:00
|
|
|
let errorMessage = Execute.executeProblemMessage Execute.HideDetails executeProblem
|
2022-03-30 16:53:14 +03:00
|
|
|
throwError (err400 BigQueryError errorMessage) {qeInternal = Just $ ExtraInternal $ J.toJSON executeProblem}
|
2021-04-12 13:18:29 +03:00
|
|
|
Right recordSet ->
|
|
|
|
pure
|
|
|
|
( encJFromJValue
|
2023-03-14 14:32:20 +03:00
|
|
|
(RunSQLRes "TuplesOk" (f (snd recordSet)))
|
2021-09-24 01:56:37 +03:00
|
|
|
)
|
2021-04-12 13:18:29 +03:00
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
recordSetAsHeaderAndRows :: Execute.RecordSet -> J.Value
|
|
|
|
recordSetAsHeaderAndRows Execute.RecordSet {rows} = J.toJSON (thead : tbody)
|
2021-04-12 13:18:29 +03:00
|
|
|
where
|
|
|
|
thead =
|
|
|
|
case rows V.!? 0 of
|
|
|
|
Nothing -> []
|
|
|
|
Just row ->
|
2021-06-28 16:29:48 +03:00
|
|
|
map (J.toJSON . (coerce :: Execute.FieldNameText -> Text)) (OMap.keys row)
|
2021-04-12 13:18:29 +03:00
|
|
|
tbody :: [[J.Value]]
|
2022-02-09 18:26:14 +03:00
|
|
|
tbody = map (map J.toJSON . OMap.elems) (toList rows)
|
2021-04-12 13:18:29 +03:00
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
recordSetAsSchema :: Execute.RecordSet -> J.Value
|
|
|
|
recordSetAsSchema rs@(Execute.RecordSet {rows}) =
|
2021-04-12 13:18:29 +03:00
|
|
|
recordSetAsHeaderAndRows $
|
2021-04-22 00:44:37 +03:00
|
|
|
rs
|
|
|
|
{ Execute.rows =
|
|
|
|
OMap.adjust
|
|
|
|
(Execute.TextOutputValue . LT.toStrict . encodeToLazyText . J.toJSON)
|
2021-06-28 16:29:48 +03:00
|
|
|
(Execute.FieldNameText "columns")
|
|
|
|
<$> rows
|
|
|
|
}
|