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

realm / realm-core / 1719

29 Sep 2023 07:44PM UTC coverage: 91.171% (-0.03%) from 91.205%
1719

push

Evergreen

web-flow
Update tests to use global logger (#6917)

* Replace local StdErrLogger with get_default_logger()
* Updated changelog; fixed logger test
* Fixed CI failures
* Use test_context.logger for test_sync tests
* Updated changelog after release

95860 of 175738 branches covered (0.0%)

48 of 48 new or added lines in 10 files covered. (100.0%)

123 existing lines in 18 files now uncovered.

232404 of 254911 relevant lines covered (91.17%)

6345713.39 hits per line

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

84.01
/src/realm/mixed.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/mixed.hpp>
20
#include <realm/decimal128.hpp>
21
#include <realm/unicode.hpp>
22
#include <realm/column_type_traits.hpp>
23
#include <realm/obj.hpp>
24
#include <realm/table.hpp>
25
#include <realm/query_value.hpp>
26
#include <realm/util/serializer.hpp>
27

28
namespace realm {
29
namespace {
30
static const int sorting_rank[19] = {
31
    // Observe! Changing these values breaks the file format for Set<Mixed>
32

33
    -1, // null
34
    1,  // type_Int = 0,
35
    0,  // type_Bool = 1,
36
    2,  // type_String = 2,
37
    -1,
38
    2,  // type_Binary = 4,
39
    -1, // type_OldTable = 5,
40
    -1, // type_Mixed = 6,
41
    -1, // type_OldDateTime = 7,
42
    3,  // type_Timestamp = 8,
43
    1,  // type_Float = 9,
44
    1,  // type_Double = 10,
45
    1,  // type_Decimal = 11,
46
    7,  // type_Link = 12,
47
    -1, // type_LinkList = 13,
48
    -1,
49
    4, // type_ObjectId = 15,
50
    6, // type_TypedLink = 16
51
    5, // type_UUID = 17
52

53
    // Observe! Changing these values breaks the file format for Set<Mixed>
54
};
55

56
int compare_string(StringData a, StringData b)
57
{
11,287,266✔
58
    if (a == b)
11,287,266✔
59
        return 0;
9,917,622✔
60
    return utf8_compare(a, b) ? -1 : 1;
1,369,644✔
61
}
1,369,644✔
62

63
int compare_binary(BinaryData a, BinaryData b)
64
{
377,154✔
65
    size_t asz = a.size();
377,154✔
66
    size_t bsz = b.size();
377,154✔
67
    size_t min_sz = std::min(asz, bsz);
377,154✔
68
    int ret = memcmp(a.data(), b.data(), min_sz);
377,154✔
69
    if (ret == 0) {
377,154✔
70
        if (asz > bsz)
11,385✔
71
            ret = 1;
1,017✔
72
        else if (asz < bsz)
10,368✔
73
            ret = -1;
906✔
74
    }
11,385✔
75
    return ret;
377,154✔
76
}
377,154✔
77

78
template <int>
79
struct IntTypeForSize;
80
template <>
81
struct IntTypeForSize<1> {
82
    using type = uint8_t;
83
};
84
template <>
85
struct IntTypeForSize<2> {
86
    using type = uint16_t;
87
};
88
template <>
89
struct IntTypeForSize<4> {
90
    using type = uint32_t;
91
};
92
template <>
93
struct IntTypeForSize<8> {
94
    using type = uint64_t;
95
};
96

97
template <typename Float>
98
int compare_float(Float a_raw, Float b_raw)
99
{
1,892,592✔
100
    bool a_nan = std::isnan(a_raw);
1,892,592✔
101
    bool b_nan = std::isnan(b_raw);
1,892,592✔
102
    if (!a_nan && !b_nan) {
1,892,592✔
103
        // Just compare as IEEE floats
843,333✔
104
        return a_raw == b_raw ? 0 : a_raw < b_raw ? -1 : 1;
1,434,804✔
105
    }
1,665,603✔
106
    if (a_nan && b_nan) {
226,989✔
107
        // Compare the nan values as unsigned
19,098✔
108
        using IntType = typename IntTypeForSize<sizeof(Float)>::type;
38,280✔
109
        IntType a = 0, b = 0;
38,280✔
110
        memcpy(&a, &a_raw, sizeof(Float));
38,280✔
111
        memcpy(&b, &b_raw, sizeof(Float));
38,280✔
112
        return a == b ? 0 : a < b ? -1 : 1;
29,112✔
113
    }
38,280✔
114
    // One is nan, the other is not
95,082✔
115
    // nans are treated as being less than all non-nan values
95,082✔
116
    return a_nan ? -1 : 1;
188,709✔
117
}
188,709✔
118

119
template <typename T>
120
int compare_generic(T lhs, T rhs)
121
{
36,703,773✔
122
    return lhs == rhs ? 0 : lhs < rhs ? -1 : 1;
35,550,468✔
123
}
36,703,773✔
124

125
// This is the tricky one. Needs to support the following cases:
126
// * Doubles with a fractional component.
127
// * Longs that can't be precisely represented as a double.
128
// * Doubles outside of the range of Longs (including +/- Inf).
129
// * NaN (defined by us as less than all Longs)
130
// * Return value is always -1, 0, or 1 to ensure it is safe to negate.
131
int compare_long_to_double(int64_t lhs, double rhs)
132
{
330,708✔
133
    // All Longs are > NaN
161,097✔
134
    if (std::isnan(rhs))
330,708✔
135
        return 1;
6✔
136

161,094✔
137
    // Ints with magnitude <= 2**53 can be precisely represented as doubles.
161,094✔
138
    // Additionally, doubles outside of this range can't have a fractional component.
161,094✔
139
    static const int64_t kEndOfPreciseDoubles = 1ll << 53;
330,702✔
140
    if (lhs <= kEndOfPreciseDoubles && lhs >= -kEndOfPreciseDoubles) {
330,702✔
141
        return compare_float(double(lhs), rhs);
330,672✔
142
    }
330,672✔
143

18✔
144
    // Large magnitude doubles (including +/- Inf) are strictly > or < all Longs.
18✔
145
    static const double kBoundOfLongRange = -static_cast<double>(LLONG_MIN); // positive 2**63
30✔
146
    if (rhs >= kBoundOfLongRange)
30✔
147
        return -1; // Can't be represented in a Long.
6✔
148
    if (rhs < -kBoundOfLongRange)
24✔
149
        return 1; // Can be represented in a Long.
6✔
150

12✔
151
    // Remaining Doubles can have their integer component precisely represented as long longs.
12✔
152
    // If they have a fractional component, they must be strictly > or < lhs even after
12✔
153
    // truncation of the fractional component since low-magnitude lhs were handled above.
12✔
154
    return compare_generic(lhs, int64_t(rhs));
18✔
155
}
18✔
156
} // anonymous namespace
157

158
Mixed::Mixed(const Obj& obj) noexcept
159
    : Mixed(ObjLink(obj.get_table()->get_key(), obj.get_key()))
160
{
90✔
161
}
90✔
162

163
bool Mixed::types_are_comparable(const Mixed& lhs, const Mixed& rhs)
164
{
3,321,489✔
165
    if (lhs.m_type == rhs.m_type)
3,321,489✔
166
        return lhs.m_type != 0;
2,241,333✔
167

535,635✔
168
    if (lhs.is_null() || rhs.is_null())
1,080,156✔
169
        return false;
345,477✔
170

362,922✔
171
    DataType l_type = lhs.get_type();
734,679✔
172
    DataType r_type = rhs.get_type();
734,679✔
173
    return data_types_are_comparable(l_type, r_type);
734,679✔
174
}
734,679✔
175

176
bool Mixed::data_types_are_comparable(DataType l_type, DataType r_type)
177
{
1,461,003✔
178
    if (l_type == r_type)
1,461,003✔
179
        return true;
700,542✔
180

375,930✔
181
    if (is_numeric(l_type, r_type)) {
760,461✔
182
        return true;
334,344✔
183
    }
334,344✔
184
    if ((l_type == type_String && r_type == type_Binary) || (r_type == type_String && l_type == type_Binary)) {
426,117✔
185
        return true;
363,108✔
186
    }
363,108✔
187
    if (l_type == type_Mixed || r_type == type_Mixed) {
63,009✔
188
        return true; // Mixed is comparable with any type
648✔
189
    }
648✔
190
    return false;
62,361✔
191
}
62,361✔
192

193
bool Mixed::accumulate_numeric_to(Decimal128& destination) const noexcept
194
{
27,804✔
195
    bool did_accumulate = false;
27,804✔
196
    if (!is_null()) {
27,804✔
197
        switch (get_type()) {
27,300✔
198
            case type_Int:
17,820✔
199
                destination += Decimal128(get_int());
17,820✔
200
                did_accumulate = true;
17,820✔
201
                break;
17,820✔
202
            case type_Double:
1,164✔
203
                destination += Decimal128(get_double());
1,164✔
204
                did_accumulate = true;
1,164✔
205
                break;
1,164✔
206
            case type_Float:
1,764✔
207
                destination += Decimal128(get_float());
1,764✔
208
                did_accumulate = true;
1,764✔
209
                break;
1,764✔
210
            case type_Decimal: {
1,752✔
211
                auto val = get_decimal();
1,752✔
212
                if (!val.is_nan()) {
1,752✔
213
                    destination += val;
1,116✔
214
                    did_accumulate = true;
1,116✔
215
                }
1,116✔
216
                break;
1,752✔
217
            }
×
218
            default:
4,800✔
219
                break;
4,800✔
220
        }
27,804✔
221
    }
27,804✔
222
    return did_accumulate;
27,804✔
223
}
27,804✔
224

225
int Mixed::compare(const Mixed& b) const noexcept
226
{
51,917,292✔
227
    // Observe! Changing this function breaks the file format for Set<Mixed>
26,006,511✔
228

26,006,511✔
229
    if (is_null()) {
51,917,292✔
230
        return b.is_null() ? 0 : -1;
747,156✔
231
    }
896,712✔
232
    if (b.is_null())
51,020,580✔
233
        return 1;
376,566✔
234

25,369,281✔
235
    // None is null
25,369,281✔
236
    auto type = get_type();
50,644,014✔
237
    switch (type) {
50,644,014✔
238
        case type_Bool: {
1,245,492✔
239
            if (b.get_type() == type_Bool) {
1,245,492✔
240
                return compare_generic(bool_val, b.bool_val);
1,244,949✔
241
            }
1,244,949✔
242
            break;
543✔
243
        }
543✔
244
        case type_Int:
34,626,987✔
245
            switch (b.get_type()) {
34,626,987✔
246
                case type_Int:
34,396,809✔
247
                    return compare_generic(int_val, b.int_val);
34,396,809✔
248
                case type_Float:
65,106✔
249
                    return compare_long_to_double(int_val, b.float_val);
65,106✔
250
                case type_Double:
156,420✔
251
                    return compare_long_to_double(int_val, b.double_val);
156,420✔
252
                case type_Decimal:
5,028✔
253
                    return Decimal128(int_val).compare(b.decimal_val);
5,028✔
254
                default:
5,691✔
255
                    break;
5,691✔
256
            }
5,691✔
257
            break;
5,691✔
258
        case type_String:
11,287,329✔
259
            if (b.get_type() == type_String)
11,287,329✔
260
                return compare_string(get<StringData>(), b.get<StringData>());
11,279,973✔
261
            [[fallthrough]];
7,356✔
262
        case type_Binary:
387,570✔
263
            if (b.get_type() == type_String || b.get_type() == type_Binary)
387,570✔
264
                return compare_binary(get<BinaryData>(), b.get<BinaryData>());
377,154✔
265
            break;
10,416✔
266
        case type_Float:
742,314✔
267
            switch (b.get_type()) {
742,314✔
268
                case type_Int:
9,621✔
269
                    return -compare_long_to_double(b.int_val, float_val);
9,621✔
270
                case type_Float:
718,803✔
271
                    return compare_float(float_val, b.float_val);
718,803✔
272
                case type_Double:
5,409✔
273
                    return compare_float(double(float_val), b.double_val);
5,409✔
274
                case type_Decimal:
4,089✔
275
                    return Decimal128(float_val).compare(b.decimal_val);
4,089✔
276
                default:
4,383✔
277
                    break;
4,383✔
278
            }
4,383✔
279
            break;
4,383✔
280
        case type_Double:
948,333✔
281
            switch (b.get_type()) {
948,333✔
282
                case type_Int:
99,576✔
283
                    return -compare_long_to_double(b.int_val, double_val);
99,576✔
284
                case type_Float:
6,231✔
285
                    return compare_float(double_val, double(b.float_val));
6,231✔
286
                case type_Double:
831,579✔
287
                    return compare_float(double_val, b.double_val);
831,579✔
288
                case type_Decimal:
5,697✔
289
                    return Decimal128(double_val).compare(b.decimal_val);
5,697✔
290
                default:
5,259✔
291
                    break;
5,259✔
292
            }
5,259✔
293
            break;
5,259✔
294
        case type_Timestamp:
599,358✔
295
            if (b.get_type() == type_Timestamp) {
599,358✔
296
                return compare_generic(date_val, b.date_val);
599,010✔
297
            }
599,010✔
298
            break;
348✔
299
        case type_ObjectId:
87,990✔
300
            if (b.get_type() == type_ObjectId) {
87,990✔
301
                return compare_generic(id_val, b.id_val);
81,594✔
302
            }
81,594✔
303
            break;
6,396✔
304
        case type_Decimal:
454,683✔
305
            switch (b.get_type()) {
454,683✔
306
                case type_Int:
13,683✔
307
                    return decimal_val.compare(Decimal128(b.int_val));
13,683✔
308
                case type_Float:
4,584✔
309
                    return decimal_val.compare(Decimal128(b.float_val));
4,584✔
310
                case type_Double:
5,658✔
311
                    return decimal_val.compare(Decimal128(b.double_val));
5,658✔
312
                case type_Decimal:
426,741✔
313
                    return decimal_val.compare(b.decimal_val);
426,741✔
314
                default:
4,017✔
315
                    break;
4,017✔
316
            }
4,017✔
317
            break;
4,017✔
318
        case type_Link:
5,541✔
319
            if (b.get_type() == type_Link) {
5,541✔
320
                return compare_generic(int_val, b.int_val);
5,541✔
321
            }
5,541✔
322
            break;
×
323
        case type_TypedLink:
15,975✔
324
            if (b.is_type(type_TypedLink)) {
15,975✔
325
                return compare_generic(link_val, b.link_val);
15,864✔
326
            }
15,864✔
327
            break;
111✔
328
        case type_UUID:
321,507✔
329
            if (b.get_type() == type_UUID) {
321,507✔
330
                return compare_generic(uuid_val, b.uuid_val);
319,017✔
331
            }
319,017✔
332
            break;
2,490✔
333
        default:
62,016✔
334
            if (type == type_TypeOfValue && b.get_type() == type_TypeOfValue) {
62,016✔
335
                return TypeOfValue(int_val).matches(TypeOfValue(b.int_val)) ? 0 : compare_generic(int_val, b.int_val);
52,074✔
336
            }
62,016✔
337
            REALM_ASSERT_RELEASE(false && "Compare not supported for this column type");
×
338
            break;
×
339
    }
41,079✔
340

19,932✔
341
    // Comparing rank of types as a fallback makes it possible to sort of a list of Mixed
19,932✔
342
    REALM_ASSERT(sorting_rank[m_type] != sorting_rank[b.m_type]);
41,079✔
343
    // Using rank table will ensure that all numeric values are kept together
19,932✔
344
    return (sorting_rank[m_type] > sorting_rank[b.m_type]) ? 1 : -1;
31,974✔
345

19,932✔
346
    // Observe! Changing this function breaks the file format for Set<Mixed>
19,932✔
347
}
41,079✔
348

349
int Mixed::compare_signed(const Mixed& b) const noexcept
350
{
22,383✔
351
    if (is_type(type_String) && b.is_type(type_String)) {
22,383✔
352
        auto a_val = get_string();
22,239✔
353
        auto b_val = b.get_string();
22,239✔
354
        return a_val == b_val ? 0 : a_val < b_val ? -1 : 1;
22,227✔
355
    }
22,239✔
356
    return compare(b);
144✔
357
}
144✔
358

359
template <>
360
int64_t Mixed::export_to_type() const noexcept
361
{
18,610,599✔
362
    // If the common type is Int, then both values must be Int
12,645,141✔
363
    REALM_ASSERT(get_type() == type_Int);
18,610,599✔
364
    return int_val;
18,610,599✔
365
}
18,610,599✔
366

367
template <>
368
float Mixed::export_to_type() const noexcept
369
{
1,452✔
370
    // If the common type is Float, then values must be either Int or Float
726✔
371
    REALM_ASSERT(m_type);
1,452✔
372
    switch (get_type()) {
1,452✔
373
        case type_Int:
678✔
374
            return float(int_val);
678✔
375
        case type_Float:
774✔
376
            return float_val;
774✔
377
        default:
✔
378
            REALM_ASSERT(false);
×
379
            break;
×
380
    }
×
381
    return 0.;
×
382
}
×
383

384
template <>
385
double Mixed::export_to_type() const noexcept
386
{
2,592✔
387
    // If the common type is Double, then values must be either Int, Float or Double
1,296✔
388
    REALM_ASSERT(m_type);
2,592✔
389
    switch (get_type()) {
2,592✔
390
        case type_Int:
1,296✔
391
            return double(int_val);
1,296✔
392
        case type_Float:
✔
393
            return double(float_val);
×
394
        case type_Double:
1,296✔
395
            return double_val;
1,296✔
396
        default:
✔
397
            REALM_ASSERT(false);
×
398
            break;
×
399
    }
×
400
    return 0.;
×
401
}
×
402

403
template <>
404
Decimal128 Mixed::export_to_type() const noexcept
405
{
192✔
406
    REALM_ASSERT(m_type);
192✔
407
    switch (get_type()) {
192✔
408
        case type_Int:
96✔
409
            return Decimal128(int_val);
96✔
410
        case type_Float:
✔
411
            return Decimal128(float_val);
×
412
        case type_Double:
✔
413
            return Decimal128(double_val);
×
414
        case type_Decimal:
96✔
415
            return decimal_val;
96✔
416
        default:
✔
417
            REALM_ASSERT(false);
×
418
            break;
×
419
    }
×
420
    return {};
×
421
}
×
422

423
template <>
424
util::Optional<int64_t> Mixed::get<util::Optional<int64_t>>() const noexcept
425
{
408✔
426
    if (is_null()) {
408✔
427
        return {};
×
428
    }
×
429
    return get<int64_t>();
408✔
430
}
408✔
431

432
template <>
433
util::Optional<bool> Mixed::get<util::Optional<bool>>() const noexcept
434
{
48✔
435
    if (is_null()) {
48✔
436
        return {};
×
437
    }
×
438
    return get<bool>();
48✔
439
}
48✔
440

441
template <>
442
util::Optional<float> Mixed::get<util::Optional<float>>() const noexcept
443
{
120✔
444
    if (is_null()) {
120✔
445
        return {};
×
446
    }
×
447
    return get<float>();
120✔
448
}
120✔
449

450
template <>
451
util::Optional<double> Mixed::get<util::Optional<double>>() const noexcept
452
{
120✔
453
    if (is_null()) {
120✔
454
        return {};
×
455
    }
×
456
    return get<double>();
120✔
457
}
120✔
458

459
template <>
460
util::Optional<ObjectId> Mixed::get<util::Optional<ObjectId>>() const noexcept
461
{
96✔
462
    if (is_null()) {
96✔
463
        return {};
×
464
    }
×
465
    return get<ObjectId>();
96✔
466
}
96✔
467

468
template <>
469
util::Optional<UUID> Mixed::get<util::Optional<UUID>>() const noexcept
470
{
96✔
471
    if (is_null()) {
96✔
472
        return {};
×
473
    }
×
474
    return get<UUID>();
96✔
475
}
96✔
476

477
static DataType get_common_type(DataType t1, DataType t2) noexcept
478
{
9,307,446✔
479
    // It might be by accident that this works, but it finds the most advanced type
6,323,649✔
480
    DataType common = std::max(t1, t2);
9,307,446✔
481
    return common;
9,307,446✔
482
}
9,307,446✔
483

484
Mixed Mixed::operator+(const Mixed& rhs) const noexcept
485
{
4,894,392✔
486
    if (!is_null() && !rhs.is_null()) {
4,894,392✔
487
        auto common_type = get_common_type(get_type(), rhs.get_type());
4,894,305✔
488
        switch (common_type) {
4,894,305✔
489
            case type_Int:
4,893,780✔
490
                return export_to_type<Int>() + rhs.export_to_type<Int>();
4,893,780✔
491
            case type_Float:
372✔
492
                return export_to_type<float>() + rhs.export_to_type<float>();
372✔
493
            case type_Double:
114✔
494
                return export_to_type<double>() + rhs.export_to_type<double>();
114✔
495
            case type_Decimal:
✔
496
                return export_to_type<Decimal128>() + rhs.export_to_type<Decimal128>();
×
497
            default:
48✔
498
                break;
48✔
499
        }
135✔
500
    }
135✔
501
    return {};
135✔
502
}
135✔
503

504
Mixed Mixed::operator-(const Mixed& rhs) const noexcept
505
{
2,449,221✔
506
    if (!is_null() && !rhs.is_null()) {
2,449,221✔
507
        auto common_type = get_common_type(get_type(), rhs.get_type());
2,449,221✔
508
        switch (common_type) {
2,449,221✔
509
            case type_Int:
2,449,125✔
510
                return export_to_type<Int>() - rhs.export_to_type<Int>();
2,449,125✔
511
            case type_Float:
96✔
512
                return export_to_type<float>() - rhs.export_to_type<float>();
96✔
513
            case type_Double:
✔
514
                return export_to_type<double>() - rhs.export_to_type<double>();
×
515
            case type_Decimal:
✔
516
                return export_to_type<Decimal128>() - rhs.export_to_type<Decimal128>();
×
517
            default:
✔
518
                break;
×
519
        }
×
520
    }
×
521
    return {};
×
522
}
×
523

524
Mixed Mixed::operator*(const Mixed& rhs) const noexcept
525
{
982,584✔
526
    if (!is_null() && !rhs.is_null()) {
982,584✔
527
        auto common_type = get_common_type(get_type(), rhs.get_type());
982,584✔
528
        switch (common_type) {
982,584✔
529
            case type_Int:
981,660✔
530
                return export_to_type<Int>() * rhs.export_to_type<Int>();
981,660✔
531
            case type_Float:
✔
532
                return export_to_type<float>() * rhs.export_to_type<float>();
×
533
            case type_Double:
924✔
534
                return export_to_type<double>() * rhs.export_to_type<double>();
924✔
535
            case type_Decimal:
✔
536
                return export_to_type<Decimal128>() * rhs.export_to_type<Decimal128>();
×
537
            default:
✔
538
                break;
×
UNCOV
539
        }
×
UNCOV
540
    }
×
UNCOV
541
    return {};
×
UNCOV
542
}
×
543

544
Mixed Mixed::operator/(const Mixed& rhs) const noexcept
545
{
981,348✔
546
    if (!is_null() && !rhs.is_null()) {
981,348✔
547
        auto common_type = get_common_type(get_type(), rhs.get_type());
981,348✔
548
        switch (common_type) {
981,348✔
549
            case type_Int: {
980,736✔
550
                auto dividend = export_to_type<Int>();
980,736✔
551
                auto divisor = rhs.export_to_type<Int>();
980,736✔
552
                // We don't want to throw here. This is usually used as part of a query
675,915✔
553
                // and in this case we would just expect a no match
675,915✔
554
                if (divisor == 0)
980,736✔
555
                    return dividend < 0 ? std::numeric_limits<int64_t>::min() : std::numeric_limits<int64_t>::max();
624✔
556
                return dividend / divisor;
980,112✔
557
            }
980,112✔
558
            case type_Float:
675,732✔
559
                static_assert(std::numeric_limits<float>::is_iec559); // Infinity is supported
258✔
560
                return export_to_type<float>() / rhs.export_to_type<float>();
258✔
561
            case type_Double:
675,732✔
562
                static_assert(std::numeric_limits<double>::is_iec559); // Infinity is supported
258✔
563
                return export_to_type<double>() / rhs.export_to_type<double>();
258✔
564
            case type_Decimal:
675,651✔
565
                return export_to_type<Decimal128>() / rhs.export_to_type<Decimal128>();
96✔
566
            default:
675,603✔
567
                break;
×
568
        }
×
569
    }
×
570
    return {};
×
571
}
×
572

573
size_t Mixed::hash() const
574
{
41,727✔
575
    if (is_null())
41,727✔
576
        return 0;
4,014✔
577

19,398✔
578
    size_t hash = 0;
37,713✔
579
    switch (get_type()) {
37,713✔
580
        case type_Int:
2,946✔
581
            hash = size_t(int_val);
2,946✔
582
            break;
2,946✔
583
        case type_Bool:
1,746✔
584
            hash = bool_val ? 0xdeadbeef : 0xcafebabe;
1,320✔
585
            break;
1,746✔
586
        case type_Float: {
✔
587
            auto unsigned_data = reinterpret_cast<const unsigned char*>(&float_val);
×
588
            hash = murmur2_or_cityhash(unsigned_data, sizeof(float));
×
589
            break;
×
590
        }
×
591
        case type_Double: {
5,862✔
592
            auto unsigned_data = reinterpret_cast<const unsigned char*>(&double_val);
5,862✔
593
            hash = murmur2_or_cityhash(unsigned_data, sizeof(double));
5,862✔
594
            break;
5,862✔
595
        }
×
596
        case type_String:
15,300✔
597
            hash = get<StringData>().hash();
15,300✔
598
            break;
15,300✔
599
        case type_Binary: {
756✔
600
            auto bin = get<BinaryData>();
756✔
601
            StringData str(bin.data(), bin.size());
756✔
602
            hash = str.hash();
756✔
603
            break;
756✔
604
        }
×
605
        case type_Timestamp:
2,850✔
606
            hash = get<Timestamp>().hash();
2,850✔
607
            break;
2,850✔
608
        case type_ObjectId:
2,751✔
609
            hash = get<ObjectId>().hash();
2,751✔
610
            break;
2,751✔
611
        case type_UUID: {
2,751✔
612
            hash = get<UUID>().hash();
2,751✔
613
            break;
2,751✔
614
        }
×
615
        case type_TypedLink: {
✔
616
            auto unsigned_data = reinterpret_cast<const unsigned char*>(&link_val);
×
617
            hash = murmur2_or_cityhash(unsigned_data, 12);
×
618
            break;
×
619
        }
×
620
        case type_Decimal: {
2,751✔
621
            auto value = get<Decimal128>();
2,751✔
622
            auto unsigned_data = reinterpret_cast<const unsigned char*>(value.raw());
2,751✔
623
            hash = murmur2_or_cityhash(unsigned_data, sizeof(Decimal128::Bid128));
2,751✔
624
            break;
2,751✔
625
        }
×
626
        case type_Mixed:
✔
627
        case type_Link:
✔
628
        case type_LinkList:
✔
629
            REALM_ASSERT_RELEASE(false && "Hash not supported for this column type");
×
630
            break;
×
631
    }
37,713✔
632

19,398✔
633
    return hash;
37,713✔
634
}
37,713✔
635

636
StringData Mixed::get_index_data(std::array<char, 16>& buffer) const noexcept
637
{
6,416,583✔
638
    if (is_null()) {
6,416,583✔
639
        return {};
458,253✔
640
    }
458,253✔
641
    switch (get_type()) {
5,958,330✔
642
        case type_Int: {
3,708,501✔
643
            int64_t i = get_int();
3,708,501✔
644
            const char* c = reinterpret_cast<const char*>(&i);
3,708,501✔
645
            realm::safe_copy_n(c, sizeof(int64_t), buffer.data());
3,708,501✔
646
            return StringData{buffer.data(), sizeof(int64_t)};
3,708,501✔
647
        }
×
648
        case type_Bool: {
36,267✔
649
            int64_t i = get_bool() ? 1 : 0;
27,591✔
650
            return Mixed(i).get_index_data(buffer);
36,267✔
651
        }
×
652
        case type_Float: {
24✔
653
            auto v2 = get_float();
24✔
654
            int i = int(v2);
24✔
655
            if (i == v2) {
24✔
656
                return Mixed(i).get_index_data(buffer);
×
657
            }
×
658
            const char* src = reinterpret_cast<const char*>(&v2);
24✔
659
            realm::safe_copy_n(src, sizeof(float), buffer.data());
24✔
660
            return StringData{buffer.data(), sizeof(float)};
24✔
661
        }
24✔
662
        case type_Double: {
48✔
663
            auto v2 = get_double();
48✔
664
            int i = int(v2);
48✔
665
            if (i == v2) {
48✔
666
                return Mixed(i).get_index_data(buffer);
24✔
667
            }
24✔
668
            const char* src = reinterpret_cast<const char*>(&v2);
24✔
669
            realm::safe_copy_n(src, sizeof(double), buffer.data());
24✔
670
            return StringData{buffer.data(), sizeof(double)};
24✔
671
        }
24✔
672
        case type_String:
1,289,754✔
673
            return get_string();
1,289,754✔
674
        case type_Binary: {
18✔
675
            auto bin = get_binary();
12✔
676
            return {bin.data(), bin.size()};
12✔
677
        }
24✔
678
        case type_Timestamp: {
46,968✔
679
            auto dt = get<Timestamp>();
46,968✔
680
            int64_t s = dt.get_seconds();
46,968✔
681
            int32_t ns = dt.get_nanoseconds();
46,968✔
682
            constexpr size_t index_size = sizeof(s) + sizeof(ns);
46,968✔
683
            const char* s_buf = reinterpret_cast<const char*>(&s);
46,968✔
684
            const char* ns_buf = reinterpret_cast<const char*>(&ns);
46,968✔
685
            realm::safe_copy_n(s_buf, sizeof(s), buffer.data());
46,968✔
686
            realm::safe_copy_n(ns_buf, sizeof(ns), buffer.data() + sizeof(s));
46,968✔
687
            return StringData{buffer.data(), index_size};
46,968✔
688
        }
24✔
689
        case type_ObjectId: {
854,574✔
690
            auto id = get<ObjectId>();
854,574✔
691
            memcpy(&buffer, &id, sizeof(ObjectId));
854,574✔
692
            return StringData{buffer.data(), sizeof(ObjectId)};
854,574✔
693
        }
24✔
694
        case type_Decimal: {
30✔
695
            auto v2 = this->get_decimal();
30✔
696
            int64_t i;
30✔
697
            if (v2.to_int(i)) {
30✔
698
                return Mixed(i).get_index_data(buffer);
18✔
699
            }
18✔
700
            const char* src = reinterpret_cast<const char*>(&v2);
12✔
701
            realm::safe_copy_n(src, sizeof(v2), buffer.data());
12✔
702
            return StringData{buffer.data(), sizeof(v2)};
12✔
703
        }
12✔
704
        case type_UUID: {
25,482✔
705
            auto id = get<UUID>();
25,482✔
706
            const auto bytes = id.to_bytes();
25,482✔
707
            std::copy_n(bytes.data(), bytes.size(), buffer.begin());
25,482✔
708
            return StringData{buffer.data(), bytes.size()};
25,482✔
709
        }
12✔
710
        case type_TypedLink: {
54✔
711
            auto link = get<ObjLink>();
54✔
712
            uint32_t k1 = link.get_table_key().value;
54✔
713
            int64_t k2 = link.get_obj_key().value;
54✔
714
            const char* src = reinterpret_cast<const char*>(&k1);
54✔
715
            realm::safe_copy_n(src, sizeof(k1), buffer.data());
54✔
716
            src = reinterpret_cast<const char*>(&k2);
54✔
717
            realm::safe_copy_n(src, sizeof(k2), buffer.data() + sizeof(k1));
54✔
718
            return StringData{buffer.data(), sizeof(k1) + sizeof(k2)};
54✔
719
        }
12✔
720
        case type_Mixed:
6✔
721
        case type_Link:
✔
722
        case type_LinkList:
✔
723
            break;
×
724
    }
×
725
    REALM_ASSERT_RELEASE(false && "Index not supported for this column type");
×
726
    return {};
×
727
}
×
728

729
void Mixed::use_buffer(std::string& buf) noexcept
730
{
3,203,865✔
731
    if (is_null()) {
3,203,865✔
732
        return;
3,164,181✔
733
    }
3,164,181✔
734
    switch (get_type()) {
39,684✔
735
        case type_String:
9,354✔
736
            buf = std::string(string_val);
9,354✔
737
            string_val = StringData(buf);
9,354✔
738
            break;
9,354✔
739
        case type_Binary:
222✔
740
            buf = std::string(binary_val);
222✔
741
            binary_val = BinaryData(buf);
222✔
742
            break;
222✔
743
        default:
30,096✔
744
            break;
30,096✔
745
    }
39,684✔
746
}
39,684✔
747

748
// LCOV_EXCL_START
749
std::ostream& operator<<(std::ostream& out, const Mixed& m)
750
{
26,001✔
751
    if (m.is_null()) {
26,001✔
752
        out << "null";
264✔
753
    }
264✔
754
    else {
25,737✔
755
        switch (m.get_type()) {
25,737✔
756
            case type_Int:
8,580✔
757
                out << m.int_val;
8,580✔
758
                break;
8,580✔
759
            case type_Bool:
882✔
760
                out << (m.bool_val ? "true" : "false");
687✔
761
                break;
882✔
762
            case type_Float:
894✔
763
                out << m.float_val;
894✔
764
                break;
894✔
765
            case type_Double:
1,488✔
766
                out << m.double_val;
1,488✔
767
                break;
1,488✔
768
            case type_String:
2,340✔
769
                out << util::serializer::print_value(m.string_val);
2,340✔
770
                break;
2,340✔
771
            case type_Binary:
876✔
772
                out << util::serializer::print_value(m.binary_val);
876✔
773
                break;
876✔
774
            case type_Timestamp:
870✔
775
                out << util::serializer::print_value(m.date_val);
870✔
776
                break;
870✔
777
            case type_Decimal:
888✔
778
                out << m.decimal_val;
888✔
779
                break;
888✔
780
            case type_ObjectId:
7,833✔
781
                out << util::serializer::print_value(m.id_val);
7,833✔
782
                break;
7,833✔
783
            case type_Link:
12✔
784
                out << util::serializer::print_value(ObjKey(m.int_val));
12✔
785
                break;
12✔
786
            case type_TypedLink:
✔
787
                out << util::serializer::print_value(m.link_val);
×
788
                break;
×
789
            case type_UUID:
1,074✔
790
                out << util::serializer::print_value(m.uuid_val);
1,074✔
791
                break;
1,074✔
792
            case type_Mixed:
✔
793
            case type_LinkList:
✔
794
                REALM_ASSERT(false);
×
795
        }
25,737✔
796
    }
25,737✔
797
    return out;
26,001✔
798
}
26,001✔
799
// LCOV_EXCL_STOP
800

801

802
} // namespace realm
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

© 2026 Coveralls, Inc