• Stars
    star
    212
  • Rank 181,052 (Top 4 %)
  • Language
    Java
  • License
    Mozilla Public Li...
  • Created over 5 years ago
  • Updated 10 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

TypeQL: the polymorphic query language of TypeDB

Factory GitHub release Discord Discussion Forum Stack Overflow Stack Overflow

Meet TypeQL (and TypeDB)

TypeDB is a strongly-typed database with a rich and logical type system. TypeDB empowers you to tackle complex problems, and TypeQL is its query language.

A higher level of expressivity

TypeQL allows you to model your domain based on logical and object-oriented principles. Composed of entity, relationship, and attribute types, as well as type hierarchies, roles, and rules, TypeQL allows you to think higher-level as opposed to join-tables, columns, documents, vertices, edges, and properties.

Entity-Relationship Model

TypeQL allows you to model your domain using the well-known Entity-Relationship model. It is composed of entity types, relation types, and attribute types, with the introduction of role types. TypeQL allows you to leverage the full expressivity of the ER model, and describe your schema through first normal form.

define

person sub entity,
  owns name,
  plays employment:employee;
company sub entity,
  owns name,
  plays employment:employer;
employment sub relation,
  relates employee,
  relates employer;
name sub attribute,
  value string;

Type Hierarchies

TypeQL allows you to easily model type inheritance into your domain model. Following logical and object-oriented principle, TypeQL allows data types to inherit the behaviours and properties of their supertypes. Complex data structures become reusable, and data interpretation becomes richer through polymorphism.

define

person sub entity,
  owns first-name,
  owns last-name;

student sub person;
undergrad sub student;
postgrad sub student;

teacher sub person;
supervisor sub teacher;
professor sub teacher;

N-ary Relations

In the real world, relations aren't just binary connections between two things. In rich systems, we often need to capture three or more things related with each other at once. Representing them as separate binary relationships would lose information. TypeQL can naturally represent arbitrary number of things as one relation.

match
 
$person isa person, has name "Leonardo";
$character isa character, has name "Jack";
$movie isa movie;
(actor: $person, character: $character, movie: $movie) isa cast;
get $movie;
 
answers>>
 
$movie isa movie, has name "Titanic";

Nested Relations

Relations are concepts we use to describe the association between two or more things. Sometimes, those things can be relations themselves. TypeQL can represent these structures naturally, as it enables relations to be nested in another relation, allowing you to express the model of your system in the most natural form.

match
 
$alice isa person, has name "Alice";
$bob isa person, has name "Bob";
$mar ($alice, $bob) isa marriage;
$city isa city;
($mar, $city) isa located;
 
answers>>
 
$city isa city, has name "London";

A higher degree of safety

Types provide a way to describe the logical structures of your data, allowing TypeDB to validate that your code inserts and queries data correctly. Query validation goes beyond static type checking, and includes logical validations of meaningless queries. With strict type-checking errors, you have a dataset that you can trust.

Logical Data Validation

Inserted data gets validated beyond static type checking of attribute value types. Entities are validated to only have the correct attributes, and relations are validated to only relate things that are logically allowed. TypeDB performs richer validation of inserted entities and relations by evaluating the polymorphic types of the things involved.

insert

$charlie isa person, has name "Charlie";
$dataCo isa company, has name "DataCo";
(husband: $charlie, wife: $dataCo) isa marriage; # invalid relation

commit>>

ERROR: invalid data detected during type validation

Logical Query Validation

Read queries executed on TypeDB go through a type resolution process. This process not only optimises the query's execution, but also acts as a static type checker to reject meaningless and unsatisfiable queries, as they are likely a user error.

match

$alice isa person, has name "Alice";
$bob isa person, has name "Bob";
($alice, $bob) isa marriage;
$dataCo isa company, has name "DataCo";
($bob, $dataCo) isa marriage; # invalid relation

answers>>

ERROR: unsatisfiable query detected during type resolution

Evolved with logical inference

TypeDB encodes your data for logical interpretation by a reasoning engine. It enables type-inference and rule-inference that creates logical abstractions of data. This allows the discovery of facts and patterns that would otherwise be too hard to find; and complex queries become much simpler.

Rules

TypeQL allows you to define rules in your schema. This extends the expressivity of your model as it enables the system to derive new conclusions when a certain logical form in your dataset is satisfied. Like functions in programming, rules can chain onto one another, creating abstractions of behaviour at the data level.

define

rule transitive-location:
when {
  (located: $x, locating: $y);
  (located: $y, locating: $z);
} then {
  (located: $x, locating: $z);
};

Inference

TypeDB's inference facility translates one query into all of its possible interpretations. This happens through two mechanisms: type-based and rule-based inference. Not only does this derive new conclusions and uncovers relationships that would otherwise be hidden, but it also enables the abstraction of complex patterns into simple queries.

match

$person isa person;
$uk isa country, has name "UK";
($person, $uk) isa location;
get $person;

answers>>

$person isa teacher, has name "Alice";
$person isa postgrad, has name "Bob";

TypeQL Grammar and Language Libraries

Note: All TypeDB Clients, as well as TypeDB Console, accept TypeQL syntax natively. If you are using TypeDB, you do not need additional libraries/tools to use TypeQL syntax natively. However, if you would like to construct TypeQL queries programmatically, you can do so with "Language Libraries" listed below.

Contributions

TypeDB & TypeQL has been built using various open-source Graph and Distributed Computing frameworks throughout its evolution. Today TypeDB & TypeQL is built using RocksDB, ANTLR, SCIP, Bazel, GRPC, and ZeroMQ, and Caffeine. In the past, TypeDB was enabled by various open-source technologies and communities that we are hugely thankful to: Apache Cassandra, Apache Hadoop, Apache Spark, Apache TinkerPop, and JanusGraph. Thank you!


Licensing

The TypeQL language libraries, such as TypeQL Rust and Java, are distributed under Apache License, Version 2.0, January 2004. The full license can be founder at: LICENSE.

However, the TypeQL Grammar libraries, located under the /grammar package in this repository, are distributed under the terms GNU Affero General Public License v3.0 ("AGPL 3.0") as published by the Free Software Foundation, but with a special exception. Please refer to TypeQL Grammar Licensing for further details.

Copyright (C) 2022 Vaticle

More Repositories

1

typedb

TypeDB: the polymorphic database powered by types
Java
3,694
star
2

typedb-ml

TypeDB-ML is the Machine Learning integrations library for TypeDB
Python
552
star
3

typedb-studio

TypeDB Studio (IDE)
Kotlin
184
star
4

biograkn

BioGrakn Knowledge Graph
180
star
5

bazel-distribution

Bazel rules for assembling and deploying software distributions (see @vaticle for usage example)
Starlark
155
star
6

typedb-driver-examples

TypeDB Driver Example Projects and Tutorials
HTML
85
star
7

typedb-driver-python

TypeDB Driver for Python
Python
67
star
8

typedb-awesome

A curated list of awesome TypeDB frameworks libraries, software and resources.
58
star
9

typedb-driver-nodejs

TypeDB Driver for Node.js
TypeScript
32
star
10

typedb-driver

TypeDB Drivers for Rust, Python, Java, Node.js, C, C++, and C#.
Rust
29
star
11

typedb-docs

TypeDB Documentation
Java
24
star
12

typedb-protocol

TypeDB (Core and Cluster) RPC Communication Protocol
Starlark
15
star
13

typedb-driver-rust

TypeDB Driver for Rust
Rust
12
star
14

dependencies

Bazel dependency declarations for build tools reused across @vaticle repositories (only for @vaticle)
Starlark
12
star
15

typedb-behaviour

TypeDB Behaviour Test Specification
Gherkin
10
star
16

bazel-intellij-rust-example

Starlark
10
star
17

typedb-benchmark

TypeDB Simulation and Benchmarking Library
Kotlin
9
star
18

typeql-lang-java

TypeQL language library for Java
Java
9
star
19

typedb-common

TypeDB Common Libraries and Scripts
Java
7
star
20

typedb-console

TypeDB Console: CLI for TypeDB and TypeDB Cluster
Java
7
star
21

homebrew-tap

Ruby
6
star
22

force-graph

Force-directed graph placement library for Java. Rewrite of d3-force.
Java
4
star
23

factory-tracing

Vaticle Factory Tracing Client API for Performance Analysis
Java
3
star
24

ansible-role-grakn

Ansible Role - Grakn
Shell
3
star
25

typedb-examples

TypeDB Examples Projects and Tutorials
3
star
26

typedb-iam

Open Source IAM Schema and Simulation
Kotlin
2
star
27

web-cosmos

Grakn Cosmos: The Universe of Orderly Systems
Handlebars
2
star
28

sudoku-solver

Java
1
star
29

compose-perf

compose-jb Performance Benchmark
Starlark
1
star
30

web-infrastructure

Shell
1
star