Skip to content
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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,8 @@ jobs:
mvn -T16 --no-transfer-progress clean install -DskipTests -Dmaven.javadoc.skip=true -Dmaven.source.skip=true
cd fory-core
mvn -T16 --no-transfer-progress test -Dtest=org.apache.fory.xlang.GoXlangTest
cd ../fory-format
mvn -T16 --no-transfer-progress test -Dtest=org.apache.fory.format.GoCrossLanguageTest
- name: Run Go IDL Tests
run: ./integration_tests/idl_tests/run_go_tests.sh

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ go/fory/tests/xlang_test_main
go/fory/tests/xlang/xlang_test_main
go/fory/xlang_test_main
go/fory/xlang
go/fory/tests/row_xlang_bin
go/fory/tests/row_xlang_bin.exe

# Build directories for all languages
# Java
Expand Down
1 change: 1 addition & 0 deletions ci/tasks/go.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ def run():
logging.info("Executing fory go tests")
common.cd_project_subdir("go/fory")
common.exec_cmd("go test -v")
common.exec_cmd("go test -v ./row/...")
logging.info("Executing fory go tests succeeds")
126 changes: 126 additions & 0 deletions go/fory/row/array_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// 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 row

import (
"encoding/binary"
"math"
"testing"

fory "github.com/apache/fory/go/fory"
"github.com/stretchr/testify/require"
)

// Arrays store an 8-byte count, a null bitmap rounded to 8-byte words,
// then elements at their natural width, padded to an 8-byte boundary.
func TestArrayExactLayout(t *testing.T) {
buf := fory.NewByteBuffer(nil)
w := NewArrayWriter(List(Int32Type{}).Elem, buf)
require.NoError(t, w.Reset(3))
w.WriteInt32(0, 1)
w.WriteInt32(1, 2)
w.WriteInt32(2, 3)
require.Equal(t, []byte{
3, 0, 0, 0, 0, 0, 0, 0, // element count
0, 0, 0, 0, 0, 0, 0, 0, // null bitmap
1, 0, 0, 0, 2, 0, 0, 0, // int32 elements at natural width
3, 0, 0, 0, 0, 0, 0, 0, // last element + alignment padding
}, w.ToBytes())

a := NewArrayData(List(Int32Type{}).Elem, w.ToBytes())
require.Equal(t, 3, a.NumElements())
require.Equal(t, int32(2), a.Int32(1))
}

func TestStringArrayWithNullElement(t *testing.T) {
buf := fory.NewByteBuffer(nil)
elem := List(StringType{}).Elem
w := NewArrayWriter(elem, buf)
require.NoError(t, w.Reset(3))
w.WriteString(0, "str1")
w.SetNullAt(1)
w.WriteString(2, "str2")

a := NewArrayData(elem, w.ToBytes())
require.Equal(t, 3, a.NumElements())
require.Equal(t, "str1", a.String(0))
require.True(t, a.IsNullAt(1))
require.Equal(t, "", a.String(1))
require.Equal(t, "str2", a.String(2))
}

func TestEmptyArray(t *testing.T) {
buf := fory.NewByteBuffer(nil)
elem := List(Int64Type{}).Elem
w := NewArrayWriter(elem, buf)
require.NoError(t, w.Reset(0))
require.Equal(t, []byte{0, 0, 0, 0, 0, 0, 0, 0}, w.ToBytes())

a := NewArrayData(elem, w.ToBytes())
require.Equal(t, 0, a.NumElements())
require.Panics(t, func() { a.Int64(0) })
}

func TestNestedArray(t *testing.T) {
buf := fory.NewByteBuffer(nil)
outerElem := List(List(Int32Type{})).Elem // item: list<int32>
innerElem := List(Int32Type{}).Elem // item: int32

outer := NewArrayWriter(outerElem, buf)
require.NoError(t, outer.Reset(2))
inner := NewArrayWriter(innerElem, buf)
for i, values := range [][]int32{{1, 2}, {3, 4, 5}} {
start := buf.WriterIndex()
require.NoError(t, inner.Reset(len(values)))
for j, v := range values {
inner.WriteInt32(j, v)
}
outer.SetOffsetAndSize(i, start, buf.WriterIndex()-start)
}

a := NewArrayData(outerElem, outer.ToBytes())
require.Equal(t, 2, a.NumElements())
first := a.Array(0)
require.Equal(t, 2, first.NumElements())
require.Equal(t, int32(2), first.Int32(1))
second := a.Array(1)
require.Equal(t, 3, second.NumElements())
require.Equal(t, int32(5), second.Int32(2))
}

func TestArrayResetRejectsInvalidLength(t *testing.T) {
w := NewArrayWriter(List(Int64Type{}).Elem, fory.NewByteBuffer(nil))
require.Error(t, w.Reset(-1))
require.Error(t, w.Reset(1<<40))
}

// Forged element counts must fail validation even when the arithmetic
// would overflow int64 and wrap the required size negative.
func TestArrayValidateBoundsRejectsForgedCounts(t *testing.T) {
for _, count := range []uint64{
math.MaxUint64, // negative after int conversion
math.MaxInt64, // bitmap width and product overflow
uint64(math.MaxInt64/8) + 1, // product overflow with 8-byte elements
1 << 32, // plausible-looking but far beyond the data
} {
data := make([]byte, 16)
binary.LittleEndian.PutUint64(data, count)
a := NewArrayData(List(Int64Type{}).Elem, data)
require.Error(t, a.validateBounds(), "count %d", count)
}
}
49 changes: 49 additions & 0 deletions go/fory/row/bitmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 row implements the Fory standard row format.
package row

// The null bitmap tracks one bit per field or array element.

// bitmapWidthInBytes returns the null bitmap size for n fields or
// elements, rounded up to a whole 8-byte word.
func bitmapWidthInBytes(n int) int {
return ((n + 63) / 64) * 8
}

// setBit marks index i as null.
func setBit(bitmap []byte, i int) {
bitmap[i>>3] |= 1 << (uint(i) & 7)
}

// clearBit marks index i as not null.
func clearBit(bitmap []byte, i int) {
bitmap[i>>3] &^= 1 << (uint(i) & 7)
}

// getBit reports whether index i is null.
func getBit(bitmap []byte, i int) bool {
return bitmap[i>>3]&(1<<(uint(i)&7)) != 0
}

// roundToWord rounds n up to the nearest multiple of 8. Row Format requires
// every variable-length value and data region to be zero-padded to an
// 8-byte boundary.
func roundToWord(n int) int {
return (n + 7) &^ 7
}
87 changes: 87 additions & 0 deletions go/fory/row/bitmap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// 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 row

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestBitmapWidthInBytes(t *testing.T) {
cases := []struct{ n, want int }{
{0, 0},
{1, 8},
{10, 8},
{63, 8},
{64, 8},
{65, 16},
{128, 16},
{129, 24},
}
for _, c := range cases {
require.Equal(t, c.want, bitmapWidthInBytes(c.n), "n=%d", c.n)
}
}

func TestBitOperations(t *testing.T) {
bitmap := make([]byte, 16)

// Bit 0 of byte 0 is index 0 (LSB-first).
setBit(bitmap, 0)
require.Equal(t, byte(0b1), bitmap[0])
require.True(t, getBit(bitmap, 0))

setBit(bitmap, 2)
require.Equal(t, byte(0b101), bitmap[0])

// Index 7 stays in byte 0; index 8 moves to byte 1.
setBit(bitmap, 7)
require.Equal(t, byte(0b10000101), bitmap[0])
setBit(bitmap, 8)
require.Equal(t, byte(0b1), bitmap[1])

// Word boundary: index 63 is the last bit of the first word,
// index 64 the first bit of the second.
setBit(bitmap, 63)
require.Equal(t, byte(0b10000000), bitmap[7])
setBit(bitmap, 64)
require.Equal(t, byte(0b1), bitmap[8])

// Clearing affects only the targeted bit.
clearBit(bitmap, 2)
require.False(t, getBit(bitmap, 2))
require.True(t, getBit(bitmap, 0))
require.True(t, getBit(bitmap, 7))

require.False(t, getBit(bitmap, 1))
}

func TestRoundToWord(t *testing.T) {
cases := []struct{ n, want int }{
{0, 0},
{1, 8},
{7, 8},
{8, 8},
{9, 16},
{16, 16},
}
for _, c := range cases {
require.Equal(t, c.want, roundToWord(c.n), "n=%d", c.n)
}
}
Loading
Loading