Skip to content

Commit 5373a77

Browse files
committed
QL: Extract common Failure class (#52281)
Shared across SQL and EQL (cherry picked from commit 1aeda20)
1 parent b0ad371 commit 5373a77

File tree

20 files changed

+52
-219
lines changed

20 files changed

+52
-219
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
package org.elasticsearch.xpack.eql.analysis;
88

9-
import org.elasticsearch.xpack.eql.common.Failure;
9+
import org.elasticsearch.xpack.ql.common.Failure;
1010
import org.elasticsearch.xpack.ql.expression.Attribute;
1111
import org.elasticsearch.xpack.ql.expression.NamedExpression;
1212
import org.elasticsearch.xpack.ql.expression.UnresolvedAttribute;

x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/VerificationException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import org.elasticsearch.rest.RestStatus;
99
import org.elasticsearch.xpack.eql.EqlClientException;
10-
import org.elasticsearch.xpack.eql.common.Failure;
10+
import org.elasticsearch.xpack.ql.common.Failure;
1111

1212
import java.util.Collection;
1313

x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/Verifier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
package org.elasticsearch.xpack.eql.analysis;
88

9-
import org.elasticsearch.xpack.eql.common.Failure;
109
import org.elasticsearch.xpack.ql.capabilities.Unresolvable;
10+
import org.elasticsearch.xpack.ql.common.Failure;
1111
import org.elasticsearch.xpack.ql.expression.Attribute;
1212
import org.elasticsearch.xpack.ql.expression.UnresolvedAttribute;
1313
import org.elasticsearch.xpack.ql.plan.logical.LogicalPlan;
@@ -23,7 +23,7 @@
2323
import java.util.Set;
2424

2525
import static java.util.stream.Collectors.toMap;
26-
import static org.elasticsearch.xpack.eql.common.Failure.fail;
26+
import static org.elasticsearch.xpack.ql.common.Failure.fail;
2727

2828
/**
2929
* The verifier has the role of checking the analyzed tree for failures and build a list of failures following this check.

x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/Planner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
package org.elasticsearch.xpack.eql.planner;
88

9-
import org.elasticsearch.xpack.eql.common.Failure;
109
import org.elasticsearch.xpack.eql.plan.physical.PhysicalPlan;
10+
import org.elasticsearch.xpack.ql.common.Failure;
1111
import org.elasticsearch.xpack.ql.plan.logical.LogicalPlan;
1212

1313
import java.util.List;

x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/PlanningException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import org.elasticsearch.rest.RestStatus;
99
import org.elasticsearch.xpack.eql.EqlClientException;
10-
import org.elasticsearch.xpack.eql.common.Failure;
10+
import org.elasticsearch.xpack.ql.common.Failure;
1111

1212
import java.util.Collection;
1313

x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/Verifier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
*/
66
package org.elasticsearch.xpack.eql.planner;
77

8-
import org.elasticsearch.xpack.eql.common.Failure;
98
import org.elasticsearch.xpack.eql.plan.physical.PhysicalPlan;
109
import org.elasticsearch.xpack.eql.plan.physical.Unexecutable;
1110
import org.elasticsearch.xpack.eql.plan.physical.UnplannedExec;
11+
import org.elasticsearch.xpack.ql.common.Failure;
1212

1313
import java.util.ArrayList;
1414
import java.util.List;
1515

16-
import static org.elasticsearch.xpack.eql.common.Failure.fail;
16+
import static org.elasticsearch.xpack.ql.common.Failure.fail;
1717

1818
abstract class Verifier {
1919

x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/common/Failure.java renamed to x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/common/Failure.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
package org.elasticsearch.xpack.eql.common;
7+
package org.elasticsearch.xpack.ql.common;
88

99
import org.elasticsearch.xpack.ql.tree.Location;
1010
import org.elasticsearch.xpack.ql.tree.Node;
@@ -36,7 +36,7 @@ public String message() {
3636

3737
@Override
3838
public int hashCode() {
39-
return Objects.hash(message, node);
39+
return Objects.hash(node);
4040
}
4141

4242
@Override
@@ -50,7 +50,7 @@ public boolean equals(Object obj) {
5050
}
5151

5252
Failure other = (Failure) obj;
53-
return Objects.equals(message, other.message) && Objects.equals(node, other.node);
53+
return Objects.equals(node, other.node);
5454
}
5555

5656
@Override
@@ -67,7 +67,7 @@ public static String failMessage(Collection<Failure> failures) {
6767
Location l = f.node().source().source();
6868
return "line " + l.getLineNumber() + ":" + l.getColumnNumber() + ": " + f.message();
6969
}).collect(Collectors.joining(StringUtils.NEW_LINE,
70-
format("Found {} problem{}\n", failures.size(), failures.size() > 1 ? "s" : StringUtils.EMPTY),
70+
format("Found {} problem{}\n", failures.size(), failures.size() > 1 ? "s" : StringUtils.EMPTY),
7171
StringUtils.EMPTY));
7272
}
7373
}

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
@@ -106,6 +106,6 @@ public void testHardLimitForSortOnAggregate() throws Exception {
106106
}
107107

108108
public static void assertFoundOneProblem(String commandResult) {
109-
assertEquals(START + "Bad request [[3;33;22mFound 1 problem(s)", commandResult);
109+
assertEquals(START + "Bad request [[3;33;22mFound 1 problem", commandResult);
110110
}
111111
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ 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\nline 1:8: Cannot determine columns for [*]", e.getMessage());
2424
}
2525
}
2626

2727
@Override
2828
public void testSelectFromMissingIndex() throws SQLException {
2929
try (Connection c = esJdbc()) {
3030
SQLException e = expectThrows(SQLException.class, () -> c.prepareStatement("SELECT * FROM test").executeQuery());
31-
assertEquals("Found 1 problem(s)\nline 1:15: Unknown index [test]", e.getMessage());
31+
assertEquals("Found 1 problem\nline 1:15: Unknown index [test]", e.getMessage());
3232
}
3333
}
3434

@@ -42,8 +42,8 @@ public void testSelectFromIndexWithoutTypes() throws Exception {
4242
try (Connection c = esJdbc()) {
4343
SQLException e = expectThrows(SQLException.class, () -> c.prepareStatement("SELECT * FROM test").executeQuery());
4444
// see https://github.com/elastic/elasticsearch/issues/34719
45-
//assertEquals("Found 1 problem(s)\nline 1:15: [test] doesn't have any types so it is incompatible with sql", e.getMessage());
46-
assertEquals("Found 1 problem(s)\nline 1:15: Unknown index [test]", e.getMessage());
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());
4747
}
4848
}
4949

@@ -52,7 +52,7 @@ public void testSelectMissingField() throws Exception {
5252
index("test", body -> body.field("test", "test"));
5353
try (Connection c = esJdbc()) {
5454
SQLException e = expectThrows(SQLException.class, () -> c.prepareStatement("SELECT missing FROM test").executeQuery());
55-
assertEquals("Found 1 problem(s)\nline 1:8: Unknown column [missing]", e.getMessage());
55+
assertEquals("Found 1 problem\nline 1:8: Unknown column [missing]", e.getMessage());
5656
}
5757
}
5858

@@ -61,7 +61,7 @@ public void testSelectMissingFunction() throws Exception {
6161
index("test", body -> body.field("foo", 1));
6262
try (Connection c = esJdbc()) {
6363
SQLException e = expectThrows(SQLException.class, () -> c.prepareStatement("SELECT missing(foo) FROM test").executeQuery());
64-
assertEquals("Found 1 problem(s)\nline 1:8: Unknown function [missing]", e.getMessage());
64+
assertEquals("Found 1 problem\nline 1:8: Unknown function [missing]", e.getMessage());
6565
}
6666
}
6767

@@ -71,7 +71,7 @@ public void testSelectProjectScoreInAggContext() throws Exception {
7171
try (Connection c = esJdbc()) {
7272
SQLException e = expectThrows(SQLException.class, () ->
7373
c.prepareStatement("SELECT foo, SCORE(), COUNT(*) FROM test GROUP BY foo").executeQuery());
74-
assertEquals("Found 1 problem(s)\nline 1:13: Cannot use non-grouped column [SCORE()], expected [foo]", e.getMessage());
74+
assertEquals("Found 1 problem\nline 1:13: Cannot use non-grouped column [SCORE()], expected [foo]", e.getMessage());
7575
}
7676
}
7777

@@ -82,7 +82,7 @@ public void testSelectOrderByScoreInAggContext() throws Exception {
8282
SQLException e = expectThrows(SQLException.class, () ->
8383
c.prepareStatement("SELECT foo, COUNT(*) FROM test GROUP BY foo ORDER BY SCORE()").executeQuery());
8484
assertEquals(
85-
"Found 1 problem(s)\nline 1:54: Cannot order by non-grouped column [SCORE()], expected [foo] or an aggregate function",
85+
"Found 1 problem\nline 1:54: Cannot order by non-grouped column [SCORE()], expected [foo] or an aggregate function",
8686
e.getMessage());
8787
}
8888
}
@@ -93,7 +93,7 @@ public void testSelectGroupByScore() throws Exception {
9393
try (Connection c = esJdbc()) {
9494
SQLException e = expectThrows(SQLException.class, () ->
9595
c.prepareStatement("SELECT COUNT(*) FROM test GROUP BY SCORE()").executeQuery());
96-
assertEquals("Found 1 problem(s)\nline 1:36: Cannot use [SCORE()] for grouping", e.getMessage());
96+
assertEquals("Found 1 problem\nline 1:36: Cannot use [SCORE()] for grouping", e.getMessage());
9797
}
9898
}
9999

@@ -113,7 +113,7 @@ public void testSelectScoreInScalar() throws Exception {
113113
try (Connection c = esJdbc()) {
114114
SQLException e = expectThrows(SQLException.class, () ->
115115
c.prepareStatement("SELECT SIN(SCORE()) FROM test").executeQuery());
116-
assertThat(e.getMessage(), startsWith("Found 1 problem(s)\nline 1:12: [SCORE()] cannot be an argument to a function"));
116+
assertThat(e.getMessage(), startsWith("Found 1 problem\nline 1:12: [SCORE()] cannot be an argument to a function"));
117117
}
118118
}
119119

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

Lines changed: 0 additions & 59 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import org.elasticsearch.common.logging.LoggerMessageFormat;
99
import org.elasticsearch.xpack.ql.capabilities.Resolvables;
10+
import org.elasticsearch.xpack.ql.common.Failure;
1011
import org.elasticsearch.xpack.ql.expression.Alias;
1112
import org.elasticsearch.xpack.ql.expression.Attribute;
1213
import org.elasticsearch.xpack.ql.expression.AttributeMap;
@@ -45,7 +46,6 @@
4546
import org.elasticsearch.xpack.ql.type.UnsupportedEsField;
4647
import org.elasticsearch.xpack.ql.util.CollectionUtils;
4748
import org.elasticsearch.xpack.ql.util.Holder;
48-
import org.elasticsearch.xpack.sql.analysis.analyzer.Verifier.Failure;
4949
import org.elasticsearch.xpack.sql.expression.Foldables;
5050
import org.elasticsearch.xpack.sql.expression.SubQueryExpression;
5151
import org.elasticsearch.xpack.sql.expression.function.scalar.Cast;

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

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,20 @@
55
*/
66
package org.elasticsearch.xpack.sql.analysis.analyzer;
77

8-
import org.elasticsearch.xpack.ql.tree.Location;
9-
import org.elasticsearch.xpack.ql.util.StringUtils;
10-
import org.elasticsearch.xpack.sql.analysis.AnalysisException;
11-
import org.elasticsearch.xpack.sql.analysis.analyzer.Verifier.Failure;
8+
import org.elasticsearch.rest.RestStatus;
9+
import org.elasticsearch.xpack.ql.common.Failure;
10+
import org.elasticsearch.xpack.sql.SqlClientException;
1211

1312
import java.util.Collection;
14-
import java.util.stream.Collectors;
1513

16-
17-
public class VerificationException extends AnalysisException {
18-
19-
private final Collection<Failure> failures;
14+
public class VerificationException extends SqlClientException {
2015

2116
protected VerificationException(Collection<Failure> sources) {
22-
super(null, StringUtils.EMPTY);
23-
failures = sources;
17+
super(Failure.failMessage(sources));
2418
}
2519

2620
@Override
27-
public String getMessage() {
28-
return failures.stream()
29-
.map(f -> {
30-
Location l = f.node().source().source();
31-
return "line " + l.getLineNumber() + ":" + l.getColumnNumber() + ": " + f.message();
32-
})
33-
.collect(Collectors.joining(StringUtils.NEW_LINE, "Found " + failures.size() + " problem(s)\n", StringUtils.EMPTY));
21+
public RestStatus status() {
22+
return RestStatus.BAD_REQUEST;
3423
}
3524
}

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

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.elasticsearch.xpack.sql.analysis.analyzer;
77

88
import org.elasticsearch.xpack.ql.capabilities.Unresolvable;
9+
import org.elasticsearch.xpack.ql.common.Failure;
910
import org.elasticsearch.xpack.ql.expression.Alias;
1011
import org.elasticsearch.xpack.ql.expression.Attribute;
1112
import org.elasticsearch.xpack.ql.expression.AttributeMap;
@@ -61,12 +62,11 @@
6162
import java.util.List;
6263
import java.util.Locale;
6364
import java.util.Map;
64-
import java.util.Objects;
6565
import java.util.Set;
6666
import java.util.function.Consumer;
6767

6868
import static java.util.stream.Collectors.toMap;
69-
import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
69+
import static org.elasticsearch.xpack.ql.common.Failure.fail;
7070
import static org.elasticsearch.xpack.ql.util.CollectionUtils.combine;
7171
import static org.elasticsearch.xpack.sql.stats.FeatureMetric.COMMAND;
7272
import static org.elasticsearch.xpack.sql.stats.FeatureMetric.GROUPBY;
@@ -89,52 +89,6 @@ public Verifier(Metrics metrics) {
8989
this.metrics = metrics;
9090
}
9191

92-
static class Failure {
93-
private final Node<?> node;
94-
private final String message;
95-
96-
Failure(Node<?> node, String message) {
97-
this.node = node;
98-
this.message = message;
99-
}
100-
101-
Node<?> node() {
102-
return node;
103-
}
104-
105-
String message() {
106-
return message;
107-
}
108-
109-
@Override
110-
public int hashCode() {
111-
return Objects.hash(node);
112-
}
113-
114-
@Override
115-
public boolean equals(Object obj) {
116-
if (this == obj) {
117-
return true;
118-
}
119-
120-
if (obj == null || getClass() != obj.getClass()) {
121-
return false;
122-
}
123-
124-
Verifier.Failure other = (Verifier.Failure) obj;
125-
return Objects.equals(node, other.node);
126-
}
127-
128-
@Override
129-
public String toString() {
130-
return message;
131-
}
132-
}
133-
134-
private static Failure fail(Node<?> source, String message, Object... args) {
135-
return new Failure(source, format(message, args));
136-
}
137-
13892
public Map<Node<?>, String> verifyFailures(LogicalPlan plan) {
13993
Collection<Failure> failures = verify(plan);
14094
return failures.stream().collect(toMap(Failure::node, Failure::message));

0 commit comments

Comments
 (0)