Skip to content

Commit

Permalink
Allow __typename in BigQuery aggregate queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Harding authored and hasura-bot committed Jun 27, 2023
1 parent 37560dd commit 87abdf1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
44 changes: 42 additions & 2 deletions server/lib/api-tests/src/Test/Queries/AggregationSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ spec = do
Fixture.customOptions =
Just
$ Fixture.defaultOptions
{ Fixture.stringifyNumbers = True,
Fixture.skipTests = Just "BigQuery returns numbers as strings, which means the second test fails"
{ Fixture.stringifyNumbers = True
}
},
(Fixture.fixture $ Fixture.Backend Postgres.backendTypeMetadata)
Expand Down Expand Up @@ -240,3 +239,44 @@ tests = do
|]

shouldReturnYaml testEnvironment actual expected

it "Fetch __typename for an aggregate" \testEnvironment -> do
let schemaName :: Schema.SchemaName
schemaName = Schema.getSchemaName testEnvironment

-- SQL Server returns int type when the input is of int type, which is different than the rest.
-- https://learn.microsoft.com/en-us/sql/t-sql/functions/avg-transact-sql?view=sql-server-ver16#return-types
let avgResult :: String
avgResult
| fmap BackendType.backendType (getBackendTypeConfig testEnvironment) == Just Fixture.SQLServer = "3"
| otherwise = "3.25"

let expected :: Value
expected =
[interpolateYaml|
data:
#{schemaName}_article_aggregate:
aggregate:
avg:
rating: #{avgResult}
__typename: #{schemaName}_article_avg_fields
|]

actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
#{schemaName}_article_aggregate {
aggregate {
avg {
rating
__typename
}
}
}
}
|]

shouldReturnYaml testEnvironment actual expected
11 changes: 7 additions & 4 deletions server/src-lib/Hasura/Backends/BigQuery/ToQuery.hs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,11 @@ fromNullAggregate = \case
fromAggregate :: Aggregate -> Printer
fromAggregate =
\case
-- There's no reason why we'd be aggregating a ValueExpression /unless/
-- that ValueExpression is a reserved GraphQL name like __typename. In
-- which case, we don't want to run it through the aggregation function;
-- we just want to return the value.
OpAggregate _ (ValueExpression (TypedValue ty val)) -> fromValue ty val
CountAggregate countable -> "COUNT(" <+> fromCountable countable <+> ")"
OpAggregate text arg ->
UnsafeTextPrinter text <+> "(" <+> fromExpression arg <+> ")"
Expand All @@ -480,10 +485,8 @@ fromAggregate =
", "
( map
( \(alias, arg) ->
UnsafeTextPrinter text
<+> "("
<+> fromExpression arg
<+> ") AS "
fromAggregate (OpAggregate text arg)
<+> " AS "
<+> fromNameText alias
)
(toList args)
Expand Down

0 comments on commit 87abdf1

Please sign in to comment.