aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/expression/functionnodes.cpp
blob: eb92595a3c49c4c2e8cd3d808006234d551b899a (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "integerresultnode.h"
#include "floatresultnode.h"
#include "stringresultnode.h"
#include "rawresultnode.h"
#include "constantnode.h"
#include "addfunctionnode.h"
#include "dividefunctionnode.h"
#include "multiplyfunctionnode.h"
#include "modulofunctionnode.h"
#include "minfunctionnode.h"
#include "maxfunctionnode.h"
#include "andfunctionnode.h"
#include "orfunctionnode.h"
#include "xorfunctionnode.h"
#include "negatefunctionnode.h"
#include "sortfunctionnode.h"
#include "reversefunctionnode.h"
#include "strlenfunctionnode.h"
#include "numelemfunctionnode.h"
#include "tostringfunctionnode.h"
#include "torawfunctionnode.h"
#include "catfunctionnode.h"
#include "tointfunctionnode.h"
#include "tofloatfunctionnode.h"
#include "strcatfunctionnode.h"
#include "xorbitfunctionnode.h"
#include "md5bitfunctionnode.h"
#include "binaryfunctionnode.h"
#include "resultvector.h"
#include "catserializer.h"
#include "strcatserializer.h"
#include "normalizesubjectfunctionnode.h"
#include "arrayoperationnode.h"
#include <vespa/vespalib/objects/serializer.hpp>
#include <vespa/vespalib/objects/deserializer.hpp>
#include <vespa/vespalib/stllike/asciistream.h>

#include <map>
#include <vespa/vespalib/util/md5.h>

namespace search::expression {

using vespalib::asciistream;
using vespalib::nbostream;
using vespalib::Serializer;
using vespalib::Deserializer;
using vespalib::make_string;
using vespalib::Identifiable;
using vespalib::BufferRef;
using vespalib::ConstBufferRef;

IMPLEMENT_ABSTRACT_EXPRESSIONNODE(ExpressionNode,       Identifiable);
IMPLEMENT_ABSTRACT_EXPRESSIONNODE(FunctionNode,         ExpressionNode);
IMPLEMENT_ABSTRACT_EXPRESSIONNODE(MultiArgFunctionNode, FunctionNode);
IMPLEMENT_ABSTRACT_EXPRESSIONNODE(UnaryFunctionNode,    MultiArgFunctionNode);
IMPLEMENT_ABSTRACT_EXPRESSIONNODE(BinaryFunctionNode,   MultiArgFunctionNode);
IMPLEMENT_ABSTRACT_EXPRESSIONNODE(BitFunctionNode,      NumericFunctionNode);
IMPLEMENT_ABSTRACT_EXPRESSIONNODE(UnaryBitFunctionNode, UnaryFunctionNode);

IMPLEMENT_EXPRESSIONNODE(ConstantNode,         ExpressionNode);
IMPLEMENT_EXPRESSIONNODE(AddFunctionNode,      NumericFunctionNode);
IMPLEMENT_EXPRESSIONNODE(DivideFunctionNode,   NumericFunctionNode);
IMPLEMENT_EXPRESSIONNODE(MultiplyFunctionNode, NumericFunctionNode);
IMPLEMENT_EXPRESSIONNODE(ModuloFunctionNode,   NumericFunctionNode);
IMPLEMENT_EXPRESSIONNODE(MinFunctionNode,      NumericFunctionNode);
IMPLEMENT_EXPRESSIONNODE(MaxFunctionNode,      NumericFunctionNode);
IMPLEMENT_EXPRESSIONNODE(XorFunctionNode,      BitFunctionNode);
IMPLEMENT_EXPRESSIONNODE(AndFunctionNode,      BitFunctionNode);
IMPLEMENT_EXPRESSIONNODE(OrFunctionNode,       BitFunctionNode);
IMPLEMENT_EXPRESSIONNODE(CatFunctionNode,      MultiArgFunctionNode);
IMPLEMENT_EXPRESSIONNODE(StrCatFunctionNode,   MultiArgFunctionNode);
IMPLEMENT_EXPRESSIONNODE(NegateFunctionNode,   UnaryFunctionNode);
IMPLEMENT_EXPRESSIONNODE(SortFunctionNode,     UnaryFunctionNode);
IMPLEMENT_EXPRESSIONNODE(ReverseFunctionNode,  UnaryFunctionNode);
IMPLEMENT_EXPRESSIONNODE(StrLenFunctionNode,   UnaryFunctionNode);
IMPLEMENT_EXPRESSIONNODE(NormalizeSubjectFunctionNode,   UnaryFunctionNode);
IMPLEMENT_EXPRESSIONNODE(ToIntFunctionNode,    UnaryFunctionNode);
IMPLEMENT_EXPRESSIONNODE(ToFloatFunctionNode,  UnaryFunctionNode);
IMPLEMENT_EXPRESSIONNODE(NumElemFunctionNode,  UnaryFunctionNode);
IMPLEMENT_EXPRESSIONNODE(ToStringFunctionNode,   UnaryFunctionNode);
IMPLEMENT_EXPRESSIONNODE(ToRawFunctionNode,    UnaryFunctionNode);
IMPLEMENT_EXPRESSIONNODE(XorBitFunctionNode,   UnaryBitFunctionNode);
IMPLEMENT_EXPRESSIONNODE(MD5BitFunctionNode,   UnaryBitFunctionNode);

void
ExpressionNode::onArgument(const ResultNode & arg, ResultNode & result) const
{
    (void) arg;
    (void) result;
    throw std::runtime_error(make_string("Class %s does not implement onArgument(const ResultNode & arg, ResultNode & result). Probably an indication that it tries to take a multivalued argument, which it can not.", getClass().name()));
}

void
ExpressionNode::executeIterative(const ResultNode & arg, ResultNode & result) const
{
    onArgument(arg, result);
}

void
ExpressionNode::wireAttributes(const search::attribute::IAttributeContext &)
{
}


class ArithmeticTypeConversion
{
public:
    ArithmeticTypeConversion() :
      _typeConversion()
    {
        _typeConversion[IntegerResultNode::classId][IntegerResultNode::classId] = Int64ResultNode::classId;
        _typeConversion[IntegerResultNode::classId][FloatResultNode::classId]   = FloatResultNode::classId;
        _typeConversion[IntegerResultNode::classId][StringResultNode::classId]  = Int64ResultNode::classId;
        _typeConversion[IntegerResultNode::classId][RawResultNode::classId]     = Int64ResultNode::classId;
        _typeConversion[FloatResultNode::classId][IntegerResultNode::classId]   = FloatResultNode::classId;
        _typeConversion[FloatResultNode::classId][FloatResultNode::classId]     = FloatResultNode::classId;
        _typeConversion[FloatResultNode::classId][StringResultNode::classId]    = FloatResultNode::classId;
        _typeConversion[FloatResultNode::classId][RawResultNode::classId]       = FloatResultNode::classId;
        _typeConversion[StringResultNode::classId][IntegerResultNode::classId]  = Int64ResultNode::classId;
        _typeConversion[StringResultNode::classId][FloatResultNode::classId]    = FloatResultNode::classId;
        _typeConversion[StringResultNode::classId][StringResultNode::classId]   = StringResultNode::classId;
        _typeConversion[StringResultNode::classId][RawResultNode::classId]      = StringResultNode::classId;
        _typeConversion[RawResultNode::classId][IntegerResultNode::classId]     = Int64ResultNode::classId;
        _typeConversion[RawResultNode::classId][FloatResultNode::classId]       = FloatResultNode::classId;
        _typeConversion[RawResultNode::classId][StringResultNode::classId]      = StringResultNode::classId;
        _typeConversion[RawResultNode::classId][RawResultNode::classId]         = RawResultNode::classId;
    }
    ResultNode::UP getType(const ResultNode & arg1, const ResultNode & arg2);
    static ResultNode::UP getType(const ResultNode & arg);
private:
    static size_t getDimension(const ResultNode & r) {
        if (r.getClass().inherits(ResultNodeVector::classId)) {
            return 1 + getDimension(* r.createBaseType());
        } else {
            return 0;
        }
    }
    static size_t getBaseType(const ResultNode & r);
    static size_t getBaseType2(const ResultNode & r);
    size_t getType(size_t arg1, size_t arg2) const {
        return _typeConversion.find(arg1)->second.find(arg2)->second;
    }
    std::map<size_t, std::map<size_t, size_t> > _typeConversion;
};

ResultNode::UP
ArithmeticTypeConversion::getType(const ResultNode & arg1, const ResultNode & arg2)
{
    size_t baseTypeId = getType(getBaseType2(arg1), getBaseType2(arg2));
    size_t dimension = std::max(getDimension(arg1), getDimension(arg2));
    if (dimension == 0) {
        return ResultNode::UP(static_cast<ResultNode *>(Identifiable::classFromId(baseTypeId)->create()));
    } else if (dimension == 1) {
        if (baseTypeId == Int64ResultNode::classId) {
            return std::make_unique<IntegerResultNodeVector>();
        } else if (baseTypeId == FloatResultNode::classId) {
            return std::make_unique<FloatResultNodeVector>();
        } else {
            throw std::runtime_error("We can not handle anything but numbers.");
        }
    } else {
        throw std::runtime_error("We are not able to handle multidimensional arrays");
    }
    return ResultNode::UP();
}

ResultNode::UP
ArithmeticTypeConversion::getType(const ResultNode & arg)
{
    size_t baseTypeId = getBaseType(arg);
    return ResultNode::UP(static_cast<ResultNode *>(Identifiable::classFromId(baseTypeId)->create()));
}

size_t
ArithmeticTypeConversion::getBaseType(const ResultNode & r)
{
    if (r.getClass().inherits(ResultNodeVector::classId)) {
        return getBaseType(* r.createBaseType());
    } else {
        return r.getClass().id();
    }
}

size_t
ArithmeticTypeConversion::getBaseType2(const ResultNode & r)
{
    if (r.getClass().inherits(ResultNodeVector::classId)) {
        return getBaseType2(* r.createBaseType());
    } else if (r.getClass().inherits(IntegerResultNode::classId)) {
        return IntegerResultNode::classId;
    } else {
        return getBaseType(r);
    }
}

namespace {
    ArithmeticTypeConversion _ArithmeticTypeConversion;
}


void
MultiArgFunctionNode::onPrepare(bool preserveAccurateTypes)
{
    for(size_t i(0), m(_args.size()); i < m; i++) {
        _args[i]->prepare(preserveAccurateTypes);
    }
    prepareResult();
}

void
MultiArgFunctionNode::onPrepareResult()
{
    if (_args.size() == 1) {
        setResultType(ArithmeticTypeConversion::getType(*_args[0]->getResult()));
    } else if (_args.size() > 1) {
        setResultType(std::unique_ptr<ResultNode>(static_cast<ResultNode *>(_args[0]->getResult()->clone())));
        for(size_t i(1), m(_args.size()); i < m; i++) {
            if (_args[i]->getResult() != nullptr) {
                setResultType(_ArithmeticTypeConversion.getType(*getResult(), *_args[i]->getResult()));
            }
        }
    }
}

bool
MultiArgFunctionNode::onExecute() const
{
    for(size_t i(0), m(_args.size()); i < m; i++) {
        _args[i]->execute();
    }
    return calculate(_args, updateResult());
}

bool
MultiArgFunctionNode::onCalculate(const ExpressionNodeVector & args, ResultNode & result) const
{
    result.set(*args[0]->getResult());
    for (size_t i(1), m(args.size()); i < m; i++) {
        executeIterative(*args[i]->getResult(), result);
    }
    return true;
}

void
BitFunctionNode::onPrepareResult()
{
    setResultType(std::make_unique<Int64ResultNode>(0));
}

void
StrCatFunctionNode::onPrepareResult()
{
    setResultType(std::make_unique<StringResultNode>());
}

void
CatFunctionNode::onPrepareResult()
{
    setResultType(std::make_unique<RawResultNode>());
}

void
CatFunctionNode::onPrepare(bool preserveAccurateTypes)
{
    (void) preserveAccurateTypes;
    MultiArgFunctionNode::onPrepare(true);
}

void
BitFunctionNode::onArgument(const ResultNode & arg, ResultNode & result) const
{
    onArgument(arg, static_cast<Int64ResultNode &>(result));
}

void AddFunctionNode::onArgument(const ResultNode & arg, ResultNode & result)        const { static_cast<NumericResultNode &>(result).add(arg); }
void DivideFunctionNode::onArgument(const ResultNode & arg, ResultNode & result)     const { static_cast<NumericResultNode &>(result).divide(arg); }
void MultiplyFunctionNode::onArgument(const ResultNode & arg, ResultNode & result)   const { static_cast<NumericResultNode &>(result).multiply(arg); }
void ModuloFunctionNode::onArgument(const ResultNode & arg, ResultNode & result)     const { static_cast<NumericResultNode &>(result).modulo(arg); }
void MinFunctionNode::onArgument(const ResultNode & arg, ResultNode & result)        const { static_cast<NumericResultNode &>(result).min(arg); }
void MaxFunctionNode::onArgument(const ResultNode & arg, ResultNode & result)        const { static_cast<NumericResultNode &>(result).max(arg); }
void AndFunctionNode::onArgument(const ResultNode & arg, Int64ResultNode & result) const { result.andOp(arg); }
void OrFunctionNode::onArgument(const ResultNode & arg, Int64ResultNode & result)  const { result.orOp(arg); }
void XorFunctionNode::onArgument(const ResultNode & arg, Int64ResultNode & result) const { result.xorOp(arg); }

ResultNode::CP
MaxFunctionNode::getInitialValue() const
{
    ResultNode::CP initial;
    const ResultNode & arg(*getArg(0).getResult());
    if (arg.inherits(FloatResultNodeVector::classId)) {
        initial.reset(new FloatResultNode(std::numeric_limits<double>::min()));
    } else if (arg.inherits(IntegerResultNodeVector::classId)) {
        initial.reset(new Int64ResultNode(std::numeric_limits<int64_t>::min()));
    } else {
        throw std::runtime_error(vespalib::string("Can not choose an initial value for class ") + arg.getClass().name());
    }
    return initial;
}

ResultNode::CP
MinFunctionNode::getInitialValue() const
{
    ResultNode::CP initial;
    const ResultNode & arg(*getArg(0).getResult());
    if (arg.inherits(FloatResultNodeVector::classId)) {
        initial.reset(new FloatResultNode(std::numeric_limits<double>::max()));
    } else if (arg.inherits(IntegerResultNodeVector::classId)) {
        initial.reset(new Int64ResultNode(std::numeric_limits<int64_t>::max()));
    } else {
        throw std::runtime_error(vespalib::string("Can not choose an initial value for class ") + arg.getClass().name());
    }
    return initial;
}

ResultNode &
ModuloFunctionNode::flatten(const ResultNodeVector &, ResultNode &) const
{
   throw std::runtime_error("ModuloFunctionNode::flatten() const not implemented since it shall never be used.");
}

ResultNode &
DivideFunctionNode::flatten(const ResultNodeVector &, ResultNode &) const
{
   throw std::runtime_error("DivideFunctionNode::flatten() const not implemented since it shall never be used.");
}

ResultNode::CP
ModuloFunctionNode::getInitialValue() const
{
   throw std::runtime_error("ModuloFunctionNode::getInitialValue() const not implemented since it shall never be used.");
}

ResultNode::CP
DivideFunctionNode::getInitialValue() const
{
   throw std::runtime_error("DivideFunctionNode::getInitialValue() const not implemented since it shall never be used.");
}

UnaryBitFunctionNode::~UnaryBitFunctionNode() = default;

void
UnaryBitFunctionNode::onPrepareResult()
{
    setResultType(std::make_unique<RawResultNode>());
}

void
UnaryBitFunctionNode::onPrepare(bool preserveAccurateTypes)
{
    (void) preserveAccurateTypes;
    UnaryFunctionNode::onPrepare(true);
}

void
UnaryFunctionNode::onPrepareResult()
{
    setResultType(std::unique_ptr<ResultNode>(getArg().getResult()->clone()));
}

void
ToStringFunctionNode::onPrepareResult()
{
    setResultType(std::make_unique<StringResultNode>());
}

bool
ToStringFunctionNode::onExecute() const
{
    getArg().execute();
    updateResult().set(*getArg().getResult());
    return true;
}

void
ToRawFunctionNode::onPrepareResult()
{
    setResultType(std::make_unique<RawResultNode>());
}

bool
ToRawFunctionNode::onExecute() const
{
    getArg().execute();
    updateResult().set(*getArg().getResult());
    return true;
}

void
ToIntFunctionNode::onPrepareResult()
{
    setResultType(std::make_unique<Int64ResultNode>());
}

bool
ToIntFunctionNode::onExecute() const
{
    getArg().execute();
    updateResult().set(*getArg().getResult());
    return true;
}

void
ToFloatFunctionNode::onPrepareResult()
{
    setResultType(std::make_unique<FloatResultNode>());
}

bool
ToFloatFunctionNode::onExecute() const
{
    getArg().execute();
    updateResult().set(*getArg().getResult());
    return true;
}

void
StrLenFunctionNode::onPrepareResult()
{
    setResultType(std::make_unique<Int64ResultNode>());
}

bool
StrLenFunctionNode::onExecute() const
{
    getArg().execute();
    char buf[32];
    static_cast<Int64ResultNode &> (updateResult()).set(getArg().getResult()->getString(BufferRef(buf, sizeof(buf))).size());
    return true;
}

void
NormalizeSubjectFunctionNode::onPrepareResult()
{
    setResultType(std::make_unique<StringResultNode>());
}

bool
NormalizeSubjectFunctionNode::onExecute() const
{
    getArg().execute();
    char buf[32];
    ConstBufferRef tmp(getArg().getResult()->getString(BufferRef(buf, sizeof(buf))));

    int pos = 0;
    if (tmp.size() >= 4) {
        if ((tmp[0] == 'R') && ((tmp[1] | 0x20) == 'e') && (tmp[2] == ':') && (tmp[3] == ' ')) {
            pos = 4;
        } else if ((tmp[0] == 'F') && ((tmp[1] | 0x20) == 'w')) {
            if ((tmp[2] == ':') && (tmp[3] == ' ')) {
                pos = 4;
            } else if (((tmp[2] | 0x20) == 'd') && (tmp[3] == ':') && (tmp[4] == ' ')) {
                pos = 5;
            }
        }
    }
    static_cast<StringResultNode &> (updateResult()).set(vespalib::stringref(tmp.c_str() + pos, tmp.size() - pos));
    return true;
}

void
NumElemFunctionNode::onPrepareResult()
{
    setResultType(std::make_unique<Int64ResultNode>(1));
}

bool
NumElemFunctionNode::onExecute() const
{
    getArg().execute();
    if (getArg().getResult()->inherits(ResultNodeVector::classId)) {
        static_cast<Int64ResultNode &> (updateResult()).set(static_cast<const ResultNodeVector &>(*getArg().getResult()).size());
    }
    return true;
}

bool
NegateFunctionNode::onExecute() const
{
    getArg().execute();
    updateResult().assign(*getArg().getResult());
    updateResult().negate();
    return true;
}

bool
SortFunctionNode::onExecute() const
{
    getArg().execute();
    updateResult().assign(*getArg().getResult());
    updateResult().sort();
    return true;
}

bool
ReverseFunctionNode::onExecute() const
{
    getArg().execute();
    updateResult().assign(*getArg().getResult());
    updateResult().reverse();
    return true;
}

bool
StrCatFunctionNode::onExecute() const
{
    asciistream os;
    StrCatSerializer nos(os);
    for(size_t i(0), m(getNumArgs()); i < m; i++) {
        getArg(i).execute();
        getArg(i).getResult()->serialize(nos);
    }
    static_cast<StringResultNode &>(updateResult()).set(os.str());
    return true;
}

bool
CatFunctionNode::onExecute() const
{
    nbostream os;
    CatSerializer nos(os);
    for(size_t i(0), m(getNumArgs()); i < m; i++) {
        getArg(i).execute();
        getArg(i).getResult()->serialize(nos);
    }
    static_cast<RawResultNode &>(updateResult()).setBuffer(os.data(), os.size());
    return true;
}

XorBitFunctionNode::XorBitFunctionNode() = default;
XorBitFunctionNode::~XorBitFunctionNode() = default;

XorBitFunctionNode::XorBitFunctionNode(ExpressionNode::UP arg, unsigned numBits) :
    UnaryBitFunctionNode(std::move(arg), numBits),
    _tmpXor(getNumBytes(), 0)
{}

bool
UnaryBitFunctionNode::onExecute() const
{
    _tmpOs.clear();
    getArg().execute();
    CatSerializer os(_tmpOs);
    getArg().getResult()->serialize(os);
    return internalExecute(_tmpOs);
}

void
XorBitFunctionNode::onPrepareResult()
{
    UnaryBitFunctionNode::onPrepareResult();
    _tmpXor.resize(getNumBytes());
}

bool
XorBitFunctionNode::internalExecute(const nbostream & os) const
{
    const size_t numBytes(_tmpXor.size());
    memset(&_tmpXor[0], 0, numBytes);
    const char * s(os.data());
    for (size_t i(0), m(os.size()/numBytes); i < m; i++) {
        for (size_t j(0), k(numBytes); j < k; j++) {
            _tmpXor[j] ^= s[j + k*i];
        }
    }
    for (size_t i((os.size()/numBytes)*numBytes); i < os.size(); i++) {
        _tmpXor[i%numBytes] = os.data()[i];
    }
    static_cast<RawResultNode &>(updateResult()).setBuffer(&_tmpXor[0], numBytes);
    return true;
}

bool
MD5BitFunctionNode::internalExecute(const nbostream & os) const
{
    const unsigned int MD5_DIGEST_LENGTH = 16;
    unsigned char md5ScratchPad[MD5_DIGEST_LENGTH];
    fastc_md5sum(os.data(), os.size(), md5ScratchPad);
    static_cast<RawResultNode &>(updateResult()).setBuffer(md5ScratchPad, std::min(sizeof(md5ScratchPad), getNumBytes()));
    return true;
}

Serializer &
FunctionNode::onSerialize(Serializer & os) const
{
    return os << _tmpResult;
}
Deserializer &
FunctionNode::onDeserialize(Deserializer & is)
{
    return is >> _tmpResult;
}

void
ConstantNode::visitMembers(vespalib::ObjectVisitor &visitor) const
{
    visit(visitor, "Value", _result);
}

Serializer &
ConstantNode::onSerialize(Serializer & os) const
{
    return os << _result;
}
Deserializer &
ConstantNode::onDeserialize(Deserializer & is)
{
    return is >> _result;
}



void
FunctionNode::visitMembers(vespalib::ObjectVisitor & visitor) const
{
    visit(visitor, "tmpResult", _tmpResult);
}

void
FunctionNode::selectMembers(const vespalib::ObjectPredicate & predicate, vespalib::ObjectOperation & operation)
{
    if (_tmpResult.get()) {
        _tmpResult->select(predicate, operation);
    }
}

void
MultiArgFunctionNode::selectMembers(const vespalib::ObjectPredicate & predicate, vespalib::ObjectOperation & operation)
{
    FunctionNode::selectMembers(predicate, operation);
    for(size_t i(0), m(_args.size()); i < m; i++) {
        _args[i]->select(predicate, operation);
    }
}

Serializer &
MultiArgFunctionNode::onSerialize(Serializer & os) const
{
    FunctionNode::onSerialize(os);
    os << _args;
    return os;
}

Deserializer &
MultiArgFunctionNode::onDeserialize(Deserializer & is)
{
    FunctionNode::onDeserialize(is);
    return is >> _args;
}

MultiArgFunctionNode::MultiArgFunctionNode() noexcept : FunctionNode() { }
MultiArgFunctionNode::MultiArgFunctionNode(const MultiArgFunctionNode &) = default;
MultiArgFunctionNode & MultiArgFunctionNode::operator = (const MultiArgFunctionNode &) = default;

MultiArgFunctionNode::~MultiArgFunctionNode() = default;

void
MultiArgFunctionNode::visitMembers(vespalib::ObjectVisitor &visitor) const
{
    FunctionNode::visitMembers(visitor);
    visit(visitor, "args", _args);
}

Serializer &
UnaryBitFunctionNode::onSerialize(Serializer & os) const
{
    UnaryFunctionNode::onSerialize(os);
    return os << _numBits;
}
Deserializer &
UnaryBitFunctionNode::onDeserialize(Deserializer & is)
{
    UnaryFunctionNode::onDeserialize(is);
    return is >> _numBits;
}

void
UnaryBitFunctionNode::visitMembers(vespalib::ObjectVisitor &visitor) const
{
    UnaryFunctionNode::visitMembers(visitor);
    visit(visitor, "numBits", _numBits);
}

}