vaticle / typedb

TypeDB: the polymorphic database powered by types
https://typedb.com
Mozilla Public License 2.0
3.72k stars 337 forks source link

TypeDB 3.0: all query clauses are chainable streaming operations #7037

Open flyingsilverfin opened 2 months ago

flyingsilverfin commented 2 months ago

Problem to Solve

We aim to make our TypeQL queries more consistent and expressive, reformulating them in terms of streams/iterators commonly known from programming languages, also allowing them to be chained into longer sequences of operations.

Proposed Solution

We aim to have the following type system for different clauses and operations on streams of concept maps (Stream<ConceptMap>). Operations can be freely composed based on this type system.

Production operations

These operations are used to produced streams.

While match can operate on an empty input stream, the following operations cannot. We also refer to them as modifying operations. They are all of type Stream<ConceptMap> -> Stream<ConceptMap>.

Effect operations

These operations are called primarily for their effect on the DB (rather than on the stream). This effect may also affect the stream.

Aggregation operations

These operations aggregate a stream into a single object (e.g. a value, a list of values, or a list of JSONs). These operation end a query clause chain.

Control operation

Control operation give the user the ability to control the execution of a chained query.

Summary

As a result of these changes, the user will always be able to truncate their query stream at any clause and execute it, without having to do an explicit get clause. This makes get redundant for this operation, and we absorb get's filtering operating into the new filter modifier.

Here's an example of building a longer query stream:

match
  ($x, $y) isa friendship;
// Unique stream<$x, $y>
filter $x; 
// Unique stream<$x>
insert
  $x has name "Alice";
// Unique stream<$x>
match
  $x has email $e; $e == "abc@email.com";
// Unique stream<$x, $e>
delete
  $x isa person;
// Non-unique stream<$e>
reduce count;
// returns number of elements in the stream, which is the number of deleted people