Skip to content

Commit

Permalink
Improve missing enumeration value error message
Browse files Browse the repository at this point in the history
Previously, the error message was a cryptic message about a failed
integral cast which is terrible for user experience. This updates things
to make the error message specifically state that the enumeration value
wasn't found.
  • Loading branch information
davisp committed Sep 8, 2023
1 parent f124d5f commit 37d4036
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
30 changes: 30 additions & 0 deletions test/src/unit-cppapi-enumerations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,36 @@ TEST_CASE_METHOD(
}
}

TEST_CASE_METHOD(
CPPEnumerationFx,
"CPP: Enumeration Query - Invalid Enumeration Value",
"[enumeration][query][basic]") {
// Basic smoke test. Check that a simple query condition applied against
// an array returns sane results.
create_array();

// Check attr1 == "fred" when attr1 is an enumeration.
QueryCondition qc(ctx_);
qc.init("attr1", "alf", 4, TILEDB_EQ);

// Execute the query condition against the array
std::vector<int> dim(5);
std::vector<int> attr1(5);

auto array = Array(ctx_, uri_, TILEDB_READ);
Query query(ctx_, array);
query.add_range("dim", 1, 5)
.set_layout(TILEDB_ROW_MAJOR)
.set_data_buffer("dim", dim)
.set_data_buffer("attr1", attr1)
.set_condition(qc);

// Check that the error message is helpful to users.
auto matcher = Catch::Matchers::ContainsSubstring(
"Enumeration value not found for field 'attr1'");
REQUIRE_THROWS_WITH(query.submit(), matcher);
}

TEST_CASE_METHOD(
CPPEnumerationFx,
"CPP: Enumeration Query - Set Use Enumeration",
Expand Down
9 changes: 9 additions & 0 deletions tiledb/sm/query/ast/query_ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ void ASTNodeVal::rewrite_enumeration_conditions(

if (op_ != QueryConditionOp::IN && op_ != QueryConditionOp::NOT_IN) {
auto idx = enumeration->index_of(get_value_ptr(), get_value_size());
if (idx == constants::enumeration_missing_value) {
throw std::invalid_argument(
"Enumeration value not found for field '" + attr->name() + "'");
}

data_ = ByteVecValue(val_size);
utils::safe_integral_cast_to_datatype(idx, attr->type(), data_);
Expand All @@ -215,6 +219,11 @@ void ASTNodeVal::rewrite_enumeration_conditions(

for (auto& member : members_) {
auto idx = enumeration->index_of(member.data(), member.size());
if (idx == constants::enumeration_missing_value) {
throw std::invalid_argument(
"Enumeration value not found for field '" + attr->name() + "'");
}

utils::safe_integral_cast_to_datatype(idx, attr->type(), curr_data);
data_writer.write(curr_data.data(), curr_data.size());
offsets_writer.write(curr_offset);
Expand Down

0 comments on commit 37d4036

Please sign in to comment.