Skip to content
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

Improve 'Duplicate map keys are not allowed' error message #2260

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions velox/functions/prestosql/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class MapFunction : public exec::VectorFunction {

static const char* kArrayLengthsMismatch =
"Key and value arrays must be the same length";
static const char* kDuplicateKey = "Duplicate map keys are not allowed";
static const char* kDuplicateKey =
"Duplicate map keys ({}) are not allowed";

MapVectorPtr mapVector;
if (decodedKeys->isIdentityMapping() &&
Expand Down Expand Up @@ -150,7 +151,9 @@ class MapFunction : public exec::VectorFunction {
for (vector_size_t i = 1; i < size; i++) {
if (mapKeys->equalValueAt(
mapKeys.get(), offset + i, offset + i - 1)) {
VELOX_USER_FAIL("{}", kDuplicateKey);
auto duplicateKey = mapKeys->wrappedVector()->toString(
mapKeys->wrappedIndex(offset + i));
VELOX_USER_FAIL(kDuplicateKey, duplicateKey);
}
}
});
Expand Down
7 changes: 5 additions & 2 deletions velox/functions/prestosql/TransformKeys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ class TransformKeysFunction : public exec::VectorFunction {
void checkDuplicateKeys(
const MapVectorPtr& mapVector,
const SelectivityVector& rows) const {
static const char* kDuplicateKey = "Duplicate map keys are not allowed";
static const char* kDuplicateKey =
"Duplicate map keys ({}) are not allowed";

MapVector::canonicalize(mapVector);

Expand All @@ -108,7 +109,9 @@ class TransformKeysFunction : public exec::VectorFunction {
auto size = sizes[row];
for (auto i = 1; i < size; i++) {
if (mapKeys->equalValueAt(mapKeys.get(), offset + i, offset + i - 1)) {
VELOX_USER_FAIL("{}", kDuplicateKey);
auto duplicateKey = mapKeys->wrappedVector()->toString(
mapKeys->wrappedIndex(offset + i));
VELOX_USER_FAIL(kDuplicateKey, duplicateKey);
}
}
});
Expand Down
35 changes: 13 additions & 22 deletions velox/functions/prestosql/tests/MapTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

#include "velox/common/base/tests/GTestUtils.h"
#include "velox/functions/prestosql/registration/RegistrationFunctions.h"
#include "velox/functions/prestosql/tests/FunctionBaseTest.h"

Expand Down Expand Up @@ -96,24 +97,18 @@ TEST_F(MapTest, duplicateKeys) {

auto sizeAt = [](vector_size_t row) { return row % 7; };
auto keys = makeArrayVector<int64_t>(
size, sizeAt, [](vector_size_t row) { return row % 3; });
size, sizeAt, [](vector_size_t row) { return 10 + row % 3; });
auto values = makeArrayVector<int32_t>(
size, sizeAt, [](vector_size_t row) { return row % 5; });

try {
evaluate<MapVector>("map(c0, c1)", makeRowVector({keys, values}));
ASSERT_TRUE(false) << "Expected an error";
} catch (const VeloxUserError& e) {
ASSERT_EQ(e.message(), "Duplicate map keys are not allowed");
}
// Trying the map version with allowing duplicates
facebook::velox::functions::prestosql::registerMapAllowingDuplicates(
std::string("map2"));
try {
evaluate<MapVector>("map2(c0, c1)", makeRowVector({keys, values}));
} catch (const VeloxUserError& e) {
ASSERT_TRUE(false) << "No error expected";
}
VELOX_ASSERT_THROW(
evaluate<MapVector>("map(c0, c1)", makeRowVector({keys, values})),
"Duplicate map keys (10) are not allowed");

// Trying the map version with allowing duplicates.
functions::prestosql::registerMapAllowingDuplicates("map2");
ASSERT_NO_THROW(
evaluate<MapVector>("map2(c0, c1)", makeRowVector({keys, values})));
}

TEST_F(MapTest, differentArraySizes) {
Expand All @@ -128,13 +123,9 @@ TEST_F(MapTest, differentArraySizes) {
[](vector_size_t row) { return row % 7; },
[](vector_size_t row) { return row % 13; });

try {
evaluate<MapVector>("map(c0, c1)", makeRowVector({keys, values}));
ASSERT_TRUE(false) << "Expected an error";
} catch (const VeloxUserError& e) {
ASSERT_EQ(
e.message(), "(0 vs. 5) Key and value arrays must be the same length");
}
VELOX_ASSERT_THROW(
evaluate<MapVector>("map(c0, c1)", makeRowVector({keys, values})),
"(0 vs. 5) Key and value arrays must be the same length");
}

TEST_F(MapTest, encodings) {
Expand Down
9 changes: 5 additions & 4 deletions velox/functions/prestosql/tests/TransformKeysTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ TEST_F(TransformKeysTest, duplicateKeys) {
nullEvery(13)),
});
registerLambda(
"mod2",
"10_plus_mod2",
rowType("x", BIGINT(), "unused", INTEGER()),
input->type(),
"x % 2");
"10 + x % 2");

VELOX_ASSERT_THROW(
evaluate<MapVector>("transform_keys(c0, function('mod2'))", input),
"Duplicate map keys are not allowed");
evaluate<MapVector>(
"transform_keys(c0, function('10_plus_mod2'))", input),
"Duplicate map keys (11) are not allowed");
}

TEST_F(TransformKeysTest, differentResultType) {
Expand Down