server: Remove multiple column count aggregate support from Data Connectors [GDW-207]

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5224
GitOrigin-RevId: be62ad21ed60cf5c9fb05cda8454b99a0c024866
This commit is contained in:
Daniel Chambers 2022-07-28 17:24:13 +10:00 committed by hasura-bot
parent ac4f3d8ed0
commit 94ddf10df6
10 changed files with 30 additions and 41 deletions

View File

@ -315,12 +315,7 @@ const starCountAggregateFunction = (rows: Record<string, ScalarValue>[]): Scalar
};
const columnCountAggregateFunction = (aggregate: ColumnCountAggregate) => (rows: Record<string, ScalarValue>[]): ScalarValue => {
// HACK: Only accept a single column. Multiple column support is being removed.
const column = aggregate.columns.length == 1
? aggregate.columns[0]
: (() => {throw new Error("Multiple columns in a count aggregate are not supported");})();
const nonNullValues = rows.map(row => row[column]).filter(v => v !== null);
const nonNullValues = rows.map(row => row[aggregate.column]).filter(v => v !== null);
return aggregate.distinct
? (new Set(nonNullValues)).size

View File

@ -829,12 +829,9 @@
},
"ColumnCountAggregate": {
"properties": {
"columns": {
"description": "The columns to apply the count aggregate function to",
"items": {
"type": "string"
},
"type": "array"
"column": {
"description": "The column to apply the count aggregate function to",
"type": "string"
},
"distinct": {
"description": "Whether or not only distinct items should be counted",
@ -848,7 +845,7 @@
}
},
"required": [
"columns",
"column",
"distinct",
"type"
],

View File

@ -4,9 +4,9 @@
export type ColumnCountAggregate = {
/**
* The columns to apply the count aggregate function to
* The column to apply the count aggregate function to
*/
columns: Array<string>;
column: string;
/**
* Whether or not only distinct items should be counted
*/

View File

@ -12,7 +12,6 @@ import Autodocodec
import Data.Aeson (FromJSON, ToJSON)
import Data.Data (Data)
import Data.HashMap.Strict qualified as HashMap
import Data.List.NonEmpty (NonEmpty)
import Data.OpenApi (ToSchema)
import GHC.Generics (Generic)
import Hasura.Backends.DataConnector.API.V0.Column qualified as API.V0
@ -57,7 +56,7 @@ instance HasCodec SingleColumnAggregateFunction where
]
data ColumnCountAggregate = ColumnCountAggregate
{ _ccaColumns :: NonEmpty API.V0.ColumnName,
{ _ccaColumn :: API.V0.ColumnName,
_ccaDistinct :: Bool
}
deriving stock (Eq, Ord, Show, Generic, Data)
@ -65,7 +64,7 @@ data ColumnCountAggregate = ColumnCountAggregate
columnCountAggregateObjectCodec :: JSONObjectCodec ColumnCountAggregate
columnCountAggregateObjectCodec =
ColumnCountAggregate
<$> requiredField "columns" "The columns to apply the count aggregate function to" .= _ccaColumns
<$> requiredField "column" "The column to apply the count aggregate function to" .= _ccaColumn
<*> requiredField "distinct" "Whether or not only distinct items should be counted" .= _ccaDistinct
data Aggregate

View File

@ -202,10 +202,10 @@ countTypeInput' ::
Maybe (P.Parser 'P.Both n IR.C.Name) ->
P.InputFieldsParser n (IR.CountDistinct -> IR.A.CountAggregate)
countTypeInput' = \case
Just columnEnum -> mkCountAggregate <$> P.fieldOptional Name._columns Nothing (P.list columnEnum)
Just columnEnum -> mkCountAggregate <$> P.fieldOptional Name._column Nothing columnEnum
Nothing -> pure $ mkCountAggregate Nothing
where
mkCountAggregate :: Maybe [IR.C.Name] -> IR.CountDistinct -> IR.A.CountAggregate
mkCountAggregate :: Maybe IR.C.Name -> IR.CountDistinct -> IR.A.CountAggregate
mkCountAggregate Nothing _ = IR.A.StarCount
mkCountAggregate (Just cols) IR.SelectCountDistinct = maybe IR.A.StarCount IR.A.ColumnDistinctCount $ nonEmpty cols
mkCountAggregate (Just cols) IR.SelectCountNonDistinct = maybe IR.A.StarCount IR.A.ColumnCount $ nonEmpty cols
mkCountAggregate (Just column) IR.SelectCountDistinct = IR.A.ColumnDistinctCount column
mkCountAggregate (Just column) IR.SelectCountNonDistinct = IR.A.ColumnCount column

View File

@ -24,8 +24,8 @@ data Aggregate
instance Witch.From Aggregate API.Aggregate where
from (SingleColumn singleColumn) = API.SingleColumn (Witch.from singleColumn)
from (Count StarCount) = API.StarCount
from (Count (ColumnCount columns)) = API.ColumnCount $ API.ColumnCountAggregate {_ccaColumns = Witch.from <$> columns, _ccaDistinct = False}
from (Count (ColumnDistinctCount columns)) = API.ColumnCount $ API.ColumnCountAggregate {_ccaColumns = Witch.from <$> columns, _ccaDistinct = True}
from (Count (ColumnCount column)) = API.ColumnCount $ API.ColumnCountAggregate {_ccaColumn = Witch.from column, _ccaDistinct = False}
from (Count (ColumnDistinctCount column)) = API.ColumnCount $ API.ColumnCountAggregate {_ccaColumn = Witch.from column, _ccaDistinct = True}
data SingleColumnAggregate = SingleColumnAggregate
{ _scaFunction :: SingleColumnAggregateFunction,
@ -71,7 +71,7 @@ instance Witch.From SingleColumnAggregateFunction API.SingleColumnAggregateFunct
data CountAggregate
= StarCount
| ColumnCount (NonEmpty IR.C.Name)
| ColumnDistinctCount (NonEmpty IR.C.Name)
| ColumnCount IR.C.Name
| ColumnDistinctCount IR.C.Name
deriving stock (Data, Eq, Generic, Ord, Show)
deriving anyclass (Cacheable, FromJSON, Hashable, NFData, ToJSON)

View File

@ -13,7 +13,6 @@ import Hasura.Backends.DataConnector.API.V0.ColumnSpec (genColumnName)
import Hasura.Prelude
import Hedgehog
import Hedgehog.Gen qualified as Gen
import Hedgehog.Range (linear)
import Test.Aeson.Utils (jsonOpenApiProperties, testToFromJSONToSchema)
import Test.Hspec
@ -31,10 +30,10 @@ spec = do
|]
describe "ColumnCount" $ do
testToFromJSONToSchema
(ColumnCount $ ColumnCountAggregate [ColumnName "my_column_name"] True)
(ColumnCount $ ColumnCountAggregate (ColumnName "my_column_name") True)
[aesonQQ|
{ "type": "column_count",
"columns": ["my_column_name"],
"column": "my_column_name",
"distinct": true
}
|]
@ -83,7 +82,7 @@ genSingleColumnAggregate =
genColumnCountAggregate :: MonadGen m => m ColumnCountAggregate
genColumnCountAggregate =
ColumnCountAggregate
<$> Gen.nonEmpty (linear 0 5) genColumnName
<$> genColumnName
<*> Gen.bool
genSingleColumnAggregateFunction :: MonadGen m => m SingleColumnAggregateFunction

View File

@ -54,8 +54,8 @@ spec api sourceName config = describe "Aggregate Queries" $ do
Data.responseAggregates response `shouldBe` expectedAggregates
describe "Column Count" $ do
it "counts all rows with non-null columns - single column" $ do
let aggregates = KeyMap.fromList [("count_cols", ColumnCount $ ColumnCountAggregate (ColumnName "BillingState" :| []) False)]
it "counts all rows with non-null columns" $ do
let aggregates = KeyMap.fromList [("count_cols", ColumnCount $ ColumnCountAggregate (ColumnName "BillingState") False)]
let queryRequest = invoicesQueryRequest aggregates
response <- (api // _query) sourceName config queryRequest
@ -66,7 +66,7 @@ spec api sourceName config = describe "Aggregate Queries" $ do
it "can count all rows with non-null values in a column, after applying pagination and filtering" $ do
let where' = ApplyBinaryComparisonOperator GreaterThanOrEqual (localComparisonColumn "InvoiceId") (ScalarValue (Number 380))
let aggregates = KeyMap.fromList [("count_cols", ColumnCount $ ColumnCountAggregate (ColumnName "BillingState" :| []) False)]
let aggregates = KeyMap.fromList [("count_cols", ColumnCount $ ColumnCountAggregate (ColumnName "BillingState") False)]
let queryRequest = invoicesQueryRequest aggregates & qrQuery %~ (qLimit .~ Just 20 >>> qWhere .~ Just where')
response <- (api // _query) sourceName config queryRequest
@ -82,7 +82,7 @@ spec api sourceName config = describe "Aggregate Queries" $ do
Data.responseAggregates response `shouldBe` expectedAggregates
it "can count all rows with distinct non-null values in a column" $ do
let aggregates = KeyMap.fromList [("count_cols", ColumnCount $ ColumnCountAggregate (ColumnName "BillingState" :| []) True)]
let aggregates = KeyMap.fromList [("count_cols", ColumnCount $ ColumnCountAggregate (ColumnName "BillingState") True)]
let queryRequest = invoicesQueryRequest aggregates
response <- (api // _query) sourceName config queryRequest
@ -93,7 +93,7 @@ spec api sourceName config = describe "Aggregate Queries" $ do
it "can count all rows with distinct non-null values in a column, after applying pagination and filtering" $ do
let where' = ApplyBinaryComparisonOperator GreaterThanOrEqual (localComparisonColumn "InvoiceId") (ScalarValue (Number 380))
let aggregates = KeyMap.fromList [("count_cols", ColumnCount $ ColumnCountAggregate (ColumnName "BillingState" :| []) True)]
let aggregates = KeyMap.fromList [("count_cols", ColumnCount $ ColumnCountAggregate (ColumnName "BillingState") True)]
let queryRequest = invoicesQueryRequest aggregates & qrQuery %~ (qLimit .~ Just 20 >>> qWhere .~ Just where')
response <- (api // _query) sourceName config queryRequest
@ -144,7 +144,7 @@ spec api sourceName config = describe "Aggregate Queries" $ do
let aggregates =
KeyMap.fromList
[ ("count", StarCount),
("distinctBillingStates", ColumnCount $ ColumnCountAggregate (ColumnName "BillingState" :| []) True),
("distinctBillingStates", ColumnCount $ ColumnCountAggregate (ColumnName "BillingState") True),
("maxTotal", SingleColumn $ SingleColumnAggregate Max (ColumnName "Total"))
]
let queryRequest = invoicesQueryRequest aggregates

View File

@ -209,8 +209,8 @@ tests opts = describe "Aggregate Query Tests" $ do
Invoice_aggregate {
aggregate {
count
countColumn: count(columns: BillingState)
countColumnDistinct: count(columns: BillingState, distinct: true)
countColumn: count(column: BillingState)
countColumnDistinct: count(column: BillingState, distinct: true)
}
}
}

View File

@ -8,7 +8,6 @@ where
import Data.Aeson qualified as Aeson
import Data.Aeson.KeyMap qualified as KM
import Data.HashMap.Strict qualified as HashMap
import Data.List.NonEmpty (NonEmpty (..))
import Harness.Backend.DataConnector (TestCase (..))
import Harness.Backend.DataConnector qualified as DataConnector
import Harness.Quoter.Graphql (graphql)
@ -223,7 +222,7 @@ tests opts = describe "Aggregate Query Tests" $ do
Invoice_aggregate(limit: 2) {
counts: aggregate {
count
uniqueBillingCountries: count(columns: BillingCountry, distinct: true)
uniqueBillingCountries: count(column: BillingCountry, distinct: true)
}
ids: aggregate {
minimum: min {
@ -311,7 +310,7 @@ tests opts = describe "Aggregate Query Tests" $ do
Just $
KM.fromList
[ ("counts_count", API.StarCount),
("counts_uniqueBillingCountries", API.ColumnCount (API.ColumnCountAggregate (API.ColumnName "BillingCountry" :| []) True)),
("counts_uniqueBillingCountries", API.ColumnCount (API.ColumnCountAggregate (API.ColumnName "BillingCountry") True)),
("ids_minimum_Id", API.SingleColumn (API.SingleColumnAggregate API.Min (API.ColumnName "InvoiceId"))),
("ids_max_InvoiceId", API.SingleColumn (API.SingleColumnAggregate API.Max (API.ColumnName "InvoiceId")))
],