|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and 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 | + |
| 17 | +#include "velox/dwio/nimble/velox/SharedDictionaryConfig.h" |
| 18 | + |
| 19 | +#include <algorithm> |
| 20 | +#include <iterator> |
| 21 | +#include <string> |
| 22 | +#include <string_view> |
| 23 | +#include <utility> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +#include "velox/dwio/nimble/common/Exceptions.h" |
| 27 | +#include "velox/type/Subfield.h" |
| 28 | + |
| 29 | +namespace facebook::nimble { |
| 30 | +namespace { |
| 31 | + |
| 32 | +velox::common::Subfield parseFieldPath( |
| 33 | + const std::string& fieldPath, |
| 34 | + std::string_view configType) { |
| 35 | + NIMBLE_USER_CHECK( |
| 36 | + !fieldPath.empty(), |
| 37 | + "Shared dictionary {} path must not be empty.", |
| 38 | + configType); |
| 39 | + velox::common::Subfield subfield; |
| 40 | + try { |
| 41 | + subfield = velox::common::Subfield{fieldPath}; |
| 42 | + } catch (const velox::VeloxException&) { |
| 43 | + NIMBLE_USER_FAIL( |
| 44 | + "Shared dictionary {} path '{}' must start with a field name.", |
| 45 | + configType, |
| 46 | + fieldPath); |
| 47 | + } |
| 48 | + NIMBLE_USER_CHECK( |
| 49 | + subfield.valid(), |
| 50 | + "Shared dictionary {} path '{}' must start with a field name.", |
| 51 | + configType, |
| 52 | + fieldPath); |
| 53 | + return subfield; |
| 54 | +} |
| 55 | + |
| 56 | +void validateValueStreamPath( |
| 57 | + const std::string& fieldPath, |
| 58 | + std::string_view configType) { |
| 59 | + const auto subfield = parseFieldPath(fieldPath, configType); |
| 60 | + for (const auto& pathElement : subfield.path()) { |
| 61 | + NIMBLE_USER_CHECK( |
| 62 | + pathElement->is(velox::common::SubfieldKind::kNestedField) || |
| 63 | + pathElement->is(velox::common::SubfieldKind::kAllSubscripts), |
| 64 | + "Shared dictionary {} path '{}' only supports nested row fields and " |
| 65 | + "all-subscript array/map elements.", |
| 66 | + configType, |
| 67 | + fieldPath); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +velox::common::Subfield validateFlatMapColumnPath( |
| 72 | + const std::string& fieldPath) { |
| 73 | + auto subfield = parseFieldPath(fieldPath, "flat-map column"); |
| 74 | + NIMBLE_USER_CHECK_EQ( |
| 75 | + subfield.path().size(), |
| 76 | + 1, |
| 77 | + "Shared dictionary flat-map column path '{}' must be a top-level writer " |
| 78 | + "input column.", |
| 79 | + fieldPath); |
| 80 | + return subfield; |
| 81 | +} |
| 82 | + |
| 83 | +void validateValueSubfield(const std::string& valueSubfield) { |
| 84 | + if (valueSubfield.empty()) { |
| 85 | + return; |
| 86 | + } |
| 87 | + const auto subfield = |
| 88 | + parseFieldPath(valueSubfield, "flat-map value subfield"); |
| 89 | + for (const auto& pathElement : subfield.path()) { |
| 90 | + NIMBLE_USER_CHECK( |
| 91 | + pathElement->is(velox::common::SubfieldKind::kNestedField) || |
| 92 | + pathElement->is(velox::common::SubfieldKind::kAllSubscripts), |
| 93 | + "Shared dictionary flat-map value subfield '{}' only supports nested " |
| 94 | + "row fields and all-subscript array/map elements.", |
| 95 | + valueSubfield); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +} // namespace |
| 100 | + |
| 101 | +SharedDictionaryConfigBuilder::SharedDictionaryConfigBuilder( |
| 102 | + SharedDictionaryEncodingConfig&& config) |
| 103 | + : config_{std::move(config)} {} |
| 104 | + |
| 105 | +SharedDictionaryConfigBuilder SharedDictionaryEncodingConfig::builder( |
| 106 | + SharedDictionaryEncodingConfig&& config) { |
| 107 | + return SharedDictionaryConfigBuilder{std::move(config)}; |
| 108 | +} |
| 109 | + |
| 110 | +SharedDictionaryConfigBuilder& |
| 111 | +SharedDictionaryConfigBuilder::setExternalResolver( |
| 112 | + std::shared_ptr<const ExternalDictionaryResolver> externalResolver) { |
| 113 | + config_.externalResolver = std::move(externalResolver); |
| 114 | + return *this; |
| 115 | +} |
| 116 | + |
| 117 | +SharedDictionaryConfigBuilder& |
| 118 | +SharedDictionaryConfigBuilder::addColumnDictionary( |
| 119 | + std::string fieldPath, |
| 120 | + SharedDictionaryConfig dictionary) { |
| 121 | + validateValueStreamPath(fieldPath, "column"); |
| 122 | + |
| 123 | + const auto duplicate = std::any_of( |
| 124 | + config_.columns.begin(), |
| 125 | + config_.columns.end(), |
| 126 | + [&](const auto& candidate) { return candidate.fieldPath == fieldPath; }); |
| 127 | + NIMBLE_USER_CHECK( |
| 128 | + !duplicate, |
| 129 | + "Duplicate shared dictionary column configuration for path '{}'.", |
| 130 | + fieldPath); |
| 131 | + config_.columns.push_back( |
| 132 | + ColumnDictionary{ |
| 133 | + .fieldPath = std::move(fieldPath), |
| 134 | + .dictionary = std::move(dictionary)}); |
| 135 | + return *this; |
| 136 | +} |
| 137 | + |
| 138 | +SharedDictionaryConfigBuilder& |
| 139 | +SharedDictionaryConfigBuilder::addFlatmapValueDictionary( |
| 140 | + std::string fieldPath, |
| 141 | + int64_t key, |
| 142 | + SharedDictionaryConfig dictionary, |
| 143 | + std::string valueSubfield) { |
| 144 | + const auto subfield = validateFlatMapColumnPath(fieldPath); |
| 145 | + validateValueSubfield(valueSubfield); |
| 146 | + |
| 147 | + auto column = std::find_if( |
| 148 | + config_.flatMapColumns.begin(), |
| 149 | + config_.flatMapColumns.end(), |
| 150 | + [&](const auto& candidate) { return candidate.fieldPath == fieldPath; }); |
| 151 | + if (column == config_.flatMapColumns.end()) { |
| 152 | + config_.flatMapColumns.push_back( |
| 153 | + FlatmapColumnDictionary{.fieldPath = fieldPath, .keys = {}}); |
| 154 | + column = std::prev(config_.flatMapColumns.end()); |
| 155 | + } |
| 156 | + |
| 157 | + const auto duplicate = std::any_of( |
| 158 | + column->keys.begin(), column->keys.end(), [&](const auto& item) { |
| 159 | + return item.key == key && item.valueSubfield == valueSubfield; |
| 160 | + }); |
| 161 | + NIMBLE_USER_CHECK( |
| 162 | + !duplicate, |
| 163 | + "Duplicate shared dictionary flat-map value configuration for path '{}', " |
| 164 | + "key {}, and value subfield '{}'.", |
| 165 | + fieldPath, |
| 166 | + key, |
| 167 | + valueSubfield); |
| 168 | + column->keys.push_back( |
| 169 | + FlatmapKeyDictionary{ |
| 170 | + .key = key, |
| 171 | + .valueSubfield = std::move(valueSubfield), |
| 172 | + .dictionary = std::move(dictionary)}); |
| 173 | + return *this; |
| 174 | +} |
| 175 | + |
| 176 | +SharedDictionaryEncodingConfig SharedDictionaryConfigBuilder::build() { |
| 177 | + return std::move(config_); |
| 178 | +} |
| 179 | + |
| 180 | +} // namespace facebook::nimble |
0 commit comments