@@ -141,6 +141,7 @@ func addCastFuncs(fn []*castFunction) {
141141func initCastTable () {
142142 castTable = make (map [arrow.Type ]* castFunction )
143143 addCastFuncs (getBooleanCasts ())
144+ addCastFuncs (getNumericCasts ())
144145}
145146
146147func getCastFunction (to arrow.DataType ) (* castFunction , error ) {
@@ -167,6 +168,67 @@ func getBooleanCasts() []*castFunction {
167168 return []* castFunction {fn }
168169}
169170
171+ func getNumericCasts () []* castFunction {
172+ out := make ([]* castFunction , 0 )
173+
174+ getFn := func (name string , ty arrow.Type , kns []exec.ScalarKernel ) * castFunction {
175+ fn := newCastFunction (name , ty )
176+ for _ , k := range kns {
177+ if err := fn .AddTypeCast (k .Signature .InputTypes [0 ].MatchID (), k ); err != nil {
178+ panic (err )
179+ }
180+ }
181+ return fn
182+ }
183+
184+ out = append (out , getFn ("cast_int8" , arrow .INT8 , kernels.GetCastToInteger [int8 ](arrow .PrimitiveTypes .Int8 )))
185+ out = append (out , getFn ("cast_int16" , arrow .INT16 , kernels.GetCastToInteger [int8 ](arrow .PrimitiveTypes .Int16 )))
186+
187+ castInt32 := getFn ("cast_int32" , arrow .INT32 , kernels.GetCastToInteger [int32 ](arrow .PrimitiveTypes .Int32 ))
188+ castInt32 .AddTypeCast (arrow .DATE32 ,
189+ kernels .GetZeroCastKernel (arrow .DATE32 ,
190+ exec .NewExactInput (arrow .FixedWidthTypes .Date32 ),
191+ exec .NewOutputType (arrow .PrimitiveTypes .Int32 )))
192+ castInt32 .AddTypeCast (arrow .TIME32 ,
193+ kernels .GetZeroCastKernel (arrow .TIME32 ,
194+ exec .NewIDInput (arrow .TIME32 ), exec .NewOutputType (arrow .PrimitiveTypes .Int32 )))
195+ out = append (out , castInt32 )
196+
197+ castInt64 := getFn ("cast_int64" , arrow .INT64 , kernels.GetCastToInteger [int64 ](arrow .PrimitiveTypes .Int64 ))
198+ castInt64 .AddTypeCast (arrow .DATE64 ,
199+ kernels .GetZeroCastKernel (arrow .DATE64 ,
200+ exec .NewIDInput (arrow .DATE64 ),
201+ exec .NewOutputType (arrow .PrimitiveTypes .Int64 )))
202+ castInt64 .AddTypeCast (arrow .TIME64 ,
203+ kernels .GetZeroCastKernel (arrow .TIME64 ,
204+ exec .NewIDInput (arrow .TIME64 ),
205+ exec .NewOutputType (arrow .PrimitiveTypes .Int64 )))
206+ castInt64 .AddTypeCast (arrow .DURATION ,
207+ kernels .GetZeroCastKernel (arrow .DURATION ,
208+ exec .NewIDInput (arrow .DURATION ),
209+ exec .NewOutputType (arrow .PrimitiveTypes .Int64 )))
210+ castInt64 .AddTypeCast (arrow .TIMESTAMP ,
211+ kernels .GetZeroCastKernel (arrow .TIMESTAMP ,
212+ exec .NewIDInput (arrow .TIMESTAMP ),
213+ exec .NewOutputType (arrow .PrimitiveTypes .Int64 )))
214+ out = append (out , castInt64 )
215+
216+ out = append (out , getFn ("cast_uint8" , arrow .UINT8 , kernels.GetCastToInteger [uint8 ](arrow .PrimitiveTypes .Uint8 )))
217+ out = append (out , getFn ("cast_uint16" , arrow .UINT16 , kernels.GetCastToInteger [uint16 ](arrow .PrimitiveTypes .Uint16 )))
218+ out = append (out , getFn ("cast_uint32" , arrow .UINT32 , kernels.GetCastToInteger [uint32 ](arrow .PrimitiveTypes .Uint32 )))
219+ out = append (out , getFn ("cast_uint64" , arrow .UINT64 , kernels.GetCastToInteger [uint64 ](arrow .PrimitiveTypes .Uint64 )))
220+
221+ out = append (out , getFn ("cast_half_float" , arrow .FLOAT16 , kernels .GetCommonCastKernels (arrow .FLOAT16 , exec .NewOutputType (arrow .FixedWidthTypes .Float16 ))))
222+ out = append (out , getFn ("cast_float" , arrow .FLOAT32 , kernels.GetCastToFloating [float32 ](arrow .PrimitiveTypes .Float32 )))
223+ out = append (out , getFn ("cast_double" , arrow .FLOAT64 , kernels.GetCastToFloating [float64 ](arrow .PrimitiveTypes .Float64 )))
224+
225+ // cast to decimal128
226+ out = append (out , getFn ("cast_decimal" , arrow .DECIMAL128 , kernels .GetCastToDecimal128 ()))
227+ // cast to decimal256
228+ out = append (out , getFn ("cast_decimal256" , arrow .DECIMAL256 , kernels .GetCastToDecimal256 ()))
229+ return out
230+ }
231+
170232// CastDatum is a convenience function for casting a Datum to another type.
171233// It is equivalent to calling CallFunction(ctx, "cast", opts, Datum) and
172234// should work for Scalar, Array or ChunkedArray Datums.
0 commit comments