Skip to content

Commit 7b64f4a

Browse files
Chaho12ebyhr
authored andcommitted
Add error message field to TrinoQueryProperties
1 parent 98c8e03 commit 7b64f4a

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

docs/routing-rules.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,10 @@ added to the list of tables in all contexts, including statements such as
195195
A routing rule can call the following methods on the `trinoQueryProperties`
196196
object:
197197

198+
* `String errorMessage()`: the error message only if there was any error while
199+
creating `trinoQueryProperties` object.
198200
* `boolean isNewQuerySubmission()`: is the request a POST to the `v1/statement`
199-
query endpoint.
200-
* `boolean isQueryParsingSuccessful()`: was the request successfully parsed.
201+
query endpoint.
201202
* `String getQueryType()`: the class name of the `Statement`, e.g. `ShowCreate`.
202203
Note that these are not mapped to the `ResourceGroup` query types. For a full
203204
list of potential query types, see the classes in

gateway-ha/src/main/java/io/trino/gateway/ha/router/TrinoQueryProperties.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public class TrinoQueryProperties
9696
private Set<String> schemas = ImmutableSet.of();
9797
private Set<String> catalogSchemas = ImmutableSet.of();
9898
private boolean isNewQuerySubmission;
99-
private boolean isQueryParsingSuccessful;
99+
private Optional<String> errorMessage = Optional.empty();
100100

101101
public static final String TRINO_CATALOG_HEADER_NAME = "X-Trino-Catalog";
102102
public static final String TRINO_SCHEMA_HEADER_NAME = "X-Trino-Schema";
@@ -114,7 +114,7 @@ public TrinoQueryProperties(
114114
@JsonProperty("schemas") Set<String> schemas,
115115
@JsonProperty("catalogSchemas") Set<String> catalogSchemas,
116116
@JsonProperty("isNewQuerySubmission") boolean isNewQuerySubmission,
117-
@JsonProperty("isQueryParsingSuccessful") boolean isQueryParsingSuccessful)
117+
@JsonProperty("errorMessage") Optional<String> errorMessage)
118118
{
119119
this.body = requireNonNullElse(body, "");
120120
this.queryType = requireNonNullElse(queryType, "");
@@ -126,7 +126,7 @@ public TrinoQueryProperties(
126126
this.schemas = requireNonNullElse(schemas, ImmutableSet.of());
127127
this.catalogSchemas = requireNonNullElse(catalogSchemas, ImmutableSet.of());
128128
this.isNewQuerySubmission = isNewQuerySubmission;
129-
this.isQueryParsingSuccessful = isQueryParsingSuccessful;
129+
this.errorMessage = requireNonNullElse(errorMessage, Optional.empty());
130130
isClientsUseV2Format = false;
131131
}
132132

@@ -208,19 +208,18 @@ else if (statement instanceof ExecuteImmediate executeImmediate) {
208208
catalogSchemaBuilder.addAll(
209209
tables.stream().map(qualifiedName -> format("%s.%s", qualifiedName.getParts().getFirst(), qualifiedName.getParts().get(1))).iterator());
210210
catalogSchemas = catalogSchemaBuilder.build();
211-
isQueryParsingSuccessful = true;
212211
}
213212
catch (IOException e) {
214213
log.warn("Error extracting request body for rules processing: %s", e.getMessage());
215-
isQueryParsingSuccessful = false;
214+
errorMessage = Optional.of(e.getMessage());
216215
}
217216
catch (ParsingException e) {
218217
log.info("Could not parse request body as SQL: %s; Message: %s", body, e.getMessage());
219-
isQueryParsingSuccessful = false;
218+
errorMessage = Optional.of(e.getMessage());
220219
}
221220
catch (RequestParsingException e) {
222221
log.warn(e, "Error parsing request for rules");
223-
isQueryParsingSuccessful = false;
222+
errorMessage = Optional.of(e.getMessage());
224223
}
225224
}
226225

@@ -291,7 +290,7 @@ private void getNames(Node node, ImmutableSet.Builder<QualifiedName> tableBuilde
291290
targetSchema = QualifiedName.of(defaultCatalog.orElseThrow(), s.getTarget().getValue());
292291
}
293292
else {
294-
isQueryParsingSuccessful = false;
293+
errorMessage = Optional.of("defaultCatalog is not present");
295294
return;
296295
}
297296
}
@@ -379,7 +378,6 @@ private void setCatalogAndSchemaNameFromSchemaQualifiedName(
379378

380379
private RequestParsingException unsetDefaultExceptionSupplier()
381380
{
382-
isQueryParsingSuccessful = false;
383381
return new RequestParsingException("Name not fully qualified");
384382
}
385383

@@ -513,7 +511,13 @@ public boolean isNewQuerySubmission()
513511
@JsonProperty("isQueryParsingSuccessful")
514512
public boolean isQueryParsingSuccessful()
515513
{
516-
return isQueryParsingSuccessful;
514+
return errorMessage.isEmpty();
515+
}
516+
517+
@JsonProperty
518+
public Optional<String> getErrorMessage()
519+
{
520+
return errorMessage;
517521
}
518522

519523
public static class AlternateStatementRequestBodyFormat

gateway-ha/src/test/java/io/trino/gateway/ha/router/TestTrinoQueryProperties.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void testJsonCreator()
3838
ImmutableSet.of("s"),
3939
ImmutableSet.of("c.s"),
4040
true,
41-
true);
41+
Optional.empty());
4242

4343
String trinoQueryPropertiesJson = codec.toJson(trinoQueryProperties);
4444
TrinoQueryProperties deserializedTrinoQueryProperties = codec.fromJson(trinoQueryPropertiesJson);
@@ -54,5 +54,6 @@ void testJsonCreator()
5454
assertThat(deserializedTrinoQueryProperties.getCatalogSchemas()).isEqualTo(trinoQueryProperties.getCatalogSchemas());
5555
assertThat(deserializedTrinoQueryProperties.isNewQuerySubmission()).isEqualTo(trinoQueryProperties.isNewQuerySubmission());
5656
assertThat(deserializedTrinoQueryProperties.isQueryParsingSuccessful()).isEqualTo(trinoQueryProperties.isQueryParsingSuccessful());
57+
assertThat(deserializedTrinoQueryProperties.getErrorMessage()).isEqualTo(trinoQueryProperties.getErrorMessage());
5758
}
5859
}

0 commit comments

Comments
 (0)