fhan688 commented on code in PR #3541: URL: https://github.com/apache/fluss/pull/3541#discussion_r3490822570
########## fluss-lake/fluss-lake-hudi/src/main/java/org/apache/fluss/lake/hudi/source/HudiSortedRecordReader.java: ########## @@ -0,0 +1,433 @@ +/* + * 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.lake.hudi.source; + +import org.apache.fluss.config.Configuration; +import org.apache.fluss.lake.hudi.utils.HudiTableInfo; +import org.apache.fluss.lake.source.RecordReader; +import org.apache.fluss.lake.source.SortedRecordReader; +import org.apache.fluss.metadata.TablePath; +import org.apache.fluss.record.GenericRecord; +import org.apache.fluss.record.LogRecord; +import org.apache.fluss.row.BinaryString; +import org.apache.fluss.row.Decimal; +import org.apache.fluss.row.GenericRow; +import org.apache.fluss.row.InternalRow; +import org.apache.fluss.types.DataTypeRoot; +import org.apache.fluss.utils.CloseableIterator; +import org.apache.fluss.utils.InternalRowUtils; + +import org.apache.flink.table.api.Schema; +import org.apache.flink.table.types.AbstractDataType; +import org.apache.flink.table.types.DataType; +import org.apache.flink.table.types.logical.DecimalType; +import org.apache.flink.table.types.logical.LocalZonedTimestampType; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.TimestampType; +import org.apache.hudi.configuration.FlinkOptions; +import org.apache.hudi.source.ExpressionPredicates; + +import javax.annotation.Nullable; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static org.apache.fluss.lake.hudi.HudiLakeCatalog.SYSTEM_COLUMNS; + +/** Sorted Hudi record reader for primary key table union read. */ +public class HudiSortedRecordReader implements SortedRecordReader { + + private static final String DELIMITER = ","; + + private final @Nullable RecordReader delegate; + private final SortOrder sortOrder; + + public HudiSortedRecordReader( + Configuration hudiConfig, + TablePath tablePath, + @Nullable HudiSplit hudiSplit, + @Nullable int[][] project, + List<ExpressionPredicates.Predicate> predicates) + throws Exception { + this.delegate = + hudiSplit == null + ? null + : new HudiRecordReader( + hudiConfig, tablePath, hudiSplit, project, predicates); + this.sortOrder = createSortOrder(hudiConfig, tablePath, project); + } + + @Override + public CloseableIterator<LogRecord> read() throws IOException { + if (delegate == null) { + return CloseableIterator.wrap(Collections.emptyIterator()); + } + CloseableIterator<LogRecord> iterator = delegate.read(); + List<LogRecord> records = new ArrayList<>(); + try { + while (iterator.hasNext()) { + records.add(copyRecord(iterator.next(), sortOrder.producedTypes)); + } + } finally { + iterator.close(); + } + records.sort( + (record1, record2) -> + compareRows( + record1.getRow(), + record2.getRow(), + sortOrder.keyPositionsInRecord, + sortOrder.keyTypes)); + return CloseableIterator.wrap(records.iterator()); + } + + @Override + public Comparator<InternalRow> order() { + return (row1, row2) -> + compareRows(row1, row2, sortOrder.keyPositionsInKey, sortOrder.keyTypes); + } + + static boolean canSortByRecordKey(HudiTableInfo hudiTableInfo, @Nullable int[][] project) { + RecordKeyInfo recordKeyInfo = resolveRecordKeyInfo(hudiTableInfo); + if (project == null) { + return true; + } + for (int originalPosition : recordKeyInfo.keyOriginalPositions) { + if (findProducedPosition(originalPosition, project) < 0) { + return false; + } + } + return true; + } + + private static SortOrder createSortOrder( + Configuration hudiConfig, TablePath tablePath, @Nullable int[][] project) + throws IOException { + try (HudiTableInfo hudiTableInfo = HudiTableInfo.create(tablePath, hudiConfig)) { + return resolveSortOrder(hudiTableInfo, project); + } + } + + private static SortOrder resolveSortOrder( + HudiTableInfo hudiTableInfo, @Nullable int[][] project) { + RecordKeyInfo recordKeyInfo = resolveRecordKeyInfo(hudiTableInfo); + int[] keyPositions = new int[recordKeyInfo.keyOriginalPositions.length]; + for (int i = 0; i < recordKeyInfo.keyOriginalPositions.length; i++) { + int keyPosition = findProducedPosition(recordKeyInfo.keyOriginalPositions[i], project); + if (keyPosition < 0) { + throw new IllegalArgumentException( + "Can not find Hudi record key field in projected fields."); + } + keyPositions[i] = keyPosition; + } + return new SortOrder( + recordKeyInfo.keyTypes, + keyPositions, + producedTypes(recordKeyInfo.userDataTypes, project)); + } + + private static RecordKeyInfo resolveRecordKeyInfo(HudiTableInfo hudiTableInfo) { + String recordKeyFields = + hudiTableInfo.getTableOptions().get(FlinkOptions.RECORD_KEY_FIELD.key()); + if (recordKeyFields == null || recordKeyFields.trim().isEmpty()) { + throw new IllegalArgumentException( + "Hudi record key field is required for sorted primary key table reader."); + } + + Map<String, DataType> dataTypesByName = new HashMap<>(); + Map<String, Integer> positionsByName = new HashMap<>(); + List<DataType> userDataTypes = new ArrayList<>(); + int position = 0; + for (Schema.UnresolvedColumn column : + hudiTableInfo.getHudiTable().getUnresolvedSchema().getColumns()) { + if (SYSTEM_COLUMNS.containsKey(column.getName())) { + continue; Review Comment: The behavior is intentional because project indexes are based on Fluss user fields, while Hudi system columns are appended internally and stripped from produced records. I renamed the variables to userFieldPosition/keyUserFieldPositions to make this clearer. -- 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]
