Skip to content
Open
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
21 changes: 21 additions & 0 deletions google-cloud-spanner/clirr-ignored-differences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,27 @@
<method>com.google.api.gax.rpc.ServerStream executeStreamingPartitionedDml(com.google.spanner.v1.ExecuteSqlRequest, java.util.Map, org.threeten.bp.Duration)</method>
</difference>

<!-- Add support for JSON data type -->
<difference>
<differenceType>7013</differenceType>
<className>com/google/cloud/spanner/AbstractStructReader</className>
<method>java.lang.String getJSONInternal(int)</method>
</difference>
<difference>
<differenceType>7013</differenceType>
<className>com/google/cloud/spanner/AbstractStructReader</className>
<method>java.util.List getJSONListInternal(int)</method>
</difference>
<difference>
<differenceType>7012</differenceType>
<className>com/google/cloud/spanner/StructReader</className>
<method>java.lang.String getJSON(int)</method>
</difference>
<difference>
<differenceType>7012</differenceType>
<className>com/google/cloud/spanner/StructReader</className>
<method>java.lang.String getJSON(java.lang.String)</method>
</difference>
<!-- Add support for NUMERIC data type -->
<difference>
<differenceType>7013</differenceType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.google.protobuf.ByteString;
import com.google.protobuf.ListValue;
import com.google.protobuf.Value.KindCase;
import com.google.protobuf.ValueOrBuilder;
import com.google.spanner.v1.PartialResultSet;
import com.google.spanner.v1.ResultSetMetadata;
import com.google.spanner.v1.ResultSetStats;
Expand Down Expand Up @@ -375,6 +376,9 @@ private Object writeReplace() {
case NUMERIC:
builder.set(fieldName).to((BigDecimal) value);
break;
case JSON:
builder.set(fieldName).to((String) value);
break;
case STRING:
builder.set(fieldName).to((String) value);
break;
Expand All @@ -401,6 +405,9 @@ private Object writeReplace() {
case NUMERIC:
builder.set(fieldName).toNumericArray((Iterable<BigDecimal>) value);
break;
case JSON:
builder.set(fieldName).toJsonArray((Iterable<String>) value);
break;
case STRING:
builder.set(fieldName).toStringArray((Iterable<String>) value);
break;
Expand Down Expand Up @@ -479,6 +486,9 @@ private static Object decodeValue(Type fieldType, com.google.protobuf.Value prot
return valueProtoToFloat64(proto);
case NUMERIC:
return new BigDecimal(proto.getStringValue());
case JSON:
checkType(fieldType, proto, KindCase.STRING_VALUE);
return proto.getStringValue();
case STRING:
checkType(fieldType, proto, KindCase.STRING_VALUE);
return proto.getStringValue();
Expand Down Expand Up @@ -542,6 +552,16 @@ static Object decodeArrayValue(Type elementType, ListValue listValue) {
}
return list;
}
case JSON:
{
// Materialize list: element conversion is expensive and should happen only once.
ArrayList<Object> list = new ArrayList<>(listValue.getValuesCount());
for (com.google.protobuf.Value value : listValue.getValuesList()) {
list.add(
value.getKindCase() == KindCase.NULL_VALUE ? null : value.getStringValue());
}
return list;
}
case STRING:
return Lists.transform(
listValue.getValuesList(),
Expand Down Expand Up @@ -649,6 +669,11 @@ protected BigDecimal getBigDecimalInternal(int columnIndex) {
return (BigDecimal) rowData.get(columnIndex);
}

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

@Override
protected String getStringInternal(int columnIndex) {
return (String) rowData.get(columnIndex);
Expand Down Expand Up @@ -682,6 +707,9 @@ protected Value getValueInternal(int columnIndex) {
return Value.int64(isNull ? null : getLongInternal(columnIndex));
case NUMERIC:
return Value.numeric(isNull ? null : getBigDecimalInternal(columnIndex));
case JSON:
return Value.json(isNull ? null : getJsonInternal(columnIndex));

case FLOAT64:
return Value.float64(isNull ? null : getDoubleInternal(columnIndex));
case STRING:
Expand All @@ -702,6 +730,8 @@ protected Value getValueInternal(int columnIndex) {
return Value.int64Array(isNull ? null : getLongListInternal(columnIndex));
case NUMERIC:
return Value.numericArray(isNull ? null : getBigDecimalListInternal(columnIndex));
case JSON:
return Value.jsonArray(isNull ? null : getJsonListInternal(columnIndex));
case FLOAT64:
return Value.float64Array(isNull ? null : getDoubleListInternal(columnIndex));
case STRING:
Expand Down Expand Up @@ -770,6 +800,12 @@ protected Float64Array getDoubleListInternal(int columnIndex) {
return (Float64Array) rowData.get(columnIndex);
}

@Override
@SuppressWarnings("unchecked") // We know ARRAY<JSON> produces a List<String>.
protected List<String> getJsonListInternal(int columnIndex) {
return (List<String>) rowData.get(columnIndex);
}

@Override
@SuppressWarnings("unchecked") // We know ARRAY<NUMERIC> produces a List<BigDecimal>.
protected List<BigDecimal> getBigDecimalListInternal(int columnIndex) {
Expand Down Expand Up @@ -1303,6 +1339,11 @@ protected BigDecimal getBigDecimalInternal(int columnIndex) {
return currRow().getBigDecimalInternal(columnIndex);
}

@Override
protected String getJsonInternal(int columnIndex) {
return currRow().getJsonInternal(columnIndex);
}

@Override
protected String getStringInternal(int columnIndex) {
return currRow().getStringInternal(columnIndex);
Expand Down Expand Up @@ -1358,6 +1399,11 @@ protected List<Double> getDoubleListInternal(int columnIndex) {
return currRow().getDoubleListInternal(columnIndex);
}

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

@Override
protected List<BigDecimal> getBigDecimalListInternal(int columnIndex) {
return currRow().getBigDecimalListInternal(columnIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public abstract class AbstractStructReader implements StructReader {

protected abstract Timestamp getTimestampInternal(int columnIndex);

protected abstract String getJsonInternal(int columnIndex);

protected abstract Date getDateInternal(int columnIndex);

protected Value getValueInternal(int columnIndex) {
Expand Down Expand Up @@ -75,6 +77,8 @@ protected Value getValueInternal(int columnIndex) {

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

protected abstract List<String> getJsonListInternal(int columnIndex);

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

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

@Override
public String getJson(int columnIndex) {
checkNonNullOfType(columnIndex, Type.json(), columnIndex);
return getJsonInternal(columnIndex);
}

@Override
public String getJson(String columnName) {
int columnIndex = getColumnIndex(columnName);
checkNonNullOfType(columnIndex, Type.json(), columnName);
return getJsonInternal(columnIndex);
}

@Override
public Value getValue(int columnIndex) {
checkNonNull(columnIndex, columnIndex);
Expand Down Expand Up @@ -356,6 +373,19 @@ public List<Date> getDateList(String columnName) {
return getDateListInternal(columnIndex);
}

@Override
public List<String> getJsonList(int columnIndex) {
checkNonNullOfType(columnIndex, Type.array(Type.json()), columnIndex);
return getJsonListInternal(columnIndex);
}

@Override
public List<String> getJsonList(String columnName) {
int columnIndex = getColumnIndex(columnName);
checkNonNullOfType(columnIndex, Type.array(Type.json()), columnName);
return getJsonListInternal(columnIndex);
}

@Override
public List<Struct> getStructList(int columnIndex) {
checkNonNullArrayOfStruct(columnIndex, columnIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,20 @@ public ByteArray getBytes(String columnName) {
return delegate.get().getBytes(columnName);
}

@Override
public String getJSON(int columnIndex) {
checkValidState();
return delegate.get().getJSON(columnIndex);
}

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



@Override
public Timestamp getTimestamp(int columnIndex) {
checkValidState();
Expand Down Expand Up @@ -298,6 +312,18 @@ public List<ByteArray> getBytesList(String columnName) {
return delegate.get().getBytesList(columnName);
}

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

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

@Override
public List<Timestamp> getTimestampList(int columnIndex) {
checkValidState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ public Map<String, Value> asMap() {
LinkedHashMap<String, Value> map = new LinkedHashMap<>();
for (int i = 0; i < columns.size(); ++i) {
Value existing = map.put(columns.get(i), values.get(i));
if (existing != null) {
throw new IllegalStateException("Duplicate column: " + columns.get(i));
}
}
return Collections.unmodifiableMap(map);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.cloud.ByteArray;
import com.google.cloud.Date;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.Value.json;
import com.google.cloud.spanner.Options.QueryOption;
import com.google.cloud.spanner.Type.Code;
import com.google.cloud.spanner.Type.StructField;
Expand Down Expand Up @@ -273,6 +274,26 @@ public Date getDate(String columnName) {
return getCurrentRowAsStruct().getDate(columnName);
}

@Override
public String getJSON(int columnIndex) {
return getCurrentRowAsStruct().getJSON(columnIndex);
}

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

@Override
public String[] getJSONArray(int columnIndex) {
return getCurrentRowAsStruct().getJSONArray(columnIndex);
}

@Override
public String[] getJSONArray(String columnName) {
return getCurrentRowAsStruct().getJSONArray(columnName);
}

@Override
public Value getValue(int columnIndex) {
return getCurrentRowAsStruct().getValue(columnIndex);
Expand Down Expand Up @@ -393,6 +414,16 @@ public List<Date> getDateList(String columnName) {
return getCurrentRowAsStruct().getDateList(columnName);
}

@Override
public List<String> getJSONList(int columnIndex) {
return getCurrentRowAsStruct().getList(columnIndex, json.class);
}

@Override
public List<String> getJSONList(String columnName) {
return getCurrentRowAsStruct().getList(columnName, json.class);
}

@Override
public List<Struct> getStructList(int columnIndex) {
return getCurrentRowAsStruct().getStructList(columnIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ protected Timestamp getTimestampInternal(int columnIndex) {
return values.get(columnIndex).getTimestamp();
}

@Override
protected String getJsonInternal(int columnIndex) {
return values.get(columnIndex).getJson();
}

@Override
protected Date getDateInternal(int columnIndex) {
return values.get(columnIndex).getDate();
Expand Down Expand Up @@ -272,6 +277,11 @@ protected List<Date> getDateListInternal(int columnIndex) {
return values.get(columnIndex).getDateArray();
}

@Override
protected List<String> getJsonListInternal(int columnIndex) {
return values.get(columnIndex).getJsonArray();
}

@Override
protected List<Struct> getStructListInternal(int columnIndex) {
return values.get(columnIndex).getStructArray();
Expand Down Expand Up @@ -347,6 +357,8 @@ private Object getAsObject(int columnIndex) {
return getTimestampInternal(columnIndex);
case DATE:
return getDateInternal(columnIndex);
case JSON:
return getJsonInternal(columnIndex);
case STRUCT:
return getStructInternal(columnIndex);
case ARRAY:
Expand All @@ -367,6 +379,8 @@ private Object getAsObject(int columnIndex) {
return getTimestampListInternal(columnIndex);
case DATE:
return getDateListInternal(columnIndex);
case JSON:
return getJsonListInternal(columnIndex);
case STRUCT:
return getStructListInternal(columnIndex);
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ public interface StructReader {
/** Returns the value of a non-{@code NULL} column with type {@link Type#bytes()}. */
ByteArray getBytes(String columnName);

/** Returns the value of a non-{@code NULL} column with type {@link Type#json()}. */
String getJson(int columnIndex);

/** Returns the value of a non-{@code NULL} column with type {@link Type#json()}. */
String getJson(String columnName);

/** Returns the value of a non-{@code NULL} column with type {@code Type.array(Type.json())}. */
List<String> getJsonList(int columnIndex);

/** Returns the value of a non-{@code NULL} column with type {@code Type.array(Type.json())}. */
List<String> getJsonList(String columnName);

/** Returns the value of a non-{@code NULL} column with type {@link Type#timestamp()}. */
Timestamp getTimestamp(int columnIndex);

Expand Down
Loading