Hi Olo,

Whether you use Decimal128 vs Float32, vs Float64 will depend on how much 
precision you need. If you need the precision and exactness for Decimal128 then 
using the builder will first require knowing the Precision and Scale you set in 
the Decimal128Type data type. For adding values, scale is going to be 
important. This is because the way that Decimal128 values are stored is 
essentially as a 128bit integer that, when paired with the scale, works out to 
the value you’re looking for.

For your example of 5.81, let’s say that your Decimal128Type is using a Scale 
of 4: This means that to get the *actual value* of the decimals in your array, 
you would take the value in a given index and multiply it by 10^-4 
(essentially, there’s a bit more complex math in there but I’m simplifying it 
for the purposes of this example). So you’d want to use the value 58100, so 
that when the scale is applied you get 5.81. Now you can use the decimal128 
package to create the decimal128.Num type that is expected for the 
Decimal128Builder’s Append function. (ref: 
https://pkg.go.dev/github.com/apache/arrow/go/[email protected]/arrow/decimal128#FromU64).
 All in all, it looks like this:

builder.Append(decimal128.FromU64(58100))

This is assuming a scale of 4, adjust as necessary for whatever scale you 
placed in the data type. That being said, we do have an easier helper function 
in the not-yet-released development version for v9. In the v9 dev version there 
are FromFloat32 and FromFloat64 functions: 
https://pkg.go.dev/github.com/apache/arrow/go/v9/arrow/decimal128#FromFloat32 
which can be used to create the decimal128.Num value for appending. Notice that 
they both take a precision and scale value, which can be pulled from the 
Decimal128 datatype that you initialized the builder with.

Hope this all helps! Feel free to ask any clarifying questions you need.

--Matt

From: Olo Sawyerr <[email protected]>
Sent: Thursday, June 16, 2022 7:38 PM
To: [email protected]
Subject: [GOLANG] Example for how to use Decimal128Builder

Hi there, I'm trying to store Decimals (exact amounts) in arrow columns. But I 
can't figure out how to use the Decimal128Builder in Golang to store either 
string "5.81" or floats 5.81 as Decimals. ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ 
‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍

Hi there,

I'm trying to store Decimals (exact amounts) in arrow columns. But I can't 
figure out how to use the Decimal128Builder in Golang to store either string 
"5.81" or floats 5.81 as Decimals.

Is the Decimal128Builder the best way to store these? If so can you please give 
an example. If not, what is the appropriate column to use?

Please can you advise?

Thx in advance.

Reply via email to