Skip to content

Commit 4b73f48

Browse files
committed
Misc. refactoring after code self-review
Signed-off-by: Simeon Widdis <sawiddis@amazon.com>
1 parent d59d81a commit 4b73f48

File tree

13 files changed

+37
-76
lines changed

13 files changed

+37
-76
lines changed

common/src/main/java/org/opensearch/sql/common/error/ErrorReport.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ public static class Builder {
189189
private Builder(Exception cause) {
190190
this.cause = cause;
191191
// Default details to the original exception message
192-
this.details = cause.getLocalizedMessage();
192+
this.details =
193+
cause.getLocalizedMessage() != null ? cause.getLocalizedMessage() : cause.getMessage();
193194
}
194195

195196
/** Set the machine-readable error code. */
@@ -200,7 +201,10 @@ public Builder code(ErrorCode code) {
200201

201202
/** Set the query processing stage where the error occurred. */
202203
public Builder stage(QueryProcessingStage stage) {
203-
this.stage = stage;
204+
// Don't overwrite more-specific stages with less-specific ones
205+
if (this.stage == null) {
206+
this.stage = stage;
207+
}
204208
return this;
205209
}
206210

common/src/main/java/org/opensearch/sql/common/error/QueryProcessingStage.java

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,20 @@
55

66
package org.opensearch.sql.common.error;
77

8+
import lombok.Getter;
9+
810
/**
911
* Enumeration of query processing stages for error location tracking. These stages represent the
10-
* major phases of query execution in the Calcite query planner.
11-
*
12-
* <p>Stage progression for a typical query: PARSING → AST_BUILDING → ANALYZING → OPTIMIZING →
13-
* PLAN_CONVERSION → EXECUTING → RESULT_PROCESSING
12+
* major phases of query execution in the Calcite query planner. May not be a complete list, add
13+
* stages if needed.
1414
*/
15+
@Getter
1516
public enum QueryProcessingStage {
16-
/**
17-
* PARSING stage: PPL/SQL syntax parsing via ANTLR lexer/parser. Errors: Syntax errors, unexpected
18-
* tokens, malformed queries.
19-
*/
20-
PARSING("Validating query syntax"),
21-
22-
/**
23-
* AST_BUILDING stage: Abstract Syntax Tree construction from parse tree. Errors: Invalid AST
24-
* structure, node creation failures.
25-
*/
26-
AST_BUILDING("Assembling the query steps"),
27-
2817
/**
2918
* ANALYZING stage: Semantic validation and type checking. Errors: Field not found, type
3019
* mismatches, semantic violations.
3120
*/
32-
ANALYZING("Validating the query"),
33-
34-
/**
35-
* OPTIMIZING stage: Logical plan optimization with transformation rules. Errors: Optimization
36-
* failures, rule application errors.
37-
*/
38-
OPTIMIZING("Optimizing the query"),
21+
ANALYZING("Parsing and validating the query"),
3922

4023
/**
4124
* PLAN_CONVERSION stage: Conversion to Calcite execution plan with system limits. Errors:
@@ -47,31 +30,15 @@ public enum QueryProcessingStage {
4730
* EXECUTING stage: Query execution via OpenSearch engine. Errors: Execution failures, index
4831
* access errors, resource limits.
4932
*/
50-
EXECUTING("Running the query"),
51-
52-
/**
53-
* RESULT_PROCESSING stage: Result formatting and cursor management. Errors: Result formatting
54-
* failures, cursor errors.
55-
*/
56-
RESULT_PROCESSING("Processing the query results"),
57-
58-
/**
59-
* CLUSTER_VALIDATION stage: Prevalidation that cluster is healthy before execution. Errors:
60-
* Cluster unhealthy, nodes unavailable.
61-
*/
62-
CLUSTER_VALIDATION("Checking cluster health");
33+
EXECUTING("Running the query");
6334

35+
/** -- GETTER -- Get human-readable display name for this stage. */
6436
private final String displayName;
6537

6638
QueryProcessingStage(String displayName) {
6739
this.displayName = displayName;
6840
}
6941

70-
/** Get human-readable display name for this stage. */
71-
public String getDisplayName() {
72-
return displayName;
73-
}
74-
7542
/** Get lowercase name suitable for JSON serialization. */
7643
public String toJsonKey() {
7744
return name().toLowerCase();

common/src/main/java/org/opensearch/sql/common/error/StageErrorHandler.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ public static <T> T executeStage(
3737
QueryProcessingStage stage, Supplier<T> operation, String location) {
3838
try {
3939
return operation.get();
40-
} catch (ErrorReport e) {
41-
// If already an ErrorReport, just add stage if not set
42-
throw ErrorReport.wrap(e).stage(stage).location(location).build();
4340
} catch (Exception e) {
4441
throw ErrorReport.wrap(e).stage(stage).location(location).build();
4542
}

common/src/test/java/org/opensearch/sql/common/error/ErrorReportTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import static org.junit.jupiter.api.Assertions.*;
99

1010
import java.util.Map;
11+
import org.hamcrest.CoreMatchers;
12+
import org.hamcrest.MatcherAssert;
1113
import org.junit.jupiter.api.Test;
1214

1315
/** Unit tests for ErrorReport. */
@@ -64,7 +66,7 @@ public void testErrorReportJsonMapWithStageInContext() {
6466
@SuppressWarnings("unchecked")
6567
Map<String, Object> context = (Map<String, Object>) json.get("context");
6668
assertEquals("analyzing", context.get("stage"));
67-
assertEquals("Validating the query", context.get("stage_description"));
69+
assertEquals("Parsing and validating the query", context.get("stage_description"));
6870
assertEquals("test", context.get("field_name"));
6971
}
7072

@@ -140,11 +142,11 @@ public void testToDetailedMessage() {
140142

141143
String message = report.toDetailedMessage();
142144

143-
assertTrue(message.contains("FIELD_NOT_FOUND"));
144-
assertTrue(message.contains("Validating the query"));
145-
assertTrue(message.contains("Field not found"));
146-
assertTrue(message.contains("while resolving fields"));
147-
assertTrue(message.contains("field_name"));
148-
assertTrue(message.contains("Check field name"));
145+
MatcherAssert.assertThat(message, CoreMatchers.containsString("FIELD_NOT_FOUND"));
146+
MatcherAssert.assertThat(message, CoreMatchers.containsString("validating the query"));
147+
MatcherAssert.assertThat(message, CoreMatchers.containsString("Field not found"));
148+
MatcherAssert.assertThat(message, CoreMatchers.containsString("while resolving fields"));
149+
MatcherAssert.assertThat(message, CoreMatchers.containsString("field_name"));
150+
MatcherAssert.assertThat(message, CoreMatchers.containsString("Check field name"));
149151
}
150152
}

docs/user/ppl/cmd/mvcombine.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,6 @@ source=mvcombine_data
124124

125125
Expected output:
126126
```text
127-
{'context': {'stage': 'analyzing', 'stage_description': 'Validating the query'}, 'details': 'Field [does_not_exist] not found.', 'location': ['while preparing and validating the query plan'], 'code': 'FIELD_NOT_FOUND', 'type': 'IllegalArgumentException'}
127+
{'context': {'stage': 'analyzing', 'stage_description': 'Parsing and validating the query'}, 'details': 'Field [does_not_exist] not found.', 'location': ['while preparing and validating the query plan'], 'code': 'FIELD_NOT_FOUND', 'type': 'IllegalArgumentException'}
128128
Error: Query returned no data
129129
```

docs/user/ppl/cmd/mvexpand.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,6 @@ source=people
132132

133133
Expected output:
134134
```text
135-
{'context': {'stage': 'analyzing', 'stage_description': 'Validating the query', 'command': 'mvexpand'}, 'details': "Field 'tags' not found in the schema", 'location': ['while preparing and validating the query plan', 'while evaluating the input field for mvexpand'], 'code': 'FIELD_NOT_FOUND', 'type': 'SemanticCheckException'}
135+
{'context': {'stage': 'analyzing', 'stage_description': 'Parsing and validating the query', 'command': 'mvexpand'}, 'details': "Field 'tags' not found in the schema", 'location': ['while preparing and validating the query plan', 'while evaluating the input field for mvexpand'], 'code': 'FIELD_NOT_FOUND', 'type': 'SemanticCheckException'}
136136
Error: Query returned no data
137137
```

docs/user/ppl/cmd/rex.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ source=accounts
228228
The query returns the following results:
229229

230230
```text
231-
{'context': {'stage': 'analyzing', 'stage_description': 'Validating the query', 'command': 'rex'}, 'details': "Invalid capture group name 'user_name'.", 'location': ['while preparing and validating the query plan', 'while processing the rex command', 'while validating the capture groups for the pattern'], 'code': 'SYNTAX_ERROR', 'type': 'IllegalArgumentException', 'suggestion': 'Java Regex capture groups must be alphanumeric and start with a letter. Update the capture group to be alphanumeric.'}
231+
{'context': {'stage': 'analyzing', 'stage_description': 'Parsing and validating the query', 'command': 'rex'}, 'details': "Invalid capture group name 'user_name'.", 'location': ['while preparing and validating the query plan', 'while processing the rex command', 'while validating the capture groups for the pattern'], 'code': 'SYNTAX_ERROR', 'type': 'IllegalArgumentException', 'suggestion': 'Java Regex capture groups must be alphanumeric and start with a letter. Update the capture group to be alphanumeric.'}
232232
Error: Query returned no data
233233
```
234234

docs/user/ppl/interfaces/protocol.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Expected output:
123123
"context": {
124124
"stage": "analyzing",
125125
"index_name": "unknown",
126-
"stage_description": "Validating the query"
126+
"stage_description": "Parsing and validating the query"
127127
},
128128
"details": "no such index [unknown]",
129129
"location": [

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalciteErrorReportStageIT.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void testMultipleFieldErrorsIncludeStage() throws IOException {
108108
}
109109

110110
@Test
111-
public void testErrorReportTypeIsCorrect() throws IOException {
111+
public void testErrorReportTypeMatchesExceptionType() throws IOException {
112112
ResponseException exception =
113113
assertThrows(
114114
ResponseException.class,
@@ -126,7 +126,7 @@ public void testErrorReportTypeIsCorrect() throws IOException {
126126
}
127127

128128
@Test
129-
public void testErrorCodePresent() throws IOException {
129+
public void testFieldNotFoundIncludesErrorCode() throws IOException {
130130
ResponseException exception =
131131
assertThrows(
132132
ResponseException.class,
@@ -136,12 +136,9 @@ public void testErrorCodePresent() throws IOException {
136136
JSONObject response = new JSONObject(responseBody);
137137
JSONObject error = response.getJSONObject("error");
138138

139-
// Verify error may have code field (optional, but if present should be valid)
140-
if (error.has("code")) {
141-
String code = error.getString("code");
142-
assertFalse("Error code should not be empty", code.isEmpty());
143-
assertFalse("Error code should not be UNKNOWN", code.equals("UNKNOWN"));
144-
}
139+
String code = error.getString("code");
140+
assertFalse("Error code should not be empty", code.isEmpty());
141+
assertFalse("Error code should not be UNKNOWN", code.equals("UNKNOWN"));
145142
}
146143

147144
@Test

integ-test/src/test/java/org/opensearch/sql/calcite/standalone/CalcitePPLIntegTestCase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,7 @@ public void onFailure(Exception e) {
200200
case IllegalArgumentException illegalArgumentException ->
201201
// most exceptions thrown by Calcite when resolve a plan.
202202
throw illegalArgumentException;
203-
case null, default ->
204-
throw new IllegalStateException("Exception happened during execution", e);
203+
default -> throw new IllegalStateException("Exception happened during execution", e);
205204
}
206205
}
207206
},

0 commit comments

Comments
 (0)