pitrou commented on code in PR #14803:
URL: https://github.com/apache/arrow/pull/14803#discussion_r1045922490


##########
cpp/src/parquet/page_index.cc:
##########
@@ -0,0 +1,234 @@
+// 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/page_index.h"
+#include "parquet/encoding.h"
+#include "parquet/exception.h"
+#include "parquet/schema.h"
+#include "parquet/statistics.h"
+#include "parquet/thrift_internal.h"
+
+#include "arrow/util/unreachable.h"
+
+#include <limits>
+#include <numeric>
+
+namespace parquet {
+
+namespace {
+
+template <typename DType>
+void Decode(std::unique_ptr<typename EncodingTraits<DType>::Decoder>& decoder,
+            const std::string& input, std::vector<typename DType::c_type>* 
output,
+            size_t output_index) {
+  if (ARROW_PREDICT_FALSE(output_index >= output->size())) {
+    throw ParquetException("Index out of bound");
+  }
+
+  decoder->SetData(/*num_values=*/1, reinterpret_cast<const 
uint8_t*>(input.c_str()),
+                   static_cast<int>(input.size()));
+  const auto num_values = decoder->Decode(&output->at(output_index), 
/*max_values=*/1);
+  if (ARROW_PREDICT_FALSE(num_values != 1)) {
+    throw ParquetException("Could not decode statistics value");
+  }
+}
+
+template <>
+void Decode<BooleanType>(std::unique_ptr<BooleanDecoder>& decoder,
+                         const std::string& input, std::vector<bool>* output,
+                         size_t output_index) {
+  if (ARROW_PREDICT_FALSE(output_index >= output->size())) {
+    throw ParquetException("Index out of bound");
+  }
+
+  bool value;
+  decoder->SetData(/*num_values=*/1, reinterpret_cast<const 
uint8_t*>(input.c_str()),
+                   static_cast<int>(input.size()));
+  const auto num_values = decoder->Decode(&value, /*max_values=*/1);
+  if (ARROW_PREDICT_FALSE(num_values != 1)) {
+    throw ParquetException("Could not decode statistics value");
+  }
+  output->at(output_index) = value;
+}
+
+template <>
+void Decode<ByteArrayType>(std::unique_ptr<ByteArrayDecoder>&, const 
std::string& input,
+                           std::vector<ByteArray>* output, size_t 
output_index) {
+  if (ARROW_PREDICT_FALSE(output_index >= output->size())) {
+    throw ParquetException("Index out of bound");
+  }
+
+  if (ARROW_PREDICT_FALSE(input.size() >
+                          
static_cast<size_t>(std::numeric_limits<uint32_t>::max()))) {
+    throw ParquetException("Invalid encoded byte array length");
+  }
+
+  output->at(output_index) = {/*len=*/static_cast<uint32_t>(input.size()),
+                              /*ptr=*/reinterpret_cast<const 
uint8_t*>(input.data())};
+}
+
+template <typename DType>
+class TypedColumnIndexImpl : public TypedColumnIndex<DType> {
+ public:
+  using T = typename DType::c_type;
+
+  TypedColumnIndexImpl(const ColumnDescriptor& descr,
+                       const format::ColumnIndex& column_index)
+      : column_index_(column_index) {
+    // Make sure the number of pages is valid and it does not overflow to 
int32_t.
+    const size_t num_pages = column_index_.null_pages.size();
+    if (num_pages >= static_cast<size_t>(std::numeric_limits<int32_t>::max()) 
||
+        column_index_.min_values.size() != num_pages ||
+        column_index_.max_values.size() != num_pages ||
+        (column_index_.__isset.null_counts &&
+         column_index_.null_counts.size() != num_pages)) {
+      throw ParquetException("Invalid column index");
+    }
+
+    const size_t num_non_null_pages = static_cast<size_t>(std::accumulate(
+        column_index_.null_pages.cbegin(), column_index_.null_pages.cend(), 0,
+        [](int32_t num_non_null_pages, bool null_page) {
+          return num_non_null_pages + (null_page ? 0 : 1);
+        }));
+    DCHECK_LE(num_non_null_pages, num_pages);
+
+    // Allocate slots for decoded values.
+    min_values_.resize(num_non_null_pages);

Review Comment:
   Sorry, I was reading it wrong again. My bad!



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