Skip to content

Commit 1663219

Browse files
committed
vendor: Update vendored sources to duckdb/duckdb@b44b8f9
Issue duckdb/duckdb#17299: Integer Rounding (duckdb/duckdb#17328)
1 parent 5f8829f commit 1663219

File tree

6 files changed

+66
-14
lines changed

6 files changed

+66
-14
lines changed

src/duckdb/extension/core_functions/scalar/math/numeric.cpp

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "duckdb/common/operator/decimal_cast_operators.hpp"
12
#include "duckdb/common/vector_operations/vector_operations.hpp"
23
#include "duckdb/common/algorithm.hpp"
34
#include "duckdb/common/likely.hpp"
@@ -566,6 +567,34 @@ struct RoundDecimalOperator {
566567
}
567568
};
568569

570+
struct RoundIntegerOperator {
571+
template <class TA, class TB, class TR>
572+
static inline TR Operation(TA input, TB precision) {
573+
if (precision < 0) {
574+
// Do all the arithmetic at higher precision
575+
using POWERS_OF_TEN_CLASS = typename DecimalCastTraits<TA>::POWERS_OF_TEN_CLASS;
576+
if (precision <= -POWERS_OF_TEN_CLASS::CACHED_POWERS_OF_TEN) {
577+
return 0;
578+
}
579+
const auto power_of_ten = POWERS_OF_TEN_CLASS::POWERS_OF_TEN[-precision];
580+
auto addition = power_of_ten / 2;
581+
if (input < 0) {
582+
addition = -addition;
583+
}
584+
addition += input;
585+
addition /= power_of_ten;
586+
if (addition) {
587+
return UnsafeNumericCast<TR>(addition * power_of_ten);
588+
} else {
589+
return 0;
590+
}
591+
} else {
592+
// Rounding integers to higher precision is a NOP
593+
return input;
594+
}
595+
}
596+
};
597+
569598
struct RoundPrecisionFunctionData : public FunctionData {
570599
explicit RoundPrecisionFunctionData(int32_t target_scale) : target_scale(target_scale) {
571600
}
@@ -698,10 +727,6 @@ ScalarFunctionSet RoundFun::GetFunctions() {
698727
scalar_function_t round_func = nullptr;
699728
bind_scalar_function_t bind_func = nullptr;
700729
bind_scalar_function_t bind_prec_func = nullptr;
701-
if (type.IsIntegral()) {
702-
// no round for integral numbers
703-
continue;
704-
}
705730
switch (type.id()) {
706731
case LogicalTypeId::FLOAT:
707732
round_func = ScalarFunction::UnaryFunction<float, float, RoundOperator>;
@@ -715,7 +740,31 @@ ScalarFunctionSet RoundFun::GetFunctions() {
715740
bind_func = BindGenericRoundFunctionDecimal<RoundDecimalOperator>;
716741
bind_prec_func = BindDecimalRoundPrecision;
717742
break;
743+
case LogicalTypeId::TINYINT:
744+
round_func = ScalarFunction::NopFunction;
745+
round_prec_func = ScalarFunction::BinaryFunction<int8_t, int32_t, int8_t, RoundIntegerOperator>;
746+
break;
747+
case LogicalTypeId::SMALLINT:
748+
round_func = ScalarFunction::NopFunction;
749+
round_prec_func = ScalarFunction::BinaryFunction<int16_t, int32_t, int16_t, RoundIntegerOperator>;
750+
break;
751+
case LogicalTypeId::INTEGER:
752+
round_func = ScalarFunction::NopFunction;
753+
round_prec_func = ScalarFunction::BinaryFunction<int32_t, int32_t, int32_t, RoundIntegerOperator>;
754+
break;
755+
case LogicalTypeId::BIGINT:
756+
round_func = ScalarFunction::NopFunction;
757+
round_prec_func = ScalarFunction::BinaryFunction<int64_t, int32_t, int64_t, RoundIntegerOperator>;
758+
break;
759+
case LogicalTypeId::HUGEINT:
760+
round_func = ScalarFunction::NopFunction;
761+
round_prec_func = ScalarFunction::BinaryFunction<hugeint_t, int32_t, hugeint_t, RoundIntegerOperator>;
762+
break;
718763
default:
764+
if (type.IsIntegral()) {
765+
// no round for integral numbers
766+
continue;
767+
}
719768
throw InternalException("Unimplemented numeric type for function \"floor\"");
720769
}
721770
round.AddFunction(ScalarFunction({type}, type, round_func, bind_func));

src/duckdb/src/common/operator/cast_operators.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,11 +1800,12 @@ struct HugeIntegerCastOperation {
18001800
}
18011801

18021802
// Get the first (left-most) digit of the decimals
1803-
while (state.decimal_total_digits > 39) {
1804-
state.decimal /= T::Operation::POWERS_OF_TEN[39];
1805-
state.decimal_total_digits -= 39;
1803+
constexpr auto MAX_DIGITS = T::Operation::CACHED_POWERS_OF_TEN - 1;
1804+
while (state.decimal_total_digits > MAX_DIGITS) {
1805+
state.decimal /= T::Operation::POWERS_OF_TEN[MAX_DIGITS];
1806+
state.decimal_total_digits -= MAX_DIGITS;
18061807
}
1807-
D_ASSERT((state.decimal_total_digits - 1) >= 0 && (state.decimal_total_digits - 1) <= 39);
1808+
D_ASSERT((state.decimal_total_digits - 1) >= 0 && (state.decimal_total_digits - 1) <= MAX_DIGITS);
18081809
state.decimal /= T::Operation::POWERS_OF_TEN[state.decimal_total_digits - 1];
18091810

18101811
if (state.decimal >= 5) {

src/duckdb/src/function/table/version/pragma_version.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#ifndef DUCKDB_PATCH_VERSION
2-
#define DUCKDB_PATCH_VERSION "0-dev2967"
2+
#define DUCKDB_PATCH_VERSION "0-dev2972"
33
#endif
44
#ifndef DUCKDB_MINOR_VERSION
55
#define DUCKDB_MINOR_VERSION 3
@@ -8,10 +8,10 @@
88
#define DUCKDB_MAJOR_VERSION 1
99
#endif
1010
#ifndef DUCKDB_VERSION
11-
#define DUCKDB_VERSION "v1.3.0-dev2967"
11+
#define DUCKDB_VERSION "v1.3.0-dev2972"
1212
#endif
1313
#ifndef DUCKDB_SOURCE_ID
14-
#define DUCKDB_SOURCE_ID "1969db05c4"
14+
#define DUCKDB_SOURCE_ID "b44b8f91db"
1515
#endif
1616
#include "duckdb/function/table/system_functions.hpp"
1717
#include "duckdb/main/database.hpp"

src/duckdb/src/include/duckdb/common/types/cast_helpers.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace duckdb {
2323
//! NumericHelper is a static class that holds helper functions for integers/doubles
2424
class NumericHelper {
2525
public:
26-
static constexpr uint8_t CACHED_POWERS_OF_TEN = 20;
26+
static constexpr uint8_t CACHED_POWERS_OF_TEN = 19;
2727
static const int64_t POWERS_OF_TEN[CACHED_POWERS_OF_TEN];
2828
static const double DOUBLE_POWERS_OF_TEN[40];
2929

src/duckdb/src/include/duckdb/common/types/hugeint.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ class Hugeint {
171171
return upper_smaller | (upper_equal & lower_smaller_equals);
172172
}
173173

174-
static const hugeint_t POWERS_OF_TEN[40];
174+
static constexpr uint8_t CACHED_POWERS_OF_TEN = 39;
175+
static const hugeint_t POWERS_OF_TEN[CACHED_POWERS_OF_TEN];
175176
};
176177

177178
template <>

src/duckdb/src/include/duckdb/common/types/uhugeint.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ class Uhugeint {
160160
return upper_smaller | (upper_equal & lower_smaller_equals);
161161
}
162162

163-
static const uhugeint_t POWERS_OF_TEN[40];
163+
static constexpr uint8_t CACHED_POWERS_OF_TEN = 39;
164+
static const uhugeint_t POWERS_OF_TEN[CACHED_POWERS_OF_TEN];
164165
};
165166

166167
template <>

0 commit comments

Comments
 (0)