/
Field.java
505 lines (455 loc) · 17.4 KB
/
Field.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
/*
* Copyright 2015 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.bigquery;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.isNullOrEmpty;
import com.google.api.client.util.Data;
import com.google.api.services.bigquery.model.TableFieldSchema;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Lists;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;
/**
* Google BigQuery Table schema field. A table field has a name, a type, a mode and possibly a
* description.
*/
public final class Field implements Serializable {
static final Function<TableFieldSchema, Field> FROM_PB_FUNCTION =
new Function<TableFieldSchema, Field>() {
@Override
public Field apply(TableFieldSchema pb) {
return Field.fromPb(pb);
}
};
static final Function<Field, TableFieldSchema> TO_PB_FUNCTION =
new Function<Field, TableFieldSchema>() {
@Override
public TableFieldSchema apply(Field field) {
return field.toPb();
}
};
private static final long serialVersionUID = 8827990270251118556L;
private final String name;
private final LegacySQLTypeName type;
private final FieldList subFields;
private final String mode;
private final String description;
private final PolicyTags policyTags;
private final Long maxLength;
private final Long scale;
private final Long precision;
private final String defaultValueExpression;
/**
* Mode for a BigQuery Table field. {@link Mode#NULLABLE} fields can be set to {@code null},
* {@link Mode#REQUIRED} fields must be provided. {@link Mode#REPEATED} fields can contain more
* than one value.
*/
public enum Mode {
NULLABLE,
REQUIRED,
REPEATED
}
public static final class Builder {
private String name;
private LegacySQLTypeName type;
private FieldList subFields;
private String mode;
private String description;
private PolicyTags policyTags;
private Long maxLength;
private Long scale;
private Long precision;
private String defaultValueExpression;
private Builder() {}
private Builder(Field field) {
this.name = field.name;
this.type = field.type;
this.subFields = field.subFields;
this.mode = field.mode;
this.description = field.description;
this.policyTags = field.policyTags;
this.maxLength = field.maxLength;
this.scale = field.scale;
this.precision = field.precision;
this.defaultValueExpression = field.defaultValueExpression;
}
/**
* Sets the field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or
* underscores (_), and must start with a letter or underscore. The maximum length is 128
* characters.
*/
public Builder setName(String name) {
this.name = checkNotNull(name);
return this;
}
/**
* Sets the type of the field.
*
* @param type BigQuery data type
* @param subFields nested schema fields in case if {@code type} is {@link
* LegacySQLTypeName#RECORD}, empty otherwise
* @throws IllegalArgumentException if {@code type == LegacySQLTypeName.RECORD &&
* subFields.length == 0} or if {@code type != LegacySQLTypeName.RECORD && subFields.length
* != 0}
* @see <a href="https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types">Data
* Types</a>
*/
public Builder setType(LegacySQLTypeName type, Field... subFields) {
return setType(type, subFields.length > 0 ? FieldList.of(subFields) : null);
}
/**
* Sets the type of the field.
*
* @param type BigQuery data type
* @param subFields nested schema fields in case if {@code type} is {@link
* StandardSQLTypeName#STRUCT}, empty otherwise
* @throws IllegalArgumentException if {@code type == StandardSQLTypeName.STRUCT &&
* subFields.length == 0} or if {@code type != StandardSQLTypeName.STRUCT &&
* subFields.length != 0}
* @see <a href="https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types">Data
* Types</a>
*/
public Builder setType(StandardSQLTypeName type, Field... subFields) {
return setType(
LegacySQLTypeName.legacySQLTypeName(type),
subFields.length > 0 ? FieldList.of(subFields) : null);
}
/**
* Sets the type of the field.
*
* @param type BigQuery data type
* @param subFields nested schema fields, in case if {@code type} is {@link
* LegacySQLTypeName#RECORD}, {@code null} otherwise.
* @throws IllegalArgumentException if {@code type == LegacySQLTypeName.RECORD && (subFields ==
* null || subFields.isEmpty())} or if {@code type != LegacySQLTypeName.RECORD && subFields
* != null}
* @see <a href="https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types">Data
* Types</a>
*/
public Builder setType(LegacySQLTypeName type, FieldList subFields) {
// LegacySQLTypeName is not an enum, cannot use reference equal.
if (LegacySQLTypeName.RECORD.equals(type)) {
if (subFields == null || subFields.isEmpty()) {
throw new IllegalArgumentException(
"The " + type + " field must have at least one sub-field");
}
} else {
if (subFields != null) {
throw new IllegalArgumentException(
"Only " + LegacySQLTypeName.RECORD + " fields can have sub-fields");
}
}
this.type = type;
this.subFields = subFields;
return this;
}
/**
* Sets the type of the field.
*
* @param type BigQuery data type
* @param subFields nested schema fields in case if {@code type} is {@link
* StandardSQLTypeName#STRUCT}, empty otherwise
* @throws IllegalArgumentException if {@code type == StandardSQLTypeName.STRUCT &&
* subFields.length == 0} or if {@code type != StandardSQLTypeName.STRUCT &&
* subFields.length != 0}
* @see <a href="https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types">Data
* Types</a>
*/
public Builder setType(StandardSQLTypeName type, FieldList subFields) {
return setType(LegacySQLTypeName.legacySQLTypeName(type), subFields);
}
/** Sets the mode of the field. When not specified {@link Mode#NULLABLE} is used. */
public Builder setMode(Mode mode) {
this.mode = mode != null ? mode.name() : Data.<String>nullOf(String.class);
return this;
}
/** Sets the field description. The maximum length is 16K characters. */
public Builder setDescription(String description) {
this.description = firstNonNull(description, Data.<String>nullOf(String.class));
return this;
}
/** Sets the policy tags for the field. */
public Builder setPolicyTags(PolicyTags policyTags) {
this.policyTags = policyTags;
return this;
}
/**
* Sets the maximum length of the field for STRING or BYTES type.
*
* <p>It is invalid to set value for types other than STRING or BYTES.
*
* <p>For STRING type, this represents the maximum UTF-8 length of strings allowed in the field.
* For BYTES type, this represents the maximum number of bytes in the field.
*/
public Builder setMaxLength(Long maxLength) {
this.maxLength = maxLength;
return this;
}
/**
* Scale can be used to constrain the maximum number of digits in the fractional part of a
* NUMERIC or BIGNUMERIC type. If the Scale value is set, the Precision value must be set as
* well. It is invalid to set values for Scale for types other than NUMERIC or BIGNUMERIC. See
* the Precision field for additional guidance about valid values.
*/
public Builder setScale(Long scale) {
this.scale = scale;
return this;
}
/**
* Precision can be used to constrain the maximum number of total digits allowed for NUMERIC or
* BIGNUMERIC types. It is invalid to set values for Precision for types other than // NUMERIC
* or BIGNUMERIC. For NUMERIC type, acceptable values for Precision must be: 1 ≤ (Precision -
* Scale) ≤ 29. Values for Scale must be: 0 ≤ Scale ≤ 9. For BIGNUMERIC type, acceptable values
* for Precision must be: 1 ≤ (Precision - Scale) ≤ 38. Values for Scale must be: 0 ≤ Scale ≤
* 38.
*/
public Builder setPrecision(Long precision) {
this.precision = precision;
return this;
}
/**
* DefaultValueExpression is used to specify the default value of a field using a SQL
* expression. It can only be set for top level fields (columns).
*
* <p>You can use struct or array expression to specify default value for the entire struct or
* array. The valid SQL expressions are:
*
* <ul>
* <ul>
* <li>Literals for all data types, including STRUCT and ARRAY.
* </ul>
* <ul>
* <li>The following functions:
* <ul>
* <li>CURRENT_TIMESTAMP
* <li>CURRENT_TIME
* <li>CURRENT_DATE
* <li>CURRENT_DATETIME
* <li>GENERATE_UUID
* <li>RAND
* <li>SESSION_USER
* <li>ST_GEOGPOINT
* </ul>
* </ul>
* <ul>
* <li>Struct or array composed with the above allowed functions, for example:
* <ul>
* <li>"[CURRENT_DATE(), DATE '2020-01-01']"
* </ul>
* </ul>
* </ul>
*/
public Builder setDefaultValueExpression(String defaultValueExpression) {
this.defaultValueExpression = defaultValueExpression;
return this;
}
/** Creates a {@code Field} object. */
public Field build() {
return new Field(this);
}
}
private Field(Builder builder) {
this.name = checkNotNull(builder.name);
this.type = checkNotNull(builder.type);
this.subFields = builder.subFields;
this.mode = builder.mode;
this.description = builder.description;
this.policyTags = builder.policyTags;
this.maxLength = builder.maxLength;
this.scale = builder.scale;
this.precision = builder.precision;
this.defaultValueExpression = builder.defaultValueExpression;
}
/** Returns the field name. */
public String getName() {
return name;
}
/**
* Returns the field type.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types">Data
* Types</a>
*/
public LegacySQLTypeName getType() {
return type;
}
/** Returns the field mode. By default {@link Mode#NULLABLE} is used. */
public Mode getMode() {
return mode != null ? Mode.valueOf(mode) : null;
}
/** Returns the field description. */
public String getDescription() {
return Data.isNull(description) ? null : description;
}
/** Returns the policy tags for the field. */
public PolicyTags getPolicyTags() {
return policyTags;
}
/** Returns the maximum length of the field for STRING or BYTES type. */
public Long getMaxLength() {
return maxLength;
}
/**
* Returns the maximum number of digits set in the fractional part of a NUMERIC or BIGNUMERIC
* type.
*/
public Long getScale() {
return scale;
}
/** Returns the maximum number of total digits allowed for NUMERIC or BIGNUMERIC types. */
public Long getPrecision() {
return precision;
}
/** Return the default value of the field. */
public String getDefaultValueExpression() {
return defaultValueExpression;
}
/**
* Returns the list of sub-fields if {@link #getType()} is a {@link LegacySQLTypeName#RECORD}.
* Returns {@code null} otherwise.
*/
public FieldList getSubFields() {
return subFields;
}
/** Returns a builder for the {@code Field} object. */
public Builder toBuilder() {
return new Builder(this);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("name", name)
.add("type", type)
.add("mode", mode)
.add("description", description)
.add("policyTags", policyTags)
.add("maxLength", maxLength)
.add("scale", scale)
.add("precision", precision)
.add("defaultValueExpression", defaultValueExpression)
.toString();
}
@Override
public int hashCode() {
return Objects.hash(name, type, mode, description, policyTags);
}
@Override
public boolean equals(Object obj) {
return obj instanceof Field && Objects.equals(toPb(), ((Field) obj).toPb());
}
/** Returns a Field object with given name and type. */
public static Field of(String name, LegacySQLTypeName type, Field... subFields) {
checkArgument(!isNullOrEmpty(name), "Provided name is null or empty");
return newBuilder(name, type, subFields).build();
}
/** Returns a Field object with given name and type. */
public static Field of(String name, StandardSQLTypeName type, Field... subFields) {
checkArgument(!isNullOrEmpty(name), "Provided name is null or empty");
return newBuilder(name, type, subFields).build();
}
/** Returns a Field object with given name and type. */
public static Field of(String name, LegacySQLTypeName type, FieldList subFields) {
checkArgument(!isNullOrEmpty(name), "Provided name is null or empty");
return newBuilder(name, type, subFields).build();
}
/** Returns a Field object with given name and type. */
public static Field of(String name, StandardSQLTypeName type, FieldList subFields) {
checkArgument(!isNullOrEmpty(name), "Provided name is null or empty");
return newBuilder(name, type, subFields).build();
}
/** Returns a builder for a Field object with given name and type. */
public static Builder newBuilder(String name, LegacySQLTypeName type, Field... subFields) {
return new Builder().setName(name).setType(type, subFields);
}
/** Returns a builder for a Field object with given name and type. */
public static Builder newBuilder(String name, StandardSQLTypeName type, Field... subFields) {
return new Builder().setName(name).setType(type, subFields);
}
/** Returns a builder for a Field object with given name and type. */
public static Builder newBuilder(String name, LegacySQLTypeName type, FieldList subFields) {
return new Builder().setName(name).setType(type, subFields);
}
/** Returns a builder for a Field object with given name and type. */
public static Builder newBuilder(String name, StandardSQLTypeName type, FieldList subFields) {
return new Builder().setName(name).setType(type, subFields);
}
TableFieldSchema toPb() {
TableFieldSchema fieldSchemaPb = new TableFieldSchema();
fieldSchemaPb.setName(name);
fieldSchemaPb.setType(type.name());
if (mode != null) {
fieldSchemaPb.setMode(mode);
}
if (description != null) {
fieldSchemaPb.setDescription(description);
}
if (policyTags != null) {
fieldSchemaPb.setPolicyTags(policyTags.toPb());
}
if (maxLength != null) {
fieldSchemaPb.setMaxLength(maxLength);
}
if (scale != null) {
fieldSchemaPb.setScale(scale);
}
if (precision != null) {
fieldSchemaPb.setPrecision(precision);
}
if (defaultValueExpression != null) {
fieldSchemaPb.setDefaultValueExpression(defaultValueExpression);
}
if (getSubFields() != null) {
List<TableFieldSchema> fieldsPb = Lists.transform(getSubFields(), TO_PB_FUNCTION);
fieldSchemaPb.setFields(fieldsPb);
}
return fieldSchemaPb;
}
static Field fromPb(TableFieldSchema fieldSchemaPb) {
Builder fieldBuilder = new Builder();
fieldBuilder.setName(fieldSchemaPb.getName());
if (fieldSchemaPb.getMode() != null) {
fieldBuilder.setMode(Mode.valueOf(fieldSchemaPb.getMode()));
}
if (fieldSchemaPb.getDescription() != null) {
fieldBuilder.setDescription(fieldSchemaPb.getDescription());
}
if (fieldSchemaPb.getPolicyTags() != null) {
fieldBuilder.setPolicyTags(PolicyTags.fromPb(fieldSchemaPb.getPolicyTags()));
}
if (fieldSchemaPb.getMaxLength() != null) {
fieldBuilder.setMaxLength(fieldSchemaPb.getMaxLength());
}
if (fieldSchemaPb.getScale() != null) {
fieldBuilder.setScale(fieldSchemaPb.getScale());
}
if (fieldSchemaPb.getPrecision() != null) {
fieldBuilder.setPrecision(fieldSchemaPb.getPrecision());
}
if (fieldSchemaPb.getDefaultValueExpression() != null) {
fieldBuilder.setDefaultValueExpression(fieldSchemaPb.getDefaultValueExpression());
}
FieldList subFields =
fieldSchemaPb.getFields() != null
? FieldList.of(Lists.transform(fieldSchemaPb.getFields(), FROM_PB_FUNCTION))
: null;
fieldBuilder.setType(LegacySQLTypeName.valueOf(fieldSchemaPb.getType()), subFields);
return fieldBuilder.build();
}
}