Skip to content

Commit

Permalink
#1389 Fix issue with evaluating any queries that match multiple tables
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens authored Oct 4, 2024
1 parent bf45a45 commit 78924e1
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 9 deletions.
6 changes: 2 additions & 4 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -69204,10 +69204,8 @@ bool flecs_query_and_any(

ecs_query_and_ctx_t *op_ctx = flecs_op_ctx(ctx, and);

if (!redo) {
if (match_flags & EcsTermMatchAny && op_ctx->remaining) {
op_ctx->remaining = flecs_ito(int16_t, remaining);
}
if (match_flags & EcsTermMatchAny && op_ctx->remaining) {
op_ctx->remaining = flecs_ito(int16_t, remaining);
}

int32_t field = op->field_index;
Expand Down
6 changes: 2 additions & 4 deletions src/query/engine/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,8 @@ bool flecs_query_and_any(

ecs_query_and_ctx_t *op_ctx = flecs_op_ctx(ctx, and);

if (!redo) {
if (match_flags & EcsTermMatchAny && op_ctx->remaining) {
op_ctx->remaining = flecs_ito(int16_t, remaining);
}
if (match_flags & EcsTermMatchAny && op_ctx->remaining) {
op_ctx->remaining = flecs_ito(int16_t, remaining);
}

int32_t field = op->field_index;
Expand Down
2 changes: 2 additions & 0 deletions test/query/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@
"this_src_w_any_written",
"this_src_w_pair_rel_any",
"this_src_w_pair_tgt_any",
"this_src_w_pair_tgt_any_n_tgts",
"this_src_w_pair_tgt_any_n_tgts_written",
"this_src_w_pair_rel_tgt_any",
"ent_src_w_wildcard",
"ent_src_w_pair_rel_wildcard",
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 @@ -2516,6 +2516,107 @@ void Basic_this_src_w_pair_tgt_any(void) {
ecs_fini(world);
}

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

ECS_TAG(world, RelA);
ECS_TAG(world, TgtA);
ECS_TAG(world, TgtB);
ECS_TAG(world, TgtC);

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

ecs_add_pair(world, e2, RelA, e1);
ecs_add_pair(world, e1, RelA, e2);
ecs_add_pair(world, e1, RelA, e3);

ecs_query_t *r = ecs_query(world, {
.expr = "RelA($this, _)",
.cache_kind = cache_kind
});

test_assert(r != NULL);

{
ecs_iter_t it = ecs_query_iter(world, r);
test_bool(true, ecs_query_next(&it));
test_uint(1, it.count);
test_uint(e2, it.entities[0]);
test_uint(ecs_pair(RelA, EcsWildcard), ecs_field_id(&it, 0));
test_uint(0, ecs_field_src(&it, 0));
test_bool(true, ecs_field_is_set(&it, 0));

test_bool(true, ecs_query_next(&it));
test_uint(1, it.count);
test_uint(e1, it.entities[0]);
test_uint(ecs_pair(RelA, EcsWildcard), ecs_field_id(&it, 0));
test_uint(0, ecs_field_src(&it, 0));
test_bool(true, ecs_field_is_set(&it, 0));

test_bool(false, ecs_query_next(&it));
}

ecs_query_fini(r);

ecs_fini(world);
}

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

ECS_TAG(world, Foo);
ECS_TAG(world, RelA);
ECS_TAG(world, TgtA);
ECS_TAG(world, TgtB);
ECS_TAG(world, TgtC);

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

ecs_add(world, e1, Foo);
ecs_add(world, e2, Foo);
ecs_add(world, e3, Foo);

ecs_add_pair(world, e2, RelA, e1);
ecs_add_pair(world, e1, RelA, e2);
ecs_add_pair(world, e1, RelA, e3);

ecs_query_t *r = ecs_query(world, {
.expr = "Foo, RelA($this, _)",
.cache_kind = cache_kind
});

test_assert(r != NULL);

{
ecs_iter_t it = ecs_query_iter(world, r);
test_bool(true, ecs_query_next(&it));
test_uint(1, it.count);
test_uint(e2, it.entities[0]);
test_uint(Foo, ecs_field_id(&it, 0));
test_uint(ecs_pair(RelA, EcsWildcard), ecs_field_id(&it, 1));
test_uint(0, ecs_field_src(&it, 0));
test_bool(true, ecs_field_is_set(&it, 0));

test_bool(true, ecs_query_next(&it));
test_uint(1, it.count);
test_uint(e1, it.entities[0]);
test_uint(Foo, ecs_field_id(&it, 0));
test_uint(ecs_pair(RelA, EcsWildcard), ecs_field_id(&it, 1));
test_uint(0, ecs_field_src(&it, 0));
test_bool(true, ecs_field_is_set(&it, 0));

test_bool(false, ecs_query_next(&it));
}

ecs_query_fini(r);

ecs_fini(world);
}

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

Expand Down
12 changes: 11 additions & 1 deletion test/query/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,8 @@ void Basic_this_src_w_any(void);
void Basic_this_src_w_any_written(void);
void Basic_this_src_w_pair_rel_any(void);
void Basic_this_src_w_pair_tgt_any(void);
void Basic_this_src_w_pair_tgt_any_n_tgts(void);
void Basic_this_src_w_pair_tgt_any_n_tgts_written(void);
void Basic_this_src_w_pair_rel_tgt_any(void);
void Basic_ent_src_w_wildcard(void);
void Basic_ent_src_w_pair_rel_wildcard(void);
Expand Down Expand Up @@ -4091,6 +4093,14 @@ bake_test_case Basic_testcases[] = {
"this_src_w_pair_tgt_any",
Basic_this_src_w_pair_tgt_any
},
{
"this_src_w_pair_tgt_any_n_tgts",
Basic_this_src_w_pair_tgt_any_n_tgts
},
{
"this_src_w_pair_tgt_any_n_tgts_written",
Basic_this_src_w_pair_tgt_any_n_tgts_written
},
{
"this_src_w_pair_rel_tgt_any",
Basic_this_src_w_pair_rel_tgt_any
Expand Down Expand Up @@ -10432,7 +10442,7 @@ static bake_test_suite suites[] = {
"Basic",
Basic_setup,
NULL,
229,
231,
Basic_testcases,
1,
Basic_params
Expand Down

0 comments on commit 78924e1

Please sign in to comment.