Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for new functions #2287

Merged
merged 13 commits into from
Jul 24, 2024
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ public void modifyFamilies() {
.addFamily("cf3")
.addFamily("cf4", Type.int64Sum())
.addFamily("cf5", GCRules.GCRULES.maxVersions(1), Type.int64Sum())
.addFamily("cf6", Type.int64Min())
.addFamily("cf7", GCRules.GCRULES.maxVersions(1), Type.int64Min())
.addFamily("cf8", Type.int64Max())
.addFamily("cf9", GCRules.GCRULES.maxVersions(1), Type.int64Max())
.addFamily("cf10", Type.int64Hll())
.addFamily("cf11", GCRules.GCRULES.maxVersions(1), Type.int64Hll())
.updateFamily("cf1", GCRules.GCRULES.maxVersions(5))
.dropFamily("cf3")
.toProto(PROJECT_ID, INSTANCE_ID);
Expand Down Expand Up @@ -119,6 +125,48 @@ public void modifyFamilies() {
com.google.bigtable.admin.v2.ColumnFamily.newBuilder()
.setGcRule(GCRules.GCRULES.maxVersions(1).toProto())
.setValueType(Type.int64Sum().toProto())))
.addModifications(
com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification.newBuilder()
.setId("cf6")
.setCreate(
com.google.bigtable.admin.v2.ColumnFamily.newBuilder()
.setGcRule(GcRule.getDefaultInstance())
.setValueType(Type.int64Min().toProto())))
.addModifications(
com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification.newBuilder()
.setId("cf7")
.setCreate(
com.google.bigtable.admin.v2.ColumnFamily.newBuilder()
.setGcRule(GCRules.GCRULES.maxVersions(1).toProto())
.setValueType(Type.int64Min().toProto())))
.addModifications(
com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification.newBuilder()
.setId("cf8")
.setCreate(
com.google.bigtable.admin.v2.ColumnFamily.newBuilder()
.setGcRule(GcRule.getDefaultInstance())
.setValueType(Type.int64Max().toProto())))
.addModifications(
com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification.newBuilder()
.setId("cf9")
.setCreate(
com.google.bigtable.admin.v2.ColumnFamily.newBuilder()
.setGcRule(GCRules.GCRULES.maxVersions(1).toProto())
.setValueType(Type.int64Max().toProto())))
.addModifications(
com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification.newBuilder()
.setId("cf10")
.setCreate(
com.google.bigtable.admin.v2.ColumnFamily.newBuilder()
.setGcRule(GcRule.getDefaultInstance())
.setValueType(Type.int64Hll().toProto())))
.addModifications(
com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification.newBuilder()
.setId("cf11")
.setCreate(
com.google.bigtable.admin.v2.ColumnFamily.newBuilder()
.setGcRule(GCRules.GCRULES.maxVersions(1).toProto())
.setValueType(Type.int64Hll().toProto())))
.addModifications(
com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification.newBuilder()
.setId("cf1")
Expand Down
Loading
Loading