-
Notifications
You must be signed in to change notification settings - Fork 99
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
2113 using compact incomplete on a library with dynamic schema with a named index can result in an unreadable index #2116
Open
G-D-Petrov
wants to merge
7
commits into
master
Choose a base branch
from
2113-using-compact_incomplete-on-a-library-with-dynamic-schema-with-a-named-index-can-result-in-an-unreadable-index
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,090
−297
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e83479d
Add named index tests
G-D-Petrov 53c75a1
Add index name matching checks to schema validation
G-D-Petrov aec5fdb
Update index name matching logic and adjust StreamDescriptorMismatch …
G-D-Petrov 654426e
Refactor index name matching logic to improve readability and maintai…
G-D-Petrov 1c20e99
Check the index names in finalize staged data on demand
G-D-Petrov 5319604
Move schema checks functions to a cpp file
G-D-Petrov 43d141c
Add sort_and_merge_tests, update exception to be more consistent, upd…
G-D-Petrov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
#include <arcticdb/version/schema_checks.hpp> | ||
#include <arcticdb/pipeline/index_segment_reader.hpp> | ||
|
||
namespace arcticdb { | ||
|
||
std::string_view normalization_operation_str(NormalizationOperation operation) { | ||
switch (operation) { | ||
case APPEND: | ||
return "APPEND"; | ||
case UPDATE: | ||
return "UPDATE"; | ||
default: | ||
util::raise_rte("Unknown operation type {}", static_cast<uint8_t>(operation)); | ||
} | ||
} | ||
|
||
IndexDescriptor::Type get_common_index_type(const IndexDescriptor::Type& left, const IndexDescriptor::Type& right) { | ||
if (left == right) { | ||
return left; | ||
} | ||
if (left == IndexDescriptor::Type::EMPTY) { | ||
return right; | ||
} | ||
if (right == IndexDescriptor::Type::EMPTY) { | ||
return left; | ||
} | ||
return IndexDescriptor::Type::UNKNOWN; | ||
} | ||
|
||
void check_normalization_index_match( | ||
NormalizationOperation operation, | ||
const StreamDescriptor& old_descriptor, | ||
const pipelines::InputTensorFrame& frame, | ||
bool empty_types | ||
) { | ||
const IndexDescriptor::Type old_idx_kind = old_descriptor.index().type(); | ||
const IndexDescriptor::Type new_idx_kind = frame.desc.index().type(); | ||
if (operation == UPDATE) { | ||
const bool new_is_timeseries = std::holds_alternative<arcticdb::stream::TimeseriesIndex>(frame.index); | ||
util::check_rte( | ||
(old_idx_kind == IndexDescriptor::Type::TIMESTAMP || old_idx_kind == IndexDescriptor::Type::EMPTY) && new_is_timeseries, | ||
"Update will not work as expected with a non-timeseries index" | ||
); | ||
} else { | ||
const IndexDescriptor::Type common_index_type = get_common_index_type(old_idx_kind, new_idx_kind); | ||
if (empty_types) { | ||
normalization::check<ErrorCode::E_INCOMPATIBLE_INDEX>( | ||
common_index_type != IndexDescriptor::Type::UNKNOWN, | ||
"Cannot append {} index to {} index", | ||
index_type_to_str(new_idx_kind), | ||
index_type_to_str(old_idx_kind) | ||
); | ||
} else { | ||
// (old_idx_kind == IndexDescriptor::Type::TIMESTAMP && new_idx_kind == IndexDescriptor::Type::ROWCOUNT) is left to preserve | ||
// pre-empty index behavior with pandas 2, see test_empty_writes.py::test_append_empty_series. Empty pd.Series | ||
// have Rowrange index, but due to: https://github.com/man-group/ArcticDB/blob/bd1776291fe402d8b18af9fea865324ebd7705f1/python/arcticdb/version_store/_normalization.py#L545 | ||
// it gets converted to DatetimeIndex (all empty indexes except categorical and multiindex are converted to datetime index | ||
// in pandas 2 if empty index type is disabled), however we still want to be able to append pd.Series to empty pd.Series. | ||
// Having this will not allow appending RowCont indexed pd.DataFrames to DateTime indexed pd.DataFrames because they would | ||
// have different field size (the rowcount index is not stored as a field). This logic is bug prone and will become better | ||
// after we enable the empty index. | ||
const bool input_frame_is_series = frame.norm_meta.has_series(); | ||
normalization::check<ErrorCode::E_INCOMPATIBLE_INDEX>( | ||
common_index_type != IndexDescriptor::Type::UNKNOWN || | ||
(input_frame_is_series && old_idx_kind == IndexDescriptor::Type::TIMESTAMP && new_idx_kind == IndexDescriptor::Type::ROWCOUNT), | ||
"Cannot append {} index to {} index", | ||
index_type_to_str(new_idx_kind), | ||
index_type_to_str(old_idx_kind) | ||
); | ||
} | ||
} | ||
} | ||
|
||
bool index_names_match( | ||
const StreamDescriptor& df_in_store_descriptor, | ||
const StreamDescriptor& new_df_descriptor | ||
) { | ||
auto df_in_store_index_field_count = df_in_store_descriptor.index().type() == IndexDescriptor::Type::EMPTY ? 0 : df_in_store_descriptor.index().field_count(); | ||
auto new_df_field_index_count = new_df_descriptor.index().type() == IndexDescriptor::Type::EMPTY ? 0 : new_df_descriptor.index().field_count(); | ||
|
||
// If either index is empty, we consider them to match | ||
if (df_in_store_index_field_count == 0 || new_df_field_index_count == 0) { | ||
return true; | ||
} | ||
|
||
if (df_in_store_index_field_count != new_df_field_index_count) { | ||
return false; | ||
} | ||
|
||
for (auto i = 0; i < int(df_in_store_index_field_count); ++i) { | ||
if (df_in_store_descriptor.fields(i).name() != new_df_descriptor.fields(i).name()) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool columns_match( | ||
const StreamDescriptor& df_in_store_descriptor, | ||
const StreamDescriptor& new_df_descriptor | ||
) { | ||
const int index_field_size = | ||
df_in_store_descriptor.index().type() == IndexDescriptor::Type::EMPTY ? new_df_descriptor.index().field_count() : 0; | ||
// The empty index is compatible with all other index types. Differences in the index fields in this case is | ||
// allowed. The index fields are always the first in the list. | ||
if (df_in_store_descriptor.fields().size() + index_field_size != new_df_descriptor.fields().size()) { | ||
return false; | ||
} | ||
// In case the left index is empty index we want to skip name/type checking of the index fields which are always | ||
// the first fields. | ||
for (auto i = 0; i < int(df_in_store_descriptor.fields().size()); ++i) { | ||
if (df_in_store_descriptor.fields(i).name() != new_df_descriptor.fields(i + index_field_size).name()) | ||
return false; | ||
|
||
const TypeDescriptor& left_type = df_in_store_descriptor.fields(i).type(); | ||
const TypeDescriptor& right_type = new_df_descriptor.fields(i + index_field_size).type(); | ||
|
||
if (!trivially_compatible_types(left_type, right_type) && | ||
!(is_empty_type(left_type.data_type()) || is_empty_type(right_type.data_type()))) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
void fix_descriptor_mismatch_or_throw( | ||
NormalizationOperation operation, | ||
bool dynamic_schema, | ||
const pipelines::index::IndexSegmentReader &existing_isr, | ||
const pipelines::InputTensorFrame &new_frame, | ||
bool empty_types) { | ||
const auto &old_sd = existing_isr.tsd().as_stream_descriptor(); | ||
check_normalization_index_match(operation, old_sd, new_frame, empty_types); | ||
|
||
fix_normalization_or_throw(operation == APPEND, existing_isr, new_frame); | ||
|
||
// We need to check that the index names match regardless of the dynamic schema setting | ||
if(!index_names_match(old_sd, new_frame.desc)) { | ||
throw StreamDescriptorMismatch( | ||
"The index names in the argument are not identical to that of the existing version", | ||
old_sd, | ||
new_frame.desc, | ||
operation); | ||
} | ||
|
||
if (!dynamic_schema && !columns_match(old_sd, new_frame.desc)) { | ||
throw StreamDescriptorMismatch( | ||
"The columns (names and types) in the argument are not identical to that of the existing version", | ||
old_sd, | ||
new_frame.desc, | ||
operation); | ||
} | ||
} | ||
} // namespace arcticdb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I've changed this so it is more consistent with the other similar exceptions