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

realm / realm-core / 2329

21 May 2024 11:16PM UTC coverage: 90.817% (-0.008%) from 90.825%
2329

push

Evergreen

web-flow
RCORE-1997 RCORE-2113 Add baas artifact generation to evergreen (#7692)

101682 of 180022 branches covered (56.48%)

214638 of 236340 relevant lines covered (90.82%)

5429875.37 hits per line

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

87.96
/src/realm/parser/driver.cpp
1
/*************************************************************************
2
 *
3
 * Copyright 2021 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/parser/driver.hpp"
20
#include "realm/parser/keypath_mapping.hpp"
21
#include "realm/parser/query_parser.hpp"
22
#include "realm/sort_descriptor.hpp"
23
#include "realm/decimal128.hpp"
24
#include "realm/uuid.hpp"
25
#include "realm/util/base64.hpp"
26
#include "realm/util/overload.hpp"
27
#include "realm/object-store/class.hpp"
28

29
#define YY_NO_UNISTD_H 1
30
#define YY_NO_INPUT 1
31
#include "realm/parser/generated/query_flex.hpp"
32

33
#include <external/mpark/variant.hpp>
34
#include <stdexcept>
35

36
using namespace realm;
37
using namespace std::string_literals;
38

39
// Whether to generate parser debug traces.
40
static bool trace_parsing = false;
41
// Whether to generate scanner debug traces.
42
static bool trace_scanning = false;
43

44
namespace {
45

46
const char* agg_op_type_to_str(query_parser::AggrNode::Type type)
47
{
272✔
48
    switch (type) {
272✔
49
        case realm::query_parser::AggrNode::MAX:
64✔
50
            return ".@max";
64✔
51
        case realm::query_parser::AggrNode::MIN:
72✔
52
            return ".@min";
72✔
53
        case realm::query_parser::AggrNode::SUM:
68✔
54
            return ".@sum";
68✔
55
        case realm::query_parser::AggrNode::AVG:
68✔
56
            return ".@avg";
68✔
57
    }
272✔
58
    return "";
×
59
}
272✔
60

61
const char* expression_cmp_type_to_str(util::Optional<ExpressionComparisonType> type)
62
{
44✔
63
    if (type) {
44✔
64
        switch (*type) {
44✔
65
            case ExpressionComparisonType::Any:
16✔
66
                return "ANY";
16✔
67
            case ExpressionComparisonType::All:
12✔
68
                return "ALL";
12✔
69
            case ExpressionComparisonType::None:
16✔
70
                return "NONE";
16✔
71
        }
44✔
72
    }
44✔
73
    return "";
×
74
}
44✔
75

76
std::string print_pretty_objlink(const ObjLink& link, const Group* g)
77
{
44✔
78
    REALM_ASSERT(g);
44✔
79
    if (link.is_null()) {
44✔
80
        return "NULL";
×
81
    }
×
82
    try {
44✔
83
        auto table = g->get_table(link.get_table_key());
44✔
84
        if (!table) {
44✔
85
            return "link to an invalid table";
×
86
        }
×
87
        auto obj = table->get_object(link.get_obj_key());
44✔
88
        Mixed pk = obj.get_primary_key();
44✔
89
        return util::format("'%1' with primary key '%2'", table->get_class_name(), util::serializer::print_value(pk));
44✔
90
    }
44✔
91
    catch (...) {
44✔
92
        return "invalid link";
×
93
    }
×
94
}
44✔
95

96
bool is_length_suffix(const std::string& s)
97
{
1,720✔
98
    return s.size() == 6 && (s[0] == 'l' || s[0] == 'L') && (s[1] == 'e' || s[1] == 'E') &&
1,720!
99
           (s[2] == 'n' || s[2] == 'N') && (s[3] == 'g' || s[3] == 'G') && (s[4] == 't' || s[4] == 'T') &&
1,720!
100
           (s[5] == 'h' || s[5] == 'H');
1,720!
101
}
1,720✔
102

103
template <typename T>
104
inline bool try_parse_specials(std::string str, T& ret)
105
{
32✔
106
    if constexpr (realm::is_any<T, float, double>::value || std::numeric_limits<T>::is_iec559) {
32✔
107
        std::transform(str.begin(), str.end(), str.begin(), toLowerAscii);
16✔
108
        if (std::numeric_limits<T>::has_quiet_NaN && (str == "nan" || str == "+nan")) {
16✔
109
            ret = std::numeric_limits<T>::quiet_NaN();
×
110
            return true;
×
111
        }
×
112
        else if (std::numeric_limits<T>::has_quiet_NaN && (str == "-nan")) {
16✔
113
            ret = -std::numeric_limits<T>::quiet_NaN();
×
114
            return true;
×
115
        }
×
116
        else if (std::numeric_limits<T>::has_infinity &&
16✔
117
                 (str == "+infinity" || str == "infinity" || str == "+inf" || str == "inf")) {
16✔
118
            ret = std::numeric_limits<T>::infinity();
×
119
            return true;
×
120
        }
×
121
        else if (std::numeric_limits<T>::has_infinity && (str == "-infinity" || str == "-inf")) {
16✔
122
            ret = -std::numeric_limits<T>::infinity();
×
123
            return true;
×
124
        }
×
125
    }
16✔
126
    return false;
16✔
127
}
32✔
128

129
template <typename T>
130
inline const char* get_type_name()
131
{
132
    return "unknown";
133
}
134
template <>
135
inline const char* get_type_name<int64_t>()
136
{
16✔
137
    return "number";
16✔
138
}
16✔
139
template <>
140
inline const char* get_type_name<float>()
141
{
8✔
142
    return "floating point number";
8✔
143
}
8✔
144
template <>
145
inline const char* get_type_name<double>()
146
{
8✔
147
    return "floating point number";
8✔
148
}
8✔
149

150
template <>
151
inline const char* get_type_name<Decimal128>()
152
{
4✔
153
    return "decimal number";
4✔
154
}
4✔
155

156
template <typename T>
157
inline T string_to(const std::string& s)
158
{
436✔
159
    std::istringstream iss(s);
436✔
160
    iss.imbue(std::locale::classic());
436✔
161
    T value;
436✔
162
    iss >> value;
436✔
163
    if (iss.fail()) {
436✔
164
        if (!try_parse_specials(s, value)) {
32✔
165
            throw InvalidQueryArgError(util::format("Cannot convert '%1' to a %2", s, get_type_name<T>()));
32✔
166
        }
32✔
167
    }
32✔
168
    return value;
404✔
169
}
436✔
170

171
template <>
172
inline Decimal128 string_to<Decimal128>(const std::string& s)
173
{
4✔
174
    Decimal128 value(s);
4✔
175
    if (value.is_nan()) {
4✔
176
        throw InvalidQueryArgError(util::format("Cannot convert '%1' to a %2", s, get_type_name<Decimal128>()));
4✔
177
    }
4✔
178
    return value;
×
179
}
4✔
180

181
class MixedArguments : public query_parser::Arguments {
182
public:
183
    using Arg = mpark::variant<Mixed, std::vector<Mixed>>;
184

185
    MixedArguments(const std::vector<Mixed>& args)
186
        : Arguments(args.size())
904✔
187
        , m_args([](const std::vector<Mixed>& args) -> std::vector<Arg> {
1,808✔
188
            std::vector<Arg> ret;
1,808✔
189
            ret.reserve(args.size());
1,808✔
190
            for (const Mixed& m : args) {
15,852✔
191
                ret.push_back(m);
15,852✔
192
            }
15,852✔
193
            return ret;
1,808✔
194
        }(args))
1,808✔
195
    {
1,808✔
196
    }
1,808✔
197
    MixedArguments(const std::vector<Arg>& args)
198
        : Arguments(args.size())
6,262✔
199
        , m_args(args)
6,262✔
200
    {
12,526✔
201
    }
12,526✔
202
    bool bool_for_argument(size_t n) final
203
    {
×
204
        return mixed_for_argument(n).get<bool>();
×
205
    }
×
206
    long long long_for_argument(size_t n) final
207
    {
×
208
        return mixed_for_argument(n).get<int64_t>();
×
209
    }
×
210
    float float_for_argument(size_t n) final
211
    {
×
212
        return mixed_for_argument(n).get<float>();
×
213
    }
×
214
    double double_for_argument(size_t n) final
215
    {
68✔
216
        return mixed_for_argument(n).get<double>();
68✔
217
    }
68✔
218
    StringData string_for_argument(size_t n) final
219
    {
20✔
220
        return mixed_for_argument(n).get<StringData>();
20✔
221
    }
20✔
222
    BinaryData binary_for_argument(size_t n) final
223
    {
×
224
        return mixed_for_argument(n).get<BinaryData>();
×
225
    }
×
226
    Timestamp timestamp_for_argument(size_t n) final
227
    {
×
228
        return mixed_for_argument(n).get<Timestamp>();
×
229
    }
×
230
    ObjectId objectid_for_argument(size_t n) final
231
    {
×
232
        return mixed_for_argument(n).get<ObjectId>();
×
233
    }
×
234
    UUID uuid_for_argument(size_t n) final
235
    {
×
236
        return mixed_for_argument(n).get<UUID>();
×
237
    }
×
238
    Decimal128 decimal128_for_argument(size_t n) final
239
    {
×
240
        return mixed_for_argument(n).get<Decimal128>();
×
241
    }
×
242
    ObjKey object_index_for_argument(size_t n) final
243
    {
×
244
        return mixed_for_argument(n).get<ObjKey>();
×
245
    }
×
246
    ObjLink objlink_for_argument(size_t n) final
247
    {
×
248
        return mixed_for_argument(n).get<ObjLink>();
×
249
    }
×
250
#if REALM_ENABLE_GEOSPATIAL
251
    Geospatial geospatial_for_argument(size_t n) final
252
    {
16✔
253
        return mixed_for_argument(n).get<Geospatial>();
16✔
254
    }
16✔
255
#endif
256
    std::vector<Mixed> list_for_argument(size_t n) final
257
    {
116✔
258
        Arguments::verify_ndx(n);
116✔
259
        return mpark::get<std::vector<Mixed>>(m_args[n]);
116✔
260
    }
116✔
261
    bool is_argument_null(size_t n) final
262
    {
428,888✔
263
        Arguments::verify_ndx(n);
428,888✔
264
        return visit(util::overload{
428,888✔
265
                         [](const Mixed& m) {
428,888✔
266
                             return m.is_null();
428,888✔
267
                         },
428,888✔
268
                         [](const std::vector<Mixed>&) {
428,888✔
269
                             return false;
×
270
                         },
×
271
                     },
428,888✔
272
                     m_args[n]);
428,888✔
273
    }
428,888✔
274
    bool is_argument_list(size_t n) final
275
    {
857,904✔
276
        Arguments::verify_ndx(n);
857,904✔
277
        static_assert(std::is_same_v<mpark::variant_alternative_t<1, Arg>, std::vector<Mixed>>);
857,904✔
278
        return m_args[n].index() == 1;
857,904✔
279
    }
857,904✔
280
    DataType type_for_argument(size_t n) final
281
    {
120✔
282
        return mixed_for_argument(n).get_type();
120✔
283
    }
120✔
284

285
    Mixed mixed_for_argument(size_t n) final
286
    {
428,940✔
287
        Arguments::verify_ndx(n);
428,940✔
288
        if (is_argument_list(n)) {
428,940✔
289
            throw InvalidQueryArgError(
×
290
                util::format("Request for scalar argument at index %1 but a list was provided", n));
×
291
        }
×
292

293
        return mpark::get<Mixed>(m_args[n]);
428,940✔
294
    }
428,940✔
295

296
private:
297
    const std::vector<Arg> m_args;
298
};
299

300
Timestamp get_timestamp_if_valid(int64_t seconds, int32_t nanoseconds)
301
{
596✔
302
    const bool both_non_negative = seconds >= 0 && nanoseconds >= 0;
596✔
303
    const bool both_non_positive = seconds <= 0 && nanoseconds <= 0;
596✔
304
    if (both_non_negative || both_non_positive) {
596✔
305
        return Timestamp(seconds, nanoseconds);
580✔
306
    }
580✔
307
    throw SyntaxError("Invalid timestamp format");
16✔
308
}
596✔
309

310
} // namespace
311

312
namespace realm {
313

314
namespace query_parser {
315

316
std::string_view string_for_op(CompareType op)
317
{
3,052✔
318
    switch (op) {
3,052✔
319
        case CompareType::EQUAL:
164✔
320
            return "=";
164✔
321
        case CompareType::NOT_EQUAL:
40✔
322
            return "!=";
40✔
323
        case CompareType::GREATER:
✔
324
            return ">";
×
325
        case CompareType::LESS:
✔
326
            return "<";
×
327
        case CompareType::GREATER_EQUAL:
✔
328
            return ">=";
×
329
        case CompareType::LESS_EQUAL:
✔
330
            return "<=";
×
331
        case CompareType::BEGINSWITH:
632✔
332
            return "beginswith";
632✔
333
        case CompareType::ENDSWITH:
616✔
334
            return "endswith";
616✔
335
        case CompareType::CONTAINS:
968✔
336
            return "contains";
968✔
337
        case CompareType::LIKE:
584✔
338
            return "like";
584✔
339
        case CompareType::IN:
8✔
340
            return "in";
8✔
341
        case CompareType::TEXT:
40✔
342
            return "text";
40✔
343
    }
3,052✔
344
    return ""; // appease MSVC warnings
×
345
}
3,052✔
346

347
NoArguments ParserDriver::s_default_args;
348
query_parser::KeyPathMapping ParserDriver::s_default_mapping;
349

350
ParserNode::~ParserNode() = default;
2,410,416✔
351

352
QueryNode::~QueryNode() = default;
910,548✔
353

354
Query NotNode::visit(ParserDriver* drv)
355
{
1,232✔
356
    Query q = drv->m_base_table->where();
1,232✔
357
    q.Not();
1,232✔
358
    q.and_query(query->visit(drv));
1,232✔
359
    return {q};
1,232✔
360
}
1,232✔
361

362
Query OrNode::visit(ParserDriver* drv)
363
{
584✔
364
    Query q(drv->m_base_table);
584✔
365
    q.group();
584✔
366
    for (auto it : children) {
430,936✔
367
        q.Or();
430,936✔
368
        q.and_query(it->visit(drv));
430,936✔
369
    }
430,936✔
370
    q.end_group();
584✔
371

372
    return q;
584✔
373
}
584✔
374

375
Query AndNode::visit(ParserDriver* drv)
376
{
708✔
377
    Query q(drv->m_base_table);
708✔
378
    for (auto it : children) {
1,592✔
379
        q.and_query(it->visit(drv));
1,592✔
380
    }
1,592✔
381
    return q;
708✔
382
}
708✔
383

384
static void verify_only_string_types(DataType type, std::string_view op_string)
385
{
3,052✔
386
    if (type != type_String && type != type_Binary && type != type_Mixed) {
3,052✔
387
        throw InvalidQueryError(util::format(
348✔
388
            "Unsupported comparison operator '%1' against type '%2', right side must be a string or binary type",
348✔
389
            op_string, get_data_type_name(type)));
348✔
390
    }
348✔
391
}
3,052✔
392

393
std::unique_ptr<Subexpr> OperationNode::visit(ParserDriver* drv, DataType type)
394
{
1,056✔
395
    std::unique_ptr<Subexpr> left;
1,056✔
396
    std::unique_ptr<Subexpr> right;
1,056✔
397

398
    const bool left_is_constant = m_left->is_constant();
1,056✔
399
    const bool right_is_constant = m_right->is_constant();
1,056✔
400
    const bool produces_multiple_values = m_left->is_list() || m_right->is_list();
1,056✔
401

402
    if (left_is_constant && right_is_constant && !produces_multiple_values) {
1,056✔
403
        right = m_right->visit(drv, type);
48✔
404
        left = m_left->visit(drv, type);
48✔
405
        auto v_left = left->get_mixed();
48✔
406
        auto v_right = right->get_mixed();
48✔
407
        Mixed result;
48✔
408
        switch (m_op) {
48✔
409
            case '+':
28✔
410
                result = v_left + v_right;
28✔
411
                break;
28✔
412
            case '-':
✔
413
                result = v_left - v_right;
×
414
                break;
×
415
            case '*':
20✔
416
                result = v_left * v_right;
20✔
417
                break;
20✔
418
            case '/':
✔
419
                result = v_left / v_right;
×
420
                break;
×
421
            default:
✔
422
                break;
×
423
        }
48✔
424
        return std::make_unique<Value<Mixed>>(result);
48✔
425
    }
48✔
426

427
    if (right_is_constant) {
1,008✔
428
        // Take left first - it cannot be a constant
429
        left = m_left->visit(drv);
416✔
430

431
        right = m_right->visit(drv, left->get_type());
416✔
432
    }
416✔
433
    else {
592✔
434
        right = m_right->visit(drv);
592✔
435
        if (left_is_constant) {
592✔
436
            left = m_left->visit(drv, right->get_type());
152✔
437
        }
152✔
438
        else {
440✔
439
            left = m_left->visit(drv);
440✔
440
        }
440✔
441
    }
592✔
442
    if (!Mixed::is_numeric(left->get_type(), right->get_type())) {
1,008✔
443
        util::serializer::SerialisationState state;
16✔
444
        std::string op(&m_op, 1);
16✔
445
        throw InvalidQueryArgError(util::format("Cannot perform '%1' operation on '%2' and '%3'", op,
16✔
446
                                                left->description(state), right->description(state)));
16✔
447
    }
16✔
448

449
    switch (m_op) {
992✔
450
        case '+':
400✔
451
            return std::make_unique<Operator<Plus>>(std::move(left), std::move(right));
400✔
452
        case '-':
136✔
453
            return std::make_unique<Operator<Minus>>(std::move(left), std::move(right));
136✔
454
        case '*':
260✔
455
            return std::make_unique<Operator<Mul>>(std::move(left), std::move(right));
260✔
456
        case '/':
196✔
457
            return std::make_unique<Operator<Div>>(std::move(left), std::move(right));
196✔
458
        default:
✔
459
            break;
×
460
    }
992✔
461
    return {};
×
462
}
992✔
463

464
Query EqualityNode::visit(ParserDriver* drv)
465
{
467,906✔
466
    auto [left, right] = drv->cmp(values);
467,906✔
467

468
    auto left_type = left->get_type();
467,906✔
469
    auto right_type = right->get_type();
467,906✔
470

471
    auto handle_typed_links = [drv](std::unique_ptr<Subexpr>& list, std::unique_ptr<Subexpr>& expr, DataType& type) {
467,906✔
472
        if (auto link_column = dynamic_cast<const Columns<Link>*>(list.get())) {
400✔
473
            // Change all TypedLink values to ObjKey values
474
            auto value = dynamic_cast<ValueBase*>(expr.get());
400✔
475
            auto left_dest_table_key = link_column->link_map().get_target_table()->get_key();
400✔
476
            auto sz = value->size();
400✔
477
            auto obj_keys = std::make_unique<Value<ObjKey>>();
400✔
478
            obj_keys->init(expr->has_multiple_values(), sz);
400✔
479
            obj_keys->set_comparison_type(expr->get_comparison_type());
400✔
480
            for (size_t i = 0; i < sz; i++) {
808✔
481
                auto val = value->get(i);
416✔
482
                // i'th entry is already NULL
483
                if (!val.is_null()) {
416✔
484
                    TableKey right_table_key;
200✔
485
                    ObjKey right_obj_key;
200✔
486
                    if (val.is_type(type_Link)) {
200✔
487
                        right_table_key = left_dest_table_key;
20✔
488
                        right_obj_key = val.get<ObjKey>();
20✔
489
                    }
20✔
490
                    else if (val.is_type(type_TypedLink)) {
180✔
491
                        right_table_key = val.get_link().get_table_key();
180✔
492
                        right_obj_key = val.get_link().get_obj_key();
180✔
493
                    }
180✔
494
                    else {
×
495
                        const char* target_type = get_data_type_name(val.get_type());
×
496
                        throw InvalidQueryError(
×
497
                            util::format("Unsupported comparison between '%1' and type '%2'",
×
498
                                         link_column->link_map().description(drv->m_serializer_state), target_type));
×
499
                    }
×
500
                    if (left_dest_table_key == right_table_key) {
200✔
501
                        obj_keys->set(i, right_obj_key);
192✔
502
                    }
192✔
503
                    else {
8✔
504
                        const Group* g = drv->m_base_table->get_parent_group();
8✔
505
                        throw InvalidQueryArgError(
8✔
506
                            util::format("The relationship '%1' which links to type '%2' cannot be compared to "
8✔
507
                                         "an argument of type %3",
8✔
508
                                         link_column->link_map().description(drv->m_serializer_state),
8✔
509
                                         link_column->link_map().get_target_table()->get_class_name(),
8✔
510
                                         print_pretty_objlink(ObjLink(right_table_key, right_obj_key), g)));
8✔
511
                    }
8✔
512
                }
200✔
513
            }
416✔
514
            expr = std::move(obj_keys);
392✔
515
            type = type_Link;
392✔
516
        }
392✔
517
    };
400✔
518

519
    if (left_type == type_Link && right->has_constant_evaluation()) {
467,906✔
520
        handle_typed_links(left, right, right_type);
388✔
521
    }
388✔
522
    if (right_type == type_Link && left->has_constant_evaluation()) {
467,906✔
523
        handle_typed_links(right, left, left_type);
12✔
524
    }
12✔
525

526
    if (left_type.is_valid() && right_type.is_valid() && !Mixed::data_types_are_comparable(left_type, right_type)) {
467,906✔
527
        throw InvalidQueryError(util::format("Unsupported comparison between type '%1' and type '%2'",
16✔
528
                                             get_data_type_name(left_type), get_data_type_name(right_type)));
16✔
529
    }
16✔
530
    if (left_type == type_TypeOfValue || right_type == type_TypeOfValue) {
467,890✔
531
        if (left_type != right_type) {
696✔
532
            throw InvalidQueryArgError(
12✔
533
                util::format("Unsupported comparison between @type and raw value: '%1' and '%2'",
12✔
534
                             get_data_type_name(left_type), get_data_type_name(right_type)));
12✔
535
        }
12✔
536
    }
696✔
537

538
    if (op == CompareType::IN) {
467,878✔
539
        Subexpr* r = right.get();
840✔
540
        if (!r->has_multiple_values()) {
840✔
541
            throw InvalidQueryArgError("The keypath following 'IN' must contain a list. Found '" +
8✔
542
                                       r->description(drv->m_serializer_state) + "'");
8✔
543
        }
8✔
544
    }
840✔
545

546
    if (op == CompareType::IN || op == CompareType::EQUAL) {
467,870✔
547
        if (auto mixed_list = dynamic_cast<ConstantMixedList*>(right.get());
463,378✔
548
            mixed_list && mixed_list->size() &&
463,378✔
549
            mixed_list->get_comparison_type().value_or(ExpressionComparisonType::Any) ==
463,378✔
550
                ExpressionComparisonType::Any) {
920✔
551
            if (auto lhs = dynamic_cast<ObjPropertyBase*>(left.get());
608✔
552
                lhs && lhs->column_key() && !lhs->column_key().is_collection() && !lhs->links_exist() &&
608✔
553
                lhs->column_key().get_type() != col_type_Mixed) {
608✔
554
                return drv->m_base_table->where().in(lhs->column_key(), mixed_list->begin(), mixed_list->end());
336✔
555
            }
336✔
556
        }
608✔
557
    }
463,378✔
558

559
    if (left_type == type_Link && left_type == right_type && right->has_constant_evaluation()) {
467,534✔
560
        if (auto link_column = dynamic_cast<const Columns<Link>*>(left.get())) {
380✔
561
            if (link_column->link_map().get_nb_hops() == 1 &&
380✔
562
                link_column->get_comparison_type().value_or(ExpressionComparisonType::Any) ==
380✔
563
                    ExpressionComparisonType::Any) {
324✔
564
                REALM_ASSERT(dynamic_cast<const Value<ObjKey>*>(right.get()));
308✔
565
                auto link_values = static_cast<const Value<ObjKey>*>(right.get());
308✔
566
                // We can use a LinksToNode based query
567
                std::vector<ObjKey> values;
308✔
568
                values.reserve(link_values->size());
308✔
569
                for (auto val : *link_values) {
324✔
570
                    values.emplace_back(val.is_null() ? ObjKey() : val.get<ObjKey>());
324✔
571
                }
324✔
572
                if (op == CompareType::EQUAL) {
308✔
573
                    return drv->m_base_table->where().links_to(link_column->link_map().get_first_column_key(),
200✔
574
                                                               values);
200✔
575
                }
200✔
576
                else if (op == CompareType::NOT_EQUAL) {
108✔
577
                    return drv->m_base_table->where().not_links_to(link_column->link_map().get_first_column_key(),
104✔
578
                                                                   values);
104✔
579
                }
104✔
580
            }
308✔
581
        }
380✔
582
    }
380✔
583
    else if (right->has_single_value() && (left_type == right_type || left_type == type_Mixed)) {
467,154✔
584
        Mixed val = right->get_mixed();
459,674✔
585
        const ObjPropertyBase* prop = dynamic_cast<const ObjPropertyBase*>(left.get());
459,674✔
586
        if (prop && !prop->links_exist() && !prop->has_path()) {
459,674✔
587
            auto col_key = prop->column_key();
444,096✔
588
            if (val.is_null()) {
444,096✔
589
                switch (op) {
212✔
590
                    case CompareType::EQUAL:
156✔
591
                    case CompareType::IN:
156✔
592
                        return drv->m_base_table->where().equal(col_key, realm::null());
156✔
593
                    case CompareType::NOT_EQUAL:
56✔
594
                        return drv->m_base_table->where().not_equal(col_key, realm::null());
56✔
595
                    default:
✔
596
                        break;
×
597
                }
212✔
598
            }
212✔
599
            switch (left->get_type()) {
443,884✔
600
                case type_Int:
10,030✔
601
                    return drv->simple_query(op, col_key, val.get_int());
10,030✔
602
                case type_Bool:
132✔
603
                    return drv->simple_query(op, col_key, val.get_bool());
132✔
604
                case type_String:
2,592✔
605
                    return drv->simple_query(op, col_key, val.get_string(), case_sensitive);
2,592✔
606
                case type_Binary:
1,416✔
607
                    return drv->simple_query(op, col_key, val.get_binary(), case_sensitive);
1,416✔
608
                case type_Timestamp:
140✔
609
                    return drv->simple_query(op, col_key, val.get<Timestamp>());
140✔
610
                case type_Float:
52✔
611
                    return drv->simple_query(op, col_key, val.get_float());
52✔
612
                case type_Double:
100✔
613
                    return drv->simple_query(op, col_key, val.get_double());
100✔
614
                case type_Decimal:
752✔
615
                    return drv->simple_query(op, col_key, val.get<Decimal128>());
752✔
616
                case type_ObjectId:
428,384✔
617
                    return drv->simple_query(op, col_key, val.get<ObjectId>());
428,384✔
618
                case type_UUID:
164✔
619
                    return drv->simple_query(op, col_key, val.get<UUID>());
164✔
620
                case type_Mixed:
120✔
621
                    return drv->simple_query(op, col_key, val, case_sensitive);
120✔
622
                default:
✔
623
                    break;
×
624
            }
443,884✔
625
        }
443,884✔
626
    }
459,674✔
627
    if (case_sensitive) {
23,134✔
628
        switch (op) {
22,312✔
629
            case CompareType::EQUAL:
18,276✔
630
            case CompareType::IN:
18,840✔
631
                return Query(std::unique_ptr<Expression>(new Compare<Equal>(std::move(left), std::move(right))));
18,840✔
632
            case CompareType::NOT_EQUAL:
3,472✔
633
                return Query(std::unique_ptr<Expression>(new Compare<NotEqual>(std::move(left), std::move(right))));
3,472✔
634
            default:
✔
635
                break;
×
636
        }
22,312✔
637
    }
22,312✔
638
    else {
822✔
639
        verify_only_string_types(right_type, util::format("%1%2", string_for_op(op), "[c]"));
822✔
640
        switch (op) {
822✔
641
            case CompareType::EQUAL:
104✔
642
            case CompareType::IN:
112✔
643
                return Query(std::unique_ptr<Expression>(new Compare<EqualIns>(std::move(left), std::move(right))));
112✔
644
            case CompareType::NOT_EQUAL:
40✔
645
                return Query(
40✔
646
                    std::unique_ptr<Expression>(new Compare<NotEqualIns>(std::move(left), std::move(right))));
40✔
647
            default:
✔
648
                break;
×
649
        }
822✔
650
    }
822✔
651
    return {};
×
652
}
23,134✔
653

654
Query BetweenNode::visit(ParserDriver* drv)
655
{
92✔
656
    if (limits->elements.size() != 2) {
92✔
657
        throw InvalidQueryError("Operator 'BETWEEN' requires list with 2 elements.");
8✔
658
    }
8✔
659

660
    if (dynamic_cast<ColumnListBase*>(prop->visit(drv, type_Int).get())) {
84✔
661
        // It's a list!
662
        util::Optional<ExpressionComparisonType> cmp_type = dynamic_cast<PropertyNode*>(prop)->comp_type;
16✔
663
        if (cmp_type.value_or(ExpressionComparisonType::Any) != ExpressionComparisonType::All) {
16✔
664
            throw InvalidQueryError("Only 'ALL' supported for operator 'BETWEEN' when applied to lists.");
12✔
665
        }
12✔
666
    }
16✔
667

668
    auto& min(limits->elements.at(0));
72✔
669
    auto& max(limits->elements.at(1));
72✔
670
    RelationalNode cmp1(prop, CompareType::GREATER_EQUAL, min);
72✔
671
    RelationalNode cmp2(prop, CompareType::LESS_EQUAL, max);
72✔
672

673
    Query q(drv->m_base_table);
72✔
674
    q.and_query(cmp1.visit(drv));
72✔
675
    q.and_query(cmp2.visit(drv));
72✔
676

677
    return q;
72✔
678
}
84✔
679

680
Query RelationalNode::visit(ParserDriver* drv)
681
{
5,308✔
682
    auto [left, right] = drv->cmp(values);
5,308✔
683

684
    auto left_type = left->get_type();
5,308✔
685
    auto right_type = right->get_type();
5,308✔
686
    const bool right_type_is_null = right->has_single_value() && right->get_mixed().is_null();
5,308✔
687
    const bool left_type_is_null = left->has_single_value() && left->get_mixed().is_null();
5,308✔
688
    REALM_ASSERT(!(left_type_is_null && right_type_is_null));
5,308✔
689

690
    if (left_type == type_Link || left_type == type_TypeOfValue) {
5,308✔
691
        throw InvalidQueryError(util::format(
×
692
            "Unsupported operator %1 in query. Only equal (==) and not equal (!=) are supported for this type.",
×
693
            string_for_op(op)));
×
694
    }
×
695

696
    if (!(left_type_is_null || right_type_is_null) && (!left_type.is_valid() || !right_type.is_valid() ||
5,308✔
697
                                                       !Mixed::data_types_are_comparable(left_type, right_type))) {
4,800✔
698
        throw InvalidQueryError(util::format("Unsupported comparison between type '%1' and type '%2'",
24✔
699
                                             get_data_type_name(left_type), get_data_type_name(right_type)));
24✔
700
    }
24✔
701

702
    const ObjPropertyBase* prop = dynamic_cast<const ObjPropertyBase*>(left.get());
5,284✔
703
    if (prop && !prop->links_exist() && !prop->has_path() && right->has_single_value() &&
5,284✔
704
        (left_type == right_type || left_type == type_Mixed)) {
5,284✔
705
        auto col_key = prop->column_key();
1,556✔
706
        switch (left->get_type()) {
1,556✔
707
            case type_Int:
692✔
708
                return drv->simple_query(op, col_key, right->get_mixed().get_int());
692✔
709
            case type_Bool:
✔
710
                break;
×
711
            case type_String:
92✔
712
                return drv->simple_query(op, col_key, right->get_mixed().get_string());
92✔
713
            case type_Binary:
✔
714
                break;
×
715
            case type_Timestamp:
48✔
716
                return drv->simple_query(op, col_key, right->get_mixed().get<Timestamp>());
48✔
717
            case type_Float:
68✔
718
                return drv->simple_query(op, col_key, right->get_mixed().get_float());
68✔
719
                break;
×
720
            case type_Double:
144✔
721
                return drv->simple_query(op, col_key, right->get_mixed().get_double());
144✔
722
                break;
×
723
            case type_Decimal:
16✔
724
                return drv->simple_query(op, col_key, right->get_mixed().get<Decimal128>());
16✔
725
                break;
×
726
            case type_ObjectId:
288✔
727
                return drv->simple_query(op, col_key, right->get_mixed().get<ObjectId>());
288✔
728
                break;
×
729
            case type_UUID:
128✔
730
                return drv->simple_query(op, col_key, right->get_mixed().get<UUID>());
128✔
731
                break;
×
732
            case type_Mixed:
80✔
733
                return drv->simple_query(op, col_key, right->get_mixed());
80✔
734
                break;
×
735
            default:
✔
736
                break;
×
737
        }
1,556✔
738
    }
1,556✔
739
    switch (op) {
3,728✔
740
        case CompareType::GREATER:
2,316✔
741
            return Query(std::unique_ptr<Expression>(new Compare<Greater>(std::move(left), std::move(right))));
2,316✔
742
        case CompareType::LESS:
284✔
743
            return Query(std::unique_ptr<Expression>(new Compare<Less>(std::move(left), std::move(right))));
284✔
744
        case CompareType::GREATER_EQUAL:
540✔
745
            return Query(std::unique_ptr<Expression>(new Compare<GreaterEqual>(std::move(left), std::move(right))));
540✔
746
        case CompareType::LESS_EQUAL:
172✔
747
            return Query(std::unique_ptr<Expression>(new Compare<LessEqual>(std::move(left), std::move(right))));
172✔
748
        default:
✔
749
            break;
×
750
    }
3,728✔
751
    return {};
×
752
}
3,728✔
753

754
Query StringOpsNode::visit(ParserDriver* drv)
755
{
2,856✔
756
    auto [left, right] = drv->cmp(values);
2,856✔
757

758
    auto left_type = left->get_type();
2,856✔
759
    auto right_type = right->get_type();
2,856✔
760
    const ObjPropertyBase* prop = dynamic_cast<const ObjPropertyBase*>(left.get());
2,856✔
761

762
    verify_only_string_types(right_type, string_for_op(op));
2,856✔
763

764
    if (prop && !prop->links_exist() && !prop->has_path() && right->has_single_value() &&
2,856✔
765
        (left_type == right_type || left_type == type_Mixed)) {
2,856✔
766
        auto col_key = prop->column_key();
548✔
767
        if (right_type == type_String) {
548✔
768
            StringData val = right->get_mixed().get_string();
332✔
769

770
            switch (op) {
332✔
771
                case CompareType::BEGINSWITH:
56✔
772
                    return drv->m_base_table->where().begins_with(col_key, val, case_sensitive);
56✔
773
                case CompareType::ENDSWITH:
64✔
774
                    return drv->m_base_table->where().ends_with(col_key, val, case_sensitive);
64✔
775
                case CompareType::CONTAINS:
120✔
776
                    return drv->m_base_table->where().contains(col_key, val, case_sensitive);
120✔
777
                case CompareType::LIKE:
56✔
778
                    return drv->m_base_table->where().like(col_key, val, case_sensitive);
56✔
779
                case CompareType::TEXT:
36✔
780
                    return drv->m_base_table->where().fulltext(col_key, val);
36✔
781
                case CompareType::IN:
✔
782
                case CompareType::EQUAL:
✔
783
                case CompareType::NOT_EQUAL:
✔
784
                case CompareType::GREATER:
✔
785
                case CompareType::LESS:
✔
786
                case CompareType::GREATER_EQUAL:
✔
787
                case CompareType::LESS_EQUAL:
✔
788
                    break;
×
789
            }
332✔
790
        }
332✔
791
        else if (right_type == type_Binary) {
216✔
792
            BinaryData val = right->get_mixed().get_binary();
216✔
793

794
            switch (op) {
216✔
795
                case CompareType::BEGINSWITH:
48✔
796
                    return drv->m_base_table->where().begins_with(col_key, val, case_sensitive);
48✔
797
                case CompareType::ENDSWITH:
56✔
798
                    return drv->m_base_table->where().ends_with(col_key, val, case_sensitive);
56✔
799
                case CompareType::CONTAINS:
72✔
800
                    return drv->m_base_table->where().contains(col_key, val, case_sensitive);
72✔
801
                case CompareType::LIKE:
40✔
802
                    return drv->m_base_table->where().like(col_key, val, case_sensitive);
40✔
803
                case CompareType::TEXT:
✔
804
                case CompareType::IN:
✔
805
                case CompareType::EQUAL:
✔
806
                case CompareType::NOT_EQUAL:
✔
807
                case CompareType::GREATER:
✔
808
                case CompareType::LESS:
✔
809
                case CompareType::GREATER_EQUAL:
✔
810
                case CompareType::LESS_EQUAL:
✔
811
                    break;
×
812
            }
216✔
813
        }
216✔
814
    }
548✔
815

816
    if (case_sensitive) {
2,308✔
817
        switch (op) {
732✔
818
            case CompareType::BEGINSWITH:
152✔
819
                return Query(std::unique_ptr<Expression>(new Compare<BeginsWith>(std::move(right), std::move(left))));
152✔
820
            case CompareType::ENDSWITH:
152✔
821
                return Query(std::unique_ptr<Expression>(new Compare<EndsWith>(std::move(right), std::move(left))));
152✔
822
            case CompareType::CONTAINS:
272✔
823
                return Query(std::unique_ptr<Expression>(new Compare<Contains>(std::move(right), std::move(left))));
272✔
824
            case CompareType::LIKE:
152✔
825
                return Query(std::unique_ptr<Expression>(new Compare<Like>(std::move(right), std::move(left))));
152✔
826
            case CompareType::TEXT: {
4✔
827
                StringData val = right->get_mixed().get_string();
4✔
828
                auto string_prop = dynamic_cast<Columns<StringData>*>(left.get());
4✔
829
                return string_prop->fulltext(val);
4✔
830
            }
×
831
            case CompareType::IN:
✔
832
            case CompareType::EQUAL:
✔
833
            case CompareType::NOT_EQUAL:
✔
834
            case CompareType::GREATER:
✔
835
            case CompareType::LESS:
✔
836
            case CompareType::GREATER_EQUAL:
✔
837
            case CompareType::LESS_EQUAL:
✔
838
                break;
×
839
        }
732✔
840
    }
732✔
841
    else {
1,576✔
842
        switch (op) {
1,576✔
843
            case CompareType::BEGINSWITH:
304✔
844
                return Query(
304✔
845
                    std::unique_ptr<Expression>(new Compare<BeginsWithIns>(std::move(right), std::move(left))));
304✔
846
            case CompareType::ENDSWITH:
272✔
847
                return Query(
272✔
848
                    std::unique_ptr<Expression>(new Compare<EndsWithIns>(std::move(right), std::move(left))));
272✔
849
            case CompareType::CONTAINS:
432✔
850
                return Query(
432✔
851
                    std::unique_ptr<Expression>(new Compare<ContainsIns>(std::move(right), std::move(left))));
432✔
852
            case CompareType::LIKE:
264✔
853
                return Query(std::unique_ptr<Expression>(new Compare<LikeIns>(std::move(right), std::move(left))));
264✔
854
            case CompareType::IN:
✔
855
            case CompareType::EQUAL:
✔
856
            case CompareType::NOT_EQUAL:
✔
857
            case CompareType::GREATER:
✔
858
            case CompareType::LESS:
✔
859
            case CompareType::GREATER_EQUAL:
✔
860
            case CompareType::LESS_EQUAL:
✔
861
            case CompareType::TEXT:
✔
862
                break;
×
863
        }
1,576✔
864
    }
1,576✔
865
    return {};
×
866
}
2,308✔
867

868
#if REALM_ENABLE_GEOSPATIAL
869
Query GeoWithinNode::visit(ParserDriver* drv)
870
{
176✔
871
    auto left = prop->visit(drv);
176✔
872
    auto left_type = left->get_type();
176✔
873
    if (left_type != type_Link) {
176✔
874
        throw InvalidQueryError(util::format("The left hand side of 'geoWithin' must be a link to geoJSON formatted "
4✔
875
                                             "data. But the provided type is '%1'",
4✔
876
                                             get_data_type_name(left_type)));
4✔
877
    }
4✔
878
    auto link_column = dynamic_cast<const Columns<Link>*>(left.get());
172✔
879

880
    if (geo) {
172✔
881
        auto right = geo->visit(drv, type_Int);
124✔
882
        auto geo_value = dynamic_cast<const ConstantGeospatialValue*>(right.get());
124✔
883
        return link_column->geo_within(geo_value->get_mixed().get<Geospatial>());
124✔
884
    }
124✔
885

886
    REALM_ASSERT_3(argument.size(), >, 1);
48✔
887
    REALM_ASSERT_3(argument[0], ==, '$');
48✔
888
    size_t arg_no = size_t(strtol(argument.substr(1).c_str(), nullptr, 10));
48✔
889
    auto right_type = drv->m_args.is_argument_null(arg_no) ? DataType(-1) : drv->m_args.type_for_argument(arg_no);
48✔
890

891
    Geospatial geo_from_argument;
48✔
892
    if (right_type == type_Geospatial) {
48✔
893
        geo_from_argument = drv->m_args.geospatial_for_argument(arg_no);
16✔
894
    }
16✔
895
    else if (right_type == type_String) {
32✔
896
        // This is a "hack" to allow users to pass in geospatial objects
897
        // serialized as a string instead of as a native type. This is because
898
        // the CAPI doesn't have support for marshalling polygons (of variable length)
899
        // yet and that project was deprioritized to geospatial phase 2. This should be
900
        // removed once SDKs are all using the binding generator.
901
        std::string str_val = drv->m_args.string_for_argument(arg_no);
20✔
902
        const std::string simulated_prefix = "simulated GEOWITHIN ";
20✔
903
        str_val = simulated_prefix + str_val;
20✔
904
        ParserDriver sub_driver;
20✔
905
        try {
20✔
906
            sub_driver.parse(str_val);
20✔
907
        }
20✔
908
        catch (const std::exception& ex) {
20✔
909
            std::string doctored_err = ex.what();
8✔
910
            size_t prefix_location = doctored_err.find(simulated_prefix);
8✔
911
            if (prefix_location != std::string::npos) {
8✔
912
                doctored_err.erase(prefix_location, simulated_prefix.size());
8✔
913
            }
8✔
914
            throw InvalidQueryError(util::format(
8✔
915
                "Invalid syntax in serialized geospatial object at argument %1: '%2'", arg_no, doctored_err));
8✔
916
        }
8✔
917
        GeoWithinNode* node = dynamic_cast<GeoWithinNode*>(sub_driver.result);
12✔
918
        REALM_ASSERT(node);
12✔
919
        if (node->geo) {
12✔
920
            if (node->geo->m_geo.get_type() != Geospatial::Type::Invalid) {
12✔
921
                geo_from_argument = node->geo->m_geo;
4✔
922
            }
4✔
923
            else {
8✔
924
                geo_from_argument = GeoPolygon{node->geo->m_points};
8✔
925
            }
8✔
926
        }
12✔
927
    }
12✔
928
    else {
12✔
929
        throw InvalidQueryError(util::format("The right hand side of 'geoWithin' must be a geospatial constant "
12✔
930
                                             "value. But the provided type is '%1'",
12✔
931
                                             get_data_type_name(right_type)));
12✔
932
    }
12✔
933

934
    if (geo_from_argument.get_type() == Geospatial::Type::Invalid) {
28✔
935
        throw InvalidQueryError(util::format(
4✔
936
            "The right hand side of 'geoWithin' must be a valid Geospatial value, got '%1'", geo_from_argument));
4✔
937
    }
4✔
938
    Status geo_status = geo_from_argument.is_valid();
24✔
939
    if (!geo_status.is_ok()) {
24✔
940
        throw InvalidQueryError(
×
941
            util::format("The Geospatial query argument region is invalid: '%1'", geo_status.reason()));
×
942
    }
×
943
    return link_column->geo_within(geo_from_argument);
24✔
944
}
24✔
945
#endif
946

947
Query TrueOrFalseNode::visit(ParserDriver* drv)
948
{
612✔
949
    Query q = drv->m_base_table->where();
612✔
950
    if (true_or_false) {
612✔
951
        q.and_query(std::unique_ptr<realm::Expression>(new TrueExpression));
460✔
952
    }
460✔
953
    else {
152✔
954
        q.and_query(std::unique_ptr<realm::Expression>(new FalseExpression));
152✔
955
    }
152✔
956
    return q;
612✔
957
}
612✔
958

959
std::unique_ptr<Subexpr> PropertyNode::visit(ParserDriver* drv, DataType)
960
{
479,008✔
961
    path->resolve_arg(drv);
479,008✔
962
    if (path->path_elems.back().is_key() && path->path_elems.back().get_key() == "@links") {
479,008✔
963
        identifier = "@links";
304✔
964
        // This is a backlink aggregate query
965
        path->path_elems.pop_back();
304✔
966
        auto link_chain = path->visit(drv, comp_type);
304✔
967
        auto sub = link_chain.get_backlink_count<Int>();
304✔
968
        return sub.clone();
304✔
969
    }
304✔
970
    m_link_chain = path->visit(drv, comp_type);
478,704✔
971
    if (!path->at_end()) {
478,704✔
972
        if (!path->current_path_elem->is_key()) {
476,874✔
973
            throw InvalidQueryError(util::format("[%1] not expected", *path->current_path_elem));
4✔
974
        }
4✔
975
        identifier = path->current_path_elem->get_key();
476,870✔
976
    }
476,870✔
977
    std::unique_ptr<Subexpr> subexpr{drv->column(m_link_chain, path)};
478,700✔
978

979
    Path indexes;
478,700✔
980
    while (!path->at_end()) {
484,208✔
981
        indexes.emplace_back(std::move(*(path->current_path_elem++)));
5,508✔
982
    }
5,508✔
983

984
    if (!indexes.empty()) {
478,700✔
985
        auto ok = false;
4,152✔
986
        const PathElement& first_index = indexes.front();
4,152✔
987
        if (indexes.size() > 1 && subexpr->get_type() != type_Mixed) {
4,152✔
988
            throw InvalidQueryError("Only Property of type 'any' can have nested collections");
×
989
        }
×
990
        if (auto mixed = dynamic_cast<Columns<Mixed>*>(subexpr.get())) {
4,152✔
991
            ok = true;
744✔
992
            mixed->path(indexes);
744✔
993
        }
744✔
994
        else if (auto dict = dynamic_cast<Columns<Dictionary>*>(subexpr.get())) {
3,408✔
995
            if (first_index.is_key()) {
256✔
996
                ok = true;
196✔
997
                auto trailing = first_index.get_key();
196✔
998
                if (trailing == "@values") {
196✔
999
                }
4✔
1000
                else if (trailing == "@keys") {
192✔
1001
                    subexpr = std::make_unique<ColumnDictionaryKeys>(*dict);
52✔
1002
                }
52✔
1003
                else {
140✔
1004
                    dict->path(indexes);
140✔
1005
                }
140✔
1006
            }
196✔
1007
            else if (first_index.is_all()) {
60✔
1008
                ok = true;
52✔
1009
                dict->path(indexes);
52✔
1010
            }
52✔
1011
        }
256✔
1012
        else if (auto coll = dynamic_cast<Columns<Lst<Mixed>>*>(subexpr.get())) {
3,152✔
1013
            ok = coll->indexes(indexes);
48✔
1014
        }
48✔
1015
        else if (auto coll = dynamic_cast<ColumnListBase*>(subexpr.get())) {
3,104✔
1016
            if (indexes.size() == 1) {
3,096✔
1017
                ok = coll->index(first_index);
3,096✔
1018
            }
3,096✔
1019
        }
3,096✔
1020

1021
        if (!ok) {
4,152✔
1022
            if (first_index.is_key()) {
1,760✔
1023
                auto trailing = first_index.get_key();
1,720✔
1024
                if (!post_op && is_length_suffix(trailing)) {
1,720✔
1025
                    // If 'length' is the operator, the last id in the path must be the name
1026
                    // of a list property
1027
                    path->path_elems.pop_back();
1,704✔
1028
                    const std::string& prop = path->path_elems.back().get_key();
1,704✔
1029
                    std::unique_ptr<Subexpr> subexpr{path->visit(drv, comp_type).column(prop, false)};
1,704✔
1030
                    if (auto list = dynamic_cast<ColumnListBase*>(subexpr.get())) {
1,704✔
1031
                        if (auto length_expr = list->get_element_length())
1,704✔
1032
                            return length_expr;
1,640✔
1033
                    }
1,704✔
1034
                }
1,704✔
1035
                throw InvalidQueryError(util::format("Property '%1.%2' has no property '%3'",
80✔
1036
                                                     m_link_chain.get_current_table()->get_class_name(), identifier,
80✔
1037
                                                     trailing));
80✔
1038
            }
1,720✔
1039
            else {
40✔
1040
                throw InvalidQueryError(util::format("Property '%1.%2' does not support index '%3'",
40✔
1041
                                                     m_link_chain.get_current_table()->get_class_name(), identifier,
40✔
1042
                                                     first_index));
40✔
1043
            }
40✔
1044
        }
1,760✔
1045
    }
4,152✔
1046
    if (post_op) {
476,940✔
1047
        return post_op->visit(drv, subexpr.get());
4,352✔
1048
    }
4,352✔
1049
    return subexpr;
472,588✔
1050
}
476,940✔
1051

1052
std::unique_ptr<Subexpr> SubqueryNode::visit(ParserDriver* drv, DataType)
1053
{
304✔
1054
    if (variable_name.size() < 2 || variable_name[0] != '$') {
304✔
1055
        throw SyntaxError(util::format("The subquery variable '%1' is invalid. The variable must start with "
4✔
1056
                                       "'$' and cannot be empty; for example '$x'.",
4✔
1057
                                       variable_name));
4✔
1058
    }
4✔
1059
    LinkChain lc = prop->path->visit(drv, prop->comp_type);
300✔
1060

1061
    ColKey col_key;
300✔
1062
    std::string identifier;
300✔
1063
    if (!prop->path->at_end()) {
300✔
1064
        identifier = prop->path->next_identifier();
8✔
1065
        col_key = lc.get_current_table()->get_column_key(identifier);
8✔
1066
    }
8✔
1067
    else {
292✔
1068
        identifier = prop->path->last_identifier();
292✔
1069
        col_key = lc.get_current_col();
292✔
1070
    }
292✔
1071

1072
    auto col_type = col_key.get_type();
300✔
1073
    if (col_key.is_list() && col_type != col_type_Link) {
300✔
1074
        throw InvalidQueryError(
4✔
1075
            util::format("A subquery can not operate on a list of primitive values (property '%1')", identifier));
4✔
1076
    }
4✔
1077
    // col_key.is_list => col_type == col_type_Link
1078
    if (!(col_key.is_list() || col_type == col_type_BackLink)) {
296✔
1079
        throw InvalidQueryError(util::format("A subquery must operate on a list property, but '%1' is type '%2'",
8✔
1080
                                             identifier, realm::get_data_type_name(DataType(col_type))));
8✔
1081
    }
8✔
1082

1083
    TableRef previous_table = drv->m_base_table;
288✔
1084
    drv->m_base_table = lc.get_current_table().cast_away_const();
288✔
1085
    bool did_add = drv->m_mapping.add_mapping(drv->m_base_table, variable_name, "");
288✔
1086
    if (!did_add) {
288✔
1087
        throw InvalidQueryError(util::format("Unable to create a subquery expression with variable '%1' since an "
4✔
1088
                                             "identical variable already exists in this context",
4✔
1089
                                             variable_name));
4✔
1090
    }
4✔
1091
    Query sub = subquery->visit(drv);
284✔
1092
    drv->m_mapping.remove_mapping(drv->m_base_table, variable_name);
284✔
1093
    drv->m_base_table = previous_table;
284✔
1094

1095
    return lc.subquery(sub);
284✔
1096
}
288✔
1097

1098
std::unique_ptr<Subexpr> PostOpNode::visit(ParserDriver*, Subexpr* subexpr)
1099
{
4,352✔
1100
    if (op_type == PostOpNode::SIZE) {
4,352✔
1101
        if (auto s = dynamic_cast<Columns<Link>*>(subexpr)) {
3,584✔
1102
            return s->count().clone();
344✔
1103
        }
344✔
1104
        if (auto s = dynamic_cast<ColumnListBase*>(subexpr)) {
3,240✔
1105
            return s->size().clone();
2,928✔
1106
        }
2,928✔
1107
        if (auto s = dynamic_cast<Columns<StringData>*>(subexpr)) {
312✔
1108
            return s->size().clone();
128✔
1109
        }
128✔
1110
        if (auto s = dynamic_cast<Columns<BinaryData>*>(subexpr)) {
184✔
1111
            return s->size().clone();
48✔
1112
        }
48✔
1113
        if (auto s = dynamic_cast<Columns<Mixed>*>(subexpr)) {
136✔
1114
            return s->size().clone();
124✔
1115
        }
124✔
1116
    }
136✔
1117
    else if (op_type == PostOpNode::TYPE) {
768✔
1118
        if (auto s = dynamic_cast<Columns<Mixed>*>(subexpr)) {
768✔
1119
            return s->type_of_value().clone();
636✔
1120
        }
636✔
1121
        if (auto s = dynamic_cast<ColumnsCollection<Mixed>*>(subexpr)) {
132✔
1122
            return s->type_of_value().clone();
112✔
1123
        }
112✔
1124
        if (auto s = dynamic_cast<ObjPropertyBase*>(subexpr)) {
20✔
1125
            return Value<TypeOfValue>(TypeOfValue(s->column_key())).clone();
8✔
1126
        }
8✔
1127
        if (dynamic_cast<Columns<Link>*>(subexpr)) {
12✔
1128
            return Value<TypeOfValue>(TypeOfValue(TypeOfValue::Attribute::ObjectLink)).clone();
12✔
1129
        }
12✔
1130
    }
12✔
1131

1132
    if (subexpr) {
12✔
1133
        throw InvalidQueryError(util::format("Operation '%1' is not supported on property of type '%2'", op_name,
12✔
1134
                                             get_data_type_name(DataType(subexpr->get_type()))));
12✔
1135
    }
12✔
1136
    REALM_UNREACHABLE();
1137
    return {};
×
1138
}
12✔
1139

1140
std::unique_ptr<Subexpr> LinkAggrNode::visit(ParserDriver* drv, DataType)
1141
{
848✔
1142
    auto subexpr = property->visit(drv);
848✔
1143
    auto link_prop = dynamic_cast<Columns<Link>*>(subexpr.get());
848✔
1144
    if (!link_prop) {
848✔
1145
        throw InvalidQueryError(util::format("Operation '%1' cannot apply to property '%2' because it is not a list",
40✔
1146
                                             agg_op_type_to_str(type), property->get_identifier()));
40✔
1147
    }
40✔
1148
    const LinkChain& link_chain = property->link_chain();
808✔
1149
    prop_name = drv->translate(link_chain, prop_name);
808✔
1150
    auto col_key = link_chain.get_current_table()->get_column_key(prop_name);
808✔
1151

1152
    switch (col_key.get_type()) {
808✔
1153
        case col_type_Int:
64✔
1154
            subexpr = link_prop->column<Int>(col_key).clone();
64✔
1155
            break;
64✔
1156
        case col_type_Float:
128✔
1157
            subexpr = link_prop->column<float>(col_key).clone();
128✔
1158
            break;
128✔
1159
        case col_type_Double:
344✔
1160
            subexpr = link_prop->column<double>(col_key).clone();
344✔
1161
            break;
344✔
1162
        case col_type_Decimal:
96✔
1163
            subexpr = link_prop->column<Decimal>(col_key).clone();
96✔
1164
            break;
96✔
1165
        case col_type_Timestamp:
56✔
1166
            subexpr = link_prop->column<Timestamp>(col_key).clone();
56✔
1167
            break;
56✔
1168
        case col_type_Mixed:
64✔
1169
            subexpr = link_prop->column<Mixed>(col_key).clone();
64✔
1170
            break;
64✔
1171
        default:
48✔
1172
            throw InvalidQueryError(util::format("collection aggregate not supported for type '%1'",
48✔
1173
                                                 get_data_type_name(DataType(col_key.get_type()))));
48✔
1174
    }
808✔
1175
    return aggregate(subexpr.get());
752✔
1176
}
808✔
1177

1178
std::unique_ptr<Subexpr> ListAggrNode::visit(ParserDriver* drv, DataType)
1179
{
2,744✔
1180
    auto subexpr = property->visit(drv);
2,744✔
1181
    return aggregate(subexpr.get());
2,744✔
1182
}
2,744✔
1183

1184
std::unique_ptr<Subexpr> AggrNode::aggregate(Subexpr* subexpr)
1185
{
3,496✔
1186
    std::unique_ptr<Subexpr> agg;
3,496✔
1187
    if (auto list_prop = dynamic_cast<ColumnListBase*>(subexpr)) {
3,496✔
1188
        switch (type) {
2,712✔
1189
            case MAX:
632✔
1190
                agg = list_prop->max_of();
632✔
1191
                break;
632✔
1192
            case MIN:
608✔
1193
                agg = list_prop->min_of();
608✔
1194
                break;
608✔
1195
            case SUM:
800✔
1196
                agg = list_prop->sum_of();
800✔
1197
                break;
800✔
1198
            case AVG:
672✔
1199
                agg = list_prop->avg_of();
672✔
1200
                break;
672✔
1201
        }
2,712✔
1202
    }
2,712✔
1203
    else if (auto prop = dynamic_cast<SubColumnBase*>(subexpr)) {
784✔
1204
        switch (type) {
752✔
1205
            case MAX:
216✔
1206
                agg = prop->max_of();
216✔
1207
                break;
216✔
1208
            case MIN:
208✔
1209
                agg = prop->min_of();
208✔
1210
                break;
208✔
1211
            case SUM:
148✔
1212
                agg = prop->sum_of();
148✔
1213
                break;
148✔
1214
            case AVG:
180✔
1215
                agg = prop->avg_of();
180✔
1216
                break;
180✔
1217
        }
752✔
1218
    }
752✔
1219
    if (!agg) {
3,496✔
1220
        throw InvalidQueryError(
232✔
1221
            util::format("Cannot use aggregate '%1' for this type of property", agg_op_type_to_str(type)));
232✔
1222
    }
232✔
1223

1224
    return agg;
3,264✔
1225
}
3,496✔
1226

1227
void ConstantNode::decode_b64()
1228
{
1,432✔
1229
    const size_t encoded_size = text.size() - 5;
1,432✔
1230
    size_t buffer_size = util::base64_decoded_size(encoded_size);
1,432✔
1231
    m_decode_buffer.resize(buffer_size);
1,432✔
1232
    StringData window(text.c_str() + 4, encoded_size);
1,432✔
1233
    util::Optional<size_t> decoded_size = util::base64_decode(window, m_decode_buffer);
1,432✔
1234
    if (!decoded_size) {
1,432✔
1235
        throw SyntaxError("Invalid base64 value");
×
1236
    }
×
1237
    REALM_ASSERT_DEBUG_EX(*decoded_size <= encoded_size, *decoded_size, encoded_size);
1,432✔
1238
    m_decode_buffer.resize(*decoded_size); // truncate
1,432✔
1239
}
1,432✔
1240

1241
Mixed ConstantNode::get_value()
1242
{
42,660✔
1243
    switch (type) {
42,660✔
1244
        case Type::NUMBER:
24,590✔
1245
            return int64_t(strtoll(text.c_str(), nullptr, 0));
24,590✔
1246
        case Type::FLOAT:
2,152✔
1247
            if (text[text.size() - 1] == 'f') {
2,152✔
1248
                return strtof(text.c_str(), nullptr);
4✔
1249
            }
4✔
1250
            return strtod(text.c_str(), nullptr);
2,148✔
1251
        case Type::INFINITY_VAL: {
152✔
1252
            bool negative = text[0] == '-';
152✔
1253
            constexpr auto inf = std::numeric_limits<double>::infinity();
152✔
1254
            return negative ? -inf : inf;
152✔
1255
        }
2,152✔
1256
        case Type::NAN_VAL:
64✔
1257
            return type_punning<double>(0x7ff8000000000000);
64✔
1258
        case Type::STRING:
8,316✔
1259
            return StringData(text.data() + 1, text.size() - 2);
8,316✔
1260
        case Type::STRING_BASE64:
716✔
1261
            decode_b64();
716✔
1262
            return StringData(m_decode_buffer.data(), m_decode_buffer.size());
716✔
1263
        case Type::TIMESTAMP: {
612✔
1264
            auto s = text;
612✔
1265
            int64_t seconds;
612✔
1266
            int32_t nanoseconds;
612✔
1267
            if (s[0] == 'T') {
612✔
1268
                size_t colon_pos = s.find(":");
536✔
1269
                std::string s1 = s.substr(1, colon_pos - 1);
536✔
1270
                std::string s2 = s.substr(colon_pos + 1);
536✔
1271
                seconds = strtol(s1.c_str(), nullptr, 0);
536✔
1272
                nanoseconds = int32_t(strtol(s2.c_str(), nullptr, 0));
536✔
1273
            }
536✔
1274
            else {
76✔
1275
                // readable format YYYY-MM-DD-HH:MM:SS:NANOS nanos optional
1276
                struct tm tmp = tm();
76✔
1277
                char sep = s.find("@") < s.size() ? '@' : 'T';
76✔
1278
                std::string fmt = "%d-%d-%d"s + sep + "%d:%d:%d:%d"s;
76✔
1279
                int cnt = sscanf(s.c_str(), fmt.c_str(), &tmp.tm_year, &tmp.tm_mon, &tmp.tm_mday, &tmp.tm_hour,
76✔
1280
                                 &tmp.tm_min, &tmp.tm_sec, &nanoseconds);
76✔
1281
                REALM_ASSERT(cnt >= 6);
76✔
1282
                tmp.tm_year -= 1900; // epoch offset (see man mktime)
76✔
1283
                tmp.tm_mon -= 1;     // converts from 1-12 to 0-11
76✔
1284

1285
                if (tmp.tm_year < 0) {
76✔
1286
                    // platform timegm functions do not throw errors, they return -1 which is also a valid time
1287
                    throw InvalidQueryError("Conversion of dates before 1900 is not supported.");
16✔
1288
                }
16✔
1289

1290
                seconds = platform_timegm(tmp); // UTC time
60✔
1291
                if (cnt == 6) {
60✔
1292
                    nanoseconds = 0;
32✔
1293
                }
32✔
1294
                if (nanoseconds < 0) {
60✔
1295
                    throw SyntaxError("The nanoseconds of a Timestamp cannot be negative.");
×
1296
                }
×
1297
                if (seconds < 0) { // seconds determines the sign of the nanoseconds part
60✔
1298
                    nanoseconds *= -1;
24✔
1299
                }
24✔
1300
            }
60✔
1301
            return get_timestamp_if_valid(seconds, nanoseconds);
596✔
1302
        }
612✔
1303
        case Type::UUID_T:
672✔
1304
            return UUID(text.substr(5, text.size() - 6));
672✔
1305
        case Type::OID:
772✔
1306
            return ObjectId(text.substr(4, text.size() - 5).c_str());
772✔
1307
        case Type::LINK:
8✔
1308
            return ObjKey(strtol(text.substr(1, text.size() - 1).c_str(), nullptr, 0));
8✔
1309
        case Type::TYPED_LINK: {
48✔
1310
            size_t colon_pos = text.find(":");
48✔
1311
            auto table_key_val = uint32_t(strtol(text.substr(1, colon_pos - 1).c_str(), nullptr, 0));
48✔
1312
            auto obj_key_val = strtol(text.substr(colon_pos + 1).c_str(), nullptr, 0);
48✔
1313
            return ObjLink(TableKey(table_key_val), ObjKey(obj_key_val));
48✔
1314
        }
612✔
1315
        case Type::NULL_VAL:
2,300✔
1316
            return {};
2,300✔
1317
        case Type::TRUE:
160✔
1318
            return {true};
160✔
1319
        case Type::FALSE:
312✔
1320
            return {false};
312✔
1321
        case Type::ARG:
✔
1322
            break;
×
1323
        case BINARY_STR: {
1,068✔
1324
            return BinaryData(text.data() + 1, text.size() - 2);
1,068✔
1325
        }
612✔
1326
        case BINARY_BASE64:
716✔
1327
            decode_b64();
716✔
1328
            return BinaryData(m_decode_buffer.data(), m_decode_buffer.size());
716✔
1329
    }
42,660✔
1330
    return {};
×
1331
}
42,660✔
1332

1333
std::unique_ptr<Subexpr> ConstantNode::visit(ParserDriver* drv, DataType hint)
1334
{
475,818✔
1335
    std::unique_ptr<Subexpr> ret;
475,818✔
1336
    std::string explain_value_message = text;
475,818✔
1337
    Mixed value;
475,818✔
1338

1339
    auto convert_if_needed = [&](Mixed& value) -> void {
475,818✔
1340
        switch (value.get_type()) {
473,292✔
1341
            case type_Int:
25,558✔
1342
                if (hint == type_Decimal) {
25,558✔
1343
                    value = Decimal128(value.get_int());
1,296✔
1344
                }
1,296✔
1345
                break;
25,558✔
1346
            case type_Double: {
2,928✔
1347
                auto double_val = value.get_double();
2,928✔
1348
                if (std::isinf(double_val) && (!Mixed::is_numeric(hint) || hint == type_Int)) {
2,928✔
1349
                    throw InvalidQueryError(util::format("Infinity not supported for %1", get_data_type_name(hint)));
8✔
1350
                }
8✔
1351

1352
                switch (hint) {
2,920✔
1353
                    case type_Float:
184✔
1354
                        value = float(double_val);
184✔
1355
                        break;
184✔
1356
                    case type_Decimal:
480✔
1357
                        // If not argument, try decode again to get full precision
1358
                        value = (type == Type::ARG) ? Decimal128(double_val) : Decimal128(text);
480✔
1359
                        break;
480✔
1360
                    case type_Int: {
80✔
1361
                        int64_t int_val = int64_t(double_val);
80✔
1362
                        // Only return an integer if it precisely represents val
1363
                        if (double(int_val) == double_val) {
80✔
1364
                            value = int_val;
24✔
1365
                        }
24✔
1366
                        break;
80✔
1367
                    }
×
1368
                    default:
2,176✔
1369
                        break;
2,176✔
1370
                }
2,920✔
1371
                break;
2,920✔
1372
            }
2,920✔
1373
            case type_Float: {
2,920✔
1374
                if (hint == type_Int) {
476✔
1375
                    float float_val = value.get_float();
52✔
1376
                    if (std::isinf(float_val)) {
52✔
1377
                        throw InvalidQueryError(
8✔
1378
                            util::format("Infinity not supported for %1", get_data_type_name(hint)));
8✔
1379
                    }
8✔
1380
                    if (std::isnan(float_val)) {
44✔
1381
                        throw InvalidQueryError(util::format("NaN not supported for %1", get_data_type_name(hint)));
8✔
1382
                    }
8✔
1383
                    int64_t int_val = int64_t(float_val);
36✔
1384
                    if (float(int_val) == float_val) {
36✔
1385
                        value = int_val;
16✔
1386
                    }
16✔
1387
                }
36✔
1388
                break;
460✔
1389
            }
476✔
1390
            case type_String: {
9,448✔
1391
                StringData str = value.get_string();
9,448✔
1392
                switch (hint) {
9,448✔
1393
                    case type_Int:
420✔
1394
                        value = string_to<int64_t>(str);
420✔
1395
                        break;
420✔
1396
                    case type_Float:
8✔
1397
                        value = string_to<float>(str);
8✔
1398
                        break;
8✔
1399
                    case type_Double:
8✔
1400
                        value = string_to<double>(str);
8✔
1401
                        break;
8✔
1402
                    case type_Decimal:
4✔
1403
                        value = string_to<Decimal128>(str);
4✔
1404
                        break;
4✔
1405
                    default:
9,008✔
1406
                        break;
9,008✔
1407
                }
9,448✔
1408
                break;
9,412✔
1409
            }
9,448✔
1410
            default:
434,876✔
1411
                break;
434,876✔
1412
        }
473,292✔
1413
    };
473,292✔
1414

1415
    if (type == Type::ARG) {
475,818✔
1416
        size_t arg_no = size_t(strtol(text.substr(1).c_str(), nullptr, 10));
433,164✔
1417
        if (m_comp_type && !drv->m_args.is_argument_list(arg_no)) {
433,164✔
1418
            throw InvalidQueryError(util::format(
12✔
1419
                "ANY/ALL/NONE are only allowed on arguments which contain a list but '%1' is not a list.",
12✔
1420
                explain_value_message));
12✔
1421
        }
12✔
1422
        if (drv->m_args.is_argument_list(arg_no)) {
433,152✔
1423
            std::vector<Mixed> mixed_list = drv->m_args.list_for_argument(arg_no);
160✔
1424
            for (auto& mixed : mixed_list) {
408✔
1425
                if (!mixed.is_null()) {
408✔
1426
                    convert_if_needed(mixed);
396✔
1427
                }
396✔
1428
            }
408✔
1429
            return copy_list_of_args(mixed_list);
160✔
1430
        }
160✔
1431
        if (drv->m_args.is_argument_null(arg_no)) {
432,992✔
1432
            explain_value_message = util::format("argument '%1' which is NULL", explain_value_message);
256✔
1433
        }
256✔
1434
        else {
432,736✔
1435
            value = drv->m_args.mixed_for_argument(arg_no);
432,736✔
1436
            if (value.is_null()) {
432,736✔
1437
                explain_value_message = util::format("argument %1 of type null", explain_value_message);
4✔
1438
            }
4✔
1439
            else if (value.is_type(type_TypedLink)) {
432,732✔
1440
                explain_value_message =
36✔
1441
                    util::format("%1 which links to %2", explain_value_message,
36✔
1442
                                 print_pretty_objlink(value.get<ObjLink>(), drv->m_base_table->get_parent_group()));
36✔
1443
            }
36✔
1444
            else {
432,696✔
1445
                explain_value_message = util::format("argument %1 with value '%2'", explain_value_message, value);
432,696✔
1446
                if (!(m_target_table || Mixed::data_types_are_comparable(value.get_type(), hint) ||
432,696✔
1447
                      Mixed::is_numeric(hint) || (value.is_type(type_String) && hint == type_TypeOfValue))) {
432,696✔
1448
                    throw InvalidQueryArgError(
124✔
1449
                        util::format("Cannot compare %1 to a %2", explain_value_message, get_data_type_name(hint)));
124✔
1450
                }
124✔
1451
            }
432,696✔
1452
        }
432,736✔
1453
    }
432,992✔
1454
    else {
42,654✔
1455
        value = get_value();
42,654✔
1456
    }
42,654✔
1457

1458
    if (m_target_table) {
475,522✔
1459
        // There is a table name set. This must be an ObjLink
1460
        const Group* g = drv->m_base_table->get_parent_group();
112✔
1461
        auto table = g->get_table(m_target_table);
112✔
1462
        if (!table) {
112✔
1463
            // Perhaps class prefix is missing
1464
            Group::TableNameBuffer buffer;
8✔
1465
            table = g->get_table(Group::class_name_to_table_name(m_target_table, buffer));
8✔
1466
        }
8✔
1467
        if (!table) {
112✔
1468
            throw InvalidQueryError(util::format("Unknown object type '%1'", m_target_table));
×
1469
        }
×
1470
        auto obj_key = table->find_primary_key(value);
112✔
1471
        value = ObjLink(table->get_key(), ObjKey(obj_key));
112✔
1472
    }
112✔
1473

1474
    if (value.is_null()) {
475,522✔
1475
        if (hint == type_String) {
2,560✔
1476
            return std::make_unique<ConstantStringValue>(StringData()); // Null string
348✔
1477
        }
348✔
1478
        else if (hint == type_Binary) {
2,212✔
1479
            return std::make_unique<Value<Binary>>(BinaryData()); // Null string
320✔
1480
        }
320✔
1481
        else {
1,892✔
1482
            return std::make_unique<Value<null>>(realm::null());
1,892✔
1483
        }
1,892✔
1484
    }
2,560✔
1485

1486
    convert_if_needed(value);
472,962✔
1487

1488
    switch (value.get_type()) {
472,962✔
1489
        case type_Int: {
24,508✔
1490
            ret = std::make_unique<Value<int64_t>>(value.get_int());
24,508✔
1491
            break;
24,508✔
1492
        }
×
1493
        case type_Float: {
616✔
1494
            ret = std::make_unique<Value<float>>(value.get_float());
616✔
1495
            break;
616✔
1496
        }
×
1497
        case type_Decimal:
2,264✔
1498
            ret = std::make_unique<Value<Decimal128>>(value.get_decimal());
2,264✔
1499
            break;
2,264✔
1500
        case type_Double: {
2,204✔
1501
            ret = std::make_unique<Value<double>>(value.get_double());
2,204✔
1502
            break;
2,204✔
1503
        }
×
1504
        case type_String: {
8,888✔
1505
            StringData str = value.get_string();
8,888✔
1506
            if (hint == type_TypeOfValue) {
8,888✔
1507
                TypeOfValue type_of_value(std::string_view(str.data(), str.size()));
680✔
1508
                ret = std::make_unique<Value<TypeOfValue>>(type_of_value);
680✔
1509
            }
680✔
1510
            else {
8,208✔
1511
                ret = std::make_unique<ConstantStringValue>(str);
8,208✔
1512
            }
8,208✔
1513
            break;
8,888✔
1514
        }
×
1515
        case type_Timestamp:
960✔
1516
            ret = std::make_unique<Value<Timestamp>>(value.get_timestamp());
960✔
1517
            break;
960✔
1518
        case type_UUID:
1,064✔
1519
            ret = std::make_unique<Value<UUID>>(value.get_uuid());
1,064✔
1520
            break;
1,064✔
1521
        case type_ObjectId:
429,392✔
1522
            ret = std::make_unique<Value<ObjectId>>(value.get_object_id());
429,392✔
1523
            break;
429,392✔
1524
        case type_Link:
20✔
1525
            ret = std::make_unique<Value<ObjKey>>(value.get<ObjKey>());
20✔
1526
            break;
20✔
1527
        case type_TypedLink:
196✔
1528
            ret = std::make_unique<Value<ObjLink>>(value.get<ObjLink>());
196✔
1529
            break;
196✔
1530
        case type_Bool:
776✔
1531
            ret = std::make_unique<Value<Bool>>(value.get_bool());
776✔
1532
            break;
776✔
1533
        case type_Binary:
1,948✔
1534
            ret = std::make_unique<ConstantBinaryValue>(value.get_binary());
1,948✔
1535
            break;
1,948✔
1536
        case type_Mixed:
✔
1537
            break;
×
1538
    }
472,962✔
1539
    if (!ret) {
472,828✔
1540
        throw InvalidQueryError(
×
1541
            util::format("Unsupported comparison between property of type '%1' and constant value: %2",
×
1542
                         get_data_type_name(hint), explain_value_message));
×
1543
    }
×
1544
    return ret;
472,828✔
1545
}
472,828✔
1546

1547
std::unique_ptr<ConstantMixedList> ConstantNode::copy_list_of_args(std::vector<Mixed>& mixed_args)
1548
{
160✔
1549
    std::unique_ptr<ConstantMixedList> args_in_list = std::make_unique<ConstantMixedList>(mixed_args.size());
160✔
1550
    size_t ndx = 0;
160✔
1551
    for (const auto& mixed : mixed_args) {
408✔
1552
        args_in_list->set(ndx++, mixed);
408✔
1553
    }
408✔
1554
    if (m_comp_type) {
160✔
1555
        args_in_list->set_comparison_type(*m_comp_type);
20✔
1556
    }
20✔
1557
    return args_in_list;
160✔
1558
}
160✔
1559

1560
Mixed Arguments::mixed_for_argument(size_t arg_no)
1561
{
3,988✔
1562
    switch (type_for_argument(arg_no)) {
3,988✔
1563
        case type_Int:
464✔
1564
            return int64_t(long_for_argument(arg_no));
464✔
1565
        case type_String:
380✔
1566
            return string_for_argument(arg_no);
380✔
1567
        case type_Binary:
176✔
1568
            return binary_for_argument(arg_no);
176✔
1569
        case type_Bool:
316✔
1570
            return bool_for_argument(arg_no);
316✔
1571
        case type_Float:
436✔
1572
            return float_for_argument(arg_no);
436✔
1573
        case type_Double:
440✔
1574
            return double_for_argument(arg_no);
440✔
1575
        case type_Timestamp:
392✔
1576
            try {
392✔
1577
                return timestamp_for_argument(arg_no);
392✔
1578
            }
392✔
1579
            catch (const std::exception&) {
392✔
1580
            }
×
1581
            return objectid_for_argument(arg_no);
×
1582
        case type_ObjectId:
524✔
1583
            try {
524✔
1584
                return objectid_for_argument(arg_no);
524✔
1585
            }
524✔
1586
            catch (const std::exception&) {
524✔
1587
            }
×
1588
            return timestamp_for_argument(arg_no);
×
1589
        case type_Decimal:
420✔
1590
            return decimal128_for_argument(arg_no);
420✔
1591
        case type_UUID:
388✔
1592
            return uuid_for_argument(arg_no);
388✔
1593
        case type_Link:
12✔
1594
            return object_index_for_argument(arg_no);
12✔
1595
        case type_TypedLink:
40✔
1596
            return objlink_for_argument(arg_no);
40✔
1597
        default:
✔
1598
            break;
×
1599
    }
3,988✔
1600
    return {};
×
1601
}
3,988✔
1602

1603
#if REALM_ENABLE_GEOSPATIAL
1604
GeospatialNode::GeospatialNode(GeospatialNode::Box, GeoPoint& p1, GeoPoint& p2)
1605
    : m_geo{Geospatial{GeoBox{p1, p2}}}
10✔
1606
{
20✔
1607
}
20✔
1608

1609
GeospatialNode::GeospatialNode(Circle, GeoPoint& p, double radius)
1610
    : m_geo{Geospatial{GeoCircle{radius, p}}}
30✔
1611
{
60✔
1612
}
60✔
1613

1614
GeospatialNode::GeospatialNode(Polygon, GeoPoint& p)
1615
    : m_points({{p}})
1616
{
×
1617
}
×
1618

1619
GeospatialNode::GeospatialNode(Loop, GeoPoint& p)
1620
    : m_points({{p}})
34✔
1621
{
68✔
1622
}
68✔
1623

1624
void GeospatialNode::add_point_to_loop(GeoPoint& p)
1625
{
272✔
1626
    m_points.back().push_back(p);
272✔
1627
}
272✔
1628

1629
void GeospatialNode::add_loop_to_polygon(GeospatialNode* node)
1630
{
8✔
1631
    m_points.push_back(node->m_points.back());
8✔
1632
}
8✔
1633

1634
std::unique_ptr<Subexpr> GeospatialNode::visit(ParserDriver*, DataType)
1635
{
124✔
1636
    std::unique_ptr<Subexpr> ret;
124✔
1637
    if (m_geo.get_type() != Geospatial::Type::Invalid) {
124✔
1638
        ret = std::make_unique<ConstantGeospatialValue>(m_geo);
72✔
1639
    }
72✔
1640
    else {
52✔
1641
        ret = std::make_unique<ConstantGeospatialValue>(GeoPolygon{m_points});
52✔
1642
    }
52✔
1643
    return ret;
124✔
1644
}
124✔
1645
#endif
1646

1647
std::unique_ptr<Subexpr> ListNode::visit(ParserDriver* drv, DataType hint)
1648
{
2,440✔
1649
    if (hint == type_TypeOfValue) {
2,440✔
1650
        try {
12✔
1651
            std::unique_ptr<Value<TypeOfValue>> ret = std::make_unique<Value<TypeOfValue>>();
12✔
1652
            constexpr bool is_list = true;
12✔
1653
            ret->init(is_list, elements.size());
12✔
1654
            ret->set_comparison_type(m_comp_type);
12✔
1655
            size_t ndx = 0;
12✔
1656
            for (auto constant : elements) {
24✔
1657
                std::unique_ptr<Subexpr> evaluated = constant->visit(drv, hint);
24✔
1658
                if (auto converted = dynamic_cast<Value<TypeOfValue>*>(evaluated.get())) {
24✔
1659
                    ret->set(ndx++, converted->get(0));
20✔
1660
                }
20✔
1661
                else {
4✔
1662
                    throw InvalidQueryError(util::format("Invalid constant inside constant list: %1",
4✔
1663
                                                         evaluated->description(drv->m_serializer_state)));
4✔
1664
                }
4✔
1665
            }
24✔
1666
            return ret;
8✔
1667
        }
12✔
1668
        catch (const std::runtime_error& e) {
12✔
1669
            throw InvalidQueryArgError(e.what());
×
1670
        }
×
1671
    }
12✔
1672

1673
    auto ret = std::make_unique<ConstantMixedList>(elements.size());
2,428✔
1674
    ret->set_comparison_type(m_comp_type);
2,428✔
1675
    size_t ndx = 0;
2,428✔
1676
    for (auto constant : elements) {
4,752✔
1677
        auto evaulated_constant = constant->visit(drv, hint);
4,752✔
1678
        if (auto value = dynamic_cast<const ValueBase*>(evaulated_constant.get())) {
4,752✔
1679
            REALM_ASSERT_EX(value->size() == 1, value->size());
4,752✔
1680
            ret->set(ndx++, value->get(0));
4,752✔
1681
        }
4,752✔
1682
        else {
×
1683
            throw InvalidQueryError("Invalid constant inside constant list");
×
1684
        }
×
1685
    }
4,752✔
1686
    return ret;
2,428✔
1687
}
2,428✔
1688

1689
void PathNode::resolve_arg(ParserDriver* drv)
1690
{
479,006✔
1691
    if (arg.size()) {
479,006✔
1692
        if (path_elems.size()) {
32✔
1693
            throw InvalidQueryError("Key path argument cannot be mixed with other elements");
×
1694
        }
×
1695
        auto arg_str = drv->get_arg_for_key_path(arg);
32✔
1696
        const char* path = arg_str.data();
32✔
1697
        do {
44✔
1698
            auto p = find_chr(path, '.');
44✔
1699
            StringData elem(path, p - path);
44✔
1700
            add_element(elem);
44✔
1701
            path = p;
44✔
1702
        } while (*path++ == '.');
44✔
1703
    }
32✔
1704
}
479,006✔
1705

1706
LinkChain PathNode::visit(ParserDriver* drv, util::Optional<ExpressionComparisonType> comp_type)
1707
{
480,994✔
1708
    LinkChain link_chain(drv->m_base_table, comp_type);
480,994✔
1709
    for (current_path_elem = path_elems.begin(); current_path_elem != path_elems.end(); ++current_path_elem) {
498,038✔
1710
        if (current_path_elem->is_key()) {
495,632✔
1711
            const std::string& raw_path_elem = current_path_elem->get_key();
495,544✔
1712
            auto path_elem = drv->translate(link_chain, raw_path_elem);
495,544✔
1713
            if (path_elem.find("@links.") == 0) {
495,544✔
1714
                std::string_view table_column_pair(path_elem);
504✔
1715
                table_column_pair = table_column_pair.substr(7);
504✔
1716
                auto dot_pos = table_column_pair.find('.');
504✔
1717
                auto table_name = table_column_pair.substr(0, dot_pos);
504✔
1718
                auto column_name = table_column_pair.substr(dot_pos + 1);
504✔
1719
                drv->backlink(link_chain, table_name, column_name);
504✔
1720
                continue;
504✔
1721
            }
504✔
1722
            if (path_elem == "@values") {
495,040✔
1723
                if (!link_chain.get_current_col().is_dictionary()) {
24✔
1724
                    throw InvalidQueryError("@values only allowed on dictionaries");
×
1725
                }
×
1726
                continue;
24✔
1727
            }
24✔
1728
            if (path_elem.empty()) {
495,016✔
1729
                continue; // this element has been removed, this happens in subqueries
284✔
1730
            }
284✔
1731

1732
            // Check if it is a link
1733
            if (link_chain.link(path_elem)) {
494,732✔
1734
                continue;
16,096✔
1735
            }
16,096✔
1736
            // The next identifier being a property on the linked to object takes precedence
1737
            if (link_chain.get_current_table()->get_column_key(path_elem)) {
478,636✔
1738
                break;
478,456✔
1739
            }
478,456✔
1740
        }
478,636✔
1741
        if (!link_chain.index(*current_path_elem))
268✔
1742
            break;
132✔
1743
    }
268✔
1744
    return link_chain;
480,994✔
1745
}
480,994✔
1746

1747
DescriptorNode::~DescriptorNode() {}
1,404✔
1748

1749
DescriptorOrderingNode::~DescriptorOrderingNode() {}
46,202✔
1750

1751
std::unique_ptr<DescriptorOrdering> DescriptorOrderingNode::visit(ParserDriver* drv)
1752
{
43,790✔
1753
    auto target = drv->m_base_table;
43,790✔
1754
    std::unique_ptr<DescriptorOrdering> ordering;
43,790✔
1755
    for (auto cur_ordering : orderings) {
43,790✔
1756
        if (!ordering)
1,140✔
1757
            ordering = std::make_unique<DescriptorOrdering>();
708✔
1758
        if (cur_ordering->get_type() == DescriptorNode::LIMIT) {
1,140✔
1759
            ordering->append_limit(LimitDescriptor(cur_ordering->limit));
340✔
1760
        }
340✔
1761
        else {
800✔
1762
            bool is_distinct = cur_ordering->get_type() == DescriptorNode::DISTINCT;
800✔
1763
            std::vector<std::vector<ExtendedColumnKey>> property_columns;
800✔
1764
            for (Path& path : cur_ordering->columns) {
836✔
1765
                std::vector<ExtendedColumnKey> columns;
836✔
1766
                LinkChain link_chain(target);
836✔
1767
                ColKey col_key;
836✔
1768
                for (size_t ndx_in_path = 0; ndx_in_path < path.size(); ++ndx_in_path) {
1,780✔
1769
                    std::string prop_name = drv->translate(link_chain, path[ndx_in_path].get_key());
952✔
1770
                    // If last column was a dictionary, We will treat the next entry as a key to
1771
                    // the dictionary
1772
                    if (col_key && col_key.is_dictionary()) {
952✔
1773
                        columns.back().set_index(prop_name);
32✔
1774
                    }
32✔
1775
                    else {
920✔
1776
                        col_key = link_chain.get_current_table()->get_column_key(prop_name);
920✔
1777
                        if (!col_key) {
920✔
1778
                            throw InvalidQueryError(util::format(
8✔
1779
                                "No property '%1' found on object type '%2' specified in '%3' clause", prop_name,
8✔
1780
                                link_chain.get_current_table()->get_class_name(), is_distinct ? "distinct" : "sort"));
8✔
1781
                        }
8✔
1782
                        columns.emplace_back(col_key);
912✔
1783
                        if (ndx_in_path < path.size() - 1) {
912✔
1784
                            link_chain.link(col_key);
116✔
1785
                        }
116✔
1786
                    }
912✔
1787
                }
952✔
1788
                property_columns.push_back(columns);
828✔
1789
            }
828✔
1790

1791
            if (is_distinct) {
792✔
1792
                ordering->append_distinct(DistinctDescriptor(property_columns));
252✔
1793
            }
252✔
1794
            else {
540✔
1795
                ordering->append_sort(SortDescriptor(property_columns, cur_ordering->ascending),
540✔
1796
                                      SortDescriptor::MergeMode::prepend);
540✔
1797
            }
540✔
1798
        }
792✔
1799
    }
1,140✔
1800

1801
    return ordering;
43,782✔
1802
}
43,790✔
1803

1804
// If one of the expresions is constant, it should be right
1805
static void verify_conditions(Subexpr* left, Subexpr* right, util::serializer::SerialisationState& state)
1806
{
475,088✔
1807
    if (dynamic_cast<ColumnListBase*>(left) && dynamic_cast<ColumnListBase*>(right)) {
475,088✔
1808
        throw InvalidQueryError(
32✔
1809
            util::format("Ordered comparison between two primitive lists is not implemented yet ('%1' and '%2')",
32✔
1810
                         left->description(state), right->description(state)));
32✔
1811
    }
32✔
1812
    if (dynamic_cast<Value<TypeOfValue>*>(left) && dynamic_cast<Value<TypeOfValue>*>(right)) {
475,056✔
1813
        throw InvalidQueryError(util::format("Comparison between two constants is not supported ('%1' and '%2')",
8✔
1814
                                             left->description(state), right->description(state)));
8✔
1815
    }
8✔
1816
    if (auto link_column = dynamic_cast<Columns<Link>*>(left)) {
475,048✔
1817
        if (link_column->has_multiple_values() && right->has_single_value() && right->get_mixed().is_null()) {
428✔
1818
            throw InvalidQueryError(
20✔
1819
                util::format("Cannot compare linklist ('%1') with NULL", left->description(state)));
20✔
1820
        }
20✔
1821
    }
428✔
1822
}
475,048✔
1823

1824
ParserDriver::ParserDriver(TableRef t, Arguments& args, const query_parser::KeyPathMapping& mapping)
1825
    : m_base_table(t)
23,332✔
1826
    , m_args(args)
23,332✔
1827
    , m_mapping(mapping)
23,332✔
1828
{
46,662✔
1829
    yylex_init(&m_yyscanner);
46,662✔
1830
}
46,662✔
1831

1832
ParserDriver::~ParserDriver()
1833
{
46,652✔
1834
    yylex_destroy(m_yyscanner);
46,652✔
1835
}
46,652✔
1836

1837
PathElement ParserDriver::get_arg_for_index(const std::string& i)
1838
{
4✔
1839
    REALM_ASSERT(i[0] == '$');
4✔
1840
    size_t arg_no = size_t(strtol(i.substr(1).c_str(), nullptr, 10));
4✔
1841
    if (m_args.is_argument_null(arg_no) || m_args.is_argument_list(arg_no)) {
4✔
1842
        throw InvalidQueryError("Invalid index parameter");
×
1843
    }
×
1844
    auto type = m_args.type_for_argument(arg_no);
4✔
1845
    switch (type) {
4✔
1846
        case type_Int:
✔
1847
            return size_t(m_args.long_for_argument(arg_no));
×
1848
        case type_String:
4✔
1849
            return m_args.string_for_argument(arg_no);
4✔
1850
        default:
✔
1851
            throw InvalidQueryError("Invalid index type");
×
1852
    }
4✔
1853
}
4✔
1854

1855
std::string ParserDriver::get_arg_for_key_path(const std::string& i)
1856
{
32✔
1857
    REALM_ASSERT(i[0] == '$');
32✔
1858
    REALM_ASSERT(i[1] == 'K');
32✔
1859
    size_t arg_no = size_t(strtol(i.substr(2).c_str(), nullptr, 10));
32✔
1860
    if (m_args.is_argument_null(arg_no) || m_args.is_argument_list(arg_no)) {
32✔
1861
        throw InvalidQueryArgError(util::format("Null or list cannot be used for parameter '%1'", i));
4✔
1862
    }
4✔
1863
    auto type = m_args.type_for_argument(arg_no);
28✔
1864
    if (type != type_String) {
28✔
1865
        throw InvalidQueryArgError(util::format("Invalid index type for '%1'. Expected a string, but found type '%2'",
4✔
1866
                                                i, get_data_type_name(type)));
4✔
1867
    }
4✔
1868
    return m_args.string_for_argument(arg_no);
24✔
1869
}
28✔
1870

1871
double ParserDriver::get_arg_for_coordinate(const std::string& str)
1872
{
80✔
1873
    REALM_ASSERT(str[0] == '$');
80✔
1874
    size_t arg_no = size_t(strtol(str.substr(1).c_str(), nullptr, 10));
80✔
1875
    if (m_args.is_argument_null(arg_no)) {
80✔
1876
        throw InvalidQueryError(util::format("NULL cannot be used in coordinate at argument '%1'", str));
4✔
1877
    }
4✔
1878
    if (m_args.is_argument_list(arg_no)) {
76✔
1879
        throw InvalidQueryError(util::format("A list cannot be used in a coordinate at argument '%1'", str));
×
1880
    }
×
1881

1882
    auto type = m_args.type_for_argument(arg_no);
76✔
1883
    switch (type) {
76✔
1884
        case type_Int:
✔
1885
            return double(m_args.long_for_argument(arg_no));
×
1886
        case type_Double:
68✔
1887
            return m_args.double_for_argument(arg_no);
68✔
1888
        case type_Float:
✔
1889
            return double(m_args.float_for_argument(arg_no));
×
1890
        default:
8✔
1891
            throw InvalidQueryError(util::format("Invalid parameter '%1' used in coordinate at argument '%2'",
8✔
1892
                                                 get_data_type_name(type), str));
8✔
1893
    }
76✔
1894
}
76✔
1895

1896
auto ParserDriver::cmp(const std::vector<ExpressionNode*>& values) -> std::pair<SubexprPtr, SubexprPtr>
1897
{
476,068✔
1898
    SubexprPtr left;
476,068✔
1899
    SubexprPtr right;
476,068✔
1900

1901
    auto left_is_constant = values[0]->is_constant();
476,068✔
1902
    auto right_is_constant = values[1]->is_constant();
476,068✔
1903

1904
    if (left_is_constant && right_is_constant) {
476,068✔
1905
        throw InvalidQueryError("Cannot compare two constants");
×
1906
    }
×
1907

1908
    if (right_is_constant) {
476,068✔
1909
        // Take left first - it cannot be a constant
1910
        left = values[0]->visit(this);
472,008✔
1911
        right = values[1]->visit(this, left->get_type());
472,008✔
1912
        verify_conditions(left.get(), right.get(), m_serializer_state);
472,008✔
1913
    }
472,008✔
1914
    else {
4,060✔
1915
        right = values[1]->visit(this);
4,060✔
1916
        if (left_is_constant) {
4,060✔
1917
            left = values[0]->visit(this, right->get_type());
1,492✔
1918
        }
1,492✔
1919
        else {
2,568✔
1920
            left = values[0]->visit(this);
2,568✔
1921
        }
2,568✔
1922
        verify_conditions(right.get(), left.get(), m_serializer_state);
4,060✔
1923
    }
4,060✔
1924
    return {std::move(left), std::move(right)};
476,068✔
1925
}
476,068✔
1926

1927
auto ParserDriver::column(LinkChain& link_chain, PathNode* path) -> SubexprPtr
1928
{
478,664✔
1929
    if (path->at_end()) {
478,664✔
1930
        // This is a link property. We can optimize by usingColumns<Link>.
1931
        // However Columns<Link> does not handle @keys and indexes
1932
        auto extended_col_key = link_chain.m_link_cols.back();
1,796✔
1933
        if (!extended_col_key.has_index()) {
1,796✔
1934
            return link_chain.create_subexpr<Link>(ColKey(extended_col_key));
1,792✔
1935
        }
1,792✔
1936
        link_chain.pop_back();
4✔
1937
        --path->current_path_elem;
4✔
1938
        --path->current_path_elem;
4✔
1939
    }
4✔
1940
    auto identifier = m_mapping.translate(link_chain, path->next_identifier());
476,872✔
1941
    if (auto col = link_chain.column(identifier, !path->at_end())) {
476,872✔
1942
        return col;
476,712✔
1943
    }
476,712✔
1944
    throw InvalidQueryError(
160✔
1945
        util::format("'%1' has no property '%2'", link_chain.get_current_table()->get_class_name(), identifier));
160✔
1946
}
476,872✔
1947

1948
void ParserDriver::backlink(LinkChain& link_chain, std::string_view raw_table_name, std::string_view raw_column_name)
1949
{
504✔
1950
    std::string table_name = m_mapping.translate_table_name(raw_table_name);
504✔
1951
    auto origin_table = m_base_table->get_parent_group()->get_table(table_name);
504✔
1952
    ColKey origin_column;
504✔
1953
    std::string column_name{raw_column_name};
504✔
1954
    if (origin_table) {
504✔
1955
        column_name = m_mapping.translate(origin_table, column_name);
496✔
1956
        origin_column = origin_table->get_column_key(column_name);
496✔
1957
    }
496✔
1958
    if (!origin_column) {
504✔
1959
        auto origin_table_name = Group::table_name_to_class_name(table_name);
12✔
1960
        auto current_table_name = link_chain.get_current_table()->get_class_name();
12✔
1961
        throw InvalidQueryError(util::format("No property '%1' found in type '%2' which links to type '%3'",
12✔
1962
                                             column_name, origin_table_name, current_table_name));
12✔
1963
    }
12✔
1964
    link_chain.backlink(*origin_table, origin_column);
492✔
1965
}
492✔
1966

1967
std::string ParserDriver::translate(const LinkChain& link_chain, const std::string& identifier)
1968
{
497,306✔
1969
    return m_mapping.translate(link_chain, identifier);
497,306✔
1970
}
497,306✔
1971

1972
int ParserDriver::parse(const std::string& str)
1973
{
46,658✔
1974
    // std::cout << str << std::endl;
1975
    parse_buffer.append(str);
46,658✔
1976
    parse_buffer.append("\0\0", 2); // Flex requires 2 terminating zeroes
46,658✔
1977
    scan_begin(m_yyscanner, trace_scanning);
46,658✔
1978
    yy::parser parse(*this, m_yyscanner);
46,658✔
1979
    parse.set_debug_level(trace_parsing);
46,658✔
1980
    int res = parse();
46,658✔
1981
    if (parse_error) {
46,658✔
1982
        throw SyntaxError(util::format("Invalid predicate: '%1': %2", str, error_string));
724✔
1983
    }
724✔
1984
    return res;
45,934✔
1985
}
46,658✔
1986

1987
void parse(const std::string& str)
1988
{
1,004✔
1989
    ParserDriver driver;
1,004✔
1990
    driver.parse(str);
1,004✔
1991
}
1,004✔
1992

1993
std::string check_escapes(const char* str)
1994
{
498,690✔
1995
    std::string ret;
498,690✔
1996
    const char* p = strchr(str, '\\');
498,690✔
1997
    while (p) {
499,114✔
1998
        ret += std::string(str, p);
424✔
1999
        p++;
424✔
2000
        if (*p == ' ') {
424✔
2001
            ret += ' ';
144✔
2002
        }
144✔
2003
        else if (*p == 't') {
280✔
2004
            ret += '\t';
280✔
2005
        }
280✔
2006
        else if (*p == 'r') {
×
2007
            ret += '\r';
×
2008
        }
×
2009
        else if (*p == 'n') {
×
2010
            ret += '\n';
×
2011
        }
×
2012
        str = p + 1;
424✔
2013
        p = strchr(str, '\\');
424✔
2014
    }
424✔
2015
    return ret + std::string(str);
498,690✔
2016
}
498,690✔
2017

2018
} // namespace query_parser
2019

2020
Query Table::query(const std::string& query_string, const std::vector<MixedArguments::Arg>& arguments) const
2021
{
12,174✔
2022
    MixedArguments args(arguments);
12,174✔
2023
    return query(query_string, args, {});
12,174✔
2024
}
12,174✔
2025

2026
Query Table::query(const std::string& query_string, const std::vector<Mixed>& arguments) const
2027
{
24✔
2028
    MixedArguments args(arguments);
24✔
2029
    return query(query_string, args, {});
24✔
2030
}
24✔
2031

2032
Query Table::query(const std::string& query_string, const std::vector<Mixed>& arguments,
2033
                   const query_parser::KeyPathMapping& mapping) const
2034
{
1,784✔
2035
    MixedArguments args(arguments);
1,784✔
2036
    return query(query_string, args, mapping);
1,784✔
2037
}
1,784✔
2038

2039
Query Table::query(const std::string& query_string, const std::vector<MixedArguments::Arg>& arguments,
2040
                   const query_parser::KeyPathMapping& mapping) const
2041
{
352✔
2042
    MixedArguments args(arguments);
352✔
2043
    return query(query_string, args, mapping);
352✔
2044
}
352✔
2045

2046
Query Table::query(const std::string& query_string, query_parser::Arguments& args,
2047
                   const query_parser::KeyPathMapping& mapping) const
2048
{
45,638✔
2049
    ParserDriver driver(m_own_ref, args, mapping);
45,638✔
2050
    driver.parse(query_string);
45,638✔
2051
    driver.result->canonicalize();
45,638✔
2052
    return driver.result->visit(&driver).set_ordering(driver.ordering->visit(&driver));
45,638✔
2053
}
45,638✔
2054

2055
std::unique_ptr<Subexpr> LinkChain::column(const std::string& col, bool has_path)
2056
{
478,586✔
2057
    auto col_key = m_current_table->get_column_key(col);
478,586✔
2058
    if (!col_key) {
478,586✔
2059
        return nullptr;
128✔
2060
    }
128✔
2061

2062
    auto col_type{col_key.get_type()};
478,458✔
2063
    if (col_key.is_dictionary()) {
478,458✔
2064
        return create_subexpr<Dictionary>(col_key);
984✔
2065
    }
984✔
2066
    if (Table::is_link_type(col_type)) {
477,474✔
2067
        add(col_key);
×
2068
        return create_subexpr<Link>(col_key);
×
2069
    }
×
2070

2071
    if (col_key.is_set()) {
477,474✔
2072
        switch (col_type) {
3,984✔
2073
            case col_type_Int:
416✔
2074
                return create_subexpr<Set<Int>>(col_key);
416✔
2075
            case col_type_Bool:
✔
2076
                return create_subexpr<Set<Bool>>(col_key);
×
2077
            case col_type_String:
512✔
2078
                return create_subexpr<Set<String>>(col_key);
512✔
2079
            case col_type_Binary:
512✔
2080
                return create_subexpr<Set<Binary>>(col_key);
512✔
2081
            case col_type_Float:
416✔
2082
                return create_subexpr<Set<Float>>(col_key);
416✔
2083
            case col_type_Double:
416✔
2084
                return create_subexpr<Set<Double>>(col_key);
416✔
2085
            case col_type_Timestamp:
248✔
2086
                return create_subexpr<Set<Timestamp>>(col_key);
248✔
2087
            case col_type_Decimal:
416✔
2088
                return create_subexpr<Set<Decimal>>(col_key);
416✔
2089
            case col_type_UUID:
248✔
2090
                return create_subexpr<Set<UUID>>(col_key);
248✔
2091
            case col_type_ObjectId:
248✔
2092
                return create_subexpr<Set<ObjectId>>(col_key);
248✔
2093
            case col_type_Mixed:
552✔
2094
                return create_subexpr<Set<Mixed>>(col_key);
552✔
2095
            default:
✔
2096
                break;
×
2097
        }
3,984✔
2098
    }
3,984✔
2099
    else if (col_key.is_list()) {
473,490✔
2100
        switch (col_type) {
16,480✔
2101
            case col_type_Int:
3,048✔
2102
                return create_subexpr<Lst<Int>>(col_key);
3,048✔
2103
            case col_type_Bool:
880✔
2104
                return create_subexpr<Lst<Bool>>(col_key);
880✔
2105
            case col_type_String:
5,024✔
2106
                return create_subexpr<Lst<String>>(col_key);
5,024✔
2107
            case col_type_Binary:
1,660✔
2108
                return create_subexpr<Lst<Binary>>(col_key);
1,660✔
2109
            case col_type_Float:
880✔
2110
                return create_subexpr<Lst<Float>>(col_key);
880✔
2111
            case col_type_Double:
880✔
2112
                return create_subexpr<Lst<Double>>(col_key);
880✔
2113
            case col_type_Timestamp:
880✔
2114
                return create_subexpr<Lst<Timestamp>>(col_key);
880✔
2115
            case col_type_Decimal:
880✔
2116
                return create_subexpr<Lst<Decimal>>(col_key);
880✔
2117
            case col_type_UUID:
880✔
2118
                return create_subexpr<Lst<UUID>>(col_key);
880✔
2119
            case col_type_ObjectId:
880✔
2120
                return create_subexpr<Lst<ObjectId>>(col_key);
880✔
2121
            case col_type_Mixed:
588✔
2122
                return create_subexpr<Lst<Mixed>>(col_key);
588✔
2123
            default:
✔
2124
                break;
×
2125
        }
16,480✔
2126
    }
16,480✔
2127
    else {
457,010✔
2128
        // Having a path implies a collection
2129
        if (m_comparison_type && !has_path) {
457,010✔
2130
            bool has_list = false;
1,260✔
2131
            for (ColKey link_key : m_link_cols) {
1,312✔
2132
                if (link_key.is_collection() || link_key.get_type() == col_type_BackLink) {
1,312✔
2133
                    has_list = true;
1,216✔
2134
                    break;
1,216✔
2135
                }
1,216✔
2136
            }
1,312✔
2137
            if (!has_list) {
1,260✔
2138
                throw InvalidQueryError(util::format("The keypath following '%1' must contain a list",
44✔
2139
                                                     expression_cmp_type_to_str(m_comparison_type)));
44✔
2140
            }
44✔
2141
        }
1,260✔
2142

2143
        switch (col_type) {
456,966✔
2144
            case col_type_Int:
14,016✔
2145
                return create_subexpr<Int>(col_key);
14,016✔
2146
            case col_type_Bool:
224✔
2147
                return create_subexpr<Bool>(col_key);
224✔
2148
            case col_type_String:
4,800✔
2149
                return create_subexpr<String>(col_key);
4,800✔
2150
            case col_type_Binary:
2,052✔
2151
                return create_subexpr<Binary>(col_key);
2,052✔
2152
            case col_type_Float:
552✔
2153
                return create_subexpr<Float>(col_key);
552✔
2154
            case col_type_Double:
1,944✔
2155
                return create_subexpr<Double>(col_key);
1,944✔
2156
            case col_type_Timestamp:
728✔
2157
                return create_subexpr<Timestamp>(col_key);
728✔
2158
            case col_type_Decimal:
1,484✔
2159
                return create_subexpr<Decimal>(col_key);
1,484✔
2160
            case col_type_UUID:
520✔
2161
                return create_subexpr<UUID>(col_key);
520✔
2162
            case col_type_ObjectId:
428,804✔
2163
                return create_subexpr<ObjectId>(col_key);
428,804✔
2164
            case col_type_Mixed:
1,836✔
2165
                return create_subexpr<Mixed>(col_key);
1,836✔
2166
            default:
✔
2167
                break;
×
2168
        }
456,966✔
2169
    }
456,966✔
2170
    REALM_UNREACHABLE();
2171
    return nullptr;
×
2172
}
477,474✔
2173

2174
std::unique_ptr<Subexpr> LinkChain::subquery(Query subquery)
2175
{
280✔
2176
    REALM_ASSERT(m_link_cols.size() > 0);
280✔
2177
    auto col_key = m_link_cols.back();
280✔
2178
    return std::make_unique<SubQueryCount>(subquery, Columns<Link>(col_key, m_base_table, m_link_cols).link_map());
280✔
2179
}
280✔
2180

2181
template <class T>
2182
SubQuery<T> column(const Table& origin, ColKey origin_col_key, Query subquery)
2183
{
2184
    static_assert(std::is_same<T, BackLink>::value, "A subquery must involve a link list or backlink column");
2185
    return SubQuery<T>(column<T>(origin, origin_col_key), std::move(subquery));
2186
}
2187

2188
} // 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