Skip to content

Commit 6a8b2b3

Browse files
committed
fix(bigquery): implement explicit equals in QueryRequestInfo
Implement explicit equals method in QueryRequestInfo to ensure it matches hashCode (which includes jobTimeoutMs) and avoid violating the equals/hashCode contract. Also added unit tests for equals and hashCode. TAG=agy CONV=2773a7e8-a0fe-4d6f-89ce-762ad24aa78d
1 parent 7dab124 commit 6a8b2b3

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/QueryRequestInfo.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,28 @@ public int hashCode() {
191191

192192
@Override
193193
public boolean equals(Object obj) {
194-
return obj == this
195-
|| obj != null
196-
&& obj.getClass().equals(QueryRequestInfo.class)
197-
&& java.util.Objects.equals(toPb(), ((QueryRequestInfo) obj).toPb());
194+
if (obj == this) {
195+
return true;
196+
}
197+
if (obj == null || !obj.getClass().equals(QueryRequestInfo.class)) {
198+
return false;
199+
}
200+
QueryRequestInfo other = (QueryRequestInfo) obj;
201+
return java.util.Objects.equals(connectionProperties, other.connectionProperties)
202+
&& java.util.Objects.equals(defaultDataset, other.defaultDataset)
203+
&& java.util.Objects.equals(dryRun, other.dryRun)
204+
&& java.util.Objects.equals(labels, other.labels)
205+
&& java.util.Objects.equals(maximumBytesBilled, other.maximumBytesBilled)
206+
&& java.util.Objects.equals(maxResults, other.maxResults)
207+
&& java.util.Objects.equals(query, other.query)
208+
&& java.util.Objects.equals(queryParameters, other.queryParameters)
209+
&& java.util.Objects.equals(requestId, other.requestId)
210+
&& java.util.Objects.equals(createSession, other.createSession)
211+
&& java.util.Objects.equals(useQueryCache, other.useQueryCache)
212+
&& java.util.Objects.equals(useLegacySql, other.useLegacySql)
213+
&& java.util.Objects.equals(jobCreationMode, other.jobCreationMode)
214+
&& java.util.Objects.equals(formatOptions, other.formatOptions)
215+
&& java.util.Objects.equals(reservation, other.reservation)
216+
&& java.util.Objects.equals(jobTimeoutMs, other.jobTimeoutMs);
198217
}
199218
}

java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryRequestInfoTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,25 @@ public void testToPb() {
180180
assertEquals(TIMEOUT, requestWithTimeoutPb.getJobTimeoutMs());
181181
}
182182

183+
@Test
184+
public void testEqualsAndHashCode() {
185+
QueryRequestInfo info1 =
186+
new QueryRequestInfo(
187+
QUERY_JOB_CONFIGURATION_SUPPORTED, DataFormatOptions.newBuilder().build());
188+
QueryRequestInfo info2 =
189+
new QueryRequestInfo(
190+
QUERY_JOB_CONFIGURATION_SUPPORTED, DataFormatOptions.newBuilder().build());
191+
192+
assertNotEquals(info1, info2);
193+
assertNotEquals(info1.hashCode(), info2.hashCode());
194+
195+
assertEquals(info1, info1);
196+
assertEquals(info1.hashCode(), info1.hashCode());
197+
198+
assertNotEquals(info1, null);
199+
assertNotEquals(info1, "different type");
200+
}
201+
183202
@Test
184203
public void equalTo() {
185204
compareQueryRequestInfo(

0 commit comments

Comments
 (0)