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 b5f3e38f23688..5d003dd80bf08 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 void resolveAsMergedMapping(String indexWildcard, String javaRegex, boole 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 c5ae7f63ad06d..68ae50a429927 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 @@ 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 2bef0cd60ae16..6e2ebc87dbe94 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 void testSelectFromMissingIndex() throws IOException { } @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 87de389e9be01..9f7eca0c90aeb 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 void testSelectFromMissingIndex() throws SQLException { } @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 void testSelectFromIndexWithoutTypes() throws Exception { 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 f4cc628779c41..2fd19d998d1bf 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 void testSelectFromMissingIndex() { } @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