Skip to content

Commit 181e676

Browse files
authored
feat(scalar): Support pointer dereferencing in decimal (#938)
Follow up to #937, also needed for cloudquery/cloudquery#11115 When reading data from a database it is common to pass a pointer to a value to support reading `nil` values, so the type of the value read is a pointer to a pointer. Without this PR, if someone uses the decimal scalar they need to dereferences the pointer manually before setting the scalar value. ---
1 parent 159e975 commit 181e676

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

scalar/decimal.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ func (s *Decimal256) Set(val any) error {
166166
}
167167
return s.Set(*value)
168168
default:
169+
if originalSrc, ok := underlyingPtrType(val); ok {
170+
return s.Set(originalSrc)
171+
}
169172
return &ValidationError{Type: s.DataType(), Msg: noConversion, Value: value}
170173
}
171174
s.Valid = true
@@ -324,6 +327,9 @@ func (s *Decimal128) Set(val any) error {
324327
}
325328
return s.Set(*value)
326329
default:
330+
if originalSrc, ok := underlyingPtrType(val); ok {
331+
return s.Set(originalSrc)
332+
}
327333
return &ValidationError{Type: s.DataType(), Msg: noConversion, Value: value}
328334
}
329335
s.Valid = true

scalar/decimal_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ func TestDecimal128Set(t *testing.T) {
2626
uint32Val := uint32(1)
2727
uint64Val := uint64(1)
2828

29+
intValPointer := &intVal
30+
2931
successfulTests := []struct {
3032
source any
3133
decimalType *arrow.Decimal128Type
@@ -53,6 +55,7 @@ func TestDecimal128Set(t *testing.T) {
5355
{source: &uint16Val, expect: Decimal128{Value: decimal128.FromU64(1), Valid: true}},
5456
{source: &uint32Val, expect: Decimal128{Value: decimal128.FromU64(1), Valid: true}},
5557
{source: &uint64Val, expect: Decimal128{Value: decimal128.FromU64(1), Valid: true}},
58+
{source: &intValPointer, expect: Decimal128{Value: decimal128.FromI64(1), Valid: true}},
5659
}
5760

5861
for i, tt := range successfulTests {
@@ -81,6 +84,8 @@ func TestDecimal256Set(t *testing.T) {
8184
uint32Val := uint32(1)
8285
uint64Val := uint64(1)
8386

87+
intValPointer := &intVal
88+
8489
successfulTests := []struct {
8590
source any
8691
decimalType *arrow.Decimal256Type
@@ -108,6 +113,7 @@ func TestDecimal256Set(t *testing.T) {
108113
{source: &uint16Val, expect: Decimal256{Value: decimal256.FromU64(1), Valid: true}},
109114
{source: &uint32Val, expect: Decimal256{Value: decimal256.FromU64(1), Valid: true}},
110115
{source: &uint64Val, expect: Decimal256{Value: decimal256.FromU64(1), Valid: true}},
116+
{source: &intValPointer, expect: Decimal256{Value: decimal256.FromI64(1), Valid: true}},
111117
}
112118

113119
for i, tt := range successfulTests {

0 commit comments

Comments
 (0)