vortex_array/arrays/widen/encode/
integer.rs1use std::ops::RangeInclusive;
10
11use vortex_error::VortexExpect;
12use vortex_error::VortexResult;
13use vortex_error::vortex_ensure;
14
15use crate::ArrayRef;
16use crate::ExecutionCtx;
17use crate::IntoArray;
18use crate::aggregate_fn::NumericalAggregateOpts;
19use crate::aggregate_fn::fns::min_max::MinMaxResult;
20use crate::aggregate_fn::fns::min_max::min_max;
21use crate::arrays::PrimitiveArray;
22use crate::arrays::WidenArray;
23use crate::arrays::widen::compute::wrap_values;
24use crate::builtins::ArrayBuiltins;
25use crate::dtype::DType;
26use crate::dtype::PType;
27use crate::scalar::PValue;
28
29impl WidenArray {
30 pub fn encode(array: PrimitiveArray, ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef> {
36 let dtype = array.dtype().clone();
37 wrap_values(narrow_values(array, ctx)?.into_array(), dtype)
38 }
39
40 #[deprecated(
48 note = "Changes the dtype. Each caller must decide whether it needs a Widen (#10733)"
49 )]
50 pub fn encode_values(
51 array: PrimitiveArray,
52 ctx: &mut ExecutionCtx,
53 ) -> VortexResult<PrimitiveArray> {
54 if !array.ptype().is_int() {
55 return Ok(array);
56 }
57
58 narrow_values(array, ctx)
59 }
60}
61
62fn narrow_values(array: PrimitiveArray, ctx: &mut ExecutionCtx) -> VortexResult<PrimitiveArray> {
68 let ptype = array.ptype();
69 vortex_ensure!(ptype.is_int(), "Widen requires integer values, got {ptype}");
70
71 if ptype.byte_width() == 1 {
73 return Ok(array);
74 }
75
76 let bounds = match min_max(array.as_ref(), ctx, NumericalAggregateOpts::default())? {
78 Some(bounds) => integer_extrema(bounds),
79 None => 0..=0,
80 };
81 let Some(stored_ptype) = smallest_integer_ptype(bounds, ptype.is_signed_int())
82 .filter(|stored_ptype| stored_ptype.byte_width() < ptype.byte_width())
83 else {
84 return Ok(array);
85 };
86
87 let stored_dtype = DType::Primitive(stored_ptype, array.dtype().nullability());
88 array
89 .into_array()
90 .cast(stored_dtype)?
91 .execute::<PrimitiveArray>(ctx)
92}
93
94pub(in crate::arrays::widen) fn integer_extrema(bounds: MinMaxResult) -> RangeInclusive<i128> {
98 let [min, max] = [bounds.min, bounds.max].map(|bound| {
99 bound
100 .as_primitive()
101 .pvalue()
102 .and_then(PValue::as_i128)
103 .vortex_expect("min_max of an integer array returns non-null integer extrema")
104 });
105
106 min..=max
107}
108
109pub(in crate::arrays::widen) fn smallest_integer_ptype(
112 bounds: RangeInclusive<i128>,
113 signed: bool,
114) -> Option<PType> {
115 let (min, max) = bounds.into_inner();
116 if signed {
117 let min_ptype = PType::min_signed_ptype_for_value(i64::try_from(min).ok()?);
118 let max_ptype = PType::min_signed_ptype_for_value(i64::try_from(max).ok()?);
119 Some(min_ptype.max_signed_ptype(max_ptype))
120 } else {
121 let min_ptype = PType::min_unsigned_ptype_for_value(u64::try_from(min).ok()?);
122 let max_ptype = PType::min_unsigned_ptype_for_value(u64::try_from(max).ok()?);
123 Some(min_ptype.max_unsigned_ptype(max_ptype))
124 }
125}