|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include "velox/connectors/hive/iceberg/DataFileStatsCollector.h" |
| 17 | +#include "velox/common/base/Exceptions.h" |
| 18 | +#include "velox/common/encode/Base64.h" |
| 19 | +#include "velox/dwio/parquet/writer/arrow/Metadata.h" |
| 20 | +#include "velox/dwio/parquet/writer/arrow/Statistics.h" |
| 21 | + |
| 22 | +namespace facebook::velox::connector::hive::iceberg { |
| 23 | + |
| 24 | +using namespace facebook::velox::parquet; |
| 25 | + |
| 26 | +DataFileStatsCollector::DataFileStatsCollector( |
| 27 | + std::shared_ptr< |
| 28 | + std::vector<std::unique_ptr<dwio::common::DataFileStatsSettings>>> |
| 29 | + settings) |
| 30 | + : FileStatsCollector(std::move(settings)) {} |
| 31 | + |
| 32 | +void DataFileStatsCollector::collectStats( |
| 33 | + const void* metadata, |
| 34 | + const std::shared_ptr<dwio::common::DataFileStatistics>& dataFileStats) { |
| 35 | + const auto& fileMetadata = |
| 36 | + *static_cast<const std::shared_ptr<parquet::arrow::FileMetaData>*>( |
| 37 | + metadata); |
| 38 | + VELOX_CHECK_NOT_NULL(fileMetadata); |
| 39 | + |
| 40 | + std::unordered_set<int32_t> skipBoundsFields; |
| 41 | + std::function<int32_t(IcebergDataFileStatsSettings*)> processFields = |
| 42 | + [&skipBoundsFields, |
| 43 | + &processFields](IcebergDataFileStatsSettings* field) -> int32_t { |
| 44 | + if (field->skipBounds) { |
| 45 | + skipBoundsFields.insert(field->fieldId); |
| 46 | + } |
| 47 | + if (field->children.empty()) { |
| 48 | + return 1; |
| 49 | + } |
| 50 | + int32_t count = 0; |
| 51 | + for (const auto& child : field->children) { |
| 52 | + count += processFields(child.get()); |
| 53 | + } |
| 54 | + return count; |
| 55 | + }; |
| 56 | + |
| 57 | + // numFields is not the number of columns in Iceberg table's schema, |
| 58 | + // e.g., schema_->size(). It also contains the sub-fields when there are |
| 59 | + // nested types in table's schema. |
| 60 | + int32_t numFields = 0; |
| 61 | + for (const auto& field : *statsSetting_) { |
| 62 | + auto* icebergField = |
| 63 | + static_cast<IcebergDataFileStatsSettings*>(field.get()); |
| 64 | + numFields += processFields(icebergField); |
| 65 | + } |
| 66 | + |
| 67 | + std::unordered_map<int32_t, std::shared_ptr<arrow::Statistics>> |
| 68 | + globalMinStats; |
| 69 | + std::unordered_map<int32_t, std::shared_ptr<arrow::Statistics>> |
| 70 | + globalMaxStats; |
| 71 | + |
| 72 | + dataFileStats->numRecords = fileMetadata->num_rows(); |
| 73 | + const auto numRowGroups = fileMetadata->num_row_groups(); |
| 74 | + for (auto i = 0; i < numRowGroups; ++i) { |
| 75 | + const auto rgm = fileMetadata->RowGroup(i); |
| 76 | + VELOX_CHECK_EQ(numFields, rgm->num_columns()); |
| 77 | + dataFileStats->splitOffsets.emplace_back(rgm->file_offset()); |
| 78 | + |
| 79 | + for (auto j = 0; j < numFields; ++j) { |
| 80 | + const auto columnChunkMetadata = rgm->ColumnChunk(j); |
| 81 | + const auto fieldId = columnChunkMetadata->field_id(); |
| 82 | + const auto numValues = columnChunkMetadata->num_values(); |
| 83 | + |
| 84 | + dataFileStats->valueCounts[fieldId] += numValues; |
| 85 | + dataFileStats->columnsSizes[fieldId] += |
| 86 | + columnChunkMetadata->total_compressed_size(); |
| 87 | + |
| 88 | + const auto columnChunkStats = columnChunkMetadata->statistics(); |
| 89 | + if (columnChunkStats->nan_count() > 0) { |
| 90 | + dataFileStats->nanValueCounts[fieldId] += columnChunkStats->nan_count(); |
| 91 | + } |
| 92 | + dataFileStats->nullValueCounts[fieldId] += columnChunkStats->null_count(); |
| 93 | + |
| 94 | + if (columnChunkStats->HasMinMax() && |
| 95 | + !skipBoundsFields.contains(fieldId)) { |
| 96 | + if (globalMaxStats.find(fieldId) == globalMaxStats.end()) { |
| 97 | + globalMinStats[fieldId] = columnChunkStats; |
| 98 | + globalMaxStats[fieldId] = columnChunkStats; |
| 99 | + } else { |
| 100 | + globalMaxStats[fieldId] = arrow::Statistics::CompareAndGetMax( |
| 101 | + globalMaxStats[fieldId], columnChunkStats); |
| 102 | + globalMinStats[fieldId] = arrow::Statistics::CompareAndGetMin( |
| 103 | + globalMinStats[fieldId], columnChunkStats); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + for (const auto& [fieldId, minStats] : globalMinStats) { |
| 110 | + const auto lowerBound = minStats->MinValue(); |
| 111 | + dataFileStats->lowerBounds[fieldId] = |
| 112 | + encoding::Base64::encode(lowerBound.data(), lowerBound.size()); |
| 113 | + } |
| 114 | + for (const auto& [fieldId, maxStats] : globalMaxStats) { |
| 115 | + const auto upperBound = maxStats->MaxValue(); |
| 116 | + dataFileStats->upperBounds[fieldId] = |
| 117 | + encoding::Base64::encode(upperBound.data(), upperBound.size()); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +} // namespace facebook::velox::connector::hive::iceberg |
0 commit comments