Skip to content

Commit

Permalink
Fuzzer Fix: Fix Avg for NULL cast to TIMESTAMP (#16394)
Browse files Browse the repository at this point in the history
Fixes duckdb/duckdb-fuzzer#3990

Fix (I think) Is to not use the DivModPositive Function as it assumes
the LHS is positive. However, it seems like when NULL is cast to
`Timestamp` a negative timestamp/hugeint_t value is used.

Another fix may be a different timestamp value when casting NULL.

Commit causing the error was
[1e40481](1e40481).

CC @hawkfish
  • Loading branch information
Mytherin authored Feb 26, 2025
2 parents d608a31 + 010a149 commit 5cdaf56
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions extension/core_functions/aggregate/algebraic/avg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ struct DiscreteAverageOperation : public BaseSumOperation<AverageSetOperation, A
if (state.count == 0) {
finalize_data.ReturnNull();
} else {
uint64_t remainder;
target = Hugeint::Cast<T>(Hugeint::DivModPositive(state.value, state.count, remainder));
hugeint_t remainder;
target = Hugeint::Cast<T>(Hugeint::DivMod(state.value, state.count, remainder));
// Round the result
target += (remainder > (state.count / 2));
}
Expand Down
9 changes: 9 additions & 0 deletions test/fuzzer/duckfuzz/temporal_avg.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# name: test/fuzzer/duckfuzz/temporal_avg.test
# description: Fuzzyduck issue #1128
# group: [duckfuzz]

statement ok
create table all_types as select * exclude(small_enum, medium_enum, large_enum) from test_all_types();

statement ok
SELECT avg(c1) FROM test_vector_types(CAST(NULL AS TIMESTAMP)) AS test_vector_types(c1);

0 comments on commit 5cdaf56

Please sign in to comment.