Skip to content

Commit

Permalink
Add parsing error message field to TrinoQueryProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaho12 committed Sep 30, 2024
1 parent a605978 commit a6024fd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public class TrinoQueryProperties
private Set<String> catalogSchemas = ImmutableSet.of();
private boolean isNewQuerySubmission;
private boolean isQueryParsingSuccessful;
private Optional<String> parseErrorMessage;

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 +115,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,
@JsonProperty("parseErrorMessage") Optional<String> parseErrorMessage)
{
this.body = requireNonNullElse(body, "");
this.queryType = requireNonNullElse(queryType, "");
Expand All @@ -127,6 +129,7 @@ public TrinoQueryProperties(
this.catalogSchemas = requireNonNullElse(catalogSchemas, ImmutableSet.of());
this.isNewQuerySubmission = isNewQuerySubmission;
this.isQueryParsingSuccessful = isQueryParsingSuccessful;
this.parseErrorMessage = requireNonNullElse(parseErrorMessage, Optional.empty());
isClientsUseV2Format = false;
}

Expand Down Expand Up @@ -212,14 +215,17 @@ else if (statement instanceof ExecuteImmediate executeImmediate) {
}
catch (IOException e) {
log.warn("Error extracting request body for rules processing: %s", e.getMessage());
parseErrorMessage = Optional.ofNullable(e.getMessage());
isQueryParsingSuccessful = false;
}
catch (ParsingException e) {
log.info("Could not parse request body as SQL: %s; Message: %s", body, e.getMessage());
parseErrorMessage = Optional.ofNullable(e.getMessage());
isQueryParsingSuccessful = false;
}
catch (RequestParsingException e) {
log.warn(e, "Error parsing request for rules");
parseErrorMessage = Optional.ofNullable(e.getMessage());
isQueryParsingSuccessful = false;
}
}
Expand Down Expand Up @@ -292,6 +298,7 @@ private void getNames(Node node, ImmutableSet.Builder<QualifiedName> tableBuilde
}
else {
isQueryParsingSuccessful = false;
parseErrorMessage = Optional.of("defaultCatalog is not present");
return;
}
}
Expand Down Expand Up @@ -379,8 +386,10 @@ private void setCatalogAndSchemaNameFromSchemaQualifiedName(

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

private QualifiedName qualifyName(QualifiedName table)
Expand Down Expand Up @@ -519,6 +528,12 @@ public boolean isQueryParsingSuccessful()
return isQueryParsingSuccessful;
}

@JsonProperty
public Optional<String> getParseErrorMessage()
{
return parseErrorMessage;
}

public static class AlternateStatementRequestBodyFormat
{
// Based on https://github.com/trinodb/trino/wiki/trino-v2-client-protocol, without session
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 @@ -54,5 +55,6 @@ void testJsonCreator()
assertThat(deserializedTrinoQueryProperties.getCatalogSchemas()).isEqualTo(trinoQueryProperties.getCatalogSchemas());
assertThat(deserializedTrinoQueryProperties.isNewQuerySubmission()).isEqualTo(trinoQueryProperties.isNewQuerySubmission());
assertThat(deserializedTrinoQueryProperties.isQueryParsingSuccessful()).isEqualTo(trinoQueryProperties.isQueryParsingSuccessful());
assertThat(deserializedTrinoQueryProperties.getParseErrorMessage()).isEqualTo(trinoQueryProperties.getParseErrorMessage());
}
}

0 comments on commit a6024fd

Please sign in to comment.