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

Prevent constructing or setting attribute with invalid cell_val_num. #4952

Merged
merged 7 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 8 additions & 6 deletions tiledb/sm/array_schema/attribute.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class AttributeStatusException : public StatusException {
}
};

using AttributeException = AttributeStatusException;
rroelke marked this conversation as resolved.
Show resolved Hide resolved

/* ********************************* */
/* CONSTRUCTORS & DESTRUCTORS */
/* ********************************* */
Expand Down Expand Up @@ -84,7 +86,7 @@ Attribute::Attribute(
if (order_ != DataOrder::UNORDERED_DATA) {
ensure_ordered_attribute_datatype_is_valid(type_);
}
check_cell_val_num(cell_val_num_);
validate_cell_val_num(cell_val_num_);
set_default_fill_value();
}

Expand All @@ -107,7 +109,7 @@ Attribute::Attribute(
, fill_value_validity_(fill_value_validity)
, order_(order)
, enumeration_name_(enumeration_name) {
check_cell_val_num(cell_val_num_);
validate_cell_val_num(cell_val_num_);
}

/* ********************************* */
Expand Down Expand Up @@ -290,12 +292,12 @@ void Attribute::serialize(
}

void Attribute::set_cell_val_num(unsigned int cell_val_num) {
check_cell_val_num(cell_val_num);
validate_cell_val_num(cell_val_num);
cell_val_num_ = cell_val_num;
set_default_fill_value();
}

void Attribute::check_cell_val_num(unsigned int cell_val_num) const {
void Attribute::validate_cell_val_num(unsigned int cell_val_num) const {
if (type_ == Datatype::ANY && cell_val_num != constants::var_num) {
throw AttributeStatusException(
rroelke marked this conversation as resolved.
Show resolved Hide resolved
"Cannot set number of values per cell; Attribute datatype `ANY` is "
Expand All @@ -306,15 +308,15 @@ void Attribute::check_cell_val_num(unsigned int cell_val_num) const {
if (order_ != DataOrder::UNORDERED_DATA) {
if (type_ == Datatype::STRING_ASCII) {
if (cell_val_num != constants::var_num) {
throw std::invalid_argument(
throw AttributeException(
"Cannot set number of values per cell; Ordered attributes with "
"datatype '" +
datatype_str(type_) +
"' must have `cell_val_num=constants::var_num`.");
}
} else {
if (cell_val_num != 1) {
throw std::invalid_argument(
throw AttributeException(
"Ordered attributes with datatype '" + datatype_str(type_) +
"' must have `cell_val_num=1`.");
}
Expand Down
20 changes: 15 additions & 5 deletions tiledb/sm/array_schema/attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class Attribute {
* @param type The type of the attribute.
* @param cell_val_num The cell number of the attribute.
* @param order The ordering of the attribute.
*
* @throws if `cell_val_num` is invalid. See `set_cell_val_num`.
*/
Attribute(
const std::string& name,
Expand All @@ -102,6 +104,8 @@ class Attribute {
* @param fill_value The fill value of the attribute.
* @param fill_value_validity The validity of fill_value.
* @param order The order of the data stored in the attribute.
*
* @throws if `cell_val_num` is invalid. See `set_cell_val_num`.
*/
Attribute(
const std::string& name,
Expand Down Expand Up @@ -237,9 +241,15 @@ class Attribute {
void serialize(Serializer& serializer, uint32_t version) const;

/**
* Sets the attribute number of values per cell. Note that if the attribute
* datatype is `ANY` this function returns an error, since `ANY` datatype
* must always be variable-sized.
* Sets the attribute number of values per cell.
*
* @throws AttributeException if `cell_val_num` is invalid
*
* A valid `cell_val_num` depends on the Attribute datatype and ordering.
* For `Datatype::ANY`, the only valid value is `constants::var_num`.
* If the attribute is unordered, then all other datatypes support any value.
* If the attribute is ordered, then an Attribute of `Datatype::STRING_ASCII`
* must have `constants::var_num`, and all other datatypes must have 1.
rroelke marked this conversation as resolved.
Show resolved Hide resolved
*/
void set_cell_val_num(unsigned int cell_val_num);

Expand Down Expand Up @@ -306,8 +316,8 @@ class Attribute {
/* ********************************* */
/* PRIVATE METHODS */
/* ********************************* */
/** Called to check if a cell val num is valid */
void check_cell_val_num(unsigned cell_val_num) const;
/** Called to validate a cell val num for this attribute */
void validate_cell_val_num(unsigned cell_val_num) const;

/** Sets the default fill value. */
void set_default_fill_value();
Expand Down
Loading
Loading