VasShabu commented on code in PR #30182: URL: https://github.com/apache/flink/pull/30182#discussion_r4725142831
########## flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/ptf/DeduplicateLiveRowsFunction.java: ########## @@ -0,0 +1,257 @@ +/* + * 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.flink.table.runtime.functions.ptf; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.api.dataview.MapView; +import org.apache.flink.table.api.dataview.ValueView; +import org.apache.flink.table.data.GenericRowData; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.data.StringData; +import org.apache.flink.table.data.binary.BinaryRowData; +import org.apache.flink.table.functions.FunctionContext; +import org.apache.flink.table.functions.SpecializedFunction.SpecializedContext; +import org.apache.flink.table.functions.definitions.DeduplicateDefinition; +import org.apache.flink.table.functions.definitions.DeduplicateDefinition.Keep; +import org.apache.flink.table.runtime.functions.ptf.DeduplicateUtils.SequenceComparator; +import org.apache.flink.table.types.DataType; +import org.apache.flink.table.types.inference.StateTypeStrategy; +import org.apache.flink.table.types.inference.utils.CallContextUtils; +import org.apache.flink.types.ColumnList; +import org.apache.flink.types.RowKind; + +import javax.annotation.Nullable; + +import java.time.Duration; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Optional; + +import static org.apache.flink.table.functions.definitions.DeduplicateDefinition.ARG_STATE_TTL; + +/** + * {@link Deduplicate} on updating input whose PARTITION BY columns contain no upsert key: a key can + * have several live rows. The {@code rows} state counts each live row and stamps it with its + * arrival, the {@code current} state holds the kept row, and the {@code arrival} state numbers the + * rows of a key. When the kept row is retracted, the next one is found among the live rows. + */ +@Internal +public class DeduplicateLiveRowsFunction extends DeduplicateRowFunctionBase { + + private static final long serialVersionUID = 1L; + + private static final String ROWS_STATE = "rows"; + + private static final String CURRENT_STATE = "current"; + + private static final String ARRIVAL_STATE = "arrival"; + + private static final int COUNT = 0; + + private static final int ARRIVAL = 1; + + private final Keep keepMode; + + private final @Nullable SequenceComparator sequenceComparator; + + DeduplicateLiveRowsFunction(final SpecializedContext context) { + super(context, liveRowsState(context)); + this.keepMode = DeduplicateDefinition.keep(context.getCallContext()); + this.sequenceComparator = DeduplicateUtils.sequenceComparator(context); + } + + @Override + public void open(final FunctionContext context) throws Exception { + super.open(context); + if (sequenceComparator != null) { + sequenceComparator.open(context); + } + } + + public void eval( + final Context ctx, + final MapView<RowData, RowData> rows, + final ValueView<RowData> currentState, + final ValueView<Long> arrivalState, + final RowData input, + @Nullable final StringData keep, + @Nullable final ColumnList sequenceBy, + @Nullable final Long stateTtl) + throws Exception { + final BinaryRowData row = toStateRow(input); + final RowData current = currentState.getValue(); + final RowKind kind = input.getRowKind(); + if (kind == RowKind.INSERT || kind == RowKind.UPDATE_AFTER) { + add(rows, currentState, arrivalState, row, current); + } else { + retract(rows, currentState, arrivalState, row, current); + } + } + + private void add( + final MapView<RowData, RowData> rows, + final ValueView<RowData> currentState, + final ValueView<Long> arrivalState, + final BinaryRowData row, + @Nullable final RowData current) + throws Exception { + final long arrival = nextArrival(rows, arrivalState); + arrivalState.setValue(arrival); + + final RowData entry = rows.get(row); + final long count = entry == null ? 1L : entry.getLong(COUNT) + 1; + final long rowArrival = + entry == null || keepMode == Keep.LAST ? arrival : entry.getLong(ARRIVAL); + rows.put(row, GenericRowData.of(count, rowArrival)); + + final RowData currentEntry = current == null ? null : rows.get(current); + if (currentEntry == null) { + // No kept row, or its entry expired: choose among the remembered rows. + selectCurrent(rows, currentState, arrivalState, current); + return; + } + if (!row.equals(current) && wins(row, rowArrival, current, currentEntry.getLong(ARRIVAL))) { + currentState.setValue(row); + if (isUpdateBeforeEmitted) { + emit(RowKind.UPDATE_BEFORE, current); + } + emit(RowKind.UPDATE_AFTER, row); + } else if (isTtlEnabled) { + currentState.setValue(current); + } + } + + private void retract( + final MapView<RowData, RowData> rows, + final ValueView<RowData> currentState, + final ValueView<Long> arrivalState, + final BinaryRowData row, + @Nullable final RowData current) + throws Exception { + final boolean isCurrent = current != null && row.equals(current); + final RowData entry = rows.get(row); + if (entry == null) { + // The entry of the kept row can expire before the kept row itself. + if (isCurrent) { + selectCurrent(rows, currentState, arrivalState, current); + } + return; + } + final long count = entry.getLong(COUNT); + if (count > 1) { + rows.put(row, GenericRowData.of(count - 1, entry.getLong(ARRIVAL))); + if (isTtlEnabled && current != null) { + currentState.setValue(current); + } + return; + } + rows.remove(row); + if (isCurrent) { + selectCurrent(rows, currentState, arrivalState, current); + } + } + + /** + * Makes the best remembered row the kept row, in place of {@code previous}, which is no longer + * among the remembered rows. + */ + private void selectCurrent( + final MapView<RowData, RowData> rows, + final ValueView<RowData> currentState, + final ValueView<Long> arrivalState, + @Nullable final RowData previous) + throws Exception { + RowData next = null; + long nextArrival = 0L; + for (final Map.Entry<RowData, RowData> candidate : rows.entries()) { + final long candidateArrival = candidate.getValue().getLong(ARRIVAL); + if (next == null || wins(candidate.getKey(), candidateArrival, next, nextArrival)) { + next = candidate.getKey(); + nextArrival = candidateArrival; + } + } Review Comment: I was talking with claude about this particular section. This essentially scans from left to right to find the next best row that we can update with. takes O(N) every time a -D is emited. You are also required to deserialise all of the potential rows. Take my proposal with a grain of salt. Keep in mind this proposed optimisation will only work when there is no state_ttl due to state expiration messing with the heap. If we store all former rows in a max/min heap (implemented with an array) the average complexity will be brought down from O(N) -> O(log N). The can be implemented with a MapView rather than ArrayView as you would have to deserialise the whole array which may be costly, instead we can use a map view as an array. EG. mapview[0] = 1 mapview[1] = 2 mapview[2] = 3 equivalent to : arrayView = [1,2,3] except we do not have to deserialise every record -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
