Skip to content

Commit cd1bd39

Browse files
peterenescumeta-codesync[bot]
authored andcommitted
fix: Allow FlatMapVector to generate nested flat map values (facebookincubator#16390)
Summary: Pull Request resolved: facebookincubator#16390 Allow nested flat maps to copy and preserve encodings. Prior to this change, copy of `MAP<type1, MAP<type2, type3>>` would always generate an inner map of type `MAP`, which may break copying into a `FLAT_MAP`. Reviewed By: pedroerp Differential Revision: D93186699 fbshipit-source-id: 20e6080787f37d8f751ef83ac9fcc33f87faf196
1 parent 932871f commit cd1bd39

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

velox/vector/FlatMapVector.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,8 @@ void FlatMapVector::copyRanges(
546546
// Then we allocate a new key values vector and in map buffer.
547547
inMapsAt(channel, true) =
548548
AlignedBuffer::allocate<bool>(size(), pool(), false);
549-
mapValues_.back() = BaseVector::create(valueType(), size(), pool());
549+
mapValues_.back() = BaseVector::createEmptyLike(
550+
sourceFlatMap->mapValues_[i].get(), size(), pool());
550551
}
551552

552553
// Finally, copy the map values and update the in map buffers.

velox/vector/tests/FlatMapVectorTest.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,5 +863,54 @@ TEST_F(FlatMapVectorTest, copyRanges) {
863863
{BaseVector::CopyRange{0, 1, 2}, BaseVector::CopyRange{3, 3, 1}});
864864
}
865865

866+
TEST_F(FlatMapVectorTest, copyAndPreserveNestedFlatMap) {
867+
// Create a nested map type: MAP<INT, MAP<INT, INT>>.
868+
auto source = std::make_shared<FlatMapVector>(
869+
pool_.get(),
870+
MAP(INTEGER(), MAP(INTEGER(), INTEGER())),
871+
nullptr,
872+
2,
873+
makeFlatVector<int32_t>({1}),
874+
std::vector<VectorPtr>{std::make_shared<FlatMapVector>(
875+
pool_.get(),
876+
MAP(INTEGER(), INTEGER()),
877+
nullptr,
878+
2,
879+
makeFlatVector<int32_t>({10, 20}),
880+
std::vector<VectorPtr>{
881+
makeFlatVector<int32_t>({100, 0}),
882+
makeFlatVector<int32_t>({200, 0})},
883+
std::vector<BufferPtr>{
884+
AlignedBuffer::allocate<bool>(2, pool_.get(), true),
885+
AlignedBuffer::allocate<bool>(2, pool_.get(), true)})},
886+
std::vector<BufferPtr>{
887+
AlignedBuffer::allocate<bool>(2, pool_.get(), true)});
888+
889+
auto target = std::make_shared<FlatMapVector>(
890+
pool_.get(),
891+
MAP(INTEGER(), MAP(INTEGER(), INTEGER())),
892+
nullptr,
893+
2,
894+
makeFlatVector<int32_t>({2}),
895+
std::vector<VectorPtr>{std::make_shared<FlatMapVector>(
896+
pool_.get(),
897+
MAP(INTEGER(), INTEGER()),
898+
nullptr,
899+
2,
900+
makeFlatVector<int32_t>({30}),
901+
std::vector<VectorPtr>{makeFlatVector<int32_t>({300, 0})},
902+
std::vector<BufferPtr>{
903+
AlignedBuffer::allocate<bool>(2, pool_.get(), true)})},
904+
std::vector<BufferPtr>{
905+
AlignedBuffer::allocate<bool>(2, pool_.get(), true)});
906+
907+
std::vector<BaseVector::CopyRange> ranges = {BaseVector::CopyRange{0, 0, 2}};
908+
target->copyRanges(
909+
source.get(), folly::Range<const BaseVector::CopyRange*>{ranges});
910+
911+
ASSERT_EQ(target->encoding(), VectorEncoding::Simple::FLAT_MAP);
912+
assertEqualVectors(source, target);
913+
}
914+
866915
} // namespace
867916
} // namespace facebook::velox::test

0 commit comments

Comments
 (0)