Skip to content

Commit 2bb34e4

Browse files
author
Lukas Wegmann
authored
SQL: Fix querying of indices without columns (#74312) (#74466)
Fixes #53001 Adds support for querying indices without columns. Previously, such queries failed. (cherry picked from commit 6f93303)
1 parent 9803dda commit 2bb34e4

File tree

15 files changed

+124
-58
lines changed

15 files changed

+124
-58
lines changed

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,6 @@ public void testSelectColumnFromMissingIndex() throws SQLException {
4040
}
4141
}
4242

43-
public void testSelectFromEmptyIndex() throws IOException, SQLException {
44-
// Create an index without any types
45-
Request request = new Request("PUT", "/test");
46-
request.setJsonEntity("{}");
47-
client().performRequest(request);
48-
49-
try (Connection c = esJdbc()) {
50-
SQLException e = expectThrows(SQLException.class, () -> c.prepareStatement("SELECT * FROM test").executeQuery());
51-
assertEquals("Found 1 problem\nline 1:8: Cannot determine columns for [*]", e.getMessage());
52-
}
53-
}
54-
5543
public void testSelectColumnFromEmptyIndex() throws IOException, SQLException {
5644
Request request = new Request("PUT", "/test");
5745
request.setJsonEntity("{}");

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ public interface ErrorsTestCase {
1818

1919
void testSelectColumnFromMissingIndex() throws Exception;
2020

21-
void testSelectFromEmptyIndex() throws Exception;
22-
2321
void testSelectColumnFromEmptyIndex() throws Exception;
2422

2523
void testSelectMissingField() throws Exception;

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,6 @@ public void testSelectColumnFromMissingIndex() throws Exception {
4343
assertEquals("line 1:17: Unknown index [test]" + END, readLine());
4444
}
4545

46-
@Override
47-
public void testSelectFromEmptyIndex() throws Exception {
48-
// Create an index without any types
49-
Request request = new Request("PUT", "/test");
50-
request.setJsonEntity("{}");
51-
client().performRequest(request);
52-
53-
assertFoundOneProblem(command("SELECT * FROM test"));
54-
assertEquals("line 1:8: Cannot determine columns for [*]" + END, readLine());
55-
}
56-
5746
@Override
5847
public void testSelectColumnFromEmptyIndex() throws Exception {
5948
Request request = new Request("PUT", "/test");

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ protected static void loadEmpDatasetIntoEs(RestClient client) throws Exception {
7272
// frozen index
7373
loadEmpDatasetIntoEs(client, "frozen_emp", "employees");
7474
freeze(client, "frozen_emp");
75+
loadNoColsDatasetIntoEs(client, "empty_mapping");
76+
}
77+
78+
private static void loadNoColsDatasetIntoEs(RestClient client, String index) throws Exception {
79+
createEmptyIndex(client, index);
80+
Request request = new Request("POST", "/" + index + "/_bulk");
81+
request.addParameter("refresh", "true");
82+
request.setJsonEntity("{\"index\":{}\n{}\n" + "{\"index\":{}\n{}\n");
83+
client.performRequest(request);
7584
}
7685

7786
public static void loadDocsDatasetIntoEs(RestClient client) throws Exception {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ public abstract class SqlSpecTestCase extends SpecBaseIntegrationTestCase {
3232
private String query;
3333

3434
@ClassRule
35-
public static LocalH2 H2 = new LocalH2(c -> c.createStatement().execute("RUNSCRIPT FROM 'classpath:/setup_test_emp.sql'"));
35+
public static LocalH2 H2 = new LocalH2((c) -> {
36+
c.createStatement().execute("RUNSCRIPT FROM 'classpath:/setup_test_emp.sql'");
37+
c.createStatement().execute("RUNSCRIPT FROM 'classpath:/setup_empty_mapping.sql'");
38+
});
3639

3740
@ParametersFactory(argumentFormatting = PARAM_FORMATTING)
3841
public static List<Object[]> readScriptSpec() throws Exception {

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -344,16 +344,6 @@ public void testSelectColumnFromMissingIndex() throws Exception {
344344
expectBadRequest(() -> runSql(mode, "SELECT abc FROM missing"), containsString("1:17: Unknown index [missing]"));
345345
}
346346

347-
@Override
348-
public void testSelectFromEmptyIndex() throws Exception {
349-
// Create an index without any types
350-
Request request = new Request("PUT", "/test");
351-
request.setJsonEntity("{}");
352-
client().performRequest(request);
353-
String mode = randomFrom("jdbc", "plain");
354-
expectBadRequest(() -> runSql(mode, "SELECT * FROM test"), containsString("1:8: Cannot determine columns for [*]"));
355-
}
356-
357347
@Override
358348
public void testSelectColumnFromEmptyIndex() throws Exception {
359349
Request request = new Request("PUT", "/test");

x-pack/plugin/sql/qa/server/src/main/resources/command.csv-spec

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,14 @@ TODAY |SCALAR
223223
showTables
224224
SHOW TABLES;
225225

226-
name | type | kind
226+
name | type | kind
227+
empty_mapping |TABLE |INDEX
227228
logs |TABLE |INDEX
228229
logs_nanos |TABLE |INDEX
229230
test_alias |VIEW |ALIAS
230-
test_alias_emp |VIEW |ALIAS
231-
test_emp |TABLE |INDEX
232-
test_emp_copy |TABLE |INDEX
231+
test_alias_emp |VIEW |ALIAS
232+
test_emp |TABLE |INDEX
233+
test_emp_copy |TABLE |INDEX
233234
;
234235

235236
showTablesSimpleLike
@@ -382,3 +383,19 @@ last_name |VARCHAR |text
382383
last_name.keyword |VARCHAR |keyword
383384
salary |INTEGER |integer
384385
;
386+
387+
388+
describeNoCols
389+
DESCRIBE "empty_mapping";
390+
391+
column:s | type:s | mapping:s
392+
----------------------+---------------+---------------
393+
;
394+
395+
396+
showColumnsInNoCols
397+
SHOW COLUMNS IN "empty_mapping";
398+
399+
column:s | type:s | mapping:s
400+
----------------------+---------------+---------------
401+
;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
// the following queries all return no rows in H2
3+
4+
selectConstAggregationWithGroupBy
5+
SELECT 1 a, COUNT(*) b, MAX(1) c FROM empty_mapping GROUP BY a;
6+
7+
a:i | b:l | c:i
8+
---------------+---------------+---------------
9+
1 |2 |1
10+
;
11+
12+
subselectWithConst
13+
SELECT 1, * FROM (SELECT * FROM empty_mapping) s;
14+
15+
1:i
16+
---------------
17+
1
18+
1
19+
;
20+
21+
subselectWithInnerConst
22+
SELECT * FROM (SELECT 1, * FROM empty_mapping) s;
23+
24+
1:i
25+
---------------
26+
1
27+
1
28+
;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
selectStar
2+
SELECT * FROM empty_mapping;
3+
selectStarWithFilter
4+
SELECT * FROM empty_mapping WHERE 2 > 1;
5+
selectStarWithFilterAndLimit
6+
SELECT * FROM empty_mapping WHERE 1 > 2 LIMIT 10;
7+
8+
selectCount
9+
SELECT COUNT(*) FROM empty_mapping;
10+
// awaits fix: https://github.com/elastic/elasticsearch/issues/74311
11+
// selectCountWithWhere
12+
// SELECT COUNT(*) FROM empty_mapping WHERE 1 + 1 = 3;
13+
14+
selectConst
15+
SELECT 1, 2, 3 FROM empty_mapping;
16+
selectConstAggregation
17+
SELECT MAX(1), SUM(2) FROM empty_mapping;
18+
19+
// fails in H2 with a syntax error but cannot be tested in CSV spec because datasets without columns cannot be parsed
20+
// awaits fix: https://github.com/elastic/elasticsearch/issues/39895 (latest H2 version can run the query)
21+
// subselect
22+
// SELECT * FROM (SELECT * FROM empty_mapping) s;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DROP TABLE IF EXISTS "empty_mapping";
2+
CREATE TABLE "empty_mapping" ();
3+
4+
INSERT INTO "empty_mapping" DEFAULT VALUES;
5+
INSERT INTO "empty_mapping" DEFAULT VALUES;

0 commit comments

Comments
 (0)