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

[DO NOT MERGE | POC] feat: Adding support for UUID type #3236

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.BitSet;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.Objects;
import java.util.function.Function;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -415,6 +416,11 @@ protected Date getDateInternal(int columnIndex) {
return currRow().getDateInternal(columnIndex);
}

@Override
protected UUID getUuidInternal(int columnIndex) {
return currRow().getUuidInternal(columnIndex);
}

@Override
protected Value getValueInternal(int columnIndex) {
return currRow().getValueInternal(columnIndex);
Expand Down Expand Up @@ -507,6 +513,11 @@ protected List<Date> getDateListInternal(int columnIndex) {
return currRow().getDateListInternal(columnIndex);
}

@Override
protected List<UUID> getUuidListInternal(int columnIndex) {
return currRow().getUuidListInternal(columnIndex);
}

@Override
protected List<Struct> getStructListInternal(int columnIndex) {
return currRow().getStructListInternal(columnIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.cloud.ByteArray;
import com.google.cloud.Date;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.Type.StructField;
import com.google.cloud.spanner.Type.Code;
import com.google.protobuf.AbstractMessage;
import com.google.protobuf.ProtocolMessageEnum;
Expand All @@ -29,6 +30,7 @@
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.UUID;

/**
* Base class for assisting {@link StructReader} implementations.
Expand Down Expand Up @@ -67,6 +69,8 @@ protected String getPgJsonbInternal(int columnIndex) {

protected abstract Date getDateInternal(int columnIndex);

protected abstract UUID getUUIDInternal(int columnIndex);

protected <T extends AbstractMessage> T getProtoMessageInternal(int columnIndex, T message) {
throw new UnsupportedOperationException("Not implemented");
}
Expand Down Expand Up @@ -128,6 +132,8 @@ protected List<String> getPgJsonbListInternal(int columnIndex) {

protected abstract List<Date> getDateListInternal(int columnIndex);

protected abstract List<UUID> getUUIDListInternal(int columnIndex);

protected abstract List<Struct> getStructListInternal(int columnIndex);

@Override
Expand Down Expand Up @@ -299,6 +305,19 @@ public Date getDate(String columnName) {
return getDateInternal(columnIndex);
}

@Override
public UUID getUUID(int columnIndex) {
checkNonNullOfType(columnIndex, Type.uuid(), columnIndex);
return getUUIDInternal(columnIndex);
}

@Override
public UUID getUUID(String columnName) {
int columnIndex = getColumnIndex(columnName);
checkNonNullOfType(columnIndex, Type.uuid(), columnName);
return getUUIDInternal(columnIndex);
}

@Override
public <T extends ProtocolMessageEnum> T getProtoEnum(
int columnIndex, Function<Integer, ProtocolMessageEnum> method) {
Expand Down Expand Up @@ -583,6 +602,19 @@ public List<Date> getDateList(String columnName) {
return getDateListInternal(columnIndex);
}

@Override
public List<UUID> getUUIDList(int columnIndex) {
checkNonNullOfType(columnIndex, Type.array(Type.uuid()), columnIndex);
return getUUIDListInternal(columnIndex);
}

@Override
public List<UUID> getUUIDList(String columnName) {
int columnIndex = getColumnIndex(columnName);
checkNonNullOfType(columnIndex, Type.array(Type.uuid()), columnName);
return getUUIDListInternal(columnIndex);
}

@Override
public List<Struct> getStructList(int columnIndex) {
checkNonNullArrayOfStruct(columnIndex, columnIndex);
Expand All @@ -600,7 +632,7 @@ public List<Struct> getStructList(String columnName) {
public int getColumnIndex(String columnName) {
// Use the Type instance for field name lookup. Type instances are naturally shared by the
// ResultSet, all Structs corresponding to rows in the read, and all Structs corresponding to
// the values of ARRAY<STRUCT<...>> columns in the read, so this is the best location to
// the values of ARRAY<STRUCT<...>> coreturn new Type.Builder(Code.STRUCT).add(new StructField("STRUCT_KEY", Type.int64())).build();lumns in the read, so this is the best location to
// amortize lookup costs.
return getType().getFieldIndex(columnName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.protobuf.AbstractMessage;
import com.google.protobuf.ProtocolMessageEnum;
import java.math.BigDecimal;
import java.util.UUID;
import java.util.List;
import java.util.function.Function;

Expand Down Expand Up @@ -231,6 +232,20 @@ public Date getDate(String columnName) {
return delegate.get().getDate(columnName);
}

@Override
public UUID getUUID(int columnIndex) {
checkValidState();
return delegate.get().getUUID(columnIndex);
}

@Override
public UUID getUUID(String columnName) {
checkValidState();
return delegate.get().getUUID(columnName);
}



@Override
public boolean[] getBooleanArray(int columnIndex) {
checkValidState();
Expand Down Expand Up @@ -409,6 +424,20 @@ public List<Date> getDateList(String columnName) {
return delegate.get().getDateList(columnName);
}

@Override
public List<UUID> getUUIDList(int columnIndex) {
checkValidState();
return delegate.get().getUUIDList(columnIndex);
}

@Override
public List<UUID> getUUIDList(String columnName) {
checkValidState();
return delegate.get().getUUIDList(columnName);
}



@Override
public <T extends AbstractMessage> List<T> getProtoMessageList(int columnIndex, T message) {
checkValidState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ private Object writeReplace() {
case PG_NUMERIC:
builder.set(fieldName).to((String) value);
break;
case UUID:
builder.set(fieldName).to((String) value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this value is from backend as string. Should we be using fromString to convert string value to java.util.UUID value? I don't think this is the right place to do this conversion. May be when customer reads it as UUID then we should use the fromString method.
https://docs.oracle.com/javase/8/docs/api/java/util/UUID.html

break;
case STRING:
builder.set(fieldName).to((String) value);
break;
Expand Down Expand Up @@ -152,6 +155,9 @@ private Object writeReplace() {
case PG_NUMERIC:
builder.set(fieldName).toPgNumericArray((Iterable<String>) value);
break;
case UUID:
builder.set(fieldName).toUuidArray((Iterable<String>) value);
break;
case STRING:
builder.set(fieldName).toStringArray((Iterable<String>) value);
break;
Expand Down Expand Up @@ -281,6 +287,7 @@ private static Object decodeValue(Type fieldType, com.google.protobuf.Value prot
return new BigDecimal(proto.getStringValue());
case PG_NUMERIC:
case STRING:
case UUID:
case JSON:
case PG_JSONB:
checkType(fieldType, proto, KindCase.STRING_VALUE);
Expand Down Expand Up @@ -339,6 +346,7 @@ static Object decodeArrayValue(Type elementType, ListValue listValue) {
case NUMERIC:
case PG_NUMERIC:
case STRING:
case UUID:
case JSON:
case PG_JSONB:
case BYTES:
Expand Down Expand Up @@ -456,6 +464,12 @@ protected String getStringInternal(int columnIndex) {
return (String) rowData.get(columnIndex);
}

@Override
protected String getUuidInternal(int columnIndex) {
ensureDecoded(columnIndex);
return (String) rowData.get(columnIndex);
}

@Override
protected String getJsonInternal(int columnIndex) {
ensureDecoded(columnIndex);
Expand Down Expand Up @@ -567,6 +581,8 @@ protected Value getValueInternal(int columnIndex) {
return Value.float32(isNull ? null : getFloatInternal(columnIndex));
case STRING:
return Value.string(isNull ? null : getStringInternal(columnIndex));
case UUID:
return Value.uuid(isNull ? null : getUuidInternal(columnIndex));
case JSON:
return Value.json(isNull ? null : getJsonInternal(columnIndex));
case PG_JSONB:
Expand Down Expand Up @@ -604,6 +620,8 @@ protected Value getValueInternal(int columnIndex) {
return Value.float32Array(isNull ? null : getFloatListInternal(columnIndex));
case STRING:
return Value.stringArray(isNull ? null : getStringListInternal(columnIndex));
case UUID:
return Value.uuidArray(isNull ? null : getStringListInternal(columnIndex));
case JSON:
return Value.jsonArray(isNull ? null : getJsonListInternal(columnIndex));
case PG_JSONB:
Expand Down Expand Up @@ -712,6 +730,13 @@ protected List<String> getStringListInternal(int columnIndex) {
return Collections.unmodifiableList((List<String>) rowData.get(columnIndex));
}

@Override
@SuppressWarnings("unchecked") // We know ARRAY<UUID> produces a List<String>.
protected List<String> getUuidListInternal(int columnIndex) {
ensureDecoded(columnIndex);
return Collections.unmodifiableList((List<String>) rowData.get(columnIndex));
}

@Override
@SuppressWarnings("unchecked") // We know ARRAY<JSON> produces a List<String>.
protected List<String> getJsonListInternal(int columnIndex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ private boolean areValuesEqual(List<Value> values, List<Value> otherValues) {
}

private boolean isNaN(Value value) {
return !value.isNull() && (isFloat64NaN(value) || isFloat32NaN(value));
return !value.isNull() && (isFloat64NaN(value));
}

// Checks if the Float64 value is either a "Double" or a "Float" NaN.
Expand All @@ -396,12 +396,6 @@ private boolean isFloat64NaN(Value value) {
return value.getType().equals(Type.float64()) && Double.isNaN(value.getFloat64());
}

// Checks if the Float32 value is either a "Double" or a "Float" NaN.
// Refer the comment above `areValuesEqual` for more details.
private boolean isFloat32NaN(Value value) {
return value.getType().equals(Type.float32()) && Float.isNaN(value.getFloat32());
}

static void toProto(Iterable<Mutation> mutations, List<com.google.spanner.v1.Mutation> out) {
Mutation last = null;
// The mutation currently being built.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.cloud.Date;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.Options.QueryOption;
import com.google.cloud.spanner.Type.StructField;
import com.google.cloud.spanner.Type.Code;
import com.google.cloud.spanner.Type.StructField;
import com.google.common.base.Preconditions;
Expand All @@ -35,6 +36,7 @@
import com.google.spanner.v1.ResultSetStats;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;

/** Utility methods for working with {@link com.google.cloud.spanner.ResultSet}. */
Expand Down Expand Up @@ -488,6 +490,26 @@ public List<ByteArray> getBytesList(String columnName) {
return getCurrentRowAsStruct().getBytesList(columnName);
}

@Override
public UUID getUUID(int columnIndex) {
return getCurrentRowAsStruct().getUUID(columnIndex);
}

@Override
public UUID getUUID(String columnName) {
return getCurrentRowAsStruct().getUUID(columnName);
}

@Override
public List<UUID> getUUIDList(int columnIndex) {
return getCurrentRowAsStruct().getUUIDList(columnIndex);
}

@Override
public List<UUID> getUUIDList(String columnName) {
return getCurrentRowAsStruct().getUUIDList(columnName);
}

@Override
public List<Timestamp> getTimestampList(int columnIndex) {
return getCurrentRowAsStruct().getTimestampList(columnIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.protobuf.ProtocolMessageEnum;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.UUID;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -226,6 +227,11 @@ protected Date getDateInternal(int columnIndex) {
return values.get(columnIndex).getDate();
}

@Override
protected UUID getUUIDInternal(int columnIndex) {
return values.get(columnIndex).getUuid();
}

@Override
protected <T extends AbstractMessage> T getProtoMessageInternal(int columnIndex, T message) {
return values.get(columnIndex).getProtoMessage(message);
Expand Down Expand Up @@ -334,6 +340,11 @@ protected List<Date> getDateListInternal(int columnIndex) {
return values.get(columnIndex).getDateArray();
}

@Override
protected List<UUID> getUUIDListInternal(int columnIndex) {
return values.get(columnIndex).getUuidArray();
}

@Override
protected List<Struct> getStructListInternal(int columnIndex) {
return values.get(columnIndex).getStructArray();
Expand Down Expand Up @@ -420,6 +431,8 @@ private Object getAsObject(int columnIndex) {
return getTimestampInternal(columnIndex);
case DATE:
return getDateInternal(columnIndex);
case UUID:
return getUUIDInternal(columnIndex);
case STRUCT:
return getStructInternal(columnIndex);
case ARRAY:
Expand Down Expand Up @@ -451,6 +464,8 @@ private Object getAsObject(int columnIndex) {
return getTimestampListInternal(columnIndex);
case DATE:
return getDateListInternal(columnIndex);
case UUID:
return getUUIDListInternal(columnIndex);
case STRUCT:
return getStructListInternal(columnIndex);
default:
Expand Down
Loading
Loading