@@ -11337,7 +11337,8 @@ bool appendstr(
1133711337 /* Update to number of characters copied to new buffer */
1133811338 b->current->pos += n;
1133911339 } else {
11340- char *remainder = ecs_os_strdup(str);
11340+ /* String doesn't fit in a single element, strdup */
11341+ char *remainder = ecs_os_strdup(str + memLeftInElement);
1134111342 ecs_strbuf_grow_str(b, remainder, remainder, n);
1134211343 }
1134311344 } else {
@@ -37049,17 +37050,41 @@ void rematch_table(
3704937050 resolve_cascade_subject(world, query, match, table, table->type);
3705037051
3705137052 /* If query has optional columns, it is possible that a column that
37052- * previously had data no longer has data, or vice versa. Do a full
37053+ * previously had data no longer has data, or vice versa. Do a
3705337054 * rematch to make sure data is consistent. */
3705437055 } else if (query->flags & EcsQueryHasOptional) {
37055- unmatch_table(query, table);
37056- if (!(query->flags & EcsQueryIsSubquery)) {
37057- flecs_table_notify(world, table, &(ecs_table_event_t){
37058- .kind = EcsTableQueryUnmatch,
37059- .query = query
37060- });
37056+ /* Check if optional terms that weren't matched before are matched
37057+ * now & vice versa */
37058+ ecs_query_table_match_t *qt = match->first;
37059+
37060+ bool rematch = false;
37061+ int32_t i, count = query->filter.term_count_actual;
37062+ for (i = 0; i < count; i ++) {
37063+ ecs_term_t *term = &query->filter.terms[i];
37064+
37065+ if (term->oper == EcsOptional) {
37066+ int32_t t = term->index;
37067+ int32_t column = 0;
37068+ flecs_term_match_table(world, term, table,
37069+ table->type, 0, &column, 0, 0, true);
37070+ if (column && (qt->columns[t] == 0)) {
37071+ rematch = true;
37072+ } else if (!column && (qt->columns[t] != 0)) {
37073+ rematch = true;
37074+ }
37075+ }
37076+ }
37077+
37078+ if (rematch) {
37079+ unmatch_table(query, table);
37080+ if (!(query->flags & EcsQueryIsSubquery)) {
37081+ flecs_table_notify(world, table, &(ecs_table_event_t){
37082+ .kind = EcsTableQueryUnmatch,
37083+ .query = query
37084+ });
37085+ }
37086+ add_table(world, query, table);
3706137087 }
37062- add_table(world, query, table);
3706337088 }
3706437089 } else {
3706537090 /* Table no longer matches, remove */
@@ -37120,7 +37145,7 @@ void rematch_tables(
3712037145 ecs_world_t *world,
3712137146 ecs_query_t *query,
3712237147 ecs_query_t *parent_query)
37123- {
37148+ {
3712437149 if (parent_query) {
3712537150 ecs_query_table_t *tables = ecs_vector_first(
3712637151 parent_query->cache.tables, ecs_query_table_t);
0 commit comments