wgtmac commented on code in PR #34728:
URL: https://github.com/apache/arrow/pull/34728#discussion_r1150007066


##########
cpp/src/parquet/CMakeLists.txt:
##########
@@ -339,6 +340,7 @@ add_parquet_test(internals-test
                  encoding_test.cc
                  metadata_test.cc
                  page_index_test.cc
+                 bloom_filter_reader_test.cc

Review Comment:
   ditto



##########
cpp/src/parquet/bloom_filter_reader.cc:
##########
@@ -0,0 +1,114 @@
+// 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.
+
+#include "parquet/bloom_filter_reader.h"
+#include "parquet/exception.h"
+#include "parquet/metadata.h"
+
+namespace parquet {
+
+class RowGroupBloomFilterReaderImpl final : public RowGroupBloomFilterReader {
+ public:
+  RowGroupBloomFilterReaderImpl(std::shared_ptr<::arrow::io::RandomAccessFile> 
input,
+                                std::shared_ptr<RowGroupMetaData> 
row_group_metadata,
+                                const ReaderProperties& properties)
+      : input_(std::move(input)),
+        row_group_metadata_(std::move(row_group_metadata)),
+        properties_(properties) {}
+
+  std::unique_ptr<BloomFilter> GetColumnBloomFilter(int i) override;
+
+ private:
+  /// The input stream that can perform random access read.
+  std::shared_ptr<::arrow::io::RandomAccessFile> input_;
+
+  /// The row group metadata to get column chunk metadata.
+  std::shared_ptr<RowGroupMetaData> row_group_metadata_;
+
+  /// Reader properties used to deserialize thrift object.
+  const ReaderProperties& properties_;
+};
+
+std::unique_ptr<BloomFilter> 
RowGroupBloomFilterReaderImpl::GetColumnBloomFilter(int i) {
+  if (i < 0 || i >= row_group_metadata_->num_columns()) {
+    throw ParquetException("Invalid column index at column ordinal ", i);
+  }
+
+  auto col_chunk = row_group_metadata_->ColumnChunk(i);
+  std::unique_ptr<ColumnCryptoMetaData> crypto_metadata = 
col_chunk->crypto_metadata();
+  if (crypto_metadata != nullptr) {
+    ParquetException::NYI("Cannot read encrypted bloom filter yet");
+  }
+
+  auto bloom_filter_offset = col_chunk->bloom_filter_offset();
+  if (!bloom_filter_offset.has_value()) {
+    return nullptr;
+  }
+  PARQUET_ASSIGN_OR_THROW(auto file_size, input_->GetSize());
+  if (file_size <= *bloom_filter_offset) {
+    throw ParquetException("file size less or equal than bloom offset");
+  }
+  auto stream = ::arrow::io::RandomAccessFile::GetStream(
+      input_, *bloom_filter_offset, file_size - *bloom_filter_offset);
+  auto bloom_filter = BlockSplitBloomFilter::Deserialize(properties_, 
stream->get());
+  return std::make_unique<BlockSplitBloomFilter>(std::move(bloom_filter));
+}
+
+class BloomFilterReaderImpl final : public BloomFilterReader {
+ public:
+  BloomFilterReaderImpl(std::shared_ptr<::arrow::io::RandomAccessFile> input,
+                        std::shared_ptr<FileMetaData> file_metadata,
+                        const ReaderProperties& properties,
+                        std::shared_ptr<InternalFileDecryptor> file_decryptor)
+      : input_(std::move(input)),
+        file_metadata_(std::move(file_metadata)),
+        properties_(properties) {
+    if (file_decryptor != nullptr) {
+      ParquetException::NYI("BloomFilter encryption not support");

Review Comment:
   ```suggestion
         ParquetException::NYI("BloomFilter decryption is not yet supported");
   ```



##########
cpp/src/parquet/file_reader.cc:
##########
@@ -319,6 +321,25 @@ class SerializedFile : public ParquetFileReader::Contents {
     return page_index_reader_;
   }
 
+  BloomFilterReader& GetBloomFilterReader() override {
+    if (!file_metadata_) {
+      // Usually this won't happen if user calls one of the static Open() 
functions
+      // to create a ParquetFileReader instance. But if user calls the 
constructor
+      // directly and calls GetBloomFilterReader() before Open() then this 
could happen.
+      throw ParquetException(
+          "Cannot call GetBloomFilterReader() due to missing file metadata. 
Did you "
+          "forget to call ParquetFileReader::Open() first?");
+    }
+    if (!bloom_filter_reader_) {

Review Comment:
   We need to document its thread safety.



##########
cpp/src/parquet/file_reader.h:
##########
@@ -146,6 +148,11 @@ class PARQUET_EXPORT ParquetFileReader {
   /// WARNING: The returned PageIndexReader must not outlive the 
ParquetFileReader.
   std::shared_ptr<PageIndexReader> GetPageIndexReader();
 
+  /// Returns the BloomFilterReader. Only one instance is ever created.
+  ///
+  /// WARNING: The returned BloomFilterReader must not outlive the 
ParquetFileReader.

Review Comment:
   I'd prefer returning a `const BloomFilterReader*` or `BloomFilterReader*` so 
we can have an optimization to return a nullptr when we are sure none of bloom 
filter exists.



##########
cpp/src/parquet/CMakeLists.txt:
##########
@@ -170,6 +170,7 @@ set(PARQUET_SRCS
     metadata.cc
     xxhasher.cc
     page_index.cc
+    bloom_filter_reader.cc

Review Comment:
   please sort it alphabetically



##########
cpp/src/parquet/file_reader.cc:
##########
@@ -33,6 +33,8 @@
 #include "arrow/util/int_util_overflow.h"
 #include "arrow/util/logging.h"
 #include "arrow/util/ubsan.h"
+#include "parquet/bloom_filter.h"

Review Comment:
   This is already included in the bloom_filter_reader.h and can be removed 
safely here.



##########
cpp/src/parquet/file_reader.cc:
##########
@@ -540,6 +561,7 @@ class SerializedFile : public ParquetFileReader::Contents {
   std::shared_ptr<FileMetaData> file_metadata_;
   ReaderProperties properties_;
   std::shared_ptr<PageIndexReader> page_index_reader_;
+  std::shared_ptr<BloomFilterReader> bloom_filter_reader_;

Review Comment:
   is unique_ptr enough?



##########
cpp/src/parquet/bloom_filter_reader.h:
##########
@@ -0,0 +1,64 @@
+// 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.
+
+#pragma once
+
+#include "parquet/bloom_filter.h"
+#include "parquet/encryption/internal_file_decryptor.h"

Review Comment:
   Use forward declaration here and move it to source file.



##########
cpp/src/parquet/file_reader.h:
##########
@@ -33,6 +33,7 @@ namespace parquet {
 class ColumnReader;
 class FileMetaData;
 class PageIndexReader;
+class BloomFilterReader;

Review Comment:
   sort it alphabetically



##########
cpp/src/parquet/bloom_filter_reader.cc:
##########
@@ -0,0 +1,114 @@
+// 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.
+
+#include "parquet/bloom_filter_reader.h"
+#include "parquet/exception.h"
+#include "parquet/metadata.h"
+
+namespace parquet {
+
+class RowGroupBloomFilterReaderImpl final : public RowGroupBloomFilterReader {
+ public:
+  RowGroupBloomFilterReaderImpl(std::shared_ptr<::arrow::io::RandomAccessFile> 
input,
+                                std::shared_ptr<RowGroupMetaData> 
row_group_metadata,
+                                const ReaderProperties& properties)
+      : input_(std::move(input)),
+        row_group_metadata_(std::move(row_group_metadata)),
+        properties_(properties) {}
+
+  std::unique_ptr<BloomFilter> GetColumnBloomFilter(int i) override;
+
+ private:
+  /// The input stream that can perform random access read.
+  std::shared_ptr<::arrow::io::RandomAccessFile> input_;
+
+  /// The row group metadata to get column chunk metadata.
+  std::shared_ptr<RowGroupMetaData> row_group_metadata_;
+
+  /// Reader properties used to deserialize thrift object.
+  const ReaderProperties& properties_;
+};
+
+std::unique_ptr<BloomFilter> 
RowGroupBloomFilterReaderImpl::GetColumnBloomFilter(int i) {
+  if (i < 0 || i >= row_group_metadata_->num_columns()) {
+    throw ParquetException("Invalid column index at column ordinal ", i);
+  }
+
+  auto col_chunk = row_group_metadata_->ColumnChunk(i);
+  std::unique_ptr<ColumnCryptoMetaData> crypto_metadata = 
col_chunk->crypto_metadata();
+  if (crypto_metadata != nullptr) {
+    ParquetException::NYI("Cannot read encrypted bloom filter yet");
+  }
+
+  auto bloom_filter_offset = col_chunk->bloom_filter_offset();
+  if (!bloom_filter_offset.has_value()) {
+    return nullptr;
+  }
+  PARQUET_ASSIGN_OR_THROW(auto file_size, input_->GetSize());
+  if (file_size <= *bloom_filter_offset) {
+    throw ParquetException("file size less or equal than bloom offset");
+  }
+  auto stream = ::arrow::io::RandomAccessFile::GetStream(
+      input_, *bloom_filter_offset, file_size - *bloom_filter_offset);
+  auto bloom_filter = BlockSplitBloomFilter::Deserialize(properties_, 
stream->get());

Review Comment:
   How about changing its signature to return 
`std::unique_ptr<BlockSplitBloomFilter>` ?



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