-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[ntuple] add support for automatic schema evolution of SoA fields #23064
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-soa-evolution
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.
+278
−3
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e22d9ae
[ntuple] read SoA field from different on-disk version
jblomer d6bd75d
[ntuple] test SoA member removal schema evolution
jblomer 11c51cf
[ntuple] test SoA type change schema evolution
jblomer c3e395e
[ntuple] explicitly zero-initialize added SoA members
jblomer 6e665d8
[ntuple] test SoA member addition schema evolution
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
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
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 | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,253 @@ | ||||||
| #include <ROOT/RField.hxx> | ||||||
| #include <ROOT/RNTupleModel.hxx> | ||||||
| #include <ROOT/RNTupleReader.hxx> | ||||||
| #include <ROOT/RNTupleWriter.hxx> | ||||||
| #include <ROOT/TestSupport.hxx> | ||||||
|
|
||||||
| #include <TDictAttributeMap.h> | ||||||
| #include <TInterpreter.h> | ||||||
|
|
||||||
| #include <string> | ||||||
| #include <string_view> | ||||||
|
|
||||||
| #include "gtest/gtest.h" | ||||||
| #include "ntuple_fork.hxx" | ||||||
|
|
||||||
| namespace { | ||||||
|
|
||||||
| void EvaluateIntImpl(const char *expression, int *value) | ||||||
| { | ||||||
| auto interpreterValue = gInterpreter->MakeInterpreterValue(); | ||||||
| ASSERT_TRUE(gInterpreter->Evaluate(expression, *interpreterValue)); | ||||||
| *value = interpreterValue->GetAsLong(); | ||||||
| } | ||||||
|
|
||||||
| #define EXPECT_EVALUATE_EQ(expression, expected) \ | ||||||
| do { \ | ||||||
| int _value; \ | ||||||
| EvaluateIntImpl(expression, &_value); \ | ||||||
| if (::testing::Test::HasFatalFailure()) \ | ||||||
| return; \ | ||||||
| EXPECT_EQ(expected, _value); \ | ||||||
| } while (0) | ||||||
|
|
||||||
| void MakeSoALink(const std::string &recordName, const std::string &soaName) | ||||||
| { | ||||||
| auto cl = TClass::GetClass(soaName.c_str()); | ||||||
| cl->CreateAttributeMap(); | ||||||
| cl->GetAttributeMap()->AddProperty("rntuple.SoARecord", recordName.c_str()); | ||||||
| } | ||||||
|
|
||||||
| } // namespace | ||||||
|
|
||||||
| TEST(RNTupleEvolutionSoA, RemovedMember) | ||||||
| { | ||||||
| ROOT::TestSupport::FileRaii fileGuard("test_ntuple_evolution_soa_removed_member.root"); | ||||||
|
|
||||||
| ExecInFork([&] { | ||||||
| // The child process writes the file and exits, but the file must be preserved to be read by the parent. | ||||||
| fileGuard.PreserveFile(); | ||||||
|
|
||||||
| ROOT::TestSupport::CheckDiagsRAII diagRAII; | ||||||
| diagRAII.requiredDiag(kWarning, "[ROOT.NTuple]", "The SoA field is experimental and still under development.", | ||||||
| true /* matchFullMessage */); | ||||||
|
|
||||||
| ASSERT_TRUE(gInterpreter->Declare(R"( | ||||||
| struct RemovedMemberRecord { | ||||||
| int fInt1; | ||||||
| int fInt2; | ||||||
| int fInt3; | ||||||
| ClassDefNV(RemovedMemberRecord, 2) | ||||||
|
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. Maybe
Suggested change
|
||||||
| }; | ||||||
| struct RemovedMemberSoA { | ||||||
| ROOT::RVec<int> fInt1; | ||||||
| ROOT::RVec<int> fInt2; | ||||||
| ROOT::RVec<int> fInt3; | ||||||
| ClassDefNV(RemovedMemberSoA, 2) | ||||||
| }; | ||||||
| )")); | ||||||
| MakeSoALink("RemovedMemberRecord", "RemovedMemberSoA"); | ||||||
|
|
||||||
| auto model = ROOT::RNTupleModel::Create(); | ||||||
| model->AddField(ROOT::RFieldBase::Create("f", "RemovedMemberSoA").Unwrap()); | ||||||
|
|
||||||
| auto writer = ROOT::RNTupleWriter::Recreate(std::move(model), "ntpl", fileGuard.GetPath()); | ||||||
| writer->Fill(); | ||||||
|
|
||||||
| void *ptr = writer->GetModel().GetDefaultEntry().GetPtr<void>("f").get(); | ||||||
| DeclarePointer("RemovedMemberSoA", "ptrRemovedMember", ptr); | ||||||
| ProcessLine("ptrRemovedMember->fInt1 = {11, 12};"); | ||||||
| ProcessLine("ptrRemovedMember->fInt2 = {13, 14};"); | ||||||
| ProcessLine("ptrRemovedMember->fInt3 = {15, 16};"); | ||||||
| writer->Fill(); | ||||||
|
|
||||||
| // Reset / close the writer and flush the file. | ||||||
| writer.reset(); | ||||||
| }); | ||||||
|
|
||||||
| ASSERT_TRUE(gInterpreter->Declare(R"( | ||||||
| struct RemovedMemberRecord { | ||||||
| int fInt1; | ||||||
| int fInt3; | ||||||
| ClassDefNV(RemovedMemberRecord, 3) | ||||||
| }; | ||||||
| struct RemovedMemberSoA { | ||||||
| ROOT::RVec<int> fInt1; | ||||||
| ROOT::RVec<int> fInt3; | ||||||
| ClassDefNV(RemovedMemberSoA, 3) | ||||||
| }; | ||||||
| )")); | ||||||
| MakeSoALink("RemovedMemberRecord", "RemovedMemberSoA"); | ||||||
|
|
||||||
| ROOT::TestSupport::CheckDiagsRAII diagRAII; | ||||||
| diagRAII.requiredDiag(kWarning, "[ROOT.NTuple]", "The SoA field is experimental and still under development.", | ||||||
| true /* matchFullMessage */); | ||||||
|
|
||||||
| auto reader = ROOT::RNTupleReader::Open("ntpl", fileGuard.GetPath()); | ||||||
| ASSERT_EQ(2, reader->GetNEntries()); | ||||||
|
|
||||||
| void *ptr = reader->GetModel().GetDefaultEntry().GetPtr<void>("f").get(); | ||||||
| DeclarePointer("RemovedMemberSoA", "ptrRemovedMember", ptr); | ||||||
|
|
||||||
| reader->LoadEntry(0); | ||||||
| EXPECT_EVALUATE_EQ("ptrRemovedMember->fInt1.size()", 0); | ||||||
| EXPECT_EVALUATE_EQ("ptrRemovedMember->fInt3.size()", 0); | ||||||
|
|
||||||
| reader->LoadEntry(1); | ||||||
| EXPECT_EVALUATE_EQ("ptrRemovedMember->fInt1[0]", 11); | ||||||
| EXPECT_EVALUATE_EQ("ptrRemovedMember->fInt1[1]", 12); | ||||||
| EXPECT_EVALUATE_EQ("ptrRemovedMember->fInt3[0]", 15); | ||||||
| EXPECT_EVALUATE_EQ("ptrRemovedMember->fInt3[1]", 16); | ||||||
| } | ||||||
|
|
||||||
| TEST(RNTupleEvolutionSoA, TypeChange) | ||||||
| { | ||||||
| ROOT::TestSupport::FileRaii fileGuard("test_ntuple_evolution_soa_type_change.root"); | ||||||
|
|
||||||
| ExecInFork([&] { | ||||||
| // The child process writes the file and exits, but the file must be preserved to be read by the parent. | ||||||
| fileGuard.PreserveFile(); | ||||||
|
|
||||||
| ASSERT_TRUE(gInterpreter->Declare(R"( | ||||||
| struct TypeChangeRecord { | ||||||
| bool fInt1; | ||||||
| long long int fInt2; | ||||||
| ClassDefNV(TypeChangeRecord, 2) | ||||||
| }; | ||||||
| struct TypeChangeSoA { | ||||||
| ROOT::RVec<bool> fInt1; | ||||||
| ROOT::RVec<long long int> fInt2; | ||||||
| ClassDefNV(TypeChangeSoA, 2) | ||||||
| }; | ||||||
| )")); | ||||||
| MakeSoALink("TypeChangeRecord", "TypeChangeSoA"); | ||||||
|
|
||||||
| auto model = ROOT::RNTupleModel::Create(); | ||||||
| model->AddField(ROOT::RFieldBase::Create("f", "TypeChangeSoA").Unwrap()); | ||||||
|
|
||||||
| auto writer = ROOT::RNTupleWriter::Recreate(std::move(model), "ntpl", fileGuard.GetPath()); | ||||||
|
|
||||||
| void *ptr = writer->GetModel().GetDefaultEntry().GetPtr<void>("f").get(); | ||||||
| DeclarePointer("TypeChangeSoA", "ptrTypeChange", ptr); | ||||||
| ProcessLine("ptrTypeChange->fInt1 = {true, false};"); | ||||||
| ProcessLine("ptrTypeChange->fInt2 = {137, 138};"); | ||||||
| writer->Fill(); | ||||||
|
|
||||||
| // Reset / close the writer and flush the file. | ||||||
| writer.reset(); | ||||||
| }); | ||||||
|
|
||||||
| ASSERT_TRUE(gInterpreter->Declare(R"( | ||||||
| struct TypeChangeRecord { | ||||||
| int fInt1; | ||||||
| int fInt2; | ||||||
| ClassDefNV(TypeChangeRecord, 3) | ||||||
| }; | ||||||
| struct TypeChangeSoA { | ||||||
| ROOT::RVec<int> fInt1; | ||||||
| ROOT::RVec<int> fInt2; | ||||||
| ClassDefNV(TypeChangeSoA, 3) | ||||||
| }; | ||||||
| )")); | ||||||
| MakeSoALink("TypeChangeRecord", "TypeChangeSoA"); | ||||||
|
|
||||||
| auto reader = ROOT::RNTupleReader::Open("ntpl", fileGuard.GetPath()); | ||||||
| ASSERT_EQ(1, reader->GetNEntries()); | ||||||
|
|
||||||
| void *ptr = reader->GetModel().GetDefaultEntry().GetPtr<void>("f").get(); | ||||||
| DeclarePointer("TypeChangeSoA", "ptrTypeChange", ptr); | ||||||
|
|
||||||
| reader->LoadEntry(0); | ||||||
| EXPECT_EVALUATE_EQ("ptrTypeChange->fInt1[0]", 1); | ||||||
| EXPECT_EVALUATE_EQ("ptrTypeChange->fInt1[1]", 0); | ||||||
| EXPECT_EVALUATE_EQ("ptrTypeChange->fInt2[0]", 137); | ||||||
| EXPECT_EVALUATE_EQ("ptrTypeChange->fInt2[1]", 138); | ||||||
| } | ||||||
|
|
||||||
| TEST(RNTupleEvolutionSoA, AddedMember) | ||||||
| { | ||||||
| ROOT::TestSupport::FileRaii fileGuard("test_ntuple_evolution_soa_added_member.root"); | ||||||
|
|
||||||
| ExecInFork([&] { | ||||||
| // The child process writes the file and exits, but the file must be preserved to be read by the parent. | ||||||
| fileGuard.PreserveFile(); | ||||||
|
|
||||||
| ASSERT_TRUE(gInterpreter->Declare(R"( | ||||||
| struct AddedMemberRecord { | ||||||
| int fInt1; | ||||||
| ClassDefNV(AddedMemberRecord, 2) | ||||||
| }; | ||||||
| struct AddedMemberSoA { | ||||||
| ROOT::RVec<int> fInt1; | ||||||
| ClassDefNV(AddedMemberSoA, 2) | ||||||
| }; | ||||||
| )")); | ||||||
| MakeSoALink("AddedMemberRecord", "AddedMemberSoA"); | ||||||
|
|
||||||
| auto model = ROOT::RNTupleModel::Create(); | ||||||
| model->AddField(ROOT::RFieldBase::Create("f", "AddedMemberSoA").Unwrap()); | ||||||
|
|
||||||
| auto writer = ROOT::RNTupleWriter::Recreate(std::move(model), "ntpl", fileGuard.GetPath()); | ||||||
|
|
||||||
| void *ptr = writer->GetModel().GetDefaultEntry().GetPtr<void>("f").get(); | ||||||
| DeclarePointer("AddedMemberSoA", "ptrAddedMember", ptr); | ||||||
| ProcessLine("for (int i = 0; i < 1000; ++i) ptrAddedMember->fInt1.push_back(137);"); | ||||||
| writer->Fill(); | ||||||
|
|
||||||
| // Reset / close the writer and flush the file. | ||||||
| writer.reset(); | ||||||
| }); | ||||||
|
|
||||||
| ASSERT_TRUE(gInterpreter->Declare(R"( | ||||||
| struct AddedMemberRecord { | ||||||
| int fInt1; | ||||||
| int fInt2; | ||||||
| std::string fStr; | ||||||
| ClassDefNV(AddedMemberRecord, 3) | ||||||
| }; | ||||||
| struct AddedMemberSoA { | ||||||
| ROOT::RVec<int> fInt1; | ||||||
| ROOT::RVec<int> fInt2; | ||||||
| ROOT::RVec<std::string> fStr; | ||||||
| ClassDefNV(AddedMemberSoA, 3) | ||||||
| }; | ||||||
| )")); | ||||||
| MakeSoALink("AddedMemberRecord", "AddedMemberSoA"); | ||||||
|
|
||||||
| auto reader = ROOT::RNTupleReader::Open("ntpl", fileGuard.GetPath()); | ||||||
| ASSERT_EQ(1, reader->GetNEntries()); | ||||||
|
|
||||||
| void *ptr = reader->GetModel().GetDefaultEntry().GetPtr<void>("f").get(); | ||||||
| DeclarePointer("AddedMemberSoA", "ptrAddedMember", ptr); | ||||||
|
|
||||||
| reader->LoadEntry(0); | ||||||
| EXPECT_EVALUATE_EQ("ptrAddedMember->fInt1.size()", 1000); | ||||||
| EXPECT_EVALUATE_EQ("ptrAddedMember->fInt1[0]", 137); | ||||||
| EXPECT_EVALUATE_EQ("ptrAddedMember->fInt1[999]", 137); | ||||||
| EXPECT_EVALUATE_EQ("ptrAddedMember->fInt2.size()", 1000); | ||||||
| EXPECT_EVALUATE_EQ("ptrAddedMember->fInt2[0]", 0); | ||||||
| EXPECT_EVALUATE_EQ("ptrAddedMember->fInt2[999]", 0); | ||||||
| EXPECT_EVALUATE_EQ("ptrAddedMember->fStr.size()", 1000); | ||||||
| EXPECT_EVALUATE_EQ("ptrAddedMember->fStr[0].size()", 0); | ||||||
| EXPECT_EVALUATE_EQ("ptrAddedMember->fStr[999].size()", 0); | ||||||
| } | ||||||
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.
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.
Does this need to be part of some user level documentation?