Skip to content

Commit

Permalink
Don't insert query populate instructions for terms with #0 source
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Jul 16, 2024
1 parent a5ca669 commit b0a4fe6
Show file tree
Hide file tree
Showing 6 changed files with 373 additions and 6 deletions.
4 changes: 3 additions & 1 deletion flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -34164,7 +34164,9 @@ int flecs_query_finalize_terms(
}
}

if (term->inout == EcsInOutNone) {
if (term->src.id == EcsIsEntity) {
nodata_term = true;
} else if (term->inout == EcsInOutNone) {
nodata_term = true;
} else if (!ecs_get_type_info(world, term->id)) {
nodata_term = true;
Expand Down
4 changes: 3 additions & 1 deletion src/query/validator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,9 @@ int flecs_query_finalize_terms(
}
}

if (term->inout == EcsInOutNone) {
if (term->src.id == EcsIsEntity) {
nodata_term = true;
} else if (term->inout == EcsInOutNone) {
nodata_term = true;
} else if (!ecs_get_type_info(world, term->id)) {
nodata_term = true;
Expand Down
14 changes: 12 additions & 2 deletions test/query/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,15 @@
"pair_first_wildcard",
"pair_first_wildcard_cached",
"pair_second_wildcard",
"pair_second_wildcard_cached"
"pair_second_wildcard_cached",
"0_src_tag",
"0_src_component",
"0_src_w_sparse",
"0_src_w_toggle",
"0_src_w_union",
"0_src_w_sparse_and_component",
"0_src_w_toggle_and_component",
"0_src_w_union_and_component"
]
}, {
"id": "Variables",
Expand Down Expand Up @@ -1903,7 +1911,9 @@
"1_sparse_self_up",
"1_sparse_written_self",
"1_sparse_written_up",
"1_sparse_written_self_up"
"1_sparse_written_self_up",
"sparse_0_src_only_term",
"sparse_0_src"
]
}, {
"id": "Union",
Expand Down
245 changes: 245 additions & 0 deletions test/query/src/Plan.c
Original file line number Diff line number Diff line change
Expand Up @@ -2317,3 +2317,248 @@ void Plan_pair_second_wildcard_cached(void) {

ecs_fini(world);
}

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

ECS_TAG(world, Foo);

ecs_query_t *q = ecs_query(world, {
.expr = "Foo(#0)"
});

test_assert(q != NULL);

ecs_log_enable_colors(false);

const char *expect =
HEAD " 0. [-1, 1] setfix "
LINE " 1. [ 0, 2] setids "
LINE " 2. [ 1, 3] yield "
LINE "";
char *plan = ecs_query_plan(q);

test_str(expect, plan);
ecs_os_free(plan);

ecs_query_fini(q);

ecs_fini(world);
}

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

ECS_COMPONENT(world, Position);

ecs_query_t *q = ecs_query(world, {
.expr = "Position(#0)"
});

test_assert(q != NULL);

ecs_log_enable_colors(false);

const char *expect =
HEAD " 0. [-1, 1] setfix "
LINE " 1. [ 0, 2] setids "
LINE " 2. [ 1, 3] yield "
LINE "";
char *plan = ecs_query_plan(q);

test_str(expect, plan);
ecs_os_free(plan);

ecs_query_fini(q);

ecs_fini(world);
}

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

ECS_COMPONENT(world, Position);

ecs_add_id(world, ecs_id(Position), EcsSparse);

ecs_query_t *q = ecs_query(world, {
.expr = "Position(#0)"
});

test_assert(q != NULL);

ecs_log_enable_colors(false);

const char *expect =
HEAD " 0. [-1, 1] setfix "
LINE " 1. [ 0, 2] setids "
LINE " 2. [ 1, 3] yield "
LINE "";
char *plan = ecs_query_plan(q);

test_str(expect, plan);
ecs_os_free(plan);

ecs_query_fini(q);

ecs_fini(world);
}

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

ECS_COMPONENT(world, Position);

ecs_add_id(world, ecs_id(Position), EcsCanToggle);

ecs_query_t *q = ecs_query(world, {
.expr = "Position(#0)"
});

test_assert(q != NULL);

ecs_log_enable_colors(false);

const char *expect =
HEAD " 0. [-1, 1] setfix "
LINE " 1. [ 0, 2] setids "
LINE " 2. [ 1, 3] yield "
LINE "";
char *plan = ecs_query_plan(q);

test_str(expect, plan);
ecs_os_free(plan);

ecs_query_fini(q);

ecs_fini(world);
}

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

ECS_TAG(world, Movement);

ecs_add_id(world, ecs_id(Movement), EcsUnion);

ecs_query_t *q = ecs_query(world, {
.expr = "Movement(#0, *)"
});

test_assert(q != NULL);

ecs_log_enable_colors(false);

const char *expect =
HEAD " 0. [-1, 1] setfix "
LINE " 1. [ 0, 2] setids "
LINE " 2. [ 1, 3] yield "
LINE "";
char *plan = ecs_query_plan(q);

test_str(expect, plan);
ecs_os_free(plan);

ecs_query_fini(q);

ecs_fini(world);
}

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

ECS_COMPONENT(world, Position);
ECS_COMPONENT(world, Velocity);

ecs_add_id(world, ecs_id(Position), EcsSparse);

ecs_query_t *q = ecs_query(world, {
.expr = "Position(#0), Velocity"
});

test_assert(q != NULL);

ecs_log_enable_colors(false);

const char *expect =
HEAD " 0. [-1, 1] setfix "
LINE " 1. [ 0, 2] setids "
LINE " 2. [ 1, 3] andid $[this] (Velocity)"
LINE " 3. [ 2, 4] popself {1}"
LINE " 4. [ 3, 5] yield "
LINE "";
char *plan = ecs_query_plan(q);

test_str(expect, plan);
ecs_os_free(plan);

ecs_query_fini(q);

ecs_fini(world);
}

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

ECS_COMPONENT(world, Position);
ECS_COMPONENT(world, Velocity);

ecs_add_id(world, ecs_id(Position), EcsCanToggle);

ecs_query_t *q = ecs_query(world, {
.expr = "Position(#0), Velocity"
});

test_assert(q != NULL);

ecs_log_enable_colors(false);

const char *expect =
HEAD " 0. [-1, 1] setfix "
LINE " 1. [ 0, 2] setids "
LINE " 2. [ 1, 3] andid $[this] (Velocity)"
LINE " 3. [ 2, 4] popself {1}"
LINE " 4. [ 3, 5] yield "
LINE "";
char *plan = ecs_query_plan(q);

test_str(expect, plan);
ecs_os_free(plan);

ecs_query_fini(q);

ecs_fini(world);
}

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

ECS_TAG(world, Movement);
ECS_COMPONENT(world, Velocity);

ecs_add_id(world, ecs_id(Movement), EcsUnion);

ecs_query_t *q = ecs_query(world, {
.expr = "Movement(#0, *), Velocity"
});

test_assert(q != NULL);

ecs_log_enable_colors(false);

const char *expect =
HEAD " 0. [-1, 1] setfix "
LINE " 1. [ 0, 2] setids "
LINE " 2. [ 1, 3] andid $[this] (Velocity)"
LINE " 3. [ 2, 4] popself {1}"
LINE " 4. [ 3, 5] yield "
LINE "";
char *plan = ecs_query_plan(q);

test_str(expect, plan);
ecs_os_free(plan);

ecs_query_fini(q);

ecs_fini(world);
}
58 changes: 58 additions & 0 deletions test/query/src/Sparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -1007,3 +1007,61 @@ void Sparse_1_sparse_written_self_up(void) {

ecs_fini(world);
}

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

ECS_COMPONENT(world, Position);

ecs_add_id(world, ecs_id(Position), EcsSparse);

ecs_query_t *q = ecs_query(world, {
.expr = "Position(#0)",
.cache_kind = cache_kind
});

test_assert(q != NULL);

ecs_iter_t it = ecs_query_iter(world, q);
test_bool(true, ecs_query_next(&it));
test_int(0, it.count);
test_uint(ecs_id(Position), ecs_field_id(&it, 0));
test_bool(false, ecs_field_is_set(&it, 0));
test_bool(false, ecs_query_next(&it));

ecs_query_fini(q);

ecs_fini(world);
}

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

ECS_COMPONENT(world, Position);
ECS_TAG(world, Foo);

ecs_add_id(world, ecs_id(Position), EcsSparse);

ecs_entity_t e = ecs_new_w(world, Foo);

ecs_query_t *q = ecs_query(world, {
.expr = "Position(#0), Foo",
.cache_kind = cache_kind
});

test_assert(q != NULL);

ecs_iter_t it = ecs_query_iter(world, q);
test_bool(true, ecs_query_next(&it));
test_int(1, it.count);
test_uint(e, it.entities[0]);
test_uint(ecs_id(Position), ecs_field_id(&it, 0));
test_uint(Foo, ecs_field_id(&it, 1));
test_bool(false, ecs_field_is_set(&it, 0));
test_bool(true, ecs_field_is_set(&it, 1));
test_bool(false, ecs_query_next(&it));

ecs_query_fini(q);

ecs_fini(world);
}
Loading

0 comments on commit b0a4fe6

Please sign in to comment.