Skip to content

Commit a0bd83a

Browse files
authored
Issue a different error message in case an index doesn't have a mapping (#52967)
1 parent 30bba95 commit a0bd83a

File tree

5 files changed

+61
-14
lines changed

5 files changed

+61
-14
lines changed

x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public void resolveAsMergedMapping(String indexWildcard, String javaRegex, boole
289289
public static IndexResolution mergedMappings(DataTypeRegistry typeRegistry, String indexPattern, String[] indexNames,
290290
Map<String, Map<String, FieldCapabilities>> fieldCaps) {
291291

292-
if (fieldCaps == null || fieldCaps.isEmpty()) {
292+
if (indexNames.length == 0) {
293293
return IndexResolution.notFound(indexPattern);
294294
}
295295

x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/ErrorsTestCase.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
public interface ErrorsTestCase {
1414
void testSelectInvalidSql() throws Exception;
1515
void testSelectFromMissingIndex() throws Exception;
16-
void testSelectFromIndexWithoutTypes() throws Exception;
16+
void testSelectColumnFromMissingIndex() throws Exception;
17+
void testSelectFromEmptyIndex() throws Exception;
18+
void testSelectColumnFromEmptyIndex() throws Exception;
1719
void testSelectMissingField() throws Exception;
1820
void testSelectMissingFunction() throws Exception;
1921
void testSelectProjectScoreInAggContext() throws Exception;

x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/cli/ErrorsTestCase.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,30 @@ public void testSelectFromMissingIndex() throws IOException {
3737
}
3838

3939
@Override
40-
public void testSelectFromIndexWithoutTypes() throws Exception {
40+
public void testSelectColumnFromMissingIndex() throws Exception {
41+
assertFoundOneProblem(command("SELECT abc FROM test"));
42+
assertEquals("line 1:17: Unknown index [test]" + END, readLine());
43+
}
44+
45+
@Override
46+
public void testSelectFromEmptyIndex() throws Exception {
4147
// Create an index without any types
4248
Request request = new Request("PUT", "/test");
4349
request.setJsonEntity("{}");
4450
client().performRequest(request);
4551

4652
assertFoundOneProblem(command("SELECT * FROM test"));
47-
//assertEquals("line 1:15: [test] doesn't have any types so it is incompatible with sql" + END, readLine());
48-
assertEquals("line 1:15: Unknown index [test]" + END, readLine());
53+
assertEquals("line 1:8: Cannot determine columns for [*]" + END, readLine());
54+
}
55+
56+
@Override
57+
public void testSelectColumnFromEmptyIndex() throws Exception {
58+
Request request = new Request("PUT", "/test");
59+
request.setJsonEntity("{}");
60+
client().performRequest(request);
61+
62+
assertFoundOneProblem(command("SELECT abc FROM test"));
63+
assertEquals("line 1:8: Unknown column [abc]" + END, readLine());
4964
}
5065

5166
@Override

x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ErrorsTestCase.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,35 @@ public void testSelectFromMissingIndex() throws SQLException {
3333
}
3434

3535
@Override
36-
public void testSelectFromIndexWithoutTypes() throws Exception {
36+
public void testSelectColumnFromMissingIndex() throws Exception {
37+
try (Connection c = esJdbc()) {
38+
SQLException e = expectThrows(SQLException.class, () -> c.prepareStatement("SELECT abc FROM test").executeQuery());
39+
assertEquals("Found 1 problem\nline 1:17: Unknown index [test]", e.getMessage());
40+
}
41+
}
42+
43+
@Override
44+
public void testSelectFromEmptyIndex() throws Exception {
3745
// Create an index without any types
3846
Request request = new Request("PUT", "/test");
3947
request.setJsonEntity("{}");
4048
client().performRequest(request);
4149

4250
try (Connection c = esJdbc()) {
4351
SQLException e = expectThrows(SQLException.class, () -> c.prepareStatement("SELECT * FROM test").executeQuery());
44-
// see https://github.com/elastic/elasticsearch/issues/34719
45-
//assertEquals("Found 1 problem\nline 1:15: [test] doesn't have any types so it is incompatible with sql", e.getMessage());
46-
assertEquals("Found 1 problem\nline 1:15: Unknown index [test]", e.getMessage());
52+
assertEquals("Found 1 problem\nline 1:8: Cannot determine columns for [*]", e.getMessage());
53+
}
54+
}
55+
56+
@Override
57+
public void testSelectColumnFromEmptyIndex() throws Exception {
58+
Request request = new Request("PUT", "/test");
59+
request.setJsonEntity("{}");
60+
client().performRequest(request);
61+
62+
try (Connection c = esJdbc()) {
63+
SQLException e = expectThrows(SQLException.class, () -> c.prepareStatement("SELECT abc FROM test").executeQuery());
64+
assertEquals("Found 1 problem\nline 1:8: Unknown column [abc]", e.getMessage());
4765
}
4866
}
4967

x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlTestCase.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,16 +317,28 @@ public void testSelectFromMissingIndex() {
317317
}
318318

319319
@Override
320-
public void testSelectFromIndexWithoutTypes() throws Exception {
320+
public void testSelectColumnFromMissingIndex() throws Exception {
321+
String mode = randomFrom("jdbc", "plain");
322+
expectBadRequest(() -> runSql(mode, "SELECT abc FROM missing"), containsString("1:17: Unknown index [missing]"));
323+
}
324+
325+
@Override
326+
public void testSelectFromEmptyIndex() throws Exception {
321327
// Create an index without any types
322328
Request request = new Request("PUT", "/test");
323329
request.setJsonEntity("{}");
324330
client().performRequest(request);
325331
String mode = randomFrom("jdbc", "plain");
326-
expectBadRequest(() -> runSql(mode, "SELECT * FROM test"),
327-
// see https://github.com/elastic/elasticsearch/issues/34719
328-
//containsString("1:15: [test] doesn't have any types so it is incompatible with sql"));
329-
containsString("1:15: Unknown index [test]"));
332+
expectBadRequest(() -> runSql(mode, "SELECT * FROM test"), containsString("1:8: Cannot determine columns for [*]"));
333+
}
334+
335+
@Override
336+
public void testSelectColumnFromEmptyIndex() throws Exception {
337+
Request request = new Request("PUT", "/test");
338+
request.setJsonEntity("{}");
339+
client().performRequest(request);
340+
String mode = randomFrom("jdbc", "plain");
341+
expectBadRequest(() -> runSql(mode, "SELECT abc FROM test"), containsString("1:8: Unknown column [abc]"));
330342
}
331343

332344
@Override

0 commit comments

Comments
 (0)