blob: db3ce811b634f708d606e4fd26278cb4f6cc2e0d (
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
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query;
import com.yahoo.processing.request.CompoundName;
import com.yahoo.search.Query;
import com.yahoo.search.grouping.GroupingRequest;
import com.yahoo.search.query.parser.ParserEnvironment;
import com.yahoo.search.query.parser.ParserFactory;
import com.yahoo.search.query.profile.types.FieldDescription;
import com.yahoo.search.query.profile.types.QueryProfileType;
import com.yahoo.search.yql.VespaGroupingStep;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* The parameters defining the where-clause and groping of a query
*
* @author henrhoi
*/
public class Select implements Cloneable {
/** The type representing the property arguments consumed by this */
private static final QueryProfileType argumentType;
private static final CompoundName argumentTypeName;
public static final String SELECT = "select";
public static final String WHERE = "where";
public static final String GROUPING = "grouping";
private static Model model;
private Query parent;
private String where = "";
private String grouping = "";
private List<GroupingRequest> groupingRequests = new ArrayList<>();
static {
argumentType = new QueryProfileType(SELECT);
argumentType.setStrict(true);
argumentType.setBuiltin(true);
argumentType.addField(new FieldDescription(WHERE, "string", "where"));
argumentType.addField(new FieldDescription(GROUPING, "string", "grouping"));
argumentType.freeze();
argumentTypeName=new CompoundName(argumentType.getId().getName());
}
public static QueryProfileType getArgumentType() { return argumentType; }
public Select(String where, String grouping){
this.where = where;
this.grouping = grouping;
}
public Select(Query query) {
setParent(query);
model = query.getModel();
}
/** Returns the query owning this, never null */
private Query getParent() { return parent; }
/** Assigns the query owning this */
public void setParent(Query parent) {
if (parent==null) throw new NullPointerException("A query models owner cannot be null");
this.parent = parent;
}
/** Set the where-clause for the query. Must be a JSON-string, with the format described in the Select Reference doc:
* @see <a href="https://docs.vespa.ai/documentation/reference/select-reference.html">https://docs.vespa.ai/documentation/reference/select-reference.html</a>
*/
public void setWhereString(String where) {
this.where = where;
model.setType(SELECT);
// Setting the queryTree to null
model.setQueryString(null);
}
/** Returns the where-clause in the query */
public String getWhereString(){ return where; }
/** Set the grouping-string for the query. Must be a JSON-string, with the format described in the Select Reference doc:
* @see <a href="https://docs.vespa.ai/documentation/reference/select-reference.html">https://docs.vespa.ai/documentation/reference/select-reference.html</a>
* */
public void setGroupingString(String grouping){
this.grouping = grouping;
SelectParser parser = (SelectParser) ParserFactory.newInstance(Query.Type.SELECT, new ParserEnvironment());
for (VespaGroupingStep step : parser.getGroupingSteps(grouping)) {
GroupingRequest.newInstance(parent)
.setRootOperation(step.getOperation())
.continuations().addAll(step.continuations());
}
}
/** Returns the grouping in the query */
public String getGroupingString(){
return grouping;
}
/** Returns the query's {@link GroupingRequest} objects */
public List<GroupingRequest> getGrouping(){ return groupingRequests; }
/** Set the query's list of {@link GroupingRequest} objects */
public void setGrouping(List<GroupingRequest> lst){
if (lst == null) {
lst = Collections.emptyList();
}
this.groupingRequests = lst;
}
@Override
public String toString() {
return "where: [" + where + "], grouping: [" + grouping+ "]";
}
}
|