Advent of TypeQL: Day 2

A Christmassy data recovery adventure!

Joshua Send


Happy Advent!

Welcome to our Advent of TypeQL. Each day, we’ll post a small series of TypeQL challenges. There will be 14 days from December 11 to December 24, inclusive, so stay tuned!

It’ll be easiest to work through the days consecutively, since they are designed to incrementally build up an intuition of TypeQL. However, each day will also have a shortcut setup to just get a database for that day’s activity, allowing you to hop in without going through all the previous days challenges if you want.

You’ll want to use TypeDB Studio or TypeDB Console running against a TypeDB Cloud or local instance of TypeDB CE.

Background

Santa was preparing to release his Christmas plans. Finally done, he had just torn down his development and staging environments in preparation for a production launch of his grand Christmas plans…

Unfortunately at that precise moment, all of his Christmas plans, supposedly safely stashed in his git repository, got deleted by a pesky engineer with an overly permissive Github token and a mistyped command. Oh dear!

Santa contacted support and was able to recover some of his plans. However, they are incomplete. We’re going to help Santa get his plans back on track for Christmas, one day at a time!

Setup

If you’ve done Day 1, you can continue without setting anything up – just open up Studio or Console and get going!

If you’re new here, you’ll first want to spin up a TypeDB instance and connect with Studio or Console, and create a new database.

Then, you can get Santa’s recovered database schema by copying the linked schema text into a schema transaction’s query interface in Studio or Console, and the commit (note: by default, Studio auto-commits each query when set to “auto” mode).

Get the recovered data loaded by doing the same (follow link, copy text, paste into Studio or Console), but this time use a write transaction – and make sure you have committed.

At this point, you should have a database ready to go!

Day 2

Yesterday we got to know Santa’s plans by poking around some of his schema and data.

Today, we’ll continue to look at else Santa’s got planned – this time we’ll do some aggregates and computation. Then we’ll see how to fill in some data that must have been lost during Santa’s data disaster!

Counting and math

Let’s work out how many countries there are in the dataset – we can do this with a `reduce` clause after matching what we’re looking for!

Answer
match $c isa country;
reduce $count = count;
Hint

https://typedb.com/docs/typeql-reference/pipelines/reduce/

We’ve got quite a lot of countries… Countries also have country-statistics entities (with population and proportion-under-12 attributes), attached by demographics relations. Let’s take the first 3 countries we retrieve (using a limit clause), and get the statistics for them.

Answer
match
  $c isa country;
  demographics ($c, $stats);
  $stats isa country-statistics, has population $pop, has proportion-under-12 $under-12;
limit 3;
Hint

https://typedb.com/docs/typeql-reference/pipelines/limit/

Well that’s interesting! Let’s tweak this to work out how many kids under 12 that is per country – multiplying the population by the proportion. You can uselet in TypeQL to create new computed values and assign to new variables

Answer
match
  $c isa country;
  demographics ($c, $stats);
  $stats isa country-statistics, has population $pop, has proportion-under-12 $under-12;
  let $kids = $pop * $under-12;
limit 3;
Hint

https://typedb.com/docs/typeql-reference/expressions/

Ok – let’s work out the total number of kids in the whole world! We’ll need to go back to using reduce, but with a sum operation this time!

Answer
match
  $c isa country;
  demographics ($c, $stats);
  $stats isa country-statistics, has population $pop, has proportion-under-12 $under-12;
  let $kids = $pop * $under-12;
reduce $total = sum($kids);

I’ve got about 1.59 billion. If you want, you can add a second reduce operation assigned to a different variable to sum all people to see how many people there are overall!

Inserting missing data

We found out yesterday that there’s meant to be 1 working elf per continent right now. Santa’s little helpers are already working at supersonic speeds to get one present to each child! However, we’re missing a working elf who lives in Asia!

Let’s hire (insert) another elf with name “Cardamom”! His email should follow “<name>@northpole.com”, and he should have status “working”.

Answer
insert
  $e1 isa elf, has name "Cardamom", has email "cardamom@northpole.com", has status "working";
Hint

https://typedb.com/docs/typeql-reference/pipelines/insert/

Now we have our last working elf – lets add his lives-in relation. Recall from Day 1 that an elf plays the role being in a lives-in relation, while a continent plays the role location.

We’ll do this by match-ing the new elf and the Asian continent, and insert-ing the relation between them.

Answer
match
  $elf isa elf, has name "Cardamom";
  $asia isa continent, has name "Asia";
insert
  lives-in (location: $asia, being: $elf);
Hint

https://typedb.com/docs/typeql-reference/pipelines/insert/#_examples

Don’t forget to commit the changes if you’re not in Studio’s auto-commit mode.

Finally, let’s just write a quick match + reduce query to count the number of elves living in continents to ensure that the data is now correct – we expect 7, one for each continent!

Answer
match
  $elf isa elf;
  $continent isa continent;
  lives-in ($elf, $continent);
reduce $elves-in-continent = count;

See you soon!

We’ve made some progress rebuilding Santa’s data – great job! We’ll continue our work tomorrow – Day 3.

If you encounter any issues, want to chat, or anything else – feel free to post in our Discord or feel free to email me directly.

Share this article

TypeDB Newsletter

Stay up to date with the latest TypeDB announcements and events.

Subscribe to newsletter

Further Learning

Feedback