@@ -74,26 +74,53 @@ PyDecimal::PyDecimal(nb::handle &obj) : obj(obj) {
7474 }
7575}
7676
77- bool PyDecimal::TryGetType (LogicalType &type) {
78- int32_t width = digits.size ();
77+ struct DecimalWidthScale {
78+ int32_t width = 0 ;
79+ int64_t scale = 0 ;
80+ bool fits = false ;
81+ };
82+
83+ static DecimalWidthScale ComputeDecimalWidthScale (idx_t digit_count, PyDecimalExponentType exponent_type,
84+ int64_t exponent_value) {
85+ DecimalWidthScale result;
86+
87+ if (digit_count > Decimal::MAX_WIDTH_DECIMAL ) {
88+ return result;
89+ }
90+
91+ if (exponent_type == PyDecimalExponentType::EXPONENT_POWER ) {
92+ if (exponent_value > Decimal::MAX_WIDTH_DECIMAL - digit_count) {
93+ return result;
94+ }
95+ result.width = static_cast <int32_t >(digit_count) + static_cast <int32_t >(exponent_value);
96+ result.scale = 0 ;
97+ result.fits = true ;
98+ return result;
99+ }
100+
101+ result.scale = exponent_value;
102+ if (result.scale > Decimal::MAX_WIDTH_INT128 ) {
103+ return result;
104+ }
79105
106+ result.width = static_cast <int32_t >(digit_count);
107+ if (result.scale > result.width ) {
108+ result.width = static_cast <int32_t >(result.scale ) + 1 ; // add 1 for the non-decimal values
109+ }
110+ result.fits = result.width <= Decimal::MAX_WIDTH_INT128 ;
111+ return result;
112+ }
113+
114+ bool PyDecimal::TryGetType (LogicalType &type) {
80115 switch (exponent_type) {
81- case PyDecimalExponentType::EXPONENT_SCALE : {
116+ case PyDecimalExponentType::EXPONENT_SCALE :
82117 case PyDecimalExponentType::EXPONENT_POWER : {
83- auto scale = exponent_value;
84- if (exponent_type == PyDecimalExponentType::EXPONENT_POWER ) {
85- width += scale;
86- }
87- if (scale > width) {
88- // The value starts with 1 or more zeros, which are optimized out of the 'digits' array
89- // 0.001; width=1, exponent=-3
90- width = scale + 1 ; // DECIMAL(4,3) - add 1 for the non-decimal values
91- }
92- if (width > Decimal::MAX_WIDTH_INT128 ) {
118+ auto ws = ComputeDecimalWidthScale (digits.size (), exponent_type, exponent_value);
119+ if (!ws.fits ) {
93120 type = LogicalType::DOUBLE ;
94121 return true ;
95122 }
96- type = LogicalType::DECIMAL (width, scale);
123+ type = LogicalType::DECIMAL (ws. width , static_cast < uint8_t >(ws. scale ) );
97124 return true ;
98125 }
99126 case PyDecimalExponentType::EXPONENT_INFINITY : {
@@ -107,7 +134,6 @@ bool PyDecimal::TryGetType(LogicalType &type) {
107134 default : // LCOV_EXCL_START
108135 throw NotImplementedException (" case not implemented for type PyDecimalExponentType" );
109136 } // LCOV_EXCL_STOP
110- }
111137}
112138// LCOV_EXCL_START
113139static void ExponentNotRecognized () {
@@ -117,12 +143,12 @@ static void ExponentNotRecognized() {
117143
118144void PyDecimal::SetExponent (nb::handle &exponent) {
119145 if (nb::isinstance<nb::int_>(exponent)) {
120- this ->exponent_value = nb::cast<int32_t >(exponent);
146+ this ->exponent_value = nb::cast<int64_t >(exponent);
121147 if (this ->exponent_value >= 0 ) {
122148 exponent_type = PyDecimalExponentType::EXPONENT_POWER ;
123149 return ;
124150 }
125- exponent_value *= - 1 ;
151+ exponent_value = exponent_value <= - INT64_MAX ? INT64_MAX : -exponent_value ;
126152 exponent_type = PyDecimalExponentType::EXPONENT_SCALE ;
127153 return ;
128154 }
@@ -142,10 +168,6 @@ void PyDecimal::SetExponent(nb::handle &exponent) {
142168 // LCOV_EXCL_STOP
143169}
144170
145- static bool WidthFitsInDecimal (int32_t width) {
146- return width >= 0 && width <= Decimal::MAX_WIDTH_DECIMAL ;
147- }
148-
149171template <class OP >
150172Value PyDecimalCastSwitch (PyDecimal &decimal, uint8_t width, uint8_t scale) {
151173 if (width > DecimalWidth<int64_t >::max) {
@@ -165,43 +187,33 @@ static Value CastToDouble(nb::handle &obj) {
165187 string converted = nb::cast<std::string>(nb::str (obj));
166188 string_t decimal_string (converted);
167189 double double_val;
168- bool try_cast = TryCast::Operation<string_t , double >(decimal_string, double_val, true );
169- ( void )try_cast ;
170- D_ASSERT (try_cast);
190+ if (! TryCast::Operation<string_t , double >(decimal_string, double_val, true )) {
191+ throw InvalidInputException ( " Failed to convert Decimal value to DOUBLE: %s " , converted) ;
192+ }
171193 return Value::DOUBLE (double_val);
172194}
173195
174196Value PyDecimal::ToDuckValue () {
175- int32_t width = digits.size ();
176- if (!WidthFitsInDecimal (width)) {
177- return CastToDouble (obj);
178- }
179197 switch (exponent_type) {
180198 case PyDecimalExponentType::EXPONENT_SCALE : {
181- uint8_t scale = exponent_value;
182- D_ASSERT (WidthFitsInDecimal (width));
183- if (scale > width) {
184- // Values like '0.001'
185- width = scale + 1 ; // leave 1 room for the non-decimal value
186- }
187- if (!WidthFitsInDecimal (width)) {
199+ auto ws = ComputeDecimalWidthScale (digits.size (), exponent_type, exponent_value);
200+ if (!ws.fits ) {
188201 return CastToDouble (obj);
189202 }
190- return PyDecimalCastSwitch<PyDecimalScaleConverter>(*this , width, scale);
203+ return PyDecimalCastSwitch<PyDecimalScaleConverter>(*this , ws. width , static_cast < uint8_t >(ws. scale ) );
191204 }
192205 case PyDecimalExponentType::EXPONENT_POWER : {
193- uint8_t scale = exponent_value;
194- width += scale;
195- if (!WidthFitsInDecimal (width)) {
206+ auto ws = ComputeDecimalWidthScale (digits.size (), exponent_type, exponent_value);
207+ if (!ws.fits ) {
196208 return CastToDouble (obj);
197209 }
198- return PyDecimalCastSwitch<PyDecimalPowerConverter>(*this , width, scale );
210+ return PyDecimalCastSwitch<PyDecimalPowerConverter>(*this , ws. width , static_cast < uint8_t >(exponent_value) );
199211 }
200212 case PyDecimalExponentType::EXPONENT_NAN : {
201213 return Value::FLOAT (NAN );
202214 }
203215 case PyDecimalExponentType::EXPONENT_INFINITY : {
204- return Value::FLOAT (INFINITY );
216+ return Value::FLOAT (signed_value ? - INFINITY : INFINITY );
205217 }
206218 // LCOV_EXCL_START
207219 default : {
0 commit comments