-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Sum and Avg aggregation feature (#1067)
* creating sum aggregation * creating avg aggregation * refactoring code to configure alias into an aggregation * Add method in aggregation query builders to accept aggregation in var args form * refactoring equals implementation * changed visibility from public to protected * Made aggregation result capable of returning double values * clirr ignore new method * Made transformer capable of parsing double values * mock webserver simulating sum and avg aggregation response * integration tests for sum and avg * Marking sum and avg methods with @BetaApi annotation * incorporating feedbacks * fixing lint * refactoring dispatcher code * incorporating feedbacks * cleaing up proxy code, as sum/avg now can be run against nightly * remove deprecated annotation * tests for sum and avg aggregations in GQL query * removing the addaggregation variant accepting list of aggregations * fix lint failures * removed BetaApi annotation from sum and avg aggregation * adding doc to AggregationResult#getDouble * adding sum/avg aggreggation test with autogenerated alias * adding a test to run sum and avg aggregation together * testing sum and avg aggregations with transactions * type check before returning the result from aggregation result class
- Loading branch information
Showing
16 changed files
with
984 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...-cloud-datastore/src/main/java/com/google/cloud/datastore/aggregation/AvgAggregation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.datastore.aggregation; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
import com.google.api.core.InternalApi; | ||
import com.google.datastore.v1.AggregationQuery; | ||
import com.google.datastore.v1.AggregationQuery.Aggregation.Avg; | ||
import com.google.datastore.v1.PropertyReference; | ||
import java.util.Objects; | ||
|
||
/** Represents an {@link Aggregation} which returns average of numerical values. */ | ||
public class AvgAggregation extends Aggregation { | ||
|
||
private final String propertyReference; | ||
|
||
public AvgAggregation(String alias, String propertyReference) { | ||
super(alias); | ||
checkArgument(propertyReference != null, "Property reference can't be null"); | ||
this.propertyReference = propertyReference; | ||
} | ||
|
||
@InternalApi | ||
@Override | ||
public AggregationQuery.Aggregation toPb() { | ||
PropertyReference reference = | ||
PropertyReference.newBuilder().setName(this.propertyReference).build(); | ||
Avg avg = Avg.newBuilder().setProperty(reference).build(); | ||
return aggregationBuilder().setAvg(avg).build(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
AvgAggregation that = (AvgAggregation) o; | ||
return Objects.equals(this.propertyReference, that.propertyReference) | ||
&& Objects.equals(getAlias(), that.getAlias()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getAlias(), this.propertyReference); | ||
} | ||
|
||
/** A builder class to create and customize a {@link AvgAggregation}. */ | ||
public static class Builder implements AggregationBuilder<AvgAggregation> { | ||
|
||
private String alias; | ||
private String propertyReference; | ||
|
||
public AvgAggregation.Builder propertyReference(String propertyReference) { | ||
this.propertyReference = propertyReference; | ||
return this; | ||
} | ||
|
||
public AvgAggregation.Builder as(String alias) { | ||
this.alias = alias; | ||
return this; | ||
} | ||
|
||
@Override | ||
public AvgAggregation build() { | ||
return new AvgAggregation(alias, propertyReference); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...-cloud-datastore/src/main/java/com/google/cloud/datastore/aggregation/SumAggregation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.datastore.aggregation; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
import com.google.api.core.InternalApi; | ||
import com.google.datastore.v1.AggregationQuery; | ||
import com.google.datastore.v1.AggregationQuery.Aggregation.Sum; | ||
import com.google.datastore.v1.PropertyReference; | ||
import java.util.Objects; | ||
|
||
/** Represents an {@link Aggregation} which returns sum of numerical values. */ | ||
public class SumAggregation extends Aggregation { | ||
|
||
private final String propertyReference; | ||
|
||
public SumAggregation(String alias, String propertyReference) { | ||
super(alias); | ||
checkArgument(propertyReference != null, "Property reference can't be null"); | ||
this.propertyReference = propertyReference; | ||
} | ||
|
||
@InternalApi | ||
@Override | ||
public AggregationQuery.Aggregation toPb() { | ||
PropertyReference reference = | ||
PropertyReference.newBuilder().setName(this.propertyReference).build(); | ||
Sum sum = Sum.newBuilder().setProperty(reference).build(); | ||
return aggregationBuilder().setSum(sum).build(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
SumAggregation that = (SumAggregation) o; | ||
return Objects.equals(this.propertyReference, that.propertyReference) | ||
&& Objects.equals(getAlias(), that.getAlias()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getAlias(), this.propertyReference); | ||
} | ||
|
||
/** A builder class to create and customize a {@link SumAggregation}. */ | ||
public static class Builder implements AggregationBuilder<SumAggregation> { | ||
|
||
private String alias; | ||
private String propertyReference; | ||
|
||
public SumAggregation.Builder propertyReference(String propertyReference) { | ||
this.propertyReference = propertyReference; | ||
return this; | ||
} | ||
|
||
public SumAggregation.Builder as(String alias) { | ||
this.alias = alias; | ||
return this; | ||
} | ||
|
||
@Override | ||
public SumAggregation build() { | ||
return new SumAggregation(alias, propertyReference); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.