From 6fecc1db84385721d0d45d8535b2175b799682fe Mon Sep 17 00:00:00 2001 From: Andrei Stefan Date: Mon, 2 Mar 2020 14:04:49 +0200 Subject: [PATCH] Issue a different error message in case an index doesn't have a mapping (#52967) (#53003) (cherry picked from commit a0bd83a0579cf196a1d727de2a46b3b101d5a73b) --- .../xpack/ql/index/IndexResolver.java | 2 +- .../xpack/sql/qa/ErrorsTestCase.java | 4 ++- .../xpack/sql/qa/cli/ErrorsTestCase.java | 21 ++++++++++++--- .../xpack/sql/qa/jdbc/ErrorsTestCase.java | 26 ++++++++++++++++--- .../xpack/sql/qa/rest/RestSqlTestCase.java | 22 ++++++++++++---- 5 files changed, 61 insertions(+), 14 deletions(-) diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java index b5f3e38f236..5d003dd80bf 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java @@ -289,7 +289,7 @@ public class IndexResolver { public static IndexResolution mergedMappings(DataTypeRegistry typeRegistry, String indexPattern, String[] indexNames, Map> fieldCaps) { - if (fieldCaps == null || fieldCaps.isEmpty()) { + if (indexNames.length == 0) { return IndexResolution.notFound(indexPattern); } diff --git a/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/ErrorsTestCase.java b/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/ErrorsTestCase.java index c5ae7f63ad0..68ae50a4299 100644 --- a/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/ErrorsTestCase.java +++ b/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/ErrorsTestCase.java @@ -13,7 +13,9 @@ package org.elasticsearch.xpack.sql.qa; public interface ErrorsTestCase { void testSelectInvalidSql() throws Exception; void testSelectFromMissingIndex() throws Exception; - void testSelectFromIndexWithoutTypes() throws Exception; + void testSelectColumnFromMissingIndex() throws Exception; + void testSelectFromEmptyIndex() throws Exception; + void testSelectColumnFromEmptyIndex() throws Exception; void testSelectMissingField() throws Exception; void testSelectMissingFunction() throws Exception; void testSelectProjectScoreInAggContext() throws Exception; diff --git a/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/cli/ErrorsTestCase.java b/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/cli/ErrorsTestCase.java index 2bef0cd60ae..6e2ebc87dbe 100644 --- a/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/cli/ErrorsTestCase.java +++ b/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/cli/ErrorsTestCase.java @@ -37,15 +37,30 @@ public abstract class ErrorsTestCase extends CliIntegrationTestCase implements o } @Override - public void testSelectFromIndexWithoutTypes() throws Exception { + public void testSelectColumnFromMissingIndex() throws Exception { + assertFoundOneProblem(command("SELECT abc FROM test")); + assertEquals("line 1:17: Unknown index [test]" + END, readLine()); + } + + @Override + public void testSelectFromEmptyIndex() throws Exception { // Create an index without any types Request request = new Request("PUT", "/test"); request.setJsonEntity("{}"); client().performRequest(request); assertFoundOneProblem(command("SELECT * FROM test")); - //assertEquals("line 1:15: [test] doesn't have any types so it is incompatible with sql" + END, readLine()); - assertEquals("line 1:15: Unknown index [test]" + END, readLine()); + assertEquals("line 1:8: Cannot determine columns for [*]" + END, readLine()); + } + + @Override + public void testSelectColumnFromEmptyIndex() throws Exception { + Request request = new Request("PUT", "/test"); + request.setJsonEntity("{}"); + client().performRequest(request); + + assertFoundOneProblem(command("SELECT abc FROM test")); + assertEquals("line 1:8: Unknown column [abc]" + END, readLine()); } @Override diff --git a/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ErrorsTestCase.java b/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ErrorsTestCase.java index 87de389e9be..9f7eca0c90a 100644 --- a/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ErrorsTestCase.java +++ b/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ErrorsTestCase.java @@ -33,7 +33,15 @@ public class ErrorsTestCase extends JdbcIntegrationTestCase implements org.elast } @Override - public void testSelectFromIndexWithoutTypes() throws Exception { + public void testSelectColumnFromMissingIndex() throws Exception { + try (Connection c = esJdbc()) { + SQLException e = expectThrows(SQLException.class, () -> c.prepareStatement("SELECT abc FROM test").executeQuery()); + assertEquals("Found 1 problem\nline 1:17: Unknown index [test]", e.getMessage()); + } + } + + @Override + public void testSelectFromEmptyIndex() throws Exception { // Create an index without any types Request request = new Request("PUT", "/test"); request.setJsonEntity("{}"); @@ -41,9 +49,19 @@ public class ErrorsTestCase extends JdbcIntegrationTestCase implements org.elast try (Connection c = esJdbc()) { SQLException e = expectThrows(SQLException.class, () -> c.prepareStatement("SELECT * FROM test").executeQuery()); - // see https://github.com/elastic/elasticsearch/issues/34719 - //assertEquals("Found 1 problem\nline 1:15: [test] doesn't have any types so it is incompatible with sql", e.getMessage()); - assertEquals("Found 1 problem\nline 1:15: Unknown index [test]", e.getMessage()); + assertEquals("Found 1 problem\nline 1:8: Cannot determine columns for [*]", e.getMessage()); + } + } + + @Override + public void testSelectColumnFromEmptyIndex() throws Exception { + Request request = new Request("PUT", "/test"); + request.setJsonEntity("{}"); + client().performRequest(request); + + try (Connection c = esJdbc()) { + SQLException e = expectThrows(SQLException.class, () -> c.prepareStatement("SELECT abc FROM test").executeQuery()); + assertEquals("Found 1 problem\nline 1:8: Unknown column [abc]", e.getMessage()); } } diff --git a/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlTestCase.java b/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlTestCase.java index f4cc628779c..2fd19d998d1 100644 --- a/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlTestCase.java +++ b/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlTestCase.java @@ -317,16 +317,28 @@ public abstract class RestSqlTestCase extends BaseRestSqlTestCase implements Err } @Override - public void testSelectFromIndexWithoutTypes() throws Exception { + public void testSelectColumnFromMissingIndex() throws Exception { + String mode = randomFrom("jdbc", "plain"); + expectBadRequest(() -> runSql(mode, "SELECT abc FROM missing"), containsString("1:17: Unknown index [missing]")); + } + + @Override + public void testSelectFromEmptyIndex() throws Exception { // Create an index without any types Request request = new Request("PUT", "/test"); request.setJsonEntity("{}"); client().performRequest(request); String mode = randomFrom("jdbc", "plain"); - expectBadRequest(() -> runSql(mode, "SELECT * FROM test"), - // see https://github.com/elastic/elasticsearch/issues/34719 - //containsString("1:15: [test] doesn't have any types so it is incompatible with sql")); - containsString("1:15: Unknown index [test]")); + expectBadRequest(() -> runSql(mode, "SELECT * FROM test"), containsString("1:8: Cannot determine columns for [*]")); + } + + @Override + public void testSelectColumnFromEmptyIndex() throws Exception { + Request request = new Request("PUT", "/test"); + request.setJsonEntity("{}"); + client().performRequest(request); + String mode = randomFrom("jdbc", "plain"); + expectBadRequest(() -> runSql(mode, "SELECT abc FROM test"), containsString("1:8: Unknown column [abc]")); } @Override