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

Add bbe for group by/collect #4554

Merged
merged 3 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 37 additions & 0 deletions examples/aggregation/aggregation.bal
Original file line number Diff line number Diff line change
@@ -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`.
KavinduZoysa marked this conversation as resolved.
Show resolved Hide resolved
// The `itemName` is a non-grouping key and it becomes a sequence variable.
group by orderId
select [itemName];

// List of items per `orderId`
KavinduZoysa marked this conversation as resolved.
Show resolved Hide resolved
io:println(items);

var quantities = from var {itemName, quantity} in orders
// The `group by` clause create groups for each `itemName`.
KavinduZoysa marked this conversation as resolved.
Show resolved Hide resolved
// 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
KavinduZoysa marked this conversation as resolved.
Show resolved Hide resolved
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
KavinduZoysa marked this conversation as resolved.
Show resolved Hide resolved
collect sum(totPrice);

// Total Income from orders
KavinduZoysa marked this conversation as resolved.
Show resolved Hide resolved
io:println(income);
}
21 changes: 21 additions & 0 deletions examples/aggregation/aggregation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Aggregation
gimantha marked this conversation as resolved.
Show resolved Hide resolved

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`.
KavinduZoysa marked this conversation as resolved.
Show resolved Hide resolved

::: 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)
2 changes: 2 additions & 0 deletions examples/aggregation/aggregation.metatags
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
description: This BBE demonstrates how to group a collection, how to handle non grouping keys.
KavinduZoysa marked this conversation as resolved.
Show resolved Hide resolved
keywords: ballerina, ballerina by example, bbe, group, non-grouping keys, aggregate, collect
4 changes: 4 additions & 0 deletions examples/aggregation/aggregation.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$ bal run aggregation.bal
[["itemA","itemA","itemB"],["itemB"]]
[{"itemName":"itemA","quantity":3},{"itemName":"itemB","quantity":6}]
196.2