Skip to content

Commit

Permalink
fix for byte array as decimal with negative value
Browse files Browse the repository at this point in the history
  • Loading branch information
hangxie committed Dec 13, 2021
1 parent 444d000 commit b091a22
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions example/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ func main() {
if err = pr.Read(&tps); err != nil {
log.Println("Read error", err)
}
tps[0].Decimal3 = types.DECIMAL_BYTE_ARRAY_ToString([]byte(tps[0].Decimal3), 10, 2)
tps[0].Decimal4 = types.DECIMAL_BYTE_ARRAY_ToString([]byte(tps[0].Decimal4), 20, 2)
log.Println(tps)
}
pr.ReadStop()
Expand Down
10 changes: 9 additions & 1 deletion types/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ func DECIMAL_INT_ToString(dec int64, precision int, scale int) string {
}

func DECIMAL_BYTE_ARRAY_ToString(dec []byte, precision int, scale int) string {
sign := ""
if dec[0] > 0x7f {
sign = "-"
for i := range dec {
dec[i] = dec[i] ^ 0xff
}
dec[len(dec)-1] += 1
}
a := new(big.Int)
a.SetBytes(dec)
sa := a.Text(10)
Expand All @@ -130,5 +138,5 @@ func DECIMAL_BYTE_ARRAY_ToString(dec []byte, precision int, scale int) string {
ln := len(sa)
sa = sa[:ln-scale] + "." + sa[ln-scale:]
}
return sa
return sign + sa
}
5 changes: 5 additions & 0 deletions types/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ func TestDECIMAL(t *testing.T) {
t.Error("DECIMAL_BYTE_ARRAY_ToString error: ", a3, sa3)
}

a4, _ := StrToParquetType("-123.456", parquet.TypePtr(parquet.Type_BYTE_ARRAY), parquet.ConvertedTypePtr(parquet.ConvertedType_DECIMAL), 9, 3)
sa4 := DECIMAL_BYTE_ARRAY_ToString([]byte(a4.(string)), 9, 3)
if sa4 != "-123.456" {
t.Error("DECIMAL_BYTE_ARRAY_ToString error: ", a4, sa4)
}
}

0 comments on commit b091a22

Please sign in to comment.