Skip to content

Commit

Permalink
Fix issue with queries that match empty tables and iterate individual…
Browse files Browse the repository at this point in the history
… entities

Summary: This diff fixes an issue with queries that are created with the `EcsQueryMatchEmptyTables` flag, and iterate individual entities vs. entire tables.

Reviewed By: Jason-M-Fugate

Differential Revision: D63857210
  • Loading branch information
SanderMertens authored and facebook-github-bot committed Oct 3, 2024
1 parent 386acc5 commit 4591db1
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
3 changes: 3 additions & 0 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -69582,6 +69582,9 @@ bool flecs_query_each(
}

if (!redo) {
if (!ecs_table_count(table)) {
return false;
}
row = op_ctx->row = range.offset;
} else {
int32_t end = range.count;
Expand Down
3 changes: 3 additions & 0 deletions src/query/engine/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,9 @@ bool flecs_query_each(
}

if (!redo) {
if (!ecs_table_count(table)) {
return false;
}
row = op_ctx->row = range.offset;
} else {
int32_t end = range.count;
Expand Down
11 changes: 6 additions & 5 deletions test/query/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
"inout_component_implicit_subject",
"inout_component_explicit_subject",
"inout_pair_implicit_subject",
"inout_pair_explicit_subject",
"inout_pair_explicit_subject",
"out_component_implicit_subject",
"out_component_explicit_subject",
"out_pair_implicit_subject",
Expand Down Expand Up @@ -244,7 +244,7 @@
"from_or",
"from_not",
"pair_implicit_subject_optional",
"pair_explicit_subject_optional",
"pair_explicit_subject_optional",
"pred_implicit_subject_w_role",
"pred_explicit_subject_w_role",
"pred_no_subject_w_role",
Expand Down Expand Up @@ -288,9 +288,9 @@
"pred_implicit_subject_superset_inclusive",
"pred_implicit_subject_superset_cascade",
"pred_implicit_subject_superset_inclusive_cascade",
"pred_implicit_subject_implicit_superset_cascade",
"pred_implicit_subject_implicit_superset_cascade",
"pred_implicit_subject_implicit_superset_inclusive_cascade",
"pred_implicit_subject_implicit_superset_cascade_w_rel",
"pred_implicit_subject_implicit_superset_cascade_w_rel",
"pred_implicit_subject_implicit_superset_inclusive_cascade_w_rel",
"pred_implicit_subject_superset_childof",
"pred_implicit_subject_cascade_superset_childof",
Expand Down Expand Up @@ -691,7 +691,8 @@
"written_pair_tgt_any_record",
"written_pair_any_any_record",
"pair_rel_any_record_component",
"pair_tgt_any_record_component"
"pair_tgt_any_record_component",
"entity_iteration_w_match_empty_tables"
]
}, {
"id": "Combinations",
Expand Down
43 changes: 43 additions & 0 deletions test/query/src/Basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -11216,3 +11216,46 @@ void Basic_pair_tgt_any_record_component(void) {

ecs_fini(world);
}

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

ECS_COMPONENT(world, Position);

ecs_query_t *q = ecs_query(world, {
.expr = "Position($x)", // non-$this variable forces entity iteration
.cache_kind = cache_kind,
.flags = EcsQueryMatchEmptyTables
});

test_assert(q != NULL);

ecs_entity_t e = ecs_new(world);
ecs_set(world, e, Position, {10, 20});

{
ecs_iter_t it = ecs_query_iter(world, q);
test_bool(true, ecs_query_next(&it));
test_uint(0, it.count);
test_uint(e, ecs_field_src(&it, 0));
test_uint(ecs_id(Position), ecs_field_id(&it, 0));
{
const Position *p = ecs_field(&it, Position, 0);
test_assert(p != NULL);
test_int(p[0].x, 10); test_int(p[0].y, 20);
}

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

// delete entity, leave empty table

ecs_delete(world, e);

{
ecs_iter_t it = ecs_query_iter(world, q);
test_bool(false, ecs_query_next(&it));
}

ecs_fini(world);
}
7 changes: 6 additions & 1 deletion test/query/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ void Basic_written_pair_tgt_any_record(void);
void Basic_written_pair_any_any_record(void);
void Basic_pair_rel_any_record_component(void);
void Basic_pair_tgt_any_record_component(void);
void Basic_entity_iteration_w_match_empty_tables(void);

// Testsuite 'Combinations'
void Combinations_setup(void);
Expand Down Expand Up @@ -4777,6 +4778,10 @@ bake_test_case Basic_testcases[] = {
{
"pair_tgt_any_record_component",
Basic_pair_tgt_any_record_component
},
{
"entity_iteration_w_match_empty_tables",
Basic_entity_iteration_w_match_empty_tables
}
};

Expand Down Expand Up @@ -10407,7 +10412,7 @@ static bake_test_suite suites[] = {
"Basic",
Basic_setup,
NULL,
224,
225,
Basic_testcases,
1,
Basic_params
Expand Down

0 comments on commit 4591db1

Please sign in to comment.