/
SparkPositionDeltaWrite.java
651 lines (545 loc) · 25.5 KB
/
SparkPositionDeltaWrite.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
/*
* 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.spark.source;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Map;
import org.apache.iceberg.ContentFile;
import org.apache.iceberg.DataFile;
import org.apache.iceberg.DeleteFile;
import org.apache.iceberg.FileFormat;
import org.apache.iceberg.IsolationLevel;
import org.apache.iceberg.MetadataColumns;
import org.apache.iceberg.PartitionKey;
import org.apache.iceberg.PartitionSpec;
import org.apache.iceberg.Partitioning;
import org.apache.iceberg.RowDelta;
import org.apache.iceberg.Schema;
import org.apache.iceberg.SerializableTable;
import org.apache.iceberg.SnapshotSummary;
import org.apache.iceberg.SnapshotUpdate;
import org.apache.iceberg.Table;
import org.apache.iceberg.deletes.PositionDelete;
import org.apache.iceberg.expressions.Expression;
import org.apache.iceberg.expressions.Expressions;
import org.apache.iceberg.io.BasePositionDeltaWriter;
import org.apache.iceberg.io.ClusteredDataWriter;
import org.apache.iceberg.io.ClusteredPositionDeleteWriter;
import org.apache.iceberg.io.DataWriteResult;
import org.apache.iceberg.io.DeleteWriteResult;
import org.apache.iceberg.io.FanoutDataWriter;
import org.apache.iceberg.io.FileIO;
import org.apache.iceberg.io.OutputFileFactory;
import org.apache.iceberg.io.PartitioningWriter;
import org.apache.iceberg.io.PositionDeltaWriter;
import org.apache.iceberg.io.WriteResult;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.spark.SparkSchemaUtil;
import org.apache.iceberg.spark.SparkWriteConf;
import org.apache.iceberg.types.Types;
import org.apache.iceberg.util.CharSequenceSet;
import org.apache.iceberg.util.StructProjection;
import org.apache.iceberg.util.Tasks;
import org.apache.iceberg.util.ThreadPools;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.broadcast.Broadcast;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.catalyst.InternalRow;
import org.apache.spark.sql.connector.distributions.Distribution;
import org.apache.spark.sql.connector.expressions.SortOrder;
import org.apache.spark.sql.connector.iceberg.write.DeltaBatchWrite;
import org.apache.spark.sql.connector.iceberg.write.DeltaWrite;
import org.apache.spark.sql.connector.iceberg.write.DeltaWriter;
import org.apache.spark.sql.connector.iceberg.write.DeltaWriterFactory;
import org.apache.spark.sql.connector.iceberg.write.ExtendedLogicalWriteInfo;
import org.apache.spark.sql.connector.iceberg.write.RowLevelOperation.Command;
import org.apache.spark.sql.connector.write.PhysicalWriteInfo;
import org.apache.spark.sql.connector.write.RequiresDistributionAndOrdering;
import org.apache.spark.sql.connector.write.WriterCommitMessage;
import org.apache.spark.sql.types.StructType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.apache.iceberg.IsolationLevel.SERIALIZABLE;
import static org.apache.spark.sql.connector.iceberg.write.RowLevelOperation.Command.DELETE;
import static org.apache.spark.sql.connector.iceberg.write.RowLevelOperation.Command.MERGE;
import static org.apache.spark.sql.connector.iceberg.write.RowLevelOperation.Command.UPDATE;
class SparkPositionDeltaWrite implements DeltaWrite, RequiresDistributionAndOrdering {
private static final Logger LOG = LoggerFactory.getLogger(SparkPositionDeltaWrite.class);
private final JavaSparkContext sparkContext;
private final Table table;
private final Command command;
private final SparkBatchQueryScan scan;
private final IsolationLevel isolationLevel;
private final Context context;
private final String applicationId;
private final boolean wapEnabled;
private final String wapId;
private final Map<String, String> extraSnapshotMetadata;
private final Distribution requiredDistribution;
private final SortOrder[] requiredOrdering;
SparkPositionDeltaWrite(SparkSession spark, Table table, Command command, SparkBatchQueryScan scan,
IsolationLevel isolationLevel, SparkWriteConf writeConf,
ExtendedLogicalWriteInfo info, Schema dataSchema,
Distribution requiredDistribution, SortOrder[] requiredOrdering) {
this.sparkContext = JavaSparkContext.fromSparkContext(spark.sparkContext());
this.table = table;
this.command = command;
this.scan = scan;
this.isolationLevel = isolationLevel;
this.context = new Context(dataSchema, writeConf, info);
this.applicationId = spark.sparkContext().applicationId();
this.wapEnabled = writeConf.wapEnabled();
this.wapId = writeConf.wapId();
this.extraSnapshotMetadata = writeConf.extraSnapshotMetadata();
this.requiredDistribution = requiredDistribution;
this.requiredOrdering = requiredOrdering;
}
@Override
public Distribution requiredDistribution() {
return requiredDistribution;
}
@Override
public SortOrder[] requiredOrdering() {
return requiredOrdering;
}
@Override
public DeltaBatchWrite toBatch() {
return new PositionDeltaBatchWrite();
}
private static <T extends ContentFile<T>> void cleanFiles(FileIO io, Iterable<T> files) {
Tasks.foreach(files)
.executeWith(ThreadPools.getWorkerPool())
.throwFailureWhenFinished()
.noRetry()
.run(file -> io.deleteFile(file.path().toString()));
}
private class PositionDeltaBatchWrite implements DeltaBatchWrite {
@Override
public DeltaWriterFactory createBatchWriterFactory(PhysicalWriteInfo info) {
// broadcast the table metadata as the writer factory will be sent to executors
Broadcast<Table> tableBroadcast = sparkContext.broadcast(SerializableTable.copyOf(table));
return new PositionDeltaWriteFactory(tableBroadcast, command, context);
}
@Override
public void commit(WriterCommitMessage[] messages) {
RowDelta rowDelta = table.newRowDelta();
CharSequenceSet referencedDataFiles = CharSequenceSet.empty();
int addedDataFilesCount = 0;
int addedDeleteFilesCount = 0;
for (WriterCommitMessage message : messages) {
DeltaTaskCommit taskCommit = (DeltaTaskCommit) message;
for (DataFile dataFile : taskCommit.dataFiles()) {
rowDelta.addRows(dataFile);
addedDataFilesCount += 1;
}
for (DeleteFile deleteFile : taskCommit.deleteFiles()) {
rowDelta.addDeletes(deleteFile);
addedDeleteFilesCount += 1;
}
referencedDataFiles.addAll(Arrays.asList(taskCommit.referencedDataFiles()));
}
// the scan may be null if the optimizer replaces it with an empty relation (e.g. the cond is false)
// no validation is needed in this case as the command does not depend on the scanned table state
if (scan != null) {
Expression conflictDetectionFilter = conflictDetectionFilter(scan);
rowDelta.conflictDetectionFilter(conflictDetectionFilter);
rowDelta.validateDataFilesExist(referencedDataFiles);
if (scan.snapshotId() != null) {
// set the read snapshot ID to check only snapshots that happened after the table was read
// otherwise, the validation will go through all snapshots present in the table
rowDelta.validateFromSnapshot(scan.snapshotId());
}
if (command == UPDATE || command == MERGE) {
rowDelta.validateDeletedFiles();
rowDelta.validateNoConflictingDeleteFiles();
}
if (isolationLevel == SERIALIZABLE) {
rowDelta.validateNoConflictingDataFiles();
}
String commitMsg = String.format(
"position delta with %d data files and %d delete files " +
"(scanSnapshotId: %d, conflictDetectionFilter: %s, isolationLevel: %s)",
addedDataFilesCount, addedDeleteFilesCount, scan.snapshotId(), conflictDetectionFilter, isolationLevel);
commitOperation(rowDelta, commitMsg);
} else {
String commitMsg = String.format(
"position delta with %d data files and %d delete files (no validation required)",
addedDataFilesCount, addedDeleteFilesCount);
commitOperation(rowDelta, commitMsg);
}
}
private Expression conflictDetectionFilter(SparkBatchQueryScan queryScan) {
Expression filter = Expressions.alwaysTrue();
for (Expression expr : queryScan.filterExpressions()) {
filter = Expressions.and(filter, expr);
}
return filter;
}
@Override
public void abort(WriterCommitMessage[] messages) {
for (WriterCommitMessage message : messages) {
if (message != null) {
DeltaTaskCommit taskCommit = (DeltaTaskCommit) message;
cleanFiles(table.io(), Arrays.asList(taskCommit.dataFiles()));
cleanFiles(table.io(), Arrays.asList(taskCommit.deleteFiles()));
}
}
}
private void commitOperation(SnapshotUpdate<?> operation, String description) {
LOG.info("Committing {} to table {}", description, table);
if (applicationId != null) {
operation.set("spark.app.id", applicationId);
}
extraSnapshotMetadata.forEach(operation::set);
if (wapEnabled && wapId != null) {
// write-audit-publish is enabled for this table and job
// stage the changes without changing the current snapshot
operation.set(SnapshotSummary.STAGED_WAP_ID_PROP, wapId);
operation.stageOnly();
}
long start = System.currentTimeMillis();
operation.commit(); // abort is automatically called if this fails
long duration = System.currentTimeMillis() - start;
LOG.info("Committed in {} ms", duration);
}
}
public static class DeltaTaskCommit implements WriterCommitMessage {
private final DataFile[] dataFiles;
private final DeleteFile[] deleteFiles;
private final CharSequence[] referencedDataFiles;
DeltaTaskCommit(WriteResult result) {
this.dataFiles = result.dataFiles();
this.deleteFiles = result.deleteFiles();
this.referencedDataFiles = result.referencedDataFiles();
}
DeltaTaskCommit(DeleteWriteResult result) {
this.dataFiles = new DataFile[0];
this.deleteFiles = result.deleteFiles().toArray(new DeleteFile[0]);
this.referencedDataFiles = result.referencedDataFiles().toArray(new CharSequence[0]);
}
DataFile[] dataFiles() {
return dataFiles;
}
DeleteFile[] deleteFiles() {
return deleteFiles;
}
CharSequence[] referencedDataFiles() {
return referencedDataFiles;
}
}
private static class PositionDeltaWriteFactory implements DeltaWriterFactory {
private final Broadcast<Table> tableBroadcast;
private final Command command;
private final Context context;
PositionDeltaWriteFactory(Broadcast<Table> tableBroadcast, Command command, Context context) {
this.tableBroadcast = tableBroadcast;
this.command = command;
this.context = context;
}
@Override
public DeltaWriter<InternalRow> createWriter(int partitionId, long taskId) {
Table table = tableBroadcast.value();
OutputFileFactory dataFileFactory = OutputFileFactory.builderFor(table, partitionId, taskId)
.format(context.dataFileFormat())
.build();
OutputFileFactory deleteFileFactory = OutputFileFactory.builderFor(table, partitionId, taskId)
.format(context.deleteFileFormat())
.build();
SparkFileWriterFactory writerFactory = SparkFileWriterFactory.builderFor(table)
.dataFileFormat(context.dataFileFormat())
.dataSchema(context.dataSchema())
.dataSparkType(context.dataSparkType())
.deleteFileFormat(context.deleteFileFormat())
.positionDeleteSparkType(context.deleteSparkType())
.build();
if (command == DELETE) {
return new DeleteOnlyDeltaWriter(table, writerFactory, deleteFileFactory, context);
} else if (table.spec().isUnpartitioned()) {
return new UnpartitionedDeltaWriter(table, writerFactory, dataFileFactory, deleteFileFactory, context);
} else {
return new PartitionedDeltaWriter(table, writerFactory, dataFileFactory, deleteFileFactory, context);
}
}
}
private abstract static class BaseDeltaWriter implements DeltaWriter<InternalRow> {
protected InternalRowWrapper initPartitionRowWrapper(Types.StructType partitionType) {
StructType sparkPartitionType = (StructType) SparkSchemaUtil.convert(partitionType);
return new InternalRowWrapper(sparkPartitionType);
}
protected Map<Integer, StructProjection> buildPartitionProjections(Types.StructType partitionType,
Map<Integer, PartitionSpec> specs) {
Map<Integer, StructProjection> partitionProjections = Maps.newHashMap();
specs.forEach((specID, spec) ->
partitionProjections.put(specID, StructProjection.create(partitionType, spec.partitionType()))
);
return partitionProjections;
}
}
private static class DeleteOnlyDeltaWriter extends BaseDeltaWriter {
private final ClusteredPositionDeleteWriter<InternalRow> delegate;
private final PositionDelete<InternalRow> positionDelete;
private final FileIO io;
private final Map<Integer, PartitionSpec> specs;
private final InternalRowWrapper partitionRowWrapper;
private final Map<Integer, StructProjection> partitionProjections;
private final int specIdOrdinal;
private final int partitionOrdinal;
private final int fileOrdinal;
private final int positionOrdinal;
private boolean closed = false;
DeleteOnlyDeltaWriter(Table table, SparkFileWriterFactory writerFactory,
OutputFileFactory deleteFileFactory, Context context) {
this.delegate = new ClusteredPositionDeleteWriter<>(
writerFactory, deleteFileFactory, table.io(), context.targetDeleteFileSize());
this.positionDelete = PositionDelete.create();
this.io = table.io();
this.specs = table.specs();
Types.StructType partitionType = Partitioning.partitionType(table);
this.partitionRowWrapper = initPartitionRowWrapper(partitionType);
this.partitionProjections = buildPartitionProjections(partitionType, specs);
this.specIdOrdinal = context.metadataSparkType().fieldIndex(MetadataColumns.SPEC_ID.name());
this.partitionOrdinal = context.metadataSparkType().fieldIndex(MetadataColumns.PARTITION_COLUMN_NAME);
this.fileOrdinal = context.deleteSparkType().fieldIndex(MetadataColumns.FILE_PATH.name());
this.positionOrdinal = context.deleteSparkType().fieldIndex(MetadataColumns.ROW_POSITION.name());
}
@Override
public void delete(InternalRow metadata, InternalRow id) throws IOException {
int specId = metadata.getInt(specIdOrdinal);
PartitionSpec spec = specs.get(specId);
InternalRow partition = metadata.getStruct(partitionOrdinal, partitionRowWrapper.size());
StructProjection partitionProjection = partitionProjections.get(specId);
partitionProjection.wrap(partitionRowWrapper.wrap(partition));
String file = id.getString(fileOrdinal);
long position = id.getLong(positionOrdinal);
positionDelete.set(file, position, null);
delegate.write(positionDelete, spec, partitionProjection);
}
@Override
public void update(InternalRow metadata, InternalRow id, InternalRow row) {
throw new UnsupportedOperationException(this.getClass().getName() + " does not implement update");
}
@Override
public void insert(InternalRow row) throws IOException {
throw new UnsupportedOperationException(this.getClass().getName() + " does not implement insert");
}
@Override
public WriterCommitMessage commit() throws IOException {
close();
DeleteWriteResult result = delegate.result();
return new DeltaTaskCommit(result);
}
@Override
public void abort() throws IOException {
close();
DeleteWriteResult result = delegate.result();
cleanFiles(io, result.deleteFiles());
}
@Override
public void close() throws IOException {
if (!closed) {
delegate.close();
this.closed = true;
}
}
}
@SuppressWarnings("checkstyle:VisibilityModifier")
private abstract static class DeleteAndDataDeltaWriter extends BaseDeltaWriter {
protected final PositionDeltaWriter<InternalRow> delegate;
private final FileIO io;
private final Map<Integer, PartitionSpec> specs;
private final InternalRowWrapper deletePartitionRowWrapper;
private final Map<Integer, StructProjection> deletePartitionProjections;
private final int specIdOrdinal;
private final int partitionOrdinal;
private final int fileOrdinal;
private final int positionOrdinal;
private boolean closed = false;
DeleteAndDataDeltaWriter(Table table, SparkFileWriterFactory writerFactory,
OutputFileFactory dataFileFactory, OutputFileFactory deleteFileFactory,
Context context) {
this.delegate = new BasePositionDeltaWriter<>(
newInsertWriter(table, writerFactory, dataFileFactory, context),
newUpdateWriter(table, writerFactory, dataFileFactory, context),
newDeleteWriter(table, writerFactory, deleteFileFactory, context));
this.io = table.io();
this.specs = table.specs();
Types.StructType partitionType = Partitioning.partitionType(table);
this.deletePartitionRowWrapper = initPartitionRowWrapper(partitionType);
this.deletePartitionProjections = buildPartitionProjections(partitionType, specs);
this.specIdOrdinal = context.metadataSparkType().fieldIndex(MetadataColumns.SPEC_ID.name());
this.partitionOrdinal = context.metadataSparkType().fieldIndex(MetadataColumns.PARTITION_COLUMN_NAME);
this.fileOrdinal = context.deleteSparkType().fieldIndex(MetadataColumns.FILE_PATH.name());
this.positionOrdinal = context.deleteSparkType().fieldIndex(MetadataColumns.ROW_POSITION.name());
}
@Override
public void delete(InternalRow meta, InternalRow id) throws IOException {
int specId = meta.getInt(specIdOrdinal);
PartitionSpec spec = specs.get(specId);
InternalRow partition = meta.getStruct(partitionOrdinal, deletePartitionRowWrapper.size());
StructProjection partitionProjection = deletePartitionProjections.get(specId);
partitionProjection.wrap(deletePartitionRowWrapper.wrap(partition));
String file = id.getString(fileOrdinal);
long position = id.getLong(positionOrdinal);
delegate.delete(file, position, spec, partitionProjection);
}
@Override
public WriterCommitMessage commit() throws IOException {
close();
WriteResult result = delegate.result();
return new DeltaTaskCommit(result);
}
@Override
public void abort() throws IOException {
close();
WriteResult result = delegate.result();
cleanFiles(io, Arrays.asList(result.dataFiles()));
cleanFiles(io, Arrays.asList(result.deleteFiles()));
}
@Override
public void close() throws IOException {
if (!closed) {
delegate.close();
this.closed = true;
}
}
private PartitioningWriter<InternalRow, DataWriteResult> newInsertWriter(Table table,
SparkFileWriterFactory writerFactory,
OutputFileFactory fileFactory,
Context context) {
long targetFileSize = context.targetDataFileSize();
if (table.spec().isPartitioned() && context.fanoutWriterEnabled()) {
return new FanoutDataWriter<>(writerFactory, fileFactory, table.io(), targetFileSize);
} else {
return new ClusteredDataWriter<>(writerFactory, fileFactory, table.io(), targetFileSize);
}
}
private PartitioningWriter<InternalRow, DataWriteResult> newUpdateWriter(Table table,
SparkFileWriterFactory writerFactory,
OutputFileFactory fileFactory,
Context context) {
long targetFileSize = context.targetDataFileSize();
if (table.spec().isPartitioned()) {
// use a fanout writer for partitioned tables to write updates as they may be out of order
return new FanoutDataWriter<>(writerFactory, fileFactory, table.io(), targetFileSize);
} else {
return new ClusteredDataWriter<>(writerFactory, fileFactory, table.io(), targetFileSize);
}
}
private ClusteredPositionDeleteWriter<InternalRow> newDeleteWriter(Table table,
SparkFileWriterFactory writerFactory,
OutputFileFactory fileFactory,
Context context) {
long targetFileSize = context.targetDeleteFileSize();
return new ClusteredPositionDeleteWriter<>(writerFactory, fileFactory, table.io(), targetFileSize);
}
}
private static class UnpartitionedDeltaWriter extends DeleteAndDataDeltaWriter {
private final PartitionSpec dataSpec;
UnpartitionedDeltaWriter(Table table, SparkFileWriterFactory writerFactory,
OutputFileFactory dataFileFactory, OutputFileFactory deleteFileFactory,
Context context) {
super(table, writerFactory, dataFileFactory, deleteFileFactory, context);
this.dataSpec = table.spec();
}
@Override
public void update(InternalRow meta, InternalRow id, InternalRow row) throws IOException {
delete(meta, id);
delegate.update(row, dataSpec, null);
}
@Override
public void insert(InternalRow row) throws IOException {
delegate.insert(row, dataSpec, null);
}
}
private static class PartitionedDeltaWriter extends DeleteAndDataDeltaWriter {
private final PartitionSpec dataSpec;
private final PartitionKey dataPartitionKey;
private final InternalRowWrapper internalRowDataWrapper;
PartitionedDeltaWriter(Table table, SparkFileWriterFactory writerFactory,
OutputFileFactory dataFileFactory, OutputFileFactory deleteFileFactory,
Context context) {
super(table, writerFactory, dataFileFactory, deleteFileFactory, context);
this.dataSpec = table.spec();
this.dataPartitionKey = new PartitionKey(dataSpec, context.dataSchema());
this.internalRowDataWrapper = new InternalRowWrapper(context.dataSparkType());
}
@Override
public void update(InternalRow meta, InternalRow id, InternalRow row) throws IOException {
delete(meta, id);
dataPartitionKey.partition(internalRowDataWrapper.wrap(row));
delegate.update(row, dataSpec, dataPartitionKey);
}
@Override
public void insert(InternalRow row) throws IOException {
dataPartitionKey.partition(internalRowDataWrapper.wrap(row));
delegate.insert(row, dataSpec, dataPartitionKey);
}
}
// a serializable helper class for common parameters required to configure writers
private static class Context implements Serializable {
private final Schema dataSchema;
private final StructType dataSparkType;
private final FileFormat dataFileFormat;
private final long targetDataFileSize;
private final StructType deleteSparkType;
private final StructType metadataSparkType;
private final FileFormat deleteFileFormat;
private final long targetDeleteFileSize;
private final boolean fanoutWriterEnabled;
Context(Schema dataSchema, SparkWriteConf writeConf, ExtendedLogicalWriteInfo info) {
this.dataSchema = dataSchema;
this.dataSparkType = info.schema();
this.dataFileFormat = writeConf.dataFileFormat();
this.targetDataFileSize = writeConf.targetDataFileSize();
this.deleteSparkType = info.rowIdSchema();
this.deleteFileFormat = writeConf.deleteFileFormat();
this.targetDeleteFileSize = writeConf.targetDeleteFileSize();
this.metadataSparkType = info.metadataSchema();
this.fanoutWriterEnabled = writeConf.fanoutWriterEnabled();
}
Schema dataSchema() {
return dataSchema;
}
StructType dataSparkType() {
return dataSparkType;
}
FileFormat dataFileFormat() {
return dataFileFormat;
}
long targetDataFileSize() {
return targetDataFileSize;
}
StructType deleteSparkType() {
return deleteSparkType;
}
StructType metadataSparkType() {
return metadataSparkType;
}
FileFormat deleteFileFormat() {
return deleteFileFormat;
}
long targetDeleteFileSize() {
return targetDeleteFileSize;
}
boolean fanoutWriterEnabled() {
return fanoutWriterEnabled;
}
}
}