Skip to content

Commit bb65f68

Browse files
committed
#1171 Fix issue with OR/wildcard queries
1 parent dedec1e commit bb65f68

File tree

6 files changed

+92
-5
lines changed

6 files changed

+92
-5
lines changed

flecs.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12435,6 +12435,7 @@ bool flecs_filter_match_table(
1243512435
if (first && match_index) {
1243612436
match_count *= match_index;
1243712437
}
12438+
1243812439
if (match_indices) {
1243912440
match_indices[t_i] = match_index;
1244012441
}
@@ -13295,10 +13296,16 @@ bool ecs_filter_next_instanced(
1329513296
}
1329613297

1329713298
/* Match the remainder of the terms */
13299+
int32_t skip_term = pivot_term;
13300+
if (ecs_id_is_wildcard(filter->terms[pivot_term].id)) {
13301+
skip_term = -1;
13302+
iter->matches_left = 1;
13303+
}
13304+
1329813305
match = flecs_filter_match_table(world, filter, table,
1329913306
it->ids, it->columns, it->sources,
1330013307
it->match_indices, &iter->matches_left, first,
13301-
pivot_term, it->flags);
13308+
skip_term, it->flags);
1330213309
if (!match) {
1330313310
it->table = table;
1330413311
iter->matches_left = 0;
@@ -13343,8 +13350,19 @@ bool ecs_filter_next_instanced(
1334313350
column = -column;
1334413351
}
1334513352

13353+
int32_t t, term_count = filter->term_count;
13354+
ecs_term_t *term = NULL;
13355+
for (t = 0; t < term_count; t ++) {
13356+
if (filter->terms[t].field_index == i) {
13357+
term = &filter->terms[t];
13358+
break;
13359+
}
13360+
}
13361+
13362+
ecs_assert(term != NULL, ECS_INTERNAL_ERROR, NULL);
13363+
1334613364
it->columns[i] = column + 1;
13347-
flecs_term_match_table(world, &filter->terms[i], table,
13365+
flecs_term_match_table(world, term, table,
1334813366
&it->ids[i], &it->columns[i], &it->sources[i],
1334913367
&it->match_indices[i], false, it->flags);
1335013368

src/filter.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,6 +2385,7 @@ bool flecs_filter_match_table(
23852385
if (first && match_index) {
23862386
match_count *= match_index;
23872387
}
2388+
23882389
if (match_indices) {
23892390
match_indices[t_i] = match_index;
23902391
}
@@ -3245,10 +3246,16 @@ bool ecs_filter_next_instanced(
32453246
}
32463247

32473248
/* Match the remainder of the terms */
3249+
int32_t skip_term = pivot_term;
3250+
if (ecs_id_is_wildcard(filter->terms[pivot_term].id)) {
3251+
skip_term = -1;
3252+
iter->matches_left = 1;
3253+
}
3254+
32483255
match = flecs_filter_match_table(world, filter, table,
32493256
it->ids, it->columns, it->sources,
32503257
it->match_indices, &iter->matches_left, first,
3251-
pivot_term, it->flags);
3258+
skip_term, it->flags);
32523259
if (!match) {
32533260
it->table = table;
32543261
iter->matches_left = 0;
@@ -3293,8 +3300,19 @@ bool ecs_filter_next_instanced(
32933300
column = -column;
32943301
}
32953302

3303+
int32_t t, term_count = filter->term_count;
3304+
ecs_term_t *term = NULL;
3305+
for (t = 0; t < term_count; t ++) {
3306+
if (filter->terms[t].field_index == i) {
3307+
term = &filter->terms[t];
3308+
break;
3309+
}
3310+
}
3311+
3312+
ecs_assert(term != NULL, ECS_INTERNAL_ERROR, NULL);
3313+
32963314
it->columns[i] = column + 1;
3297-
flecs_term_match_table(world, &filter->terms[i], table,
3315+
flecs_term_match_table(world, term, table,
32983316
&it->ids[i], &it->columns[i], &it->sources[i],
32993317
&it->match_indices[i], false, it->flags);
33003318

test/addons/src/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9141,6 +9141,7 @@ bake_test_case Alerts_testcases[] = {
91419141
}
91429142
};
91439143

9144+
91449145
static bake_test_suite suites[] = {
91459146
{
91469147
"Parser",

test/api/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,7 @@
13101310
"filter_iter_3_or",
13111311
"filter_iter_2_or_other_type",
13121312
"filter_iter_2_or_same_type",
1313+
"filter_or_w_wildcard",
13131314
"filter_iter_1_component",
13141315
"filter_iter_2_components",
13151316
"filter_iter_pair_id",

test/api/src/Filter.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4643,6 +4643,50 @@ void Filter_filter_iter_2_or_same_type(void) {
46434643
ecs_fini(world);
46444644
}
46454645

4646+
void Filter_filter_or_w_wildcard(void) {
4647+
ecs_world_t *world = ecs_mini();
4648+
4649+
ECS_TAG(world, Rel);
4650+
ECS_TAG(world, TgtA);
4651+
ECS_TAG(world, TgtB);
4652+
ECS_TAG(world, TagA);
4653+
ECS_TAG(world, TagB);
4654+
4655+
ecs_filter_t *q = ecs_filter(world, {
4656+
.terms = {
4657+
{ TagA, .oper = EcsOr },
4658+
{ TagB },
4659+
{ ecs_pair(Rel, EcsWildcard) }
4660+
}
4661+
});
4662+
4663+
test_assert(q != NULL);
4664+
4665+
ecs_entity_t e = ecs_new_id(world);
4666+
ecs_add(world, e, TagA);
4667+
ecs_add(world, e, TagB);
4668+
ecs_add_pair(world, e, Rel, TgtA);
4669+
ecs_add_pair(world, e, Rel, TgtB);
4670+
4671+
ecs_iter_t it = ecs_filter_iter(world, q);
4672+
test_bool(true, ecs_filter_next(&it));
4673+
test_int(1, it.count);
4674+
test_uint(e, it.entities[0]);
4675+
test_uint(TagA, ecs_field_id(&it, 1));
4676+
test_uint(ecs_pair(Rel, TgtA), ecs_field_id(&it, 2));
4677+
4678+
test_bool(true, ecs_filter_next(&it));
4679+
test_int(1, it.count);
4680+
test_uint(e, it.entities[0]);
4681+
test_uint(TagA, ecs_field_id(&it, 1));
4682+
test_uint(ecs_pair(Rel, TgtB), ecs_field_id(&it, 2));
4683+
test_bool(false, ecs_filter_next(&it));
4684+
4685+
ecs_filter_fini(q);
4686+
4687+
ecs_fini(world);
4688+
}
4689+
46464690
void Filter_filter_iter_2_or(void) {
46474691
ecs_world_t *world = ecs_mini();
46484692

test/api/src/main.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,7 @@ void Filter_filter_iter_2_or(void);
12511251
void Filter_filter_iter_3_or(void);
12521252
void Filter_filter_iter_2_or_other_type(void);
12531253
void Filter_filter_iter_2_or_same_type(void);
1254+
void Filter_filter_or_w_wildcard(void);
12541255
void Filter_filter_iter_1_component(void);
12551256
void Filter_filter_iter_2_components(void);
12561257
void Filter_filter_iter_pair_id(void);
@@ -7566,6 +7567,10 @@ bake_test_case Filter_testcases[] = {
75667567
"filter_iter_2_or_same_type",
75677568
Filter_filter_iter_2_or_same_type
75687569
},
7570+
{
7571+
"filter_or_w_wildcard",
7572+
Filter_filter_or_w_wildcard
7573+
},
75697574
{
75707575
"filter_iter_1_component",
75717576
Filter_filter_iter_1_component
@@ -13648,7 +13653,7 @@ static bake_test_suite suites[] = {
1364813653
"Filter",
1364913654
NULL,
1365013655
NULL,
13651-
304,
13656+
305,
1365213657
Filter_testcases
1365313658
},
1365413659
{

0 commit comments

Comments
 (0)