Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: return single object from non-SETOF SQL functions #13

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 202 additions & 3 deletions server/lib/api-tests/src/Test/Schema/ComputedFields/TableSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Harness.Quoter.Graphql (graphql)
import Harness.Quoter.Yaml (interpolateYaml, yaml)
import Harness.Test.BackendType qualified as BackendType
import Harness.Test.Fixture qualified as Fixture
import Harness.Test.FixtureName (backendTypesForFixture)
import Harness.Test.Permissions (Permission (SelectPermission), SelectPermissionDetails (..), selectPermission)
import Harness.Test.Permissions qualified as Permission
import Harness.Test.Schema (SchemaName (..), Table (..), table)
Expand Down Expand Up @@ -102,6 +103,11 @@ articleTable =
Schema.VStr "Article 3 Title",
Schema.VStr "Article 3 by Author 2, has search keyword",
Schema.VInt 2
],
[ Schema.VInt 4,
Schema.VStr "Article 4 Title",
Schema.VStr "Article 4 by unknown author",
Schema.VInt 3
]
]
}
Expand All @@ -112,6 +118,7 @@ postgresSetupFunctions :: TestEnvironment -> [Fixture.SetupAction]
postgresSetupFunctions testEnv =
let schemaName = Schema.getSchemaName testEnv
articleTableSQL = unSchemaName schemaName <> ".article"
authorTableSQL = unSchemaName schemaName <> ".author"
in [ Fixture.SetupAction
{ Fixture.setupAction =
Postgres.run_ testEnv $
Expand Down Expand Up @@ -140,13 +147,42 @@ postgresSetupFunctions testEnv =
$$ LANGUAGE sql STABLE;
|],
Fixture.teardownAction = \_ -> pure ()
},
Fixture.SetupAction
{ Fixture.setupAction =
Postgres.run_ testEnv $
[i|
CREATE FUNCTION #{ fetch_author schemaName }(article_row article, filter_author_id int)
RETURNS author AS $$
SELECT *
FROM #{ authorTableSQL }
WHERE id = article_row.author_id AND id = filter_author_id
LIMIT 1
$$ LANGUAGE sql STABLE;
|],
Fixture.teardownAction = \_ -> pure ()
},
Fixture.SetupAction
{ Fixture.setupAction =
Postgres.run_ testEnv $
[i|
CREATE FUNCTION #{ fetch_author_no_user_args schemaName }(article_row article)
RETURNS author AS $$
SELECT *
FROM #{ authorTableSQL }
WHERE id = article_row.author_id
LIMIT 1
$$ LANGUAGE sql STABLE;
|],
Fixture.teardownAction = \_ -> pure ()
}
]

bigquerySetupFunctions :: TestEnvironment -> [Fixture.SetupAction]
bigquerySetupFunctions testEnv =
let schemaName = Schema.getSchemaName testEnv
articleTableSQL = unSchemaName schemaName <> ".article"
authorTableSQL = unSchemaName schemaName <> ".author"
in [ Fixture.SetupAction
{ Fixture.setupAction =
BigQuery.run_ $
Expand Down Expand Up @@ -174,6 +210,39 @@ bigquerySetupFunctions testEnv =
)
|],
Fixture.teardownAction = \_ -> pure ()
},
Fixture.SetupAction
{ Fixture.setupAction =
BigQuery.run_ $
[i|
CREATE TABLE FUNCTION
#{ fetch_author schemaName }(a_id INT64, filter_author_id INT64)
AS
(
SELECT au.*
FROM #{ authorTableSQL } as au
JOIN #{ articleTableSQL } as ar
ON ar.author_id = au.id
WHERE ar.id = a_id AND au.id = filter_author_id
)
|],
Fixture.teardownAction = \_ -> pure ()
},
Fixture.SetupAction
{ Fixture.setupAction =
BigQuery.run_ $
[i|
CREATE TABLE FUNCTION
#{ fetch_author_no_user_args schemaName }(a_id INT64)
AS
(
SELECT au.* FROM #{ authorTableSQL } AS au
JOIN #{ articleTableSQL } as ar
ON ar.author_id = au.id
WHERE ar.id = a_id
)
|],
Fixture.teardownAction = \_ -> pure ()
}
]

Expand All @@ -185,6 +254,14 @@ fetch_articles_no_user_args :: SchemaName -> T.Text
fetch_articles_no_user_args schemaName =
unSchemaName schemaName <> ".fetch_articles_no_user_args"

fetch_author :: SchemaName -> T.Text
fetch_author schemaName =
unSchemaName schemaName <> ".fetch_author"

fetch_author_no_user_args :: SchemaName -> T.Text
fetch_author_no_user_args schemaName =
unSchemaName schemaName <> ".fetch_author_no_user_args"

setupMetadata :: TestEnvironment -> [Fixture.SetupAction]
setupMetadata testEnvironment =
let backendTypeMetadata = fromMaybe (error "Unknown backend") $ getBackendTypeConfig testEnvironment
Expand All @@ -203,9 +280,9 @@ setupMetadata testEnvironment =
"search_articles"
[yaml| a_id: id |]
[yaml|
name: article
dataset: *schemaName
|]
name: article
dataset: *schemaName
|]
testEnvironment,
Fixture.teardownAction = \_ -> pure ()
},
Expand Down Expand Up @@ -262,6 +339,36 @@ setupMetadata testEnvironment =
selectPermissionColumns = (["id", "name"] :: [Text])
},
Fixture.teardownAction = \_ -> pure ()
},
Fixture.SetupAction
{ Fixture.setupAction =
Schema.trackComputedField
source
articleTable
"fetch_author"
"author"
[yaml| a_id: id |]
[yaml|
name: author
dataset: *schemaName
|]
testEnvironment,
Fixture.teardownAction = \_ -> pure ()
},
Fixture.SetupAction
{ Fixture.setupAction =
Schema.trackComputedField
source
articleTable
"fetch_author_no_user_args"
"author_no_args"
[yaml| a_id: id |]
[yaml|
name: author
dataset: *schemaName
|]
testEnvironment,
Fixture.teardownAction = \_ -> pure ()
}
]

Expand Down Expand Up @@ -473,3 +580,95 @@ tests opts = do
id: 2
title: Article 2 Title
|]

it "Query single nullable value for non-SETOF function" $ \testEnv -> do
let schemaName = Schema.getSchemaName testEnv
TestEnvironment { fixtureName } = testEnv

shouldReturnYaml
opts
( GraphqlEngine.postGraphql
testEnv
[graphql|
query {
#{schemaName}_article(order_by: {id: desc} limit: 2) {
id
title
author(args: {filter_author_id: 1}) {
id
name
}
}
}
|]
)
if Fixture.Postgres `elem` backendTypesForFixture fixtureName then
[interpolateYaml|
data:
#{schemaName}_article:
- id: 4
title: Article 4 Title
author: null
- id: 3
title: Article 3 Title
author: null
|]
else
[interpolateYaml|
data:
#{schemaName}_article:
- id: 4
title: Article 4 Title
author: []
- id: 3
title: Article 3 Title
author: []
|]

it "Query single nullable value for non-SETOF function without arguments" $ \testEnv -> do
let schemaName = Schema.getSchemaName testEnv
TestEnvironment { fixtureName } = testEnv

shouldReturnYaml
opts
( GraphqlEngine.postGraphql
testEnv
[graphql|
query {
#{schemaName}_article(order_by: {id: desc} limit: 2) {
id
title
author_no_args {
id
name
}
}
}
|]
)
if Fixture.Postgres `elem` backendTypesForFixture fixtureName then
[interpolateYaml|
data:
#{schemaName}_article:
- id: 4
title: Article 4 Title
author_no_args: null
- id: 3
title: Article 3 Title
author_no_args:
id: 2
name: Author 2
|]
else
[interpolateYaml|
data:
#{schemaName}_article:
- id: 4
title: Article 4 Title
author_no_args: []
- id: 3
title: Article 3 Title
author_no_args:
- id: 2
name: Author 2
|]
12 changes: 7 additions & 5 deletions server/src-lib/Hasura/Backends/Postgres/DDL/BoolExp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -313,16 +313,18 @@ buildComputedFieldBooleanExp boolExpResolver rhsParser rootFieldInfoMap colInfoM
[] -> do
let hasuraSession = _berpSessionValue rhsParser
computedFieldFunctionArgs = flip FunctionArgsExp mempty $ PG.fromComputedFieldImplicitArguments hasuraSession _cffComputedFieldImplicitArgs
cfbeFromTable tableName = do
tableBoolExp <- decodeValue colVal
tableFieldInfoMap <- askFieldInfoMapSource tableName
annTableBoolExp <- (getBoolExpResolver boolExpResolver) rhsParser tableFieldInfoMap tableFieldInfoMap $ unBoolExp tableBoolExp
pure $ CFBETable tableName annTableBoolExp
AnnComputedFieldBoolExp _cfiXComputedFieldInfo _cfiName _cffName computedFieldFunctionArgs
<$> case _cfiReturnType of
CFRScalar scalarType ->
CFBEScalar
<$> parseBoolExpOperations (_berpValueParser rhsParser) rootFieldInfoMap colInfoMap (ColumnReferenceComputedField _cfiName scalarType) colVal
CFRSetofTable table -> do
tableBoolExp <- decodeValue colVal
tableFieldInfoMap <- askFieldInfoMapSource table
annTableBoolExp <- (getBoolExpResolver boolExpResolver) rhsParser tableFieldInfoMap tableFieldInfoMap $ unBoolExp tableBoolExp
pure $ CFBETable table annTableBoolExp
CFRTable t -> cfbeFromTable t
CFRSetofTable t -> cfbeFromTable t
_ ->
throw400
UnexpectedPayload
Expand Down
4 changes: 3 additions & 1 deletion server/src-lib/Hasura/Backends/Postgres/DDL/ComputedField.hs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ buildComputedFieldInfo trackedTables table _tableColumns computedField definitio
MV.dispute $
pure $
CFVEReturnTableNotFound returnTable
pure $ PG.CFRSetofTable returnTable
pure $ if rfiReturnsSet rawFunctionInfo
then PG.CFRSetofTable returnTable
else PG.CFRTable returnTable
else do
let scalarType = _qptName functionReturnType
unless (isBaseType functionReturnType) $
Expand Down
1 change: 1 addition & 0 deletions server/src-lib/Hasura/Backends/Postgres/Instances/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ instance
computedFieldReturnType = \case
Postgres.CFRScalar scalarType -> ReturnsScalar scalarType
Postgres.CFRSetofTable table -> ReturnsTable table
Postgres.CFRTable table -> ReturnsTable table
fromComputedFieldImplicitArguments = Postgres.fromComputedFieldImplicitArguments

tableGraphQLName = Postgres.qualifiedObjectToName
Expand Down
4 changes: 2 additions & 2 deletions server/src-lib/Hasura/Backends/Postgres/SQL/DML.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ module Hasura.Backends.Postgres.SQL.DML
Extractor (..),
FromExp (..),
FromItem (..),
FunctionAlias (FunctionAlias),
FunctionAlias (FunctionAlias, _faIdentifier),
FunctionDefinitionListItem (..),
FunctionArgs (FunctionArgs),
FunctionExp (FunctionExp),
FunctionExp (FunctionExp, feAlias),
GroupByExp (GroupByExp),
HavingExp (HavingExp),
JoinCond (..),
Expand Down
20 changes: 19 additions & 1 deletion server/src-lib/Hasura/Backends/Postgres/Schema/Select.hs
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,28 @@ computedFieldPG ComputedFieldInfo {..} parentTable tableInfo = runMaybeT do
)
dummyParser <- lift $ columnParser @('Postgres pgKind) (ColumnScalar scalarReturnType) (G.Nullability True)
pure $ P.selection fieldName fieldDescription fieldArgsParser dummyParser
Postgres.CFRTable tableName -> do
otherTableInfo <- lift $ askTableInfo tableName
remotePerms <- hoistMaybe $ tableSelectPermissions roleName otherTableInfo
selectionSetParser <- MaybeT $ tableSelectionSet otherTableInfo
let fieldArgsParser = functionArgsParser
pure $
P.subselection fieldName fieldDescription fieldArgsParser selectionSetParser
<&> \(functionArgs', fields) ->
IR.AFComputedField _cfiXComputedFieldInfo _cfiName $
IR.CFSTable JASSingleObject $
IR.AnnSelectG
{ IR._asnFields = fields,
IR._asnFrom = IR.FromFunction (_cffName _cfiFunction) functionArgs' Nothing,
IR._asnPerm = tablePermissionsInfo remotePerms,
IR._asnArgs = noSelectArgs,
IR._asnStrfyNum = stringifyNumbers,
IR._asnNamingConvention = Just tCase
}
Postgres.CFRSetofTable tableName -> do
otherTableInfo <- lift $ askTableInfo tableName
remotePerms <- hoistMaybe $ tableSelectPermissions roleName otherTableInfo
selectionSetParser <- MaybeT (fmap (P.multiple . P.nonNullableParser) <$> tableSelectionSet otherTableInfo)
selectionSetParser <- MaybeT (fmap (P.nonNullableParser . P.multiple . P.nonNullableParser) <$> tableSelectionSet otherTableInfo)
selectArgsParser <- lift $ tableArguments otherTableInfo
let fieldArgsParser = liftA2 (,) functionArgsParser selectArgsParser
pure $
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,15 @@ processAnnFields sourcePrefix fieldAlias similarArrFields annFields tCase = do
fieldName
PLSQNotRequired
sel
let computedFieldTableSetSource = ComputedFieldTableSetSource fieldName selectSource
let selectSourceWithoutNulls =
case selectSource of
SelectSource {_ssFrom = S.FIFunc (S.FunctionExp {S.feAlias = Just (S.FunctionAlias {S._faIdentifier = fnIden})})} ->
selectSource {_ssWhere = S.BENotNull $ S.SERowIdentifier $ S.getTableAlias fnIden}
_ -> selectSource
computedFieldTableSetSource = ComputedFieldTableSetSource fieldName selectSourceWithoutNulls
extractor =
asJsonAggExtr selectTy (S.toColumnAlias fieldName) PLSQNotRequired $
orderByForJsonAgg selectSource
orderByForJsonAgg selectSourceWithoutNulls
pure
( computedFieldTableSetSource,
extractor,
Expand Down
2 changes: 1 addition & 1 deletion server/src-lib/Hasura/Backends/Postgres/Translate/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Hasura.Backends.Postgres.Translate.Types
SelectNode (SelectNode),
SelectSlicing (SelectSlicing, _ssLimit, _ssOffset),
SelectSorting (..),
SelectSource (SelectSource, _ssPrefix),
SelectSource (SelectSource, _ssPrefix, _ssWhere, _ssFrom),
SortingAndSlicing (SortingAndSlicing),
SourcePrefixes (..),
SimilarArrayFields,
Expand Down
Loading