diff options
author | Henrik Høiness <31851923+henrhoi@users.noreply.github.com> | 2018-08-07 13:00:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-07 13:00:51 +0200 |
commit | d0cf8f54872dd5483e1d319c9fdde3987bdcf9b1 (patch) | |
tree | bf5a51df21b7c6c1a3f53a59a5abe67e6bb90c97 /container-search/src/main/java/com/yahoo/search/query/Select.java | |
parent | b224eec759b379119a56f38bbf4f23227793f6d6 (diff) | |
parent | c332574e0b74f208da85c14dc41bdaa8f915c6a6 (diff) |
Merge pull request #6496 from vespa-engine/henrhoi/api-for-new-parameter-with-yql-and-grouping
Henrhoi/api-for-new-parameter-with-yql-and-grouping
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 | 110 |
1 files changed, 110 insertions, 0 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 new file mode 100644 index 00000000000..3ffc6bddb24 --- /dev/null +++ b/container-search/src/main/java/com/yahoo/search/query/Select.java @@ -0,0 +1,110 @@ +// 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; + + + +/** + * 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 = ""; + + 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 - https://docs.vespa.ai/documentation/reference/select-reference.html. */ + public void setWhere(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 this.where; + } + + + /** Set the grouping-string for the query. Must be a JSON-string, with the format described in the Select Reference doc - https://docs.vespa.ai/documentation/reference/select-reference.html. */ + public void setGrouping(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 this.grouping; + } + + + @Override + public String toString() { + return "where: [" + where + "], grouping: [" + grouping+ "]"; + } + +} |