aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/select/compare.cpp
blob: dea8b5b9a4a274656c9ecd4c1d228820a1e95380 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "compare.h"
#include "valuenode.h"
#include "visitor.h"
#include <vespa/document/datatype/datatype.h>
#include <vespa/document/fieldvalue/document.h>
#include <vespa/document/util/stringutil.h>
#include <ostream>

namespace document::select {

Compare::Compare(std::unique_ptr<ValueNode> left,
                 const Operator& op,
                 std::unique_ptr<ValueNode> right,
                 const BucketIdFactory& bucketIdFactory)
    : Node("Compare", std::max(left->max_depth(), right->max_depth()) + 1),
      _left(std::move(left)),
      _right(std::move(right)),
      _operator(op),
      _bucketIdFactory(bucketIdFactory)
{
}

Compare::~Compare() = default;

Node::UP
Compare::clone() const
{
    return wrapParens(new Compare(_left->clone(),
                                  _operator,
                                  _right->clone(),
                                  _bucketIdFactory));
}

namespace {

    template<typename T>
    ResultList containsValue(const T& value, const ValueNode& l, const ValueNode& r,
                         const Operator& op)
    {
        std::unique_ptr<Value> left(l.getValue(value));
        std::unique_ptr<Value> right(r.getValue(value));
        if (left->getType() == Value::Bucket
            || right->getType() == Value::Bucket)
        {
            Value& bVal(left->getType() == Value::Bucket ? *left : *right);
            Value& nVal(left->getType() == Value::Bucket ? *right : *left);
            if (nVal.getType() == Value::Integer
                && (op == FunctionOperator::EQ || op == FunctionOperator::NE
                    || op == GlobOperator::GLOB))
            {
                document::BucketId b( static_cast<IntegerValue&>(bVal).getValue());
                document::BucketId s( static_cast<IntegerValue&>(nVal).getValue());

                if (op == FunctionOperator::NE) {
                    return ! ResultList(Result::get(s.contains(b)));
                }
                return ResultList(Result::get(s.contains(b)));
            } else {
                return ResultList(Result::Invalid);
            }
        }
        return op.compare(*left, *right);
    }

    template<typename T>
    ResultList traceValue(const T& value, const ValueNode& l, const ValueNode& r,
                      const Operator& op, std::ostream& out)
    {
        std::unique_ptr<Value> left(l.traceValue(value, out));
        std::unique_ptr<Value> right(r.traceValue(value, out));
        if (left->getType() == Value::Bucket
            || right->getType() == Value::Bucket)
        {
            Value& bVal(left->getType() == Value::Bucket ? *left : *right);
            Value& nVal(left->getType() == Value::Bucket ? *right : *left);
            if (nVal.getType() == Value::Integer
                && (op == FunctionOperator::EQ || op == FunctionOperator::NE
                    || op == GlobOperator::GLOB))
            {
                document::BucketId b(
                        static_cast<IntegerValue&>(bVal).getValue());
                document::BucketId s(
                        static_cast<IntegerValue&>(nVal).getValue());

                ResultList resultList = (op == FunctionOperator::NE)
                        ? !ResultList(Result::get(s.contains(b)))
                        : ResultList(Result::get(s.contains(b)));

                out << "Checked if " << b.toString() << " is ";
                if (op == FunctionOperator::NE) { out << "not "; }
                out << "contained in " << s.toString()
                    << ". Result was " << resultList << ".\n";
                return resultList;
            } else {
                out << "Compare type " << left->getType() << " vs "
                    << right->getType() << " - Result is thus invalid.\n";

                return ResultList(Result::Invalid);
            }
        }
        out << "Compare - Left value ";
        left->print(out, false, "");
        out << " " << op.getName() << " right value ";
        right->print(out, false, "");
        out << "\n";
        ResultList result = op.trace(*left, *right, out);
        out << "Result from compare was " << result << ".\n";
        return result;
    }
}

ResultList
Compare::contains(const Context& context) const
{
    return containsValue<Context>(context, *_left, *_right, _operator);
}

ResultList
Compare::trace(const Context& context, std::ostream& out) const
{
    return traceValue(context, *_left, *_right, _operator, out);
}


void
Compare::visit(Visitor &v) const
{
    v.visitComparison(*this);
}


void
Compare::print(std::ostream& out, bool verbose, const std::string& indent) const
{
    if (_parentheses) out << '(';
    _left->print(out, verbose, indent);
    out << " ";
    _operator.print(out, verbose, indent);
    out << " ";
    _right->print(out, verbose, indent);
    if (_parentheses) out << ')';
}

}