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

realm / realm-core / thomas.goyne_112

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

push

Evergreen

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

Release/13.23.2

91754 of 168238 branches covered (0.0%)

230143 of 251285 relevant lines covered (91.59%)

7082763.83 hits per line

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

90.39
/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,100,443✔
37
}
3,100,443✔
38

39

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

2,138,121✔
46
    while (REALM_LIKELY(start < end)) {
4,487,868✔
47
        size_t m = m_children[current_cond]->find_first_local(start, end);
4,455,258✔
48

2,297,244✔
49
        if (m != start) {
4,455,258✔
50
            // Pointer advanced - we will have to check all other conditions
1,338,120✔
51
            nb_cond_to_test = sz;
2,563,989✔
52
            start = m;
2,563,989✔
53
        }
2,563,989✔
54

2,297,244✔
55
        nb_cond_to_test--;
4,455,258✔
56

2,297,244✔
57
        // Optimized for one condition where this will be true first time
2,297,244✔
58
        if (REALM_LIKELY(nb_cond_to_test == 0))
4,455,258✔
59
            return m;
4,286,592✔
60

176,376✔
61
        current_cond++;
345,042✔
62

176,376✔
63
        if (current_cond == sz)
345,042✔
64
            current_cond = 0;
115,254✔
65
    }
345,042✔
66
    return not_found;
2,153,478✔
67
}
4,142,826✔
68

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

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

1,929,585✔
99
    m_state = st;
5,255,163✔
100
    m_source_column = source_column;
5,255,163✔
101
    size_t local_matches = 0;
5,255,163✔
102

1,929,585✔
103
    if (m_children.size() == 1) {
6,103,476✔
104
        return find_all_local(start, end);
6,086,589✔
105
    }
6,086,589✔
106

2,147,483,647✔
107
    size_t r = start - 1;
2,147,500,534✔
108
    for (;;) {
2,147,947,441✔
109
        if (local_matches == local_limit) {
954,408✔
110
            m_dD = double(r - start) / (local_matches + 1.1);
14,337✔
111
            return r + 1;
14,337✔
112
        }
14,337✔
113

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

464,088✔
122
        local_matches++;
902,643✔
123

464,088✔
124
        // Find first match in remaining condition nodes
464,088✔
125
        size_t m = r;
902,643✔
126

464,088✔
127
        for (size_t c = 1; c < m_children.size(); c++) {
1,390,623✔
128
            m = m_children[c]->find_first_local(r, r + 1);
968,229✔
129
            if (m != r) {
968,229✔
130
                break;
480,249✔
131
            }
480,249✔
132
        }
968,229✔
133

464,088✔
134
        // If index of first match in this node equals index of first match in all remaining nodes, we have a final
464,088✔
135
        // match
464,088✔
136
        if (m == r) {
902,643✔
137
            Mixed val;
422,493✔
138
            if (source_column) {
422,493✔
139
                val = source_column->get_any(r);
408✔
140
            }
408✔
141
            bool cont = st->match(r, val);
422,493✔
142
            if (!cont) {
422,493✔
143
                return static_cast<size_t>(-1);
27✔
144
            }
27✔
145
        }
422,493✔
146
    }
902,643✔
147
}
2,147,500,534✔
148

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

38,595✔
277
    const bool uses_index = has_search_index();
77,820✔
278
    if (m_is_string_enum) {
77,820✔
279
        m_dT = 1.0;
2,658✔
280
    }
2,658✔
281
    else if (uses_index) {
75,162✔
282
        m_dT = 0.0;
2,844✔
283
    }
2,844✔
284
    else {
72,318✔
285
        m_dT = 10.0;
72,318✔
286
    }
72,318✔
287

38,595✔
288
    if (uses_index) {
77,820✔
289
        _search_index_init();
3,306✔
290
    }
3,306✔
291
}
77,820✔
292

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

562,599✔
297
    if (m_index_evaluator) {
1,089,099✔
298
        return m_index_evaluator->do_search_index(m_cluster, start, end);
241,620✔
299
    }
241,620✔
300

436,308✔
301
    return _find_first_local(start, end);
847,479✔
302
}
847,479✔
303

304

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

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

323,151✔
316
    m_index_matches.reset();
646,302✔
317
    switch (fr) {
646,302✔
318
        case FindRes_single:
2,610✔
319
            m_actual_key = ObjKey(res.payload);
2,610✔
320
            m_results_end = 1;
2,610✔
321
            break;
2,610✔
322
        case FindRes_column:
1,221✔
323
            m_index_matches.reset(new IntegerColumn(index->get_alloc(), ref_type(res.payload))); // Throws
1,221✔
324
            m_results_start = res.start_ndx;
1,221✔
325
            m_results_end = res.end_ndx;
1,221✔
326
            m_actual_key = ObjKey(m_index_matches->get(m_results_start));
1,221✔
327
            break;
1,221✔
328
        case FindRes_not_found:
642,471✔
329
            m_results_end = 0;
642,471✔
330
            break;
642,471✔
331
    }
646,302✔
332
    m_results_ndx = m_results_start;
646,302✔
333
}
646,302✔
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,836✔
351
    if (start >= end) {
883,836✔
352
        return not_found;
9✔
353
    }
9✔
354

447,396✔
355
    ObjKey first_key = cluster->get_real_key(start);
883,827✔
356
    if (first_key < m_last_start_key) {
883,827✔
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,827✔
363

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

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

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

386
void StringNode<Equal>::_search_index_init()
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);
3,180✔
390
    m_index_evaluator->init(index, StringData(StringNodeBase::m_value));
3,180✔
391
}
3,180✔
392

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

462✔
399
    auto& other = static_cast<StringNode<Equal>&>(node);
231✔
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()) {
462✔
403
        m_needles.insert(m_value ? StringData(*m_value) : StringData());
462✔
404
    }
156✔
405
    if (auto& str = other.m_value) {
162✔
406
        m_needle_storage.push_back(std::make_unique<char[]>(str->size()));
462✔
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
    }
444✔
410
    else {
444✔
411
        m_needles.emplace();
18✔
412
    }
18✔
413
    return true;
18✔
414
}
462✔
415

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

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

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

66✔
447

448
void StringNode<EqualIns>::_search_index_init()
449
{
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

126✔
457
size_t StringNode<EqualIns>::_find_first_local(size_t start, size_t end)
126✔
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()) {
499
        std::set<ObjKey> tmp;
500
        for (auto k : m_index_matches) {
360✔
501
            auto ndxs = m_link_map->get_origin_ndxs(k);
360✔
502
            tmp.insert(ndxs.begin(), ndxs.end());
360✔
503
        }
360✔
504
        m_index_matches.assign(tmp.begin(), tmp.end());
360✔
505
    }
180✔
506

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

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

×
551
size_t size_of_list_from_ref(ref_type ref, Allocator& alloc, ColumnType col_type, bool is_nullable)
4,698✔
552
{
✔
553
    switch (col_type) {
✔
554
        case col_type_Int: {
×
555
            if (is_nullable) {
×
556
                BPlusTree<util::Optional<Int>> list(alloc);
×
557
                list.init_from_ref(ref);
×
558
                return list.size();
×
559
            }
560
            else {
561
                BPlusTree<Int> list(alloc);
204✔
562
                list.init_from_ref(ref);
204✔
563
                return list.size();
84✔
564
            }
84✔
565
        }
6✔
566
        case col_type_Bool: {
6✔
567
            BPlusTree<Bool> list(alloc);
6✔
568
            list.init_from_ref(ref);
6✔
569
            return list.size();
78✔
570
        }
78✔
571
        case col_type_String: {
78✔
572
            BPlusTree<String> list(alloc);
78✔
573
            list.init_from_ref(ref);
78✔
574
            return list.size();
×
575
        }
12✔
576
        case col_type_Binary: {
12✔
577
            BPlusTree<Binary> list(alloc);
12✔
578
            list.init_from_ref(ref);
12✔
579
            return list.size();
×
580
        }
12✔
581
        case col_type_Timestamp: {
12✔
582
            BPlusTree<Timestamp> list(alloc);
12✔
583
            list.init_from_ref(ref);
12✔
584
            return list.size();
×
585
        }
12✔
586
        case col_type_Float: {
12✔
587
            BPlusTree<Float> list(alloc);
12✔
588
            list.init_from_ref(ref);
12✔
589
            return list.size();
×
590
        }
12✔
591
        case col_type_Double: {
12✔
592
            BPlusTree<Double> list(alloc);
12✔
593
            list.init_from_ref(ref);
12✔
594
            return list.size();
×
595
        }
12✔
596
        case col_type_Decimal: {
12✔
597
            BPlusTree<Decimal128> list(alloc);
12✔
598
            list.init_from_ref(ref);
12✔
599
            return list.size();
×
600
        }
12✔
601
        case col_type_ObjectId: {
12✔
602
            BPlusTree<ObjectId> list(alloc);
12✔
603
            list.init_from_ref(ref);
12✔
604
            return list.size();
×
605
        }
12✔
606
        case col_type_UUID: {
12✔
607
            BPlusTree<UUID> list(alloc);
12✔
608
            list.init_from_ref(ref);
12✔
609
            return list.size();
×
610
        }
12✔
611
        case col_type_Mixed: {
12✔
612
            BPlusTree<Mixed> list(alloc);
12✔
613
            list.init_from_ref(ref);
12✔
614
            return list.size();
×
615
        }
12✔
616
        case col_type_LinkList: {
12✔
617
            BPlusTree<ObjKey> list(alloc);
12✔
618
            list.init_from_ref(ref);
12✔
619
            return list.size();
×
620
        }
✔
621
        case col_type_TypedLink: {
×
622
            BPlusTree<ObjLink> list(alloc);
×
623
            list.init_from_ref(ref);
×
624
            return list.size();
×
625
        }
12✔
626
        case col_type_Link:
12✔
627
        case col_type_BackLink:
12✔
628
            break;
12✔
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
    }
×
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
    }
13,716✔
644
    else if (start <= m_known_range_end && end > m_known_range_end) {
13,716✔
645
        return find_first_overlap_upper(start, end);
1,992✔
646
    }
1,992✔
647
    else { // start > m_known_range_end || end < m_known_range_start
11,724✔
648
        return find_first_no_overlap(start, end);
11,208✔
649
    }
11,208✔
650
}
516!
651

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

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

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

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

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

×
714
size_t NotNode::find_first_overlap_lower(size_t start, size_t end)
11,208✔
715
{
×
716
    REALM_ASSERT_DEBUG(start < m_known_range_start && end >= m_known_range_start && end <= m_known_range_end);
×
717
    static_cast<void>(end);
11,208✔
718
    // CASE: partial overlap, lower end
5,604✔
719
    // [   ###]#####
5,604✔
720
    size_t result;
11,208✔
721
    result = find_first_loop(start, m_known_range_start);
11,208✔
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
    // ####[###    ]
×
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
        }
168✔
740
        else {
168✔
741
            result = find_first_loop(start, end);
84✔
742
            update_known(m_known_range_start, end, m_first_in_known_range);
84✔
743
        }
168✔
744
    }
168✔
745
    else {
144✔
746
        result = find_first_loop(m_known_range_end, end);
×
747
        update_known(m_known_range_start, end, result);
×
748
    }
×
749
    return result;
144✔
750
}
144✔
751

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

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

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

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

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

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

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

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

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

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

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

72✔
846
            BPlusTree<ObjKey> links(alloc);
156✔
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);
96✔
850
                    for (auto& key : m_target_keys) {
6,013,164✔
851
                        if (key) {
3,006,582✔
852
                            if (links.find_first(key) != not_found)
6,013,164✔
853
                                return i;
6✔
854
                        }
3,006,579✔
855
                    }
6,013,158✔
856
                }
6,021,486✔
857
            }
6,020,952✔
858
        }
6,016,680✔
859
    }
6,016,692✔
860
    else if (m_list) {
6,016,692✔
861
        for (auto& key : m_target_keys) {
6,016,692✔
862
            auto pos = m_list->find_first(key, start, end);
6,012,624✔
863
            if (pos != realm::npos) {
6,016,692✔
864
                return pos;
6,016,692✔
865
            }
6,016,680✔
866
        }
6,020,952✔
867
    }
6,013,158✔
868

6,013,260✔
869
    return not_found;
36,738✔
870
}
36,738✔
871

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

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

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

2,382✔
927
    return not_found;
2,418✔
928
}
4,278✔
929

4,782✔
930
} // namespace realm
4,614✔
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