Skip to content

Commit b665984

Browse files
authored
SQL: handle wildcard expansion on incorrect fields (#35134)
1 parent 4f78e2b commit b665984

File tree

6 files changed

+51
-6
lines changed

6 files changed

+51
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public abstract class ErrorsTestCase extends CliIntegrationTestCase implements o
2727
@Override
2828
public void testSelectInvalidSql() throws Exception {
2929
assertFoundOneProblem(command("SELECT * FRO"));
30-
assertEquals("line 1:8: Cannot determine columns for *" + END, readLine());
30+
assertEquals("line 1:8: Cannot determine columns for [*]" + END, readLine());
3131
}
3232

3333
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ErrorsTestCase extends JdbcIntegrationTestCase implements org.elast
2020
public void testSelectInvalidSql() throws Exception {
2121
try (Connection c = esJdbc()) {
2222
SQLException e = expectThrows(SQLException.class, () -> c.prepareStatement("SELECT * FRO").executeQuery());
23-
assertEquals("Found 1 problem(s)\nline 1:8: Cannot determine columns for *", e.getMessage());
23+
assertEquals("Found 1 problem(s)\nline 1:8: Cannot determine columns for [*]", e.getMessage());
2424
}
2525
}
2626

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public void testSelectWhereExistsFails() throws Exception {
202202
@Override
203203
public void testSelectInvalidSql() {
204204
String mode = randomFrom("jdbc", "plain");
205-
expectBadRequest(() -> runSql(mode, "SELECT * FRO"), containsString("1:8: Cannot determine columns for *"));
205+
expectBadRequest(() -> runSql(mode, "SELECT * FRO"), containsString("1:8: Cannot determine columns for [*]"));
206206
}
207207

208208
@Override

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Analyzer.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,13 @@ private List<NamedExpression> expandProjections(List<? extends NamedExpression>
380380
List<Attribute> output = child.output();
381381
for (NamedExpression ne : projections) {
382382
if (ne instanceof UnresolvedStar) {
383-
result.addAll(expandStar((UnresolvedStar) ne, output));
383+
List<NamedExpression> expanded = expandStar((UnresolvedStar) ne, output);
384+
// the field exists, but cannot be expanded (no sub-fields)
385+
if (expanded.isEmpty()) {
386+
result.add(ne);
387+
} else {
388+
result.addAll(expanded);
389+
}
384390
} else if (ne instanceof UnresolvedAlias) {
385391
UnresolvedAlias ua = (UnresolvedAlias) ne;
386392
if (ua.child() instanceof UnresolvedStar) {
@@ -403,6 +409,13 @@ private List<NamedExpression> expandStar(UnresolvedStar us, List<Attribute> outp
403409
// since this is an unresolved start we don't know whether it's a path or an actual qualifier
404410
Attribute q = resolveAgainstList(us.qualifier(), output);
405411

412+
// the wildcard couldn't be expanded because the field doesn't exist at all
413+
// so, add to the list of expanded attributes its qualifier (the field without the wildcard)
414+
// the qualifier will be unresolved and later used in the error message presented to the user
415+
if (q == null) {
416+
expanded.add(us.qualifier());
417+
return expanded;
418+
}
406419
// now use the resolved 'qualifier' to match
407420
for (Attribute attr : output) {
408421
// filter the attributes that match based on their path

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/UnresolvedStar.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ public boolean equals(Object obj) {
6666
}
6767

6868
private String message() {
69-
return (qualifier() != null ? qualifier() + "." : "") + "*";
69+
return (qualifier() != null ? qualifier().qualifiedName() + "." : "") + "*";
7070
}
7171

7272
@Override
7373
public String unresolvedMessage() {
74-
return "Cannot determine columns for " + message();
74+
return "Cannot determine columns for [" + message() + "]";
7575
}
7676

7777
@Override

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/VerifierErrorMessagesTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,38 @@ public void testMissingIndex() {
4949
public void testMissingColumn() {
5050
assertEquals("1:8: Unknown column [xxx]", verify("SELECT xxx FROM test"));
5151
}
52+
53+
public void testMissingColumnWithWildcard() {
54+
assertEquals("1:8: Unknown column [xxx]", verify("SELECT xxx.* FROM test"));
55+
}
56+
57+
public void testMisspelledColumnWithWildcard() {
58+
assertEquals("1:8: Unknown column [tex], did you mean [text]?", verify("SELECT tex.* FROM test"));
59+
}
60+
61+
public void testColumnWithNoSubFields() {
62+
assertEquals("1:8: Cannot determine columns for [text.*]", verify("SELECT text.* FROM test"));
63+
}
64+
65+
public void testMultipleColumnsWithWildcard1() {
66+
assertEquals("1:14: Unknown column [a]\n" +
67+
"line 1:17: Unknown column [b]\n" +
68+
"line 1:22: Unknown column [c]\n" +
69+
"line 1:25: Unknown column [tex], did you mean [text]?", verify("SELECT bool, a, b.*, c, tex.* FROM test"));
70+
}
71+
72+
public void testMultipleColumnsWithWildcard2() {
73+
assertEquals("1:8: Unknown column [tex], did you mean [text]?\n" +
74+
"line 1:21: Unknown column [a]\n" +
75+
"line 1:24: Unknown column [dat], did you mean [date]?\n" +
76+
"line 1:31: Unknown column [c]", verify("SELECT tex.*, bool, a, dat.*, c FROM test"));
77+
}
78+
79+
public void testMultipleColumnsWithWildcard3() {
80+
assertEquals("1:8: Unknown column [ate], did you mean [date]?\n" +
81+
"line 1:21: Unknown column [keyw], did you mean [keyword]?\n" +
82+
"line 1:29: Unknown column [da], did you mean [date]?" , verify("SELECT ate.*, bool, keyw.*, da FROM test"));
83+
}
5284

5385
public void testMisspelledColumn() {
5486
assertEquals("1:8: Unknown column [txt], did you mean [text]?", verify("SELECT txt FROM test"));

0 commit comments

Comments
 (0)