Skip to content

Commit

Permalink
feat: Add support for new functions (#2287)
Browse files Browse the repository at this point in the history
* feat: Add support for additional types

* fix build

* fix test

* fix build

* improve readability

* fix a whoopsie

* improve readability

* improve readability

* Update clirr-ignored-differences.xml

* Update clirr-ignored-differences.xml

* Update clirr-ignored-differences.xml

* Update clirr-ignored-differences.xml
  • Loading branch information
ron-gal authored and lqiu96 committed Jul 26, 2024
1 parent 07f4031 commit 0d1e394
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 21 deletions.
39 changes: 39 additions & 0 deletions google-cloud-bigtable/clirr-ignored-differences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,43 @@
<className>com/google/cloud/bigtable/data/v2/models/ChangeStreamRecordAdapter$ChangeStreamRecordBuilder</className>
<method>*</method>
</difference>
<!-- BetaApi was updated -->
<difference>
<differenceType>2000</differenceType>
<className>com/google/cloud/bigtable/admin/v2/models/Type</className>
</difference>
<difference>
<differenceType>2000</differenceType>
<className>com/google/cloud/bigtable/admin/v2/models/Type$SumAggregateInput</className>
</difference>
<difference>
<differenceType>5001</differenceType>
<className>com/google/cloud/bigtable/admin/v2/models/Type$SumAggregateInput</className>
<to>com/google/cloud/bigtable/admin/v2/models/Type</to>
</difference>
<difference>
<differenceType>5001</differenceType>
<className>com/google/cloud/bigtable/admin/v2/models/Type$Aggregate</className>
<to>com/google/cloud/bigtable/admin/v2/models/Type</to>
</difference>
<difference>
<differenceType>5001</differenceType>
<className>com/google/cloud/bigtable/admin/v2/models/Type$Bytes</className>
<to>com/google/cloud/bigtable/admin/v2/models/Type</to>
</difference>
<difference>
<differenceType>5001</differenceType>
<className>com/google/cloud/bigtable/admin/v2/models/Type$Int64</className>
<to>com/google/cloud/bigtable/admin/v2/models/Type</to>
</difference>
<difference>
<differenceType>5001</differenceType>
<className>com/google/cloud/bigtable/admin/v2/models/Type$Int64</className>
<to>com/google/cloud/bigtable/admin/v2/models/Type$SumAggregateInput</to>
</difference>
<difference>
<differenceType>5001</differenceType>
<className>com/google/cloud/bigtable/admin/v2/models/Type$Raw</className>
<to>com/google/cloud/bigtable/admin/v2/models/Type</to>
</difference>
</differences>
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@
* @see com.google.bigtable.admin.v2.Type
*/
@BetaApi
public abstract class Type {
private Type() {}

public interface Type {
/**
* This type is a marker type that allows types to be used as the input to the SUM aggregate
* function.
* These types are marker types that allow types to be used as the input to aggregate function.
*/
public abstract static class SumAggregateInput extends Type {}
public static interface SumAggregateInput extends Type {}

public static interface MinAggregateInput extends Type {}

public static interface MaxAggregateInput extends Type {}

abstract com.google.bigtable.admin.v2.Type toProto();
public static interface HllAggregateInput extends Type {}

com.google.bigtable.admin.v2.Type toProto();

static Type fromProto(com.google.bigtable.admin.v2.Type source) {
switch (source.getKindCase()) {
Expand Down Expand Up @@ -73,7 +76,7 @@ public static Bytes bytes(Bytes.Encoding encoding) {
* Creates an Int64 type with a big-endian encoding. The bytes are then encoded in "raw" format.
*/
public static Int64 bigEndianInt64() {
return Int64.create(Int64.Encoding.BigEndianBytes.create(Bytes.rawBytes()));
return Int64.create(Int64.Encoding.BigEndianBytes.create(Type.rawBytes()));
}

/** Creates an Int64 type with the specified encoding. */
Expand All @@ -91,9 +94,39 @@ public static Aggregate sum(SumAggregateInput inputType) {
return Aggregate.create(inputType, Aggregate.Aggregator.Sum.create());
}

/** Creates an Aggregate type with a MIN aggregator and Int64 input type. */
public static Aggregate int64Min() {
return min(bigEndianInt64());
}

/** Creates an Aggregate type with a MIN aggregator and specified input type. */
public static Aggregate min(MinAggregateInput inputType) {
return Aggregate.create(inputType, Aggregate.Aggregator.Min.create());
}

/** Creates an Aggregate type with a MAX aggregator and Int64 input type. */
public static Aggregate int64Max() {
return max(bigEndianInt64());
}

/** Creates an Aggregate type with a MAX aggregator and specified input type. */
public static Aggregate max(MaxAggregateInput inputType) {
return Aggregate.create(inputType, Aggregate.Aggregator.Max.create());
}

/** Creates an Aggregate type with a HLL aggregator and Int64 input type. */
public static Aggregate int64Hll() {
return hll(bigEndianInt64());
}

/** Creates an Aggregate type with a HLL aggregator and specified input type. */
public static Aggregate hll(HllAggregateInput inputType) {
return Aggregate.create(inputType, Aggregate.Aggregator.Hll.create());
}

/** Represents a string of bytes with a specific encoding. */
@AutoValue
public abstract static class Bytes extends Type {
public abstract static class Bytes implements Type {
public static Bytes create(Encoding encoding) {
return new AutoValue_Type_Bytes(encoding);
}
Expand All @@ -102,7 +135,7 @@ public static Bytes create(Encoding encoding) {
public abstract Encoding getEncoding();

@Override
com.google.bigtable.admin.v2.Type toProto() {
public com.google.bigtable.admin.v2.Type toProto() {
com.google.bigtable.admin.v2.Type.Builder builder =
com.google.bigtable.admin.v2.Type.newBuilder();
builder.getBytesTypeBuilder().setEncoding(getEncoding().toProto());
Expand Down Expand Up @@ -142,7 +175,7 @@ public static Raw create() {
.build();

@Override
com.google.bigtable.admin.v2.Type.Bytes.Encoding toProto() {
public com.google.bigtable.admin.v2.Type.Bytes.Encoding toProto() {
return PROTO_INSTANCE;
}
}
Expand All @@ -151,7 +184,8 @@ com.google.bigtable.admin.v2.Type.Bytes.Encoding toProto() {

/** Represents a 64-bit integer with a specific encoding. */
@AutoValue
public abstract static class Int64 extends SumAggregateInput {
public abstract static class Int64
implements SumAggregateInput, MinAggregateInput, MaxAggregateInput, HllAggregateInput {
public static Int64 create(Encoding encoding) {
return new AutoValue_Type_Int64(encoding);
}
Expand All @@ -169,7 +203,7 @@ static Encoding fromProto(com.google.bigtable.admin.v2.Type.Int64.Encoding sourc
return BigEndianBytes.create(
Bytes.fromProto(source.getBigEndianBytes().getBytesType()));
case ENCODING_NOT_SET:
return BigEndianBytes.create(Bytes.rawBytes());
return BigEndianBytes.create(Type.rawBytes());
}
throw new UnsupportedOperationException();
}
Expand All @@ -185,7 +219,7 @@ public static BigEndianBytes create(Bytes bytes) {
public abstract Bytes getBytes();

@Override
com.google.bigtable.admin.v2.Type.Int64.Encoding toProto() {
public com.google.bigtable.admin.v2.Type.Int64.Encoding toProto() {
com.google.bigtable.admin.v2.Type.Int64.Encoding.Builder builder =
com.google.bigtable.admin.v2.Type.Int64.Encoding.newBuilder();
builder.getBigEndianBytesBuilder().setBytesType(getBytes().toProto().getBytesType());
Expand All @@ -195,7 +229,7 @@ com.google.bigtable.admin.v2.Type.Int64.Encoding toProto() {
}

@Override
com.google.bigtable.admin.v2.Type toProto() {
public com.google.bigtable.admin.v2.Type toProto() {
com.google.bigtable.admin.v2.Type.Builder builder =
com.google.bigtable.admin.v2.Type.newBuilder();
builder.getInt64TypeBuilder().setEncoding(getEncoding().toProto());
Expand All @@ -208,13 +242,13 @@ static Int64 fromProto(com.google.bigtable.admin.v2.Type.Int64 source) {
}

@AutoValue
public abstract static class Raw extends Type {
public abstract static class Raw implements Type {
public static Raw create() {
return new AutoValue_Type_Raw();
}

@Override
com.google.bigtable.admin.v2.Type toProto() {
public com.google.bigtable.admin.v2.Type toProto() {
return com.google.bigtable.admin.v2.Type.getDefaultInstance();
}
}
Expand All @@ -226,7 +260,7 @@ com.google.bigtable.admin.v2.Type toProto() {
* the `input_type` or `state_type`, and reads will always return the `state_type` .
*/
@AutoValue
public abstract static class Aggregate extends Type {
public abstract static class Aggregate implements Type {
public static Aggregate create(Type inputType, Aggregator aggregator) {
return new AutoValue_Type_Aggregate(inputType, aggregator);
}
Expand All @@ -250,11 +284,49 @@ void buildTo(com.google.bigtable.admin.v2.Type.Aggregate.Builder builder) {
}
}

@AutoValue
public abstract static class Min extends Aggregator {
public static Min create() {
return new AutoValue_Type_Aggregate_Aggregator_Min();
}

@Override
void buildTo(com.google.bigtable.admin.v2.Type.Aggregate.Builder builder) {
builder.setMin(com.google.bigtable.admin.v2.Type.Aggregate.Min.getDefaultInstance());
}
}

@AutoValue
public abstract static class Max extends Aggregator {
public static Max create() {
return new AutoValue_Type_Aggregate_Aggregator_Max();
}

@Override
void buildTo(com.google.bigtable.admin.v2.Type.Aggregate.Builder builder) {
builder.setMax(com.google.bigtable.admin.v2.Type.Aggregate.Max.getDefaultInstance());
}
}

@AutoValue
public abstract static class Hll extends Aggregator {
public static Hll create() {
return new AutoValue_Type_Aggregate_Aggregator_Hll();
}

@Override
void buildTo(com.google.bigtable.admin.v2.Type.Aggregate.Builder builder) {
builder.setHllppUniqueCount(
com.google.bigtable.admin.v2.Type.Aggregate.HyperLogLogPlusPlusUniqueCount
.getDefaultInstance());
}
}

abstract void buildTo(com.google.bigtable.admin.v2.Type.Aggregate.Builder builder);
}

@Override
com.google.bigtable.admin.v2.Type toProto() {
public com.google.bigtable.admin.v2.Type toProto() {
com.google.bigtable.admin.v2.Type.Builder typeBuilder =
com.google.bigtable.admin.v2.Type.newBuilder();
com.google.bigtable.admin.v2.Type.Aggregate.Builder aggregateBuilder =
Expand All @@ -271,6 +343,15 @@ static Aggregate fromProto(com.google.bigtable.admin.v2.Type.Aggregate source) {
case SUM:
aggregator = Aggregator.Sum.create();
break;
case MIN:
aggregator = Aggregator.Min.create();
break;
case MAX:
aggregator = Aggregator.Max.create();
break;
case HLLPP_UNIQUE_COUNT:
aggregator = Aggregator.Hll.create();
break;
case AGGREGATOR_NOT_SET:
throw new UnsupportedOperationException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,24 @@ public void testCreateTable() {
ColumnFamily.newBuilder()
.setGcRule(GcRule.getDefaultInstance())
.setValueType(TypeProtos.intSumType())
.build())
.putColumnFamilies(
"cf2",
ColumnFamily.newBuilder()
.setGcRule(GcRule.getDefaultInstance())
.setValueType(TypeProtos.intMinType())
.build())
.putColumnFamilies(
"cf3",
ColumnFamily.newBuilder()
.setGcRule(GcRule.getDefaultInstance())
.setValueType(TypeProtos.intMaxType())
.build())
.putColumnFamilies(
"cf4",
ColumnFamily.newBuilder()
.setGcRule(GcRule.getDefaultInstance())
.setValueType(TypeProtos.intHllType())
.build()))
.build();

Expand All @@ -267,7 +285,12 @@ public void testCreateTable() {

// Execute
Table result =
adminClient.createTable(CreateTableRequest.of(TABLE_ID).addFamily("cf1", Type.int64Sum()));
adminClient.createTable(
CreateTableRequest.of(TABLE_ID)
.addFamily("cf1", Type.int64Sum())
.addFamily("cf2", Type.int64Min())
.addFamily("cf3", Type.int64Max())
.addFamily("cf4", Type.int64Hll()));

// Verify
assertThat(result).isEqualTo(Table.fromProto(expectedResponse));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,33 @@ public static com.google.bigtable.admin.v2.Type intSumType() {
.setSum(com.google.bigtable.admin.v2.Type.Aggregate.Sum.getDefaultInstance()))
.build();
}

public static com.google.bigtable.admin.v2.Type intMinType() {
return com.google.bigtable.admin.v2.Type.newBuilder()
.setAggregateType(
com.google.bigtable.admin.v2.Type.Aggregate.newBuilder()
.setInputType(TypeProtos.int64Type())
.setMin(com.google.bigtable.admin.v2.Type.Aggregate.Min.getDefaultInstance()))
.build();
}

public static com.google.bigtable.admin.v2.Type intMaxType() {
return com.google.bigtable.admin.v2.Type.newBuilder()
.setAggregateType(
com.google.bigtable.admin.v2.Type.Aggregate.newBuilder()
.setInputType(TypeProtos.int64Type())
.setMax(com.google.bigtable.admin.v2.Type.Aggregate.Max.getDefaultInstance()))
.build();
}

public static com.google.bigtable.admin.v2.Type intHllType() {
return com.google.bigtable.admin.v2.Type.newBuilder()
.setAggregateType(
com.google.bigtable.admin.v2.Type.Aggregate.newBuilder()
.setInputType(TypeProtos.int64Type())
.setHllppUniqueCount(
com.google.bigtable.admin.v2.Type.Aggregate.HyperLogLogPlusPlusUniqueCount
.getDefaultInstance()))
.build();
}
}
Loading

0 comments on commit 0d1e394

Please sign in to comment.