Reputation: 684
Is it possible to create subqueries in TypeQL?
I have the following Typedb schema
define
color-name sub attribute, value string;
color-red-component sub attribute, value long;
color-green-component sub attribute, value long;
color-blue-component sub attribute, value long;
color-to-color-distance sub attribute, value double;
color sub entity,
owns color-name,
owns color-red-component,
owns color-green-component,
owns color-blue-component,
plays color-to-color-distance-relation:color-instance;
color-to-color-distance-relation sub relation,
owns color-to-color-distance,
relates color-instance;
I want to find the closest color to a given color. I am using the following query for that.
match
$input-color isa color, has color-name "Scarlet";
$closest-color isa color, has color-name $closest-color-name;
$closest-color has color-red-component $r;
$closest-color has color-green-component $g;
$closest-color has color-blue-component $b;
$distance-relation (color-instance:$input-color, color-instance:$closest-color) isa color-to-color-distance-relation;
$distance-relation has color-to-color-distance $distance;
get $distance, $closest-color, $closest-color-name, $r, $g, $b;
sort $distance asc;
limit 1;
However, it is really slow. It takes ~1 min on my machine. To solve that I changed it as follows:
match
input-color isa color, has color-name "Scarlet";
$closest-color isa color, has color-name $closest-color-name;
$distance-relation (color-instance:input-color, color-instance:$closest-color) isa color-to-color-distance-relation;
$distance-relation has color-to-color-distance $distance;
get $distance, $closest-color;
sort $distance asc;
limit 1;
And now it takes less than 200 milliseconds. Therefore, I thought I could write the last query as a subquery, and select other attributes that I want in the outer query. So, is it possible to write subqueries in TypeQL? If not, what would be another solution to my use case?
Upvotes: 0
Views: 118
Reputation: 779
TypeDB/TypeQL doesn't have support for subqueries at the moment. A workaround for this is to pipeline your queries on the client side, i.e. make the query, get the result, and feed it back in to your next query.
It should also be possible for you to build a larger query where you repeat your subquery within.
Upvotes: 1