Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parsing error message field to TrinoQueryProperties #497

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,9 @@ public class TrinoQueryProperties
private Set<String> schemas = ImmutableSet.of();
private Set<String> catalogSchemas = ImmutableSet.of();
private boolean isNewQuerySubmission;
@SuppressWarnings("unused")
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 +116,8 @@ public TrinoQueryProperties(
@JsonProperty("schemas") Set<String> schemas,
@JsonProperty("catalogSchemas") Set<String> catalogSchemas,
@JsonProperty("isNewQuerySubmission") boolean isNewQuerySubmission,
@JsonProperty("isQueryParsingSuccessful") boolean isQueryParsingSuccessful)
@JsonProperty("isQueryParsingSuccessful") boolean isQueryParsingSuccessful,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we still need this field? I thought just having the getter is sufficient.

@JsonProperty("errorMessage") Optional<String> errorMessage)
{
this.body = requireNonNullElse(body, "");
this.queryType = requireNonNullElse(queryType, "");
Expand All @@ -127,6 +130,7 @@ public TrinoQueryProperties(
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 +212,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 +294,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,7 +382,6 @@ private void setCatalogAndSchemaNameFromSchemaQualifiedName(

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

Expand Down Expand Up @@ -513,7 +515,13 @@ public boolean isNewQuerySubmission()
@JsonProperty("isQueryParsingSuccessful")
public boolean isQueryParsingSuccessful()
{
return isQueryParsingSuccessful;
return errorMessage.isEmpty();
}

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

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

String trinoQueryPropertiesJson = codec.toJson(trinoQueryProperties);
TrinoQueryProperties deserializedTrinoQueryProperties = codec.fromJson(trinoQueryPropertiesJson);
Expand All @@ -53,6 +54,7 @@ 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.isQueryParsingSuccessful()).isEqualTo(trinoQueryProperties.isQueryParsingSuccessful());;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assertThat(deserializedTrinoQueryProperties.isQueryParsingSuccessful()).isEqualTo(trinoQueryProperties.isQueryParsingSuccessful());;
assertThat(deserializedTrinoQueryProperties.isQueryParsingSuccessful()).isEqualTo(trinoQueryProperties.isQueryParsingSuccessful());

assertThat(deserializedTrinoQueryProperties.getErrorMessage()).isEqualTo(trinoQueryProperties.getErrorMessage());
}
}
Loading