@@ -57,7 +57,9 @@ use super::SimpleDomain;
57
57
use super :: SimpleType ;
58
58
use super :: SimpleValueType ;
59
59
use super :: ValueType ;
60
+ use crate :: types:: CoreNumber ;
60
61
use crate :: types:: DataType ;
62
+ use crate :: types:: F64 ;
61
63
use crate :: utils:: arrow:: buffer_into_mut;
62
64
use crate :: with_decimal_mapped_type;
63
65
use crate :: with_decimal_type;
@@ -71,11 +73,15 @@ use crate::Value;
71
73
#[ derive( Debug , Clone , PartialEq , Eq ) ]
72
74
pub struct CoreDecimal < T : Decimal > ( PhantomData < T > ) ;
73
75
76
+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
77
+ pub struct CoreScalarDecimal < T : Decimal > ( PhantomData < T > ) ;
78
+
74
79
pub type Decimal64Type = DecimalType < i64 > ;
75
80
pub type Decimal128Type = DecimalType < i128 > ;
76
81
pub type Decimal256Type = DecimalType < i256 > ;
77
82
78
83
pub type DecimalType < T > = SimpleValueType < CoreDecimal < T > > ;
84
+ pub type DecimalScalarType < T > = SimpleValueType < CoreScalarDecimal < T > > ;
79
85
80
86
impl < Num : Decimal > SimpleType for CoreDecimal < Num > {
81
87
type Scalar = Num ;
@@ -149,6 +155,95 @@ impl<Num: Decimal> SimpleType for CoreDecimal<Num> {
149
155
}
150
156
}
151
157
158
+ impl < Num : Decimal > SimpleType for CoreScalarDecimal < Num > {
159
+ type Scalar = ( Num , DecimalSize ) ;
160
+ type Domain = SimpleDomain < ( Num , DecimalSize ) > ;
161
+
162
+ fn downcast_scalar ( scalar : & ScalarRef ) -> Option < Self :: Scalar > {
163
+ let scalar = scalar. as_decimal ( ) ?;
164
+ Num :: try_downcast_scalar ( scalar) . map ( |v| ( v, scalar. size ( ) ) )
165
+ }
166
+
167
+ fn downcast_column ( col : & Column ) -> Option < Buffer < Self :: Scalar > > {
168
+ Num :: try_downcast_column ( col) . map ( |( col, x) | col. into_iter ( ) . map ( |v| ( v, x) ) . collect ( ) )
169
+ }
170
+
171
+ fn downcast_domain ( domain : & Domain ) -> Option < Self :: Domain > {
172
+ let size = domain. as_decimal ( ) ?. decimal_size ( ) ;
173
+ let domain = Num :: try_downcast_domain ( domain. as_decimal ( ) ?) ?;
174
+ Some ( SimpleDomain {
175
+ min : ( domain. min , size) ,
176
+ max : ( domain. max , size) ,
177
+ } )
178
+ }
179
+
180
+ // It's not allowed to call downcast_builder temporarily
181
+ fn downcast_builder ( _builder : & mut ColumnBuilder ) -> Option < & mut Vec < Self :: Scalar > > {
182
+ None
183
+ }
184
+
185
+ fn downcast_owned_builder ( builder : ColumnBuilder ) -> Option < Vec < Self :: Scalar > > {
186
+ let size = builder. as_decimal ( ) ?. decimal_size ( ) ;
187
+ let b = Num :: try_downcast_owned_builder ( builder) ;
188
+ b. map ( |v| v. into_iter ( ) . map ( |v| ( v, size) ) . collect ( ) )
189
+ }
190
+
191
+ fn upcast_column_builder (
192
+ builder : Vec < Self :: Scalar > ,
193
+ data_type : & DataType ,
194
+ ) -> Option < ColumnBuilder > {
195
+ Some ( ColumnBuilder :: Decimal ( Num :: upcast_builder (
196
+ builder. into_iter ( ) . map ( |( v, _) | v) . collect ( ) ,
197
+ * data_type. as_decimal ( ) . unwrap ( ) ,
198
+ ) ) )
199
+ }
200
+
201
+ fn upcast_scalar ( scalar : Self :: Scalar , data_type : & DataType ) -> Scalar {
202
+ let size = * data_type. as_decimal ( ) . unwrap ( ) ;
203
+ Num :: upcast_scalar ( scalar. 0 , size)
204
+ }
205
+
206
+ fn upcast_column ( col : Buffer < Self :: Scalar > , data_type : & DataType ) -> Column {
207
+ let col = col. into_iter ( ) . map ( |( v, _) | v) . collect ( ) ;
208
+ let size = * data_type. as_decimal ( ) . unwrap ( ) ;
209
+ Num :: upcast_column ( col, size)
210
+ }
211
+
212
+ fn upcast_domain ( domain : Self :: Domain , data_type : & DataType ) -> Domain {
213
+ let domain = SimpleDomain {
214
+ min : domain. min . 0 ,
215
+ max : domain. max . 0 ,
216
+ } ;
217
+ let size = * data_type. as_decimal ( ) . unwrap ( ) ;
218
+ Num :: upcast_domain ( domain, size)
219
+ }
220
+
221
+ #[ inline( always) ]
222
+ fn compare ( lhs : & Self :: Scalar , rhs : & Self :: Scalar ) -> Ordering {
223
+ lhs. cmp ( rhs)
224
+ }
225
+
226
+ #[ inline( always) ]
227
+ fn greater_than ( left : & Self :: Scalar , right : & Self :: Scalar ) -> bool {
228
+ left > right
229
+ }
230
+
231
+ #[ inline( always) ]
232
+ fn greater_than_equal ( left : & Self :: Scalar , right : & Self :: Scalar ) -> bool {
233
+ left >= right
234
+ }
235
+
236
+ #[ inline( always) ]
237
+ fn less_than ( left : & Self :: Scalar , right : & Self :: Scalar ) -> bool {
238
+ left < right
239
+ }
240
+
241
+ #[ inline( always) ]
242
+ fn less_than_equal ( left : & Self :: Scalar , right : & Self :: Scalar ) -> bool {
243
+ left <= right
244
+ }
245
+ }
246
+
152
247
impl < Num : Decimal > DecimalType < Num > {
153
248
pub fn full_domain ( size : & DecimalSize ) -> SimpleDomain < Num > {
154
249
SimpleDomain {
@@ -194,6 +289,12 @@ impl DecimalScalar {
194
289
} )
195
290
}
196
291
292
+ pub fn scale ( & self ) -> u8 {
293
+ with_decimal_type ! ( |DECIMAL | match self {
294
+ DecimalScalar :: DECIMAL ( _, size) => size. scale,
295
+ } )
296
+ }
297
+
197
298
pub fn as_decimal < D : Decimal > ( & self ) -> D {
198
299
with_decimal_type ! ( |DECIMAL | match self {
199
300
DecimalScalar :: DECIMAL ( value, _) => value. as_decimal( ) ,
@@ -243,6 +344,8 @@ impl DecimalDomain {
243
344
Copy ,
244
345
PartialEq ,
245
346
Eq ,
347
+ PartialOrd ,
348
+ Ord ,
246
349
Hash ,
247
350
Serialize ,
248
351
Deserialize ,
@@ -254,6 +357,15 @@ pub struct DecimalSize {
254
357
scale : u8 ,
255
358
}
256
359
360
+ impl Default for DecimalSize {
361
+ fn default ( ) -> Self {
362
+ DecimalSize {
363
+ precision : 15 ,
364
+ scale : 2 ,
365
+ }
366
+ }
367
+ }
368
+
257
369
impl DecimalSize {
258
370
pub fn new_unchecked ( precision : u8 , scale : u8 ) -> DecimalSize {
259
371
DecimalSize { precision, scale }
@@ -2801,6 +2913,8 @@ impl Ord for i256 {
2801
2913
}
2802
2914
2803
2915
pub type DecimalView < F , T > = ComputeView < DecimalConvert < F , T > , CoreDecimal < F > , CoreDecimal < T > > ;
2916
+ pub type DecimalF64View < F > =
2917
+ ComputeView < DecimalConvert < F , F64 > , CoreScalarDecimal < F > , CoreNumber < F64 > > ;
2804
2918
2805
2919
#[ derive( Debug , Clone , PartialEq , Eq , Default ) ]
2806
2920
pub struct DecimalConvert < F , T > ( std:: marker:: PhantomData < ( F , T ) > ) ;
@@ -2823,10 +2937,32 @@ where
2823
2937
}
2824
2938
}
2825
2939
2940
+ impl < F > Compute < CoreScalarDecimal < F > , CoreNumber < F64 > > for DecimalConvert < F , F64 >
2941
+ where F : Decimal
2942
+ {
2943
+ #[ inline]
2944
+ fn compute ( value : & ( F , DecimalSize ) ) -> F64 {
2945
+ value. 0 . to_float64 ( value. 1 . scale ( ) ) . into ( )
2946
+ }
2947
+
2948
+ fn compute_domain ( domain : & SimpleDomain < ( F , DecimalSize ) > ) -> SimpleDomain < F64 > {
2949
+ let min = domain. min . 0 . to_float64 ( domain. min . 1 . scale ( ) ) ;
2950
+ let max = domain. max . 0 . to_float64 ( domain. max . 1 . scale ( ) ) ;
2951
+
2952
+ SimpleDomain {
2953
+ min : min. into ( ) ,
2954
+ max : max. into ( ) ,
2955
+ }
2956
+ }
2957
+ }
2958
+
2959
+ pub type Decimal64AsF64Type = DecimalF64View < i64 > ;
2960
+ pub type Decimal128AsF64Type = DecimalF64View < i128 > ;
2961
+ pub type Decimal256AsF64Type = DecimalF64View < i256 > ;
2962
+
2826
2963
macro_rules! decimal_convert_type {
2827
2964
( $from: ty, $to: ty, $alias: ident, $compat: ident) => {
2828
- pub type $alias =
2829
- ComputeView <DecimalConvert <$from, $to>, CoreDecimal <$from>, CoreDecimal <$to>>;
2965
+ pub type $alias = DecimalView <$from, $to>;
2830
2966
pub type $compat = DecimalConvert <$from, $to>;
2831
2967
} ;
2832
2968
}
0 commit comments