From 30ceb90b8a3d6944a2e747d6005186a7ea14144a Mon Sep 17 00:00:00 2001 From: kavindu Date: Tue, 9 May 2023 11:06:37 +0530 Subject: [PATCH 1/3] Add bbe for group by/collect --- examples/aggregation/aggregation.bal | 37 +++++++++++++++++++++++ examples/aggregation/aggregation.md | 21 +++++++++++++ examples/aggregation/aggregation.metatags | 2 ++ examples/aggregation/aggregation.out | 4 +++ 4 files changed, 64 insertions(+) create mode 100644 examples/aggregation/aggregation.bal create mode 100644 examples/aggregation/aggregation.md create mode 100644 examples/aggregation/aggregation.metatags create mode 100644 examples/aggregation/aggregation.out diff --git a/examples/aggregation/aggregation.bal b/examples/aggregation/aggregation.bal new file mode 100644 index 0000000000..c7451256f3 --- /dev/null +++ b/examples/aggregation/aggregation.bal @@ -0,0 +1,37 @@ +import ballerina/io; + +public function main() returns error? { + var orders = [ + {orderId: 1, itemName: "A", price: 23.4, quantity: 2}, + {orderId: 1, itemName: "A", price: 20.4, quantity: 1}, + {orderId: 2, itemName: "B", price: 21.5, quantity: 3}, + {orderId: 1, itemName: "B", price: 21.5, quantity: 3} + ]; + + var items = from var {orderId, itemName} in orders + // The `group by` clause create groups for each `orderId`. + // The `itemName` is a non-grouping key and it becomes a sequence variable. + group by orderId + select [itemName]; + + // List of items per `orderId` + io:println(items); + + var quantities = from var {itemName, quantity} in orders + // The `group by` clause create groups for each `itemName`. + // The `quantity` is a non-grouping key and it becomes a sequence variable. + group by itemName + select {itemName, quantity: sum(quantity)}; + + // List of quantity per item + io:println(quantities); + + var income = from var {price, quantity} in orders + let var totPrice = price*quantity + // The `collect` clause creates a single group and all variables become + // non-grouping keys + collect sum(totPrice); + + // Total Income from orders + io:println(income); +} diff --git a/examples/aggregation/aggregation.md b/examples/aggregation/aggregation.md new file mode 100644 index 0000000000..f91738020c --- /dev/null +++ b/examples/aggregation/aggregation.md @@ -0,0 +1,21 @@ +# Aggregation + +The `group by` clause in the query expression can be used to group the elements in a collection. Grouping happens based on the grouping keys provided in `group by` clause. Non grouping keys becomes aggregated variables that can be used as a list or an argument to a rest parameter of a langlib function. + +The `collect` clause is used to collect the collection into one group. All the variables becoes aggregated variables and those variables can be used as a list or an argument to a rest parameter of a langlib function same as in `group by`. + +::: code aggregation.bal ::: + +::: out aggregation.out ::: + +## Related links +- [Query expressions](/learn/by-example/query-expressions) +- [Let clause in query expression](/learn/by-example/let-clause) +- [Limit clause in query expression](/learn/by-example/limit-clause) +- [Joining iterable objects using query](/learn/by-example/joining-iterable-objects) +- [Querying tables](/learn/by-example/querying-tables) +- [Create maps with query expression](/learn/by-example/create-maps-with-query) +- [Create tables with query expression](/learn/by-example/create-tables-with-query) +- [Create streams with query expression](/learn/by-example/create-streams-with-query) +- [On conflict clause in query expression](/learn/by-example/on-conflict-clause) +- [Nested query expressions](/learn/by-example/nested-query-expressions) diff --git a/examples/aggregation/aggregation.metatags b/examples/aggregation/aggregation.metatags new file mode 100644 index 0000000000..e6146e8607 --- /dev/null +++ b/examples/aggregation/aggregation.metatags @@ -0,0 +1,2 @@ +description: This BBE demonstrates how to group a collection, how to handle non grouping keys. +keywords: ballerina, ballerina by example, bbe, group, non-grouping keys, aggregate, collect diff --git a/examples/aggregation/aggregation.out b/examples/aggregation/aggregation.out new file mode 100644 index 0000000000..37427165ae --- /dev/null +++ b/examples/aggregation/aggregation.out @@ -0,0 +1,4 @@ +$ bal run aggregation.bal +[["itemA","itemA","itemB"],["itemB"]] +[{"itemName":"itemA","quantity":3},{"itemName":"itemB","quantity":6}] +196.2 From 0077fc5a9f07343233e14f060198d7873da04daa Mon Sep 17 00:00:00 2001 From: kavindu Date: Tue, 13 Jun 2023 13:18:09 +0530 Subject: [PATCH 2/3] Fix review suggestions --- examples/aggregation/aggregation.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/aggregation/aggregation.md b/examples/aggregation/aggregation.md index f91738020c..ff5b269936 100644 --- a/examples/aggregation/aggregation.md +++ b/examples/aggregation/aggregation.md @@ -1,8 +1,8 @@ # Aggregation -The `group by` clause in the query expression can be used to group the elements in a collection. Grouping happens based on the grouping keys provided in `group by` clause. Non grouping keys becomes aggregated variables that can be used as a list or an argument to a rest parameter of a langlib function. +The `group by` clause in the query expression can group the elements in a collection. Grouping happens based on the grouping keys provided in `group by` clause. For each group, grouping keys are unique. All other variables other than grouping keys are called non-grouping keys. For each group, non-grouping keys become sequence variables. Those variables can be used as a list or an argument to a rest parameter of a langlib function. -The `collect` clause is used to collect the collection into one group. All the variables becoes aggregated variables and those variables can be used as a list or an argument to a rest parameter of a langlib function same as in `group by`. +The `collect` clause collects the collection into one group. All the variables become aggregated variables and those variables can be used as a list or an argument to a rest parameter of a langlib function same as in `group by`. ::: code aggregation.bal ::: From 18418b272ee445f5565a2ac9237e9e0cc380a203 Mon Sep 17 00:00:00 2001 From: kavindu Date: Fri, 16 Jun 2023 21:13:22 +0530 Subject: [PATCH 3/3] Change output --- examples/aggregation/aggregation.out | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/aggregation/aggregation.out b/examples/aggregation/aggregation.out index 37427165ae..2636263ed2 100644 --- a/examples/aggregation/aggregation.out +++ b/examples/aggregation/aggregation.out @@ -1,4 +1,4 @@ $ bal run aggregation.bal -[["itemA","itemA","itemB"],["itemB"]] -[{"itemName":"itemA","quantity":3},{"itemName":"itemB","quantity":6}] +[["A","A","B"],["B"]] +[{"itemName":"A","quantity":3},{"itemName":"B","quantity":6}] 196.2