|
22 | 22 | import org.apache.paimon.codegen.Projection; |
23 | 23 | import org.apache.paimon.data.BinaryRow; |
24 | 24 | import org.apache.paimon.data.InternalRow; |
| 25 | +import org.apache.paimon.data.InternalRow.FieldGetter; |
25 | 26 | import org.apache.paimon.table.SpecialFields; |
26 | 27 | import org.apache.paimon.types.RowType; |
27 | 28 |
|
28 | 29 | import javax.annotation.Nullable; |
29 | 30 |
|
| 31 | +import java.io.Serializable; |
30 | 32 | import java.util.List; |
31 | 33 |
|
32 | 34 | /** The extractor to get partition, index field and row id from records. */ |
33 | | -public class RowIdIndexFieldsExtractor { |
| 35 | +public class RowIdIndexFieldsExtractor implements Serializable { |
| 36 | + |
| 37 | + private static final long serialVersionUID = 1L; |
34 | 38 |
|
35 | | - private final Projection partitionProjection; |
36 | | - private final InternalRow.FieldGetter indexFieldGetter; |
37 | 39 | private final int rowIdPos; |
| 40 | + private final RowType readType; |
| 41 | + private final List<String> partitionKeys; |
| 42 | + private final String indexField; |
| 43 | + |
| 44 | + private transient Projection lazyPartitionProjection; |
| 45 | + private transient FieldGetter lazyIndexFieldGetter; |
38 | 46 |
|
39 | 47 | public RowIdIndexFieldsExtractor( |
40 | 48 | RowType readType, List<String> partitionKeys, String indexField) { |
41 | | - this.partitionProjection = CodeGenUtils.newProjection(readType, partitionKeys); |
42 | | - int indexFieldPos = readType.getFieldIndex(indexField); |
43 | | - this.indexFieldGetter = |
44 | | - InternalRow.createFieldGetter(readType.getTypeAt(indexFieldPos), indexFieldPos); |
| 49 | + this.readType = readType; |
| 50 | + this.partitionKeys = partitionKeys; |
| 51 | + this.indexField = indexField; |
45 | 52 | this.rowIdPos = readType.getFieldIndex(SpecialFields.ROW_ID.name()); |
46 | 53 | } |
47 | 54 |
|
| 55 | + private Projection partitionProjection() { |
| 56 | + if (lazyPartitionProjection == null) { |
| 57 | + lazyPartitionProjection = CodeGenUtils.newProjection(readType, partitionKeys); |
| 58 | + } |
| 59 | + return lazyPartitionProjection; |
| 60 | + } |
| 61 | + |
| 62 | + private FieldGetter indexFieldGetter() { |
| 63 | + if (lazyIndexFieldGetter == null) { |
| 64 | + int indexFieldPos = readType.getFieldIndex(indexField); |
| 65 | + lazyIndexFieldGetter = |
| 66 | + InternalRow.createFieldGetter(readType.getTypeAt(indexFieldPos), indexFieldPos); |
| 67 | + } |
| 68 | + return lazyIndexFieldGetter; |
| 69 | + } |
| 70 | + |
48 | 71 | public BinaryRow extractPartition(InternalRow record) { |
49 | | - // projection will reuse returning record, copy is necessary |
50 | | - return partitionProjection.apply(record).copy(); |
| 72 | + return partitionProjection().apply(record).copy(); |
51 | 73 | } |
52 | 74 |
|
53 | 75 | @Nullable |
54 | 76 | public Object extractIndexField(InternalRow record) { |
55 | | - return indexFieldGetter.getFieldOrNull(record); |
| 77 | + return indexFieldGetter().getFieldOrNull(record); |
56 | 78 | } |
57 | 79 |
|
58 | 80 | public Long extractRowId(InternalRow record) { |
|
0 commit comments