Skip to content

Commit

Permalink
Improve duplicated key error message (#9374)
Browse files Browse the repository at this point in the history
Summary:

So the user has more context.

Differential Revision: D55779686
  • Loading branch information
Daniel Munoz authored and facebook-github-bot committed Apr 5, 2024
1 parent 776ab24 commit f20defe
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
45 changes: 42 additions & 3 deletions velox/dwio/dwrf/test/ColumnWriterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1215,22 +1215,41 @@ TEST_F(ColumnWriterTest, TestMapWriterInt64Key) {
testMapWriterNumericKey<int64_t>(/* useFlatMap */ true, /* useStruct */ true);
}

TEST_F(ColumnWriterTest, TestMapWriterDuplicatedInt64Key) {
using T = int64_t;
using b = MapBuilder<T, T>;

auto pool = memory::memoryManager()->addLeafPool();
auto batch = b::create(
*pool, {typename b::row{typename b::pair{5, 3}, typename b::pair{5, 4}}});

EXPECT_THAT(
([&]() { testMapWriter<T, T>(*pool, batch, /* useFlatMap */ true); }),
Throws<
facebook::velox::dwio::common::exception::LoggedException>(Property(
&facebook::velox::dwio::common::exception::LoggedException::message,
HasSubstr("Duplicated key in map: 5"))));
}

TEST_F(ColumnWriterTest, TestMapWriterInt32Key) {
testMapWriterNumericKey<int32_t>(/* useFlatMap */ false);
testMapWriterNumericKey<int32_t>(/* useFlatMap */ true);
testMapWriterNumericKey<int32_t>(/* useFlatMap */ true, /* useStruct */ true);
testMapWriterNumericKey<int32_t>(
/* useFlatMap */ true, /* useStruct */ true);
}

TEST_F(ColumnWriterTest, TestMapWriterInt16Key) {
testMapWriterNumericKey<int16_t>(/* useFlatMap */ false);
testMapWriterNumericKey<int16_t>(/* useFlatMap */ true);
testMapWriterNumericKey<int16_t>(/* useFlatMap */ true, /* useStruct */ true);
testMapWriterNumericKey<int16_t>(
/* useFlatMap */ true, /* useStruct */ true);
}

TEST_F(ColumnWriterTest, TestMapWriterInt8Key) {
testMapWriterNumericKey<int8_t>(/* useFlatMap */ false);
testMapWriterNumericKey<int8_t>(/* useFlatMap */ true);
testMapWriterNumericKey<int8_t>(/* useFlatMap */ true, /* useStruct */ true);
testMapWriterNumericKey<int8_t>(
/* useFlatMap */ true, /* useStruct */ true);
}

TEST_F(ColumnWriterTest, TestMapWriterStringKey) {
Expand All @@ -1250,6 +1269,26 @@ TEST_F(ColumnWriterTest, TestMapWriterStringKey) {
*pool_, batch, /* useFlatMap */ true, true, /* useStruct */ true);
}

TEST_F(ColumnWriterTest, TestMapWriterDuplicatedStringKey) {
using keyType = StringView;
using valueType = StringView;
using b = MapBuilder<keyType, valueType>;

auto pool = memory::memoryManager()->addLeafPool();
auto batch = b::create(
*pool_,
{b::row{b::pair{"2", "5"}, b::pair{"2", "8"}}}); // Duplicated key: 2

EXPECT_THAT(
([&]() {
testMapWriter<keyType, valueType>(*pool_, batch, /* useFlatMap */ true);
}),
Throws<
facebook::velox::dwio::common::exception::LoggedException>(Property(
&facebook::velox::dwio::common::exception::LoggedException::message,
HasSubstr("Duplicated key in map: 2"))));
}

TEST_F(ColumnWriterTest, TestMapWriterDifferentNumericKeyValue) {
using keyType = float;
using valueType = int32_t;
Expand Down
8 changes: 7 additions & 1 deletion velox/dwio/dwrf/writer/FlatMapColumnWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@ class ValueWriter {

void addOffset(uint64_t offset, uint64_t inMapIndex) {
if (UNLIKELY(inMapBuffer_[inMapIndex])) {
DWIO_RAISE("Duplicate key in map");
std::string duplicatedKey = "<unknown>";
if (keyInfo_.has_byteskey()) {
duplicatedKey = keyInfo_.byteskey();
} else if (keyInfo_.has_intkey()) {
duplicatedKey = std::to_string(keyInfo_.intkey());
}
DWIO_RAISE("Duplicated key in map: ", duplicatedKey);
}

ranges_.add(offset, offset + 1);
Expand Down

0 comments on commit f20defe

Please sign in to comment.