Skip to content

Commit 853a398

Browse files
authored
fix: Fix decimal dynamic float cast (#28126)
1 parent 73848be commit 853a398

2 files changed

Lines changed: 51 additions & 44 deletions

File tree

‎crates/polars-core/src/utils/supertype.rs‎

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,49 @@ pub fn get_supertype_with_options(
442442
None => None
443443
}
444444
},
445+
#[cfg(feature = "dtype-struct")]
446+
(Struct(fields_a), Struct(fields_b)) => {
447+
super_type_structs(fields_a, fields_b)
448+
}
449+
#[cfg(feature = "dtype-struct")]
450+
(Struct(fields_a), rhs) if rhs.is_primitive_numeric() => {
451+
let mut new_fields = Vec::with_capacity(fields_a.len());
452+
for a in fields_a {
453+
let st = get_supertype(&a.dtype, rhs)?;
454+
new_fields.push(Field::new(a.name.clone(), st))
455+
}
456+
Some(Struct(new_fields))
457+
}
458+
#[cfg(feature = "dtype-decimal")]
459+
(Decimal(p1, s1), Decimal(p2, s2)) => {
460+
Some(Decimal((*p1).max(*p2), (*s1).max(*s2)))
461+
},
462+
#[cfg(all(feature = "dtype-decimal", feature = "dtype-f16"))]
463+
(Decimal(_, _), Float16) => Some(Float64),
464+
#[cfg(feature = "dtype-decimal")]
465+
(Decimal(_, _), Float32 | Float64 | Unknown(UnknownKind::Float)) => Some(Float64),
466+
#[cfg(feature = "dtype-decimal")]
467+
(Decimal(prec, scale), dt) if dt.is_signed_integer() || dt.is_unsigned_integer() => {
468+
let fits = |v| { i128_to_dec128(v, *prec, *scale).is_some() };
469+
let fits_orig_prec_scale = match dt {
470+
UInt8 => fits(u8::MAX as i128),
471+
UInt16 => fits(u16::MAX as i128),
472+
UInt32 => fits(u32::MAX as i128),
473+
UInt64 => fits(u64::MAX as i128),
474+
UInt128 => false,
475+
Int8 => fits(i8::MAX as i128),
476+
Int16 => fits(i16::MAX as i128),
477+
Int32 => fits(i32::MAX as i128),
478+
Int64 => fits(i64::MAX as i128),
479+
Int128 => false,
480+
_ => unreachable!(),
481+
};
482+
if fits_orig_prec_scale {
483+
Some(Decimal(*prec, *scale))
484+
} else {
485+
Some(Decimal(DEC128_MAX_PREC, *scale))
486+
}
487+
}
445488
(dt, Unknown(kind)) => {
446489
match kind {
447490
UnknownKind::Float | UnknownKind::Int(_) if dt.is_string() => {
@@ -453,7 +496,7 @@ pub fn get_supertype_with_options(
453496
},
454497
// Materialize float to float
455498
UnknownKind::Float | UnknownKind::Int(_) if dt.is_float() => Some(dt.clone()),
456-
UnknownKind::Float if dt.is_integer() | dt.is_decimal() => Some(Unknown(UnknownKind::Float)),
499+
UnknownKind::Float if dt.is_integer() => Some(Unknown(UnknownKind::Float)),
457500
// Materialize str
458501
UnknownKind::Str if dt.is_string() | dt.is_enum() => Some(dt.clone()),
459502
// Materialize str
@@ -498,49 +541,6 @@ pub fn get_supertype_with_options(
498541
_ => Some(Unknown(UnknownKind::Any))
499542
}
500543
},
501-
#[cfg(feature = "dtype-struct")]
502-
(Struct(fields_a), Struct(fields_b)) => {
503-
super_type_structs(fields_a, fields_b)
504-
}
505-
#[cfg(feature = "dtype-struct")]
506-
(Struct(fields_a), rhs) if rhs.is_primitive_numeric() => {
507-
let mut new_fields = Vec::with_capacity(fields_a.len());
508-
for a in fields_a {
509-
let st = get_supertype(&a.dtype, rhs)?;
510-
new_fields.push(Field::new(a.name.clone(), st))
511-
}
512-
Some(Struct(new_fields))
513-
}
514-
#[cfg(feature = "dtype-decimal")]
515-
(Decimal(p1, s1), Decimal(p2, s2)) => {
516-
Some(Decimal((*p1).max(*p2), (*s1).max(*s2)))
517-
},
518-
#[cfg(all(feature = "dtype-decimal", feature = "dtype-f16"))]
519-
(Decimal(_, _), Float16) => Some(Float64),
520-
#[cfg(feature = "dtype-decimal")]
521-
(Decimal(_, _), Float32 | Float64) => Some(Float64),
522-
#[cfg(feature = "dtype-decimal")]
523-
(Decimal(prec, scale), dt) if dt.is_signed_integer() || dt.is_unsigned_integer() => {
524-
let fits = |v| { i128_to_dec128(v, *prec, *scale).is_some() };
525-
let fits_orig_prec_scale = match dt {
526-
UInt8 => fits(u8::MAX as i128),
527-
UInt16 => fits(u16::MAX as i128),
528-
UInt32 => fits(u32::MAX as i128),
529-
UInt64 => fits(u64::MAX as i128),
530-
UInt128 => false,
531-
Int8 => fits(i8::MAX as i128),
532-
Int16 => fits(i16::MAX as i128),
533-
Int32 => fits(i32::MAX as i128),
534-
Int64 => fits(i64::MAX as i128),
535-
Int128 => false,
536-
_ => unreachable!(),
537-
};
538-
if fits_orig_prec_scale {
539-
Some(Decimal(*prec, *scale))
540-
} else {
541-
Some(Decimal(DEC128_MAX_PREC, *scale))
542-
}
543-
}
544544
_ => None,
545545
}
546546
}

‎py-polars/tests/unit/datatypes/test_datatypes.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import copy
44
import pickle
55
from datetime import datetime, time, timedelta
6+
from decimal import Decimal as D
67
from typing import TYPE_CHECKING, Any, Optional, Union
78

89
import pytest
@@ -259,3 +260,9 @@ def test_max_min(
259260
df = pl.select(min=dtype.min(), max=dtype.max())
260261
assert df.to_series(0).item() == lower
261262
assert df.to_series(1).item() == upper
263+
264+
265+
def test_unknown_resolve() -> None:
266+
267+
df = pl.LazyFrame({"dec": [D("0.25")]})
268+
assert "Float64" in df.select(pl.col("dec") * (1.0 * 1)).explain()

0 commit comments

Comments
 (0)