|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.search.externalengine; |
| 10 | + |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.List; |
| 14 | +import org.apache.lucene.search.TotalHits; |
| 15 | +import org.opensearch.action.search.SearchRequest; |
| 16 | +import org.opensearch.action.search.SearchResponse; |
| 17 | +import org.opensearch.action.search.ShardSearchFailure; |
| 18 | +import org.opensearch.core.ParseField; |
| 19 | +import org.opensearch.core.action.ActionListener; |
| 20 | +import org.opensearch.core.common.ParsingException; |
| 21 | +import org.opensearch.core.common.io.stream.StreamInput; |
| 22 | +import org.opensearch.core.common.io.stream.StreamOutput; |
| 23 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 24 | +import org.opensearch.core.xcontent.XContentParser; |
| 25 | +import org.opensearch.search.SearchExtBuilder; |
| 26 | +import org.opensearch.search.SearchHit; |
| 27 | +import org.opensearch.search.SearchHits; |
| 28 | +import org.opensearch.search.internal.InternalSearchResponse; |
| 29 | + |
| 30 | +/** |
| 31 | + * SQLQueryEngine. |
| 32 | + */ |
| 33 | +public class SQLQueryEngine extends QueryEngine { |
| 34 | + |
| 35 | + public static final String NAME = "sql"; |
| 36 | + |
| 37 | + public SQLQueryEngine() { |
| 38 | + |
| 39 | + } |
| 40 | + public SQLQueryEngine(StreamInput in) { |
| 41 | + } |
| 42 | + @Override |
| 43 | + public String getWriteableName() { |
| 44 | + return "sql"; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void writeTo(StreamOutput out) throws IOException { |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + @Override |
| 53 | + public void executeQuery(SearchRequest searchRequest, |
| 54 | + ActionListener<SearchResponse> actionListener) { |
| 55 | + // Creating a minimal response is OK, because SearchResponse self |
| 56 | + // is tested elsewhere. |
| 57 | + SearchHit[] hits = new SearchHit[] { }; |
| 58 | + SearchResponse response = new SearchResponse( |
| 59 | + new InternalSearchResponse( |
| 60 | + new SearchHits(hits, new TotalHits(0, TotalHits.Relation.EQUAL_TO), 0), |
| 61 | + null, |
| 62 | + null, |
| 63 | + null, |
| 64 | + false, |
| 65 | + null, |
| 66 | + 1, |
| 67 | + List.of(new SQLResponseExternalBuilder("13")) |
| 68 | + ), |
| 69 | + null, |
| 70 | + 0, |
| 71 | + 0, |
| 72 | + 0, |
| 73 | + 0, |
| 74 | + ShardSearchFailure.EMPTY_ARRAY, |
| 75 | + SearchResponse.Clusters.EMPTY, |
| 76 | + null |
| 77 | + ); |
| 78 | + actionListener.onResponse(response); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + public static QueryEngine fromXContent(XContentParser parser) throws IOException { |
| 87 | + XContentParser.Token token; |
| 88 | + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { |
| 89 | + token = parser.nextToken(); |
| 90 | + } |
| 91 | + return new SQLQueryEngine(); |
| 92 | + } |
| 93 | + static class SQLResponseExternalBuilder extends SearchExtBuilder { |
| 94 | + |
| 95 | + static ParseField DUMMY_FIELD = new ParseField("dummy"); |
| 96 | + |
| 97 | + protected final String id; |
| 98 | + |
| 99 | + |
| 100 | + public SQLResponseExternalBuilder(String id) { |
| 101 | + this.id = id; |
| 102 | + } |
| 103 | + |
| 104 | + public SQLResponseExternalBuilder(StreamInput in) throws IOException { |
| 105 | + this.id = in.readString(); |
| 106 | + } |
| 107 | + |
| 108 | + public String getId() { |
| 109 | + return this.id; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String getWriteableName() { |
| 114 | + return DUMMY_FIELD.getPreferredName(); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void writeTo(StreamOutput out) throws IOException { |
| 119 | + out.writeString(this.id); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 124 | + return builder.field("dummy", id); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public int hashCode() { |
| 129 | + return 0; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public boolean equals(Object obj) { |
| 134 | + if (obj == null) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | + if (!(obj instanceof SQLResponseExternalBuilder)) { |
| 139 | + return false; |
| 140 | + } |
| 141 | + |
| 142 | + return this.id.equals(((SQLResponseExternalBuilder) obj).getId()); |
| 143 | + } |
| 144 | + |
| 145 | + public static SQLResponseExternalBuilder parse(XContentParser parser) throws IOException { |
| 146 | + String id; |
| 147 | + XContentParser.Token token = parser.currentToken(); |
| 148 | + if (token == XContentParser.Token.VALUE_STRING) { |
| 149 | + id = parser.text(); |
| 150 | + } else { |
| 151 | + throw new ParsingException(parser.getTokenLocation(), "Expected a VALUE_STRING but got " + token); |
| 152 | + } |
| 153 | + if (id == null) { |
| 154 | + throw new ParsingException(parser.getTokenLocation(), "no id specified for " + DUMMY_FIELD.getPreferredName()); |
| 155 | + } |
| 156 | + return new SQLResponseExternalBuilder(id); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments