beryllw commented on code in PR #3420: URL: https://github.com/apache/fluss/pull/3420#discussion_r3541168187
########## fluss-flink/fluss-flink-common/src/main/java/org/apache/fluss/flink/tiering/source/watermark/SimpleWatermarkExtractor.java: ########## @@ -0,0 +1,218 @@ +/* + * 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.batch.ArrowRecordBatch; +import org.apache.fluss.lake.batch.RecordBatch; +import org.apache.fluss.lake.watermark.WatermarkExtractor; +import org.apache.fluss.metadata.TableInfo; +import org.apache.fluss.record.ArrowBatchData; +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) Review Comment: ``` long count = props.keySet().stream() .filter(k -> k.startsWith(WATERMARK_PROPERTY_PREFIX) && k.endsWith(WATERMARK_ROWTIME_SUFFIX)) .count(); if (count > 1) { LOG.warn("More than 1 watermark definition..."); return null; } ``` -- 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]
