Skip to content

Commit 33103ec

Browse files
committed
Apply comments and revert TestGatewayHaSingleBackend
1 parent 25359b1 commit 33103ec

File tree

6 files changed

+75
-62
lines changed

6 files changed

+75
-62
lines changed

gateway-ha/src/main/java/io/trino/gateway/ha/handler/ProxyUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import io.airlift.log.Logger;
1919
import io.trino.gateway.ha.router.SerializableExpression;
2020
import io.trino.gateway.ha.router.TrinoQueryProperties;
21-
import io.trino.sql.tree.Expression;
2221
import io.trino.sql.tree.StringLiteral;
2322
import jakarta.servlet.http.HttpServletRequest;
2423
import jakarta.ws.rs.HttpMethod;

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import com.fasterxml.jackson.annotation.JsonCreator;
1717
import com.fasterxml.jackson.annotation.JsonProperty;
1818
import io.trino.sql.tree.CallArgument;
19-
import io.trino.sql.tree.Expression;
2019
import io.trino.sql.tree.Identifier;
2120

2221
import java.util.Objects;
@@ -28,21 +27,21 @@
2827
// additional complexity and is not meaningful in this context
2928
public class SerializableCallArgument
3029
{
31-
private final Optional<Identifier> name;
30+
private final Optional<String> name;
3231
private final SerializableExpression value;
3332

3433
@JsonCreator
3534
public SerializableCallArgument(
3635
@JsonProperty("name") Optional<String> name,
3736
@JsonProperty("value") SerializableExpression value)
3837
{
39-
this.name = requireNonNull(name, "name is null").map(Identifier::new);
38+
this.name = name;
4039
this.value = requireNonNull(value, "value is null");
4140
}
4241

4342
public SerializableCallArgument(CallArgument callArgument)
4443
{
45-
this.name = callArgument.getName();
44+
this.name = callArgument.getName().map(Identifier::getValue);
4645
this.value = new SerializableExpression(callArgument.getValue());
4746
}
4847

@@ -55,7 +54,7 @@ public SerializableExpression getValue()
5554
@JsonProperty
5655
public Optional<String> getName()
5756
{
58-
return name.map(Identifier::getValue);
57+
return name;
5958
}
6059

6160
@Override
@@ -66,11 +65,9 @@ public boolean equals(Object obj)
6665
}
6766
else if (obj != null && this.getClass() == obj.getClass()) {
6867
SerializableCallArgument that = (SerializableCallArgument) obj;
69-
return Objects.equals(this.value, that.value) && Objects.equals(this.name, that.name);
70-
}
71-
else {
72-
return false;
68+
return Objects.equals(this.value, that.value) && Objects.equals(this.name, that.name);
7369
}
70+
return false;
7471
}
7572

7673
@Override

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

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,21 @@
1515

1616
import com.fasterxml.jackson.annotation.JsonCreator;
1717
import com.fasterxml.jackson.annotation.JsonProperty;
18-
import com.google.common.collect.ImmutableList;
1918
import io.trino.sql.tree.Expression;
20-
import io.trino.sql.tree.Node;
21-
import io.trino.sql.tree.NodeLocation;
2219
import io.trino.sql.tree.StringLiteral;
23-
import net.minidev.json.annotate.JsonIgnore;
2420

25-
import java.util.List;
2621
import java.util.Objects;
27-
import java.util.Optional;
2822

2923
import static java.lang.Class.forName;
3024

3125
public class SerializableExpression
32-
extends Expression
3326
{
3427
private final String value;
3528
private final Class originalClass;
3629

3730
@JsonCreator
3831
public SerializableExpression(@JsonProperty("value") String value, @JsonProperty("originalClass") String originalClass)
3932
{
40-
this(Optional.empty(), value, originalClass);
41-
}
42-
43-
protected SerializableExpression(Optional<NodeLocation> location, String value, String originalClass)
44-
{
45-
super(location);
4633
this.value = value;
4734
try {
4835
this.originalClass = forName(originalClass);
@@ -54,12 +41,12 @@ protected SerializableExpression(Optional<NodeLocation> location, String value,
5441

5542
public SerializableExpression(Expression expression)
5643
{
57-
super(expression.getLocation());
5844
originalClass = expression.getClass();
5945
if (expression instanceof StringLiteral) {
6046
// special handling for this common case so that quotes do not need to be stripped
6147
value = ((StringLiteral) expression).getValue();
62-
} else {
48+
}
49+
else {
6350
value = expression.toString();
6451
}
6552
}
@@ -76,13 +63,6 @@ public Class getOriginalClass()
7663
return originalClass;
7764
}
7865

79-
@JsonIgnore
80-
@Override
81-
public List<? extends Node> getChildren()
82-
{
83-
return ImmutableList.of();
84-
}
85-
8666
@Override
8767
public int hashCode()
8868
{
@@ -97,10 +77,8 @@ public boolean equals(Object obj)
9777
}
9878
else if (obj != null && this.getClass() == obj.getClass()) {
9979
SerializableExpression that = (SerializableExpression) obj;
100-
return Objects.equals(this.value, that.value) && Objects.equals(this.originalClass, that.originalClass);
101-
}
102-
else {
103-
return false;
80+
return Objects.equals(this.value, that.value) && Objects.equals(this.originalClass, that.originalClass);
10481
}
82+
return false;
10583
}
10684
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public TrinoQueryProperties(
135135
this.catalogSchemas = requireNonNullElse(catalogSchemas, ImmutableSet.of());
136136
this.isNewQuerySubmission = isNewQuerySubmission;
137137
this.procedureArguments = requireNonNullElse(procedureArguments, ImmutableList.of());
138-
this.procedure = requireNonNullElse(procedure.map(this::parseIdentifierStringToQualifiedName), Optional.empty());
138+
this.procedure = procedure.map(this::parseIdentifierStringToQualifiedName);
139139
this.errorMessage = requireNonNullElse(errorMessage, Optional.empty());
140140
isClientsUseV2Format = false;
141141
maxBodySize = -1;
@@ -410,7 +410,8 @@ private QualifiedName qualifyName(QualifiedName name)
410410
{
411411
List<String> nameParts = name.getParts();
412412
return switch (nameParts.size()) {
413-
case 1 -> QualifiedName.of(defaultCatalog.orElseThrow(this::unsetDefaultExceptionSupplier), defaultSchema.orElseThrow(this::unsetDefaultExceptionSupplier), nameParts.getFirst());
413+
case 1 ->
414+
QualifiedName.of(defaultCatalog.orElseThrow(this::unsetDefaultExceptionSupplier), defaultSchema.orElseThrow(this::unsetDefaultExceptionSupplier), nameParts.getFirst());
414415
case 2 -> QualifiedName.of(defaultCatalog.orElseThrow(this::unsetDefaultExceptionSupplier), nameParts.getFirst(), nameParts.get(1));
415416
case 3 -> QualifiedName.of(nameParts.getFirst(), nameParts.get(1), nameParts.get(2));
416417
default -> throw new RequestParsingException("Unexpected qualified name: " + name.getParts());
@@ -664,6 +665,4 @@ public void serialize(Optional<QualifiedName> qualifiedName, JsonGenerator jsonG
664665
jsonGenerator.writeString(qualifiedName.map(QualifiedName::toString).orElse(null));
665666
}
666667
}
667-
668-
669668
}

gateway-ha/src/test/java/io/trino/gateway/ha/TestGatewayHaSingleBackend.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@
3030
import org.junit.jupiter.params.provider.MethodSource;
3131
import org.testcontainers.containers.TrinoContainer;
3232

33-
import java.io.IOException;
3433
import java.util.List;
3534

36-
import static java.util.Locale.ENGLISH;
3735
import static org.assertj.core.api.Assertions.assertThat;
3836
import static org.testcontainers.utility.MountableFile.forClasspathResource;
3937

@@ -64,26 +62,6 @@ void setup()
6462
"trino1", "http://localhost:" + backendPort, "externalUrl", true, "adhoc", routerPort);
6563
}
6664

67-
@ParameterizedTest
68-
@MethodSource("protocols")
69-
void testEmptyBody(Protocol protocol)
70-
throws IOException
71-
{
72-
OkHttpClient httpClient = new OkHttpClient.Builder().protocols(ImmutableList.of(protocol)).build();
73-
74-
RequestBody requestBody = RequestBody.create("", null);
75-
Request request =
76-
new Request.Builder()
77-
.url("http://localhost:" + routerPort + "/v1/statement")
78-
.addHeader("X-Trino-User", "test")
79-
.addHeader("Host", "test.host.com")
80-
.post(requestBody)
81-
.build();
82-
Response response = httpClient.newCall(request).execute();
83-
String responseBody = response.body().string();
84-
assertThat(responseBody.toLowerCase(ENGLISH)).matches(".*statement is empty");
85-
}
86-
8765
@ParameterizedTest
8866
@MethodSource("protocols")
8967
void testRequestDelivery(Protocol protocol)

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,66 @@ void testJsonCreator()
6464
assertThat(deserializedTrinoQueryProperties.isQueryParsingSuccessful()).isEqualTo(trinoQueryProperties.isQueryParsingSuccessful());
6565
assertThat(deserializedTrinoQueryProperties.getErrorMessage()).isEqualTo(trinoQueryProperties.getErrorMessage());
6666
}
67+
68+
@Test
69+
void testJsonCreatorWithEmptyProperties()
70+
{
71+
JsonCodec<TrinoQueryProperties> codec = JsonCodec.jsonCodec(TrinoQueryProperties.class);
72+
TrinoQueryProperties trinoQueryProperties = new TrinoQueryProperties(
73+
"SELECT c1 from c.s.t1",
74+
"SELECT",
75+
"SELECT",
76+
ImmutableList.of(),
77+
Optional.empty(),
78+
Optional.empty(),
79+
ImmutableSet.of(),
80+
ImmutableSet.of(),
81+
ImmutableSet.of(),
82+
Optional.empty(),
83+
ImmutableList.of(),
84+
true,
85+
Optional.empty());
86+
87+
String trinoQueryPropertiesJson = codec.toJson(trinoQueryProperties);
88+
TrinoQueryProperties deserializedTrinoQueryProperties = codec.fromJson(trinoQueryPropertiesJson);
89+
90+
assertThat(deserializedTrinoQueryProperties.getBody()).isEqualTo(trinoQueryProperties.getBody());
91+
assertThat(deserializedTrinoQueryProperties.getQueryType()).isEqualTo(trinoQueryProperties.getQueryType());
92+
assertThat(deserializedTrinoQueryProperties.getResourceGroupQueryType()).isEqualTo(trinoQueryProperties.getResourceGroupQueryType());
93+
assertThat(deserializedTrinoQueryProperties.getTables()).isEqualTo(trinoQueryProperties.getTables());
94+
assertThat(deserializedTrinoQueryProperties.getDefaultCatalog()).isEqualTo(trinoQueryProperties.getDefaultCatalog());
95+
assertThat(deserializedTrinoQueryProperties.getDefaultSchema()).isEqualTo(trinoQueryProperties.getDefaultSchema());
96+
assertThat(deserializedTrinoQueryProperties.getSchemas()).isEqualTo(trinoQueryProperties.getSchemas());
97+
assertThat(deserializedTrinoQueryProperties.getCatalogs()).isEqualTo(trinoQueryProperties.getCatalogs());
98+
assertThat(deserializedTrinoQueryProperties.getCatalogSchemas()).isEqualTo(trinoQueryProperties.getCatalogSchemas());
99+
assertThat(deserializedTrinoQueryProperties.getProcedure()).isEqualTo(trinoQueryProperties.getProcedure());
100+
assertThat(deserializedTrinoQueryProperties.getProcedureArguments()).isEqualTo(trinoQueryProperties.getProcedureArguments());
101+
assertThat(deserializedTrinoQueryProperties.isNewQuerySubmission()).isEqualTo(trinoQueryProperties.isNewQuerySubmission());
102+
assertThat(deserializedTrinoQueryProperties.isQueryParsingSuccessful()).isEqualTo(trinoQueryProperties.isQueryParsingSuccessful());
103+
assertThat(deserializedTrinoQueryProperties.getErrorMessage()).isEqualTo(trinoQueryProperties.getErrorMessage());
104+
}
105+
106+
@Test
107+
void testJsonMissingFields()
108+
{
109+
JsonCodec<TrinoQueryProperties> codec = JsonCodec.jsonCodec(TrinoQueryProperties.class);
110+
111+
String emptyJson = "{}";
112+
TrinoQueryProperties deserializedTrinoQueryProperties = codec.fromJson(emptyJson);
113+
114+
assertThat(deserializedTrinoQueryProperties.getBody()).isEqualTo("");
115+
assertThat(deserializedTrinoQueryProperties.getQueryType()).isEqualTo("");
116+
assertThat(deserializedTrinoQueryProperties.getResourceGroupQueryType()).isEqualTo(null);
117+
assertThat(deserializedTrinoQueryProperties.getTables()).isEqualTo(ImmutableSet.of());
118+
assertThat(deserializedTrinoQueryProperties.getDefaultCatalog()).isEqualTo(Optional.empty());
119+
assertThat(deserializedTrinoQueryProperties.getDefaultSchema()).isEqualTo(Optional.empty());
120+
assertThat(deserializedTrinoQueryProperties.getSchemas()).isEqualTo(ImmutableSet.of());
121+
assertThat(deserializedTrinoQueryProperties.getCatalogs()).isEqualTo(ImmutableSet.of());
122+
assertThat(deserializedTrinoQueryProperties.getCatalogSchemas()).isEqualTo(ImmutableSet.of());
123+
assertThat(deserializedTrinoQueryProperties.getProcedure()).isEqualTo(Optional.empty());
124+
assertThat(deserializedTrinoQueryProperties.getProcedureArguments()).isEqualTo(ImmutableList.of());
125+
assertThat(deserializedTrinoQueryProperties.isNewQuerySubmission()).isEqualTo(false);
126+
assertThat(deserializedTrinoQueryProperties.isQueryParsingSuccessful()).isEqualTo(true);
127+
assertThat(deserializedTrinoQueryProperties.getErrorMessage()).isEqualTo(Optional.empty());
128+
}
67129
}

0 commit comments

Comments
 (0)