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

Issue #9978: Approximate Quantile Overflow #9985

Merged
merged 1 commit into from Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/core_functions/aggregate/holistic/approximate_quantile.cpp
Expand Up @@ -121,7 +121,15 @@ struct ApproxQuantileScalarOperation : public ApproxQuantileOperation {
state.h->compress();
auto &bind_data = finalize_data.input.bind_data->template Cast<ApproximateQuantileBindData>();
D_ASSERT(bind_data.quantiles.size() == 1);
target = Cast::template Operation<SAVE_TYPE, TARGET_TYPE>(state.h->quantile(bind_data.quantiles[0]));
// The result is approximate, so clamp instead of overflowing.
const auto source = state.h->quantile(bind_data.quantiles[0]);
if (TryCast::Operation(source, target, false)) {
return;
} else if (source < 0) {
target = NumericLimits<TARGET_TYPE>::Minimum();
} else {
target = NumericLimits<TARGET_TYPE>::Maximum();
}
}
};

Expand Down
11 changes: 11 additions & 0 deletions test/sql/show_select/test_summarize.test
Expand Up @@ -47,3 +47,14 @@ e BLOB (empty) a\x00b\x00c 2 NULL NULL NULL NULL NULL 3 33.33

statement ok
SUMMARIZE VALUES (1.0),(6754950520);

# Various overflows
statement ok
SUMMARIZE SELECT 9223372036854775296;

statement ok
summarize select bigint from test_all_types();

statement ok
summarize select 9223372036854775295;