Skip to content

Commit

Permalink
Add error message field to TrinoQueryProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaho12 committed Oct 2, 2024
1 parent 8165f34 commit 1e2e607
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
5 changes: 3 additions & 2 deletions docs/routing-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,10 @@ added to the list of tables in all contexts, including statements such as
A routing rule can call the following methods on the `trinoQueryProperties`
object:

* `String errorMessage()`: the error message only if there was any error while
creating `trinoQueryProperties` object.
* `boolean isNewQuerySubmission()`: is the request a POST to the `v1/statement`
query endpoint.
* `boolean isQueryParsingSuccessful()`: was the request successfully parsed.
query endpoint.
* `String getQueryType()`: the class name of the `Statement`, e.g. `ShowCreate`.
Note that these are not mapped to the `ResourceGroup` query types. For a full
list of potential query types, see the classes in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class TrinoQueryProperties
private Set<String> schemas = ImmutableSet.of();
private Set<String> catalogSchemas = ImmutableSet.of();
private boolean isNewQuerySubmission;
private boolean isQueryParsingSuccessful;
private Optional<String> errorMessage;

public static final String TRINO_CATALOG_HEADER_NAME = "X-Trino-Catalog";
public static final String TRINO_SCHEMA_HEADER_NAME = "X-Trino-Schema";
Expand All @@ -114,7 +114,7 @@ public TrinoQueryProperties(
@JsonProperty("schemas") Set<String> schemas,
@JsonProperty("catalogSchemas") Set<String> catalogSchemas,
@JsonProperty("isNewQuerySubmission") boolean isNewQuerySubmission,
@JsonProperty("isQueryParsingSuccessful") boolean isQueryParsingSuccessful)
@JsonProperty("errorMessage") Optional<String> errorMessage)
{
this.body = requireNonNullElse(body, "");
this.queryType = requireNonNullElse(queryType, "");
Expand All @@ -126,7 +126,7 @@ public TrinoQueryProperties(
this.schemas = requireNonNullElse(schemas, ImmutableSet.of());
this.catalogSchemas = requireNonNullElse(catalogSchemas, ImmutableSet.of());
this.isNewQuerySubmission = isNewQuerySubmission;
this.isQueryParsingSuccessful = isQueryParsingSuccessful;
this.errorMessage = requireNonNullElse(errorMessage, Optional.empty());
isClientsUseV2Format = false;
}

Expand Down Expand Up @@ -208,19 +208,18 @@ else if (statement instanceof ExecuteImmediate executeImmediate) {
catalogSchemaBuilder.addAll(
tables.stream().map(qualifiedName -> format("%s.%s", qualifiedName.getParts().getFirst(), qualifiedName.getParts().get(1))).iterator());
catalogSchemas = catalogSchemaBuilder.build();
isQueryParsingSuccessful = true;
}
catch (IOException e) {
log.warn("Error extracting request body for rules processing: %s", e.getMessage());
isQueryParsingSuccessful = false;
errorMessage = Optional.of(e.getMessage());
}
catch (ParsingException e) {
log.info("Could not parse request body as SQL: %s; Message: %s", body, e.getMessage());
isQueryParsingSuccessful = false;
errorMessage = Optional.of(e.getMessage());
}
catch (RequestParsingException e) {
log.warn(e, "Error parsing request for rules");
isQueryParsingSuccessful = false;
errorMessage = Optional.of(e.getMessage());
}
}

Expand Down Expand Up @@ -291,7 +290,7 @@ private void getNames(Node node, ImmutableSet.Builder<QualifiedName> tableBuilde
targetSchema = QualifiedName.of(defaultCatalog.orElseThrow(), s.getTarget().getValue());
}
else {
isQueryParsingSuccessful = false;
errorMessage = Optional.of("defaultCatalog is not present");
return;
}
}
Expand Down Expand Up @@ -379,8 +378,8 @@ private void setCatalogAndSchemaNameFromSchemaQualifiedName(

private RequestParsingException unsetDefaultExceptionSupplier()
{
isQueryParsingSuccessful = false;
return new RequestParsingException("Name not fully qualified");
String errorString = "Name not fully qualified";
return new RequestParsingException(errorString);
}

private QualifiedName qualifyName(QualifiedName table)
Expand Down Expand Up @@ -510,10 +509,10 @@ public boolean isNewQuerySubmission()
return isNewQuerySubmission;
}

@JsonProperty("isQueryParsingSuccessful")
public boolean isQueryParsingSuccessful()
@JsonProperty
public Optional<String> getErrorMessage()
{
return isQueryParsingSuccessful;
return errorMessage;
}

public static class AlternateStatementRequestBodyFormat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void testJsonCreator()
ImmutableSet.of("s"),
ImmutableSet.of("c.s"),
true,
true);
Optional.empty());

String trinoQueryPropertiesJson = codec.toJson(trinoQueryProperties);
TrinoQueryProperties deserializedTrinoQueryProperties = codec.fromJson(trinoQueryPropertiesJson);
Expand All @@ -53,6 +53,6 @@ void testJsonCreator()
assertThat(deserializedTrinoQueryProperties.getCatalogs()).isEqualTo(trinoQueryProperties.getCatalogs());
assertThat(deserializedTrinoQueryProperties.getCatalogSchemas()).isEqualTo(trinoQueryProperties.getCatalogSchemas());
assertThat(deserializedTrinoQueryProperties.isNewQuerySubmission()).isEqualTo(trinoQueryProperties.isNewQuerySubmission());
assertThat(deserializedTrinoQueryProperties.isQueryParsingSuccessful()).isEqualTo(trinoQueryProperties.isQueryParsingSuccessful());
assertThat(deserializedTrinoQueryProperties.getErrorMessage()).isEqualTo(trinoQueryProperties.getErrorMessage());
}
}

0 comments on commit 1e2e607

Please sign in to comment.