Skip to content

Commit

Permalink
GH-35975: [Go] Support importing decimal256 (#35981)
Browse files Browse the repository at this point in the history
### Rationale for this change

We didn't implement this type.

### What changes are included in this PR?

We now import this type. (And handle some other error cases.)

### Are these changes tested?

Yes.

### Are there any user-facing changes?

Yes.
* Closes: #35975

Authored-by: David Li <li.davidm96@gmail.com>
Signed-off-by: Matt Topol <zotthewizard@gmail.com>
  • Loading branch information
lidavidm committed Jun 7, 2023
1 parent 9dd4c52 commit 5e5002d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
33 changes: 27 additions & 6 deletions go/arrow/cdata/cdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,35 @@ func importSchema(schema *CArrowSchema) (ret arrow.Field, err error) {
case "d": // decimal types are d:<precision>,<scale>[,<bitsize>] size is assumed 128 if left out
props := typs[1]
propList := strings.Split(props, ",")
if len(propList) == 3 {
err = xerrors.New("only decimal128 is supported")
return
bitwidth := 128
var precision, scale int

if len(propList) < 2 || len(propList) > 3 {
return ret, xerrors.Errorf("invalid decimal spec '%s': wrong number of properties", f)
} else if len(propList) == 3 {
bitwidth, err = strconv.Atoi(propList[2])
if err != nil {
return ret, xerrors.Errorf("could not parse decimal bitwidth in '%s': %s", f, err.Error())
}
}

precision, err = strconv.Atoi(propList[0])
if err != nil {
return ret, xerrors.Errorf("could not parse decimal precision in '%s': %s", f, err.Error())
}

precision, _ := strconv.Atoi(propList[0])
scale, _ := strconv.Atoi(propList[1])
dt = &arrow.Decimal128Type{Precision: int32(precision), Scale: int32(scale)}
scale, err = strconv.Atoi(propList[1])
if err != nil {
return ret, xerrors.Errorf("could not parse decimal scale in '%s': %s", f, err.Error())
}

if bitwidth == 128 {
dt = &arrow.Decimal128Type{Precision: int32(precision), Scale: int32(scale)}
} else if bitwidth == 256 {
dt = &arrow.Decimal256Type{Precision: int32(precision), Scale: int32(scale)}
} else {
return ret, xerrors.Errorf("only decimal128 and decimal256 are supported, got '%s'", f)
}
}

if f[0] == '+' { // types with children
Expand Down
26 changes: 26 additions & 0 deletions go/arrow/cdata/cdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func TestPrimitiveSchemas(t *testing.T) {
{&arrow.Decimal128Type{Precision: 16, Scale: 4}, "d:16,4"},
{&arrow.Decimal128Type{Precision: 15, Scale: 0}, "d:15,0"},
{&arrow.Decimal128Type{Precision: 15, Scale: -4}, "d:15,-4"},
{&arrow.Decimal256Type{Precision: 15, Scale: -4}, "d:15,-4,256"},
}

for _, tt := range tests {
Expand All @@ -138,6 +139,31 @@ func TestPrimitiveSchemas(t *testing.T) {
}
}

func TestDecimalSchemaErrors(t *testing.T) {
tests := []struct {
fmt string
errorMessage string
}{
{"d:", "invalid decimal spec 'd:': wrong number of properties"},
{"d:1", "invalid decimal spec 'd:1': wrong number of properties"},
{"d:1,2,3,4", "invalid decimal spec 'd:1,2,3,4': wrong number of properties"},
{"d:a,2,3", "could not parse decimal precision in 'd:a,2,3':"},
{"d:1,a,3", "could not parse decimal scale in 'd:1,a,3':"},
{"d:1,2,a", "could not parse decimal bitwidth in 'd:1,2,a':"},
{"d:1,2,384", "only decimal128 and decimal256 are supported, got 'd:1,2,384'"},
}

for _, tt := range tests {
t.Run(tt.fmt, func(t *testing.T) {
sc := testPrimitive(tt.fmt)

_, err := ImportCArrowField(&sc)
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.errorMessage)
})
}
}

func TestImportTemporalSchema(t *testing.T) {
tests := []struct {
typ arrow.DataType
Expand Down

0 comments on commit 5e5002d

Please sign in to comment.