Skip to content

Commit

Permalink
Fix issue with query traversal that could cause duplicate results
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed May 4, 2024
1 parent cf1a8ef commit 7b8369c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
2 changes: 2 additions & 0 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -38410,6 +38410,8 @@ bool flecs_query_up_select(
continue;
}

op_ctx->row = row;

bool match_empty = (q->flags & EcsQueryMatchEmptyTables) != 0;
down = op_ctx->down = flecs_query_get_down_cache(ctx, &op_ctx->cache,
op_ctx->trav, entity, op_ctx->idr_with, self, match_empty);
Expand Down
2 changes: 2 additions & 0 deletions src/query/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,8 @@ bool flecs_query_up_select(
continue;
}

op_ctx->row = row;

bool match_empty = (q->flags & EcsQueryMatchEmptyTables) != 0;
down = op_ctx->down = flecs_query_get_down_cache(ctx, &op_ctx->cache,
op_ctx->trav, entity, op_ctx->idr_with, self, match_empty);
Expand Down
1 change: 1 addition & 0 deletions test/query/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,7 @@
"this_self_up_childof_pair_for_var_written_n_targets",
"this_written_self_up_childof_pair_for_var_written_n_targets",
"self_up_2_levels",
"self_up_mixed_traversable",
"not_up_disabled",
"up_2_rel_instances",
"up_2_rel_instances_match_2nd",
Expand Down
37 changes: 37 additions & 0 deletions test/query/src/Traversal.c
Original file line number Diff line number Diff line change
Expand Up @@ -7329,6 +7329,43 @@ void Traversal_self_up_2_levels(void) {
ecs_fini(world);
}

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

ECS_TAG(world, TagA);

ecs_entity_t p1 = ecs_new(world);
ecs_entity_t p2 = ecs_new(world);
ecs_entity_t c = ecs_new_w_pair(world, EcsChildOf, p2);

ecs_add(world, p1, TagA);
ecs_add(world, p2, TagA);

ecs_query_t *q = ecs_query(world, {
.expr = "TagA(self|up ChildOf)"
});

test_assert(q != NULL);

ecs_iter_t it = ecs_query_iter(world, q);
test_bool(true, ecs_query_next(&it));
test_int(2, it.count);
test_uint(p1, it.entities[0]);
test_uint(p2, it.entities[1]);
test_uint(0, ecs_field_src(&it, 0));

test_bool(true, ecs_query_next(&it));
test_int(1, it.count);
test_uint(c, it.entities[0]);
test_uint(p2, ecs_field_src(&it, 0));

test_bool(false, ecs_query_next(&it));

ecs_query_fini(q);

ecs_fini(world);
}

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

Expand Down
7 changes: 6 additions & 1 deletion test/query/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,7 @@ void Traversal_this_written_self_up_childof_pair_for_var_written(void);
void Traversal_this_self_up_childof_pair_for_var_written_n_targets(void);
void Traversal_this_written_self_up_childof_pair_for_var_written_n_targets(void);
void Traversal_self_up_2_levels(void);
void Traversal_self_up_mixed_traversable(void);
void Traversal_not_up_disabled(void);
void Traversal_up_2_rel_instances(void);
void Traversal_up_2_rel_instances_match_2nd(void);
Expand Down Expand Up @@ -6778,6 +6779,10 @@ bake_test_case Traversal_testcases[] = {
"self_up_2_levels",
Traversal_self_up_2_levels
},
{
"self_up_mixed_traversable",
Traversal_self_up_mixed_traversable
},
{
"not_up_disabled",
Traversal_not_up_disabled
Expand Down Expand Up @@ -8958,7 +8963,7 @@ static bake_test_suite suites[] = {
"Traversal",
Traversal_setup,
NULL,
98,
99,
Traversal_testcases,
1,
Traversal_params
Expand Down

0 comments on commit 7b8369c

Please sign in to comment.