hcrosse commented on code in PR #1501: URL: https://github.com/apache/arrow-go/pull/1501#discussion_r4520717626
########## arrow/scalar/decimal.go: ########## @@ -0,0 +1,118 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package scalar + +import ( + "fmt" + "unsafe" + + "github.com/apache/arrow-go/v18/arrow" + "github.com/apache/arrow-go/v18/arrow/decimal" + "github.com/apache/arrow-go/v18/arrow/decimal128" + "github.com/apache/arrow-go/v18/arrow/decimal256" +) + +type decimalScalar[T interface { + decimal.DecimalTypes + decimal.Num[T] +}] struct { + scalar + Value T +} + +type ( + Decimal32 = decimalScalar[decimal.Decimal32] + Decimal64 = decimalScalar[decimal.Decimal64] +) + +func NewDecimal32Scalar(val decimal.Decimal32, typ arrow.DataType) *Decimal32 { + return &Decimal32{scalar{typ, true}, val} +} + +func NewDecimal64Scalar(val decimal.Decimal64, typ arrow.DataType) *Decimal64 { + return &Decimal64{scalar{typ, true}, val} +} + +func (s *decimalScalar[T]) Data() []byte { + return unsafe.Slice((*byte)(unsafe.Pointer(&s.Value)), unsafe.Sizeof(s.Value)) +} + +func (s *decimalScalar[T]) value() interface{} { return s.Value } + +func (s *decimalScalar[T]) equals(rhs Scalar) bool { + return s.Value == rhs.(*decimalScalar[T]).Value +} + +func (s *decimalScalar[T]) String() string { + if !s.Valid { + return "null" + } + val, err := s.CastTo(arrow.BinaryTypes.String) + if err != nil { + return "..." + } + return string(val.(*String).Value.Bytes()) +} + +// CastTo casts to string or to a decimal type at least as wide as the +// scalar's own type. +func (s *decimalScalar[T]) CastTo(to arrow.DataType) (Scalar, error) { + if !s.Valid { + return MakeNullScalar(to), nil + } + + dt := s.Type.(arrow.DecimalType) + if to.ID() == arrow.STRING { + return NewStringScalar(s.Value.ToBigFloat(dt.GetScale()).Text('g', int(dt.GetPrecision()))), nil Review Comment: Confirmed. This also affected `ValueStr` and JSON output for `array.Decimal32` and `array.Decimal64` on main. `ToBigFloat` now applies the power of two separately and computes the power of five with a `big.Float` at 128 bits of precision. `TestDecimalToReal` covers scales of 19 and negative 19, and the scalar tests check `String()` for both new types. String output now uses a shared helper to format the coefficient directly when the magnitude of the scale exceeds 1000, avoiding slow decimal conversion for tiny values. ########## arrow/scalar/parse.go: ########## @@ -868,6 +883,20 @@ func ParseScalar(dt arrow.DataType, val string) (Scalar, error) { return NewTime64Scalar(tm, dt), nil case arrow.DICTIONARY: return ParseScalar(dt.(*arrow.DictionaryType).ValueType, val) + case arrow.DECIMAL32: + typ := dt.(*arrow.Decimal32Type) + n, err := decimal.Decimal32FromString(val, typ.Precision, typ.Scale) Review Comment: Confirmed. This also affected `Decimal32FromString`, `Decimal64FromString`, `array.FromJSON` and the decimal builders' `AppendValueFromString` methods on main. For nonnegative scales, `decimalFromString` now checks the signed range before narrowing and no longer builds the multiplier in `int64`. For negative scales it divides using `big.Int` before narrowing, preserving representable results that already worked. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
