Repository navigation
Expand file tree
/
Copy pathSchemaUpdate.java
More file actions
881 lines (759 loc) · 30.7 KB
/
Copy pathSchemaUpdate.java
File metadata and controls
881 lines (759 loc) · 30.7 KB
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.iceberg;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.iceberg.expressions.Literal;
import org.apache.iceberg.mapping.MappingUtil;
import org.apache.iceberg.mapping.NameMapping;
import org.apache.iceberg.mapping.NameMappingParser;
import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;
import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.relocated.com.google.common.collect.Multimap;
import org.apache.iceberg.relocated.com.google.common.collect.Multimaps;
import org.apache.iceberg.relocated.com.google.common.collect.Sets;
import org.apache.iceberg.schema.UnionByNameVisitor;
import org.apache.iceberg.types.Type;
import org.apache.iceberg.types.TypeUtil;
import org.apache.iceberg.types.Types;
import org.apache.iceberg.util.PropertyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** Schema evolution API implementation. */
class SchemaUpdate implements UpdateSchema {
private static final Logger LOG = LoggerFactory.getLogger(SchemaUpdate.class);
private static final int TABLE_ROOT_ID = -1;
private final TableOperations ops;
private final TableMetadata base;
private final Schema schema;
private final Map<Integer, Integer> idToParent;
private final List<Integer> deletes = Lists.newArrayList();
private final Map<Integer, Types.NestedField> updates = Maps.newHashMap();
private final Multimap<Integer, Integer> parentToAddedIds =
Multimaps.newListMultimap(Maps.newHashMap(), Lists::newArrayList);
private final Map<String, Integer> addedNameToId = Maps.newHashMap();
private final Multimap<Integer, Move> moves =
Multimaps.newListMultimap(Maps.newHashMap(), Lists::newArrayList);
private int lastColumnId;
private boolean allowIncompatibleChanges = false;
private Set<String> identifierFieldNames;
private boolean caseSensitive = true;
SchemaUpdate(TableOperations ops) {
this(ops, ops.current());
}
@VisibleForTesting
SchemaUpdate(Schema schema, int lastColumnId) {
this(null, null, schema, lastColumnId);
}
private SchemaUpdate(TableOperations ops, TableMetadata base) {
this(ops, base, base.schema(), base.lastColumnId());
}
private SchemaUpdate(TableOperations ops, TableMetadata base, Schema schema, int lastColumnId) {
this.ops = ops;
this.base = base;
this.schema = schema;
this.lastColumnId = lastColumnId;
this.idToParent = Maps.newHashMap(TypeUtil.indexParents(schema.asStruct()));
this.identifierFieldNames = schema.identifierFieldNames();
}
@Override
public SchemaUpdate allowIncompatibleChanges() {
this.allowIncompatibleChanges = true;
return this;
}
@Override
public UpdateSchema addColumn(
String parent, String name, Type type, String doc, Literal<?> defaultValue) {
internalAddColumn(parent, name, true, type, doc, defaultValue);
return this;
}
@Override
public UpdateSchema addRequiredColumn(
String parent, String name, Type type, String doc, Literal<?> defaultValue) {
internalAddColumn(parent, name, false, type, doc, defaultValue);
return this;
}
private void internalAddColumn(
String parent,
String name,
boolean isOptional,
Type type,
String doc,
Literal<?> defaultValue) {
int parentId = TABLE_ROOT_ID;
String fullName;
if (parent != null) {
Types.NestedField parentField = findField(parent);
Preconditions.checkArgument(parentField != null, "Cannot find parent struct: %s", parent);
Type parentType = parentField.type();
if (parentType.isNestedType()) {
Type.NestedType nested = parentType.asNestedType();
if (nested.isMapType()) {
// fields are added to the map value type
parentField = nested.asMapType().fields().get(1);
} else if (nested.isListType()) {
// fields are added to the element type
parentField = nested.asListType().fields().get(0);
}
}
Preconditions.checkArgument(
parentField.type().isNestedType() && parentField.type().asNestedType().isStructType(),
"Cannot add to non-struct column: %s: %s",
parent,
parentField.type());
parentId = parentField.fieldId();
Types.NestedField currentField = findField(parent + "." + name);
Preconditions.checkArgument(
!deletes.contains(parentId), "Cannot add to a column that will be deleted: %s", parent);
Preconditions.checkArgument(
currentField == null || deletes.contains(currentField.fieldId()),
"Cannot add column, name already exists: %s.%s",
parent,
name);
fullName = schema.findColumnName(parentId) + "." + name;
} else {
Types.NestedField currentField = findField(name);
Preconditions.checkArgument(
currentField == null || deletes.contains(currentField.fieldId()),
"Cannot add column, name already exists: %s",
name);
fullName = name;
}
Preconditions.checkArgument(
defaultValue != null || isOptional || allowIncompatibleChanges,
"Incompatible change: cannot add required column without a default value: %s",
fullName);
// assign new IDs in order
int newId = assignNewColumnId();
// update tracking for moves
addedNameToId.put(caseSensitivityAwareName(fullName), newId);
if (parentId != TABLE_ROOT_ID) {
idToParent.put(newId, parentId);
}
Types.NestedField newField =
Types.NestedField.builder()
.withName(name)
.isOptional(isOptional)
.withId(newId)
.ofType(TypeUtil.assignFreshIds(type, this::assignNewColumnId))
.withDoc(doc)
.withInitialDefault(defaultValue)
.withWriteDefault(defaultValue)
.build();
updates.put(newId, newField);
parentToAddedIds.put(parentId, newId);
}
@Override
public UpdateSchema deleteColumn(String name) {
Types.NestedField field = findField(name);
Preconditions.checkArgument(field != null, "Cannot delete missing column: %s", name);
Preconditions.checkArgument(
!parentToAddedIds.containsKey(field.fieldId()),
"Cannot delete a column that has additions: %s",
name);
Preconditions.checkArgument(
!updates.containsKey(field.fieldId()), "Cannot delete a column that has updates: %s", name);
deletes.add(field.fieldId());
return this;
}
@Override
public UpdateSchema renameColumn(String name, String newName) {
Types.NestedField field = findField(name);
Preconditions.checkArgument(field != null, "Cannot rename missing column: %s", name);
Preconditions.checkArgument(newName != null, "Cannot rename a column to null");
Preconditions.checkArgument(
!deletes.contains(field.fieldId()),
"Cannot rename a column that will be deleted: %s",
field.name());
// merge with an update, if present
int fieldId = field.fieldId();
Types.NestedField update = updates.get(fieldId);
Types.NestedField newField =
Types.NestedField.from(update != null ? update : field).withName(newName).build();
updates.put(fieldId, newField);
if (identifierFieldNames.contains(name)) {
identifierFieldNames.remove(name);
identifierFieldNames.add(newName);
}
return this;
}
@Override
public UpdateSchema requireColumn(String name) {
internalUpdateColumnRequirement(name, false);
return this;
}
@Override
public UpdateSchema makeColumnOptional(String name) {
internalUpdateColumnRequirement(name, true);
return this;
}
private void internalUpdateColumnRequirement(String name, boolean isOptional) {
Types.NestedField field = findForUpdate(name);
Preconditions.checkArgument(field != null, "Cannot update missing column: %s", name);
if ((!isOptional && field.isRequired()) || (isOptional && field.isOptional())) {
// if the change is a noop, allow it even if allowIncompatibleChanges is false
return;
}
boolean isDefaultedAdd = isAdded(name) && field.initialDefault() != null;
Preconditions.checkArgument(
isOptional || isDefaultedAdd || allowIncompatibleChanges,
"Cannot change column nullability: %s: optional -> required",
name);
Preconditions.checkArgument(
!deletes.contains(field.fieldId()),
"Cannot update a column that will be deleted: %s",
field.name());
int fieldId = field.fieldId();
Types.NestedField.Builder builder = Types.NestedField.from(field);
if (isOptional) {
builder.asOptional();
} else {
builder.asRequired();
}
updates.put(fieldId, builder.build());
}
@Override
public UpdateSchema updateColumn(String name, Type.PrimitiveType newType) {
Types.NestedField field = findForUpdate(name);
Preconditions.checkArgument(field != null, "Cannot update missing column: %s", name);
Preconditions.checkArgument(
!deletes.contains(field.fieldId()),
"Cannot update a column that will be deleted: %s",
field.name());
if (field.type().equals(newType)) {
return this;
}
Preconditions.checkArgument(
TypeUtil.isPromotionAllowed(field.type(), newType),
"Cannot change column type: %s: %s -> %s",
name,
field.type(),
newType);
// merge with a rename, if present
int fieldId = field.fieldId();
Types.NestedField newField = Types.NestedField.from(field).ofType(newType).build();
updates.put(fieldId, newField);
return this;
}
@Override
public UpdateSchema updateColumnDoc(String name, String doc) {
Types.NestedField field = findForUpdate(name);
Preconditions.checkArgument(field != null, "Cannot update missing column: %s", name);
Preconditions.checkArgument(
!deletes.contains(field.fieldId()),
"Cannot update a column that will be deleted: %s",
field.name());
if (Objects.equals(field.doc(), doc)) {
return this;
}
// merge with a rename or update, if present
int fieldId = field.fieldId();
Types.NestedField newField = Types.NestedField.from(field).withDoc(doc).build();
updates.put(fieldId, newField);
return this;
}
@Override
public UpdateSchema updateColumnDefault(String name, Literal<?> newDefault) {
Types.NestedField field = findForUpdate(name);
Preconditions.checkArgument(field != null, "Cannot update missing column: %s", name);
Preconditions.checkArgument(
!deletes.contains(field.fieldId()),
"Cannot update a column that will be deleted: %s",
field.name());
// if the value can be converted to the expected type, check if it is already set
// if it can't be converted, the builder will throw an exception
Literal<?> converted = newDefault != null ? newDefault.to(field.type()) : null;
if (converted != null && Objects.equals(field.writeDefault(), converted.value())) {
return this;
}
// write default is always set and initial default is only set if the field requires one
int fieldId = field.fieldId();
Types.NestedField newField = Types.NestedField.from(field).withWriteDefault(newDefault).build();
updates.put(fieldId, newField);
return this;
}
@Override
public UpdateSchema moveFirst(String name) {
Integer fieldId = findForMove(name);
Preconditions.checkArgument(fieldId != null, "Cannot move missing column: %s", name);
internalMove(name, Move.first(fieldId));
return this;
}
@Override
public UpdateSchema moveBefore(String name, String beforeName) {
Integer fieldId = findForMove(name);
Preconditions.checkArgument(fieldId != null, "Cannot move missing column: %s", name);
Integer beforeId = findForMove(beforeName);
Preconditions.checkArgument(
beforeId != null, "Cannot move %s before missing column: %s", name, beforeName);
Preconditions.checkArgument(!fieldId.equals(beforeId), "Cannot move %s before itself", name);
internalMove(name, Move.before(fieldId, beforeId));
return this;
}
@Override
public UpdateSchema moveAfter(String name, String afterName) {
Integer fieldId = findForMove(name);
Preconditions.checkArgument(fieldId != null, "Cannot move missing column: %s", name);
Integer afterId = findForMove(afterName);
Preconditions.checkArgument(
afterId != null, "Cannot move %s after missing column: %s", name, afterName);
Preconditions.checkArgument(!fieldId.equals(afterId), "Cannot move %s after itself", name);
internalMove(name, Move.after(fieldId, afterId));
return this;
}
@Override
public UpdateSchema unionByNameWith(Schema newSchema) {
UnionByNameVisitor.visit(this, schema, newSchema, caseSensitive);
return this;
}
@Override
public UpdateSchema setIdentifierFields(Collection<String> names) {
this.identifierFieldNames = Sets.newHashSet(names);
return this;
}
@Override
public UpdateSchema caseSensitive(boolean caseSensitivity) {
this.caseSensitive = caseSensitivity;
return this;
}
private boolean isAdded(String name) {
return addedNameToId.containsKey(caseSensitivityAwareName(name));
}
private Types.NestedField findForUpdate(String name) {
Types.NestedField existing = findField(name);
if (existing != null) {
Types.NestedField pendingUpdate = updates.get(existing.fieldId());
if (pendingUpdate != null) {
return pendingUpdate;
}
return existing;
}
Integer addedId = addedNameToId.get(caseSensitivityAwareName(name));
if (addedId != null) {
return updates.get(addedId);
}
return null;
}
private Integer findForMove(String name) {
Integer addedId = addedNameToId.get(caseSensitivityAwareName(name));
if (addedId != null) {
return addedId;
}
Types.NestedField field = findField(name);
if (field != null) {
return field.fieldId();
}
return null;
}
private void internalMove(String name, Move move) {
Integer parentId = idToParent.get(move.fieldId());
if (parentId != null) {
Types.NestedField parent = schema.findField(parentId);
Preconditions.checkArgument(
parent.type().isStructType(), "Cannot move fields in non-struct type: %s", parent.type());
if (move.type() == Move.MoveType.AFTER || move.type() == Move.MoveType.BEFORE) {
Preconditions.checkArgument(
parentId.equals(idToParent.get(move.referenceFieldId())),
"Cannot move field %s to a different struct",
name);
}
moves.put(parentId, move);
} else {
if (move.type() == Move.MoveType.AFTER || move.type() == Move.MoveType.BEFORE) {
Preconditions.checkArgument(
idToParent.get(move.referenceFieldId()) == null,
"Cannot move field %s to a different struct",
name);
}
moves.put(TABLE_ROOT_ID, move);
}
}
/**
* Apply the pending changes to the original schema and returns the result.
*
* <p>This does not result in a permanent update.
*
* @return the result Schema when all pending updates are applied
*/
@Override
public Schema apply() {
return applyChanges(
schema, deletes, updates, parentToAddedIds, moves, identifierFieldNames, caseSensitive);
}
@Override
public void commit() {
TableMetadata update = applyChangesToMetadata(base.updateSchema(apply()));
ops.commit(base, update);
}
private int assignNewColumnId() {
int next = lastColumnId + 1;
this.lastColumnId = next;
return next;
}
private TableMetadata applyChangesToMetadata(TableMetadata metadata) {
String mappingJson = metadata.property(TableProperties.DEFAULT_NAME_MAPPING, null);
TableMetadata newMetadata = metadata;
if (mappingJson != null) {
try {
// parse and update the mapping
NameMapping mapping = NameMappingParser.fromJson(mappingJson);
NameMapping updated = MappingUtil.update(mapping, updates, parentToAddedIds);
// replace the table property
Map<String, String> updatedProperties = Maps.newHashMap();
updatedProperties.putAll(metadata.properties());
updatedProperties.put(
TableProperties.DEFAULT_NAME_MAPPING, NameMappingParser.toJson(updated));
newMetadata = metadata.replaceProperties(updatedProperties);
} catch (RuntimeException e) {
// log the error, but do not fail the update
LOG.warn("Failed to update external schema mapping: {}", mappingJson, e);
}
}
// Transform the metrics if they exist
if (base != null && base.properties() != null) {
Schema newSchema = newMetadata.schema();
List<String> deletedColumns =
deletes.stream().map(schema::findColumnName).collect(Collectors.toList());
Map<String, String> renamedColumns =
updates.keySet().stream()
.filter(id -> !addedNameToId.containsValue(id)) // remove added columns
.filter(id -> !schema.findColumnName(id).equals(newSchema.findColumnName(id)))
.collect(Collectors.toMap(schema::findColumnName, newSchema::findColumnName));
if (!deletedColumns.isEmpty() || !renamedColumns.isEmpty()) {
Set<String> columnProperties =
ImmutableSet.of(
TableProperties.METRICS_MODE_COLUMN_CONF_PREFIX,
TableProperties.PARQUET_BLOOM_FILTER_COLUMN_ENABLED_PREFIX,
TableProperties.PARQUET_COLUMN_STATS_ENABLED_PREFIX,
TableProperties.PARQUET_COLUMN_COMPRESSION_CODEC_PREFIX,
TableProperties.PARQUET_COLUMN_COMPRESSION_LEVEL_PREFIX);
Map<String, String> updatedProperties =
PropertyUtil.applySchemaChanges(
newMetadata.properties(), deletedColumns, renamedColumns, columnProperties);
newMetadata = newMetadata.replaceProperties(updatedProperties);
}
}
return newMetadata;
}
private static Schema applyChanges(
Schema schema,
List<Integer> deletes,
Map<Integer, Types.NestedField> updates,
Multimap<Integer, Integer> parentToAddedIds,
Multimap<Integer, Move> moves,
Set<String> identifierFieldNames,
boolean caseSensitive) {
// validate existing identifier fields are not deleted
Map<Integer, Integer> idToParent = TypeUtil.indexParents(schema.asStruct());
for (String name : identifierFieldNames) {
Types.NestedField field =
caseSensitive ? schema.findField(name) : schema.caseInsensitiveFindField(name);
if (field != null) {
Preconditions.checkArgument(
!deletes.contains(field.fieldId()),
"Cannot delete identifier field %s. To force deletion, "
+ "also call setIdentifierFields to update identifier fields.",
field);
Integer parentId = idToParent.get(field.fieldId());
while (parentId != null) {
Preconditions.checkArgument(
!deletes.contains(parentId),
"Cannot delete field %s as it will delete nested identifier field %s",
schema.findField(parentId),
field);
parentId = idToParent.get(parentId);
}
}
}
// apply schema changes
Types.StructType struct =
TypeUtil.visit(schema, new ApplyChanges(deletes, updates, parentToAddedIds, moves))
.asNestedType()
.asStructType();
// validate identifier requirements based on the latest schema
Map<String, Integer> nameToId =
caseSensitive ? TypeUtil.indexByName(struct) : TypeUtil.indexByLowerCaseName(struct);
Set<Integer> freshIdentifierFieldIds = Sets.newHashSet();
for (String name : identifierFieldNames) {
Preconditions.checkArgument(
nameToId.containsKey(name),
"Cannot add field %s as an identifier field: not found in current schema or added columns",
name);
freshIdentifierFieldIds.add(nameToId.get(name));
}
Map<Integer, Types.NestedField> idToField = TypeUtil.indexById(struct);
freshIdentifierFieldIds.forEach(
id -> Schema.validateIdentifierField(id, idToField, idToParent));
return new Schema(struct.fields(), freshIdentifierFieldIds);
}
private static class ApplyChanges extends TypeUtil.SchemaVisitor<Type> {
private final List<Integer> deletes;
private final Map<Integer, Types.NestedField> updates;
private final Multimap<Integer, Integer> parentToAddedIds;
private final Multimap<Integer, Move> moves;
private ApplyChanges(
List<Integer> deletes,
Map<Integer, Types.NestedField> updates,
Multimap<Integer, Integer> parentToAddedIds,
Multimap<Integer, Move> moves) {
this.deletes = deletes;
this.updates = updates;
this.parentToAddedIds = parentToAddedIds;
this.moves = moves;
}
@Override
public Type schema(Schema schema, Type structResult) {
List<Types.NestedField> addedFields =
parentToAddedIds.get(TABLE_ROOT_ID).stream()
.map(updates::get)
.collect(Collectors.toList());
List<Types.NestedField> fields =
addAndMoveFields(
structResult.asStructType().fields(), addedFields, moves.get(TABLE_ROOT_ID));
if (fields != null) {
return Types.StructType.of(fields);
}
return structResult;
}
@Override
public Type struct(Types.StructType struct, List<Type> fieldResults) {
boolean hasChange = false;
List<Types.NestedField> newFields = Lists.newArrayListWithExpectedSize(fieldResults.size());
for (int i = 0; i < fieldResults.size(); i += 1) {
Type resultType = fieldResults.get(i);
if (resultType == null) {
hasChange = true;
continue;
}
Types.NestedField field = struct.fields().get(i);
Types.NestedField update = updates.get(field.fieldId());
Types.NestedField updated =
Types.NestedField.from(update != null ? update : field).ofType(resultType).build();
if (field.equals(updated)) {
newFields.add(field);
} else {
hasChange = true;
newFields.add(updated);
}
}
if (hasChange) {
// TODO: What happens if there are no fields left?
return Types.StructType.of(newFields);
}
return struct;
}
@Override
public Type field(Types.NestedField field, Type fieldResult) {
// the API validates deletes, updates, and additions don't conflict
// handle deletes
int fieldId = field.fieldId();
if (deletes.contains(fieldId)) {
return null;
}
// handle updates
Types.NestedField update = updates.get(field.fieldId());
if (update != null && update.type() != field.type()) {
// rename is handled in struct, but struct needs the correct type from the field result
return update.type();
}
// handle adds
Collection<Types.NestedField> newFields =
parentToAddedIds.get(fieldId).stream().map(updates::get).collect(Collectors.toList());
Collection<Move> columnsToMove = moves.get(fieldId);
if (!newFields.isEmpty() || !columnsToMove.isEmpty()) {
// if either collection is non-null, then this must be a struct type. try to apply the
// changes
List<Types.NestedField> fields =
addAndMoveFields(fieldResult.asStructType().fields(), newFields, columnsToMove);
if (fields != null) {
return Types.StructType.of(fields);
}
}
return fieldResult;
}
@Override
public Type list(Types.ListType list, Type elementResult) {
// use field to apply updates
Types.NestedField elementField = list.fields().get(0);
Type elementType = field(elementField, elementResult);
if (elementType == null) {
throw new IllegalArgumentException("Cannot delete element type from list: " + list);
}
Types.NestedField elementUpdate = updates.get(elementField.fieldId());
boolean isElementOptional =
elementUpdate != null ? elementUpdate.isOptional() : list.isElementOptional();
if (isElementOptional == elementField.isOptional() && list.elementType() == elementType) {
return list;
}
if (isElementOptional) {
return Types.ListType.ofOptional(list.elementId(), elementType);
} else {
return Types.ListType.ofRequired(list.elementId(), elementType);
}
}
@Override
public Type map(Types.MapType map, Type kResult, Type valueResult) {
// if any updates are intended for the key, throw an exception
int keyId = map.fields().get(0).fieldId();
if (deletes.contains(keyId)) {
throw new IllegalArgumentException("Cannot delete map keys: " + map);
} else if (updates.containsKey(keyId)) {
throw new IllegalArgumentException("Cannot update map keys: " + map);
} else if (parentToAddedIds.containsKey(keyId)) {
throw new IllegalArgumentException("Cannot add fields to map keys: " + map);
} else if (!map.keyType().equals(kResult)) {
throw new IllegalArgumentException("Cannot alter map keys: " + map);
}
// use field to apply updates to the value
Types.NestedField valueField = map.fields().get(1);
Type valueType = field(valueField, valueResult);
if (valueType == null) {
throw new IllegalArgumentException("Cannot delete value type from map: " + map);
}
Types.NestedField valueUpdate = updates.get(valueField.fieldId());
boolean isValueOptional =
valueUpdate != null ? valueUpdate.isOptional() : map.isValueOptional();
if (isValueOptional == map.isValueOptional() && map.valueType() == valueType) {
return map;
}
if (isValueOptional) {
return Types.MapType.ofOptional(map.keyId(), map.valueId(), map.keyType(), valueType);
} else {
return Types.MapType.ofRequired(map.keyId(), map.valueId(), map.keyType(), valueType);
}
}
@Override
public Type variant(Types.VariantType variant) {
return variant;
}
@Override
public Type primitive(Type.PrimitiveType primitive) {
return primitive;
}
}
private static List<Types.NestedField> addAndMoveFields(
List<Types.NestedField> fields, Collection<Types.NestedField> adds, Collection<Move> moves) {
if (adds != null && !adds.isEmpty()) {
if (moves != null && !moves.isEmpty()) {
// always apply adds first so that added fields can be moved
return moveFields(addFields(fields, adds), moves);
} else {
return addFields(fields, adds);
}
} else if (moves != null && !moves.isEmpty()) {
return moveFields(fields, moves);
}
return null;
}
private static List<Types.NestedField> addFields(
List<Types.NestedField> fields, Collection<Types.NestedField> adds) {
List<Types.NestedField> newFields = Lists.newArrayList(fields);
newFields.addAll(adds);
return newFields;
}
@SuppressWarnings({"checkstyle:IllegalType", "JdkObsolete"})
private static List<Types.NestedField> moveFields(
List<Types.NestedField> fields, Collection<Move> moves) {
LinkedList<Types.NestedField> reordered = Lists.newLinkedList(fields);
for (Move move : moves) {
Types.NestedField toMove =
Iterables.find(reordered, field -> field.fieldId() == move.fieldId());
reordered.remove(toMove);
switch (move.type()) {
case FIRST:
reordered.addFirst(toMove);
break;
case BEFORE:
Types.NestedField before =
Iterables.find(reordered, field -> field.fieldId() == move.referenceFieldId());
int beforeIndex = reordered.indexOf(before);
// insert the new node at the index of the existing node
reordered.add(beforeIndex, toMove);
break;
case AFTER:
Types.NestedField after =
Iterables.find(reordered, field -> field.fieldId() == move.referenceFieldId());
int afterIndex = reordered.indexOf(after);
reordered.add(afterIndex + 1, toMove);
break;
default:
throw new UnsupportedOperationException("Unknown move type: " + move.type());
}
}
return reordered;
}
/** Represents a requested column move in a struct. */
private static class Move {
private enum MoveType {
FIRST,
BEFORE,
AFTER
}
@Override
public String toString() {
String suffix = "";
if (type != MoveType.FIRST) {
suffix = " field " + referenceFieldId;
}
return "Move column " + fieldId + " " + type.toString() + suffix;
}
static Move first(int fieldId) {
return new Move(fieldId, -1, MoveType.FIRST);
}
static Move before(int fieldId, int referenceFieldId) {
return new Move(fieldId, referenceFieldId, MoveType.BEFORE);
}
static Move after(int fieldId, int referenceFieldId) {
return new Move(fieldId, referenceFieldId, MoveType.AFTER);
}
private final int fieldId;
private final int referenceFieldId;
private final MoveType type;
private Move(int fieldId, int referenceFieldId, MoveType type) {
this.fieldId = fieldId;
this.referenceFieldId = referenceFieldId;
this.type = type;
}
public int fieldId() {
return fieldId;
}
public int referenceFieldId() {
return referenceFieldId;
}
public MoveType type() {
return type;
}
}
private Types.NestedField findField(String fieldName) {
return caseSensitive ? schema.findField(fieldName) : schema.caseInsensitiveFindField(fieldName);
}
private String caseSensitivityAwareName(String name) {
return caseSensitive ? name : name.toLowerCase(Locale.ROOT);
}
}