Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Add support for writing Go int16 type #505

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions buffer_go18_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

func TestGenericBuffer(t *testing.T) {
testGenericBuffer[booleanColumn](t)
testGenericBuffer[int16Column](t)
testGenericBuffer[int32Column](t)
testGenericBuffer[int64Column](t)
testGenericBuffer[int96Column](t)
Expand Down Expand Up @@ -103,6 +104,7 @@ type generator[T any] interface {
func BenchmarkGenericBuffer(b *testing.B) {
benchmarkGenericBuffer[benchmarkRowType](b)
benchmarkGenericBuffer[booleanColumn](b)
benchmarkGenericBuffer[int16Column](b)
benchmarkGenericBuffer[int32Column](b)
benchmarkGenericBuffer[int64Column](b)
benchmarkGenericBuffer[floatColumn](b)
Expand Down
29 changes: 29 additions & 0 deletions column_buffer_go18.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func writeRowsFuncOf(t reflect.Type, schema *Schema, path columnPath) writeRowsF
reflect.Float64,
reflect.String:
return writeRowsFuncOfRequired(t, schema, path)
case reflect.Int16,
reflect.Uint16:
return writeRowsFuncOfInt16(t, schema, path)

case reflect.Slice:
if t.Elem().Kind() == reflect.Uint8 {
Expand Down Expand Up @@ -440,6 +443,32 @@ func writeRowsFuncOfJSON(t reflect.Type, schema *Schema, path columnPath) writeR
}
}

func writeRowsFuncOfInt16(_ reflect.Type, schema *Schema, path columnPath) writeRowsFunc {
t := reflect.TypeOf(int32(0))
elemSize := uintptr(t.Size())
writeRows := writeRowsFuncOf(t, schema, path)

return func(columns []ColumnBuffer, rows sparse.Array, levels columnLevels) error {
if rows.Len() == 0 {
return writeRows(columns, rows, levels)
}

vals := rows.Int16Array()
for i := 0; i < vals.Len(); i++ {
t := vals.Index(i)
var val int32
val = int32(t)

a := makeArray(unsafecast.PointerOfValue(reflect.ValueOf(val)), 1, elemSize)
if err := writeRows(columns, a, levels); err != nil {
return err
}
}

return nil
}
}

func writeRowsFuncOfTime(_ reflect.Type, schema *Schema, path columnPath) writeRowsFunc {
t := reflect.TypeOf(int64(0))
elemSize := uintptr(t.Size())
Expand Down
8 changes: 8 additions & 0 deletions parquet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ func (row int32Column) generate(prng *rand.Rand) int32Column {
return int32Column{Value: prng.Int31n(100)}
}

type int16Column struct {
Value int16 `parquet:",delta"`
}

func (row int16Column) generate(prng *rand.Rand) int16Column {
return int16Column{Value: int16(prng.Intn(100))}
}

type int64Column struct {
Value int64 `parquet:",delta"`
}
Expand Down
2 changes: 2 additions & 0 deletions reader_go18_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

func TestGenericReader(t *testing.T) {
testGenericReader[booleanColumn](t)
testGenericReader[int16Column](t)
testGenericReader[int32Column](t)
testGenericReader[int64Column](t)
testGenericReader[int96Column](t)
Expand Down Expand Up @@ -227,6 +228,7 @@ func testReadMinPageSize(readSize int, t *testing.T) {
func BenchmarkGenericReader(b *testing.B) {
benchmarkGenericReader[benchmarkRowType](b)
benchmarkGenericReader[booleanColumn](b)
benchmarkGenericReader[int16Column](b)
benchmarkGenericReader[int32Column](b)
benchmarkGenericReader[int64Column](b)
benchmarkGenericReader[floatColumn](b)
Expand Down
5 changes: 5 additions & 0 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ var readerTests = []struct {
model: booleanColumn{},
},

{
scenario: "INT16",
model: int16Column{},
},

{
scenario: "INT32",
model: int32Column{},
Expand Down
1 change: 1 addition & 0 deletions row_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

func TestRowBuffer(t *testing.T) {
testRowBuffer[booleanColumn](t)
testRowBuffer[int16Column](t)
testRowBuffer[int32Column](t)
testRowBuffer[int64Column](t)
testRowBuffer[int96Column](t)
Expand Down
2 changes: 1 addition & 1 deletion schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ func makeNodeOf(t reflect.Type, name string, tag []string) Node {

case "delta":
switch t.Kind() {
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint32, reflect.Uint64:
case reflect.Int, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint32, reflect.Uint64, reflect.Int16, reflect.Uint16:
setEncoding(&DeltaBinaryPacked)
case reflect.String:
setEncoding(&DeltaByteArray)
Expand Down
11 changes: 11 additions & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ func TestSchemaOf(t *testing.T) {
required binary value (JSON);
}
}
}`,
},

{
value: new(struct {
Ushort uint16 `parquet:"ushort"`
Short int16 `parquet:"short"`
}),
print: `message {
required int32 ushort (INT(16,false));
required int32 short (INT(16,true));
}`,
},
}
Expand Down
4 changes: 2 additions & 2 deletions sparse/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (a Int8Array) UnsafeArray() Array { return Array{a.array} }
type Int16Array struct{ array }

func MakeInt16Array(values []int16) Int16Array {
return Int16Array{makeArray(*(*unsafe.Pointer)(unsafe.Pointer(&values)), uintptr(len(values)), 8)}
return Int16Array{makeArray(*(*unsafe.Pointer)(unsafe.Pointer(&values)), uintptr(len(values)), 2)}
}

func UnsafeInt16Array(base unsafe.Pointer, length int, offset uintptr) Int16Array {
Expand Down Expand Up @@ -212,7 +212,7 @@ func (a Uint8Array) UnsafeArray() Array { return Array{a.array} }
type Uint16Array struct{ array }

func MakeUint16Array(values []uint16) Uint16Array {
return Uint16Array{makeArray(*(*unsafe.Pointer)(unsafe.Pointer(&values)), uintptr(len(values)), 8)}
return Uint16Array{makeArray(*(*unsafe.Pointer)(unsafe.Pointer(&values)), uintptr(len(values)), 2)}
}

func UnsafeUint16Array(base unsafe.Pointer, length int, offset uintptr) Uint16Array {
Expand Down
5 changes: 5 additions & 0 deletions value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func TestValueClone(t *testing.T) {
values: []interface{}{false, true},
},

{
scenario: "INT16",
values: []interface{}{int16(0), int16(1), int16(math.MinInt16), int16(math.MaxInt16)},
},

{
scenario: "INT32",
values: []interface{}{int32(0), int32(1), int32(math.MinInt32), int32(math.MaxInt32)},
Expand Down
1 change: 1 addition & 0 deletions writer_go18_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
func BenchmarkGenericWriter(b *testing.B) {
benchmarkGenericWriter[benchmarkRowType](b)
benchmarkGenericWriter[booleanColumn](b)
benchmarkGenericWriter[int16Column](b)
benchmarkGenericWriter[int32Column](b)
benchmarkGenericWriter[int64Column](b)
benchmarkGenericWriter[floatColumn](b)
Expand Down
29 changes: 29 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ type event struct {
Category string `parquet:"-"`
}

type usesShort struct {
Size int16 `parquet:"size"`
}

var writerTests = []struct {
scenario string
version int
Expand Down Expand Up @@ -448,6 +452,31 @@ DOUBLE value
*** row group 1 of 1, values 1 to 2 ***
value 1: R:0 D:0 V:42.0
value 2: R:0 D:0 V:1.0
`,
},

{
scenario: "int16",
version: v2,
rows: []interface{}{
usesShort{Size: 0},
usesShort{Size: 32767},
usesShort{Size: -32768},
},
dump: `row group 0
--------------------------------------------------------------------------------
size: INT32 UNCOMPRESSED DO:0 FPO:4 SZ:40/40/1.00 VC:3 ENC:PLAIN ST:[min: -32768, max: 32767, num_nulls not defined]

size TV=3 RL=0 DL=0
----------------------------------------------------------------------------
page 0: DLE:RLE RLE:RLE VLE:PLAIN ST:[no stats for this column] SZ:12 VC:3

INT32 size
--------------------------------------------------------------------------------
*** row group 1 of 1, values 1 to 3 ***
value 1: R:0 D:0 V:0
value 2: R:0 D:0 V:32767
value 3: R:0 D:0 V:-32768
`,
},
}
Expand Down