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

realm / realm-core / thomas.goyne_114

27 Oct 2023 10:49AM UTC coverage: 91.595% (+0.02%) from 91.571%
thomas.goyne_114

push

Evergreen

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

Release/13.23.2

91732 of 168218 branches covered (0.0%)

230210 of 251336 relevant lines covered (91.59%)

6770777.37 hits per line

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

90.5
/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,040,149✔
37
}
3,040,149✔
38

39

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

2,406,882✔
46
    while (REALM_LIKELY(start < end)) {
4,805,595✔
47
        size_t m = m_children[current_cond]->find_first_local(start, end);
4,775,124✔
48

2,563,122✔
49
        if (m != start) {
4,775,124✔
50
            // Pointer advanced - we will have to check all other conditions
1,311,351✔
51
            nb_cond_to_test = sz;
2,624,721✔
52
            start = m;
2,624,721✔
53
        }
2,624,721✔
54

2,563,122✔
55
        nb_cond_to_test--;
4,775,124✔
56

2,563,122✔
57
        // Optimized for one condition where this will be true first time
2,563,122✔
58
        if (REALM_LIKELY(nb_cond_to_test == 0))
4,775,124✔
59
            return m;
4,595,526✔
60

170,955✔
61
        current_cond++;
350,553✔
62

170,955✔
63
        if (current_cond == sz)
350,553✔
64
            current_cond = 0;
117,531✔
65
    }
350,553✔
66
    return not_found;
2,422,638✔
67
}
4,455,042✔
68

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

79
bool ParentNode::match(const Obj& obj)
80
{
1,208,691✔
81
    return obj.evaluate([this](const Cluster* cluster, size_t row) {
1,208,655✔
82
        set_cluster(cluster);
1,208,655✔
83
        size_t m = find_first(row, row + 1);
1,208,655✔
84
        return m != npos;
1,208,655✔
85
    });
1,208,655✔
86
}
1,208,691✔
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,799,288✔
91
    // aggregate called on non-integer column type. Speed of this function is not as critical as speed of the
2,458,743✔
92
    // integer version, because find_first_local() is relatively slower here (because it's non-integers).
2,458,743✔
93
    //
2,458,743✔
94
    // Todo: Two speedups are possible. Simple: Initially test if there are no sub criterias and run
2,458,743✔
95
    // find_first_local()
2,458,743✔
96
    // in a tight loop if so (instead of testing if there are sub criterias after each match). Harder: Specialize
2,458,743✔
97
    // data type array to make array call match() directly on each match, like for integers.
2,458,743✔
98

2,458,743✔
99
    m_state = st;
5,799,288✔
100
    m_source_column = source_column;
5,799,288✔
101
    size_t local_matches = 0;
5,799,288✔
102

2,458,743✔
103
    if (m_children.size() == 1) {
5,971,068✔
104
        return find_all_local(start, end);
5,943,120✔
105
    }
5,943,120✔
106

2,147,483,647✔
107
    size_t r = start - 1;
2,147,511,595✔
108
    for (;;) {
2,147,956,450✔
109
        if (local_matches == local_limit) {
1,024,419✔
110
            m_dD = double(r - start) / (local_matches + 1.1);
15,858✔
111
            return r + 1;
15,858✔
112
        }
15,858✔
113

542,571✔
114
        // Find first match in this condition node
542,571✔
115
        auto pos = r + 1;
1,008,561✔
116
        r = find_first_local(pos, end);
1,008,561✔
117
        if (r == not_found) {
1,008,561✔
118
            m_dD = double(pos - start) / (local_matches + 1.1);
37,761✔
119
            return end;
37,761✔
120
        }
37,761✔
121

523,296✔
122
        local_matches++;
970,800✔
123

523,296✔
124
        // Find first match in remaining condition nodes
523,296✔
125
        size_t m = r;
970,800✔
126

523,296✔
127
        for (size_t c = 1; c < m_children.size(); c++) {
1,504,338✔
128
            m = m_children[c]->find_first_local(r, r + 1);
1,036,461✔
129
            if (m != r) {
1,036,461✔
130
                break;
502,923✔
131
            }
502,923✔
132
        }
1,036,461✔
133

523,296✔
134
        // If index of first match in this node equals index of first match in all remaining nodes, we have a final
523,296✔
135
        // match
523,296✔
136
        if (m == r) {
970,800✔
137
            Mixed val;
467,994✔
138
            if (source_column) {
467,994✔
139
                val = source_column->get_any(r);
408✔
140
            }
408✔
141
            bool cont = st->match(r, val);
467,994✔
142
            if (!cont) {
467,994✔
143
                return static_cast<size_t>(-1);
39✔
144
            }
39✔
145
        }
467,994✔
146
    }
970,800✔
147
}
2,147,511,595✔
148

149
size_t ParentNode::find_all_local(size_t start, size_t end)
150
{
305,742✔
151
    while (start < end) {
10,570,839✔
152
        start = find_first_local(start, end);
10,265,127✔
153
        if (start != not_found) {
10,265,127✔
154
            Mixed val;
10,093,080✔
155
            if (m_source_column) {
10,093,080✔
156
                val = m_source_column->get_any(start);
1,014✔
157
            }
1,014✔
158
            bool cont = m_state->match(start, val);
10,093,080✔
159
            if (!cont) {
10,093,080✔
160
                return static_cast<size_t>(-1);
30✔
161
            }
30✔
162
            start++;
10,093,050✔
163
        }
10,093,050✔
164
    }
10,265,127✔
165
    return end;
305,727✔
166
}
305,742✔
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
{
77,856✔
275
    StringNodeBase::init(will_query_ranges);
77,856✔
276

38,613✔
277
    const bool uses_index = has_search_index();
77,856✔
278
    if (m_is_string_enum) {
77,856✔
279
        m_dT = 1.0;
2,658✔
280
    }
2,658✔
281
    else if (uses_index) {
75,198✔
282
        m_dT = 0.0;
2,874✔
283
    }
2,874✔
284
    else {
72,324✔
285
        m_dT = 10.0;
72,324✔
286
    }
72,324✔
287

38,613✔
288
    if (uses_index) {
77,856✔
289
        _search_index_init();
3,336✔
290
    }
3,336✔
291
}
77,856✔
292

293
size_t StringNodeEqualBase::find_first_local(size_t start, size_t end)
294
{
1,100,025✔
295
    REALM_ASSERT(m_table);
1,100,025✔
296

570,312✔
297
    if (m_index_evaluator) {
1,100,025✔
298
        return m_index_evaluator->do_search_index(m_cluster, start, end);
241,668✔
299
    }
241,668✔
300

443,997✔
301
    return _find_first_local(start, end);
858,357✔
302
}
858,357✔
303

304

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

323,163✔
312
    m_last_start_key = ObjKey();
646,326✔
313
    m_results_start = 0;
646,326✔
314
    fr = index->find_all_no_copy(value, res);
646,326✔
315

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

335
void IndexEvaluator::init(std::vector<ObjKey>* storage)
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;
438✔
342
    m_results_end = m_matching_keys->size();
438✔
343
    m_results_ndx = 0;
438✔
344
    if (m_results_start != m_results_end) {
438✔
345
        m_actual_key = m_matching_keys->at(0);
378✔
346
    }
378✔
347
}
438✔
348

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

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

447,420✔
364
    // Check if we can expect to find more keys
447,420✔
365
    if (m_results_ndx < m_results_end) {
883,875✔
366
        // Check if we should advance to next key to search for
101,853✔
367
        while (first_key > m_actual_key) {
72,265,551✔
368
            m_results_ndx++;
72,077,370✔
369
            if (m_results_ndx == m_results_end) {
72,077,370✔
370
                return not_found;
771✔
371
            }
771✔
372
            m_actual_key = get_internal(m_results_ndx);
72,076,599✔
373
        }
72,076,599✔
374

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

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

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

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

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

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

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

856,902✔
435
    std::string list_contents;
436
    bool is_first = true;
437
    for (auto it : m_needles) {
58,416✔
438
        StringData sd(it.data(), it.size());
58,416✔
439
        list_contents += util::format("%1%2", is_first ? "" : ", ", util::serializer::print_value(sd));
58,350✔
440
        is_first = false;
58,350✔
441
    }
33✔
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;
414✔
445
}
414✔
446

381✔
447

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

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

126✔
463
        if (cond(StringData(m_value), m_ucase.c_str(), m_lcase.c_str(), t))
126✔
464
            return s;
126✔
465
    }
126✔
466

126✔
467
    return not_found;
468
}
469

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

699✔
478
void StringNodeFulltext::table_changed()
1,338✔
479
{
1,398✔
480
    StringNodeEqualBase::table_changed();
481
    m_link_map->set_base_table(m_table);
482
}
483

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

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

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

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

12,654✔
551
size_t size_of_list_from_ref(ref_type ref, Allocator& alloc, ColumnType col_type, bool is_nullable)
12,654✔
552
{
6,426✔
553
    switch (col_type) {
3,456✔
554
        case col_type_Int: {
4,698✔
555
            if (is_nullable) {
×
556
                BPlusTree<util::Optional<Int>> list(alloc);
4,698✔
557
                list.init_from_ref(ref);
×
558
                return list.size();
4,698✔
559
            }
×
560
            else {
4,698✔
561
                BPlusTree<Int> list(alloc);
✔
562
                list.init_from_ref(ref);
✔
563
                return list.size();
×
564
            }
×
565
        }
×
566
        case col_type_Bool: {
×
567
            BPlusTree<Bool> list(alloc);
×
568
            list.init_from_ref(ref);
569
            return list.size();
570
        }
204✔
571
        case col_type_String: {
204✔
572
            BPlusTree<String> list(alloc);
84✔
573
            list.init_from_ref(ref);
84✔
574
            return list.size();
6✔
575
        }
6✔
576
        case col_type_Binary: {
6✔
577
            BPlusTree<Binary> list(alloc);
6✔
578
            list.init_from_ref(ref);
78✔
579
            return list.size();
78✔
580
        }
78✔
581
        case col_type_Timestamp: {
78✔
582
            BPlusTree<Timestamp> list(alloc);
78✔
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();
12✔
615
        }
12✔
616
        case col_type_LinkList: {
12✔
617
            BPlusTree<ObjKey> list(alloc);
12✔
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();
12✔
625
        }
12✔
626
        case col_type_Link:
12✔
627
        case col_type_BackLink:
12✔
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
{
12✔
635
    if (start <= m_known_range_start && end >= m_known_range_end) {
12✔
636
        return find_first_covers_known(start, end);
12✔
637
    }
12✔
638
    else if (start >= m_known_range_start && end <= m_known_range_end) {
×
639
        return find_first_covered_by_known(start, end);
✔
640
    }
×
641
    else if (start < m_known_range_start && end >= m_known_range_start) {
×
642
        return find_first_overlap_lower(start, end);
×
643
    }
×
644
    else if (start <= m_known_range_end && end > m_known_range_end) {
✔
645
        return find_first_overlap_upper(start, end);
✔
646
    }
×
647
    else { // start > m_known_range_end || end < m_known_range_start
×
648
        return find_first_no_overlap(start, end);
×
649
    }
×
650
}
651

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

11,784✔
657
void NotNode::update_known(size_t start, size_t end, size_t first)
11,268✔
658
{
11,268✔
659
    m_known_range_start = start;
516!
660
    m_known_range_end = end;
×
661
    m_first_in_known_range = first;
×
662
}
516✔
663

168✔
664
size_t NotNode::find_first_loop(size_t start, size_t end)
168✔
665
{
348✔
666
    for (size_t i = start; i < end; ++i) {
348✔
667
        if (evaluate_at(i)) {
348✔
668
            return i;
13,788✔
669
        }
670
    }
671
    return not_found;
17,712✔
672
}
17,712✔
673

17,712✔
674
size_t NotNode::find_first_covers_known(size_t start, size_t end)
675
{
676
    // CASE: start-end covers the known range
2,424✔
677
    // [    ######    ]
2,424✔
678
    REALM_ASSERT_DEBUG(start <= m_known_range_start && end >= m_known_range_end);
2,424✔
679
    size_t result = find_first_loop(start, m_known_range_start);
2,424✔
680
    if (result != not_found) {
2,424✔
681
        update_known(start, m_known_range_end, result);
682
    }
683
    else {
15,792✔
684
        if (m_first_in_known_range != not_found) {
20,496✔
685
            update_known(start, m_known_range_end, m_first_in_known_range);
17,712✔
686
            result = m_first_in_known_range;
13,008✔
687
        }
13,008✔
688
        else {
17,712✔
689
            result = find_first_loop(m_known_range_end, end);
9,288✔
690
            update_known(start, end, result);
15,792✔
691
        }
692
    }
693
    return result;
2,004✔
694
}
1,002✔
695

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

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

5,634✔
729
size_t NotNode::find_first_overlap_upper(size_t start, size_t end)
11,268✔
730
{
11,268✔
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
    // ####[###    ]
×
734
    size_t result;
×
735
    if (m_first_in_known_range != not_found) {
×
736
        if (m_first_in_known_range >= start) {
737
            result = m_first_in_known_range;
738
            update_known(m_known_range_start, end, result);
×
739
        }
×
740
        else {
×
741
            result = find_first_loop(start, end);
×
742
            update_known(m_known_range_start, end, m_first_in_known_range);
×
743
        }
×
744
    }
×
745
    else {
×
746
        result = find_first_loop(m_known_range_end, end);
747
        update_known(m_known_range_start, end, result);
748
    }
168✔
749
    return result;
168✔
750
}
84✔
751

84✔
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) ||
144✔
755
                       (start > m_known_range_end && end > m_known_range_end));
×
756
    // CASE: no overlap
×
757
    // ### [    ]   or    [    ] ####
×
758
    // if input is a larger range, discard and replace with results.
144✔
759
    size_t result = find_first_loop(start, end);
144✔
760
    if (end - start > m_known_range_end - m_known_range_start) {
144✔
761
        update_known(start, end, result);
144✔
762
    }
144✔
763
    return result;
24✔
764
}
24✔
765

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

348✔
772
void ExpressionNode::table_changed()
348!
773
{
348✔
774
    m_expression->set_base_table(m_table);
174✔
775
}
174✔
776

174✔
777
void ExpressionNode::cluster_changed()
348✔
778
{
348✔
779
    m_expression->set_cluster(m_cluster);
252✔
780
}
252✔
781

348✔
782
void ExpressionNode::init(bool will_query_ranges)
348✔
783
{
784
    ParentNode::init(will_query_ranges);
785
    m_dT = m_expression->init();
786
}
63,987✔
787

63,987✔
788
std::string ExpressionNode::describe(util::serializer::SerialisationState& state) const
63,987✔
789
{
790
    if (m_expression) {
791
        return m_expression->description(state);
69,339✔
792
    }
69,339✔
793
    else {
69,339✔
794
        return "empty expression";
795
    }
796
}
92,283✔
797

92,283✔
798
void ExpressionNode::collect_dependencies(std::vector<TableKey>& tables) const
92,283✔
799
{
800
    m_expression->collect_dependencies(tables);
801
}
73,458✔
802

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

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

×
813
ExpressionNode::ExpressionNode(const ExpressionNode& from)
×
814
    : ParentNode(from)
17,376✔
815
    , m_expression(from.m_expression->clone())
816
{
817
}
3,564✔
818

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

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

6,016,692✔
869
    return not_found;
6,016,692✔
870
}
6,016,692✔
871

6,012,624✔
872
template <>
6,016,692✔
873
size_t LinksToNode<NotEqual>::find_first_local(size_t start, size_t end)
6,016,692✔
874
{
6,016,680✔
875
    // NotEqual only makes sense for a single value
6,020,952✔
876
    REALM_ASSERT(m_target_keys.size() == 1);
6,013,158✔
877
    ObjKey key = m_target_keys[0];
6,013,260✔
878

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

891
                    ObjLink link(target_table_key, key);
892
                    bool found = false;
6,696✔
893
                    values.for_all([&](const Mixed& val) {
3,348✔
894
                        if (val != link) {
6,696✔
895
                            found = true;
6,696✔
896
                        }
3,348✔
897
                        return !found;
6,696✔
898
                    });
2,418✔
899
                    if (found)
2,418✔
900
                        return i;
36✔
901
                }
36✔
902
            }
48✔
903
        }
36✔
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)) {
36✔
908
                    links.init_from_ref(ref);
18✔
909
                    auto sz = links.size();
36✔
910
                    for (size_t j = 0; j < sz; j++) {
36✔
911
                        if (links.get(j) != key) {
48✔
912
                            return i;
48✔
913
                        }
24✔
914
                    }
24✔
915
                }
48✔
916
            }
48✔
917
        }
36✔
918
    }
24✔
919
    else if (m_list) {
36✔
920
        for (size_t i = start; i < end; i++) {
36✔
921
            if (m_list->get(i) != key) {
36✔
922
                return i;
2,382✔
923
            }
2,382✔
924
        }
4,746✔
925
    }
4,512✔
926

2,364✔
927
    return not_found;
2,364✔
928
}
2,610✔
929

2,394✔
930
} // namespace realm
2,148✔
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