diff --git a/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/rest/RestSqlTestCase.java b/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/rest/RestSqlTestCase.java index 3f6bfc03c6e..f95ca22da4e 100644 --- a/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/rest/RestSqlTestCase.java +++ b/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/rest/RestSqlTestCase.java @@ -62,15 +62,13 @@ public abstract class RestSqlTestCase extends ESRestTestCase { public void testNextPage() throws IOException { StringBuilder bulk = new StringBuilder(); for (int i = 0; i < 20; i++) { - // NOCOMMIT we need number2 because we can't process the same column twice in two ways bulk.append("{\"index\":{\"_id\":\"" + i + "\"}}\n"); - bulk.append("{\"text\":\"text" + i + "\", \"number\":" + i + ", \"number2\": " + i + "}\n"); + bulk.append("{\"text\":\"text" + i + "\", \"number\":" + i + "}\n"); } client().performRequest("POST", "/test/test/_bulk", singletonMap("refresh", "true"), new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON)); - // NOCOMMIT we need tests for inner hits extractor and const extractor - String request = "{\"query\":\"SELECT text, number, SIN(number2) FROM test ORDER BY number\", \"fetch_size\":2}"; + String request = "{\"query\":\"SELECT text, number, SIN(number) AS s FROM test ORDER BY number\", \"fetch_size\":2}"; String cursor = null; for (int i = 0; i < 20; i += 2) { @@ -86,7 +84,7 @@ public abstract class RestSqlTestCase extends ESRestTestCase { expected.put("columns", Arrays.asList( columnInfo("text", "text"), columnInfo("number", "long"), - columnInfo("SIN(number2)", "double"))); + columnInfo("s", "double"))); } expected.put("rows", Arrays.asList( Arrays.asList("text" + i, i, Math.sin(i)), @@ -122,7 +120,7 @@ public abstract class RestSqlTestCase extends ESRestTestCase { new StringEntity("{\"query\":\"SELECT DAY_OF_YEAR(test), COUNT(*) FROM test\"}", ContentType.APPLICATION_JSON))); } - public void testMissingIndex() throws IOException { + public void missingIndex() throws IOException { expectBadRequest(() -> runSql("SELECT foo FROM missing"), containsString("1:17: index [missing] does not exist")); } @@ -156,11 +154,19 @@ public abstract class RestSqlTestCase extends ESRestTestCase { } private Map runSql(String sql) throws IOException { - return runSql(new StringEntity("{\"query\":\"" + sql + "\"}", ContentType.APPLICATION_JSON)); + return runSql(sql, ""); + } + + private Map runSql(String sql, String suffix) throws IOException { + return runSql(suffix, new StringEntity("{\"query\":\"" + sql + "\"}", ContentType.APPLICATION_JSON)); } private Map runSql(HttpEntity sql) throws IOException { - Response response = client().performRequest("POST", "/_sql", singletonMap("error_trace", "true"), sql); + return runSql("", sql); + } + + private Map runSql(String suffix, HttpEntity sql) throws IOException { + Response response = client().performRequest("POST", "/_sql" + suffix, singletonMap("error_trace", "true"), sql); try (InputStream content = response.getEntity().getContent()) { return XContentHelper.convertToMap(JsonXContent.jsonXContent, content, false); } @@ -173,4 +179,22 @@ public abstract class RestSqlTestCase extends ESRestTestCase { fail("Response does not match:\n" + message.toString()); } } + + public void testBasicTranslateQuery() throws IOException { + StringBuilder bulk = new StringBuilder(); + bulk.append("{\"index\":{\"_id\":\"1\"}}\n"); + bulk.append("{\"test\":\"test\"}\n"); + bulk.append("{\"index\":{\"_id\":\"2\"}}\n"); + bulk.append("{\"test\":\"test\"}\n"); + client().performRequest("POST", "/test_translate/test/_bulk", singletonMap("refresh", "true"), + new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON)); + + Map response = runSql("SELECT * FROM test_translate", "/translate/"); + assertEquals(response.get("size"), 1000); + @SuppressWarnings("unchecked") + Map source = (Map) response.get("_source"); + assertNotNull(source); + assertEquals(emptyList(), source.get("excludes")); + assertEquals(singletonList("test"), source.get("includes")); + } } 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 71159db67db..d40040bdc5d 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 @@ -31,6 +31,7 @@ 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; @@ -72,6 +73,7 @@ public class SqlPlugin implements ActionPlugin { IndexNameExpressionResolver indexNameExpressionResolver, Supplier nodesInCluster) { return Arrays.asList(new RestSqlAction(settings, restController), + new RestSqlTranslateAction(settings, restController), new RestSqlCliAction(settings, restController), new RestSqlJdbcAction(settings, restController, sqlLicenseChecker)); }