From 25a0ec42f67c3491ee52fdbdd2660b9bf5f19b0f Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Fri, 3 Nov 2017 23:25:52 +0000 Subject: [PATCH] SQL: Reorganize the translate action (elastic/x-pack-elasticsearch#2841) Organizes the SQL translate action to match the way that x-pack has been organizing new actions for a while. All of the pieces are put into the same class file. Original commit: elastic/x-pack-elasticsearch@def911c0abbf64d595960990f1a8f07bc9ce66fc --- .../security/authz/AuthorizationService.java | 2 +- .../elasticsearch/xpack/sql/SqlLicenseIT.java | 5 +- .../xpack/sql/SqlTranslateActionIT.java | 6 +- .../xpack/sql/plugin/SqlPlugin.java | 11 +- .../xpack/sql/plugin/SqlTranslateAction.java | 218 ++++++++++++++++++ .../plugin/sql/action/SqlTranslateAction.java | 29 --- .../sql/action/SqlTranslateRequest.java | 39 ---- .../action/SqlTranslateRequestBuilder.java | 39 ---- .../sql/action/SqlTranslateResponse.java | 66 ------ .../action/TransportSqlTranslateAction.java | 46 ---- .../sql/rest/RestSqlTranslateAction.java | 46 ---- .../action => }/SqlTranslateRequestTests.java | 28 +-- .../SqlTranslateResponseTests.java | 21 +- 13 files changed, 256 insertions(+), 300 deletions(-) create mode 100644 sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlTranslateAction.java delete mode 100644 sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateAction.java delete mode 100644 sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateRequest.java delete mode 100644 sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateRequestBuilder.java delete mode 100644 sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateResponse.java delete mode 100644 sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/TransportSqlTranslateAction.java delete mode 100644 sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/rest/RestSqlTranslateAction.java rename sql/server/src/test/java/org/elasticsearch/xpack/sql/plugin/{sql/action => }/SqlTranslateRequestTests.java (57%) rename sql/server/src/test/java/org/elasticsearch/xpack/sql/plugin/{sql/action => }/SqlTranslateResponseTests.java (58%) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/security/authz/AuthorizationService.java b/plugin/src/main/java/org/elasticsearch/xpack/security/authz/AuthorizationService.java index 3e3743b1ff4..5b27a14e80f 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/security/authz/AuthorizationService.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/security/authz/AuthorizationService.java @@ -67,7 +67,7 @@ import org.elasticsearch.xpack.security.user.User; import org.elasticsearch.xpack.security.user.XPackSecurityUser; import org.elasticsearch.xpack.security.user.XPackUser; import org.elasticsearch.xpack.sql.plugin.sql.action.SqlAction; -import org.elasticsearch.xpack.sql.plugin.sql.action.SqlTranslateAction; +import org.elasticsearch.xpack.sql.plugin.SqlTranslateAction; import java.util.Arrays; import java.util.Collections; diff --git a/plugin/src/test/java/org/elasticsearch/xpack/sql/SqlLicenseIT.java b/plugin/src/test/java/org/elasticsearch/xpack/sql/SqlLicenseIT.java index ca25fbc5d36..64d700aa350 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/sql/SqlLicenseIT.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/sql/SqlLicenseIT.java @@ -29,8 +29,7 @@ import org.elasticsearch.xpack.sql.jdbc.net.protocol.MetaTableResponse; import org.elasticsearch.xpack.sql.jdbc.net.protocol.Proto; import org.elasticsearch.xpack.sql.plugin.sql.action.SqlAction; import org.elasticsearch.xpack.sql.plugin.sql.action.SqlResponse; -import org.elasticsearch.xpack.sql.plugin.sql.action.SqlTranslateAction; -import org.elasticsearch.xpack.sql.plugin.sql.action.SqlTranslateResponse; +import org.elasticsearch.xpack.sql.plugin.SqlTranslateAction; import org.elasticsearch.xpack.sql.protocol.shared.Request; import org.elasticsearch.xpack.sql.protocol.shared.Response; import org.hamcrest.Matchers; @@ -149,7 +148,7 @@ public class SqlLicenseIT extends AbstractLicensesIntegrationTestCase { assertThat(e.getMessage(), equalTo("current license is non-compliant for [sql]")); enableSqlLicensing(); - SqlTranslateResponse response = client().prepareExecute(SqlTranslateAction.INSTANCE).query("SELECT * FROM test").get(); + SqlTranslateAction.Response response = client().prepareExecute(SqlTranslateAction.INSTANCE).query("SELECT * FROM test").get(); SearchSourceBuilder source = response.source(); assertThat(source.docValueFields(), Matchers.contains("count")); FetchSourceContext fetchSource = source.fetchSource(); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/sql/SqlTranslateActionIT.java b/plugin/src/test/java/org/elasticsearch/xpack/sql/SqlTranslateActionIT.java index 6f31b859eb7..9ff5588bceb 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/sql/SqlTranslateActionIT.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/sql/SqlTranslateActionIT.java @@ -10,8 +10,7 @@ import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.search.sort.SortBuilders; -import org.elasticsearch.xpack.sql.plugin.sql.action.SqlTranslateAction; -import org.elasticsearch.xpack.sql.plugin.sql.action.SqlTranslateResponse; +import org.elasticsearch.xpack.sql.plugin.SqlTranslateAction; import static java.util.Collections.singletonList; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; @@ -29,7 +28,7 @@ public class SqlTranslateActionIT extends AbstractSqlIntegTestCase { boolean columnOrder = randomBoolean(); String columns = columnOrder ? "data, count" : "count, data"; - SqlTranslateResponse response = client().prepareExecute(SqlTranslateAction.INSTANCE) + SqlTranslateAction.Response response = client().prepareExecute(SqlTranslateAction.INSTANCE) .query("SELECT " + columns + " FROM test ORDER BY count").get(); SearchSourceBuilder source = response.source(); FetchSourceContext fetch = source.fetchSource(); @@ -39,4 +38,3 @@ public class SqlTranslateActionIT extends AbstractSqlIntegTestCase { assertEquals(singletonList(SortBuilders.fieldSort("count")), source.sorts()); } } - diff --git a/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPlugin.java b/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPlugin.java index d40040bdc5d..ff79acc2180 100644 --- a/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPlugin.java +++ b/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPlugin.java @@ -27,11 +27,8 @@ import org.elasticsearch.xpack.sql.analysis.catalog.FilteredCatalog; import org.elasticsearch.xpack.sql.execution.PlanExecutor; import org.elasticsearch.xpack.sql.plugin.SqlGetIndicesAction.TransportAction.CatalogHolder; import org.elasticsearch.xpack.sql.plugin.sql.action.SqlAction; -import org.elasticsearch.xpack.sql.plugin.sql.action.SqlTranslateAction; import org.elasticsearch.xpack.sql.plugin.sql.action.TransportSqlAction; -import org.elasticsearch.xpack.sql.plugin.sql.action.TransportSqlTranslateAction; import org.elasticsearch.xpack.sql.plugin.sql.rest.RestSqlAction; -import org.elasticsearch.xpack.sql.plugin.sql.rest.RestSqlTranslateAction; import org.elasticsearch.xpack.sql.session.Cursor; import java.util.Arrays; @@ -68,12 +65,12 @@ public class SqlPlugin implements ActionPlugin { } @Override - public List getRestHandlers(Settings settings, RestController restController, - ClusterSettings clusterSettings, IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, + public List getRestHandlers(Settings settings, RestController restController, + ClusterSettings clusterSettings, IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, Supplier nodesInCluster) { return Arrays.asList(new RestSqlAction(settings, restController), - new RestSqlTranslateAction(settings, restController), + new SqlTranslateAction.RestAction(settings, restController), new RestSqlCliAction(settings, restController), new RestSqlJdbcAction(settings, restController, sqlLicenseChecker)); } @@ -82,6 +79,6 @@ public class SqlPlugin implements ActionPlugin { public List> getActions() { return Arrays.asList(new ActionHandler<>(SqlAction.INSTANCE, TransportSqlAction.class), new ActionHandler<>(SqlGetIndicesAction.INSTANCE, SqlGetIndicesAction.TransportAction.class), - new ActionHandler<>(SqlTranslateAction.INSTANCE, TransportSqlTranslateAction.class)); + new ActionHandler<>(SqlTranslateAction.INSTANCE, SqlTranslateAction.TransportAction.class)); } } diff --git a/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlTranslateAction.java b/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlTranslateAction.java new file mode 100644 index 00000000000..350e1201f5c --- /dev/null +++ b/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlTranslateAction.java @@ -0,0 +1,218 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.xpack.sql.plugin; + +import org.elasticsearch.action.Action; +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.ActionRequestBuilder; +import org.elasticsearch.action.ActionRequestValidationException; +import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.support.ActionFilters; +import org.elasticsearch.action.support.HandledTransportAction; +import org.elasticsearch.client.ElasticsearchClient; +import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.settings.Setting; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.xcontent.ObjectParser; +import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.rest.BaseRestHandler; +import org.elasticsearch.rest.RestController; +import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xpack.sql.execution.PlanExecutor; +import org.elasticsearch.xpack.sql.plugin.sql.action.AbstractSqlRequest; +import org.elasticsearch.xpack.sql.session.Configuration; +import org.joda.time.DateTimeZone; + +import static org.elasticsearch.action.ValidateActions.addValidationError; +import static org.elasticsearch.rest.RestRequest.Method.GET; +import static org.elasticsearch.rest.RestRequest.Method.POST; +import static org.elasticsearch.xpack.sql.plugin.sql.action.AbstractSqlRequest.DEFAULT_FETCH_SIZE; +import static org.elasticsearch.xpack.sql.plugin.sql.action.AbstractSqlRequest.DEFAULT_PAGE_TIMEOUT; +import static org.elasticsearch.xpack.sql.plugin.sql.action.AbstractSqlRequest.DEFAULT_REQUEST_TIMEOUT; +import static org.elasticsearch.xpack.sql.plugin.sql.action.AbstractSqlRequest.DEFAULT_TIME_ZONE; + +import java.io.IOException; +import java.util.Objects; + +public class SqlTranslateAction + extends Action { + + public static final SqlTranslateAction INSTANCE = new SqlTranslateAction(); + public static final String NAME = "indices:data/read/sql/translate"; + + private SqlTranslateAction() { + super(NAME); + } + + @Override + public RequestBuilder newRequestBuilder(ElasticsearchClient client) { + return new RequestBuilder(client, this); + } + + @Override + public Response newResponse() { + return new Response(); + } + + public static class Request extends AbstractSqlRequest { + public static final ObjectParser PARSER = objectParser(Request::new); + + public Request() {} + + public Request(String query, DateTimeZone timeZone, int fetchSize, TimeValue requestTimeout, TimeValue pageTimeout) { + super(query, timeZone, fetchSize, requestTimeout, pageTimeout); + } + + @Override + public ActionRequestValidationException validate() { + ActionRequestValidationException validationException = null; + if ((false == Strings.hasText(query()))) { + validationException = addValidationError("query is required", validationException); + } + return validationException; + } + + @Override + public String getDescription() { + return "SQL Translate [" + query() + "]"; + } + } + + public static class RequestBuilder extends ActionRequestBuilder { + public RequestBuilder(ElasticsearchClient client, SqlTranslateAction action) { + this(client, action, null, DEFAULT_TIME_ZONE, DEFAULT_FETCH_SIZE, DEFAULT_REQUEST_TIMEOUT, DEFAULT_PAGE_TIMEOUT); + } + + public RequestBuilder(ElasticsearchClient client, SqlTranslateAction action, String query, DateTimeZone timeZone, + int fetchSize, TimeValue requestTimeout, TimeValue pageTimeout) { + super(client, action, new Request(query, timeZone, fetchSize, requestTimeout, pageTimeout)); + } + + public RequestBuilder query(String query) { + request.query(query); + return this; + } + + public RequestBuilder timeZone(DateTimeZone timeZone) { + request.timeZone(timeZone); + return this; + } + } + + public static class Response extends ActionResponse implements ToXContentObject { + private SearchSourceBuilder source; + + public Response() { + } + + public Response(SearchSourceBuilder source) { + this.source = source; + } + + public SearchSourceBuilder source() { + return source; + } + + @Override + public void readFrom(StreamInput in) throws IOException { + source = new SearchSourceBuilder(in); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + source.writeTo(out); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj == null || getClass() != obj.getClass()) { + return false; + } + + Response other = (Response) obj; + return Objects.equals(source, other.source); + } + + @Override + public int hashCode() { + return Objects.hash(source); + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + return source.toXContent(builder, params); + } + } + + public static class TransportAction extends HandledTransportAction { + private final PlanExecutor planExecutor; + private final SqlLicenseChecker sqlLicenseChecker; + + @Inject + public TransportAction(Settings settings, ThreadPool threadPool, + TransportService transportService, ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + PlanExecutor planExecutor, + SqlLicenseChecker sqlLicenseChecker) { + super(settings, SqlTranslateAction.NAME, threadPool, transportService, actionFilters, + indexNameExpressionResolver, Request::new); + + this.planExecutor = planExecutor; + this.sqlLicenseChecker = sqlLicenseChecker; + } + + @Override + protected void doExecute(Request request, ActionListener listener) { + sqlLicenseChecker.checkIfSqlAllowed(); + String query = request.query(); + + Configuration cfg = new Configuration(request.timeZone(), request.fetchSize(), + request.requestTimeout(), request.pageTimeout()); + + listener.onResponse(new Response(planExecutor.searchSource(query, cfg))); + } + } + + public static class RestAction extends BaseRestHandler { + public RestAction(Settings settings, RestController controller) { + super(settings); + controller.registerHandler(GET, "/_sql/translate", this); + controller.registerHandler(POST, "/_sql/translate", this); + } + + @Override + protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { + Request sqlRequest; + try (XContentParser parser = request.contentOrSourceParamParser()) { + sqlRequest = Request.PARSER.apply(parser, null); + } + + return channel -> client.executeLocally(SqlTranslateAction.INSTANCE, + sqlRequest, new RestToXContentListener(channel)); + } + + @Override + public String getName() { + return "sql_translate_action"; + } + } +} diff --git a/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateAction.java b/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateAction.java deleted file mode 100644 index d38c0351209..00000000000 --- a/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateAction.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.sql.plugin.sql.action; - -import org.elasticsearch.action.Action; -import org.elasticsearch.client.ElasticsearchClient; - -public class SqlTranslateAction extends Action { - - public static final SqlTranslateAction INSTANCE = new SqlTranslateAction(); - public static final String NAME = "indices:data/read/sql/translate"; - - private SqlTranslateAction() { - super(NAME); - } - - @Override - public SqlTranslateRequestBuilder newRequestBuilder(ElasticsearchClient client) { - return new SqlTranslateRequestBuilder(client, this); - } - - @Override - public SqlTranslateResponse newResponse() { - return new SqlTranslateResponse(); - } -} diff --git a/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateRequest.java b/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateRequest.java deleted file mode 100644 index e7b4131cce1..00000000000 --- a/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateRequest.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.sql.plugin.sql.action; - -import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.common.Strings; -import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.xcontent.ObjectParser; -import org.joda.time.DateTimeZone; - -import static org.elasticsearch.action.ValidateActions.addValidationError; - -public class SqlTranslateRequest extends AbstractSqlRequest { - - public static final ObjectParser PARSER = objectParser(SqlTranslateRequest::new); - - public SqlTranslateRequest() {} - - public SqlTranslateRequest(String query, DateTimeZone timeZone, int fetchSize, TimeValue requestTimeout, TimeValue pageTimeout) { - super(query, timeZone, fetchSize, requestTimeout, pageTimeout); - } - - @Override - public ActionRequestValidationException validate() { - ActionRequestValidationException validationException = null; - if ((false == Strings.hasText(query()))) { - validationException = addValidationError("query is required", validationException); - } - return validationException; - } - - @Override - public String getDescription() { - return "SQL Translate [" + query() + "]"; - } -} \ No newline at end of file diff --git a/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateRequestBuilder.java b/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateRequestBuilder.java deleted file mode 100644 index e9774a0495d..00000000000 --- a/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateRequestBuilder.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.sql.plugin.sql.action; - -import org.elasticsearch.action.ActionRequestBuilder; -import org.elasticsearch.client.ElasticsearchClient; -import org.elasticsearch.common.unit.TimeValue; -import org.joda.time.DateTimeZone; - -import static org.elasticsearch.xpack.sql.plugin.sql.action.AbstractSqlRequest.DEFAULT_FETCH_SIZE; -import static org.elasticsearch.xpack.sql.plugin.sql.action.AbstractSqlRequest.DEFAULT_PAGE_TIMEOUT; -import static org.elasticsearch.xpack.sql.plugin.sql.action.AbstractSqlRequest.DEFAULT_REQUEST_TIMEOUT; -import static org.elasticsearch.xpack.sql.plugin.sql.action.AbstractSqlRequest.DEFAULT_TIME_ZONE; - -public class SqlTranslateRequestBuilder - extends ActionRequestBuilder { - - public SqlTranslateRequestBuilder(ElasticsearchClient client, SqlTranslateAction action) { - this(client, action, null, DEFAULT_TIME_ZONE, DEFAULT_FETCH_SIZE, DEFAULT_REQUEST_TIMEOUT, DEFAULT_PAGE_TIMEOUT); - } - - public SqlTranslateRequestBuilder(ElasticsearchClient client, SqlTranslateAction action, String query, DateTimeZone timeZone, - int fetchSize, TimeValue requestTimeout, TimeValue pageTimeout) { - super(client, action, new SqlTranslateRequest(query, timeZone, fetchSize, requestTimeout, pageTimeout)); - } - - public SqlTranslateRequestBuilder query(String query) { - request.query(query); - return this; - } - - public SqlTranslateRequestBuilder timeZone(DateTimeZone timeZone) { - request.timeZone(timeZone); - return this; - } -} diff --git a/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateResponse.java b/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateResponse.java deleted file mode 100644 index 78f872813a9..00000000000 --- a/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateResponse.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.sql.plugin.sql.action; - -import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.ToXContentObject; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.search.builder.SearchSourceBuilder; - -import java.io.IOException; -import java.util.Objects; - -public class SqlTranslateResponse extends ActionResponse implements ToXContentObject { - - private SearchSourceBuilder source; - - public SqlTranslateResponse() { - } - - public SqlTranslateResponse(SearchSourceBuilder source) { - this.source = source; - } - - public SearchSourceBuilder source() { - return source; - } - - @Override - public void readFrom(StreamInput in) throws IOException { - source = new SearchSourceBuilder(in); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - source.writeTo(out); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - - if (obj == null || getClass() != obj.getClass()) { - return false; - } - - SqlTranslateResponse other = (SqlTranslateResponse) obj; - return Objects.equals(source, other.source); - } - - @Override - public int hashCode() { - return Objects.hash(source); - } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return source.toXContent(builder, params); - } -} \ No newline at end of file diff --git a/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/TransportSqlTranslateAction.java b/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/TransportSqlTranslateAction.java deleted file mode 100644 index 5010ae9aebf..00000000000 --- a/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/action/TransportSqlTranslateAction.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.sql.plugin.sql.action; - -import org.elasticsearch.action.ActionListener; -import org.elasticsearch.action.support.ActionFilters; -import org.elasticsearch.action.support.HandledTransportAction; -import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; -import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.threadpool.ThreadPool; -import org.elasticsearch.transport.TransportService; -import org.elasticsearch.xpack.sql.execution.PlanExecutor; -import org.elasticsearch.xpack.sql.plugin.SqlLicenseChecker; -import org.elasticsearch.xpack.sql.session.Configuration; - -public class TransportSqlTranslateAction extends HandledTransportAction { - - private final PlanExecutor planExecutor; - private final SqlLicenseChecker sqlLicenseChecker; - - @Inject - public TransportSqlTranslateAction(Settings settings, ThreadPool threadPool, - TransportService transportService, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver, - PlanExecutor planExecutor, - SqlLicenseChecker sqlLicenseChecker) { - super(settings, SqlTranslateAction.NAME, threadPool, transportService, actionFilters, indexNameExpressionResolver, SqlTranslateRequest::new); - - this.planExecutor = planExecutor; - this.sqlLicenseChecker = sqlLicenseChecker; - } - - @Override - protected void doExecute(SqlTranslateRequest request, ActionListener listener) { - sqlLicenseChecker.checkIfSqlAllowed(); - String query = request.query(); - - Configuration cfg = new Configuration(request.timeZone(), request.fetchSize(), request.requestTimeout(), request.pageTimeout()); - - listener.onResponse(new SqlTranslateResponse(planExecutor.searchSource(query, cfg))); - } -} \ No newline at end of file diff --git a/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/rest/RestSqlTranslateAction.java b/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/rest/RestSqlTranslateAction.java deleted file mode 100644 index 99e2405defd..00000000000 --- a/sql/server/src/main/java/org/elasticsearch/xpack/sql/plugin/sql/rest/RestSqlTranslateAction.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.xpack.sql.plugin.sql.rest; - -import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.rest.BaseRestHandler; -import org.elasticsearch.rest.RestController; -import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.RestToXContentListener; -import org.elasticsearch.xpack.sql.plugin.sql.action.SqlTranslateAction; -import org.elasticsearch.xpack.sql.plugin.sql.action.SqlTranslateRequest; -import org.elasticsearch.xpack.sql.plugin.sql.action.SqlTranslateResponse; - -import java.io.IOException; - -import static org.elasticsearch.rest.RestRequest.Method.GET; -import static org.elasticsearch.rest.RestRequest.Method.POST; - -public class RestSqlTranslateAction extends BaseRestHandler { - - public RestSqlTranslateAction(Settings settings, RestController controller) { - super(settings); - controller.registerHandler(GET, "/_sql/translate", this); - controller.registerHandler(POST, "/_sql/translate", this); - } - - @Override - protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { - SqlTranslateRequest sqlRequest; - try (XContentParser parser = request.contentOrSourceParamParser()) { - sqlRequest = SqlTranslateRequest.PARSER.apply(parser, null); - } - - return channel -> client.executeLocally(SqlTranslateAction.INSTANCE, sqlRequest, new RestToXContentListener(channel)); - } - - @Override - public String getName() { - return "sql_translate_action"; - } -} \ No newline at end of file diff --git a/sql/server/src/test/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateRequestTests.java b/sql/server/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlTranslateRequestTests.java similarity index 57% rename from sql/server/src/test/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateRequestTests.java rename to sql/server/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlTranslateRequestTests.java index 0bdb4e33ca1..ce86b850a0a 100644 --- a/sql/server/src/test/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateRequestTests.java +++ b/sql/server/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlTranslateRequestTests.java @@ -3,19 +3,19 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -package org.elasticsearch.xpack.sql.plugin.sql.action; +package org.elasticsearch.xpack.sql.plugin; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.test.AbstractStreamableTestCase; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils.MutateFunction; -public class SqlTranslateRequestTests extends AbstractStreamableTestCase { +public class SqlTranslateRequestTests extends AbstractStreamableTestCase { @Override - protected SqlTranslateRequest createTestInstance() { - return new SqlTranslateRequest(randomAlphaOfLength(10), randomDateTimeZone(), between(1, Integer.MAX_VALUE), randomTV(), - randomTV()); + protected SqlTranslateAction.Request createTestInstance() { + return new SqlTranslateAction.Request(randomAlphaOfLength(10), randomDateTimeZone(), between(1, Integer.MAX_VALUE), + randomTV(), randomTV()); } private TimeValue randomTV() { @@ -23,23 +23,23 @@ public class SqlTranslateRequestTests extends AbstractStreamableTestCase getMutateFunction() { + protected MutateFunction getMutateFunction() { return randomFrom( - request -> (SqlTranslateRequest) getCopyFunction().copy(request) + request -> (SqlTranslateAction.Request) getCopyFunction().copy(request) .query(randomValueOtherThan(request.query(), () -> randomAlphaOfLength(5))), - request -> (SqlTranslateRequest) getCopyFunction().copy(request) + request -> (SqlTranslateAction.Request) getCopyFunction().copy(request) .timeZone(randomValueOtherThan(request.timeZone(), ESTestCase::randomDateTimeZone)), - request -> (SqlTranslateRequest) getCopyFunction().copy(request) + request -> (SqlTranslateAction.Request) getCopyFunction().copy(request) .fetchSize(randomValueOtherThan(request.fetchSize(), () -> between(1, Integer.MAX_VALUE))), - request -> (SqlTranslateRequest) getCopyFunction().copy(request) + request -> (SqlTranslateAction.Request) getCopyFunction().copy(request) .requestTimeout(randomValueOtherThan(request.requestTimeout(), () -> randomTV())), - request -> (SqlTranslateRequest) getCopyFunction().copy(request) + request -> (SqlTranslateAction.Request) getCopyFunction().copy(request) .pageTimeout(randomValueOtherThan(request.pageTimeout(), () -> randomTV()))); } -} \ No newline at end of file +} diff --git a/sql/server/src/test/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateResponseTests.java b/sql/server/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlTranslateResponseTests.java similarity index 58% rename from sql/server/src/test/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateResponseTests.java rename to sql/server/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlTranslateResponseTests.java index d2a9c2ea492..cedb5a97d71 100644 --- a/sql/server/src/test/java/org/elasticsearch/xpack/sql/plugin/sql/action/SqlTranslateResponseTests.java +++ b/sql/server/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlTranslateResponseTests.java @@ -3,15 +3,16 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -package org.elasticsearch.xpack.sql.plugin.sql.action; +package org.elasticsearch.xpack.sql.plugin; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.test.AbstractStreamableTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils.MutateFunction; -public class SqlTranslateResponseTests extends AbstractStreamableTestCase { +public class SqlTranslateResponseTests extends AbstractStreamableTestCase { @Override - protected SqlTranslateResponse createTestInstance() { + protected SqlTranslateAction.Response createTestInstance() { SearchSourceBuilder s = new SearchSourceBuilder(); if (randomBoolean()) { long docValues = iterations(5, 10); @@ -29,12 +30,20 @@ public class SqlTranslateResponseTests extends AbstractStreamableTestCase getMutateFunction() { + return response -> { + SqlTranslateAction.Response copy = getCopyFunction().copy(response); + copy.source().size(randomValueOtherThan(response.source().size(), () -> between(0, Integer.MAX_VALUE))); + return copy; + }; + } }