forked from apache/paimon
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Introduce binlog system table to pack the UB and UA (apache#4520)
- Loading branch information
Showing
7 changed files
with
381 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
paimon-common/src/main/java/org/apache/paimon/reader/PackChangelogReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* 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.paimon.reader; | ||
|
||
import org.apache.paimon.data.InternalRow; | ||
import org.apache.paimon.data.serializer.InternalRowSerializer; | ||
import org.apache.paimon.types.RowKind; | ||
import org.apache.paimon.types.RowType; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import java.io.IOException; | ||
import java.util.function.BiFunction; | ||
|
||
/** The reader which will pack the update before and update after message together. */ | ||
public class PackChangelogReader implements RecordReader<InternalRow> { | ||
|
||
private final RecordReader<InternalRow> reader; | ||
private final BiFunction<InternalRow, InternalRow, InternalRow> function; | ||
private final InternalRowSerializer serializer; | ||
private boolean initialized = false; | ||
|
||
public PackChangelogReader( | ||
RecordReader<InternalRow> reader, | ||
BiFunction<InternalRow, InternalRow, InternalRow> function, | ||
RowType rowType) { | ||
this.reader = reader; | ||
this.function = function; | ||
this.serializer = new InternalRowSerializer(rowType); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public RecordIterator<InternalRow> readBatch() throws IOException { | ||
if (!initialized) { | ||
initialized = true; | ||
return new InternRecordIterator(reader, function, serializer); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
reader.close(); | ||
} | ||
|
||
private static class InternRecordIterator implements RecordIterator<InternalRow> { | ||
|
||
private RecordIterator<InternalRow> currentBatch; | ||
|
||
private final BiFunction<InternalRow, InternalRow, InternalRow> function; | ||
private final RecordReader<InternalRow> reader; | ||
private final InternalRowSerializer serializer; | ||
private boolean endOfData; | ||
|
||
public InternRecordIterator( | ||
RecordReader<InternalRow> reader, | ||
BiFunction<InternalRow, InternalRow, InternalRow> function, | ||
InternalRowSerializer serializer) { | ||
this.reader = reader; | ||
this.function = function; | ||
this.serializer = serializer; | ||
this.endOfData = false; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public InternalRow next() throws IOException { | ||
InternalRow row1 = nextRow(); | ||
if (row1 == null) { | ||
return null; | ||
} | ||
InternalRow row2 = null; | ||
if (row1.getRowKind() == RowKind.UPDATE_BEFORE) { | ||
row1 = serializer.copy(row1); | ||
row2 = nextRow(); | ||
} | ||
return function.apply(row1, row2); | ||
} | ||
|
||
@Nullable | ||
private InternalRow nextRow() throws IOException { | ||
InternalRow row = null; | ||
while (!endOfData && row == null) { | ||
RecordIterator<InternalRow> batch = nextBatch(); | ||
if (batch == null) { | ||
endOfData = true; | ||
return null; | ||
} | ||
|
||
row = batch.next(); | ||
if (row == null) { | ||
releaseBatch(); | ||
} | ||
} | ||
return row; | ||
} | ||
|
||
@Nullable | ||
private RecordIterator<InternalRow> nextBatch() throws IOException { | ||
if (currentBatch == null) { | ||
currentBatch = reader.readBatch(); | ||
} | ||
return currentBatch; | ||
} | ||
|
||
@Override | ||
public void releaseBatch() { | ||
if (currentBatch != null) { | ||
currentBatch.releaseBatch(); | ||
currentBatch = null; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
paimon-core/src/main/java/org/apache/paimon/table/system/BinlogTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* 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.paimon.table.system; | ||
|
||
import org.apache.paimon.data.GenericArray; | ||
import org.apache.paimon.data.GenericRow; | ||
import org.apache.paimon.data.InternalRow; | ||
import org.apache.paimon.reader.PackChangelogReader; | ||
import org.apache.paimon.reader.RecordReader; | ||
import org.apache.paimon.table.FileStoreTable; | ||
import org.apache.paimon.table.SpecialFields; | ||
import org.apache.paimon.table.Table; | ||
import org.apache.paimon.table.source.DataSplit; | ||
import org.apache.paimon.table.source.InnerTableRead; | ||
import org.apache.paimon.table.source.Split; | ||
import org.apache.paimon.types.ArrayType; | ||
import org.apache.paimon.types.DataField; | ||
import org.apache.paimon.types.RowType; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.apache.paimon.catalog.Catalog.SYSTEM_TABLE_SPLITTER; | ||
|
||
/** | ||
* A {@link Table} for reading binlog of table. The binlog format is as below. | ||
* | ||
* <p>INSERT: [+I, [co1, null], [col2, null]] | ||
* | ||
* <p>UPDATE: [+U, [co1_ub, col1_ua], [col2_ub, col2_ua]] | ||
* | ||
* <p>DELETE: [-D, [co1, null], [col2, null]] | ||
*/ | ||
public class BinlogTable extends AuditLogTable { | ||
|
||
public static final String BINLOG = "binlog"; | ||
|
||
private final FileStoreTable wrapped; | ||
|
||
public BinlogTable(FileStoreTable wrapped) { | ||
super(wrapped); | ||
this.wrapped = wrapped; | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return wrapped.name() + SYSTEM_TABLE_SPLITTER + BINLOG; | ||
} | ||
|
||
@Override | ||
public RowType rowType() { | ||
List<DataField> fields = new ArrayList<>(); | ||
fields.add(SpecialFields.ROW_KIND); | ||
for (DataField field : wrapped.rowType().getFields()) { | ||
DataField newField = | ||
new DataField( | ||
field.id(), | ||
field.name(), | ||
new ArrayType(field.type().nullable()), // convert to nullable | ||
field.description()); | ||
fields.add(newField); | ||
} | ||
return new RowType(fields); | ||
} | ||
|
||
@Override | ||
public InnerTableRead newRead() { | ||
return new BinlogRead(wrapped.newRead()); | ||
} | ||
|
||
@Override | ||
public Table copy(Map<String, String> dynamicOptions) { | ||
return new BinlogTable(wrapped.copy(dynamicOptions)); | ||
} | ||
|
||
private class BinlogRead extends AuditLogRead { | ||
|
||
private BinlogRead(InnerTableRead dataRead) { | ||
super(dataRead); | ||
} | ||
|
||
@Override | ||
public RecordReader<InternalRow> createReader(Split split) throws IOException { | ||
DataSplit dataSplit = (DataSplit) split; | ||
if (dataSplit.isStreaming()) { | ||
return new PackChangelogReader( | ||
dataRead.createReader(split), | ||
(row1, row2) -> | ||
new AuditLogRow( | ||
readProjection, | ||
convertToArray( | ||
row1, row2, wrapped.rowType().fieldGetters())), | ||
wrapped.rowType()); | ||
} else { | ||
return dataRead.createReader(split) | ||
.transform( | ||
(row) -> | ||
new AuditLogRow( | ||
readProjection, | ||
convertToArray( | ||
row, | ||
null, | ||
wrapped.rowType().fieldGetters()))); | ||
} | ||
} | ||
|
||
private InternalRow convertToArray( | ||
InternalRow row1, | ||
@Nullable InternalRow row2, | ||
InternalRow.FieldGetter[] fieldGetters) { | ||
GenericRow row = new GenericRow(row1.getFieldCount()); | ||
for (int i = 0; i < row1.getFieldCount(); i++) { | ||
Object o1 = fieldGetters[i].getFieldOrNull(row1); | ||
Object o2; | ||
if (row2 != null) { | ||
o2 = fieldGetters[i].getFieldOrNull(row2); | ||
row.setField(i, new GenericArray(new Object[] {o1, o2})); | ||
} else { | ||
row.setField(i, new GenericArray(new Object[] {o1})); | ||
} | ||
} | ||
// If no row2 provided, then follow the row1 kind. | ||
if (row2 == null) { | ||
row.setRowKind(row1.getRowKind()); | ||
} else { | ||
row.setRowKind(row2.getRowKind()); | ||
} | ||
return row; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.