-
Notifications
You must be signed in to change notification settings - Fork 193
Add NaN and NULL count to MIN/MAX stats #3139
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,9 +46,15 @@ void MinMaxAggregatorData::aggregate(const ColumnWithStrings& input_column) { | |
| [[maybe_unused]] bool any_nan{false}; | ||
| arcticdb::for_each<typename type_info::TDT>(*input_column.column_, [&](auto value) { | ||
| const auto& curr = static_cast<RawType>(value); | ||
| if constexpr (is_floating_point_type(type_info::data_type) || is_time_type(type_info::data_type)) { | ||
| // Skip NaN/NaT as they don't generate a stable ordering | ||
| if constexpr (is_floating_point_type(type_info::data_type)) { | ||
| if (is_nat_or_nan(curr)) { | ||
| ++nan_count_; | ||
| any_nan = true; | ||
| return; | ||
| } | ||
| } else if constexpr (is_time_type(type_info::data_type)) { | ||
| if (is_nat_or_nan(curr)) { | ||
| ++nat_count_; | ||
| any_nan = true; | ||
| return; | ||
| } | ||
|
|
@@ -78,8 +84,8 @@ void MinMaxAggregatorData::aggregate(const ColumnWithStrings& input_column) { | |
|
|
||
| SegmentInMemory MinMaxAggregatorData::finalize(const std::vector<ColumnName>& output_column_names) const { | ||
| internal::check<ErrorCode::E_ASSERTION_FAILURE>( | ||
| output_column_names.size() == 2, | ||
| "Expected 2 output column names in MinMaxAggregatorData::finalize, but got {}", | ||
| output_column_names.size() == 4, | ||
| "Expected 4 output column names in MinMaxAggregatorData::finalize, but got {}", | ||
| output_column_names.size() | ||
| ); | ||
| SegmentInMemory seg; | ||
|
|
@@ -93,16 +99,30 @@ SegmentInMemory MinMaxAggregatorData::finalize(const std::vector<ColumnName>& ou | |
| auto max_col = std::make_shared<Column>(make_scalar_type(max_->data_type()), Sparsity::PERMITTED); | ||
| max_col->push_back<RawType>(max_->get<RawType>()); | ||
|
|
||
| auto nan_count_col = std::make_shared<Column>(make_scalar_type(DataType::UINT64), Sparsity::PERMITTED); | ||
| nan_count_col->push_back<uint64_t>(nan_count_); | ||
|
|
||
| auto nat_count_col = std::make_shared<Column>(make_scalar_type(DataType::UINT64), Sparsity::PERMITTED); | ||
| nat_count_col->push_back<uint64_t>(nat_count_); | ||
|
|
||
| auto& entry_list = (*header.mutable_stats_by_column())[data_col_offset_]; | ||
| auto* min_entry = entry_list.add_entries(); | ||
| min_entry->set_stats_seg_offset(0); | ||
| min_entry->set_type(arcticc::pb2::column_stats_pb2::MIN_V1); | ||
| auto* max_entry = entry_list.add_entries(); | ||
| max_entry->set_stats_seg_offset(1); | ||
| max_entry->set_type(arcticc::pb2::column_stats_pb2::MAX_V1); | ||
| auto* nan_entry = entry_list.add_entries(); | ||
| nan_entry->set_stats_seg_offset(2); | ||
| nan_entry->set_type(arcticc::pb2::column_stats_pb2::NAN_COUNT_V1); | ||
| auto* nat_entry = entry_list.add_entries(); | ||
| nat_entry->set_stats_seg_offset(3); | ||
| nat_entry->set_type(arcticc::pb2::column_stats_pb2::NAT_COUNT_V1); | ||
|
|
||
| seg.add_column(scalar_field(min_col->type().data_type(), output_column_names[0].value), min_col); | ||
| seg.add_column(scalar_field(max_col->type().data_type(), output_column_names[1].value), max_col); | ||
| seg.add_column(scalar_field(DataType::UINT64, output_column_names[2].value), nan_count_col); | ||
| seg.add_column(scalar_field(DataType::UINT64, output_column_names[3].value), nat_count_col); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Two concerns that should be addressed before merge:
|
||
| }); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -274,6 +274,70 @@ def test_column_stats_only_nat_values(lmdb_version_store, any_output_format): | |
| assert raw_stats["v1_MAX(col_1)"].values.view("int64")[0] == nat_sentinel | ||
|
|
||
|
|
||
| def test_column_stats_nan_and_nat_counts(lmdb_version_store, any_output_format): | ||
| lib = lmdb_version_store | ||
| lib._set_output_format_for_pipeline_tests(any_output_format) | ||
| sym = "test_column_stats_nan_and_nat_counts" | ||
|
|
||
| # Each write/append produces a separate segment, so we get one row per dataframe in the stats. | ||
| # float_col counts toward v1_NAN_COUNT, ts_col counts toward v1_NAT_COUNT. | ||
| df0 = pd.DataFrame( | ||
| {"float_col": [1.0, 2.0], "ts_col": [pd.Timestamp("2020-01-01"), pd.Timestamp("2020-06-01")]}, | ||
| index=pd.date_range("2000-01-01", periods=2), | ||
| ) | ||
| df1 = pd.DataFrame( | ||
| {"float_col": [np.nan, 5.0], "ts_col": [pd.NaT, pd.Timestamp("2021-01-01")]}, | ||
| index=pd.date_range("2000-01-03", periods=2), | ||
| ) | ||
| df2 = pd.DataFrame( | ||
| {"float_col": [np.nan, np.nan], "ts_col": [pd.NaT, pd.NaT]}, | ||
| index=pd.date_range("2000-01-05", periods=2), | ||
| ) | ||
| df3 = pd.DataFrame( | ||
| {"float_col": [1.0, np.nan, 2.0], "ts_col": [pd.Timestamp("2022-01-01"), pd.NaT, pd.Timestamp("2022-06-01")]}, | ||
| index=pd.date_range("2000-01-07", periods=3), | ||
| ) | ||
| lib.write(sym, df0) | ||
| lib.append(sym, df1) | ||
| lib.append(sym, df2) | ||
| lib.append(sym, df3) | ||
|
|
||
| column_stats_dict = {"float_col": {"MINMAX"}, "ts_col": {"MINMAX"}} | ||
| lib.create_column_stats(sym, column_stats_dict) | ||
|
|
||
| expected_column_stats = index_columns_to_pl(lib, sym).with_columns( | ||
| pl.Series("v1_MIN(float_col)", [1.0, 5.0, np.nan, 1.0]), | ||
| pl.Series("v1_MAX(float_col)", [2.0, 5.0, np.nan, 2.0]), | ||
| pl.Series("v1_NAN_COUNT(float_col)", [0, 1, 2, 1], dtype=pl.UInt64), | ||
| pl.Series("v1_NAT_COUNT(float_col)", [0, 0, 0, 0], dtype=pl.UInt64), | ||
| pl.Series( | ||
| "v1_MIN(ts_col)", | ||
| [ | ||
| pd.Timestamp("2020-01-01").value, | ||
| pd.Timestamp("2021-01-01").value, | ||
| None, | ||
| pd.Timestamp("2022-01-01").value, | ||
| ], | ||
| dtype=pl.Int64, | ||
| ).cast(pl.Datetime("ns")), | ||
| pl.Series( | ||
| "v1_MAX(ts_col)", | ||
| [ | ||
| pd.Timestamp("2020-06-01").value, | ||
| pd.Timestamp("2021-01-01").value, | ||
| None, | ||
| pd.Timestamp("2022-06-01").value, | ||
| ], | ||
| dtype=pl.Int64, | ||
| ).cast(pl.Datetime("ns")), | ||
| pl.Series("v1_NAN_COUNT(ts_col)", [0, 0, 0, 0], dtype=pl.UInt64), | ||
| pl.Series("v1_NAT_COUNT(ts_col)", [0, 1, 2, 1], dtype=pl.UInt64), | ||
| ) | ||
|
|
||
| column_stats = lib.read_column_stats(sym) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new test only covers writing-then-reading with the same (new) client. Please add (or update an existing test) so that the all-NaN / all-NaT segments verify the new |
||
| assert_stats_equal(column_stats, expected_column_stats) | ||
|
|
||
|
|
||
| def test_column_stats_as_of(version_store_factory, lib_name, encoding_version, any_output_format): | ||
| lib = version_store_factory( | ||
| column_group_size=2, | ||
|
|
||
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.
external_to_internal(MINMAX)now unconditionally returns 4 internal stat types.drop()calls this to construct the list of column names to remove (v1_MIN,v1_MAX,v1_NAN_COUNT,v1_NAT_COUNT). For column stats segments that were written by an older client (onlyv1_MINandv1_MAXcolumns exist), dropping will produce names for columns that aren't in the segment.Please verify what the downstream consumer of
dropped_namesdoes when asked to drop a non-existent column — if it raises, this is a forward-compatibility break that needs handling; if it silently ignores, please add a test that creates column stats with the old format and then drops them with the new client.