aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/select/valuenodes.cpp
blob: ac020009512fa99588385cd048073f49fe0b0c78 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "valuenodes.h"
#include "visitor.h"
#include "parser.h"
#include <vespa/document/base/exceptions.h>
#include <vespa/document/update/documentupdate.h>
#include <vespa/document/fieldvalue/fieldvalues.h>
#include <vespa/document/fieldvalue/referencefieldvalue.h>
#include <vespa/document/fieldvalue/iteratorhandler.h>
#include <vespa/document/datatype/documenttype.h>
#include <vespa/vespalib/util/md5.h>
#include <vespa/document/util/stringutil.h>
#include <vespa/vespalib/text/lowercase.h>
#include <cassert>
#include <iomanip>
#include <sys/time.h>

#include <vespa/log/log.h>
LOG_SETUP(".document.select.valuenode");

namespace document::select {

namespace {

[[nodiscard]] bool document_type_is_a(const DocumentType& type, vespalib::stringref name) {
    if (type.getName() == name) {
        return true;
    }
    // No exact match; try to recursively match name against any types inherited from.
    for (const auto* parent : type.getInheritedTypes()) {
        if (document_type_is_a(*parent, name)) {
            return true;
        }
    }
    return false;
}

}

InvalidValueNode::InvalidValueNode(vespalib::stringref name)
    : _name(name)
{}


void
InvalidValueNode::visit(Visitor &visitor) const
{
    visitor.visitInvalidValueNode(*this);
}


void
InvalidValueNode::print(std::ostream& out, bool verbose,
                        const std::string& indent) const
{
    (void) verbose; (void) indent;
    if (hadParentheses()) out << '(';
    out << _name;
    if (hadParentheses()) out << ')';
}

NullValueNode::NullValueNode() = default;

void
NullValueNode::visit(Visitor &visitor) const
{
    visitor.visitNullValueNode(*this);
}


void
NullValueNode::print(std::ostream& out, bool verbose,
                        const std::string& indent) const
{
    (void) verbose; (void) indent;
    if (hadParentheses()) out << '(';
    out << "null";
    if (hadParentheses()) out << ')';
}

StringValueNode::StringValueNode(vespalib::stringref val)
    : _value(val)
{
}


void
StringValueNode::visit(Visitor &visitor) const
{
    visitor.visitStringValueNode(*this);
}


void
StringValueNode::print(std::ostream& out, bool verbose,
                       const std::string& indent) const
{
    (void) verbose; (void) indent;
    if (hadParentheses()) out << '(';
    out << "\"" << StringUtil::escape(_value) << "\"";
    if (hadParentheses()) out << ')';
}


void
IntegerValueNode::visit(Visitor &visitor) const
{
    visitor.visitIntegerValueNode(*this);
}


void
IntegerValueNode::print(std::ostream& out, bool verbose,
                       const std::string& indent) const
{
    (void) verbose; (void) indent;
    if (hadParentheses()) out << '(';
    out << _value;
    if (hadParentheses()) out << ')';
}

void
BoolValueNode::visit(Visitor& visitor) const
{
    visitor.visitBoolValueNode(*this);
}

void
BoolValueNode::print(std::ostream& out,
                     [[maybe_unused]] bool verbose,
                     [[maybe_unused]] const std::string& indent) const
{
    if (hadParentheses()) out << '(';
    out << bool_value_str();
    if (hadParentheses()) out << ')';
}


int64_t
CurrentTimeValueNode::getValue() const
{
    struct timeval mytime;
    gettimeofday(&mytime, nullptr);
    return mytime.tv_sec;
}


void
CurrentTimeValueNode::visit(Visitor &visitor) const
{
    visitor.visitCurrentTimeValueNode(*this);
}


void
CurrentTimeValueNode::print(std::ostream& out, bool verbose,
                            const std::string& indent) const
{
    (void) verbose; (void) indent;
    out << "now()";
}

std::unique_ptr<Value>
VariableValueNode::getValue(const Context& context) const {
    return context.getValue(_value);
}

void
VariableValueNode::visit(Visitor &visitor) const
{
    visitor.visitVariableValueNode(*this);
}


void
VariableValueNode::print(std::ostream& out, bool verbose,
                       const std::string& indent) const
{
    (void) verbose; (void) indent;
    if (hadParentheses()) out << '(';
    out << "$" << _value;
    if (hadParentheses()) out << ')';
}


void
FloatValueNode::visit(Visitor &visitor) const
{
    visitor.visitFloatValueNode(*this);
}


void
FloatValueNode::print(std::ostream& out, bool verbose,
                       const std::string& indent) const
{
    (void) verbose; (void) indent;
    if (hadParentheses()) out << '(';
    out << _value;
    if (hadParentheses()) out << ')';
}

FieldValueNode::FieldValueNode(const vespalib::string& doctype,
                               const vespalib::string& fieldExpression)
    : _doctype(doctype),
      _fieldExpression(fieldExpression),
      _fieldName(extractFieldName(fieldExpression))
{
}

FieldValueNode::~FieldValueNode() = default;

namespace {

size_t first_ident_length_or_npos(const vespalib::string& expr) {
    for (size_t i = 0; i < expr.size(); ++i) {
        switch (expr[i]) {
        case '.':
        case '{':
        case '[':
        case ' ':
        case '\n':
        case '\t':
            return i;
        default:
            continue;
        }
    }
    return vespalib::string::npos;
}

}

// TODO remove this pile of fun in favor of actually parsed AST nodes...!
vespalib::string
FieldValueNode::extractFieldName(const vespalib::string & fieldExpression) {
    // When we get here the actual contents of the field expression shall already
    // have been structurally and syntactically verified by the parser.
    return fieldExpression.substr(0, first_ident_length_or_npos(fieldExpression));
}

namespace {

class IteratorHandler : public fieldvalue::IteratorHandler {
public:
    IteratorHandler();
    ~IteratorHandler() override;
    bool hasSingleValue() const;
    std::unique_ptr<Value> stealSingleValue() &&;
    std::vector<ArrayValue::VariableValue> stealValues() &&;
private:
    std::unique_ptr<Value> _firstValue;
    std::vector<ArrayValue::VariableValue> _values;

    void onPrimitive(uint32_t fid, const Content &fv) override;
    std::unique_ptr<Value> getInternalValue(const FieldValue &fval) const;
};

IteratorHandler::IteratorHandler() = default;
IteratorHandler::~IteratorHandler() = default;

bool
IteratorHandler::hasSingleValue() const {
    return _firstValue && _values.empty();
}

std::unique_ptr<Value>
IteratorHandler::stealSingleValue() && {
    return std::move(_firstValue);
}

std::vector<ArrayValue::VariableValue>
IteratorHandler::stealValues() && {
    if (_firstValue) {
        _values.insert(_values.begin(), ArrayValue::VariableValue(fieldvalue::VariableMap(), Value::SP(_firstValue.release())));
    }

    return std::move(_values);
}

void
IteratorHandler::onPrimitive(uint32_t fid, const Content& fv) {
    (void) fid;
    if (!_firstValue && getVariables().empty()) {
        _firstValue = getInternalValue(fv.getValue());
    } else {
        _values.emplace_back(stealVariables(), Value::SP(getInternalValue(fv.getValue()).release()));
    }
}

std::unique_ptr<Value>
IteratorHandler::getInternalValue(const FieldValue& fval) const
{
    switch(fval.type()) {
        case FieldValue::Type::BOOL:
        {
            const auto& val(dynamic_cast<const BoolFieldValue&>(fval));
            return std::make_unique<IntegerValue>(val.getAsInt(), false);
        }
        case FieldValue::Type::INT:
        {
            const auto& val(dynamic_cast<const IntFieldValue&>(fval));
            return std::make_unique<IntegerValue>(val.getAsInt(), false);
        }
        case FieldValue::Type::BYTE:
        {
            const auto& val(dynamic_cast<const ByteFieldValue&>(fval));
            return std::make_unique<IntegerValue>(val.getAsByte(), false);
        }
        case FieldValue::Type::LONG:
        {
            const auto& val(dynamic_cast<const LongFieldValue&>(fval));
            return std::make_unique<IntegerValue>(val.getAsLong(), false);
        }
        case FieldValue::Type::FLOAT:
        {
            const auto& val(dynamic_cast<const FloatFieldValue&>(fval));
            return std::make_unique<FloatValue>(val.getAsFloat());
        }
        case FieldValue::Type::DOUBLE:
        {
            const auto& val(dynamic_cast<const DoubleFieldValue&>(fval));
            return std::make_unique<FloatValue>(val.getAsDouble());
        }
        case FieldValue::Type::STRING:
        {
            const auto& val(dynamic_cast<const StringFieldValue&>(fval));
            return std::make_unique<StringValue>(val.getAsString());
        }
        case FieldValue::Type::REFERENCE:
        {
            const auto& val(dynamic_cast<const ReferenceFieldValue&>(fval));
            if (val.hasValidDocumentId()) {
                return std::make_unique<StringValue>(val.getDocumentId().toString());
            } else {
                return std::make_unique<InvalidValue>();
            }
        }
        case FieldValue::Type::ARRAY:
        {
            const auto& val(dynamic_cast<const ArrayFieldValue&>(fval));
            if (val.size() == 0) {
                return std::make_unique<NullValue>();
            } else {
                // TODO: Array comparison.
                return std::make_unique<ArrayValue>(std::vector<ArrayValue::VariableValue>());
            }
        }
        case FieldValue::Type::STRUCT:
        {
            const auto& val(dynamic_cast<const StructFieldValue&>(fval));
            if (val.empty()) {
                return std::make_unique<NullValue>();
            } else {
                StructValue::ValueMap values;
                for (StructFieldValue::const_iterator it(val.begin()); it != val.end(); ++it) {
                    FieldValue::UP fv(val.getValue(it.field()));
                    values[it.field().getName()] = Value::SP(getInternalValue(*fv).release());
                }
                return std::make_unique<StructValue>(std::move(values));
            }
        }
        case FieldValue::Type::MAP:
        {
            const auto& val(static_cast<const MapFieldValue&>(fval));
            if (val.isEmpty()) {
                return std::make_unique<NullValue>();
            } else {
                // TODO: Map comparison
                return std::make_unique<ArrayValue>(std::vector<ArrayValue::VariableValue>());
            }
        }
        default:
            break;
    }
    LOG(warning, "Tried to use unsupported datatype %s in field comparison",
        fval.getDataType()->toString().c_str());
    return std::make_unique<InvalidValue>();
}

}

void
FieldValueNode::initFieldPath(const DocumentType& type) const {
    if (_fieldPath.empty()) {
        type.buildFieldPath(_fieldPath, _fieldExpression);
    }
}

namespace {

bool looks_like_complex_field_path(const vespalib::string& expr) {
    for (const char c : expr) {
        switch (c) {
        case '.':
        case '[':
        case '{':
            return true;
        default: continue;
        }
    }
    return false;
}

bool is_simple_imported_field(const vespalib::string& expr, const DocumentType& doc_type) {
    if (looks_like_complex_field_path(expr)) {
        return false;
    }
    return (doc_type.has_imported_field_name(expr));
}

}

std::unique_ptr<Value>
FieldValueNode::getValue(const Context& context) const
{
    if (context._doc == nullptr) {
        return std::make_unique<InvalidValue>();
    }

    const Document& doc = *context._doc;

    if (!document_type_is_a(doc.getType(), _doctype)) {
        return std::make_unique<InvalidValue>();
    }
    // Imported fields can only be meaningfully evaluated inside Proton, so we
    // explicitly treat them as if they are valid fields with missing values. This
    // will be treated the same as if it's a normal field by the selection operators.
    // This avoids any awkward interaction with Invalid values or having to
    // augment the FieldPath code with knowledge of imported fields.
    // When a selection is running inside Proton, it will patch FieldValueNodes for
    // imported fields, which removes this check entirely.
    if (is_simple_imported_field(_fieldExpression, doc.getType())) {
        return std::make_unique<NullValue>();
    }
    try {
        initFieldPath(doc.getType());

        IteratorHandler handler;
        doc.iterateNested(_fieldPath.getFullRange(), handler);

        if (handler.hasSingleValue()) {
            return std::move(handler).stealSingleValue();
        } else {
            std::vector<ArrayValue::VariableValue> values = std::move(handler).stealValues();

            if (values.empty()) {
                return std::make_unique<NullValue>();
            } else {
                return std::make_unique<ArrayValue>(std::move(values));
            }
        }
    } catch (vespalib::IllegalArgumentException& e) {
        LOG(warning, "Caught exception while fetching field from document: %s", e.what());
        return std::make_unique<InvalidValue>();
    } catch (FieldNotFoundException& e) {
        LOG(warning, "Tried to compare to field %s, not found in document type", _fieldExpression.c_str());
        return std::make_unique<InvalidValue>();
    }
}

void
FieldValueNode::visit(Visitor &visitor) const
{
    visitor.visitFieldValueNode(*this);
}


void
FieldValueNode::print(std::ostream& out, bool verbose,
                       const std::string& indent) const
{
    (void) verbose; (void) indent;
    if (hadParentheses()) out << '(';
    out << _doctype << "." << _fieldExpression;
    if (hadParentheses()) out << ')';
}


std::unique_ptr<Value>
FieldValueNode::traceValue(const Context &context, std::ostream& out) const
{
    if (context._doc == nullptr) {
        return defaultTrace(getValue(context), out);
    }
    const Document &doc(*context._doc);
    if (!document_type_is_a(doc.getType(), _doctype)) {
        out << "Document is of type " << doc.getType() << " which isn't a "
            << _doctype << " document, thus resolving invalid.\n";
        return std::make_unique<InvalidValue>();
    }
    if (is_simple_imported_field(_fieldExpression, doc.getType())) {
        out << "Field '" << _fieldExpression << "' refers to an imported field; "
            << "returning NullValue to treat this as an unset field value.\n";
        return std::make_unique<NullValue>();
    }
    try {
        initFieldPath(doc.getType());

        IteratorHandler handler;
        doc.iterateNested(_fieldPath.getFullRange(), handler);

        if (handler.hasSingleValue()) {
            return std::move(handler).stealSingleValue();
        } else {
            std::vector<ArrayValue::VariableValue> values = std::move(handler).stealValues();

            if (values.size() == 0) {
                return std::make_unique<NullValue>();
            } else {
                return std::make_unique<ArrayValue>(std::move(values));
            }
        }
    } catch (FieldNotFoundException& e) {
        LOG(warning, "Tried to compare to field %s, not found in document type",
                     _fieldExpression.c_str());
        out << "Field not found in document type " << doc.getType()
            << ". Returning invalid.\n";
        return std::make_unique<InvalidValue>();
    }
}

IdValueNode::IdValueNode(const BucketIdFactory& bucketIdFactory,
                         vespalib::stringref name, vespalib::stringref type,
                         int widthBits, int divisionBits)
    : _bucketIdFactory(bucketIdFactory),
      _id(name),
      _typestring(type),
      _type(ALL),
      _widthBits(widthBits),
      _divisionBits(divisionBits)
{
    if (type.length() > 2) switch (type[0]) {
    case 'b': _type = BUCKET;
        break;
    case 'n': _type = NS;
        break;
    case 'g':
        if (type[1] == 'r') {
            _type = GROUP;
        } else if (type[1] == 'i') {
            _type = GID;
        }
        break;
    case 's': {
        if (type[1] == 'c') { _type = SCHEME; } else { _type = SPEC; }
        break;
    }
    case 't':
        _type = TYPE;
        break;
    case 'u':
        _type = USER;
        break;
        break;
    }
}


std::unique_ptr<Value>
IdValueNode::getValue(const Context& context) const
{
    if (context._doc != NULL) {
        return getValue(context._doc->getId());
    } else if (context._docId != NULL) {
        return getValue(*context._docId);
    } else {
        return getValue(context._docUpdate->getId());
    }
}


std::unique_ptr<Value>
IdValueNode::getValue(const DocumentId& id) const
{
    vespalib::string value;
    switch (_type) {
    case BUCKET:
        return std::make_unique<IntegerValue>(_bucketIdFactory.getBucketId(id).getId(), true);
    case NS:
        value = id.getScheme().getNamespace(); break;
    case SCHEME:
        value = "id";
        break;
    case TYPE:
        if (id.getScheme().hasDocType()) {
            value = id.getScheme().getDocType();
        } else {
            return std::make_unique<InvalidValue>();
        }
        break;
    case SPEC:
        value = id.getScheme().getNamespaceSpecific();
        break;
    case ALL:
        value = id.getScheme().toString();
        break;
    case GROUP:
        if (id.getScheme().hasGroup()) {
            value = id.getScheme().getGroup();
        } else {
            fprintf(stderr, "***** Returning invalid value for %s\n",
                    id.toString().c_str());
            return std::make_unique<InvalidValue>();
        }
        break;
    case GID:
        value = id.getGlobalId().toString();
        break;
    case USER:
        if (id.getScheme().hasNumber()) {
            return std::make_unique<IntegerValue>(id.getScheme().getNumber(), false);
        } else {
            return std::make_unique<InvalidValue>();
        }
    }

    return std::make_unique<StringValue>(value);
}


std::unique_ptr<Value>
IdValueNode::traceValue(const Context& context,
                        std::ostream &out) const
{
    if (context._doc != NULL) {
        return traceValue(context._doc->getId(), out);
    } else if (context._docId != NULL) {
        return traceValue(*context._docId, out);
    } else {
        return traceValue(context._docUpdate->getId(), out);
    }
}


std::unique_ptr<Value>
IdValueNode::traceValue(const DocumentId& id, std::ostream& out) const
{
    vespalib::string value;
    switch (_type) {
    case BUCKET:
        {
            document::BucketId bucket(_bucketIdFactory.getBucketId(id));
            out << "Found id.bucket specification. Resolved to " << bucket.toString() << ".\n";
            return std::make_unique<IntegerValue>(bucket.getId(), true);
        }
    case NS:
        value = id.getScheme().getNamespace();
        out << "Resolved id.namespace to value\"" << value << "\".\n";
        break;
    case SCHEME:
        value = "id";
        out << "Resolved id.scheme to value\"" << value << "\".\n";
        break;
    case TYPE:
        if (id.getScheme().hasDocType()) {
            value = id.getScheme().getDocType();
            out << "Resolved id.type to value\"" << value << "\".\n";
        } else {
            out << "Could not resolve type of doc " << id << ".\n";
            return std::make_unique<InvalidValue>();
        }
        break;
    case SPEC:
        value = id.getScheme().getNamespaceSpecific();
        out << "Resolved id.specific to value\"" << value << "\".\n";
        break;
    case ALL:
        value = id.getScheme().toString();
        out << "Resolved id to \"" << value << "\".\n";
        break;
    case GROUP:
        if (id.getScheme().hasGroup()) {
            value = id.getScheme().getGroup();
            out << "Resolved group of doc (type id) to \"" << value << "\".\n";
        } else {
            out << "Can't resolve group of doc \"" << id << "\".\n";
            return std::make_unique<InvalidValue>();
        }
        break;
    case GID:
        value = id.getGlobalId().toString();
        out << "Resolved gid to \"" << value << "\".\n";
        break;
    case USER:
        if (id.getScheme().hasNumber()) {
            auto result = std::make_unique<IntegerValue>(id.getScheme().getNumber(), false);
            out << "Resolved user of doc type 'id' to " << *result << ".\n";
            return result;
        } else {
            out << "Could not resolve user of doc " << id << ".\n";
            return std::make_unique<InvalidValue>();
        }
    }

    return std::make_unique<StringValue>(value);
}


void
IdValueNode::visit(Visitor &visitor) const
{
    visitor.visitIdValueNode(*this);
}


void
IdValueNode::print(std::ostream& out, bool verbose,
                   const std::string& indent) const
{
    (void) verbose; (void) indent;
    if (hadParentheses()) out << '(';
    out << _id;
    if (_type != ALL) {
        out << '.' << _typestring;
    }
    if (hadParentheses()) out << ')';
}

namespace {
    union HashUnion {
        unsigned char _key[16];
        int64_t       _hash[2];
    };
    int64_t hash(const void* data, uint32_t len) {
        HashUnion hash;
        fastc_md5sum((const unsigned char*) data, len, hash._key);
        return hash._hash[0];
    }
}

FunctionValueNode::FunctionValueNode(vespalib::stringref name,
                                     std::unique_ptr<ValueNode> src)
    : _function(),
      _funcname(name),
      _source(std::move(src))
{
    if (name == "lowercase") {
        _function = LOWERCASE;
    } else if (name == "hash") {
        _function = HASH;
    } else if (name == "abs") {
        _function = ABS;
    } else {
        throw ParsingFailedException("No function '"+name+"' exist.",
                                     VESPA_STRLOC);
    }
}

std::unique_ptr<Value>
FunctionValueNode::getValue(std::unique_ptr<Value> val) const
{
    switch (val->getType()) {
        case Value::String:
        {
            StringValue& sval(static_cast<StringValue&>(*val));
            if (_function == LOWERCASE) {
                return std::make_unique<StringValue>(vespalib::LowerCase::convert(sval.getValue()));
            } else if (_function == HASH) {
                return std::make_unique<IntegerValue>(hash(sval.getValue().c_str(), sval.getValue().size()), false);
            }
            break;
        }
        case Value::Float:
        {
            FloatValue& fval(static_cast<FloatValue&>(*val));
            if (_function == HASH) {
                FloatValue::ValueType ffval = fval.getValue();
                return std::make_unique<IntegerValue>(hash(&ffval, sizeof(ffval)), false);
            } else if (_function == ABS) {
                FloatValue::ValueType ffval = fval.getValue();
                if (ffval < 0) ffval *= -1;
                return std::make_unique<FloatValue>(ffval);
            }
            break;
        }
        case Value::Integer:
        {
            IntegerValue& ival(static_cast<IntegerValue&>(*val));
            if (_function == HASH) {
                IntegerValue::ValueType iival = ival.getValue();
                return std::make_unique<IntegerValue>(hash(&iival, sizeof(iival)), false);
            } else if (_function == ABS) {
                IntegerValue::ValueType iival = ival.getValue();
                if (iival < 0) iival *= -1;
                return std::make_unique<IntegerValue>(iival, false);
            }
            break;
        }
        case Value::Bucket:
        {
            throw ParsingFailedException(
                    "No functioncalls are allowed on value of type bucket",
                    VESPA_STRLOC);
            break;
        }

        case Value::Array: break;
        case Value::Struct: break;
        case Value::Invalid: break;
        case Value::Null: break;
    }
    return std::make_unique<InvalidValue>();
}

std::unique_ptr<Value>
FunctionValueNode::traceValue(std::unique_ptr<Value> val, std::ostream& out) const
{
    switch (val->getType()) {
        case Value::String:
        {
            StringValue& sval(static_cast<StringValue&>(*val));
            if (_function == LOWERCASE) {
                auto result = std::make_unique<StringValue>(vespalib::LowerCase::convert(sval.getValue()));
                out << "Performed lowercase function on '" << sval
                    << "' => '" << *result << "'.\n";
                return result;
            } else if (_function == HASH) {
                auto result = std::make_unique<IntegerValue>(hash(sval.getValue().c_str(), sval.getValue().size()), false);
                out << "Performed hash on string '" << sval << "' -> "
                    << *result << "\n";
                return result;
            }
            break;
        }
        case Value::Float:
        {
            FloatValue& fval(static_cast<FloatValue&>(*val));
            if (_function == HASH) {
                FloatValue::ValueType ffval = fval.getValue();
                auto result = std::make_unique<IntegerValue>(hash(&ffval, sizeof(ffval)), false);
                out << "Performed hash on float " << ffval << " -> " << *result
                    << "\n";
                return result;
            } else if (_function == ABS) {
                FloatValue::ValueType ffval = fval.getValue();
                if (ffval < 0) ffval *= -1;
                out << "Performed abs on float " << fval.getValue() << " -> "
                    << ffval << "\n";
                return std::make_unique<FloatValue>(ffval);
            }
            break;
        }
        case Value::Integer:
        {
            IntegerValue& ival(static_cast<IntegerValue&>(*val));
            if (_function == HASH) {
                IntegerValue::ValueType iival = ival.getValue();
                auto result = std::make_unique<IntegerValue>(hash(&iival, sizeof(iival)), false);
                out << "Performed hash on float " << iival << " -> " << *result
                    << "\n";
                return result;
            } else if (_function == ABS) {
                IntegerValue::ValueType iival = ival.getValue();
                if (iival < 0) iival *= -1;
                out << "Performed abs on integer " << ival.getValue() << " -> "
                    << iival << "\n";
                return std::make_unique<IntegerValue>(iival, false);
            }
            break;
        }
        case Value::Bucket: break;
        case Value::Array: break;
        case Value::Struct: break;
        case Value::Invalid: break;
        case Value::Null: break;
    }
    out << "Cannot use function " << _function << " on a value of type "
        << val->getType() << ". Resolving invalid.\n";
    return std::make_unique<InvalidValue>();
}


void
FunctionValueNode::visit(Visitor &visitor) const
{
    visitor.visitFunctionValueNode(*this);
}


void
FunctionValueNode::print(std::ostream& out, bool verbose,
                         const std::string& indent) const
{
    if (hadParentheses()) out << '(';
    _source->print(out, verbose, indent);
    out << '.' << _funcname << "()";
    if (hadParentheses()) out << ')';
}

ArithmeticValueNode::ArithmeticValueNode(
        std::unique_ptr<ValueNode> left, vespalib::stringref op,
        std::unique_ptr<ValueNode> right)
    : ValueNode(std::max(left->max_depth(), right->max_depth()) + 1),
      _operator(),
      _left(std::move(left)),
      _right(std::move(right))
{
    if (op.size() == 1) switch (op[0]) {
        case '+': _operator = ADD; return;
        case '-': _operator = SUB; return;
        case '*': _operator = MUL; return;
        case '/': _operator = DIV; return;
        case '%': _operator = MOD; return;
    }
    throw ParsingFailedException(
            "Arithmetic operator '"+op+"' does not exist.", VESPA_STRLOC);
}

const char*
ArithmeticValueNode::getOperatorName() const
{
    switch (_operator) {
        case ADD: return "+";
        case SUB: return "-";
        case MUL: return "*";
        case DIV: return "/";
        case MOD: return "%";
    }
    return "UNKNOWN";
}



std::unique_ptr<Value>
ArithmeticValueNode::getValue(std::unique_ptr<Value> lval,
                              std::unique_ptr<Value> rval) const
{
    switch (_operator) {
        case ADD:
        {
            if (lval->getType() == Value::String &&
                rval->getType() == Value::String)
            {
                StringValue& slval(static_cast<StringValue&>(*lval));
                StringValue& srval(static_cast<StringValue&>(*rval));
                return std::make_unique<StringValue>(slval.getValue() + srval.getValue());
            }
        }
        [[fallthrough]];
        case SUB:
        case MUL:
        case DIV:
        {
            if (lval->getType() == Value::Integer &&
                rval->getType() == Value::Integer)
            {
                IntegerValue& ilval(static_cast<IntegerValue&>(*lval));
                IntegerValue& irval(static_cast<IntegerValue&>(*rval));
                IntegerValue::ValueType res = 0;
                switch (_operator) {
                    case ADD: res = ilval.getValue() + irval.getValue(); break;
                    case SUB: res = ilval.getValue() - irval.getValue(); break;
                    case MUL: res = ilval.getValue() * irval.getValue(); break;
                    case DIV:
                        if (irval.getValue() != 0) {
                            res = ilval.getValue() / irval.getValue();
                        } else {
                            throw vespalib::IllegalArgumentException("Division by zero");
                        }
                        break;
                    case MOD: assert(0);
                }
                return std::make_unique<IntegerValue>(res, false);
            }
            NumberValue* nlval(dynamic_cast<NumberValue*>(lval.get()));
            NumberValue* nrval(dynamic_cast<NumberValue*>(rval.get()));
            if (nlval != 0 && nrval != 0) {
                NumberValue::CommonValueType res = 0;
                switch (_operator) {
                    case ADD: res = nlval->getCommonValue()
                                  + nrval->getCommonValue(); break;
                    case SUB: res = nlval->getCommonValue()
                                  - nrval->getCommonValue(); break;
                    case MUL: res = nlval->getCommonValue()
                                  * nrval->getCommonValue(); break;
                    case DIV:
                        if (nrval->getCommonValue() != 0) {
                            res = nlval->getCommonValue()
                                  / nrval->getCommonValue();
                        } else {
                            throw vespalib::IllegalArgumentException("Division by zero");
                        }
                        break;
                    case MOD: assert(0);
                }
                return std::make_unique<FloatValue>(res);
            }
        }
        break;
        case MOD:
        {
            if (lval->getType() == Value::Integer &&
                rval->getType() == Value::Integer)
            {
                IntegerValue& ilval(static_cast<IntegerValue&>(*lval));
                IntegerValue& irval(static_cast<IntegerValue&>(*rval));
                if (irval.getValue() != 0) {
                    return std::make_unique<IntegerValue>(ilval.getValue() % irval.getValue(), false);
                } else {
                    throw vespalib::IllegalArgumentException("Division by zero");
                }
            }
        }
        break;
    }
    return std::make_unique<InvalidValue>();
}

std::unique_ptr<Value>
ArithmeticValueNode::traceValue(std::unique_ptr<Value> lval,
                                std::unique_ptr<Value> rval,
                                std::ostream& out) const
{
    switch (_operator) {
        case ADD:
        {
            if (lval->getType() == Value::String &&
                rval->getType() == Value::String)
            {
                StringValue& slval(static_cast<StringValue&>(*lval));
                StringValue& srval(static_cast<StringValue&>(*rval));
                auto result = std::make_unique<StringValue>(slval.getValue() + srval.getValue());
                out << "Appended strings '" << slval << "' + '" << srval
                    << "' -> '" << *result << "'.\n";
                return result;
            }
        }
        [[fallthrough]];
        case SUB:
        case MUL:
        case DIV:
        {
            if (lval->getType() == Value::Integer &&
                rval->getType() == Value::Integer)
            {
                IntegerValue& ilval(static_cast<IntegerValue&>(*lval));
                IntegerValue& irval(static_cast<IntegerValue&>(*rval));
                IntegerValue::ValueType res = 0;
                switch (_operator) {
                    case ADD: res = ilval.getValue() + irval.getValue(); break;
                    case SUB: res = ilval.getValue() - irval.getValue(); break;
                    case MUL: res = ilval.getValue() * irval.getValue(); break;
                    case DIV: res = ilval.getValue() / irval.getValue(); break;
                    case MOD: assert(0);
                }
                auto result = std::make_unique<IntegerValue>(res, false);
                out << "Performed integer operation " << ilval << " "
                    << getOperatorName() << " " << irval << " = " << *result
                    << "\n";
                return result;
            }
            NumberValue* nlval(dynamic_cast<NumberValue*>(lval.get()));
            NumberValue* nrval(dynamic_cast<NumberValue*>(lval.get()));
            if (nlval != 0 && nrval != 0) {
                NumberValue::CommonValueType res = 0;
                switch (_operator) {
                    case ADD: res = nlval->getCommonValue()
                                  + nrval->getCommonValue(); break;
                    case SUB: res = nlval->getCommonValue()
                                  - nrval->getCommonValue(); break;
                    case MUL: res = nlval->getCommonValue()
                                  * nrval->getCommonValue(); break;
                    case DIV: res = nlval->getCommonValue()
                                  / nrval->getCommonValue(); break;
                    case MOD: assert(0);
                }
                auto result = std::make_unique<FloatValue>(res);
                out << "Performed float operation " << nlval << " "
                    << getOperatorName() << " " << nrval << " = " << *result
                    << "\n";
                return result;
            }
        }
        break;
        case MOD:
        {
            if (lval->getType() == Value::Integer &&
                rval->getType() == Value::Integer)
            {
                IntegerValue& ilval(static_cast<IntegerValue&>(*lval));
                IntegerValue& irval(static_cast<IntegerValue&>(*rval));
                auto result = std::make_unique<IntegerValue>(ilval.getValue() % irval.getValue(), false);
                out << "Performed integer operation " << ilval << " "
                    << getOperatorName() << " " << irval << " = " << *result
                    << "\n";
                return result;
            }
        }
        break;
    }
    out << "Failed to do operation " << getOperatorName()
        << " on values of type " << lval->getType() << " and "
        << rval->getType() << ". Resolving invalid.\n";
    return std::make_unique<InvalidValue>();
}


void
ArithmeticValueNode::visit(Visitor &visitor) const
{
    visitor.visitArithmeticValueNode(*this);
}


void
ArithmeticValueNode::print(std::ostream& out, bool verbose,
                           const std::string& indent) const
{
    if (hadParentheses()) out << '(';
    _left->print(out, verbose, indent);
    switch (_operator) {
        case ADD: out << " + "; break;
        case SUB: out << " - "; break;
        case MUL: out << " * "; break;
        case DIV: out << " / "; break;
        case MOD: out << " % "; break;
    }
    _right->print(out, verbose, indent);
    if (hadParentheses()) out << ')';
}

FieldExprNode::~FieldExprNode() = default;

std::unique_ptr<FieldValueNode> FieldExprNode::convert_to_field_value() const {
    const auto& doctype = resolve_doctype();
    // FIXME deprecate manual post-parsing of field expressions in favor of
    // actually using the structural parser in the way nature intended.
    vespalib::string mangled_expression;
    build_mangled_expression(mangled_expression);
    return std::make_unique<FieldValueNode>(doctype, mangled_expression);
}

std::unique_ptr<FunctionValueNode> FieldExprNode::convert_to_function_call() const {
    // Right hand expr string contains function call, lhs contains field spec on which
    // the function is to be invoked.
    if ((_left_expr == nullptr) || (_left_expr->_left_expr == nullptr)) {
        throw vespalib::IllegalArgumentException(
                vespalib::make_string("Cannot call function '%s' directly on document type", _right_expr.c_str()));
    }
    auto lhs = _left_expr->convert_to_field_value();
    const auto& function_name = _right_expr;
    return std::make_unique<FunctionValueNode>(function_name, std::move(lhs));
}

void FieldExprNode::build_mangled_expression(vespalib::string& dest) const {
    // Leftmost node is doctype, which should not be emitted as part of mangled expression.
    if (_left_expr && _left_expr->_left_expr) {
        _left_expr->build_mangled_expression(dest);
        dest.push_back('.');
    }
    dest.append(_right_expr);
}

const vespalib::string& FieldExprNode::resolve_doctype() const {
    const auto* leftmost = this;
    while (leftmost->_left_expr) {
        leftmost = leftmost->_left_expr.get();
    }
    return leftmost->_right_expr;
}

}