diff options
Diffstat (limited to 'container-search/src/main/java/com/yahoo/search/query/Select.java')
-rw-r--r-- | container-search/src/main/java/com/yahoo/search/query/Select.java | 95 |
1 files changed, 57 insertions, 38 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/query/Select.java b/container-search/src/main/java/com/yahoo/search/query/Select.java index ef6a7fe8272..bbc152c6391 100644 --- a/container-search/src/main/java/com/yahoo/search/query/Select.java +++ b/container-search/src/main/java/com/yahoo/search/query/Select.java @@ -12,7 +12,9 @@ import com.yahoo.search.yql.VespaGroupingStep; import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Objects; /** @@ -30,69 +32,75 @@ public class Select implements Cloneable { 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<>(); + private final Query parent; + private final List<GroupingRequest> groupingRequests; + + private String where; + private String grouping; 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.addField(new FieldDescription(WHERE, "string")); + argumentType.addField(new FieldDescription(GROUPING, "string")); argumentType.freeze(); - argumentTypeName=new CompoundName(argumentType.getId().getName()); + argumentTypeName = new CompoundName(argumentType.getId().getName()); } public static QueryProfileType getArgumentType() { return argumentType; } - public Select(String where, String grouping){ - this.where = where; - this.grouping = grouping; - } - + /** Creates an empty select statement */ public Select(Query query) { - setParent(query); - model = query.getModel(); + this("", "", query); } + public Select(String where, String grouping, Query query) { + this(where, grouping, query, Collections.emptyList()); + } - /** 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; + private Select(String where, String grouping, Query query, List<GroupingRequest> groupingRequests) { + this.where = Objects.requireNonNull(where, "A Select must have a where string (possibly the empty string)"); + this.grouping = Objects.requireNonNull(grouping, "A Select must have a select string (possibly the empty string)"); + this.parent = Objects.requireNonNull(query, "A Select must have a parent query"); + this.groupingRequests = deepCopy(groupingRequests, this); } + private static List<GroupingRequest> deepCopy(List<GroupingRequest> groupingRequests, Select parentOfCopy) { + List<GroupingRequest> copy = new ArrayList<>(groupingRequests.size()); + for (GroupingRequest request : groupingRequests) + copy.add(request.copy(parentOfCopy)); + return copy; + } - /** 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> + /** + * Sets the document selection criterion of the query. + * + * @param where the documents to select as a JSON string on the format specified in + * <a href="https://docs.vespa.ai/documentation/reference/select-reference.html">the select reference doc</a> */ public void setWhereString(String where) { this.where = where; - model.setType(SELECT); + parent.getModel().setType(SELECT); - // Setting the queryTree to null - model.setQueryString(null); + // This replaces the current query + parent.getModel().clearQueryTree(); } - - /** Returns the where-clause in the query */ + /** Returns the where clause string previously assigned, or an empty string if none */ 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){ + /** + * Sets the grouping operation of the query. + * + * @param grouping the grouping to perform as a JSON string on the format specified in + * <a href="https://docs.vespa.ai/documentation/reference/select-reference.html">the select reference doc</a> + */ + public void setGroupingString(String grouping) { + groupingRequests.clear(); 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()) @@ -106,9 +114,11 @@ public class Select implements Cloneable { return grouping; } - - /** Returns the query's {@link GroupingRequest} objects, as mutable list */ - public List<GroupingRequest> getGrouping(){ return groupingRequests; } + /** + * Returns the query's {@link GroupingRequest} as a mutable list. Changing this directly changes the grouping + * operations which will be performed by this query. + */ + public List<GroupingRequest> getGrouping() { return groupingRequests; } @Override @@ -116,4 +126,13 @@ public class Select implements Cloneable { return "where: [" + where + "], grouping: [" + grouping+ "]"; } + @Override + public Object clone() { + return new Select(where, grouping, parent, groupingRequests); + } + + public Select cloneFor(Query parent) { + return new Select(where, grouping, parent, groupingRequests); + } + } |