/
Struct.java
411 lines (358 loc) · 12.9 KB
/
Struct.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* Copyright 2017 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
*
* http://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.spanner;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import com.google.cloud.ByteArray;
import com.google.cloud.Date;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.Type.Code;
import com.google.cloud.spanner.Type.StructField;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Booleans;
import com.google.common.primitives.Doubles;
import com.google.common.primitives.Longs;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.annotation.concurrent.Immutable;
/**
* Represents a non-{@code NULL} value of {@link Type.Code#STRUCT}. Such values are a tuple of named
* and typed columns, where individual columns may be null. Individual rows from a read or query
* operation can be considered as structs; {@link ResultSet#getCurrentRowAsStruct()} allows an
* immutable struct to be created from the row that the result set is currently positioned over.
*
* <p>{@code Struct} instances are immutable.
*
* <p>This class does not support representing typed {@code NULL} {@code Struct} values.
*
* <p>However, struct values <em>inside</em> SQL queries are always typed and can be externally
* supplied to a query only in the form of struct/array-of-struct query parameter values for which
* typed {@code NULL} struct values can be specified in the following ways:
*
* <p>1. As a standalone {@code NULL} struct value or as a nested struct field value, constructed
* using {@link ValueBinder#to(Type, Struct)} or {@link Value#struct(Type, Struct)}.
*
* <p>2. As as a null {@code Struct} reference representing a {@code NULL} struct typed element
* value inside an array/list of '{@code Struct}' references, that is used to construct an
* array-of-struct value using {@link Value#structArray(Type, Iterable)} or {@link
* ValueBinder#toStructArray(Type, Iterable)}. In this case, the type of the {@code NULL} struct
* value is assumed to be the same as the explicitly specified struct element type of the
* array/list.
*/
@Immutable
public abstract class Struct extends AbstractStructReader implements Serializable {
// Only implementations within the package are allowed.
Struct() {}
/** Returns a builder for creating a non-{@code NULL} {@code Struct} instance. */
public static Builder newBuilder() {
return new Builder();
}
/** Builder for constructing non-{@code NULL} {@code Struct} instances. */
public static final class Builder {
private final List<Type.StructField> types = new ArrayList<>();
private final List<Value> values = new ArrayList<>();
private final ValueBinder<Builder> binder;
private String currentField;
private Builder() {
this.binder =
new ValueBinder<Builder>() {
@Override
Builder handle(Value value) {
checkBindingInProgress(true);
addInternal(currentField, value);
currentField = null;
return Builder.this;
}
};
}
/**
* Returns a binder to set the value of a new field in the struct named {@code fieldName}.
*
* @param fieldName name of the field to set. Can be empty or the same as an existing field name
* in the {@code STRUCT}
*/
public ValueBinder<Builder> set(String fieldName) {
checkBindingInProgress(false);
currentField = checkNotNull(fieldName);
return binder;
}
/** Adds a new unnamed field {@code fieldName} with the given value. */
public Builder add(Value value) {
checkBindingInProgress(false);
addInternal("", value);
return this;
}
public Struct build() {
checkBindingInProgress(false);
return new ValueListStruct(types, values);
}
private void addInternal(String fieldName, Value value) {
types.add(Type.StructField.of(fieldName, value.getType()));
values.add(value);
}
private void checkBindingInProgress(boolean expectInProgress) {
if (expectInProgress) {
checkState(currentField != null, "No binding currently active");
} else if (currentField != null) {
throw new IllegalStateException("Incomplete binding for column " + currentField);
}
}
}
/**
* TODO(user) : Consider moving these methods to the StructReader interface once STRUCT-typed
* columns are supported in {@link ResultSet}.
*/
/* Public methods for accessing struct-typed fields */
public Struct getStruct(int columnIndex) {
checkNonNullStruct(columnIndex, columnIndex);
return getStructInternal(columnIndex);
}
public Struct getStruct(String columnName) {
int columnIndex = getColumnIndex(columnName);
checkNonNullStruct(columnIndex, columnName);
return getStructInternal(columnIndex);
}
/* Sub-classes must implement this method */
protected abstract Struct getStructInternal(int columnIndex);
private void checkNonNullStruct(int columnIndex, Object columnNameForError) {
Type actualType = getColumnType(columnIndex);
checkState(
actualType.getCode() == Code.STRUCT,
"Column %s is not of correct type: expected STRUCT<...> but was %s",
columnNameForError,
actualType);
checkNonNull(columnIndex, columnNameForError);
}
/** Default implementation for value structs produced by {@link Builder}. */
private static class ValueListStruct extends Struct {
private final Type type;
private final List<Value> values;
private ValueListStruct(Iterable<StructField> types, Iterable<Value> values) {
this.type = Type.struct(types);
this.values = ImmutableList.copyOf(values);
}
@Override
protected boolean getBooleanInternal(int columnIndex) {
return values.get(columnIndex).getBool();
}
@Override
protected long getLongInternal(int columnIndex) {
return values.get(columnIndex).getInt64();
}
@Override
protected double getDoubleInternal(int columnIndex) {
return values.get(columnIndex).getFloat64();
}
@Override
protected BigDecimal getBigDecimalInternal(int columnIndex) {
return values.get(columnIndex).getNumeric();
}
@Override
protected String getStringInternal(int columnIndex) {
return values.get(columnIndex).getString();
}
@Override
protected String getJsonInternal(int columnIndex) {
return values.get(columnIndex).getJson();
}
@Override
protected String getPgJsonbInternal(int columnIndex) {
return values.get(columnIndex).getPgJsonb();
}
@Override
protected ByteArray getBytesInternal(int columnIndex) {
return values.get(columnIndex).getBytes();
}
@Override
protected Timestamp getTimestampInternal(int columnIndex) {
return values.get(columnIndex).getTimestamp();
}
@Override
protected Date getDateInternal(int columnIndex) {
return values.get(columnIndex).getDate();
}
@Override
protected Value getValueInternal(int columnIndex) {
return values.get(columnIndex);
}
@Override
protected Struct getStructInternal(int columnIndex) {
return values.get(columnIndex).getStruct();
}
@Override
protected boolean[] getBooleanArrayInternal(int columnIndex) {
return Booleans.toArray(getBooleanListInternal(columnIndex));
}
@Override
protected List<Boolean> getBooleanListInternal(int columnIndex) {
return values.get(columnIndex).getBoolArray();
}
@Override
protected long[] getLongArrayInternal(int columnIndex) {
return Longs.toArray(getLongListInternal(columnIndex));
}
@Override
protected List<Long> getLongListInternal(int columnIndex) {
return values.get(columnIndex).getInt64Array();
}
@Override
protected double[] getDoubleArrayInternal(int columnIndex) {
return Doubles.toArray(getDoubleListInternal(columnIndex));
}
@Override
protected List<Double> getDoubleListInternal(int columnIndex) {
return values.get(columnIndex).getFloat64Array();
}
@Override
protected List<BigDecimal> getBigDecimalListInternal(int columnIndex) {
return values.get(columnIndex).getNumericArray();
}
@Override
protected List<String> getStringListInternal(int columnIndex) {
return values.get(columnIndex).getStringArray();
}
@Override
protected List<String> getJsonListInternal(int columnIndex) {
return values.get(columnIndex).getJsonArray();
}
@Override
protected List<String> getPgJsonbListInternal(int columnIndex) {
return values.get(columnIndex).getPgJsonbArray();
}
@Override
protected List<ByteArray> getBytesListInternal(int columnIndex) {
return values.get(columnIndex).getBytesArray();
}
@Override
protected List<Timestamp> getTimestampListInternal(int columnIndex) {
return values.get(columnIndex).getTimestampArray();
}
@Override
protected List<Date> getDateListInternal(int columnIndex) {
return values.get(columnIndex).getDateArray();
}
@Override
protected List<Struct> getStructListInternal(int columnIndex) {
return values.get(columnIndex).getStructArray();
}
@Override
public Type getType() {
return type;
}
@Override
public boolean isNull(int columnIndex) {
return values.get(columnIndex).isNull();
}
@Override
public String toString() {
// TODO(user): Consider pulling a generic toString() up to Struct.
return values.toString();
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Struct)) {
return false;
}
Struct that = (Struct) o;
if (!getType().equals(that.getType())) {
return false;
}
for (int i = 0; i < getColumnCount(); ++i) {
if (!Objects.equals(getAsObject(i), that.getAsObject(i))) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
int result = getType().hashCode();
for (int i = 0; i < getColumnCount(); ++i) {
result = 31 * result + Objects.hashCode(getAsObject(i));
}
return result;
}
private Object getAsObject(int columnIndex) {
Type type = getColumnType(columnIndex);
if (isNull(columnIndex)) {
return null;
}
switch (type.getCode()) {
case BOOL:
return getBooleanInternal(columnIndex);
case INT64:
return getLongInternal(columnIndex);
case FLOAT64:
return getDoubleInternal(columnIndex);
case NUMERIC:
return getBigDecimalInternal(columnIndex);
case PG_NUMERIC:
return getStringInternal(columnIndex);
case STRING:
return getStringInternal(columnIndex);
case JSON:
return getJsonInternal(columnIndex);
case PG_JSONB:
return getPgJsonbInternal(columnIndex);
case BYTES:
return getBytesInternal(columnIndex);
case TIMESTAMP:
return getTimestampInternal(columnIndex);
case DATE:
return getDateInternal(columnIndex);
case STRUCT:
return getStructInternal(columnIndex);
case ARRAY:
switch (type.getArrayElementType().getCode()) {
case BOOL:
return getBooleanListInternal(columnIndex);
case INT64:
return getLongListInternal(columnIndex);
case FLOAT64:
return getDoubleListInternal(columnIndex);
case NUMERIC:
return getBigDecimalListInternal(columnIndex);
case PG_NUMERIC:
return getStringListInternal(columnIndex);
case STRING:
return getStringListInternal(columnIndex);
case JSON:
return getJsonListInternal(columnIndex);
case PG_JSONB:
return getPgJsonbListInternal(columnIndex);
case BYTES:
return getBytesListInternal(columnIndex);
case TIMESTAMP:
return getTimestampListInternal(columnIndex);
case DATE:
return getDateListInternal(columnIndex);
case STRUCT:
return getStructListInternal(columnIndex);
default:
throw new AssertionError("Invalid type " + type);
}
default:
throw new AssertionError("Invalid type " + type);
}
}
}