Copilot commented on code in PR #3420: URL: https://github.com/apache/fluss/pull/3420#discussion_r3378985526
########## fluss-flink/fluss-flink-common/src/main/java/org/apache/fluss/flink/tiering/source/watermark/SimpleWatermarkExtractor.java: ########## @@ -0,0 +1,203 @@ +/* + * 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.fluss.flink.tiering.source.watermark; + +import org.apache.fluss.lake.watermark.WatermarkExtractor; +import org.apache.fluss.metadata.TableInfo; +import org.apache.fluss.row.InternalRow; +import org.apache.fluss.types.RowType; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Extracts epoch-millis watermark values from {@link InternalRow} by parsing Flink watermark + * definitions stored in table properties. Only two expression formats are supported: + * + * <ul> + * <li>{@code WATERMARK FOR ts AS ts} — direct column reference, zero delay + * <li>{@code WATERMARK FOR ts AS ts - INTERVAL '5' SECOND} — column minus interval delay + * </ul> + * + * <p>The rowtime column must be a physical column (computed columns are not supported). Returns + * {@code null} from {@link #create(TableInfo)} if the watermark configuration cannot be parsed. + */ +public class SimpleWatermarkExtractor implements WatermarkExtractor { + + private static final Logger LOG = LoggerFactory.getLogger(SimpleWatermarkExtractor.class); + + private static final String WATERMARK_PROPERTY_PREFIX = "schema.watermark."; + private static final String WATERMARK_ROWTIME_SUFFIX = ".rowtime"; + private static final String WATERMARK_STRATEGY_EXPR_SUFFIX = ".strategy.expr"; + private static final String WATERMARK_STRATEGY_DATA_TYPE_SUFFIX = ".strategy.data-type"; + + private static final Pattern WATERMARK_EXPR_SIMPLE_COLUMN_PATTERN = + Pattern.compile("(`\\w+`|\\w+)"); + private static final Pattern WATERMARK_EXPR_COLUMN_MINUS_INTERVAL_PATTERN = + Pattern.compile( + "(`\\w+`|\\w+)\\s+-\\s+INTERVAL\\s+'(\\d+\\.?\\d*)'\\s+(SECOND|MINUTE|HOUR|DAY)", + Pattern.CASE_INSENSITIVE); + private static final Pattern TIMESTAMP_TYPE_PATTERN = + Pattern.compile("TIMESTAMP(?:_LTZ)?\\((\\d+)\\)", Pattern.CASE_INSENSITIVE); + + private final int fieldIndex; + private final int precision; + private final boolean isTimestampLtz; + private final long delayMillis; + + private SimpleWatermarkExtractor( + int fieldIndex, int precision, boolean isTimestampLtz, long delayMillis) { + this.fieldIndex = fieldIndex; + this.precision = precision; + this.isTimestampLtz = isTimestampLtz; + this.delayMillis = delayMillis; + } + + /** + * Creates a {@link SimpleWatermarkExtractor} from the table's properties. Returns null if no + * watermark configuration is found or the watermark configuration cannot be parsed. + */ + @Nullable + public static SimpleWatermarkExtractor create(TableInfo tableInfo) { + Map<String, String> props = tableInfo.getCustomProperties().toMap(); + RowType rowType = tableInfo.getRowType(); + + boolean isWatermarkDefined = false; + String rowtimeColumn = null; + String watermarkIndex = null; + + for (Map.Entry<String, String> entry : props.entrySet()) { + String key = entry.getKey(); + if (key.startsWith(WATERMARK_PROPERTY_PREFIX) + && key.endsWith(WATERMARK_ROWTIME_SUFFIX)) { + isWatermarkDefined = true; + rowtimeColumn = entry.getValue(); + watermarkIndex = + key.substring( + WATERMARK_PROPERTY_PREFIX.length(), + key.length() - WATERMARK_ROWTIME_SUFFIX.length()); + if (!String.valueOf(0).equals(watermarkIndex)) { + LOG.warn( + "There are more than 1 watermark definition for {}, which is not supported for watermark extraction.", + tableInfo.getTablePath()); + return null; + } + break; + } + } + + if (!isWatermarkDefined) { + return null; + } + + int fieldIndex = rowType.getFieldIndex(rowtimeColumn); + if (fieldIndex < 0) { + LOG.warn( + "Watermark rowtime column '{}' not found in row type for {}, " + + "computed column is not supported for watermark extraction.", + tableInfo.getTablePath(), + rowtimeColumn); Review Comment: The LOG.warn placeholder arguments are swapped here. As written, the log will print the table path where the rowtime column name should be, and vice versa, which makes the warning misleading when watermark config cannot be applied. ########## fluss-flink/fluss-flink-common/src/main/java/org/apache/fluss/flink/tiering/committer/TieringCommitOperator.java: ########## @@ -232,14 +233,35 @@ private CommitResult commitWriteResults( Map<TableBucket, Long> logEndOffsets = new HashMap<>(); Map<TableBucket, Long> logMaxTieredTimestamps = new HashMap<>(); + Long watermark = null; for (TableBucketWriteResult<WriteResult> writeResult : nonEmptyResults) { TableBucket tableBucket = writeResult.tableBucket(); logEndOffsets.put(tableBucket, writeResult.logEndOffset()); logMaxTieredTimestamps.put(tableBucket, writeResult.maxTimestamp()); + Long writeResultWatermark = writeResult.writeResult().getWatermark(); + if (writeResultWatermark == null) { + continue; + } + watermark = + watermark == null + ? writeResultWatermark + : Math.min(watermark, writeResultWatermark); + } + + if (nonEmptyResults.size() < committableWriteResults.size()) { + // Empty results means some splits has not been processed, possibly caused by force + // completion. Do not update watermark here. + if (watermark != null) { Review Comment: Grammar in the comment: "some splits has" should be "some splits have" (and "force completion" reads more naturally as "forced completion"). ########## fluss-common/src/main/java/org/apache/fluss/lake/committer/LakeCommitter.java: ########## @@ -34,22 +35,36 @@ * @since 0.7 */ @PublicEvolving -public interface LakeCommitter<WriteResult, CommittableT> extends AutoCloseable { +public interface LakeCommitter<WriteResult extends LakeWriteResult, CommittableT> + extends AutoCloseable { /** * The property key used to store the file path of lake table bucket offsets in snapshot * properties. */ String FLUSS_LAKE_SNAP_BUCKET_OFFSET_PROPERTY = "fluss-offsets"; + /** + * Converts a list of write results to a committable object with watermark. + * + * @param writeResults the list of write results + * @param watermark watermark to be committed + * @return the committable object + * @throws IOException if an I/O error occurs + */ + CommittableT toCommittable(List<WriteResult> writeResults, @Nullable Long watermark) + throws IOException; + /** * Converts a list of write results to a committable object. * * @param writeResults the list of write results * @return the committable object * @throws IOException if an I/O error occurs */ - CommittableT toCommittable(List<WriteResult> writeResults) throws IOException; + default CommittableT toCommittable(List<WriteResult> writeResults) throws IOException { + return toCommittable(writeResults, null); + } Review Comment: This change adds a new abstract method to a `@PublicEvolving` interface, which is a source-breaking change for any external LakeCommitter implementations (they must now implement the new overload). Consider instead making the new watermark overload a default method delegating to the existing toCommittable(List<WriteResult>) so existing implementations keep compiling and watermark support remains opt-in. -- 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]
