summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/query/Select.java
blob: 3ffc6bddb24cf817d0daf7fa743533c42dc41e11 (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
// 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+ "]";
    }

}