michalursa commented on a change in pull request #11150: URL: https://github.com/apache/arrow/pull/11150#discussion_r717144846
########## File path: cpp/src/arrow/compute/exec/schema_util.h ########## @@ -0,0 +1,197 @@ +// 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 <cstdint> +#include <memory> +#include <string> +#include <vector> + +#include "arrow/compute/exec/key_encode.h" // for KeyColumnMetadata +#include "arrow/type.h" // for DataType, FieldRef, Field and Schema +#include "arrow/util/mutex.h" + +namespace arrow { + +using internal::checked_cast; + +namespace compute { + +// Helper class for managing related row schemas. +// Used to efficiently map any column in one schema to a corresponding column in another +// schema if such exists. +// Materialized mappings are generated lazily at the time of the first access. +// Thread-safe apart from initialization. +// +template <typename SchemaHandleType> +class FieldMap { + public: + static constexpr int kMissingField = -1; + + void RegisterSchema(SchemaHandleType handle, const Schema& schema) { + std::vector<FieldInfo> out_fields; + const FieldVector& in_fields = schema.fields(); + out_fields.resize(in_fields.size()); + for (size_t i = 0; i < in_fields.size(); ++i) { + const std::string& name = in_fields[i]->name(); + const std::shared_ptr<DataType>& type = in_fields[i]->type(); + out_fields[i].field_ref = FieldRef(name); + out_fields[i].data_type = type; + out_fields[i].column_metadata = ColumnMetadataFromDataType(type); + } + schemas_.push_back(std::make_pair(handle, out_fields)); + } + + Status RegisterProjectedSchema(SchemaHandleType handle, + const std::vector<FieldRef>& selected_fields, + const Schema& full_schema) { + std::vector<FieldInfo> out_fields; + const FieldVector& in_fields = full_schema.fields(); + out_fields.resize(selected_fields.size()); + for (size_t i = 0; i < selected_fields.size(); ++i) { + // All fields must be found in schema without ambiguity + ARROW_ASSIGN_OR_RAISE(auto match, selected_fields[i].FindOne(full_schema)); + const std::string& name = in_fields[match[0]]->name(); + const std::shared_ptr<DataType>& type = in_fields[match[0]]->type(); + out_fields[i].field_ref = FieldRef(name); + out_fields[i].data_type = type; + out_fields[i].column_metadata = ColumnMetadataFromDataType(type); + } + schemas_.push_back(std::make_pair(handle, out_fields)); + return Status::OK(); + } + + void RegisterEnd() { + size_t size = schemas_.size(); + mapping_ptrs_.resize(size * size); + mapping_bufs_.resize(size * size); + } + + int num_cols(SchemaHandleType schema_handle) const { + int id = schema_id(schema_handle); + return static_cast<int>(schemas_[id].second.size()); + } + + const KeyEncoder::KeyColumnMetadata& column_metadata(SchemaHandleType schema_handle, + int field_id) const { + return field(schema_handle, field_id).column_metadata; + } + + const FieldRef& field_ref(SchemaHandleType schema_handle, int field_id) const { + return field(schema_handle, field_id).field_ref; + } + + const std::shared_ptr<DataType>& data_type(SchemaHandleType schema_handle, + int field_id) const { + return field(schema_handle, field_id).data_type; + } + + const int* map(SchemaHandleType from, SchemaHandleType to) { + int id_from = schema_id(from); + int id_to = schema_id(to); + int num_schemas = static_cast<int>(schemas_.size()); + int pos = id_from * num_schemas + id_to; + const int* ptr = mapping_ptrs_[pos]; + if (!ptr) { + auto guard = mutex_.Lock(); // acquire the lock + if (!ptr) { + GenerateMap(id_from, id_to); + } + ptr = mapping_ptrs_[pos]; + } + return ptr; + } + + protected: + struct FieldInfo { + FieldRef field_ref; + std::shared_ptr<DataType> data_type; + KeyEncoder::KeyColumnMetadata column_metadata; + }; + + KeyEncoder::KeyColumnMetadata ColumnMetadataFromDataType( + const std::shared_ptr<DataType>& type) { + if (type->id() == Type::DICTIONARY) { + auto bit_width = checked_cast<const FixedWidthType&>(*type).bit_width(); + ARROW_DCHECK(bit_width % 8 == 0); + return KeyEncoder::KeyColumnMetadata(true, bit_width / 8); + } else if (type->id() == Type::BOOL) { + return KeyEncoder::KeyColumnMetadata(true, 0); + } else if (is_fixed_width(type->id())) { + return KeyEncoder::KeyColumnMetadata( + true, checked_cast<const FixedWidthType&>(*type).bit_width() / 8); + } else if (is_binary_like(type->id())) { + return KeyEncoder::KeyColumnMetadata(false, sizeof(uint32_t)); + } else { + ARROW_DCHECK(false); + return KeyEncoder::KeyColumnMetadata(true, 0); + } + } + + int schema_id(SchemaHandleType schema_handle) const { + for (size_t i = 0; i < schemas_.size(); ++i) { + if (schemas_[i].first == schema_handle) { + return static_cast<int>(i); + } + } + // We should never get here + ARROW_DCHECK(false); + return -1; + } + + const FieldInfo& field(SchemaHandleType schema_handle, int field_id) const { + int id = schema_id(schema_handle); + const std::vector<FieldInfo>& field_infos = schemas_[id].second; + return field_infos[field_id]; + } + + void GenerateMap(int id_from, int id_to) { + int num_schemas = static_cast<int>(schemas_.size()); + int pos = id_from * num_schemas + id_to; + + int num_cols_from = static_cast<int>(schemas_[id_from].second.size()); + int num_cols_to = static_cast<int>(schemas_[id_to].second.size()); + mapping_bufs_[pos].resize(num_cols_from); + const std::vector<FieldInfo>& fields_from = schemas_[id_from].second; + const std::vector<FieldInfo>& fields_to = schemas_[id_to].second; + for (int i = 0; i < num_cols_from; ++i) { + int field_id = kMissingField; + for (int j = 0; j < num_cols_to; ++j) { + if (fields_from[i].field_ref.Equals(fields_to[j].field_ref) && + fields_from[i].data_type->Equals(*fields_to[j].data_type)) { + ARROW_DCHECK(fields_from[i].column_metadata.is_fixed_length == + fields_to[j].column_metadata.is_fixed_length && + fields_from[i].column_metadata.fixed_length == + fields_to[j].column_metadata.fixed_length); + field_id = j; + break; + } + } + mapping_bufs_[pos][i] = field_id; + } + mapping_ptrs_[pos] = mapping_bufs_[pos].data(); + } + + std::vector<int*> mapping_ptrs_; + std::vector<std::vector<int>> mapping_bufs_; + std::vector<std::pair<SchemaHandleType, std::vector<FieldInfo>>> schemas_; Review comment: comment added -- 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]
