Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public static List<String> sequenceGroupProtectedFields(String optionValue) {
private final List<WrapperWithFieldIndex<FieldsComparator>> fieldSeqComparators;
private final boolean fieldSequenceEnabled;
private final List<WrapperWithFieldIndex<FieldAggregator>> fieldAggregators;
private final Map<Integer, Object> emptyAccumulators;
private final boolean removeRecordOnDelete;
private final Set<Integer> sequenceGroupPartialDelete;
private final boolean[] nullables;
Expand Down Expand Up @@ -164,6 +165,14 @@ protected PartialUpdateMergeFunction(
this.ignoreDelete = ignoreDelete;
this.fieldSeqComparators = getKeySortedListFromMap(fieldSeqComparators);
this.fieldAggregators = getKeySortedListFromMap(fieldAggregators);
this.emptyAccumulators = new HashMap<>();
fieldAggregators.forEach(
(i, aggregator) -> {
Object emptyAccumulator = aggregator.emptyAccumulator();
if (!nullables[i] && emptyAccumulator != null) {
emptyAccumulators.put(i, emptyAccumulator);
}
});
this.fieldSequenceEnabled = fieldSequenceEnabled;
this.removeRecordOnDelete = removeRecordOnDelete;
this.sequenceGroupPartialDelete = sequenceGroupPartialDelete;
Expand All @@ -188,7 +197,11 @@ public void add(KeyValue kv) {
if (kv.valueKind().isRetract()) {

if (!notNullColumnFilled) {
initRow(row, kv.value());
if (fieldSequenceEnabled && !ignoreDelete) {
initRetractRow(row, kv.value());
} else {
initRow(row, kv.value());
}
notNullColumnFilled = true;
}

Expand Down Expand Up @@ -226,6 +239,11 @@ public void add(KeyValue kv) {
throw new IllegalArgumentException(msg);
}

if (ignoreDelete && fieldSequenceEnabled && notNullColumnFilled && !meetInsert) {
// The first ignored retract supplied the payload for a possible DELETE result.
// Start the first positive record with an empty accumulator.
row = new GenericRow(getters.length);
}
latestSequenceNumber = kv.sequenceNumber();
if (fieldSeqComparators.isEmpty()) {
currentDeleteRow = false;
Expand Down Expand Up @@ -415,6 +433,27 @@ private void retractWithSequenceGroup(KeyValue kv) {
}
}

private void initRetractRow(GenericRow row, InternalRow value) {
// A required aggregate starts empty even if its sequence group skips every record.
for (int i = 0; i < getters.length; i++) {
if (!nullables[i]) {
Object field = getters[i].getFieldOrNull(value);
if (field == null) {
throw new IllegalArgumentException("Field " + i + " can not be null");
}
row.setField(i, emptyAccumulators.getOrDefault(i, field));
}
}
}

private GenericRow copyRow(GenericRow row) {
GenericRow copy = new GenericRow(row.getRowKind(), row.getFieldCount());
for (int i = 0; i < row.getFieldCount(); i++) {
copy.setField(i, row.getField(i));
}
return copy;
}

private void initRow(GenericRow row, InternalRow value) {
for (int i = 0; i < getters.length; i++) {
Object field = getters[i].getFieldOrNull(value);
Expand All @@ -432,7 +471,23 @@ public KeyValue getResult() {
}

RowKind rowKind = currentDeleteRow || !meetInsert ? RowKind.DELETE : RowKind.INSERT;
return reused.replace(currentKey, latestSequenceNumber, rowKind, row);
GenericRow result = row;
if (fieldSequenceEnabled && !meetInsert && !currentDeleteRow && !ignoreDelete) {
// A later merge retracts the DELETE payload again. Let each aggregator convert its
// retraction-only accumulator, and keep the accumulator for subsequent calls to add().
for (WrapperWithFieldIndex<FieldAggregator> field : fieldAggregators) {
int i = field.fieldIndex;
Object accumulator = row.getField(i);
Object retraction = field.getValue().retractionResult(accumulator);
if (retraction != accumulator) {
if (result == row) {
result = copyRow(row);
}
result.setField(i, retraction);
}
}
}
return reused.replace(currentKey, latestSequenceNumber, rowKind, result);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.apache.paimon.types.DataType;

import javax.annotation.Nullable;

import java.io.Serializable;

/** abstract class of aggregating a field of a row. */
Expand All @@ -44,6 +46,25 @@ public Object aggReversed(Object accumulator, Object inputField) {
/** reset the aggregator to a clean start state. */
public void reset() {}

/**
* Returns the accumulator before any input is aggregated, or null if the aggregator does not
* define one. It is used when a non-nullable field must be initialized from a retraction.
*/
@Nullable
public Object emptyAccumulator() {
return null;
}

/**
* Converts an accumulator that only received retractions into the value of the emitted retract
* record. A later merge retracts this value again, so the result must undo the accumulated
* retractions.
*/
@Nullable
public Object retractionResult(@Nullable Object accumulator) {
return accumulator;
}

public Object retract(Object accumulator, Object retractField) {
throw new UnsupportedOperationException(
String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public void reset() {
aggregator.reset();
}

@Override
public Object emptyAccumulator() {
return aggregator.emptyAccumulator();
}

@Override
public Object retract(Object accumulator, Object retractField) {
return accumulator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.paimon.data.Decimal;
import org.apache.paimon.types.DataType;
import org.apache.paimon.utils.DecimalUtils;
import org.apache.paimon.utils.TypeUtils;

/** Sum aggregate a field of a row. */
public class FieldSumAgg extends FieldAggregator {
Expand All @@ -34,6 +35,16 @@ public FieldSumAgg(String name, DataType dataType, boolean failOnOverflow) {
this.failOnOverflow = failOnOverflow;
}

@Override
public Object emptyAccumulator() {
return TypeUtils.castFromString("0", fieldType);
}

@Override
public Object retractionResult(Object accumulator) {
return negative(accumulator);
}

@Override
public Object agg(Object accumulator, Object inputField) {
if (accumulator == null || inputField == null) {
Expand Down
Loading
Loading