aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/select/valuenodes.h
blob: f6fa0400d7cca5e6793d8dd205b7014e9d5e9c5d (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "valuenode.h"
#include <vespa/document/base/fieldpath.h>

namespace document {
    class BucketIdFactory;
    class DocumentId;
    class BucketId;
    class DocumentType;
}

namespace document::select {

class InvalidValueNode : public ValueNode
{
    vespalib::string _name;
public:
    InvalidValueNode(vespalib::stringref name);

    std::unique_ptr<Value> getValue(const Context&) const override {
        return std::make_unique<InvalidValue>();
    }

    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
    void visit(Visitor& visitor) const override;

    ValueNode::UP clone() const override {
        return wrapParens(new InvalidValueNode(_name));
    }
};

class NullValueNode : public ValueNode
{
public:
    NullValueNode();

    std::unique_ptr<Value> getValue(const Context&) const override {
        return std::make_unique<NullValue>();
    }

    void print(std::ostream& out, bool verbose, const std::string& indent) const override;

    void visit(Visitor& visitor) const override;

    ValueNode::UP clone() const override {
        return wrapParens(new NullValueNode());
    }
};

class StringValueNode : public ValueNode
{
    vespalib::string _value;
public:
    explicit StringValueNode(vespalib::stringref val);

    const vespalib::string& getValue() const { return _value; }

    std::unique_ptr<Value> getValue(const Context&) const override {
        return std::make_unique<StringValue>(_value);
    }

    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
    void visit(Visitor& visitor) const override;

    ValueNode::UP clone() const override {
        return wrapParens(new StringValueNode(_value));
    }
};

class IntegerValueNode : public ValueNode
{
    int64_t _value;
    bool _isBucketValue;
public:
    IntegerValueNode(int64_t val, bool isBucketValue)
        : _value(val), _isBucketValue(isBucketValue) {}

    int64_t getValue() const { return _value; }

    std::unique_ptr<Value> getValue(const Context&) const override {
        return std::make_unique<IntegerValue>(_value, _isBucketValue);
    }

    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
    void visit(Visitor& visitor) const override;

    ValueNode::UP clone() const override {
        return wrapParens(new IntegerValueNode(_value, _isBucketValue));
    }
};

class CurrentTimeValueNode : public ValueNode
{
public:
    int64_t getValue() const;

    std::unique_ptr<Value> getValue(const Context&) const override {
        return std::make_unique<IntegerValue>(getValue(), false);
    }

    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
    void visit(Visitor& visitor) const override;

    ValueNode::UP clone() const override {
        return wrapParens(new CurrentTimeValueNode);
    }
};

class VariableValueNode : public ValueNode
{
    vespalib::string _value;
public:
    // TODO stringref
    VariableValueNode(const vespalib::string & variableName) : _value(variableName) {}

    const vespalib::string& getVariableName() const { return _value; }

    std::unique_ptr<Value> getValue(const Context& context) const override;
    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
    void visit(Visitor& visitor) const override;

    ValueNode::UP clone() const override {
        return wrapParens(new VariableValueNode(_value));
    }
};

class FloatValueNode : public ValueNode
{
    double _value;
public:
    FloatValueNode(double val) : _value(val) {}

    double getValue() const { return _value; }

    std::unique_ptr<Value> getValue(const Context&) const override {
        return std::make_unique<FloatValue>(_value);
    }

    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
    void visit(Visitor& visitor) const override;

    ValueNode::UP clone() const override {
        return wrapParens(new FloatValueNode(_value));
    }
};

class FieldValueNode : public ValueNode
{
    vespalib::string _doctype;
    vespalib::string _fieldExpression;
    vespalib::string _fieldName;
    mutable FieldPath _fieldPath;

public:
    FieldValueNode(const vespalib::string& doctype, const vespalib::string& fieldExpression);
    FieldValueNode(const FieldValueNode &) = delete;
    FieldValueNode & operator = (const FieldValueNode &) = delete;
    FieldValueNode(FieldValueNode &&) = default;
    FieldValueNode & operator = (FieldValueNode &&) = default;
    ~FieldValueNode() override;

    const vespalib::string& getDocType() const { return _doctype; }
    const vespalib::string& getRealFieldName() const { return _fieldName; }
    const vespalib::string& getFieldName() const { return _fieldExpression; }

    std::unique_ptr<Value> getValue(const Context& context) const override;
    std::unique_ptr<Value> traceValue(const Context &context, std::ostream& out) const override;
    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
    void visit(Visitor& visitor) const override;

    ValueNode::UP clone() const override {
        return wrapParens(new FieldValueNode(_doctype, _fieldExpression));
    }

    static vespalib::string extractFieldName(const vespalib::string & fieldExpression);

private:

    void initFieldPath(const DocumentType&) const;
};

class FunctionValueNode;

// Only used by the parser to build a partial field expression. Never part of
// an AST tree returned to the caller.
class FieldExprNode final : public ValueNode {
    std::unique_ptr<FieldExprNode> _left_expr;
    vespalib::string _right_expr;
public:
    explicit FieldExprNode(const vespalib::string& doctype) : _left_expr(), _right_expr(doctype) {}
    FieldExprNode(std::unique_ptr<FieldExprNode> left_expr, vespalib::stringref right_expr)
        : ValueNode(left_expr->max_depth() + 1),
          _left_expr(std::move(left_expr)),
          _right_expr(right_expr)
    {}
    FieldExprNode(const FieldExprNode &) = delete;
    FieldExprNode & operator = (const FieldExprNode &) = delete;
    FieldExprNode(FieldExprNode &&) = default;
    FieldExprNode & operator = (FieldExprNode &&) = default;
    ~FieldExprNode() override;

    std::unique_ptr<FieldValueNode> convert_to_field_value() const;
    std::unique_ptr<FunctionValueNode> convert_to_function_call() const;
private:
    void build_mangled_expression(vespalib::string& dest) const;
    const vespalib::string& resolve_doctype() const;

    // These are not used, can just return dummy values.
    std::unique_ptr<Value> getValue(const Context& context) const override {
        (void) context;
        return std::unique_ptr<Value>();
    }
    std::unique_ptr<Value> traceValue(const Context &context, std::ostream& out) const override {
        (void) context;
        (void) out;
        return std::unique_ptr<Value>();
    }
    void print(std::ostream& out, bool verbose, const std::string& indent) const override {
        (void) out;
        (void) verbose;
        (void) indent;
    }
    void visit(Visitor& visitor) const override {
        (void) visitor;
    }

    ValueNode::UP clone() const override {
        if (_left_expr) {
            return wrapParens(new FieldExprNode(std::unique_ptr<FieldExprNode>(
                    static_cast<FieldExprNode*>(_left_expr->clone().release())), _right_expr));
        } else {
            return wrapParens(new FieldExprNode(_right_expr));
        }
    }
};

class IdValueNode : public ValueNode
{
public:
    enum Type { SCHEME, NS, TYPE, USER, GROUP, GID, SPEC, BUCKET, ALL };

    IdValueNode(const BucketIdFactory& bucketIdFactory,
                vespalib::stringref name, vespalib::stringref type,
                int widthBits = -1, int divisionBits = -1);

    Type getType() const { return _type; }

    std::unique_ptr<Value> getValue(const Context& context) const override;

    std::unique_ptr<Value> getValue(const DocumentId& id) const;

    std::unique_ptr<Value> traceValue(const Context& context, std::ostream &out) const override;

    std::unique_ptr<Value> traceValue(const DocumentId& val, std::ostream& out) const;

    void print(std::ostream& out, bool verbose, const std::string& indent) const override;

    void visit(Visitor& visitor) const override;

    ValueNode::UP clone() const override {
        return wrapParens(new IdValueNode(_bucketIdFactory, _id, _typestring, _widthBits, _divisionBits));
    }

    int getWidthBits() const { return _widthBits; }
    int getDivisionBits() const { return _divisionBits; }

private:
    const BucketIdFactory& _bucketIdFactory;
    vespalib::string _id;
    vespalib::string _typestring;
    Type _type;
    int _widthBits;
    int _divisionBits;
};

class FunctionValueNode : public ValueNode
{
public:
    enum Function { LOWERCASE, HASH, ABS };

    FunctionValueNode(vespalib::stringref name, std::unique_ptr<ValueNode> src);

    Function getFunction() const { return _function; }
    const vespalib::string &getFunctionName(void) const { return _funcname; }

    std::unique_ptr<Value> getValue(const Context& context) const override {
        return getValue(_source->getValue(context));
    }

    std::unique_ptr<Value> traceValue(const Context &context, std::ostream& out) const override {
        return traceValue(_source->getValue(context), out);
    }

    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
    void visit(Visitor& visitor) const override;

    ValueNode::UP clone() const override {
        return wrapParens(new FunctionValueNode(_funcname, _source->clone()));
    }

    const ValueNode& getChild() const { return *_source; }

private:
    Function _function;
    vespalib::string _funcname;
    std::unique_ptr<ValueNode> _source;

    virtual std::unique_ptr<Value> getValue(std::unique_ptr<Value> val) const;
    virtual std::unique_ptr<Value> traceValue(std::unique_ptr<Value> val,
                                            std::ostream& out) const;
};

class ArithmeticValueNode : public ValueNode
{
public:
    enum Operator { ADD, SUB, MUL, DIV, MOD };

    ArithmeticValueNode(std::unique_ptr<ValueNode> left,
                        vespalib::stringref op,
                        std::unique_ptr<ValueNode> right);

    Operator getOperator() const { return _operator; }
    const char* getOperatorName() const;

    std::unique_ptr<Value>
    getValue(const Context& context) const override {
        return getValue(_left->getValue(context), _right->getValue(context));
    }
    
    std::unique_ptr<Value>
    traceValue(const Context &context, std::ostream& out) const override {
        return traceValue(_left->getValue(context), _right->getValue(context), out);
    }

    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
    void visit(Visitor& visitor) const override;

    ValueNode::UP clone() const override {
        return wrapParens(new ArithmeticValueNode(_left->clone(),
                                                  getOperatorName(),
                                                  _right->clone()));
    }

    const ValueNode& getLeft() const { return *_left; }
    const ValueNode& getRight() const { return *_right; }

private:
    Operator _operator;
    std::unique_ptr<ValueNode> _left;
    std::unique_ptr<ValueNode> _right;

    virtual std::unique_ptr<Value> getValue(std::unique_ptr<Value> lval,
                                          std::unique_ptr<Value> rval) const;
    virtual std::unique_ptr<Value> traceValue(std::unique_ptr<Value> lval,
                                            std::unique_ptr<Value> rval,
                                            std::ostream&) const;
};

}