2022-03-16 03:39:21 +03:00
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
-- |
|
|
|
|
-- Working example:
|
|
|
|
--
|
|
|
|
-- \$ 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"
|
|
|
|
-- }
|
|
|
|
-- }
|
|
|
|
-- EOF
|
|
|
|
-- {"result_type":"TuplesOk","result":[["foo","bar"],["12","Hello, World!"]]}
|
2021-04-12 13:18:29 +03:00
|
|
|
module Hasura.Backends.BigQuery.DDL.RunSQL
|
2021-09-24 01:56:37 +03:00
|
|
|
( runSQL,
|
|
|
|
runDatabaseInspection,
|
|
|
|
BigQueryRunSQL,
|
2021-04-12 13:18:29 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2021-09-24 01:56:37 +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
|
|
|
|
import Hasura.Backends.BigQuery.Source (BigQuerySourceConfig (..))
|
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.EncJSON
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.DDL.Schema (RunSQLRes (..))
|
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-09-24 01:56:37 +03:00
|
|
|
import Hasura.SQL.Backend
|
2021-04-22 00:44:37 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
data BigQueryRunSQL = BigQueryRunSQL
|
|
|
|
{ _mrsSql :: Text,
|
|
|
|
_mrsSource :: !SourceName
|
|
|
|
}
|
|
|
|
deriving (Show, Eq)
|
2021-04-12 13:18:29 +03:00
|
|
|
|
|
|
|
$(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-09-24 01:56:37 +03:00
|
|
|
BigQuerySourceConfig {_scDatasets = dataSets} <- askSourceConfig @'BigQuery source
|
|
|
|
let queries =
|
|
|
|
[ "SELECT *, ARRAY(SELECT as STRUCT * from "
|
|
|
|
<> dataSet
|
|
|
|
<> ".INFORMATION_SCHEMA.COLUMNS WHERE table_name = t.table_name) as columns from "
|
|
|
|
<> dataSet
|
|
|
|
<> ".INFORMATION_SCHEMA.TABLES as t"
|
|
|
|
| dataSet <- dataSets
|
|
|
|
]
|
2021-04-12 13:18:29 +03:00
|
|
|
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
|
|
|
|
let errorMessage = Execute.executeProblemMessage executeProblem
|
|
|
|
throwError (err400 BigQueryError errorMessage) {qeInternal = Just $ ExtraInternal $ J.toJSON executeProblem}
|
2021-04-12 13:18:29 +03:00
|
|
|
Right recordSet ->
|
|
|
|
pure
|
2021-09-24 01:56:37 +03:00
|
|
|
( encJFromJValue
|
|
|
|
(RunSQLRes "TuplesOk" (f recordSet))
|
|
|
|
)
|
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-09-24 01:56:37 +03:00
|
|
|
rs
|
|
|
|
{ Execute.rows =
|
|
|
|
OMap.adjust
|
|
|
|
(Execute.TextOutputValue . LT.toStrict . encodeToLazyText . J.toJSON)
|
|
|
|
(Execute.FieldNameText "columns")
|
|
|
|
<$> rows
|
|
|
|
}
|