You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorials/modules/ROOT/pages/pipelines-advanced.adoc
+33-26Lines changed: 33 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
= Advanced pipelines
1
+
= Advanced query pipelines
2
2
:keywords: typedb, typeql, tutorial, console
3
3
:pageTitle:
4
4
:summary:
@@ -16,6 +16,8 @@ This tutorial assumes you have access to a running TypeDB instance, as well as a
16
16
17
17
We recommend xref:{page-version}@home::install/ce.adoc[installing TypeDB Community Edition] as it is distributed bundled with Console for easy setup.
18
18
19
+
This is a continuation of xref:{page-version}@tutorials::pipelines-read.adoc[] and xref:{page-version}@tutorials::pipelines-crud.adoc[], though you don't need to have completed them to follow along.
20
+
19
21
== Example schema and data
20
22
21
23
IMPORTANT: TODO PERMALINKS TO `typedb/typedb-examples`
== Select order status conditionally using disjunctions
48
+
== Set order status based on stock using `or`
47
49
48
50
Let's say we want to create an order for a user, but we want to check the book is in stock first.
49
51
If it isn't, we need to mark the order as invalid.
50
52
51
-
We can do this by using a xref:{page-version}@typeql-reference::patterns/disjunctions.adoc[disjunction] to check if the book is in stock or not and setting `$status` accordingly.
53
+
We can do this by using an `or` to check if the book is in stock or not and setting `$status` accordingly.
52
54
53
55
[,typeql]
54
56
----
@@ -71,9 +73,9 @@ insert
71
73
has quantity 1;
72
74
----
73
75
74
-
== Collect sales statistics using ``reduce``-``groupby``
76
+
== Group sales statistics by genre using `reduce` with `groupby`
75
77
76
-
We can use a xref:{page-version}@typeql-reference::pipelines/reduce.adoc[reduce] stage to collect sales statistics for each genre within a given year.
78
+
We can use a reduce stage to collect sales statistics for each genre within a given year.
77
79
78
80
The `groupby` keyword allows us to sum the sale quantities grouped by the value of a specific variable, in this case `$genre`.
79
81
@@ -82,11 +84,8 @@ The `groupby` keyword allows us to sum the sale quantities grouped by the value
82
84
#!test[write, rollback, count = 8]
83
85
match
84
86
$item isa book, has genre $genre;
85
-
$order-line isa order-line,
86
-
links ($order, $item),
87
-
has quantity $quantity;
88
-
($order) isa action-execution,
89
-
has timestamp $timestamp;
87
+
order-line ($order, $item), has quantity $quantity;
88
+
action-execution ($order), has timestamp $timestamp;
90
89
$timestamp >= 2022-01-01T00:00;
91
90
$timestamp < 2023-01-01T00:00;
92
91
reduce
@@ -117,30 +116,26 @@ Note that when a book has multiple genres, the number of times it has been sold
117
116
Finished. Total answers: 8
118
117
----
119
118
120
-
== Update user data conditionally using optionals
119
+
== Update user loyalty tier based on spending using `try`
121
120
122
121
Imagine we want to update a user's loyalty tier based on their total spending.
123
-
When a user pays for an order, we want to update their total spending and, if their spending has crossed some threshold, we want to increase their loyalty tier as well.
122
+
After a user has paid for an order, we want to update their total spending and, if their spending has crossed some threshold, we want to increase their loyalty tier as well.
123
+
124
+
We've covered in xref:{page-version}@tutorials::pipelines-read.adoc#_fetch_discounted_price_with_optionals[Reading data] how to use a `try` pattern to assign a variable based on whether the data
125
+
exists in the database.
126
+
This extends to all kinds of patterns, including comparisons.
127
+
All patterns in a `try` block must succeed, otherwise the new variables defined in it will not be assigned.
124
128
125
-
We can use an xref:{page-version}@typeql-reference::patterns/optionals.adoc[optional] pattern to only assign the `$new-loyalty-tier` when necessary.
129
+
We can use a `try` pattern to only assign the `$new-loyalty-tier` when the user's total spending has exceeded the threshold.
126
130
Then, we can use the `try` keyword to conditionally set their loyalty tier in the `update` stage.
127
131
The `try` block in the `update` stage will only be executed if the `$new-loyalty-tier` is assigned.
128
132
129
133
[,typeql]
130
134
----
131
135
#!test[write, rollback, count = 1]
132
136
match
133
-
$user isa user, has id "u0002";
134
-
$order isa order, has id "o0004";
135
-
($user, $order) isa action-execution;
136
-
($order, $item) isa order-line, has quantity $quantity;
137
-
$item has price $price;
138
-
let $line-total = $price * $quantity;
139
-
reduce
140
-
$total = sum($line-total) groupby $order, $user;
141
-
match
142
-
$user has total-spending $spending;
143
-
let $new-spending = $spending + $total;
137
+
$user isa user, has id "u0002", has total-spending $spending;
138
+
let $new-spending = $spending + 9.99;
144
139
try {
145
140
$user has loyalty-tier $tier;
146
141
$new-spending > 100 * $tier * $tier;
@@ -168,9 +163,21 @@ TypeQL core concepts
168
163
Learn more about chaining TypeQL clauses into pipelines
0 commit comments