Skip to content

Commit

Permalink
#1171 Fix issue with OR/wildcard queries
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Apr 23, 2024
1 parent dedec1e commit bb65f68
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 5 deletions.
22 changes: 20 additions & 2 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -12435,6 +12435,7 @@ bool flecs_filter_match_table(
if (first && match_index) {
match_count *= match_index;
}

if (match_indices) {
match_indices[t_i] = match_index;
}
Expand Down Expand Up @@ -13295,10 +13296,16 @@ bool ecs_filter_next_instanced(
}

/* Match the remainder of the terms */
int32_t skip_term = pivot_term;
if (ecs_id_is_wildcard(filter->terms[pivot_term].id)) {
skip_term = -1;
iter->matches_left = 1;
}

match = flecs_filter_match_table(world, filter, table,
it->ids, it->columns, it->sources,
it->match_indices, &iter->matches_left, first,
pivot_term, it->flags);
skip_term, it->flags);
if (!match) {
it->table = table;
iter->matches_left = 0;
Expand Down Expand Up @@ -13343,8 +13350,19 @@ bool ecs_filter_next_instanced(
column = -column;
}

int32_t t, term_count = filter->term_count;
ecs_term_t *term = NULL;
for (t = 0; t < term_count; t ++) {
if (filter->terms[t].field_index == i) {
term = &filter->terms[t];
break;
}
}

ecs_assert(term != NULL, ECS_INTERNAL_ERROR, NULL);

it->columns[i] = column + 1;
flecs_term_match_table(world, &filter->terms[i], table,
flecs_term_match_table(world, term, table,
&it->ids[i], &it->columns[i], &it->sources[i],
&it->match_indices[i], false, it->flags);

Expand Down
22 changes: 20 additions & 2 deletions src/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -2385,6 +2385,7 @@ bool flecs_filter_match_table(
if (first && match_index) {
match_count *= match_index;
}

if (match_indices) {
match_indices[t_i] = match_index;
}
Expand Down Expand Up @@ -3245,10 +3246,16 @@ bool ecs_filter_next_instanced(
}

/* Match the remainder of the terms */
int32_t skip_term = pivot_term;
if (ecs_id_is_wildcard(filter->terms[pivot_term].id)) {
skip_term = -1;
iter->matches_left = 1;
}

match = flecs_filter_match_table(world, filter, table,
it->ids, it->columns, it->sources,
it->match_indices, &iter->matches_left, first,
pivot_term, it->flags);
skip_term, it->flags);
if (!match) {
it->table = table;
iter->matches_left = 0;
Expand Down Expand Up @@ -3293,8 +3300,19 @@ bool ecs_filter_next_instanced(
column = -column;
}

int32_t t, term_count = filter->term_count;
ecs_term_t *term = NULL;
for (t = 0; t < term_count; t ++) {
if (filter->terms[t].field_index == i) {
term = &filter->terms[t];
break;
}
}

ecs_assert(term != NULL, ECS_INTERNAL_ERROR, NULL);

it->columns[i] = column + 1;
flecs_term_match_table(world, &filter->terms[i], table,
flecs_term_match_table(world, term, table,
&it->ids[i], &it->columns[i], &it->sources[i],
&it->match_indices[i], false, it->flags);

Expand Down
1 change: 1 addition & 0 deletions test/addons/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9141,6 +9141,7 @@ bake_test_case Alerts_testcases[] = {
}
};


static bake_test_suite suites[] = {
{
"Parser",
Expand Down
1 change: 1 addition & 0 deletions test/api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,7 @@
"filter_iter_3_or",
"filter_iter_2_or_other_type",
"filter_iter_2_or_same_type",
"filter_or_w_wildcard",
"filter_iter_1_component",
"filter_iter_2_components",
"filter_iter_pair_id",
Expand Down
44 changes: 44 additions & 0 deletions test/api/src/Filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -4643,6 +4643,50 @@ void Filter_filter_iter_2_or_same_type(void) {
ecs_fini(world);
}

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

ECS_TAG(world, Rel);
ECS_TAG(world, TgtA);
ECS_TAG(world, TgtB);
ECS_TAG(world, TagA);
ECS_TAG(world, TagB);

ecs_filter_t *q = ecs_filter(world, {
.terms = {
{ TagA, .oper = EcsOr },
{ TagB },
{ ecs_pair(Rel, EcsWildcard) }
}
});

test_assert(q != NULL);

ecs_entity_t e = ecs_new_id(world);
ecs_add(world, e, TagA);
ecs_add(world, e, TagB);
ecs_add_pair(world, e, Rel, TgtA);
ecs_add_pair(world, e, Rel, TgtB);

ecs_iter_t it = ecs_filter_iter(world, q);
test_bool(true, ecs_filter_next(&it));
test_int(1, it.count);
test_uint(e, it.entities[0]);
test_uint(TagA, ecs_field_id(&it, 1));
test_uint(ecs_pair(Rel, TgtA), ecs_field_id(&it, 2));

test_bool(true, ecs_filter_next(&it));
test_int(1, it.count);
test_uint(e, it.entities[0]);
test_uint(TagA, ecs_field_id(&it, 1));
test_uint(ecs_pair(Rel, TgtB), ecs_field_id(&it, 2));
test_bool(false, ecs_filter_next(&it));

ecs_filter_fini(q);

ecs_fini(world);
}

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

Expand Down
7 changes: 6 additions & 1 deletion test/api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,7 @@ void Filter_filter_iter_2_or(void);
void Filter_filter_iter_3_or(void);
void Filter_filter_iter_2_or_other_type(void);
void Filter_filter_iter_2_or_same_type(void);
void Filter_filter_or_w_wildcard(void);
void Filter_filter_iter_1_component(void);
void Filter_filter_iter_2_components(void);
void Filter_filter_iter_pair_id(void);
Expand Down Expand Up @@ -7566,6 +7567,10 @@ bake_test_case Filter_testcases[] = {
"filter_iter_2_or_same_type",
Filter_filter_iter_2_or_same_type
},
{
"filter_or_w_wildcard",
Filter_filter_or_w_wildcard
},
{
"filter_iter_1_component",
Filter_filter_iter_1_component
Expand Down Expand Up @@ -13648,7 +13653,7 @@ static bake_test_suite suites[] = {
"Filter",
NULL,
NULL,
304,
305,
Filter_testcases
},
{
Expand Down

0 comments on commit bb65f68

Please sign in to comment.