Skip to content

Commit

Permalink
#1524 Fix query issue with matching (ChildOf, 0) when iterating compo…
Browse files Browse the repository at this point in the history
…nent ids
  • Loading branch information
SanderMertens authored Jan 17, 2025
1 parent c4457d7 commit c57e7c3
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 2 deletions.
7 changes: 7 additions & 0 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -71407,6 +71407,7 @@ bool flecs_query_idsright(
}
}

next:
do {
cur = op_ctx->cur = op_ctx->cur->first.next;
} while (cur && !cur->cache.tables.count); /* Skip empty ids */
Expand All @@ -71415,6 +71416,12 @@ bool flecs_query_idsright(
return false;
}

if (cur->id == ecs_pair(EcsChildOf, 0)) {
/* Skip the special (ChildOf, 0) entry for root entities, as 0 is
* not a valid target and could be matched by (ChildOf, *) */
goto next;
}

flecs_query_set_vars(op, cur->id, ctx);

if (op->field_index != -1) {
Expand Down
7 changes: 7 additions & 0 deletions src/query/engine/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ bool flecs_query_idsright(
}
}

next:
do {
cur = op_ctx->cur = op_ctx->cur->first.next;
} while (cur && !cur->cache.tables.count); /* Skip empty ids */
Expand All @@ -635,6 +636,12 @@ bool flecs_query_idsright(
return false;
}

if (cur->id == ecs_pair(EcsChildOf, 0)) {
/* Skip the special (ChildOf, 0) entry for root entities, as 0 is
* not a valid target and could be matched by (ChildOf, *) */
goto next;
}

flecs_query_set_vars(op, cur->id, ctx);

if (op->field_index != -1) {
Expand Down
2 changes: 2 additions & 0 deletions test/query/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,8 @@
"2_any_src",
"2_any_src_w_pair",
"1_any_src_w_pair_tgt_var",
"1_any_src_w_pair_tgt_var_2",
"1_any_src_w_pair_tgt_var_childof",
"1_any_src_w_pair_rel_var",
"1_any_src_w_pair_tgt_this",
"1_any_src_w_pair_rel_this",
Expand Down
101 changes: 101 additions & 0 deletions test/query/src/Basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -3702,6 +3702,107 @@ void Basic_1_any_src_w_pair_tgt_var(void) {
ecs_fini(world);
}

void Basic_1_any_src_w_pair_tgt_var_2(void) {
ecs_world_t *world = ecs_mini();

ECS_TAG(world, Rel);
ECS_TAG(world, Foo);

ecs_entity_t e1 = ecs_new(world);
ecs_entity_t e2 = ecs_new(world);
ecs_entity_t e3 = ecs_new(world);

ecs_entity_t tgt_a = ecs_new(world);
ecs_entity_t tgt_b = ecs_new(world);

ecs_add_pair(world, e1, Rel, tgt_a);
ecs_add_pair(world, e2, Rel, tgt_b);
ecs_add_pair(world, e3, Rel, tgt_b);
ecs_add(world, e3, Foo);

ecs_query_t *q = ecs_query(world, {
.expr = "Rel(_, $x)",
.cache_kind = cache_kind
});

test_assert(q != NULL);

int x_var = ecs_query_find_var(q, "x");
test_assert(x_var != -1);

ecs_iter_t it = ecs_query_iter(world, q);
test_bool(true, ecs_query_next(&it));
test_int(0, it.count);
test_uint(ecs_pair(Rel, tgt_b), ecs_field_id(&it, 0));
test_uint(tgt_b, ecs_iter_get_var(&it, x_var));

test_bool(true, ecs_query_next(&it));
test_int(0, it.count);
test_uint(ecs_pair(Rel, tgt_a), ecs_field_id(&it, 0));
test_uint(tgt_a, ecs_iter_get_var(&it, x_var));

test_bool(false, ecs_query_next(&it));

ecs_query_fini(q);

ecs_fini(world);
}

void Basic_1_any_src_w_pair_tgt_var_childof(void) {
ecs_world_t *world = ecs_mini();

ECS_TAG(world, Rel);
ECS_TAG(world, Foo);

ecs_entity_t e1 = ecs_new(world);
ecs_entity_t e2 = ecs_new(world);
ecs_entity_t e3 = ecs_new(world);

ecs_entity_t tgt_a = ecs_new(world);
ecs_entity_t tgt_b = ecs_new(world);

ecs_add_pair(world, e1, Rel, tgt_a);
ecs_add_pair(world, e2, Rel, tgt_b);
ecs_add_pair(world, e3, Rel, tgt_b);
ecs_add(world, e3, Foo);

ecs_query_t *q = ecs_query(world, {
.expr = "ChildOf(_, $x)",
.cache_kind = cache_kind
});

test_assert(q != NULL);

int x_var = ecs_query_find_var(q, "x");
test_assert(x_var != -1);

ecs_iter_t it = ecs_query_iter(world, q);
test_bool(true, ecs_query_next(&it));
test_int(0, it.count);
test_uint(ecs_pair(EcsChildOf, EcsFlecs), ecs_field_id(&it, 0));
test_uint(EcsFlecs, ecs_iter_get_var(&it, x_var));
test_uint(EcsWildcard, ecs_field_src(&it, 0));

test_bool(true, ecs_query_next(&it));
ecs_entity_t internals = ecs_lookup(world, "flecs.core.internals");
test_int(0, it.count);
test_uint(ecs_pair(EcsChildOf, internals), ecs_field_id(&it, 0));
test_uint(internals, ecs_iter_get_var(&it, x_var));
test_uint(EcsWildcard, ecs_field_src(&it, 0));

test_bool(true, ecs_query_next(&it));
test_int(0, it.count);
test_uint(ecs_pair(EcsChildOf, EcsFlecsCore), ecs_field_id(&it, 0));
test_uint(EcsFlecsCore, ecs_iter_get_var(&it, x_var));
test_uint(EcsWildcard, ecs_field_src(&it, 0));

test_bool(false, ecs_query_next(&it));

ecs_query_fini(q);

ecs_fini(world);
}

void Basic_1_any_src_w_pair_rel_var(void) {
ecs_world_t *world = ecs_mini();

Expand Down
1 change: 0 additions & 1 deletion test/query/src/Variables.c
Original file line number Diff line number Diff line change
Expand Up @@ -11560,4 +11560,3 @@ void Variables_second_invalid_var_name_and_id(void) {

ecs_fini(world);
}

12 changes: 11 additions & 1 deletion test/query/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ void Basic_1_any_src_w_pair(void);
void Basic_2_any_src(void);
void Basic_2_any_src_w_pair(void);
void Basic_1_any_src_w_pair_tgt_var(void);
void Basic_1_any_src_w_pair_tgt_var_2(void);
void Basic_1_any_src_w_pair_tgt_var_childof(void);
void Basic_1_any_src_w_pair_rel_var(void);
void Basic_1_any_src_w_pair_tgt_this(void);
void Basic_1_any_src_w_pair_rel_this(void);
Expand Down Expand Up @@ -4260,6 +4262,14 @@ bake_test_case Basic_testcases[] = {
"1_any_src_w_pair_tgt_var",
Basic_1_any_src_w_pair_tgt_var
},
{
"1_any_src_w_pair_tgt_var_2",
Basic_1_any_src_w_pair_tgt_var_2
},
{
"1_any_src_w_pair_tgt_var_childof",
Basic_1_any_src_w_pair_tgt_var_childof
},
{
"1_any_src_w_pair_rel_var",
Basic_1_any_src_w_pair_rel_var
Expand Down Expand Up @@ -10626,7 +10636,7 @@ static bake_test_suite suites[] = {
"Basic",
Basic_setup,
NULL,
234,
236,
Basic_testcases,
1,
Basic_params
Expand Down

0 comments on commit c57e7c3

Please sign in to comment.