summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/grouping/vespa/RequestBuilder.java
blob: 5ee6f7bc604a44fa6f6de0e5ad3c0b445c812954 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.grouping.vespa;

import com.yahoo.search.grouping.Continuation;
import com.yahoo.search.grouping.GroupingRequest;
import com.yahoo.search.grouping.request.AllOperation;
import com.yahoo.search.grouping.request.EachOperation;
import com.yahoo.search.grouping.request.GroupingExpression;
import com.yahoo.search.grouping.request.GroupingOperation;
import com.yahoo.search.grouping.request.NegFunction;
import com.yahoo.searchlib.aggregation.*;
import com.yahoo.searchlib.expression.ExpressionNode;

import java.util.*;

/**
 * This class implements the necessary logic to build a list of {@link Grouping} objects from an instance of {@link
 * GroupingOperation}. It is used by the {@link GroupingExecutor}.
 *
 * @author Simon Thoresen Hult
 */
class RequestBuilder {

    private static final int LOOKAHEAD = 1;
    private final ExpressionConverter converter = new ExpressionConverter();
    private final List<Grouping> requestList = new LinkedList<>();
    private final GroupingTransform transform;
    private GroupingOperation root;
    private int tag = 0;
    private int defaultMaxHits = -1;
    private int defaultMaxGroups = -1;

    /**
     * Constructs a new instance of this class.
     *
     * @param requestId The id of the corresponding {@link GroupingRequest}.
     */
    public RequestBuilder(int requestId) {
        this.transform = new GroupingTransform(requestId);
    }

    /**
     * Sets the abstract syntax tree of the request whose back-end queries to create.
     *
     * @param root The grouping request to convert.
     * @return This, to allow chaining.
     */
    public RequestBuilder setRootOperation(GroupingOperation root) {
        root.getClass(); // throws NullPointerException
        this.root = root;
        return this;
    }

    /**
     * Sets the time zone to build the request for. This information is propagated to the time-based grouping
     * expressions so that the produced groups are reasonable for the given zone.
     *
     * @param timeZone The time zone to set.
     * @return This, to allow chaining.
     */
    public RequestBuilder setTimeZone(TimeZone timeZone) {
        converter.setTimeOffset(timeZone != null ? timeZone.getOffset(System.currentTimeMillis())
                                                 : ExpressionConverter.DEFAULT_TIME_OFFSET);
        return this;
    }

    /**
     * Sets the name of the summary class to use if a {@link com.yahoo.search.grouping.request.SummaryValue} has none.
     *
     * @param summaryName The summary class name to set.
     * @return This, to allow chaining.
     */
    public RequestBuilder setDefaultSummaryName(String summaryName) {
        converter.setDefaultSummaryName(summaryName != null ? summaryName
                                                            : ExpressionConverter.DEFAULT_SUMMARY_NAME);
        return this;
    }

    /**
     * Returns the transform that was created when {@link #build()} was called.
     *
     * @return The grouping transform that was built.
     */
    public GroupingTransform getTransform() {
        return transform;
    }

    /**
     * Returns the list of grouping objects that were created when {@link #build()} was called.
     *
     * @return The list of built grouping objects.
     */
    public List<Grouping> getRequestList() {
        return requestList;
    }

    /**
     * Constructs a set of Vespa specific grouping request that corresponds to the parameters given to this builder.
     * This method might fail due to unsupported constructs in the request, in which case an exception is thrown.
     *
     * @throws IllegalStateException         If this method is called more than once.
     * @throws UnsupportedOperationException If the grouping request contains unsupported constructs.
     */
    public void build() {
        if (tag != 0) {
            throw new IllegalStateException();
        }
        root.resolveLevel(1);

        Grouping grouping = new Grouping();
        grouping.getRoot().setTag(++tag);
        grouping.setForceSinglePass(root.getForceSinglePass() || root.containsHint("singlepass"));
        Stack<BuildFrame> stack = new Stack<>();
        stack.push(new BuildFrame(grouping, new BuildState(), root));
        while (!stack.isEmpty()) {
            BuildFrame frame = stack.pop();
            processRequestNode(frame);
            List<GroupingOperation> children = frame.astNode.getChildren();
            if (children.isEmpty()) {
                requestList.add(frame.grouping);
            } else {
                for (int i = children.size(); --i >= 0; ) {
                    Grouping childGrouping = (i == 0) ? frame.grouping : frame.grouping.clone();
                    BuildState childState = (i == 0) ? frame.state : new BuildState(frame.state);
                    BuildFrame child = new BuildFrame(childGrouping, childState, children.get(i));
                    stack.push(child);
                }
            }
        }
        pruneRequests();
    }

    public RequestBuilder addContinuations(Iterable<Continuation> continuations) {
        for (Continuation continuation : continuations) {
            if (continuation == null) {
                continue;
            }
            transform.addContinuation(continuation);
        }
        return this;
    }

    public RequestBuilder setDefaultMaxGroups(int v) { this.defaultMaxGroups = v; return this; }

    public RequestBuilder setDefaultMaxHits(int v) { this.defaultMaxHits = v; return this; }

    private void processRequestNode(BuildFrame frame) {
        int level = frame.astNode.getLevel();
        if (level > 2) {
            throw new UnsupportedOperationException("Can not operate on " +
                                                    GroupingOperation.getLevelDesc(level) + ".");
        }
        if (frame.astNode instanceof EachOperation) {
            resolveEach(frame);
        } else {
            resolveOutput(frame);
        }
        resolveState(frame);
        injectGroupByToExpressionCountAggregator(frame);
    }

    private void injectGroupByToExpressionCountAggregator(BuildFrame frame) {
        Group group = getLeafGroup(frame);
        // The ExpressionCountAggregationResult uses the group-by expression to simulate aggregation of list of groups.
        group.getAggregationResults().stream()
                .filter(aggr -> aggr instanceof ExpressionCountAggregationResult)
                .forEach(aggr -> aggr.setExpression(frame.state.groupBy.clone()));
    }

    private void resolveEach(BuildFrame frame) {
        int parentTag = getLeafGroup(frame).getTag();
        if (frame.state.groupBy != null) {
            GroupingLevel grpLevel = new GroupingLevel();
            grpLevel.getGroupPrototype().setTag(++tag);
            grpLevel.setExpression(frame.state.groupBy);
            frame.state.groupBy = null;
            int offset = transform.getOffset(tag);
            if (frame.state.precision != null) {
                grpLevel.setPrecision(frame.state.precision + offset);
                frame.state.precision = null;
            }
            int max = -1;
            if (frame.state.max != null) {
                max = frame.state.max;
                frame.state.max = null;
            } else if (defaultMaxGroups >= 0) {
                max = defaultMaxGroups;
            }
            if (max >= 0) {
                transform.putMax(tag, max, "group list");
                grpLevel.setMaxGroups(LOOKAHEAD + max + offset);
            }
            frame.grouping.getLevels().add(grpLevel);
        }
        String label = frame.astNode.getLabel();
        if (label != null) {
            frame.state.label = label;
        }
        if (frame.astNode.getLevel() > 0) {
            transform.putLabel(parentTag, getLeafGroup(frame).getTag(), frame.state.label, "group list");
        }
        resolveOutput(frame);
        if (!frame.state.orderByExp.isEmpty()) {
            GroupingLevel grpLevel = getLeafGroupingLevel(frame);
            for (int i = 0, len = frame.state.orderByExp.size(); i < len; ++i) {
                grpLevel.getGroupPrototype().addOrderBy(frame.state.orderByExp.get(i),
                                                        frame.state.orderByAsc.get(i));
            }
            frame.state.orderByExp.clear();
            frame.state.orderByAsc.clear();
        }
    }

    private void resolveState(BuildFrame frame) {
        resolveGroupBy(frame);
        resolveMax(frame);
        resolveOrderBy(frame);
        resolvePrecision(frame);
        resolveWhere(frame);
    }

    private void resolveGroupBy(BuildFrame frame) {
        GroupingExpression exp = frame.astNode.getGroupBy();
        if (exp != null) {
            if (frame.state.groupBy != null) {
                throw new UnsupportedOperationException("Can not group list of groups.");
            }
            frame.state.groupBy = converter.toExpressionNode(exp);
            frame.state.label = exp.toString(); // label for next each()

        } else {
            int level = frame.astNode.getLevel();
            if (level == 0) {
                // no next each()
            } else if (level == 1) {
                frame.state.label = "hits"; // next each() is hitlist
            } else {
                throw new UnsupportedOperationException("Can not create anonymous " +
                                                        GroupingOperation.getLevelDesc(level) + ".");
            }
        }
    }

    private long computeNewTopN(long oldMax, long newMax) {
        return (oldMax < 0) ? newMax : Math.min(oldMax, newMax);
    }
    private void resolveMax(BuildFrame frame) {
        if (frame.astNode.hasMax()) {
            int max = frame.astNode.getMax();
            if (isTopNAllowed(frame)) {
                frame.grouping.setTopN(computeNewTopN(frame.grouping.getTopN(), max));
            } else {
                frame.state.max = max;
            }
        }
    }

    private void resolveOrderBy(BuildFrame frame) {
        List<GroupingExpression> lst = frame.astNode.getOrderBy();
        if (lst == null || lst.isEmpty()) {
            return;
        }
        int reqLevel = frame.astNode.getLevel();
        if (reqLevel != 2) {
            throw new UnsupportedOperationException(
                    "Can not order " + GroupingOperation.getLevelDesc(reqLevel) + " content.");
        }
        for (GroupingExpression exp : lst) {
            boolean asc = true;
            if (exp instanceof NegFunction) {
                asc = false;
                exp = ((NegFunction)exp).getArg(0);
            }
            frame.state.orderByExp.add(converter.toExpressionNode(exp));
            frame.state.orderByAsc.add(asc);
        }
    }

    private void resolveOutput(BuildFrame frame) {
        List<GroupingExpression> lst = frame.astNode.getOutputs();
        if (lst == null || lst.isEmpty()) {
            return;
        }
        Group group = getLeafGroup(frame);
        for (GroupingExpression exp : lst) {
            group.addAggregationResult(toAggregationResult(exp, group, frame));
        }
    }

    private AggregationResult toAggregationResult(GroupingExpression exp, Group group, BuildFrame frame) {
        AggregationResult result = converter.toAggregationResult(exp);
        result.setTag(++tag);

        String label = exp.getLabel();
        if (result instanceof HitsAggregationResult) {
            if (label != null) {
                throw new UnsupportedOperationException("Can not label expression '" + exp + "'.");
            }
            HitsAggregationResult hits = (HitsAggregationResult)result;
            int max = -1;
            if (frame.state.max != null) {
                max = frame.state.max;
                frame.state.max = null;
            } else if (defaultMaxHits >= 0) {
                max = defaultMaxHits;
            }
            if (max >= 0) {
                transform.putMax(tag, max, "hit list");
                int offset = transform.getOffset(tag);
                hits.setMaxHits(LOOKAHEAD + max + offset);
            }
            transform.putLabel(group.getTag(), tag, frame.state.label, "hit list");
        } else {
            transform.putLabel(group.getTag(), tag, label != null ? label : exp.toString(), "output");
        }
        return result;
    }

    private void resolvePrecision(BuildFrame frame) {
        int precision = frame.astNode.getPrecision();
        if (precision > 0) {
            frame.state.precision = precision;
        }
    }

    private void resolveWhere(BuildFrame frame) {
        String where = frame.astNode.getWhere();
        if (where != null) {
            if (!isRootOperation(frame)) {
                throw new UnsupportedOperationException("Can not apply 'where' to non-root group.");
            }
            switch (where) {
            case "true":
                frame.grouping.setAll(true);
                break;
            case "$query":
                // ignore
                break;
            default:
                throw new UnsupportedOperationException("Operation 'where' does not support '" + where + "'.");
            }
        }
    }

    private boolean isRootOperation(BuildFrame frame) {
        return frame.astNode == root && frame.state.groupBy == null;
    }

    private boolean isTopNAllowed(BuildFrame frame) {
        return (frame.astNode instanceof AllOperation) && (frame.state.groupBy == null);
    }

    private GroupingLevel getLeafGroupingLevel(BuildFrame frame) {
        if (frame.grouping.getLevels().isEmpty()) {
            return null;
        }
        return frame.grouping.getLevels().get(frame.grouping.getLevels().size() - 1);
    }

    private Group getLeafGroup(BuildFrame frame) {
        if (frame.grouping.getLevels().isEmpty()) {
            return frame.grouping.getRoot();
        } else {
            GroupingLevel grpLevel = getLeafGroupingLevel(frame);
            return grpLevel != null ? grpLevel.getGroupPrototype() : null;
        }
    }

    private void pruneRequests() {
        for (int reqIdx = requestList.size(); --reqIdx >= 0; ) {
            Grouping request = requestList.get(reqIdx);
            List<GroupingLevel> lst = request.getLevels();
            for (int lvlIdx = lst.size(); --lvlIdx >= 0; ) {
                if (!lst.get(lvlIdx).getGroupPrototype().getAggregationResults().isEmpty()) {
                    break;
                }
                lst.remove(lvlIdx);
            }
            if (lst.isEmpty() && request.getRoot().getAggregationResults().isEmpty()) {
                requestList.remove(reqIdx);
            }
        }
    }

    private static class BuildFrame {

        final Grouping grouping;
        final BuildState state;
        final GroupingOperation astNode;

        BuildFrame(Grouping grouping, BuildState state, GroupingOperation astNode) {
            this.grouping = grouping;
            this.state = state;
            this.astNode = astNode;
        }
    }

    private static class BuildState {

        final List<ExpressionNode> orderByExp = new ArrayList<>();
        final List<Boolean> orderByAsc = new ArrayList<>();
        ExpressionNode groupBy = null;
        String label = null;
        Integer max = null;
        Integer precision = null;

        BuildState() {
            // empty
        }

        BuildState(BuildState obj) {
            for (ExpressionNode e : obj.orderByExp) {
                orderByExp.add(e.clone());
            }
            orderByAsc.addAll(obj.orderByAsc);
            groupBy = obj.groupBy;
            label = obj.label;
            max = obj.max;
            precision = obj.precision;
        }
    }
}