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

realm / realm-core / 1768

19 Oct 2023 08:39AM UTC coverage: 91.591% (-0.05%) from 91.639%
1768

push

Evergreen

web-flow
Update JSON library to 3.11.2 (#7068)

94274 of 173492 branches covered (0.0%)

0 of 7 new or added lines in 1 file covered. (0.0%)

172 existing lines in 18 files now uncovered.

230516 of 251680 relevant lines covered (91.59%)

7181601.5 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,271,888✔
58
    if (a == b)
11,271,888✔
59
        return 0;
9,905,586✔
60
    return utf8_compare(a, b) ? -1 : 1;
1,366,302✔
61
}
1,366,302✔
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,914,303✔
100
    bool a_nan = std::isnan(a_raw);
1,914,303✔
101
    bool b_nan = std::isnan(b_raw);
1,914,303✔
102
    if (!a_nan && !b_nan) {
1,914,303✔
103
        // Just compare as IEEE floats
861,981✔
104
        return a_raw == b_raw ? 0 : a_raw < b_raw ? -1 : 1;
1,450,476✔
105
    }
1,687,053✔
106
    if (a_nan && b_nan) {
227,250✔
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,088✔
115
    // nans are treated as being less than all non-nan values
95,088✔
116
    return a_nan ? -1 : 1;
188,970✔
117
}
188,970✔
118

119
template <typename T>
120
int compare_generic(T lhs, T rhs)
121
{
36,687,684✔
122
    return lhs == rhs ? 0 : lhs < rhs ? -1 : 1;
35,428,953✔
123
}
36,687,684✔
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
{
351,921✔
133
    // All Longs are > NaN
179,535✔
134
    if (std::isnan(rhs))
351,921✔
135
        return 1;
6✔
136

179,532✔
137
    // Ints with magnitude <= 2**53 can be precisely represented as doubles.
179,532✔
138
    // Additionally, doubles outside of this range can't have a fractional component.
179,532✔
139
    static const int64_t kEndOfPreciseDoubles = 1ll << 53;
351,915✔
140
    if (lhs <= kEndOfPreciseDoubles && lhs >= -kEndOfPreciseDoubles) {
351,915✔
141
        return compare_float(double(lhs), rhs);
351,879✔
142
    }
351,879✔
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
36✔
146
    if (rhs >= kBoundOfLongRange)
36✔
147
        return -1; // Can't be represented in a Long.
6✔
148
    if (rhs < -kBoundOfLongRange)
30✔
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));
24✔
155
}
24✔
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,394,065✔
165
    if (lhs.m_type == rhs.m_type)
3,394,065✔
166
        return lhs.m_type != 0;
2,292,297✔
167

554,541✔
168
    if (lhs.is_null() || rhs.is_null())
1,101,768✔
169
        return false;
345,489✔
170

381,723✔
171
    DataType l_type = lhs.get_type();
756,279✔
172
    DataType r_type = rhs.get_type();
756,279✔
173
    return data_types_are_comparable(l_type, r_type);
756,279✔
174
}
756,279✔
175

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

394,362✔
181
    if (is_numeric(l_type, r_type)) {
781,650✔
182
        return true;
355,539✔
183
    }
355,539✔
184
    if ((l_type == type_String && r_type == type_Binary) || (r_type == type_String && l_type == type_Binary)) {
426,111✔
185
        return true;
363,108✔
186
    }
363,108✔
187
    if (l_type == type_Mixed || r_type == type_Mixed) {
63,003✔
188
        return true; // Mixed is comparable with any type
648✔
189
    }
648✔
190
    return false;
62,355✔
191
}
62,355✔
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,793,155✔
227
    // Observe! Changing this function breaks the file format for Set<Mixed>
25,879,368✔
228

25,879,368✔
229
    if (is_null()) {
51,793,155✔
230
        return b.is_null() ? 0 : -1;
745,278✔
231
    }
894,675✔
232
    if (b.is_null())
50,898,480✔
233
        return 1;
375,897✔
234

25,243,605✔
235
    // None is null
25,243,605✔
236
    auto type = get_type();
50,522,583✔
237
    switch (type) {
50,522,583✔
238
        case type_Bool: {
1,234,893✔
239
            if (b.get_type() == type_Bool) {
1,234,893✔
240
                return compare_generic(bool_val, b.bool_val);
1,234,338✔
241
            }
1,234,338✔
242
            break;
555✔
243
        }
555✔
244
        case type_Int:
34,638,882✔
245
            switch (b.get_type()) {
34,638,882✔
246
                case type_Int:
34,394,079✔
247
                    return compare_generic(int_val, b.int_val);
34,394,079✔
248
                case type_Float:
86,349✔
249
                    return compare_long_to_double(int_val, b.float_val);
86,349✔
250
                case type_Double:
156,414✔
251
                    return compare_long_to_double(int_val, b.double_val);
156,414✔
252
                case type_Decimal:
5,022✔
253
                    return Decimal128(int_val).compare(b.decimal_val);
5,022✔
254
                default:
5,676✔
255
                    break;
5,676✔
256
            }
5,676✔
257
            break;
5,676✔
258
        case type_String:
11,270,814✔
259
            if (b.get_type() == type_String)
11,270,814✔
260
                return compare_string(get<StringData>(), b.get<StringData>());
11,257,230✔
261
            [[fallthrough]];
13,584✔
262
        case type_Binary:
393,798✔
263
            if (b.get_type() == type_String || b.get_type() == type_Binary)
393,798✔
264
                return compare_binary(get<BinaryData>(), b.get<BinaryData>());
377,154✔
265
            break;
16,644✔
266
        case type_Float:
742,611✔
267
            switch (b.get_type()) {
742,611✔
268
                case type_Int:
9,627✔
269
                    return -compare_long_to_double(b.int_val, float_val);
9,627✔
270
                case type_Float:
719,097✔
271
                    return compare_float(float_val, b.float_val);
719,097✔
272
                case type_Double:
5,409✔
273
                    return compare_float(double(float_val), b.double_val);
5,409✔
274
                case type_Decimal:
4,095✔
275
                    return Decimal128(float_val).compare(b.decimal_val);
4,095✔
276
                default:
4,383✔
277
                    break;
4,383✔
278
            }
4,383✔
279
            break;
4,383✔
280
        case type_Double:
948,417✔
281
            switch (b.get_type()) {
948,417✔
282
                case type_Int:
99,534✔
283
                    return -compare_long_to_double(b.int_val, double_val);
99,534✔
284
                case type_Float:
6,231✔
285
                    return compare_float(double_val, double(b.float_val));
6,231✔
286
                case type_Double:
831,714✔
287
                    return compare_float(double_val, b.double_val);
831,714✔
288
                case type_Decimal:
5,700✔
289
                    return Decimal128(double_val).compare(b.decimal_val);
5,700✔
290
                default:
5,271✔
291
                    break;
5,271✔
292
            }
5,271✔
293
            break;
5,271✔
294
        case type_Timestamp:
601,293✔
295
            if (b.get_type() == type_Timestamp) {
601,293✔
296
                return compare_generic(date_val, b.date_val);
600,939✔
297
            }
600,939✔
298
            break;
354✔
299
        case type_ObjectId:
88,056✔
300
            if (b.get_type() == type_ObjectId) {
88,056✔
301
                return compare_generic(id_val, b.id_val);
81,669✔
302
            }
81,669✔
303
            break;
6,387✔
304
        case type_Decimal:
454,683✔
305
            switch (b.get_type()) {
454,683✔
306
                case type_Int:
13,686✔
307
                    return decimal_val.compare(Decimal128(b.int_val));
13,686✔
308
                case type_Float:
4,581✔
309
                    return decimal_val.compare(Decimal128(b.float_val));
4,581✔
310
                case type_Double:
5,658✔
311
                    return decimal_val.compare(Decimal128(b.double_val));
5,658✔
312
                case type_Decimal:
426,735✔
313
                    return decimal_val.compare(b.decimal_val);
426,735✔
314
                default:
4,023✔
315
                    break;
4,023✔
316
            }
4,023✔
317
            break;
4,023✔
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,972✔
324
            if (b.is_type(type_TypedLink)) {
15,972✔
325
                return compare_generic(link_val, b.link_val);
15,876✔
326
            }
15,876✔
327
            break;
96✔
328
        case type_UUID:
319,530✔
329
            if (b.get_type() == type_UUID) {
319,530✔
330
                return compare_generic(uuid_val, b.uuid_val);
317,052✔
331
            }
317,052✔
332
            break;
2,478✔
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,067✔
340

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

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

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

359
template <>
360
int64_t Mixed::export_to_type() const noexcept
361
{
20,311,920✔
362
    // If the common type is Int, then both values must be Int
11,060,778✔
363
    REALM_ASSERT(get_type() == type_Int);
20,311,920✔
364
    return int_val;
20,311,920✔
365
}
20,311,920✔
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
{
10,158,117✔
479
    // It might be by accident that this works, but it finds the most advanced type
5,531,463✔
480
    DataType common = std::max(t1, t2);
10,158,117✔
481
    return common;
10,158,117✔
482
}
10,158,117✔
483

484
Mixed Mixed::operator+(const Mixed& rhs) const noexcept
485
{
5,319,726✔
486
    if (!is_null() && !rhs.is_null()) {
5,319,726✔
487
        auto common_type = get_common_type(get_type(), rhs.get_type());
5,319,642✔
488
        switch (common_type) {
5,319,642✔
489
            case type_Int:
5,319,114✔
490
                return export_to_type<Int>() + rhs.export_to_type<Int>();
5,319,114✔
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
        }
132✔
500
    }
132✔
501
    return {};
132✔
502
}
132✔
503

504
Mixed Mixed::operator-(const Mixed& rhs) const noexcept
505
{
2,685,513✔
506
    if (!is_null() && !rhs.is_null()) {
2,685,513✔
507
        auto common_type = get_common_type(get_type(), rhs.get_type());
2,685,513✔
508
        switch (common_type) {
2,685,513✔
509
            case type_Int:
2,685,420✔
510
                return export_to_type<Int>() - rhs.export_to_type<Int>();
2,685,420✔
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
{
1,077,102✔
526
    if (!is_null() && !rhs.is_null()) {
1,077,102✔
527
        auto common_type = get_common_type(get_type(), rhs.get_type());
1,077,102✔
528
        switch (common_type) {
1,077,102✔
529
            case type_Int:
1,076,178✔
530
                return export_to_type<Int>() * rhs.export_to_type<Int>();
1,076,178✔
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
{
1,075,866✔
546
    if (!is_null() && !rhs.is_null()) {
1,075,866✔
547
        auto common_type = get_common_type(get_type(), rhs.get_type());
1,075,866✔
548
        switch (common_type) {
1,075,866✔
549
            case type_Int: {
1,075,254✔
550
                auto dividend = export_to_type<Int>();
1,075,254✔
551
                auto divisor = rhs.export_to_type<Int>();
1,075,254✔
552
                // We don't want to throw here. This is usually used as part of a query
587,895✔
553
                // and in this case we would just expect a no match
587,895✔
554
                if (divisor == 0)
1,075,254✔
555
                    return dividend < 0 ? std::numeric_limits<int64_t>::min() : std::numeric_limits<int64_t>::max();
624✔
556
                return dividend / divisor;
1,074,630✔
557
            }
1,074,630✔
558
            case type_Float:
587,712✔
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:
587,712✔
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:
587,631✔
565
                return export_to_type<Decimal128>() / rhs.export_to_type<Decimal128>();
96✔
566
            default:
587,583✔
567
                break;
×
UNCOV
568
        }
×
UNCOV
569
    }
×
UNCOV
570
    return {};
×
UNCOV
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,403,293✔
638
    if (is_null()) {
6,403,293✔
639
        return {};
457,944✔
640
    }
457,944✔
641
    switch (get_type()) {
5,945,349✔
642
        case type_Int: {
3,693,819✔
643
            int64_t i = get_int();
3,693,819✔
644
            const char* c = reinterpret_cast<const char*>(&i);
3,693,819✔
645
            realm::safe_copy_n(c, sizeof(int64_t), buffer.data());
3,693,819✔
646
            return StringData{buffer.data(), sizeof(int64_t)};
3,693,819✔
647
        }
×
648
        case type_Bool: {
34,845✔
649
            int64_t i = get_bool() ? 1 : 0;
27,786✔
650
            return Mixed(i).get_index_data(buffer);
34,845✔
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,292,724✔
673
            return get_string();
1,292,724✔
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,998✔
679
            auto dt = get<Timestamp>();
46,998✔
680
            int64_t s = dt.get_seconds();
46,998✔
681
            int32_t ns = dt.get_nanoseconds();
46,998✔
682
            constexpr size_t index_size = sizeof(s) + sizeof(ns);
46,998✔
683
            const char* s_buf = reinterpret_cast<const char*>(&s);
46,998✔
684
            const char* ns_buf = reinterpret_cast<const char*>(&ns);
46,998✔
685
            realm::safe_copy_n(s_buf, sizeof(s), buffer.data());
46,998✔
686
            realm::safe_copy_n(ns_buf, sizeof(ns), buffer.data() + sizeof(s));
46,998✔
687
            return StringData{buffer.data(), index_size};
46,998✔
688
        }
24✔
689
        case type_ObjectId: {
854,673✔
690
            auto id = get<ObjectId>();
854,673✔
691
            memcpy(&buffer, &id, sizeof(ObjectId));
854,673✔
692
            return StringData{buffer.data(), sizeof(ObjectId)};
854,673✔
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,191,556✔
731
    if (is_null()) {
3,191,556✔
732
        return;
3,151,854✔
733
    }
3,151,854✔
734
    switch (get_type()) {
39,702✔
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,702✔
746
}
39,702✔
747

748
// LCOV_EXCL_START
749
std::ostream& operator<<(std::ostream& out, const Mixed& m)
750
{
25,956✔
751
    if (m.is_null()) {
25,956✔
752
        out << "null";
264✔
753
    }
264✔
754
    else {
25,692✔
755
        switch (m.get_type()) {
25,692✔
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,788✔
781
                out << util::serializer::print_value(m.id_val);
7,788✔
782
                break;
7,788✔
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,692✔
796
    }
25,692✔
797
    return out;
25,956✔
798
}
25,956✔
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