122981 {
122982 const auto new_hll_count = new_hll.Count();
122983 if (new_hll_count == 0) {
122984 return;
122985 }
122986
122988
122989
122990
122991
122992
122993
122994 double avg = 0;
122995 for (
idx_t i = 0; i < HyperLogLog::M; i++) {
122996 const auto max_new = MinValue(new_hll.GetRegister(i), duckdb_hll::maximum_zeros());
122998 avg += static_cast<double>(max_new);
122999 }
123000 avg /= static_cast<double>(HyperLogLog::M);
123001
123002
123003 if (avg > 10) {
123004 avg *= 0.75;
123005 } else if (avg > 2) {
123006 avg -= 2;
123007 }
123008
123009
123010
123011 double default_val = 0;
123012 for (
idx_t opt_idx = 0; opt_idx < 5; opt_idx++) {
123013 if (IsWithinAcceptableRange(new_hll_count, Count())) {
123014 break;
123015 }
123016
123017
123018 const double delta = avg / static_cast<double>(1 << (opt_idx + 1));
123019 if (Count() > new_hll_count) {
123020 default_val = delta > default_val ? 0 : default_val - delta;
123021 } else {
123022 default_val += delta;
123023 }
123024
123025
123026 const double floor_fraction = 1 - (default_val - floor(default_val));
123027 for (
idx_t i = 0; i < HyperLogLog::M; i++) {
123028 const auto max_new = MinValue(new_hll.GetRegister(i), duckdb_hll::maximum_zeros());
123030 if (static_cast<double>(i) / static_cast<double>(HyperLogLog::M) < floor_fraction) {
123031 register_value = ExactNumericCast<uint8_t>(floor(default_val));
123032 } else {
123033 register_value = ExactNumericCast<uint8_t>(ceil(default_val));
123034 }
123035 register_value = MinValue(register_value, max_new);
123036 for (
idx_t j = 1; j < mult; j++) {
123037 D_ASSERT(i * mult + j < duckdb_hll::num_registers());
123039 }
123040 }
123041 }
123042 }
void set_register(robj *o, size_t index, uint8_t count)
Set the count of the register.
Definition duckdb.cpp:484451