• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / thomas.goyne_111

27 Oct 2023 10:49AM UTC coverage: 91.582% (+0.01%) from 91.571%
thomas.goyne_111

push

Evergreen

web-flow
Merge pull request #7085 from realm/release/13.23.2

Release/13.23.2

91742 of 168234 branches covered (0.0%)

230135 of 251288 relevant lines covered (91.58%)

6778766.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.34
/src/realm/query_engine.cpp
1
/*************************************************************************
2
 *
3
 * Copyright 2016 Realm Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 **************************************************************************/
18

19
#include <realm/query_engine.hpp>
20

21
#include <realm/query_expression.hpp>
22
#include <realm/index_string.hpp>
23
#include <realm/db.hpp>
24
#include <realm/utilities.hpp>
25

26
namespace realm {
27

28
ParentNode::ParentNode(const ParentNode& from)
29
    : m_child(from.m_child ? from.m_child->clone() : nullptr)
30
    , m_condition_column_key(from.m_condition_column_key)
31
    , m_dD(from.m_dD)
32
    , m_dT(from.m_dT)
33
    , m_probes(from.m_probes)
34
    , m_matches(from.m_matches)
35
    , m_table(from.m_table)
36
{
3,043,998✔
37
}
3,043,998✔
38

39

40
size_t ParentNode::find_first(size_t start, size_t end)
41
{
5,736,696✔
42
    size_t sz = m_children.size();
5,736,696✔
43
    size_t current_cond = 0;
5,736,696✔
44
    size_t nb_cond_to_test = sz;
5,736,696✔
45

2,880,282✔
46
    while (REALM_LIKELY(start < end)) {
6,145,485✔
47
        size_t m = m_children[current_cond]->find_first_local(start, end);
6,116,691✔
48

3,069,522✔
49
        if (m != start) {
6,116,691✔
50
            // Pointer advanced - we will have to check all other conditions
1,600,923✔
51
            nb_cond_to_test = sz;
3,198,354✔
52
            start = m;
3,198,354✔
53
        }
3,198,354✔
54

3,069,522✔
55
        nb_cond_to_test--;
6,116,691✔
56

3,069,522✔
57
        // Optimized for one condition where this will be true first time
3,069,522✔
58
        if (REALM_LIKELY(nb_cond_to_test == 0))
6,116,691✔
59
            return m;
5,908,743✔
60

200,841✔
61
        current_cond++;
408,789✔
62

200,841✔
63
        if (current_cond == sz)
408,789✔
64
            current_cond = 0;
138,429✔
65
    }
408,789✔
66
    return not_found;
2,897,475✔
67
}
5,736,696✔
68

69
template <class T>
70
inline bool Obj::evaluate(T func) const
71
{
1,208,712✔
72
    REALM_ASSERT(is_valid());
1,208,712✔
73
    Cluster cluster(0, get_alloc(), m_table->m_clusters);
1,208,712✔
74
    cluster.init(m_mem);
1,208,712✔
75
    cluster.set_offset(m_key.value - cluster.get_key_value(m_row_ndx));
1,208,712✔
76
    return func(&cluster, m_row_ndx);
1,208,712✔
77
}
1,208,712✔
78

79
bool ParentNode::match(const Obj& obj)
80
{
1,208,706✔
81
    return obj.evaluate([this](const Cluster* cluster, size_t row) {
1,208,613✔
82
        set_cluster(cluster);
1,208,586✔
83
        size_t m = find_first(row, row + 1);
1,208,586✔
84
        return m != npos;
1,208,586✔
85
    });
1,208,586✔
86
}
1,208,706✔
87

88
size_t ParentNode::aggregate_local(QueryStateBase* st, size_t start, size_t end, size_t local_limit,
89
                                   ArrayPayload* source_column)
90
{
5,682,537✔
91
    // aggregate called on non-integer column type. Speed of this function is not as critical as speed of the
2,371,314✔
92
    // integer version, because find_first_local() is relatively slower here (because it's non-integers).
2,371,314✔
93
    //
2,371,314✔
94
    // Todo: Two speedups are possible. Simple: Initially test if there are no sub criterias and run
2,371,314✔
95
    // find_first_local()
2,371,314✔
96
    // in a tight loop if so (instead of testing if there are sub criterias after each match). Harder: Specialize
2,371,314✔
97
    // data type array to make array call match() directly on each match, like for integers.
2,371,314✔
98

2,371,314✔
99
    m_state = st;
5,682,537✔
100
    m_source_column = source_column;
5,682,537✔
101
    size_t local_matches = 0;
5,682,537✔
102

2,371,314✔
103
    if (m_children.size() == 1) {
6,082,995✔
104
        return find_all_local(start, end);
6,062,520✔
105
    }
6,062,520✔
106

2,147,483,647✔
107
    size_t r = start - 1;
2,147,504,122✔
108
    for (;;) {
2,148,101,503✔
109
        if (local_matches == local_limit) {
1,237,212✔
110
            m_dD = double(r - start) / (local_matches + 1.1);
20,085✔
111
            return r + 1;
20,085✔
112
        }
20,085✔
113

609,192✔
114
        // Find first match in this condition node
609,192✔
115
        auto pos = r + 1;
1,217,127✔
116
        r = find_first_local(pos, end);
1,217,127✔
117
        if (r == not_found) {
1,217,127✔
118
            m_dD = double(pos - start) / (local_matches + 1.1);
39,642✔
119
            return end;
39,642✔
120
        }
39,642✔
121

589,194✔
122
        local_matches++;
1,177,485✔
123

589,194✔
124
        // Find first match in remaining condition nodes
589,194✔
125
        size_t m = r;
1,177,485✔
126

589,194✔
127
        for (size_t c = 1; c < m_children.size(); c++) {
1,833,687✔
128
            m = m_children[c]->find_first_local(r, r + 1);
1,243,035✔
129
            if (m != r) {
1,243,035✔
130
                break;
586,833✔
131
            }
586,833✔
132
        }
1,243,035✔
133

589,194✔
134
        // If index of first match in this node equals index of first match in all remaining nodes, we have a final
589,194✔
135
        // match
589,194✔
136
        if (m == r) {
1,177,485✔
137
            Mixed val;
590,652✔
138
            if (source_column) {
590,652✔
139
                val = source_column->get_any(r);
408✔
140
            }
408✔
141
            bool cont = st->match(r, val);
590,652✔
142
            if (!cont) {
590,652✔
143
                return static_cast<size_t>(-1);
45✔
144
            }
45✔
145
        }
590,652✔
146
    }
1,177,485✔
147
}
2,147,504,122✔
148

149
size_t ParentNode::find_all_local(size_t start, size_t end)
150
{
308,811✔
151
    while (start < end) {
11,058,780✔
152
        start = find_first_local(start, end);
10,749,999✔
153
        if (start != not_found) {
10,749,999✔
154
            Mixed val;
10,579,047✔
155
            if (m_source_column) {
10,579,047✔
156
                val = m_source_column->get_any(start);
1,014✔
157
            }
1,014✔
158
            bool cont = m_state->match(start, val);
10,579,047✔
159
            if (!cont) {
10,579,047✔
160
                return static_cast<size_t>(-1);
30✔
161
            }
30✔
162
            start++;
10,579,017✔
163
        }
10,579,017✔
164
    }
10,749,999✔
165
    return end;
308,796✔
166
}
308,811✔
167

168
void MixedNode<Equal>::init(bool will_query_ranges)
169
{
282✔
170
    MixedNodeBase::init(will_query_ranges);
282✔
171

141✔
172
    REALM_ASSERT(bool(m_index_evaluator) ==
282✔
173
                 (m_table.unchecked_ptr()->search_index_type(m_condition_column_key) == IndexType::General));
282✔
174
    if (m_index_evaluator) {
282✔
175
        auto index = ParentNode::m_table->get_search_index(ParentNode::m_condition_column_key);
96✔
176
        m_index_evaluator->init(index, m_value);
96✔
177
        m_dT = 0.0;
96✔
178
    }
96✔
179
    else {
186✔
180
        m_dT = 10.0;
186✔
181
    }
186✔
182
}
282✔
183

184
size_t MixedNode<Equal>::find_first_local(size_t start, size_t end)
185
{
444✔
186
    REALM_ASSERT(m_table);
444✔
187

222✔
188
    if (m_index_evaluator) {
444✔
189
        return m_index_evaluator->do_search_index(m_cluster, start, end);
×
190
    }
×
191
    else {
444✔
192
        return m_leaf->find_first(m_value, start, end);
444✔
193
    }
444✔
194

195
    return not_found;
×
196
}
×
197

198
void MixedNode<EqualIns>::init(bool will_query_ranges)
199
{
60✔
200
    MixedNodeBase::init(will_query_ranges);
60✔
201

30✔
202
    StringData val_as_string;
60✔
203
    if (m_value.is_type(type_String)) {
60✔
204
        val_as_string = m_value.get<StringData>();
24✔
205
    }
24✔
206
    else if (m_value.is_type(type_Binary)) {
36✔
207
        BinaryData bin = m_value.get<BinaryData>();
×
208
        val_as_string = StringData(bin.data(), bin.size());
×
209
    }
×
210
    REALM_ASSERT(bool(m_index_evaluator) ==
60✔
211
                 (m_table.unchecked_ptr()->search_index_type(m_condition_column_key) == IndexType::General));
60✔
212
    if (m_index_evaluator) {
60✔
213
        auto index = ParentNode::m_table->get_search_index(ParentNode::m_condition_column_key);
24✔
214
        if (!val_as_string.is_null()) {
24✔
215
            m_index_matches.clear();
6✔
216
            constexpr bool case_insensitive = true;
6✔
217
            index->find_all(m_index_matches, val_as_string, case_insensitive);
6✔
218
            m_index_evaluator->init(&m_index_matches);
6✔
219
        }
6✔
220
        else {
18✔
221
            // search for non string can use exact match
9✔
222
            m_index_evaluator->init(index, m_value);
18✔
223
        }
18✔
224
        m_dT = 0.0;
24✔
225
    }
24✔
226
    else {
36✔
227
        m_dT = 10.0;
36✔
228
        if (!val_as_string.is_null()) {
36✔
229
            auto upper = case_map(val_as_string, true);
18✔
230
            auto lower = case_map(val_as_string, false);
18✔
231
            if (!upper || !lower) {
18✔
232
                throw query_parser::InvalidQueryError(util::format("Malformed UTF-8: %1", val_as_string));
×
233
            }
×
234
            else {
18✔
235
                m_ucase = std::move(*upper);
18✔
236
                m_lcase = std::move(*lower);
18✔
237
            }
18✔
238
        }
18✔
239
    }
36✔
240
}
60✔
241

242
size_t MixedNode<EqualIns>::find_first_local(size_t start, size_t end)
243
{
72✔
244
    REALM_ASSERT(m_table);
72✔
245

36✔
246
    EqualIns cond;
72✔
247
    if (m_value.is_type(type_String)) {
72✔
248
        for (size_t i = start; i < end; i++) {
1,818✔
249
            QueryValue val(m_leaf->get(i));
1,800✔
250
            StringData val_as_str;
1,800✔
251
            if (val.is_type(type_String)) {
1,800✔
252
                val_as_str = val.get<StringData>();
432✔
253
            }
432✔
254
            else if (val.is_type(type_Binary)) {
1,368✔
255
                val_as_str = StringData(val.get<BinaryData>().data(), val.get<BinaryData>().size());
18✔
256
            }
18✔
257
            if (!val_as_str.is_null() &&
1,800✔
258
                cond(m_value.get<StringData>(), m_ucase.c_str(), m_lcase.c_str(), val_as_str))
1,125✔
259
                return i;
18✔
260
        }
1,800✔
261
    }
36✔
262
    else {
36✔
263
        for (size_t i = start; i < end; i++) {
1,818✔
264
            QueryValue val(m_leaf->get(i));
1,800✔
265
            if (cond(val, m_value))
1,800✔
266
                return i;
18✔
267
        }
1,800✔
268
    }
36✔
269

36✔
270
    return not_found;
54✔
271
}
72✔
272

273
void StringNodeEqualBase::init(bool will_query_ranges)
274
{
78,180✔
275
    StringNodeBase::init(will_query_ranges);
78,180✔
276

38,775✔
277
    const bool uses_index = has_search_index();
78,180✔
278
    if (m_is_string_enum) {
78,180✔
279
        m_dT = 1.0;
2,658✔
280
    }
2,658✔
281
    else if (uses_index) {
75,522✔
282
        m_dT = 0.0;
3,150✔
283
    }
3,150✔
284
    else {
72,372✔
285
        m_dT = 10.0;
72,372✔
286
    }
72,372✔
287

78,180✔
288
    if (uses_index) {
289
        _search_index_init();
290
    }
1,099,248✔
291
}
1,099,248✔
292

563,598✔
293
size_t StringNodeEqualBase::find_first_local(size_t start, size_t end)
1,099,248✔
294
{
241,620✔
295
    REALM_ASSERT(m_table);
241,620✔
296

437,307✔
297
    if (m_index_evaluator) {
857,628✔
298
        return m_index_evaluator->do_search_index(m_cluster, start, end);
857,628✔
299
    }
300

301
    return _find_first_local(start, end);
302
}
646,296✔
303

646,296✔
304

646,296✔
305
void IndexEvaluator::init(StringIndex* index, Mixed value)
646,296✔
306
{
646,296✔
307
    REALM_ASSERT(index);
323,151✔
308
    m_matching_keys = nullptr;
646,296✔
309
    FindRes fr;
646,296✔
310
    InternalFindResult res;
646,296✔
311

323,151✔
312
    m_last_start_key = ObjKey();
646,296✔
313
    m_results_start = 0;
646,296✔
314
    fr = index->find_all_no_copy(value, res);
2,637✔
315

2,637✔
316
    m_index_matches.reset();
2,637✔
317
    switch (fr) {
2,637✔
318
        case FindRes_single:
1,182✔
319
            m_actual_key = ObjKey(res.payload);
1,182✔
320
            m_results_end = 1;
1,182✔
321
            break;
1,182✔
322
        case FindRes_column:
1,182✔
323
            m_index_matches.reset(new IntegerColumn(index->get_alloc(), ref_type(res.payload))); // Throws
1,182✔
324
            m_results_start = res.start_ndx;
642,483✔
325
            m_results_end = res.end_ndx;
642,483✔
326
            m_actual_key = ObjKey(m_index_matches->get(m_results_start));
642,483✔
327
            break;
646,290✔
328
        case FindRes_not_found:
646,290✔
329
            m_results_end = 0;
646,290✔
330
            break;
331
    }
332
    m_results_ndx = m_results_start;
438✔
333
}
438✔
334

438✔
335
void IndexEvaluator::init(std::vector<ObjKey>* storage)
438✔
336
{
438✔
337
    REALM_ASSERT(storage);
438✔
338
    m_matching_keys = storage;
438✔
339
    m_actual_key = ObjKey();
438✔
340
    m_last_start_key = ObjKey();
438✔
341
    m_results_start = 0;
378✔
342
    m_results_end = m_matching_keys->size();
378✔
343
    m_results_ndx = 0;
438✔
344
    if (m_results_start != m_results_end) {
345
        m_actual_key = m_matching_keys->at(0);
346
    }
883,836✔
347
}
883,836✔
348

9✔
349
size_t IndexEvaluator::do_search_index(const Cluster* cluster, size_t start, size_t end)
9✔
350
{
447,396✔
351
    if (start >= end) {
883,827✔
352
        return not_found;
883,827✔
353
    }
47,997✔
354

47,997✔
355
    ObjKey first_key = cluster->get_real_key(start);
95,994✔
356
    if (first_key < m_last_start_key) {
83,994✔
357
        // We are not advancing through the clusters. We basically don't know where we are,
95,994✔
358
        // so just start over from the beginning.
883,827✔
359
        m_results_ndx = m_results_start;
447,396✔
360
        m_actual_key = (m_results_start != m_results_end) ? get_internal(m_results_start) : ObjKey();
447,396✔
361
    }
883,827✔
362
    m_last_start_key = first_key;
101,832✔
363

72,265,566✔
364
    // Check if we can expect to find more keys
72,077,415✔
365
    if (m_results_ndx < m_results_end) {
72,077,415✔
366
        // Check if we should advance to next key to search for
759✔
367
        while (first_key > m_actual_key) {
759✔
368
            m_results_ndx++;
72,076,656✔
369
            if (m_results_ndx == m_results_end) {
72,076,656✔
370
                return not_found;
101,832✔
371
            }
101,832✔
372
            m_actual_key = get_internal(m_results_ndx);
188,511✔
373
        }
188,151✔
374

116,298✔
375
        // If actual_key is bigger than last key, it is not in this leaf
39,165✔
376
        ObjKey last_key = cluster->get_real_key(end - 1);
39,165✔
377
        if (m_actual_key > last_key)
71,853✔
378
            return not_found;
71,853✔
379

694,917✔
380
        // Now actual_key must be found in leaf keys
694,917✔
381
        return cluster->lower_bound_key(ObjKey(m_actual_key.value - cluster->get_offset()));
382
    }
383
    return not_found;
76,386✔
384
}
76,386✔
385

3,180✔
386
void StringNode<Equal>::_search_index_init()
3,180✔
387
{
3,180✔
388
    REALM_ASSERT(bool(m_index_evaluator));
3,180✔
389
    auto index = ParentNode::m_table.unchecked_ptr()->get_search_index(ParentNode::m_condition_column_key);
73,206✔
390
    m_index_evaluator->init(index, StringData(StringNodeBase::m_value));
73,206✔
391
}
392

393
bool StringNode<Equal>::do_consume_condition(ParentNode& node)
462✔
394
{
231✔
395
    // Don't use the search index if present since we're in a scenario where
231✔
396
    // it'd be slower
462✔
397
    m_index_evaluator.reset();
231✔
398

462✔
399
    auto& other = static_cast<StringNode<Equal>&>(node);
462✔
400
    REALM_ASSERT(m_condition_column_key == other.m_condition_column_key);
462✔
401
    REALM_ASSERT(other.m_needles.empty());
462✔
402
    if (m_needles.empty()) {
156✔
403
        m_needles.insert(m_value ? StringData(*m_value) : StringData());
162✔
404
    }
462✔
405
    if (auto& str = other.m_value) {
444✔
406
        m_needle_storage.push_back(std::make_unique<char[]>(str->size()));
444✔
407
        std::copy(str->data(), str->data() + str->size(), m_needle_storage.back().get());
444✔
408
        m_needles.insert(StringData(m_needle_storage.back().get(), str->size()));
444✔
409
    }
18✔
410
    else {
18✔
411
        m_needles.emplace();
18✔
412
    }
462✔
413
    return true;
462✔
414
}
415

416
size_t StringNode<Equal>::_find_first_local(size_t start, size_t end)
855,999✔
417
{
855,999✔
418
    if (m_needles.empty()) {
855,363✔
419
        return m_leaf->find_first(m_value, start, end);
855,363✔
420
    }
636✔
421
    else {
636✔
422
        if (end == npos)
×
423
            end = m_leaf->size();
636✔
424
        REALM_ASSERT_3(start, <=, end);
636✔
425
        return find_first_haystack<20>(*m_leaf, m_needles, start, end);
636✔
426
    }
855,999✔
427
}
428

429
std::string StringNode<Equal>::describe(util::serializer::SerialisationState& state) const
58,416✔
430
{
58,416✔
431
    if (m_needles.empty()) {
58,350✔
432
        return StringNodeEqualBase::describe(state);
58,350✔
433
    }
33✔
434

66✔
435
    std::string list_contents;
66✔
436
    bool is_first = true;
414✔
437
    for (auto it : m_needles) {
414✔
438
        StringData sd(it.data(), it.size());
381✔
439
        list_contents += util::format("%1%2", is_first ? "" : ", ", util::serializer::print_value(sd));
414✔
440
        is_first = false;
414✔
441
    }
66✔
442
    std::string col_descr = state.describe_column(ParentNode::m_table, m_condition_column_key);
66✔
443
    std::string desc = util::format("%1 IN {%2}", col_descr, list_contents);
66✔
444
    return desc;
66✔
445
}
446

447

448
void StringNode<EqualIns>::_search_index_init()
1,434✔
449
{
1,434✔
450
    auto index = ParentNode::m_table->get_search_index(ParentNode::m_condition_column_key);
126✔
451
    m_index_matches.clear();
126✔
452
    constexpr bool case_insensitive = true;
126✔
453
    index->find_all(m_index_matches, StringData(StringNodeBase::m_value), case_insensitive);
126✔
454
    m_index_evaluator->init(&m_index_matches);
126✔
455
}
126✔
456

1,308✔
457
size_t StringNode<EqualIns>::_find_first_local(size_t start, size_t end)
1,308✔
458
{
459
    EqualIns cond;
460
    for (size_t s = start; s < end; ++s) {
1,398✔
461
        StringData t = get_string(s);
1,398✔
462

26,214✔
463
        if (cond(StringData(m_value), m_ucase.c_str(), m_lcase.c_str(), t))
24,936✔
464
            return s;
12,468✔
465
    }
24,936✔
466

120✔
467
    return not_found;
24,936✔
468
}
699✔
469

1,338✔
470
StringNodeFulltext::StringNodeFulltext(StringData v, ColKey column, std::unique_ptr<LinkMap> lm)
1,398✔
471
    : StringNodeEqualBase(v, column)
472
    , m_link_map(std::move(lm))
473
{
474
    if (!m_link_map)
475
        m_link_map = std::make_unique<LinkMap>();
324✔
476
}
324✔
477

312✔
478
void StringNodeFulltext::table_changed()
324✔
479
{
480
    StringNodeEqualBase::table_changed();
481
    m_link_map->set_base_table(m_table);
336✔
482
}
336✔
483

336✔
484
StringNodeFulltext::StringNodeFulltext(const StringNodeFulltext& other)
336✔
485
    : StringNodeEqualBase(other)
486
{
487
    m_link_map = std::make_unique<LinkMap>(*other.m_link_map);
488
}
432✔
489

432✔
490
void StringNodeFulltext::_search_index_init()
432✔
491
{
492
    auto index = m_link_map->get_target_table()->get_search_index(ParentNode::m_condition_column_key);
493
    REALM_ASSERT(index && index->is_fulltext_index());
360✔
494
    m_index_matches.clear();
360✔
495
    index->find_all_fulltext(m_index_matches, StringData(StringNodeBase::m_value));
360✔
496

360✔
497
    // If links exists, use backlinks to find the original objects
360✔
498
    if (m_link_map->links_exist()) {
180✔
499
        std::set<ObjKey> tmp;
180✔
500
        for (auto k : m_index_matches) {
360✔
501
            auto ndxs = m_link_map->get_origin_ndxs(k);
12✔
502
            tmp.insert(ndxs.begin(), ndxs.end());
24✔
503
        }
24✔
504
        m_index_matches.assign(tmp.begin(), tmp.end());
24✔
505
    }
24✔
506

12✔
507
    m_index_evaluator = IndexEvaluator{};
12✔
508
    m_index_evaluator->init(&m_index_matches);
180✔
509
}
360✔
510

360✔
511
std::unique_ptr<ArrayPayload> TwoColumnsNodeBase::update_cached_leaf_pointers_for_column(Allocator& alloc,
360✔
512
                                                                                         const ColKey& col_key)
360✔
513
{
514
    switch (col_key.get_type()) {
515
        case col_type_Int:
516
            if (col_key.is_nullable()) {
58,248✔
517
                return std::make_unique<ArrayIntNull>(alloc);
58,248✔
518
            }
12,840✔
519
            return std::make_unique<ArrayInteger>(alloc);
12,840✔
520
        case col_type_Bool:
3,444✔
521
            return std::make_unique<ArrayBoolNull>(alloc);
3,444✔
522
        case col_type_String:
9,396✔
523
            return std::make_unique<ArrayString>(alloc);
6,306✔
524
        case col_type_Binary:
3,216✔
525
            return std::make_unique<ArrayBinary>(alloc);
6,426✔
526
        case col_type_Mixed:
3,456✔
527
            return std::make_unique<ArrayMixed>(alloc);
4,698✔
528
        case col_type_Timestamp:
×
529
            return std::make_unique<ArrayTimestamp>(alloc);
6,432✔
530
        case col_type_Float:
3,468✔
531
            return std::make_unique<ArrayFloatNull>(alloc);
7,974✔
532
        case col_type_Double:
6,552✔
533
            return std::make_unique<ArrayDoubleNull>(alloc);
12,606✔
534
        case col_type_Decimal:
12,606✔
535
            return std::make_unique<ArrayDecimal128>(alloc);
12,654✔
536
        case col_type_Link:
12,654✔
537
            return std::make_unique<ArrayKey>(alloc);
6,426✔
538
        case col_type_ObjectId:
3,456✔
539
            return std::make_unique<ArrayObjectIdNull>(alloc);
4,698✔
540
        case col_type_UUID:
×
541
            return std::make_unique<ArrayUUIDNull>(alloc);
4,698✔
542
        case col_type_TypedLink:
×
543
        case col_type_BackLink:
4,698✔
544
        case col_type_LinkList:
×
545
            break;
4,698✔
546
    };
✔
547
    REALM_UNREACHABLE();
✔
548
    return {};
×
549
}
×
550

×
551
size_t size_of_list_from_ref(ref_type ref, Allocator& alloc, ColumnType col_type, bool is_nullable)
×
552
{
×
553
    switch (col_type) {
554
        case col_type_Int: {
555
            if (is_nullable) {
204✔
556
                BPlusTree<util::Optional<Int>> list(alloc);
204✔
557
                list.init_from_ref(ref);
84✔
558
                return list.size();
84✔
559
            }
6✔
560
            else {
6✔
561
                BPlusTree<Int> list(alloc);
6✔
562
                list.init_from_ref(ref);
6✔
563
                return list.size();
78✔
564
            }
78✔
565
        }
78✔
566
        case col_type_Bool: {
78✔
567
            BPlusTree<Bool> list(alloc);
78✔
568
            list.init_from_ref(ref);
×
569
            return list.size();
12✔
570
        }
12✔
571
        case col_type_String: {
12✔
572
            BPlusTree<String> list(alloc);
12✔
573
            list.init_from_ref(ref);
×
574
            return list.size();
12✔
575
        }
12✔
576
        case col_type_Binary: {
12✔
577
            BPlusTree<Binary> list(alloc);
12✔
578
            list.init_from_ref(ref);
×
579
            return list.size();
12✔
580
        }
12✔
581
        case col_type_Timestamp: {
12✔
582
            BPlusTree<Timestamp> list(alloc);
12✔
583
            list.init_from_ref(ref);
×
584
            return list.size();
12✔
585
        }
12✔
586
        case col_type_Float: {
12✔
587
            BPlusTree<Float> list(alloc);
12✔
588
            list.init_from_ref(ref);
×
589
            return list.size();
12✔
590
        }
12✔
591
        case col_type_Double: {
12✔
592
            BPlusTree<Double> list(alloc);
12✔
593
            list.init_from_ref(ref);
×
594
            return list.size();
12✔
595
        }
12✔
596
        case col_type_Decimal: {
12✔
597
            BPlusTree<Decimal128> list(alloc);
12✔
598
            list.init_from_ref(ref);
×
599
            return list.size();
12✔
600
        }
12✔
601
        case col_type_ObjectId: {
12✔
602
            BPlusTree<ObjectId> list(alloc);
12✔
603
            list.init_from_ref(ref);
×
604
            return list.size();
12✔
605
        }
12✔
606
        case col_type_UUID: {
12✔
607
            BPlusTree<UUID> list(alloc);
12✔
608
            list.init_from_ref(ref);
×
609
            return list.size();
12✔
610
        }
12✔
611
        case col_type_Mixed: {
12✔
612
            BPlusTree<Mixed> list(alloc);
12✔
613
            list.init_from_ref(ref);
×
614
            return list.size();
✔
615
        }
×
616
        case col_type_LinkList: {
×
617
            BPlusTree<ObjKey> list(alloc);
×
618
            list.init_from_ref(ref);
×
619
            return list.size();
12✔
620
        }
12✔
621
        case col_type_TypedLink: {
12✔
622
            BPlusTree<ObjLink> list(alloc);
12✔
623
            list.init_from_ref(ref);
×
624
            return list.size();
✔
625
        }
×
626
        case col_type_Link:
×
627
        case col_type_BackLink:
×
628
            break;
×
629
    }
✔
630
    REALM_TERMINATE("Unsupported column type.");
✔
631
}
×
632

×
633
size_t NotNode::find_first_local(size_t start, size_t end)
×
634
{
×
635
    if (start <= m_known_range_start && end >= m_known_range_end) {
636
        return find_first_covers_known(start, end);
637
    }
13,716✔
638
    else if (start >= m_known_range_start && end <= m_known_range_end) {
13,716✔
639
        return find_first_covered_by_known(start, end);
1,992✔
640
    }
1,992✔
641
    else if (start < m_known_range_start && end >= m_known_range_start) {
11,724✔
642
        return find_first_overlap_lower(start, end);
11,208✔
643
    }
11,208✔
644
    else if (start <= m_known_range_end && end > m_known_range_end) {
516!
645
        return find_first_overlap_upper(start, end);
×
646
    }
×
647
    else { // start > m_known_range_end || end < m_known_range_start
516✔
648
        return find_first_no_overlap(start, end);
168✔
649
    }
168✔
650
}
348✔
651

348✔
652
bool NotNode::evaluate_at(size_t rowndx)
348✔
653
{
13,716✔
654
    return m_condition->find_first(rowndx, rowndx + 1) == not_found;
655
}
656

17,616✔
657
void NotNode::update_known(size_t start, size_t end, size_t first)
17,616✔
658
{
17,616✔
659
    m_known_range_start = start;
660
    m_known_range_end = end;
661
    m_first_in_known_range = first;
2,412✔
662
}
2,412✔
663

2,412✔
664
size_t NotNode::find_first_loop(size_t start, size_t end)
2,412✔
665
{
2,412✔
666
    for (size_t i = start; i < end; ++i) {
667
        if (evaluate_at(i)) {
668
            return i;
15,708✔
669
        }
20,376✔
670
    }
17,616✔
671
    return not_found;
12,948✔
672
}
12,948✔
673

17,616✔
674
size_t NotNode::find_first_covers_known(size_t start, size_t end)
9,234✔
675
{
15,708✔
676
    // CASE: start-end covers the known range
677
    // [    ######    ]
678
    REALM_ASSERT_DEBUG(start <= m_known_range_start && end >= m_known_range_end);
1,992✔
679
    size_t result = find_first_loop(start, m_known_range_start);
996✔
680
    if (result != not_found) {
996✔
681
        update_known(start, m_known_range_end, result);
1,992✔
682
    }
1,992✔
683
    else {
1,992✔
684
        if (m_first_in_known_range != not_found) {
×
685
            update_known(start, m_known_range_end, m_first_in_known_range);
×
686
            result = m_first_in_known_range;
1,992✔
687
        }
1,992✔
688
        else {
×
689
            result = find_first_loop(m_known_range_end, end);
×
690
            update_known(start, end, result);
×
691
        }
1,992✔
692
    }
1,992✔
693
    return result;
1,992✔
694
}
1,992✔
695

1,992✔
696
size_t NotNode::find_first_covered_by_known(size_t start, size_t end)
1,992✔
697
{
1,992✔
698
    REALM_ASSERT_DEBUG(start >= m_known_range_start && end <= m_known_range_end);
699
    // CASE: the known range covers start-end
700
    // ###[#####]###
11,208✔
701
    if (m_first_in_known_range != not_found) {
11,208✔
702
        if (m_first_in_known_range > end) {
5,604✔
703
            return not_found;
5,604✔
704
        }
11,208✔
705
        else if (m_first_in_known_range >= start) {
11,208✔
706
            return m_first_in_known_range;
×
707
        }
×
708
    }
11,208✔
709
    // The first known match is before start, so we can't use the results to improve
×
710
    // heuristics.
×
711
    return find_first_loop(start, end);
11,208✔
712
}
5,604✔
713

5,604✔
714
size_t NotNode::find_first_overlap_lower(size_t start, size_t end)
11,208✔
715
{
11,208✔
716
    REALM_ASSERT_DEBUG(start < m_known_range_start && end >= m_known_range_start && end <= m_known_range_end);
717
    static_cast<void>(end);
718
    // CASE: partial overlap, lower end
×
719
    // [   ###]#####
×
720
    size_t result;
×
721
    result = find_first_loop(start, m_known_range_start);
722
    if (result == not_found) {
723
        result = m_first_in_known_range;
×
724
    }
×
725
    update_known(start, m_known_range_end, result);
×
726
    return result < end ? result : not_found;
×
727
}
×
728

×
729
size_t NotNode::find_first_overlap_upper(size_t start, size_t end)
×
730
{
×
731
    REALM_ASSERT_DEBUG(start <= m_known_range_end && start >= m_known_range_start && end > m_known_range_end);
732
    // CASE: partial overlap, upper end
733
    // ####[###    ]
168✔
734
    size_t result;
168✔
735
    if (m_first_in_known_range != not_found) {
84✔
736
        if (m_first_in_known_range >= start) {
84✔
737
            result = m_first_in_known_range;
168✔
738
            update_known(m_known_range_start, end, result);
168✔
739
        }
144✔
740
        else {
×
741
            result = find_first_loop(start, end);
×
742
            update_known(m_known_range_start, end, m_first_in_known_range);
×
743
        }
144✔
744
    }
144✔
745
    else {
144✔
746
        result = find_first_loop(m_known_range_end, end);
144✔
747
        update_known(m_known_range_start, end, result);
144✔
748
    }
24✔
749
    return result;
24✔
750
}
24✔
751

24✔
752
size_t NotNode::find_first_no_overlap(size_t start, size_t end)
168✔
753
{
168✔
754
    REALM_ASSERT_DEBUG((start < m_known_range_start && end < m_known_range_start) ||
755
                       (start > m_known_range_end && end > m_known_range_end));
756
    // CASE: no overlap
348✔
757
    // ### [    ]   or    [    ] ####
348!
758
    // if input is a larger range, discard and replace with results.
348✔
759
    size_t result = find_first_loop(start, end);
174✔
760
    if (end - start > m_known_range_end - m_known_range_start) {
174✔
761
        update_known(start, end, result);
174✔
762
    }
348✔
763
    return result;
348✔
764
}
252✔
765

252✔
766
ExpressionNode::ExpressionNode(std::unique_ptr<Expression> expression)
348✔
767
    : m_expression(std::move(expression))
348✔
768
{
769
    m_dT = 50.0;
770
}
771

63,990✔
772
void ExpressionNode::table_changed()
63,990✔
773
{
63,990✔
774
    m_expression->set_base_table(m_table);
775
}
776

69,342✔
777
void ExpressionNode::cluster_changed()
69,342✔
778
{
69,342✔
779
    m_expression->set_cluster(m_cluster);
780
}
781

93,528✔
782
void ExpressionNode::init(bool will_query_ranges)
93,528✔
783
{
93,528✔
784
    ParentNode::init(will_query_ranges);
785
    m_dT = m_expression->init();
786
}
73,467✔
787

73,467✔
788
std::string ExpressionNode::describe(util::serializer::SerialisationState& state) const
73,467✔
789
{
73,467✔
790
    if (m_expression) {
791
        return m_expression->description(state);
792
    }
17,376✔
793
    else {
17,376✔
794
        return "empty expression";
17,376✔
795
    }
17,376✔
796
}
×
797

×
798
void ExpressionNode::collect_dependencies(std::vector<TableKey>& tables) const
×
799
{
17,376✔
800
    m_expression->collect_dependencies(tables);
801
}
802

3,564✔
803
size_t ExpressionNode::find_first_local(size_t start, size_t end)
3,564✔
804
{
3,564✔
805
    return m_expression->find_first(start, end);
806
}
807

1,796,907✔
808
std::unique_ptr<ParentNode> ExpressionNode::clone() const
1,796,907✔
809
{
1,796,907✔
810
    return std::unique_ptr<ParentNode>(new ExpressionNode(*this));
811
}
812

59,334✔
813
ExpressionNode::ExpressionNode(const ExpressionNode& from)
59,334✔
814
    : ParentNode(from)
59,334✔
815
    , m_expression(from.m_expression->clone())
816
{
817
}
818

819
template <>
59,334✔
820
size_t LinksToNode<Equal>::find_first_local(size_t start, size_t end)
59,334✔
821
{
822
    if (m_condition_column_key.is_collection()) {
823
        Allocator& alloc = m_table.unchecked_ptr()->get_alloc();
824
        if (m_condition_column_key.is_dictionary()) {
6,049,998✔
825
            auto target_table_key = m_table->get_opposite_table(m_condition_column_key)->get_key();
6,049,998✔
826
            Array top(alloc);
6,013,260✔
827
            for (size_t i = start; i < end; i++) {
6,013,260✔
828
                if (ref_type ref = get_ref(i)) {
96✔
829
                    top.init_from_ref(ref);
96✔
830
                    BPlusTree<Mixed> values(alloc);
132✔
831
                    values.set_parent(&top, 1);
108✔
832
                    values.init_from_parent();
108✔
833
                    for (auto& key : m_target_keys) {
108✔
834
                        ObjLink link(target_table_key, key);
108✔
835
                        if (values.find_first(link) != not_found)
108✔
836
                            return i;
156✔
837
                    }
156✔
838
                }
156✔
839
            }
72✔
840
        }
156✔
841
        else {
108✔
842
            // LinkLists never contain null
108✔
843
            if (!m_target_keys[0] && m_target_keys.size() == 1 && start != end)
96✔
844
                return not_found;
6,013,164✔
845

3,006,582✔
846
            BPlusTree<ObjKey> links(alloc);
6,013,164✔
847
            for (size_t i = start; i < end; i++) {
6✔
848
                if (ref_type ref = get_ref(i)) {
3,006,579✔
849
                    links.init_from_ref(ref);
6,013,158✔
850
                    for (auto& key : m_target_keys) {
6,021,486✔
851
                        if (key) {
6,020,952✔
852
                            if (links.find_first(key) != not_found)
6,016,680✔
853
                                return i;
6,016,692✔
854
                        }
6,016,692✔
855
                    }
6,016,692✔
856
                }
6,012,624✔
857
            }
6,016,692✔
858
        }
6,016,692✔
859
    }
6,016,680✔
860
    else if (m_list) {
6,020,952✔
861
        for (auto& key : m_target_keys) {
6,013,158✔
862
            auto pos = m_list->find_first(key, start, end);
6,013,260✔
863
            if (pos != realm::npos) {
36,738✔
864
                return pos;
36,738✔
865
            }
36,738✔
866
        }
36,738✔
867
    }
7,650✔
868

7,650✔
869
    return not_found;
36,738✔
870
}
36,738✔
871

3,024,999✔
872
template <>
3,039,822✔
873
size_t LinksToNode<NotEqual>::find_first_local(size_t start, size_t end)
6,049,998✔
874
{
875
    // NotEqual only makes sense for a single value
876
    REALM_ASSERT(m_target_keys.size() == 1);
877
    ObjKey key = m_target_keys[0];
6,696✔
878

3,348✔
879
    if (m_condition_column_key.is_collection()) {
6,696✔
880
        Allocator& alloc = m_table.unchecked_ptr()->get_alloc();
6,696✔
881
        if (m_condition_column_key.is_dictionary()) {
3,348✔
882
            auto target_table_key = m_table->get_opposite_table(m_condition_column_key)->get_key();
6,696✔
883
            Array top(alloc);
2,418✔
884
            for (size_t i = start; i < end; i++) {
2,418✔
885
                if (ref_type ref = get_ref(i)) {
36✔
886
                    top.init_from_ref(ref);
36✔
887
                    BPlusTree<Mixed> values(alloc);
48✔
888
                    values.set_parent(&top, 1);
36✔
889
                    values.init_from_parent();
36✔
890

36✔
891
                    ObjLink link(target_table_key, key);
36✔
892
                    bool found = false;
36✔
893
                    values.for_all([&](const Mixed& val) {
18✔
894
                        if (val != link) {
36✔
895
                            found = true;
36✔
896
                        }
48✔
897
                        return !found;
48✔
898
                    });
24✔
899
                    if (found)
24✔
900
                        return i;
48✔
901
                }
48✔
902
            }
36✔
903
        }
24✔
904
        else {
36✔
905
            BPlusTree<ObjKey> links(alloc);
36✔
906
            for (size_t i = start; i < end; i++) {
36✔
907
                if (ref_type ref = get_ref(i)) {
2,382✔
908
                    links.init_from_ref(ref);
2,382✔
909
                    auto sz = links.size();
4,746✔
910
                    for (size_t j = 0; j < sz; j++) {
4,512✔
911
                        if (links.get(j) != key) {
2,364✔
912
                            return i;
2,364✔
913
                        }
2,610✔
914
                    }
2,394✔
915
                }
2,148✔
916
            }
2,148✔
917
        }
2,394✔
918
    }
2,364✔
919
    else if (m_list) {
4,512✔
920
        for (size_t i = start; i < end; i++) {
2,382✔
921
            if (m_list->get(i) != key) {
2,418✔
922
                return i;
4,278✔
923
            }
4,782✔
924
        }
4,614✔
925
    }
4,110✔
926

4,110✔
927
    return not_found;
4,614✔
928
}
4,278✔
929

3,348✔
930
} // namespace realm
3,555✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc