-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[ntuple] fixes to subfield name handling #20629
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -38,11 +38,22 @@ void ROOT::Internal::SetAllowFieldSubstitutions(RFieldZero &fieldZero, bool val) | |||||||||||||||
| fieldZero.fAllowFieldSubstitutions = val; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| void ROOT::RFieldZero::Attach(std::unique_ptr<RFieldBase> child) | ||||||||||||||||
| { | ||||||||||||||||
| const std::string childName = child->GetFieldName(); | ||||||||||||||||
|
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.
Suggested change
to avoid the copy? |
||||||||||||||||
| if (fSubFieldNames.count(childName) > 0) | ||||||||||||||||
| throw RException(R__FAIL("duplicate field name: " + childName)); | ||||||||||||||||
| RFieldBase::Attach(std::move(child), ""); | ||||||||||||||||
| fSubFieldNames.insert(childName); | ||||||||||||||||
|
Comment on lines
+44
to
+47
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. Can do this in one go:
Suggested change
|
||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| std::unique_ptr<ROOT::RFieldBase> ROOT::RFieldZero::CloneImpl(std::string_view /*newName*/) const | ||||||||||||||||
| { | ||||||||||||||||
| auto result = std::make_unique<RFieldZero>(); | ||||||||||||||||
| for (auto &f : fSubfields) | ||||||||||||||||
| for (auto &f : fSubfields) { | ||||||||||||||||
| result->Attach(f->Clone(f->GetFieldName())); | ||||||||||||||||
| result->fSubFieldNames.insert(f->GetFieldName()); | ||||||||||||||||
| } | ||||||||||||||||
| return result; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -565,14 +576,15 @@ ROOT::RRecordField::RRecordField(std::string_view fieldName, std::string_view ty | |||||||||||||||
| { | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| void ROOT::RRecordField::AttachItemFields(std::vector<std::unique_ptr<RFieldBase>> itemFields) | ||||||||||||||||
| void ROOT::RRecordField::AttachItemFields(std::vector<std::unique_ptr<RFieldBase>> itemFields, bool useNumberedFields) | ||||||||||||||||
| { | ||||||||||||||||
| fTraits |= kTraitTrivialType; | ||||||||||||||||
| for (auto &item : itemFields) { | ||||||||||||||||
| fMaxAlignment = std::max(fMaxAlignment, item->GetAlignment()); | ||||||||||||||||
| fSize += GetItemPadding(fSize, item->GetAlignment()) + item->GetValueSize(); | ||||||||||||||||
| fTraits &= item->GetTraits(); | ||||||||||||||||
| Attach(std::move(item)); | ||||||||||||||||
| const auto N = itemFields.size(); | ||||||||||||||||
| for (std::size_t i = 0; i < N; ++i) { | ||||||||||||||||
| fMaxAlignment = std::max(fMaxAlignment, itemFields[i]->GetAlignment()); | ||||||||||||||||
| fSize += GetItemPadding(fSize, itemFields[i]->GetAlignment()) + itemFields[i]->GetValueSize(); | ||||||||||||||||
| fTraits &= itemFields[i]->GetTraits(); | ||||||||||||||||
| Attach(std::move(itemFields[i]), useNumberedFields ? ("_" + std::to_string(i)) : ""); | ||||||||||||||||
| } | ||||||||||||||||
| // Trailing padding: although this is implementation-dependent, most add enough padding to comply with the | ||||||||||||||||
| // requirements of the type with strictest alignment | ||||||||||||||||
|
|
@@ -602,13 +614,19 @@ ROOT::RRecordField::RRecordField(std::string_view fieldName, std::vector<std::un | |||||||||||||||
| { | ||||||||||||||||
| fTraits |= kTraitTrivialType; | ||||||||||||||||
| fOffsets.reserve(itemFields.size()); | ||||||||||||||||
| std::unordered_set<std::string> fieldNames; | ||||||||||||||||
|
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. For the local |
||||||||||||||||
| for (auto &item : itemFields) { | ||||||||||||||||
| const auto itemName = item->GetFieldName(); | ||||||||||||||||
|
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. This will copy the field name |
||||||||||||||||
| if (fieldNames.count(itemName) > 0) { | ||||||||||||||||
| throw RException(R__FAIL("duplicate field name: " + itemName)); | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+620
to
+622
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.
Suggested change
|
||||||||||||||||
| fSize += GetItemPadding(fSize, item->GetAlignment()); | ||||||||||||||||
| fOffsets.push_back(fSize); | ||||||||||||||||
| fMaxAlignment = std::max(fMaxAlignment, item->GetAlignment()); | ||||||||||||||||
| fSize += item->GetValueSize(); | ||||||||||||||||
| fTraits &= item->GetTraits(); | ||||||||||||||||
| Attach(std::move(item)); | ||||||||||||||||
| fieldNames.insert(itemName); | ||||||||||||||||
| } | ||||||||||||||||
| fTraits |= !emulatedFromType.empty() * kTraitEmulatedField; | ||||||||||||||||
| // Trailing padding: although this is implementation-dependent, most add enough padding to comply with the | ||||||||||||||||
|
|
@@ -842,7 +860,7 @@ ROOT::RNullableField::RNullableField(std::string_view fieldName, const std::stri | |||||||||||||||
| : ROOT::RFieldBase(fieldName, typePrefix + "<" + itemField->GetTypeName() + ">", ROOT::ENTupleStructure::kCollection, | ||||||||||||||||
| false /* isSimple */) | ||||||||||||||||
| { | ||||||||||||||||
| Attach(std::move(itemField)); | ||||||||||||||||
| Attach(std::move(itemField), "_0"); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| const ROOT::RFieldBase::RColumnRepresentations &ROOT::RNullableField::GetColumnRepresentations() const | ||||||||||||||||
|
|
@@ -1183,7 +1201,7 @@ ROOT::RAtomicField::RAtomicField(std::string_view fieldName, std::unique_ptr<RFi | |||||||||||||||
| fTraits |= kTraitTriviallyConstructible; | ||||||||||||||||
| if (itemField->GetTraits() & kTraitTriviallyDestructible) | ||||||||||||||||
| fTraits |= kTraitTriviallyDestructible; | ||||||||||||||||
| Attach(std::move(itemField)); | ||||||||||||||||
| Attach(std::move(itemField), "_0"); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| std::unique_ptr<ROOT::RFieldBase> ROOT::RAtomicField::CloneImpl(std::string_view newName) const | ||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| #include "ntuple_test.hxx" | ||
|
|
||
| TEST(RNTuple, SubFieldName) | ||
| { | ||
| EXPECT_EQ("_0", ROOT::RAtomicField("f", std::make_unique<RField<char>>("x")).begin()->GetFieldName()); | ||
| EXPECT_EQ("_0", ROOT::RVectorField("f", std::make_unique<RField<char>>("x")).begin()->GetFieldName()); | ||
| EXPECT_EQ("_0", ROOT::RRVecField("f", std::make_unique<RField<char>>("x")).begin()->GetFieldName()); | ||
| EXPECT_EQ("_0", ROOT::RArrayField("f", std::make_unique<RField<char>>("x"), 2).begin()->GetFieldName()); | ||
| EXPECT_EQ("_0", ROOT::ROptionalField("f", std::make_unique<RField<char>>("x")).begin()->GetFieldName()); | ||
| EXPECT_EQ("_0", ROOT::RAtomicField("f", std::make_unique<RField<char>>("x")).begin()->GetFieldName()); | ||
| EXPECT_EQ("_0", ROOT::RSetField("f", ROOT::RSetField::ESetType::kSet, std::make_unique<RField<char>>("x")) | ||
| .begin() | ||
| ->GetFieldName()); | ||
|
|
||
| { | ||
| std::unique_ptr<ROOT::RPairField> p{ | ||
| new ROOT::RPairField("x", {std::make_unique<RField<char>>("x"), std::make_unique<RField<char>>("x")})}; | ||
| EXPECT_EQ("_0", ROOT::RMapField("f", ROOT::RMapField::EMapType::kMap, std::move(p)).begin()->GetFieldName()); | ||
| } | ||
|
|
||
| { | ||
| std::vector<std::unique_ptr<ROOT::RFieldBase>> items; | ||
| items.emplace_back(std::make_unique<RField<char>>("x")); | ||
| items.emplace_back(std::make_unique<RField<int>>("x")); | ||
| ROOT::RVariantField f("f", std::move(items)); | ||
| auto itr = f.begin(); | ||
| EXPECT_EQ("_0", itr->GetFieldName()); | ||
| itr++; | ||
| EXPECT_EQ("_1", itr->GetFieldName()); | ||
| } | ||
|
|
||
| { | ||
| std::array<std::unique_ptr<ROOT::RFieldBase>, 2> items; | ||
| items[0] = std::make_unique<RField<char>>("x"); | ||
| items[1] = std::make_unique<RField<char>>("x"); | ||
| ROOT::RPairField f("f", std::move(items)); | ||
| auto itr = f.begin(); | ||
| EXPECT_EQ("_0", itr->GetFieldName()); | ||
| itr++; | ||
| EXPECT_EQ("_1", itr->GetFieldName()); | ||
| } | ||
|
|
||
| { | ||
| std::vector<std::unique_ptr<ROOT::RFieldBase>> items; | ||
| items.emplace_back(std::make_unique<RField<char>>("x")); | ||
| items.emplace_back(std::make_unique<RField<char>>("x")); | ||
| ROOT::RTupleField f("f", std::move(items)); | ||
| auto itr = f.begin(); | ||
| EXPECT_EQ("_0", itr->GetFieldName()); | ||
| itr++; | ||
| EXPECT_EQ("_1", itr->GetFieldName()); | ||
| } | ||
| } | ||
|
|
||
| TEST(RNTuple, FieldNameCollision) | ||
| { | ||
| ROOT::RFieldZero zero; | ||
| zero.Attach(std::make_unique<RField<char>>("x")); | ||
| EXPECT_THROW(zero.Attach(std::make_unique<RField<char>>("x")), ROOT::RException); | ||
|
|
||
| std::vector<std::unique_ptr<ROOT::RFieldBase>> items; | ||
| items.emplace_back(std::make_unique<RField<char>>("x")); | ||
| items.emplace_back(std::make_unique<RField<char>>("x")); | ||
| try { | ||
| ROOT::RRecordField("f", std::move(items)); | ||
| FAIL() << "duplicate field names should throw"; | ||
| } catch (const ROOT::RException &err) { | ||
| EXPECT_THAT(err.what(), testing::HasSubstr("duplicate field name")); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,7 +126,7 @@ TEST(RNtuplePrint, ArrayAsRVec) | |
| testField.AcceptVisitor(visitor); | ||
| std::string expected{std::string("") + | ||
| "$ Field 1 : arrayasrvecfield (ROOT::VecOps::RVec<float>) $\n" + | ||
| "$ Field 1.1 : myfloat (float) $\n"}; | ||
| "$ Field 1.1 : _0 (float) $\n"}; | ||
|
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. This seems to be a degradation. The user used on line 121 the name
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. Yes, because the binary format specification says that the item field has name
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. Humm .... when reading back how will the user properly retrieve |
||
| EXPECT_EQ(expected, os.str()); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm,
RFieldBase::Attachis non-virtualso "overriding" it in the base class only works if we statically know the type of theRFieldZero- is that the case?