in transforms.go [370:424]
func (t TruncateTransform) Transformer(src Type) (func(any) any, error) {
switch src.(type) {
case Int32Type:
return func(v any) any {
if v == nil {
return nil
}
val := v.(int32)
return val - (val % int32(t.Width))
}, nil
case Int64Type:
return func(v any) any {
if v == nil {
return nil
}
val := v.(int64)
return val - (val % int64(t.Width))
}, nil
case StringType, BinaryType:
return func(v any) any {
switch v := v.(type) {
case string:
return v[:min(len(v), t.Width)]
case []byte:
return v[:min(len(v), t.Width)]
default:
return nil
}
}, nil
case DecimalType:
bigWidth := big.NewInt(int64(t.Width))
return func(v any) any {
if v == nil {
return nil
}
val := v.(Decimal)
unscaled := val.Val.BigInt()
// unscaled - (((unscaled % width) + width) % width)
applied := (&big.Int{}).Mod(unscaled, bigWidth)
applied.Add(applied, bigWidth).Mod(applied, bigWidth)
val.Val = decimal128.FromBigInt(unscaled.Sub(unscaled, applied))
return val
}, nil
}
return nil, fmt.Errorf("%w: cannot truncate for type %s",
ErrInvalidArgument, src)
}