kou commented on code in PR #42133: URL: https://github.com/apache/arrow/pull/42133#discussion_r1639122336
########## cpp/src/arrow/array/statistics.h: ########## @@ -0,0 +1,84 @@ +// 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 <optional> +#include <variant> + +#include "arrow/util/visibility.h" + +namespace arrow { + +/// \brief Statistics for an Array +/// +/// Apache Arrow format doesn't have statistics but data source such +/// as Apache Parquet may have statistics. Statistics associate with +/// data source can be read unified API via this class. +struct ARROW_EXPORT ArrayStatistics { + public: + using ElementBufferType = std::variant<bool, int8_t, uint8_t, int16_t, uint16_t, + int32_t, uint32_t, int64_t, uint64_t>; + + ArrayStatistics() = default; + ~ArrayStatistics() = default; + + /// \brief The number of null values, may not be set + std::optional<int64_t> null_count = std::nullopt; + + /// \brief The number of distinct values, may not be set + std::optional<int64_t> distinct_count = std::nullopt; Review Comment: You're right. This is not useful for now because Parquet C++ doesn't populate it. I just add this because `parquet::Statistics` has this. ########## cpp/src/arrow/array/statistics.h: ########## @@ -0,0 +1,84 @@ +// 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 <optional> +#include <variant> + +#include "arrow/util/visibility.h" + +namespace arrow { + +/// \brief Statistics for an Array +/// +/// Apache Arrow format doesn't have statistics but data source such +/// as Apache Parquet may have statistics. Statistics associate with +/// data source can be read unified API via this class. +struct ARROW_EXPORT ArrayStatistics { + public: + using ElementBufferType = std::variant<bool, int8_t, uint8_t, int16_t, uint16_t, + int32_t, uint32_t, int64_t, uint64_t>; + + ArrayStatistics() = default; + ~ArrayStatistics() = default; + + /// \brief The number of null values, may not be set + std::optional<int64_t> null_count = std::nullopt; + + /// \brief The number of distinct values, may not be set + std::optional<int64_t> distinct_count = std::nullopt; + + /// \brief The current minimum value buffer, may not be set + std::optional<ElementBufferType> min_buffer = std::nullopt; Review Comment: Thanks. I didn't know that Parquet has exact or not for min/max values: https://github.com/apache/arrow/blob/d078d5cafc53823f23bdab9918611b65a54f21be/cpp/src/parquet/parquet.thrift#L265-L282 Let's add `is_min_exact`/`is_max_exact`. ########## cpp/src/arrow/array/statistics.h: ########## @@ -0,0 +1,84 @@ +// 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 <optional> +#include <variant> + +#include "arrow/util/visibility.h" + +namespace arrow { + +/// \brief Statistics for an Array +/// +/// Apache Arrow format doesn't have statistics but data source such +/// as Apache Parquet may have statistics. Statistics associate with +/// data source can be read unified API via this class. +struct ARROW_EXPORT ArrayStatistics { + public: + using ElementBufferType = std::variant<bool, int8_t, uint8_t, int16_t, uint16_t, Review Comment: > Apart from var-len type, how do we support timestamp, internal and other primitive types? Ah, we need `arrow::DataType` for them. We can add it to `ArrayStatistics` or users can use it in associated `ArrayData`. The latter is a bit difficult to use but it may be preferred because it can reduce needed memory a bit. > should we add an optional buffer to store values for string/binary types? Thus we can use std::string_view to represent them. If the min/max values are exact values, we can point them to the values in the arrow array and do not store any value in the optional buffer. This is a discussion point in the PR description. In general, I think that `std::string_view` is better but there is a problem where we should store referred data as you mentioned. It may be costly to find the min/max values in the Arrow array because statistics provided by data source will not provide the value position. ########## cpp/src/parquet/arrow/reader_internal.cc: ########## @@ -330,15 +331,30 @@ Status TransferInt(RecordReader* reader, MemoryPool* pool, auto values = reinterpret_cast<const ParquetCType*>(reader->values()); auto out_ptr = reinterpret_cast<ArrowCType*>(data->mutable_data()); std::copy(values, values + length, out_ptr); + int64_t null_count = 0; + std::vector<std::shared_ptr<Buffer>> buffers = {nullptr, std::move(data)}; if (field->nullable()) { - *out = std::make_shared<ArrayType<ArrowType>>(field->type(), length, std::move(data), - reader->ReleaseIsValid(), - reader->null_count()); - } else { - *out = - std::make_shared<ArrayType<ArrowType>>(field->type(), length, std::move(data), - /*null_bitmap=*/nullptr, /*null_count=*/0); + null_count = reader->null_count(); + buffers[0] = reader->ReleaseIsValid(); + } + auto array_data = + ::arrow::ArrayData::Make(field->type(), length, std::move(buffers), null_count); + array_data->statistics.null_count = null_count; + auto statistics = metadata->statistics().get(); + if (statistics) { + if (statistics->HasDistinctCount()) { + array_data->statistics.distinct_count = statistics->distinct_count(); + } + if (statistics->HasMinMax()) { + auto typed_statistics = + static_cast<::parquet::TypedStatistics<ParquetType>*>(statistics); + array_data->statistics.min_buffer = + static_cast<ArrowCType>(typed_statistics->min()); + array_data->statistics.max_buffer = + static_cast<ArrowCType>(typed_statistics->max()); + } } + *out = std::make_shared<ArrayType<ArrowType>>(std::move(array_data)); Review Comment: Thanks for the information. I didn't know it. It seems that arrays in a chunked array correspond to column chunks in Parquet, right? If so, `ColumnChunkMetaData` for each column chunk has statistics for the column chunk, right? Or does it have statistics for the row group of the column chunk not the column chunk itself? (Does you mean that we can't associate `ColumnChunkMetaData::statistics()` information with `arrow::Array` because it has the statistics for row group not column chunk?) -- 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]
