vaticle / typedb

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

Wrong and inconsistent query results with inference enabled #7051

Open Proinn opened 2 months ago

Proinn commented 2 months ago

Description

I am getting inconsistent query results from this schema/data/query combination (with inference). I am sometimes getting the following inferred relation in the query results:

$b iid 0x847080077ffffffffffffffe (relation_1: iid 0x847080067fffffffffffffff) isa sub_relation_2;
$a iid 0x847080067fffffffffffffff (role_entity_1: iid 0x826e80018000000000000000) isa sub_relation_1_2;

which should not be inferred, as there is no rule which can add the _sub_relation2 with a _sub_relation_12 as the _relation1 role in my schema. But also running the query multiple times gives different results. see the following screenshots:

Screenshot from 2024-04-26 17-14-39 Screenshot from 2024-04-26 17-14-17

Environment

  1. TypeDB distribution: Core

  2. TypeDB version: 2.27.0

  3. Environment: Linux

  4. Client and version: It seems to go wrong both with the python driver and using typedb studio. typedb-driver==2.27.0 typedb-protocol==2.18.0

  5. Other details:

Reproducible Steps

  1. Set up create a database with this schema:
    
    define 
    entity_1 sub entity,
    plays relation_1:role_entity_1;

entity_2 sub entity, plays sub_relation_1:role_entity_2,
plays sub_sub_relation_1_1:role_1_entity_2;

relation_2 sub relation, relates relation_1; sub_relation_2 sub relation_2;

relation_1 sub relation, relates role_entity_1, plays relation_2:relation_1;

sub_relation_1 sub relation_1, 
    relates role_entity_2;
    sub_sub_relation_1_2 sub sub_relation_1;
    sub_sub_relation_1_1 sub sub_relation_1,
        relates role_1_entity_2 as role_entity_2;

    sub_relation_1_1 sub relation_1;
    sub_relation_1_2 sub relation_1;

rule rule_sub_sub_relation_1_1: when { $entity_1 isa entity_1; $sub_sub_relation_1_1 (role_entity_1: $entity_1, role_1_entity_2: $role_1_entity_2) isa sub_sub_relation_1_1; } then { (relation_1: $sub_sub_relation_1_1) isa sub_relation_2; };

rule rule_sub_relation_1_1: when { $entity_1 isa entity_1; $sub_relation_1_1 (role_entity_1: $entity_1) isa sub_relation_1_1; } then { (relation_1: $sub_relation_1_1) isa sub_relation_2; };

rule sub_relation_1_1: when { $e isa entity_1; } then { (role_entity_1: $e) isa sub_relation_1_1; };

rule sub_relation_1_2: when { $e isa entity_1; } then { (role_entity_1: $e) isa sub_relation_1_2; };


add this data:

insert $entity_1 isa entity_1;

2. Execute
Execute this query with inference enabled:

match $e isa entity_1; $a (role_entity_1: $e) isa relation_1; $b (relation_1: $a) isa relation_2; get;


3. Unexpected result
Results where variable $a is matched to an inferred relation sub_relation_1_2.

{ $e iid 0x826e80028000000000000000 isa entity_1; $b iid 0x847080027fffffffffffffff (relation_1: iid 0x847080037fffffffffffffff) isa sub_relation_2; $a iid 0x847080037fffffffffffffff (role_entity_1: iid 0x826e80028000000000000000) isa sub_relation_1_1; } { $b iid 0x847080027ffffffffffffffe (relation_1: iid 0x847080047fffffffffffffff) isa sub_relation_2; $e iid 0x826e80028000000000000000 isa entity_1; $a iid 0x847080047fffffffffffffff (role_entity_1: iid 0x826e80028000000000000000) isa sub_relation_1_2; }

Also, the output of the query is randomly changing when rerunning the query to the correct answer:

{ $e iid 0x826e80028000000000000000 isa entity_1; $b iid 0x847080027fffffffffffffff (relation_1: iid 0x847080037fffffffffffffff) isa sub_relation_2; $a iid 0x847080037fffffffffffffff (role_entity_1: iid 0x826e80028000000000000000) isa sub_relation_1_1; }


## Expected result
Only getting the following result consistently.

{ $e iid 0x826e80028000000000000000 isa entity_1; $b iid 0x847080027fffffffffffffff (relation_1: iid 0x847080037fffffffffffffff) isa sub_relation_2; $a iid 0x847080037fffffffffffffff (role_entity_1: iid 0x826e80028000000000000000) isa sub_relation_1_1; }



## Additional information

Relevant logs from TypeDB or Driver: