-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathValue.java
3060 lines (2640 loc) · 92.3 KB
/
Value.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 com.google.cloud.ByteArray;
import com.google.cloud.Date;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.AbstractResultSet.LazyByteArray;
import com.google.cloud.spanner.Type.Code;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.io.CharSource;
import com.google.protobuf.AbstractMessage;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.EnumDescriptor;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.ListValue;
import com.google.protobuf.NullValue;
import com.google.protobuf.ProtocolMessageEnum;
import com.google.protobuf.Value.KindCase;
import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.BitSet;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
/**
* Represents a value to be consumed by the Cloud Spanner API. A value can be {@code NULL} or
* non-{@code NULL}; regardless, values always have an associated type.
*
* <p>The {@code Value} API is optimized for construction, since this is the majority use-case when
* using this class with the Cloud Spanner libraries. The factory method signatures and internal
* representations are design to minimize memory usage and object creation while still maintaining
* the immutability contract of this class. In particular, arrays of primitive types can be
* constructed without requiring boxing into collections of wrapper types. The getters in this class
* are intended primarily for test purposes, and so do not share the same performance
* characteristics; in particular, getters for array types may be expensive.
*
* <p>{@code Value} instances are immutable.
*/
@Immutable
public abstract class Value implements Serializable {
/**
* Placeholder value to be passed to a mutation to make Cloud Spanner store the commit timestamp
* in that column. The commit timestamp is the timestamp corresponding to when Cloud Spanner
* commits the transaction containing the mutation.
*
* <p>Note that this particular timestamp instance has no semantic meaning. In particular the
* value of seconds and nanoseconds in this timestamp are meaningless. This placeholder can only
* be used for columns that have set the option "(allow_commit_timestamp=true)" in the schema.
*
* <p>When reading the value stored in such a column, the value returned is an actual timestamp
* corresponding to the commit time of the transaction, which has no relation to this placeholder.
*
* @see <a href="https://cloud.google.com/spanner/docs/transactions#rw_transaction_semantics">
* Transaction Semantics</a>
*/
public static final Timestamp COMMIT_TIMESTAMP = Timestamp.ofTimeMicroseconds(0L);
static final com.google.protobuf.Value NULL_PROTO =
com.google.protobuf.Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
/** Constant to specify a PG Numeric NaN value. */
public static final String NAN = "NaN";
private static final int MAX_DEBUG_STRING_LENGTH = 36;
private static final String ELLIPSIS = "...";
private static final String NULL_STRING = "NULL";
private static final char LIST_SEPARATOR = ',';
private static final char LIST_OPEN = '[';
private static final char LIST_CLOSE = ']';
private static final long serialVersionUID = -5289864325087675338L;
/**
* Returns a {@link Value} that wraps the given proto value. This can be used to construct a value
* without a specific type, and let the backend infer the type based on the statement where it is
* used.
*
* @param value the non-null proto value (a {@link NullValue} is allowed)
*/
public static Value untyped(com.google.protobuf.Value value) {
return new ProtoBackedValueImpl(Preconditions.checkNotNull(value), null);
}
/** Returns a generic Value backed by a protobuf value. This is used for unrecognized types. */
static Value unrecognized(com.google.protobuf.Value value, Type type) {
Preconditions.checkArgument(
type.getCode() == Code.UNRECOGNIZED
|| type.getCode() == Code.ARRAY
&& type.getArrayElementType().getCode() == Code.UNRECOGNIZED);
return new ProtoBackedValueImpl(Preconditions.checkNotNull(value), type);
}
/**
* Returns a {@code BOOL} value.
*
* @param v the value, which may be null
*/
public static Value bool(@Nullable Boolean v) {
return new BoolImpl(v == null, v == null ? false : v);
}
/** Returns a {@code BOOL} value. */
public static Value bool(boolean v) {
return new BoolImpl(false, v);
}
/**
* Returns an {@code INT64} value.
*
* @param v the value, which may be null
*/
public static Value int64(@Nullable Long v) {
return new Int64Impl(v == null, v == null ? 0 : v);
}
/** Returns an {@code INT64} value. */
public static Value int64(long v) {
return new Int64Impl(false, v);
}
/**
* Returns a {@code FLOAT32} value.
*
* @param v the value, which may be null
*/
public static Value float32(@Nullable Float v) {
return new Float32Impl(v == null, v == null ? 0 : v);
}
/** Returns a {@code FLOAT32} value. */
public static Value float32(float v) {
return new Float32Impl(false, v);
}
/**
* Returns a {@code FLOAT64} value.
*
* @param v the value, which may be null
*/
public static Value float64(@Nullable Double v) {
return new Float64Impl(v == null, v == null ? 0 : v);
}
/** Returns a {@code FLOAT64} value. */
public static Value float64(double v) {
return new Float64Impl(false, v);
}
/**
* Returns a {@code NUMERIC} value. The valid value range for the whole component of the {@link
* BigDecimal} is from -9,999,999,999,999,999,999,999,999 to +9,999,999,999,999,999,999,999,999
* (both inclusive), i.e. the max length of the whole component is 29 digits. The max length of
* the fractional part is 9 digits. Trailing zeros in the fractional part are not considered and
* will be lost, as Cloud Spanner does not preserve the precision of a numeric value.
*
* <p>If you set a numeric value of a record to for example 0.10, Cloud Spanner will return this
* value as 0.1 in subsequent queries. Use {@link BigDecimal#stripTrailingZeros()} to compare
* inserted values with retrieved values if your application might insert numeric values with
* trailing zeros.
*
* @param v the value, which may be null
*/
public static Value numeric(@Nullable BigDecimal v) {
if (v != null) {
// Cloud Spanner does not preserve the precision, so 0.1 is considered equal to 0.10.
BigDecimal test = v.stripTrailingZeros();
if (test.scale() > 9) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.OUT_OF_RANGE,
String.format(
"Max scale for a numeric is 9. The requested numeric has scale %d", test.scale()));
}
if (test.precision() - test.scale() > 29) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.OUT_OF_RANGE,
String.format(
"Max precision for the whole component of a numeric is 29. The requested numeric has a whole component with precision %d",
test.precision() - test.scale()));
}
}
return new NumericImpl(v == null, v);
}
/**
* Returns a {@code PG NUMERIC} value. This value has flexible precision and scale which is
* specified in the Database DDL. This value also supports {@code NaNs}, which can be specified
* with {@code Value.pgNumeric(Value.NAN)} or simply as {@code Value.pgNumeric("NaN")}.
*
* <p>Note that this flavour of numeric is different than Spanner numerics ({@link
* Value#numeric(BigDecimal)}). It should be used only for handling numerics in the PostgreSQL
* dialect.
*
* @param v the value, which may be null
*/
public static Value pgNumeric(@Nullable String v) {
return new PgNumericImpl(v == null, v);
}
/**
* Returns a {@code STRING} value.
*
* @param v the value, which may be null
*/
public static Value string(@Nullable String v) {
return new StringImpl(v == null, v);
}
/**
* Returns a {@code JSON} value.
*
* @param v the value, which may be null
*/
public static Value json(@Nullable String v) {
return new JsonImpl(v == null, v);
}
/**
* Returns a {@code PG JSONB} value.
*
* @param v the value, which may be null
*/
public static Value pgJsonb(@Nullable String v) {
return new PgJsonbImpl(v == null, v);
}
/**
* Returns an {@code PG_OID} value.
*
* @param v the value, which may be null
*/
public static Value pgOid(@Nullable Long v) {
return new PgOidImpl(v == null, v == null ? 0 : v);
}
/** Returns an {@code PG_OID} value. */
public static Value pgOid(long v) {
return new PgOidImpl(false, v);
}
/**
* Return a {@code PROTO} value for not null proto messages.
*
* @param v Not null Proto message.
*/
public static Value protoMessage(AbstractMessage v) {
Preconditions.checkNotNull(
v, "Use protoMessage((ByteArray) null, MyProtoClass.getDescriptor()) for null values.");
return protoMessage(
ByteArray.copyFrom(v.toByteArray()), v.getDescriptorForType().getFullName());
}
/**
* Return a {@code PROTO} value
*
* @param v Serialized Proto Array, which may be null.
* @param protoTypeFqn Fully qualified name of proto representing the proto definition. Use static
* method from proto class {@code MyProtoClass.getDescriptor().getFullName()}
*/
public static Value protoMessage(@Nullable ByteArray v, String protoTypeFqn) {
return new ProtoMessageImpl(v == null, v, protoTypeFqn);
}
/**
* Return a {@code PROTO} value
*
* @param v Serialized Proto Array, which may be null.
* @param descriptor Proto Type Descriptor, use static method from proto class {@code
* MyProtoClass.getDescriptor()}.
*/
public static Value protoMessage(@Nullable ByteArray v, Descriptor descriptor) {
Preconditions.checkNotNull(descriptor, "descriptor can't be null.");
return protoMessage(v, descriptor.getFullName());
}
/**
* Return a {@code ENUM} value for not null proto messages.
*
* @param v Proto Enum, which may be null.
*/
public static Value protoEnum(ProtocolMessageEnum v) {
Preconditions.checkNotNull(
v, "Use protoEnum((Long) null, MyProtoEnum.getDescriptor()) for null values.");
return protoEnum(v.getNumber(), v.getDescriptorForType().getFullName());
}
/**
* Return a {@code ENUM} value.
*
* @param v Enum non-primitive Integer constant.
* @param protoTypeFqn Fully qualified name of proto representing the enum definition. Use static
* method from proto class {@code MyProtoEnum.getDescriptor().getFullName()}
*/
public static Value protoEnum(@Nullable Long v, String protoTypeFqn) {
return new ProtoEnumImpl(v == null, v, protoTypeFqn);
}
/**
* Return a {@code ENUM} value.
*
* @param v Enum non-primitive Integer constant.
* @param enumDescriptor Enum Type Descriptor. Use static method from proto class {@code *
* MyProtoEnum.getDescriptor()}.
*/
public static Value protoEnum(@Nullable Long v, EnumDescriptor enumDescriptor) {
Preconditions.checkNotNull(enumDescriptor, "descriptor can't be null.");
return protoEnum(v, enumDescriptor.getFullName());
}
/**
* Return a {@code ENUM} value.
*
* @param v Enum integer primitive constant.
* @param protoTypeFqn Fully qualified name of proto representing the enum definition. Use static
* method from proto class {@code MyProtoEnum.getDescriptor().getFullName()}
*/
public static Value protoEnum(long v, String protoTypeFqn) {
return new ProtoEnumImpl(false, v, protoTypeFqn);
}
/**
* e Returns a {@code BYTES} value. Returns a {@code BYTES} value.
*
* @param v the value, which may be null
*/
public static Value bytes(@Nullable ByteArray v) {
return new LazyBytesImpl(v == null, v);
}
/**
* Returns a {@code BYTES} value.
*
* @param base64String the value in Base64 encoding, which may be null. This value must be a valid
* base64 string.
*/
public static Value bytesFromBase64(@Nullable String base64String) {
return new LazyBytesImpl(
base64String == null, base64String == null ? null : new LazyByteArray(base64String));
}
static Value internalBytes(@Nullable LazyByteArray bytes) {
return new LazyBytesImpl(bytes == null, bytes);
}
/** Returns a {@code TIMESTAMP} value. */
public static Value timestamp(@Nullable Timestamp v) {
return new TimestampImpl(v == null, v == Value.COMMIT_TIMESTAMP, v);
}
/**
* Returns a {@code DATE} value. The range [1678-01-01, 2262-01-01) is the legal interval for
* cloud spanner dates. A write to a date column is rejected if the value is outside of that
* interval.
*/
public static Value date(@Nullable Date v) {
return new DateImpl(v == null, v);
}
/** Returns a non-{@code NULL} {#code STRUCT} value. */
public static Value struct(Struct v) {
Preconditions.checkNotNull(v, "Illegal call to create a NULL struct value.");
return new StructImpl(v);
}
/**
* Returns a {@code STRUCT} value of {@code Type} type.
*
* @param type the type of the {@code STRUCT} value
* @param v the struct {@code STRUCT} value. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. If non-{@code null}, {@link Struct#getType()} must match
* type.
*/
public static Value struct(Type type, @Nullable Struct v) {
if (v == null) {
Preconditions.checkArgument(
type.getCode() == Code.STRUCT,
"Illegal call to create a NULL struct with a non-struct type.");
return new StructImpl(type);
} else {
Preconditions.checkArgument(
type.equals(v.getType()), "Mismatch between struct value and type.");
return new StructImpl(v);
}
}
/**
* Returns an {@code ARRAY<BOOL>} value.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
*/
public static Value boolArray(@Nullable boolean[] v) {
return boolArray(v, 0, v == null ? 0 : v.length);
}
/**
* Returns an {@code ARRAY<BOOL>} value that takes its elements from a region of an array.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
* @param pos the start position of {@code v} to copy values from. Ignored if {@code v} is {@code
* null}.
* @param length the number of values to copy from {@code v}. Ignored if {@code v} is {@code
* null}.
*/
public static Value boolArray(@Nullable boolean[] v, int pos, int length) {
return boolArrayFactory.create(v, pos, length);
}
/**
* Returns an {@code ARRAY<BOOL>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value boolArray(@Nullable Iterable<Boolean> v) {
// TODO(user): Consider memory optimizing boolArray() to use BitSet instead of boolean[].
return boolArrayFactory.create(v);
}
/**
* Returns an {@code ARRAY<INT64>} value.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
*/
public static Value int64Array(@Nullable long[] v) {
return int64Array(v, 0, v == null ? 0 : v.length);
}
/**
* Returns an {@code ARRAY<INT64>} value that takes its elements from a region of an array.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
* @param pos the start position of {@code v} to copy values from. Ignored if {@code v} is {@code
* null}.
* @param length the number of values to copy from {@code v}. Ignored if {@code v} is {@code
* null}.
*/
public static Value int64Array(@Nullable long[] v, int pos, int length) {
return int64ArrayFactory.create(v, pos, length);
}
/**
* Returns an {@code ARRAY<INT64>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value int64Array(@Nullable Iterable<Long> v) {
return int64ArrayFactory.create(v);
}
/**
* Returns an {@code ARRAY<FLOAT32>} value.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
*/
public static Value float32Array(@Nullable float[] v) {
return float32Array(v, 0, v == null ? 0 : v.length);
}
/**
* Returns an {@code ARRAY<FLOAT32>} value that takes its elements from a region of an array.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
* @param pos the start position of {@code v} to copy values from. Ignored if {@code v} is {@code
* null}.
* @param length the number of values to copy from {@code v}. Ignored if {@code v} is {@code
* null}.
*/
public static Value float32Array(@Nullable float[] v, int pos, int length) {
return float32ArrayFactory.create(v, pos, length);
}
/**
* Returns an {@code ARRAY<FLOAT32>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value float32Array(@Nullable Iterable<Float> v) {
return float32ArrayFactory.create(v);
}
/**
* Returns an {@code ARRAY<FLOAT64>} value.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
*/
public static Value float64Array(@Nullable double[] v) {
return float64Array(v, 0, v == null ? 0 : v.length);
}
/**
* Returns an {@code ARRAY<FLOAT64>} value that takes its elements from a region of an array.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
* @param pos the start position of {@code v} to copy values from. Ignored if {@code v} is {@code
* null}.
* @param length the number of values to copy from {@code v}. Ignored if {@code v} is {@code
* null}.
*/
public static Value float64Array(@Nullable double[] v, int pos, int length) {
return float64ArrayFactory.create(v, pos, length);
}
/**
* Returns an {@code ARRAY<FLOAT64>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value float64Array(@Nullable Iterable<Double> v) {
return float64ArrayFactory.create(v);
}
/**
* Returns an {@code ARRAY<NUMERIC>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value numericArray(@Nullable Iterable<BigDecimal> v) {
return new NumericArrayImpl(v == null, v == null ? null : immutableCopyOf(v));
}
/**
* Returns an {@code ARRAY<PG_NUMERIC>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}. Individual
* elements may be {@code "NaN"} or {@link Value#NAN}.
*/
public static Value pgNumericArray(@Nullable Iterable<String> v) {
return new PgNumericArrayImpl(v == null, v == null ? null : immutableCopyOf(v));
}
/**
* Returns an {@code ARRAY<STRING>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value stringArray(@Nullable Iterable<String> v) {
return new StringArrayImpl(v == null, v == null ? null : immutableCopyOf(v));
}
/**
* Returns an {@code ARRAY<JSON>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value jsonArray(@Nullable Iterable<String> v) {
return new JsonArrayImpl(v == null, v == null ? null : immutableCopyOf(v));
}
/**
* Returns an {@code ARRAY<JSONB>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value pgJsonbArray(@Nullable Iterable<String> v) {
return new PgJsonbArrayImpl(v == null, v == null ? null : immutableCopyOf(v));
}
/**
* Returns an {@code ARRAY<PG_OID>} value.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
*/
public static Value pgOidArray(@Nullable long[] v) {
return pgOidArray(v, 0, v == null ? 0 : v.length);
}
/**
* Returns an {@code ARRAY<PG_OID>} value that takes its elements from a region of an array.
*
* @param v the source of element values, which may be null to produce a value for which {@code
* isNull()} is {@code true}
* @param pos the start position of {@code v} to copy values from. Ignored if {@code v} is {@code
* null}.
* @param length the number of values to copy from {@code v}. Ignored if {@code v} is {@code
* null}.
*/
public static Value pgOidArray(@Nullable long[] v, int pos, int length) {
return pgOidArrayFactory.create(v, pos, length);
}
/**
* Returns an {@code ARRAY<PG_OID>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value pgOidArray(@Nullable Iterable<Long> v) {
return pgOidArrayFactory.create(v);
}
/**
* Returns an {@code ARRAY<PROTO>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
* @param descriptor Proto Type Descriptor, use static method from proto class {@code
* MyProtoClass.getDescriptor()}.
*/
public static Value protoMessageArray(
@Nullable Iterable<AbstractMessage> v, Descriptor descriptor) {
if (v == null) {
return new ProtoMessageArrayImpl(true, null, descriptor.getFullName());
}
List<ByteArray> serializedArray = new ArrayList<>();
v.forEach(
(message) -> {
if (message != null) {
serializedArray.add(ByteArray.copyFrom(message.toByteArray()));
} else {
serializedArray.add(null);
}
});
return new ProtoMessageArrayImpl(false, serializedArray, descriptor.getFullName());
}
/**
* Returns an {@code ARRAY<PROTO>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
* @param protoTypeFqn Fully qualified name of proto representing the proto definition. Use static
* method from proto class {@code MyProtoClass.getDescriptor().getFullName()}
*/
public static Value protoMessageArray(@Nullable Iterable<ByteArray> v, String protoTypeFqn) {
return new ProtoMessageArrayImpl(
v == null, v != null ? immutableCopyOf(v) : null, protoTypeFqn);
}
/**
* Returns an {@code ARRAY<ENUM>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
* @param descriptor Proto Type Descriptor, use static method from proto class {@code
* MyProtoClass.getDescriptor()}.
*/
public static Value protoEnumArray(
@Nullable Iterable<ProtocolMessageEnum> v, EnumDescriptor descriptor) {
if (v == null) {
return new ProtoEnumArrayImpl(true, null, descriptor.getFullName());
}
List<Long> enumConstValues = new ArrayList<>();
v.forEach(
(protoEnum) -> {
if (protoEnum != null) {
enumConstValues.add((long) protoEnum.getNumber());
} else {
enumConstValues.add(null);
}
});
return new ProtoEnumArrayImpl(false, enumConstValues, descriptor.getFullName());
}
/**
* Returns an {@code ARRAY<ENUM>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
* @param protoTypeFqn Fully qualified name of proto representing the enum definition. Use static
* method from proto class {@code MyProtoEnum.getDescriptor().getFullName()}
*/
public static Value protoEnumArray(@Nullable Iterable<Long> v, String protoTypeFqn) {
return new ProtoEnumArrayImpl(v == null, v != null ? immutableCopyOf(v) : null, protoTypeFqn);
}
/**
* Returns an {@code ARRAY<BYTES>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value bytesArray(@Nullable Iterable<ByteArray> v) {
return new LazyBytesArrayImpl(v == null, v == null ? null : byteArraysToLazyByteArrayList(v));
}
private static List<LazyByteArray> byteArraysToLazyByteArrayList(Iterable<ByteArray> byteArrays) {
List<LazyByteArray> list = new ArrayList<>();
for (ByteArray byteArray : byteArrays) {
list.add(byteArray == null ? null : new LazyByteArray(byteArray));
}
return Collections.unmodifiableList(list);
}
/**
* Returns an {@code ARRAY<BYTES>} value.
*
* @param base64Strings the source of element values. This may be {@code null} to produce a value
* for which {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
* Non-null values must be a valid Base64 string.
*/
public static Value bytesArrayFromBase64(@Nullable Iterable<String> base64Strings) {
return new LazyBytesArrayImpl(
base64Strings == null,
base64Strings == null ? null : base64StringsToLazyByteArrayList(base64Strings));
}
private static List<LazyByteArray> base64StringsToLazyByteArrayList(
Iterable<String> base64Strings) {
List<LazyByteArray> list = new ArrayList<>();
for (String base64 : base64Strings) {
list.add(base64 == null ? null : new LazyByteArray(base64));
}
return Collections.unmodifiableList(list);
}
/**
* Returns an {@code ARRAY<TIMESTAMP>} value.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value timestampArray(@Nullable Iterable<Timestamp> v) {
return new TimestampArrayImpl(v == null, v == null ? null : immutableCopyOf(v));
}
/**
* Returns an {@code ARRAY<DATE>} value. The range [1678-01-01, 2262-01-01) is the legal interval
* for cloud spanner dates. A write to a date column is rejected if the value is outside of that
* interval.
*
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value dateArray(@Nullable Iterable<Date> v) {
return new DateArrayImpl(v == null, v == null ? null : immutableCopyOf(v));
}
/**
* Returns an {@code ARRAY<STRUCT<...>>} value.
*
* @param elementType
* @param v the source of element values. This may be {@code null} to produce a value for which
* {@code isNull()} is {@code true}. Individual elements may also be {@code null}.
*/
public static Value structArray(Type elementType, @Nullable Iterable<Struct> v) {
if (v == null) {
Preconditions.checkArgument(
elementType.getCode() == Code.STRUCT,
"Illegal call to create a NULL array-of-struct with a non-struct element type.");
return new StructArrayImpl(elementType, null);
}
List<Struct> values = immutableCopyOf(v);
for (Struct value : values) {
if (value != null) {
Preconditions.checkArgument(
value.getType().equals(elementType),
"Members of v must have type %s (found %s)",
elementType,
value.getType());
}
}
return new StructArrayImpl(elementType, values);
}
private Value() {}
/** Returns the type of this value. This will return a type even if {@code isNull()} is true. */
public abstract Type getType();
/** Returns {@code true} if this instance represents a {@code NULL} value. */
public abstract boolean isNull();
/**
* Returns the value of a {@code BOOL}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract boolean getBool();
/**
* Returns the value of a {@code INT64}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract long getInt64();
/**
* Returns the value of a {@code FLOAT32}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract float getFloat32();
/**
* Returns the value of a {@code FLOAT64}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract double getFloat64();
/**
* Returns the value of a {@code NUMERIC}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract BigDecimal getNumeric();
/**
* Returns the value of a {@code STRING}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract String getString();
/**
* Returns the value of a {@code JSON}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public String getJson() {
throw new UnsupportedOperationException("Not implemented");
}
/**
* Returns the value of a {@code JSONB}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public String getPgJsonb() {
throw new UnsupportedOperationException("Not implemented");
}
/**
* Returns the value of a {@code PROTO}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public <T extends AbstractMessage> T getProtoMessage(T m) {
throw new UnsupportedOperationException("Not implemented");
}
/**
* Returns the value of a {@code ENUM}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public <T extends ProtocolMessageEnum> T getProtoEnum(
Function<Integer, ProtocolMessageEnum> method) {
throw new UnsupportedOperationException("Not implemented");
}
/**
* Returns the value of a {@code BYTES}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract ByteArray getBytes();
/**
* Returns the value of a {@code TIMESTAMP}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type or
* {@link #isCommitTimestamp()}.
*/
public abstract Timestamp getTimestamp();
/** Returns true if this is a commit timestamp value. */
public abstract boolean isCommitTimestamp();
/**
* Returns the value of a {@code DATE}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract Date getDate();
/**
* Returns the value of a {@code STRUCT}-typed instance.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract Struct getStruct();
/**
* Returns the value of an {@code ARRAY<BOOL>}-typed instance. While the returned list itself will
* never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List<Boolean> getBoolArray();
/**
* Returns the value of an {@code ARRAY<INT64>}-typed instance. While the returned list itself
* will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List<Long> getInt64Array();
/**
* Returns the value of an {@code ARRAY<FLOAT32>}-typed instance. While the returned list itself
* will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List<Float> getFloat32Array();
/**
* Returns the value of an {@code ARRAY<FLOAT64>}-typed instance. While the returned list itself
* will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List<Double> getFloat64Array();
/**
* Returns the value of an {@code ARRAY<NUMERIC>}-typed instance. While the returned list itself
* will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List<BigDecimal> getNumericArray();
/**
* Returns the value of an {@code ARRAY<STRING>}-typed instance. While the returned list itself
* will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public abstract List<String> getStringArray();
/**
* Returns the value of an {@code ARRAY<JSON>}-typed instance. While the returned list itself will
* never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public List<String> getJsonArray() {
throw new UnsupportedOperationException("Not implemented");
}
/**
* Returns the value of an {@code ARRAY<JSONB>}-typed instance. While the returned list itself
* will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public List<String> getPgJsonbArray() {
throw new UnsupportedOperationException("Not implemented");
}
/**
* Returns the value of an {@code ARRAY<PROTO>}-typed instance. While the returned list itself
* will never be {@code null}, elements of that list may be null.
*
* @throws IllegalStateException if {@code isNull()} or the value is not of the expected type
*/
public <T extends AbstractMessage> List<T> getProtoMessageArray(T m) {
throw new UnsupportedOperationException("Not implemented");