-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[df] Fix casting of projected cardinality fields to int #21738
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jblomer
wants to merge
5
commits into
root-project:master
Choose a base branch
from
jblomer:ntuple-fix-ds-cardinality
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
13ce8b8
[df] simple test for RNTupleCardinality field
jblomer cbbb7dd
[df] make RRDFCardinalityField templated
jblomer 13fae1a
[df] present RNTuple cardinality fields as integer
jblomer 8ed2edf
[df] allow for alternative int types for cardinality columns
jblomer a7fa47f
[df] integer overflow check for RNTuple cardinality columns
jblomer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,11 +27,13 @@ | |
| #include <TSystem.h> | ||
|
|
||
| #include <cassert> | ||
| #include <limits> | ||
| #include <memory> | ||
| #include <mutex> | ||
| #include <string> | ||
| #include <vector> | ||
| #include <typeinfo> | ||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| // clang-format off | ||
|
|
@@ -49,32 +51,29 @@ | |
| // clang-format on | ||
|
|
||
| namespace ROOT::Internal::RDF { | ||
| /// An artificial field that transforms an RNTuple column that contains the offset of collections into | ||
| /// collection sizes. It is used to provide the "number of" RDF columns for collections, e.g. | ||
| /// `R_rdf_sizeof_jets` for a collection named `jets`. | ||
| /// | ||
| /// This field owns the collection offset field but instead of exposing the collection offsets it exposes | ||
| /// the collection sizes (offset(N+1) - offset(N)). For the time being, we offer this functionality only in RDataFrame. | ||
| /// TODO(jblomer): consider providing a general set of useful virtual fields as part of RNTuple. | ||
| class RRDFCardinalityField final : public ROOT::RFieldBase { | ||
| class RRDFCardinalityFieldBase : public ROOT::RFieldBase { | ||
| protected: | ||
| std::unique_ptr<ROOT::RFieldBase> CloneImpl(std::string_view newName) const final | ||
| { | ||
| return std::make_unique<RRDFCardinalityField>(newName); | ||
| } | ||
| void ConstructValue(void *where) const final { *static_cast<std::size_t *>(where) = 0; } | ||
|
|
||
| // We construct these fields and know that they match the page source | ||
| void ReconcileOnDiskField(const RNTupleDescriptor &) final {} | ||
|
|
||
| public: | ||
| RRDFCardinalityField(std::string_view name) | ||
| : ROOT::RFieldBase(name, "std::size_t", ROOT::ENTupleStructure::kPlain, false /* isSimple */) | ||
| RRDFCardinalityFieldBase(std::string_view name, std::string_view type) | ||
| : ROOT::RFieldBase(name, type, ROOT::ENTupleStructure::kPlain, false /* isSimple */) | ||
| { | ||
| } | ||
| RRDFCardinalityField(RRDFCardinalityField &&other) = default; | ||
| RRDFCardinalityField &operator=(RRDFCardinalityField &&other) = default; | ||
| ~RRDFCardinalityField() override = default; | ||
|
|
||
| // Field is only used for reading | ||
| void GenerateColumns() final { throw RException(R__FAIL("Cardinality fields must only be used for reading")); } | ||
| void GenerateColumns(const ROOT::RNTupleDescriptor &desc) final | ||
| { | ||
| GenerateColumnsImpl<ROOT::Internal::RColumnIndex>(desc); | ||
| } | ||
|
|
||
| public: | ||
| RRDFCardinalityFieldBase(const RRDFCardinalityFieldBase &other) = delete; | ||
| RRDFCardinalityFieldBase &operator=(const RRDFCardinalityFieldBase &other) = delete; | ||
| RRDFCardinalityFieldBase(RRDFCardinalityFieldBase &&other) = default; | ||
| RRDFCardinalityFieldBase &operator=(RRDFCardinalityFieldBase &&other) = default; | ||
| ~RRDFCardinalityFieldBase() override = default; | ||
|
jblomer marked this conversation as resolved.
|
||
|
|
||
| const RColumnRepresentations &GetColumnRepresentations() const final | ||
| { | ||
|
|
@@ -85,23 +84,57 @@ class RRDFCardinalityField final : public ROOT::RFieldBase { | |
| {}); | ||
| return representations; | ||
| } | ||
| // Field is only used for reading | ||
| void GenerateColumns() final { throw RException(R__FAIL("Cardinality fields must only be used for reading")); } | ||
| void GenerateColumns(const ROOT::RNTupleDescriptor &desc) final | ||
| }; | ||
|
|
||
| /// An artificial field that transforms an RNTuple column that contains the offset of collections into | ||
| /// collection sizes. It is used to provide the "number of" RDF columns for collections, e.g. | ||
| /// `R_rdf_sizeof_jets` for a collection named `jets`. | ||
| /// | ||
| /// This is similar to the RCardinalityField but it presents itself as an integer type. | ||
| /// The template argument T must be an integral type. | ||
| template <typename T> | ||
|
vepadulano marked this conversation as resolved.
|
||
| class RRDFCardinalityField final : public RRDFCardinalityFieldBase { | ||
| static_assert(std::is_integral_v<T>, "T must be an integral type"); | ||
|
|
||
| inline void CheckSize(ROOT::NTupleSize_t size) const | ||
| { | ||
| if constexpr (std::is_same_v<T, bool> || std::is_same_v<T, std::uint64_t>) | ||
| return; | ||
| if (size > std::numeric_limits<T>::max()) { | ||
| throw RException(R__FAIL(std::string("integer overflow in field ") + GetFieldName() + | ||
| ". Please read the column with a larger-sized integral type.")); | ||
| } | ||
| } | ||
|
|
||
| protected: | ||
| std::unique_ptr<ROOT::RFieldBase> CloneImpl(std::string_view newName) const final | ||
| { | ||
| return std::make_unique<RRDFCardinalityField>(newName); | ||
| } | ||
| void ConstructValue(void *where) const final { *static_cast<T *>(where) = 0; } | ||
|
|
||
| public: | ||
| RRDFCardinalityField(std::string_view name) | ||
| : RRDFCardinalityFieldBase(name, ROOT::Internal::GetRenormalizedTypeName(typeid(T))) | ||
| { | ||
| GenerateColumnsImpl<ROOT::Internal::RColumnIndex>(desc); | ||
| } | ||
| RRDFCardinalityField(const RRDFCardinalityField &other) = delete; | ||
| RRDFCardinalityField &operator=(const RRDFCardinalityField &other) = delete; | ||
| RRDFCardinalityField(RRDFCardinalityField &&other) = default; | ||
| RRDFCardinalityField &operator=(RRDFCardinalityField &&other) = default; | ||
| ~RRDFCardinalityField() override = default; | ||
|
jblomer marked this conversation as resolved.
|
||
|
|
||
| size_t GetValueSize() const final { return sizeof(std::size_t); } | ||
| size_t GetAlignment() const final { return alignof(std::size_t); } | ||
| std::size_t GetValueSize() const final { return sizeof(T); } | ||
| std::size_t GetAlignment() const final { return alignof(T); } | ||
|
|
||
| /// Get the number of elements of the collection identified by globalIndex | ||
| void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final | ||
| { | ||
| RNTupleLocalIndex collectionStart; | ||
| ROOT::NTupleSize_t size; | ||
| fPrincipalColumn->GetCollectionInfo(globalIndex, &collectionStart, &size); | ||
| *static_cast<std::size_t *>(to) = size; | ||
| CheckSize(size); | ||
| *static_cast<T *>(to) = size; | ||
| } | ||
|
|
||
| /// Get the number of elements of the collection identified by clusterIndex | ||
|
|
@@ -110,7 +143,8 @@ class RRDFCardinalityField final : public ROOT::RFieldBase { | |
| RNTupleLocalIndex collectionStart; | ||
| ROOT::NTupleSize_t size; | ||
| fPrincipalColumn->GetCollectionInfo(localIndex, &collectionStart, &size); | ||
| *static_cast<std::size_t *>(to) = size; | ||
| CheckSize(size); | ||
| *static_cast<T *>(to) = size; | ||
| } | ||
| }; | ||
|
|
||
|
|
@@ -144,7 +178,8 @@ class RArraySizeField final : public ROOT::RFieldBase { | |
|
|
||
| public: | ||
| RArraySizeField(std::string_view name, std::size_t arrayLength) | ||
| : ROOT::RFieldBase(name, "std::size_t", ROOT::ENTupleStructure::kPlain, false /* isSimple */), | ||
| : ROOT::RFieldBase(name, ROOT::Internal::GetRenormalizedTypeName(typeid(std::size_t)), | ||
| ROOT::ENTupleStructure::kPlain, false /* isSimple */), | ||
|
Comment on lines
+168
to
+169
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is this related? |
||
| fArrayLength(arrayLength) | ||
| { | ||
| } | ||
|
|
@@ -325,6 +360,18 @@ void ROOT::RDF::RNTupleDS::AddField(const ROOT::RNTupleDescriptor &desc, std::st | |
| if (!fieldOrException) | ||
| return; | ||
| auto valueField = fieldOrException.Unwrap(); | ||
| if (const auto cardinalityField = dynamic_cast<const ROOT::RCardinalityField *>(valueField.get())) { | ||
| // Cardinality fields in RDataFrame are presented as integers | ||
| if (cardinalityField->As32Bit()) { | ||
| valueField = | ||
| std::make_unique<ROOT::Internal::RDF::RRDFCardinalityField<std::uint32_t>>(fieldDesc.GetFieldName()); | ||
| } else if (cardinalityField->As64Bit()) { | ||
| valueField = | ||
| std::make_unique<ROOT::Internal::RDF::RRDFCardinalityField<std::uint64_t>>(fieldDesc.GetFieldName()); | ||
| } else { | ||
| R__ASSERT(false && "cardinality field stored with an unexpected integer type"); | ||
| } | ||
| } | ||
| valueField->SetOnDiskId(fieldId); | ||
| for (auto &f : *valueField) { | ||
| f.SetOnDiskId(desc.FindFieldId(f.GetFieldName(), f.GetParent()->GetOnDiskId())); | ||
|
|
@@ -337,7 +384,7 @@ void ROOT::RDF::RNTupleDS::AddField(const ROOT::RNTupleDescriptor &desc, std::st | |
| if (info.fNRepetitions > 0) { | ||
| cardinalityField = std::make_unique<ROOT::Internal::RDF::RArraySizeField>(name, info.fNRepetitions); | ||
| } else { | ||
| cardinalityField = std::make_unique<ROOT::Internal::RDF::RRDFCardinalityField>(name); | ||
| cardinalityField = std::make_unique<ROOT::Internal::RDF::RRDFCardinalityField<std::size_t>>(name); | ||
| } | ||
| cardinalityField->SetOnDiskId(info.fFieldId); | ||
| } | ||
|
|
@@ -475,7 +522,7 @@ ROOT::RFieldBase *ROOT::RDF::RNTupleDS::GetFieldWithTypeChecks(std::string_view | |
| // If the field corresponding to the provided name is not a cardinality column and the requested type is different | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the comment? |
||
| // from the proto field that was created when the data source was constructed, we first have to create an | ||
| // alternative proto field for the column reader. Otherwise, we can directly use the existing proto field. | ||
| if (fieldName.substr(0, 13) != "R_rdf_sizeof_" && requestedType != fColumnTypes[index]) { | ||
| if (requestedType != fColumnTypes[index]) { | ||
| auto &altProtoFields = fAlternativeProtoFields[index]; | ||
|
|
||
| // If we can find the requested type in the registered alternative protofields, return the corresponding field | ||
|
|
@@ -488,12 +535,41 @@ ROOT::RFieldBase *ROOT::RDF::RNTupleDS::GetFieldWithTypeChecks(std::string_view | |
| } | ||
|
|
||
| // Otherwise, create a new protofield and register it in the alternatives before returning | ||
| auto newAltProtoFieldOrException = ROOT::RFieldBase::Create(std::string(fieldName), requestedType); | ||
| if (!newAltProtoFieldOrException) { | ||
| throw std::runtime_error("RNTupleDS: Could not create field with type \"" + requestedType + | ||
| "\" for column \"" + std::string(fieldName) + "\""); | ||
| std::unique_ptr<RFieldBase> newAltProtoField; | ||
| const std::string strName = std::string(fieldName); | ||
| if (dynamic_cast<ROOT::Internal::RDF::RRDFCardinalityFieldBase *>(fProtoFields[index].get())) { | ||
| if (requestedType == "bool") { | ||
| newAltProtoField = std::make_unique<ROOT::Internal::RDF::RRDFCardinalityField<bool>>(strName); | ||
| } else if (requestedType == "char") { | ||
| newAltProtoField = std::make_unique<ROOT::Internal::RDF::RRDFCardinalityField<char>>(strName); | ||
| } else if (requestedType == "std::int8_t") { | ||
| newAltProtoField = std::make_unique<ROOT::Internal::RDF::RRDFCardinalityField<std::int8_t>>(strName); | ||
| } else if (requestedType == "std::uint8_t") { | ||
| newAltProtoField = std::make_unique<ROOT::Internal::RDF::RRDFCardinalityField<std::uint8_t>>(strName); | ||
| } else if (requestedType == "std::int16_t") { | ||
| newAltProtoField = std::make_unique<ROOT::Internal::RDF::RRDFCardinalityField<std::int16_t>>(strName); | ||
| } else if (requestedType == "std::uint16_t") { | ||
| newAltProtoField = std::make_unique<ROOT::Internal::RDF::RRDFCardinalityField<std::uint16_t>>(strName); | ||
| } else if (requestedType == "std::int32_t") { | ||
|
vepadulano marked this conversation as resolved.
|
||
| newAltProtoField = std::make_unique<ROOT::Internal::RDF::RRDFCardinalityField<std::int32_t>>(strName); | ||
| } else if (requestedType == "std::uint32_t") { | ||
| newAltProtoField = std::make_unique<ROOT::Internal::RDF::RRDFCardinalityField<std::uint32_t>>(strName); | ||
| } else if (requestedType == "std::int64_t") { | ||
| newAltProtoField = std::make_unique<ROOT::Internal::RDF::RRDFCardinalityField<std::int64_t>>(strName); | ||
| } else if (requestedType == "std::uint64_t") { | ||
| newAltProtoField = std::make_unique<ROOT::Internal::RDF::RRDFCardinalityField<std::uint64_t>>(strName); | ||
| } else { | ||
| throw std::runtime_error("RNTupleDS: Could not create field with type \"" + requestedType + | ||
| "\" for column \"" + std::string(fieldName) + "\""); | ||
| } | ||
| } else { | ||
| auto newAltProtoFieldOrException = ROOT::RFieldBase::Create(strName, requestedType); | ||
| if (!newAltProtoFieldOrException) { | ||
| throw std::runtime_error("RNTupleDS: Could not create field with type \"" + requestedType + | ||
| "\" for column \"" + std::string(fieldName) + "\""); | ||
| } | ||
| newAltProtoField = newAltProtoFieldOrException.Unwrap(); | ||
| } | ||
| auto newAltProtoField = newAltProtoFieldOrException.Unwrap(); | ||
| newAltProtoField->SetOnDiskId(fProtoFields[index]->GetOnDiskId()); | ||
| auto *newField = newAltProtoField.get(); | ||
| altProtoFields.emplace_back(std::move(newAltProtoField)); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.