vaticle / typedb

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

Matching by role type without scope incorrectly rejects query #7014

Open flyingsilverfin opened 3 months ago

flyingsilverfin commented 3 months ago

Description

If a user queries using just the roles of relations, without specifying the relation types, TypeDB can accidentally Alias the role types together, and cause the query to be rejected.

Environment

  1. TypeDB distribution: Core/Cloud
  2. TypeDB version: 2.10 onwards

Reproducible Steps

define
  person sub entity;
  pet sub entity;
  toy sub entity;
  pet-ownership sub relation, relates owner, relates pet;
  toy-ownership sub relation, relates owner, relates toy;
  person plays pet-ownership:owner;
  pet plays pet-ownership:pet, plays toy-ownership:owner;
  toy plays toy-ownership:toy;
match
  (owner: $x, pet: $y);
  (owner: $y, toy: $z);
get;

We get:

[CXN05] The transaction is closed because of the error(s):
[QRY16] Invalid Query Pattern: Could not infer compatible types for the match pattern:
'{{ 
$_1 (owner:$y, toy:$z); 
$_0 (owner:$x, pet:$y); 
}}'

Expected result

We should find that the query is satisfiable, returning person that have pets, where the pets have toys.

Additional information

TypeDB receives type labelled variables: relation:owner, relation:pet, relation:owner, and relation:toy Due to receiving the same type label twice, we then construct a query graph with only one Type Variable for relation:owner! However, these should be kept separate and wired up to the neighbors.

This will require allowing anonymous type variables, and generating a new type variable with an anonymous name for each scoped type label (and the label constraint), and then allowing Type inference to resolve the labels on the variables independently.