voonhous commented on code in PR #19732:
URL: https://github.com/apache/hudi/pull/19732#discussion_r3863581339


##########
hudi-trino/src/main/java/io/trino/plugin/hudi/io/TrinoParquetFileReader.java:
##########
@@ -0,0 +1,437 @@
+/*
+ * Licensed 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 io.trino.plugin.hudi.io;
+
+import com.google.common.collect.ImmutableList;
+import io.trino.filesystem.TrinoFileSystem;
+import io.trino.filesystem.TrinoInputFile;
+import io.trino.memory.context.AggregatedMemoryContext;
+import io.trino.parquet.Column;
+import io.trino.parquet.Field;
+import io.trino.parquet.ParquetCorruptionException;
+import io.trino.parquet.ParquetDataSource;
+import io.trino.parquet.ParquetReaderOptions;
+import io.trino.parquet.metadata.BlockMetadata;
+import io.trino.parquet.metadata.FileMetadata;
+import io.trino.parquet.metadata.ParquetMetadata;
+import io.trino.parquet.predicate.TupleDomainParquetPredicate;
+import io.trino.parquet.reader.MetadataReader;
+import io.trino.parquet.reader.ParquetReader;
+import io.trino.parquet.reader.RowGroupInfo;
+import io.trino.plugin.base.metrics.FileFormatDataSourceStats;
+import io.trino.plugin.hive.HiveColumnHandle;
+import io.trino.plugin.hive.parquet.ParquetReaderConfig;
+import io.trino.plugin.hudi.HudiUtil;
+import io.trino.plugin.hudi.storage.HudiTrinoStorage;
+import io.trino.plugin.hudi.util.HudiAvroSerializer;
+import io.trino.spi.Page;
+import io.trino.spi.TrinoException;
+import io.trino.spi.connector.SourcePage;
+import io.trino.spi.predicate.TupleDomain;
+import io.trino.spi.type.SqlVarbinary;
+import org.apache.avro.Schema;
+import org.apache.avro.generic.IndexedRecord;
+import org.apache.hudi.common.bloom.BloomFilter;
+import org.apache.hudi.common.schema.HoodieSchema;
+import org.apache.hudi.common.util.collection.ClosableIterator;
+import org.apache.hudi.common.util.collection.Pair;
+import org.apache.hudi.core.io.storage.HoodieAvroFileReader;
+import org.apache.hudi.storage.HoodieStorage;
+import org.apache.hudi.storage.StoragePath;
+import org.apache.parquet.column.ColumnDescriptor;
+import org.apache.parquet.io.MessageColumnIO;
+import org.apache.parquet.schema.MessageType;
+import org.joda.time.DateTimeZone;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Optional;
+import java.util.OptionalLong;
+import java.util.Set;
+import java.util.stream.IntStream;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static 
io.trino.memory.context.AggregatedMemoryContext.newSimpleAggregatedMemoryContext;
+import static io.trino.parquet.ParquetTypeUtils.constructField;
+import static io.trino.parquet.ParquetTypeUtils.getColumnIO;
+import static io.trino.parquet.ParquetTypeUtils.getDescriptors;
+import static io.trino.parquet.ParquetTypeUtils.lookupColumnByName;
+import static io.trino.parquet.predicate.PredicateUtils.buildPredicate;
+import static io.trino.parquet.predicate.PredicateUtils.getFilteredRowGroups;
+import static 
io.trino.plugin.hive.parquet.ParquetPageSourceFactory.createDataSource;
+import static 
io.trino.plugin.hive.parquet.ParquetPageSourceFactory.getParquetMessageType;
+import static io.trino.plugin.hudi.HudiErrorCode.HUDI_BAD_DATA;
+import static io.trino.plugin.hudi.HudiErrorCode.HUDI_CURSOR_ERROR;
+import static io.trino.plugin.hudi.HudiErrorCode.HUDI_SCHEMA_ERROR;
+import static io.trino.spi.type.VarbinaryType.VARBINARY;
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Reads an LSM archived-timeline Parquet file through Trino's {@link 
ParquetReader}, turning each
+ * {@link Page} it produces into an Avro {@link IndexedRecord} with {@link 
HudiAvroSerializer}. Hudi's
+ * archived-timeline loader asks {@link HudiTrinoFileReaderFactory} for an 
Avro file reader over the
+ * history files under {@code .hoodie/timeline/history}, and this is the 
connector's answer to that
+ * request. It is not a general data-file reader: record-key, key-prefix and 
row-key lookups are
+ * unsupported, and so are the bloom filter and min/max record key lookups -- 
a timeline file carries
+ * none of the data-file footer metadata those rely on.
+ */
+public class TrinoParquetFileReader
+        extends HoodieAvroFileReader
+{
+    private static final String PARQUET_AVRO_SCHEMA_KEY = 
"parquet.avro.schema";
+    private static final DateTimeZone UTC_TIME_ZONE = DateTimeZone.UTC;
+    private static final int DOMAIN_COMPACTION_THRESHOLD = 1000;
+
+    private final StoragePath path;
+    private final ParquetReaderOptions readerOptions = new 
ParquetReaderConfig().toParquetReaderOptions();
+    private final TrinoInputFile inputFile;
+    private final long fileLength;
+    private final ParquetMetadata parquetMetadata;
+    private final HoodieSchema hoodieSchema;
+    private final long totalRecords;
+    // Every iterator handed out by getIndexedRecordIterator, so that close() 
can release the ParquetReader
+    // of one the caller left open. A reader instance is used by a single 
thread, but the list is cheap to guard
+    private final List<ParquetIndexedRecordIterator> openIterators = new 
ArrayList<>();
+
+    public TrinoParquetFileReader(HoodieStorage storage, StoragePath path)
+    {
+        this.path = requireNonNull(path, "path is null");
+        requireNonNull(storage, "storage is null");
+        checkArgument(storage instanceof HudiTrinoStorage, "storage must be an 
instance of HudiTrinoStorage");
+        HudiTrinoStorage trinoStorage = (HudiTrinoStorage) storage;
+
+        // HudiTrinoStorage#getFileSystem is typed Object so that hudi-common 
stays free of Trino types
+        TrinoFileSystem fileSystem = (TrinoFileSystem) 
trinoStorage.getFileSystem();
+        this.inputFile = 
fileSystem.newInputFile(HudiTrinoStorage.convertToLocation(path));
+        try {
+            this.fileLength = inputFile.length();
+            this.parquetMetadata = readParquetMetadata();
+        }
+        catch (IOException e) {
+            // Failing to open the file surfaces the way a failing read does: 
a corrupt footer as HUDI_BAD_DATA,
+            // anything else as HUDI_CURSOR_ERROR, each with its cause attached
+            throw handleException(path, e);
+        }
+        Schema avroSchema = 
extractAvroSchema(parquetMetadata.getFileMetaData());
+        this.hoodieSchema = HoodieSchema.fromAvroSchema(avroSchema);
+        this.totalRecords = parquetMetadata.getBlocks().stream()

Review Comment:
   Done in 82d9faa: the totalRecords computation sits inside the constructor 
try now, so a failing getBlocks() surfaces through handleException like the 
length and footer reads. The module compiles again.



##########
hudi-trino/src/main/java/io/trino/plugin/hudi/io/TrinoParquetFileReader.java:
##########
@@ -0,0 +1,437 @@
+/*
+ * Licensed 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 io.trino.plugin.hudi.io;
+
+import com.google.common.collect.ImmutableList;
+import io.trino.filesystem.TrinoFileSystem;
+import io.trino.filesystem.TrinoInputFile;
+import io.trino.memory.context.AggregatedMemoryContext;
+import io.trino.parquet.Column;
+import io.trino.parquet.Field;
+import io.trino.parquet.ParquetCorruptionException;
+import io.trino.parquet.ParquetDataSource;
+import io.trino.parquet.ParquetReaderOptions;
+import io.trino.parquet.metadata.BlockMetadata;
+import io.trino.parquet.metadata.FileMetadata;
+import io.trino.parquet.metadata.ParquetMetadata;
+import io.trino.parquet.predicate.TupleDomainParquetPredicate;
+import io.trino.parquet.reader.MetadataReader;
+import io.trino.parquet.reader.ParquetReader;
+import io.trino.parquet.reader.RowGroupInfo;
+import io.trino.plugin.base.metrics.FileFormatDataSourceStats;
+import io.trino.plugin.hive.HiveColumnHandle;
+import io.trino.plugin.hive.parquet.ParquetReaderConfig;
+import io.trino.plugin.hudi.HudiUtil;
+import io.trino.plugin.hudi.storage.HudiTrinoStorage;
+import io.trino.plugin.hudi.util.HudiAvroSerializer;
+import io.trino.spi.Page;
+import io.trino.spi.TrinoException;
+import io.trino.spi.connector.SourcePage;
+import io.trino.spi.predicate.TupleDomain;
+import io.trino.spi.type.SqlVarbinary;
+import org.apache.avro.Schema;
+import org.apache.avro.generic.IndexedRecord;
+import org.apache.hudi.common.bloom.BloomFilter;
+import org.apache.hudi.common.schema.HoodieSchema;
+import org.apache.hudi.common.util.collection.ClosableIterator;
+import org.apache.hudi.common.util.collection.Pair;
+import org.apache.hudi.core.io.storage.HoodieAvroFileReader;
+import org.apache.hudi.storage.HoodieStorage;
+import org.apache.hudi.storage.StoragePath;
+import org.apache.parquet.column.ColumnDescriptor;
+import org.apache.parquet.io.MessageColumnIO;
+import org.apache.parquet.schema.MessageType;
+import org.joda.time.DateTimeZone;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Optional;
+import java.util.OptionalLong;
+import java.util.Set;
+import java.util.stream.IntStream;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static 
io.trino.memory.context.AggregatedMemoryContext.newSimpleAggregatedMemoryContext;
+import static io.trino.parquet.ParquetTypeUtils.constructField;
+import static io.trino.parquet.ParquetTypeUtils.getColumnIO;
+import static io.trino.parquet.ParquetTypeUtils.getDescriptors;
+import static io.trino.parquet.ParquetTypeUtils.lookupColumnByName;
+import static io.trino.parquet.predicate.PredicateUtils.buildPredicate;
+import static io.trino.parquet.predicate.PredicateUtils.getFilteredRowGroups;
+import static 
io.trino.plugin.hive.parquet.ParquetPageSourceFactory.createDataSource;
+import static 
io.trino.plugin.hive.parquet.ParquetPageSourceFactory.getParquetMessageType;
+import static io.trino.plugin.hudi.HudiErrorCode.HUDI_BAD_DATA;
+import static io.trino.plugin.hudi.HudiErrorCode.HUDI_CURSOR_ERROR;
+import static io.trino.plugin.hudi.HudiErrorCode.HUDI_SCHEMA_ERROR;
+import static io.trino.spi.type.VarbinaryType.VARBINARY;
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Reads an LSM archived-timeline Parquet file through Trino's {@link 
ParquetReader}, turning each
+ * {@link Page} it produces into an Avro {@link IndexedRecord} with {@link 
HudiAvroSerializer}. Hudi's
+ * archived-timeline loader asks {@link HudiTrinoFileReaderFactory} for an 
Avro file reader over the
+ * history files under {@code .hoodie/timeline/history}, and this is the 
connector's answer to that
+ * request. It is not a general data-file reader: record-key, key-prefix and 
row-key lookups are
+ * unsupported, and so are the bloom filter and min/max record key lookups -- 
a timeline file carries
+ * none of the data-file footer metadata those rely on.
+ */
+public class TrinoParquetFileReader
+        extends HoodieAvroFileReader
+{
+    private static final String PARQUET_AVRO_SCHEMA_KEY = 
"parquet.avro.schema";
+    private static final DateTimeZone UTC_TIME_ZONE = DateTimeZone.UTC;
+    private static final int DOMAIN_COMPACTION_THRESHOLD = 1000;
+
+    private final StoragePath path;
+    private final ParquetReaderOptions readerOptions = new 
ParquetReaderConfig().toParquetReaderOptions();
+    private final TrinoInputFile inputFile;
+    private final long fileLength;
+    private final ParquetMetadata parquetMetadata;
+    private final HoodieSchema hoodieSchema;
+    private final long totalRecords;
+    // Every iterator handed out by getIndexedRecordIterator, so that close() 
can release the ParquetReader
+    // of one the caller left open. A reader instance is used by a single 
thread, but the list is cheap to guard
+    private final List<ParquetIndexedRecordIterator> openIterators = new 
ArrayList<>();
+
+    public TrinoParquetFileReader(HoodieStorage storage, StoragePath path)
+    {
+        this.path = requireNonNull(path, "path is null");
+        requireNonNull(storage, "storage is null");
+        checkArgument(storage instanceof HudiTrinoStorage, "storage must be an 
instance of HudiTrinoStorage");
+        HudiTrinoStorage trinoStorage = (HudiTrinoStorage) storage;
+
+        // HudiTrinoStorage#getFileSystem is typed Object so that hudi-common 
stays free of Trino types
+        TrinoFileSystem fileSystem = (TrinoFileSystem) 
trinoStorage.getFileSystem();
+        this.inputFile = 
fileSystem.newInputFile(HudiTrinoStorage.convertToLocation(path));
+        try {
+            this.fileLength = inputFile.length();
+            this.parquetMetadata = readParquetMetadata();
+        }
+        catch (IOException e) {
+            // Failing to open the file surfaces the way a failing read does: 
a corrupt footer as HUDI_BAD_DATA,
+            // anything else as HUDI_CURSOR_ERROR, each with its cause attached
+            throw handleException(path, e);
+        }
+        Schema avroSchema = 
extractAvroSchema(parquetMetadata.getFileMetaData());
+        this.hoodieSchema = HoodieSchema.fromAvroSchema(avroSchema);
+        this.totalRecords = parquetMetadata.getBlocks().stream()
+                .mapToLong(BlockMetadata::rowCount)
+                .sum();
+    }
+
+    @Override
+    public ClosableIterator<IndexedRecord> 
getIndexedRecordIterator(HoodieSchema readerSchema, HoodieSchema 
requestedSchema, Map<String, String> renamedColumns)
+    {
+        // Timeline files are never schema-evolved, so no column can have been 
renamed under them
+        HoodieSchema projectedSchema = requestedSchema != null ? 
requestedSchema : hoodieSchema;
+        ParquetIndexedRecordIterator iterator = new 
ParquetIndexedRecordIterator(projectedSchema);
+        synchronized (openIterators) {
+            openIterators.add(iterator);
+        }
+        return iterator;
+    }
+
+    @Override
+    public ClosableIterator<IndexedRecord> 
getIndexedRecordsByKeysIterator(List<String> keys, HoodieSchema readerSchema)
+    {
+        throw new UnsupportedOperationException("Reading records by keys is 
not supported by this reader");
+    }
+
+    @Override
+    public ClosableIterator<IndexedRecord> 
getIndexedRecordsByKeyPrefixIterator(List<String> sortedKeyPrefixes, 
HoodieSchema readerSchema)
+    {
+        throw new UnsupportedOperationException("Reading records by key 
prefixes is not supported by this reader");
+    }
+
+    @Override
+    public String[] readMinMaxRecordKeys()
+    {
+        throw new UnsupportedOperationException("Reading min/max record keys 
is not supported by this reader");
+    }
+
+    @Override
+    public BloomFilter readBloomFilter()
+    {
+        throw new UnsupportedOperationException("Reading a bloom filter is not 
supported by this reader");
+    }
+
+    @Override
+    public Set<Pair<String, Long>> filterRowKeys(Set<String> candidateRowKeys)
+    {
+        throw new UnsupportedOperationException("Filtering row keys is not 
supported by this reader");
+    }
+
+    @Override
+    public ClosableIterator<String> getRecordKeyIterator()
+    {
+        throw new UnsupportedOperationException("Iterating over only record 
keys is not supported by this reader");
+    }
+
+    @Override
+    public HoodieSchema getSchema()
+    {
+        return hoodieSchema;
+    }
+
+    @Override
+    public long getTotalRecords()
+    {
+        return totalRecords;
+    }
+
+    @Override
+    public void close()
+    {
+        // The only resource this reader opens outside the constructor is the 
ParquetReader of an iterator.
+        // Closing the reader releases every iterator it handed out, including 
any the caller left open;
+        // an iterator's close() is idempotent, so closing one that is already 
closed is a no-op. One that fails
+        // to close does not stop the others from being closed: the first 
failure is rethrown once every iterator
+        // has been released, with the later failures attached to it as 
suppressed
+        synchronized (openIterators) {
+            RuntimeException failure = null;
+            for (ParquetIndexedRecordIterator iterator : openIterators) {
+                try {
+                    iterator.close();
+                }
+                catch (RuntimeException e) {
+                    if (failure == null) {
+                        failure = e;
+                    }
+                    else {
+                        failure.addSuppressed(e);
+                    }
+                }
+            }
+            openIterators.clear();
+            if (failure != null) {
+                throw failure;
+            }
+        }
+    }
+
+    private ParquetMetadata readParquetMetadata()
+            throws IOException
+    {
+        // No estimated size: with one at or below the small-file threshold 
createDataSource returns a
+        // MemoryParquetDataSource that slurps the whole file, where the 
footer read only needs the tail
+        try (ParquetDataSource dataSource = 
openDataSource(newSimpleAggregatedMemoryContext(), OptionalLong.empty())) {
+            return MetadataReader.readFooter(dataSource, Optional.empty());
+        }
+    }
+
+    private ParquetDataSource openDataSource(AggregatedMemoryContext 
memoryContext, OptionalLong estimatedFileSize)
+            throws IOException
+    {
+        return createDataSource(inputFile, estimatedFileSize, readerOptions, 
memoryContext, new FileFormatDataSourceStats());
+    }
+
+    private Schema extractAvroSchema(FileMetadata fileMetaData)
+    {
+        String avroSchemaStr = 
fileMetaData.getKeyValueMetaData().get(PARQUET_AVRO_SCHEMA_KEY);
+        if (avroSchemaStr == null) {
+            throw new TrinoException(HUDI_SCHEMA_ERROR, "Parquet file does not 
contain Avro schema in metadata: " + path);
+        }
+        return new Schema.Parser().parse(avroSchemaStr);
+    }
+
+    /**
+     * One {@link HiveColumnHandle} per field of the projection, typed from 
the field's Avro schema the way
+     * {@link HudiUtil#toColumnHandle} types the handles of a data-file read. 
Handle {@code i} is built from
+     * field {@code i}, so a handle's index is its field's position in the 
records this reader produces.

Review Comment:
   Done in 78466c9: the javadoc now says a handle's position in the returned 
list is its field's position in the records, that this list position is what 
binaryFieldPositions relies on, and that the handle's own column index is left 
at 0 by toColumnHandle. HudiAvroSerializer maps by channel and field name too, 
so nothing reads that index.



-- 
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]

Reply via email to