You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fory.apache.org by "ayush00git (via GitHub)" <gi...@apache.org> on 2026/07/30 07:14:14 UTC

[PR] feat(go): support native fory row format [fory]

ayush00git opened a new pull request, #3901:
URL: https://github.com/apache/fory/pull/3901

   <!--
   **Thanks for contributing to Apache Fory™.**
   
   **If this is your first time opening a PR on fory, you can refer to [CONTRIBUTING.md](https://github.com/apache/fory/blob/main/CONTRIBUTING.md).**
   
   Contribution Checklist
   
       - The **Apache Fory™** community has requirements on the naming of pr titles. You can also find instructions in [CONTRIBUTING.md](https://github.com/apache/fory/blob/main/CONTRIBUTING.md).
   
       - Apache Fory™ has a strong focus on performance. If the PR you submit will have an impact on performance, please benchmark it first and provide the benchmark result here.
   -->
   
   ## Why?
   
   <!-- Describe the purpose of this PR. -->
   
   ## What does this PR do?
   
   <!-- Describe the details of this PR. -->
   
   ## Related issues
   
   <!--
   Is there any related issue? If this PR closes them you say say fix/closes:
   
   - #xxxx0
   - #xxxx1
   - Fixes #xxxx2
   -->
   
   ## AI Contribution Checklist
   
   <!-- Full requirements and disclosure template:
   https://github.com/apache/fory/blob/main/AI_POLICY.md#9-contributor-checklist-for-ai-assisted-prs -->
   
   - [ ] Substantial AI assistance was used in this PR: `yes` / `no`
   - [ ] If `yes`, I included a completed [AI Contribution Checklist](https://github.com/apache/fory/blob/main/AI_POLICY.md#9-contributor-checklist-for-ai-assisted-prs) in this PR description and the required `AI Usage Disclosure`.
   - [ ] If `yes`, my PR description includes the required `ai_review` summary and screenshot evidence or equivalent persisted links of the final clean AI review results from both fresh reviewers described in `AI_POLICY.md`, the Fory-guided reviewer and the independent general reviewer, on the current PR diff or current HEAD after the latest code changes.
   
   <!-- If substantial AI assistance = `yes`, paste the completed checklist and disclosure block here, including the final ai_review summary and screenshot evidence or equivalent persisted links from both fresh reviewers described in AI_POLICY.md, the Fory-guided reviewer and the independent general reviewer, on the current PR diff or current HEAD after the latest code changes. -->
   
   ## Does this PR introduce any user-facing change?
   
   <!--
   If any user-facing interface changes, please [open an issue](https://github.com/apache/fory/issues/new/choose) describing the need to do so and update the document if necessary.
   
   Delete section if not applicable.
   -->
   
   - [ ] Does this PR introduce any public API change?
   - [ ] Does this PR introduce any binary protocol compatibility change?
   
   ## Benchmark
   
   <!--
   When the PR has an impact on performance (if you don't know whether the PR will have an impact on performance, you can submit the PR first, and if it will have impact on performance, the code reviewer will explain it), be sure to attach a benchmark data here.
   
   Delete section if not applicable.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org


Re: [PR] feat(go): support native fory row format [fory]

Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on code in PR #3901:
URL: https://github.com/apache/fory/pull/3901#discussion_r3704758656


##########
go/fory/row/encoder.go:
##########
@@ -0,0 +1,490 @@
+// 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"
+	"fmt"
+	"reflect"
+	"time"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+// Encoder converts values of struct type T to and from row bytes using
+// a codec tree compiled once with reflection at construction time. The
+// schema is inferred per InferSchema.
+//
+// An Encoder owns a reusable write buffer and is NOT goroutine-safe;
+// create one Encoder per goroutine. Decoding is safe on corrupt or
+// untrusted input.
+type Encoder[T any] struct {
+	codec *structCodec
+	hash  int64
+	buf   *fory.ByteBuffer
+}
+
+func NewEncoder[T any]() (*Encoder[T], error) {
+	t := reflect.TypeOf((*T)(nil)).Elem()
+	if t.Kind() != reflect.Struct {
+		return nil, fmt.Errorf("row: Encoder requires a struct type, got %v", t)
+	}
+	buf := fory.NewByteBuffer(nil)
+	codec, err := newStructCodec(t, buf)
+	if err != nil {
+		return nil, err
+	}
+	return &Encoder[T]{codec: codec, hash: ComputeSchemaHash(codec.schema), buf: buf}, nil
+}
+
+func (e *Encoder[T]) Schema() *Schema { return e.codec.schema }
+
+// SchemaHash returns the cross-language schema hash used by Encode and
+// Decode to detect out-of-sync struct definitions.
+func (e *Encoder[T]) SchemaHash() int64 { return e.hash }
+
+// ToRow serializes v to row bytes (no framing, just the row).
+func (e *Encoder[T]) ToRow(v *T) ([]byte, error) {
+	e.buf.SetWriterIndex(0)
+	if err := e.codec.writeStruct(reflect.ValueOf(v).Elem()); err != nil {
+		return nil, err
+	}
+	out := make([]byte, e.buf.WriterIndex())
+	copy(out, e.buf.GetData()[:e.buf.WriterIndex()])
+	return out, nil
+}
+
+// FromRow deserializes row bytes produced with this schema.
+func (e *Encoder[T]) FromRow(data []byte) (T, error) {
+	var out T
+	err := e.FromRowInto(data, &out)
+	return out, err
+}
+
+func (e *Encoder[T]) FromRowInto(data []byte, out *T) (err error) {
+	defer func() {
+		if p := recover(); p != nil {
+			err = fmt.Errorf("row: corrupt row data: %v", p)
+		}
+	}()
+	return e.codec.readStruct(NewRow(e.codec.schema, data), reflect.ValueOf(out).Elem())
+}
+
+// Encode frames the row for cross-language exchange: an int64
+// little-endian schema hash followed by the row bytes, matching the
+// Java and Python row encoders.
+func (e *Encoder[T]) Encode(v *T) ([]byte, error) {
+	rowBytes, err := e.ToRow(v)
+	if err != nil {
+		return nil, err
+	}
+	out := make([]byte, 8+len(rowBytes))
+	binary.LittleEndian.PutUint64(out, uint64(e.hash))
+	copy(out[8:], rowBytes)
+	return out, nil
+}
+
+// Decode verifies the schema hash and deserializes the row.
+func (e *Encoder[T]) Decode(data []byte) (T, error) {
+	var out T
+	if len(data) < 8 {
+		return out, fmt.Errorf("row: encoded data of %d bytes is too short for the schema hash", len(data))
+	}
+	hash := int64(binary.LittleEndian.Uint64(data))
+	if hash != e.hash {
+		return out, fmt.Errorf("row: schema hash mismatch: data has %d, encoder expects %d; writer and reader struct definitions are out of sync", hash, e.hash)
+	}
+	err := e.FromRowInto(data[8:], &out)
+	return out, err
+}
+
+// slotWriter is the shared write surface of RowWriter and ArrayWriter,
+// letting one value codec serve struct fields and array elements.
+type slotWriter interface {
+	SetNullAt(i int)
+	WriteBool(i int, v bool)
+	WriteInt8(i int, v int8)
+	WriteInt16(i int, v int16)
+	WriteInt32(i int, v int32)
+	WriteInt64(i int, v int64)
+	WriteFloat32(i int, v float32)
+	WriteFloat64(i int, v float64)
+	WriteDate(i int, d fory.Date) error
+	WriteTimestamp(i int, t time.Time)
+	WriteDuration(i int, d time.Duration)
+	WriteString(i int, s string) error
+	WriteBytes(i int, b []byte) error
+	SetOffsetAndSize(i, absStart, size int) error
+}
+
+// valueReader is the shared read surface of Row and ArrayData.
+type valueReader interface {
+	IsNullAt(i int) bool
+	Bool(i int) bool
+	Int8(i int) int8
+	Int16(i int) int16
+	Int32(i int) int32
+	Int64(i int) int64
+	Float32(i int) float32
+	Float64(i int) float64
+	Date(i int) fory.Date
+	Timestamp(i int) time.Time
+	Duration(i int) time.Duration
+	String(i int) string
+	Binary(i int) []byte
+	Struct(i int) *Row
+	Array(i int) *ArrayData
+	Map(i int) *MapData
+}
+
+// valueCodec reads or writes one value at slot/element i. Write
+// functions handle nil for nilable Go types; read functions overwrite
+// the target even when the field is null, so reused targets stay clean.
+type valueCodec struct {
+	write func(w slotWriter, i int, v reflect.Value) error
+	read  func(g valueReader, i int, v reflect.Value) error
+}
+
+// structCodec writes and reads one struct level; nested structs hold
+// their own child codec with a child RowWriter sharing the buffer.
+type structCodec struct {
+	schema *Schema
+	writer *RowWriter
+	fields []fieldCodec
+}
+
+type fieldCodec struct {
+	goIndex int
+	codec   valueCodec
+}
+
+func newStructCodec(t reflect.Type, buf *fory.ByteBuffer) (*structCodec, error) {
+	layout, err := inferStructLayout(t, nil)
+	if err != nil {
+		return nil, err
+	}
+	sc := &structCodec{
+		schema: layout.schema,
+		writer: NewRowWriterWithBuffer(layout.schema, buf),
+		fields: make([]fieldCodec, 0, layout.schema.NumFields()),
+	}
+	for ordinal, goIndex := range layout.indexes {
+		codec, err := newValueCodec(t.Field(goIndex).Type, layout.schema.Field(ordinal).Type, buf)
+		if err != nil {
+			return nil, err
+		}
+		sc.fields = append(sc.fields, fieldCodec{goIndex: goIndex, codec: codec})
+	}
+	return sc, nil
+}
+
+func (sc *structCodec) writeStruct(v reflect.Value) error {
+	sc.writer.Reset()
+	for ordinal := range sc.fields {
+		fc := &sc.fields[ordinal]
+		if err := fc.codec.write(sc.writer, ordinal, v.Field(fc.goIndex)); err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
+func (sc *structCodec) readStruct(r *Row, v reflect.Value) error {
+	for ordinal := range sc.fields {
+		fc := &sc.fields[ordinal]
+		if err := fc.codec.read(r, ordinal, v.Field(fc.goIndex)); err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
+func newValueCodec(goType reflect.Type, dataType DataType, buf *fory.ByteBuffer) (valueCodec, error) {
+	if goType.Kind() == reflect.Ptr {
+		elemType := goType.Elem()
+		inner, err := newValueCodec(elemType, dataType, buf)
+		if err != nil {
+			return valueCodec{}, err
+		}
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error {
+				if v.IsNil() {
+					w.SetNullAt(i)
+					return nil
+				}
+				return inner.write(w, i, v.Elem())
+			},
+			read: func(g valueReader, i int, v reflect.Value) error {
+				if g.IsNullAt(i) {
+					v.Set(reflect.Zero(goType))
+					return nil
+				}
+				p := reflect.New(elemType)
+				if err := inner.read(g, i, p.Elem()); err != nil {
+					return err
+				}
+				v.Set(p)
+				return nil
+			},
+		}, nil
+	}
+
+	switch dt := dataType.(type) {
+	case BoolType:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error { w.WriteBool(i, v.Bool()); return nil },
+			read:  func(g valueReader, i int, v reflect.Value) error { v.SetBool(g.Bool(i)); return nil },
+		}, nil
+	case Int8Type:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error { w.WriteInt8(i, int8(v.Int())); return nil },
+			read:  func(g valueReader, i int, v reflect.Value) error { v.SetInt(int64(g.Int8(i))); return nil },
+		}, nil
+	case Int16Type:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error { w.WriteInt16(i, int16(v.Int())); return nil },
+			read:  func(g valueReader, i int, v reflect.Value) error { v.SetInt(int64(g.Int16(i))); return nil },
+		}, nil
+	case Int32Type:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error { w.WriteInt32(i, int32(v.Int())); return nil },
+			read:  func(g valueReader, i int, v reflect.Value) error { v.SetInt(int64(g.Int32(i))); return nil },
+		}, nil
+	case Int64Type:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error { w.WriteInt64(i, v.Int()); return nil },
+			read:  func(g valueReader, i int, v reflect.Value) error { v.SetInt(g.Int64(i)); return nil },
+		}, nil
+	case Float32Type:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error { w.WriteFloat32(i, float32(v.Float())); return nil },
+			read:  func(g valueReader, i int, v reflect.Value) error { v.SetFloat(float64(g.Float32(i))); return nil },
+		}, nil
+	case Float64Type:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error { w.WriteFloat64(i, v.Float()); return nil },
+			read:  func(g valueReader, i int, v reflect.Value) error { v.SetFloat(g.Float64(i)); return nil },
+		}, nil
+	case Date32Type:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error { return w.WriteDate(i, v.Interface().(fory.Date)) },
+			read:  func(g valueReader, i int, v reflect.Value) error { v.Set(reflect.ValueOf(g.Date(i))); return nil },
+		}, nil
+	case TimestampType:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error {
+				w.WriteTimestamp(i, v.Interface().(time.Time))
+				return nil
+			},
+			read: func(g valueReader, i int, v reflect.Value) error { v.Set(reflect.ValueOf(g.Timestamp(i))); return nil },
+		}, nil
+	case DurationType:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error {
+				w.WriteDuration(i, time.Duration(v.Int()))
+				return nil
+			},
+			read: func(g valueReader, i int, v reflect.Value) error { v.SetInt(int64(g.Duration(i))); return nil },
+		}, nil
+	case StringType:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error { return w.WriteString(i, v.String()) },
+			read:  func(g valueReader, i int, v reflect.Value) error { v.SetString(g.String(i)); return nil },
+		}, nil
+	case BinaryType:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error {
+				if v.IsNil() {
+					w.SetNullAt(i)
+					return nil
+				}
+				return w.WriteBytes(i, v.Bytes())
+			},
+			read: func(g valueReader, i int, v reflect.Value) error {
+				b := g.Binary(i)
+				if b == nil {
+					v.SetBytes(nil)
+					return nil
+				}
+				// Copy so the decoded struct never aliases the input.
+				owned := make([]byte, len(b))
+				copy(owned, b)
+				v.SetBytes(owned)
+				return nil
+			},
+		}, nil
+	case *ListType:
+		return newListCodec(goType, dt, buf)
+	case *MapType:
+		return newMapCodec(goType, dt, buf)
+	case *StructType:
+		child, err := newStructCodec(goType, buf)
+		if err != nil {
+			return valueCodec{}, err
+		}
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error {
+				start := child.writer.buf.WriterIndex()
+				if err := child.writeStruct(v); err != nil {
+					return err
+				}
+				return w.SetOffsetAndSize(i, start, child.writer.buf.WriterIndex()-start)
+			},
+			read: func(g valueReader, i int, v reflect.Value) error {
+				nested := g.Struct(i)
+				if nested == nil {
+					v.Set(reflect.Zero(goType))
+					return nil
+				}
+				return child.readStruct(nested, v)
+			},
+		}, nil
+	default:
+		return valueCodec{}, fmt.Errorf("row: data type %s is not supported by the encoder", dataType)
+	}
+}
+
+func newListCodec(goType reflect.Type, listType *ListType, buf *fory.ByteBuffer) (valueCodec, error) {
+	elemCodec, err := newValueCodec(goType.Elem(), listType.Elem.Type, buf)
+	if err != nil {
+		return valueCodec{}, err
+	}
+	writer := NewArrayWriter(listType.Elem, buf)
+	return valueCodec{
+		write: func(w slotWriter, i int, v reflect.Value) error {
+			if v.IsNil() {
+				w.SetNullAt(i)
+				return nil
+			}
+			n := v.Len()
+			start := buf.WriterIndex()
+			if err := writer.Reset(n); err != nil {
+				return err
+			}
+			for j := 0; j < n; j++ {
+				if err := elemCodec.write(writer, j, v.Index(j)); err != nil {
+					return err
+				}
+			}
+			return w.SetOffsetAndSize(i, start, buf.WriterIndex()-start)
+		},
+		read: func(g valueReader, i int, v reflect.Value) error {
+			arr := g.Array(i)
+			if arr == nil {
+				v.Set(reflect.Zero(goType))
+				return nil
+			}
+			if err := arr.validateBounds(); err != nil {
+				return err
+			}
+			n := arr.NumElements()
+			s := reflect.MakeSlice(goType, n, n)
+			for j := 0; j < n; j++ {
+				if err := elemCodec.read(arr, j, s.Index(j)); err != nil {
+					return err
+				}
+			}
+			v.Set(s)
+			return nil
+		},
+	}, nil
+}
+
+func newMapCodec(goType reflect.Type, mapType *MapType, buf *fory.ByteBuffer) (valueCodec, error) {
+	keyCodec, err := newValueCodec(goType.Key(), mapType.Key.Type, buf)
+	if err != nil {
+		return valueCodec{}, err
+	}
+	valueCodecImpl, err := newValueCodec(goType.Elem(), mapType.Value.Type, buf)
+	if err != nil {
+		return valueCodec{}, err
+	}
+	mapWriter := NewMapWriter(buf)
+	keysWriter := NewArrayWriter(mapType.Key, buf)
+	valuesWriter := NewArrayWriter(mapType.Value, buf)
+	return valueCodec{
+		write: func(w slotWriter, i int, v reflect.Value) error {
+			if v.IsNil() {
+				w.SetNullAt(i)
+				return nil
+			}
+			n := v.Len()
+			start := buf.WriterIndex()
+			mapWriter.Reset()
+			// Go map iteration order changes between iterations, so
+			// snapshot entries once to keep keys and values aligned.
+			keys := make([]reflect.Value, 0, n)

Review Comment:
   Remove per-entry reflection allocations from map codecs
   
   The write path retains two []reflect.Value snapshots and MapIter.Key/Value can allocate per entry; the read path separately performs two reflect.New calls per entry. Write keys while taking one typed value snapshot with SetIterValue, and hoist reusable key/value scratch values outside the decode loop. Add an allocation benchmark so this hot path stays linear without 2N temporary objects.



##########
go/fory/row/encoder.go:
##########
@@ -0,0 +1,490 @@
+// 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"
+	"fmt"
+	"reflect"
+	"time"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+// Encoder converts values of struct type T to and from row bytes using
+// a codec tree compiled once with reflection at construction time. The
+// schema is inferred per InferSchema.
+//
+// An Encoder owns a reusable write buffer and is NOT goroutine-safe;
+// create one Encoder per goroutine. Decoding is safe on corrupt or
+// untrusted input.
+type Encoder[T any] struct {
+	codec *structCodec
+	hash  int64
+	buf   *fory.ByteBuffer
+}
+
+func NewEncoder[T any]() (*Encoder[T], error) {
+	t := reflect.TypeOf((*T)(nil)).Elem()
+	if t.Kind() != reflect.Struct {
+		return nil, fmt.Errorf("row: Encoder requires a struct type, got %v", t)
+	}
+	buf := fory.NewByteBuffer(nil)
+	codec, err := newStructCodec(t, buf)
+	if err != nil {
+		return nil, err
+	}
+	return &Encoder[T]{codec: codec, hash: ComputeSchemaHash(codec.schema), buf: buf}, nil
+}
+
+func (e *Encoder[T]) Schema() *Schema { return e.codec.schema }
+
+// SchemaHash returns the cross-language schema hash used by Encode and
+// Decode to detect out-of-sync struct definitions.
+func (e *Encoder[T]) SchemaHash() int64 { return e.hash }
+
+// ToRow serializes v to row bytes (no framing, just the row).
+func (e *Encoder[T]) ToRow(v *T) ([]byte, error) {
+	e.buf.SetWriterIndex(0)
+	if err := e.codec.writeStruct(reflect.ValueOf(v).Elem()); err != nil {
+		return nil, err
+	}
+	out := make([]byte, e.buf.WriterIndex())
+	copy(out, e.buf.GetData()[:e.buf.WriterIndex()])
+	return out, nil
+}
+
+// FromRow deserializes row bytes produced with this schema.
+func (e *Encoder[T]) FromRow(data []byte) (T, error) {
+	var out T
+	err := e.FromRowInto(data, &out)
+	return out, err
+}
+
+func (e *Encoder[T]) FromRowInto(data []byte, out *T) (err error) {
+	defer func() {
+		if p := recover(); p != nil {
+			err = fmt.Errorf("row: corrupt row data: %v", p)
+		}
+	}()
+	return e.codec.readStruct(NewRow(e.codec.schema, data), reflect.ValueOf(out).Elem())
+}
+
+// Encode frames the row for cross-language exchange: an int64
+// little-endian schema hash followed by the row bytes, matching the
+// Java and Python row encoders.
+func (e *Encoder[T]) Encode(v *T) ([]byte, error) {
+	rowBytes, err := e.ToRow(v)

Review Comment:
   Avoid copying the complete row twice in Encode
   
   Encode calls ToRow, which already allocates and copies the reusable buffer, then allocates the framed result and copies the row again. Reserve or prepend the hash in the encoder buffer and make only the final owned-output copy; add a benchmark for framed encoding.



##########
go/fory/row/infer.go:
##########
@@ -0,0 +1,252 @@
+// 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 (
+	"fmt"
+	"reflect"
+	"sort"
+	"strings"
+	"time"
+	"unicode"
+	"unicode/utf8"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+var (
+	goDateType     = reflect.TypeOf(fory.Date{})
+	goTimeType     = reflect.TypeOf(time.Time{})
+	goDurationType = reflect.TypeOf(time.Duration(0))
+)
+
+// InferSchema infers the row schema for a struct type or pointer to
+// struct, including every exported field not tagged `fory:"ignore"`.
+//
+// Fields are sorted by their lowerCamel name and named by its
+// snake_case form (UserName -> user_name), matching Java's schema
+// inference so both languages derive identical schemas.
+//
+// Type mapping:
+//   - bool, int8/16/32/64, float32/64: same-width row types; int maps to int64
+//   - string, []byte: string and binary, nullable
+//   - slices, maps, nested structs: list, map, and struct, nullable
+//   - *T: the row type of T, nullable
+//   - fory.Date, time.Time, time.Duration: date32, timestamp, duration
+//
+// Unsigned integers, fixed-size arrays, nested pointers, and pointer
+// map keys are unsupported and return an error.
+func InferSchema(t reflect.Type) (*Schema, error) {
+	layout, err := inferStructLayout(t, nil)
+	if err != nil {
+		return nil, err
+	}
+	return layout.schema, nil
+}
+
+// structLayout maps schema ordinals back to Go struct field indexes.
+type structLayout struct {
+	schema  *Schema
+	indexes []int // schema ordinal -> reflect struct field index
+}
+
+func inferStructLayout(t reflect.Type, path []reflect.Type) (*structLayout, error) {
+	if t.Kind() == reflect.Ptr {
+		t = t.Elem()
+	}
+	if t.Kind() != reflect.Struct || t == goDateType || t == goTimeType {
+		return nil, fmt.Errorf("row: schema inference expects a struct type, got %v", t)
+	}
+	for _, seen := range path {
+		if seen == t {
+			return nil, fmt.Errorf("row: circular reference through type %v", t)
+		}
+	}
+	path = append(path, t)
+
+	type member struct {
+		lowerCamel string
+		goIndex    int
+		fieldType  reflect.Type
+	}
+	var members []member
+	for i := 0; i < t.NumField(); i++ {
+		f := t.Field(i)
+		if f.PkgPath != "" {
+			continue
+		}
+		ignored, err := hasIgnoreTag(f.Tag.Get("fory"))
+		if err != nil {
+			return nil, fmt.Errorf("%w (field %s of %v)", err, f.Name, t)
+		}
+		if ignored {
+			continue
+		}
+		members = append(members, member{lowerFirst(f.Name), i, f.Type})
+	}
+	// Java sorts by the lowerCamel member name before converting names
+	// to snake_case; matching that order is required for schema and
+	// row-layout compatibility.
+	sort.Slice(members, func(a, b int) bool { return members[a].lowerCamel < members[b].lowerCamel })
+
+	layout := &structLayout{indexes: make([]int, 0, len(members))}
+	fields := make([]Field, 0, len(members))
+	for _, m := range members {
+		f, err := inferField(lowerCamelToLowerUnderscore(m.lowerCamel), m.fieldType, path)
+		if err != nil {
+			return nil, fmt.Errorf("%w (field %s of %v)", err, t.Field(m.goIndex).Name, t)
+		}
+		fields = append(fields, f)
+		layout.indexes = append(layout.indexes, m.goIndex)
+	}
+	layout.schema = NewSchema(fields)
+	return layout, nil
+}
+
+func inferField(name string, t reflect.Type, path []reflect.Type) (Field, error) {
+	if t.Kind() == reflect.Ptr {
+		if t.Elem().Kind() == reflect.Ptr {
+			return Field{}, fmt.Errorf("row: nested pointer type %v is unsupported", t)
+		}
+		inner, err := inferField(name, t.Elem(), path)
+		if err != nil {
+			return Field{}, err
+		}
+		inner.Nullable = true
+		return inner, nil
+	}
+	switch t {
+	case goDateType:
+		return Field{Name: name, Type: Date32Type{}}, nil
+	case goTimeType:
+		return Field{Name: name, Type: TimestampType{}}, nil
+	case goDurationType:
+		return Field{Name: name, Type: DurationType{}}, nil
+	}
+	switch t.Kind() {
+	case reflect.Bool:
+		return Field{Name: name, Type: BoolType{}}, nil
+	case reflect.Int8:
+		return Field{Name: name, Type: Int8Type{}}, nil
+	case reflect.Int16:
+		return Field{Name: name, Type: Int16Type{}}, nil
+	case reflect.Int32:
+		return Field{Name: name, Type: Int32Type{}}, nil
+	case reflect.Int64, reflect.Int:
+		// `int` maps to int64 so the width never depends on the platform.
+		return Field{Name: name, Type: Int64Type{}}, nil
+	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
+		return Field{}, fmt.Errorf("row: unsigned type %v is unsupported, use a signed type", t)
+	case reflect.Float32:
+		return Field{Name: name, Type: Float32Type{}}, nil
+	case reflect.Float64:
+		return Field{Name: name, Type: Float64Type{}}, nil
+	case reflect.String:
+		return Field{Name: name, Type: StringType{}, Nullable: true}, nil
+	case reflect.Slice:
+		if t.Elem().Kind() == reflect.Uint8 {
+			return Field{Name: name, Type: BinaryType{}, Nullable: true}, nil
+		}
+		elem, err := inferField(listItemName, t.Elem(), path)
+		if err != nil {
+			return Field{}, err
+		}
+		return Field{Name: name, Type: &ListType{Elem: elem}, Nullable: true}, nil
+	case reflect.Map:
+		if t.Key().Kind() == reflect.Ptr {
+			return Field{}, fmt.Errorf("row: pointer map key type %v is unsupported", t)
+		}
+		key, err := inferField(mapKeyName, t.Key(), path)
+		if err != nil {
+			return Field{}, err
+		}
+		key.Nullable = false
+		value, err := inferField(mapValueName, t.Elem(), path)
+		if err != nil {
+			return Field{}, err
+		}
+		return Field{Name: name, Type: &MapType{Key: key, Value: value}, Nullable: true}, nil
+	case reflect.Struct:
+		layout, err := inferStructLayout(t, path)
+		if err != nil {
+			return Field{}, err
+		}
+		return Field{Name: name, Type: &StructType{Fields: layout.schema.Fields()}, Nullable: true}, nil
+	default:
+		return Field{}, fmt.Errorf("row: type %v is unsupported in row format", t)
+	}
+}
+
+// hasIgnoreTag mirrors the ignore semantics of the core fory tag
+// parser (parseFieldTag in field_spec.go): a whole tag of "-" or an
+// ignore/ignore=true part skips the field, ignore=false keeps it, and
+// any other ignore value is an error. Other tag keys are not used by
+// the row format.
+func hasIgnoreTag(tag string) (bool, error) {
+	if tag == "-" {
+		return true, nil
+	}
+	ignore := false
+	for _, part := range strings.Split(tag, ",") {
+		part = strings.TrimSpace(part)
+		if part == "ignore" {
+			ignore = true
+		} else if strings.HasPrefix(part, "ignore=") {

Review Comment:
   Use the core fory tag grammar
   
   This parser does not actually mirror parseFieldTag: the core accepts case-insensitive true/1/yes and false/0/no, trims both sides of '=', and rejects duplicate keys. Here ignore=yes is rejected, ignore = true is ignored, and duplicates silently overwrite. Reuse the core parser or implement the same grammar and update the test that currently treats ignore=yes as invalid.



##########
go/fory/row/writer.go:
##########
@@ -0,0 +1,381 @@
+// 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"
+	"fmt"
+	"math"
+	"time"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+const maxArrayDataBytes = math.MaxInt32 - 15
+
+// RowWriter writes one row into a ByteBuffer: null bitmap, fixed 8-byte
+// field slots, then the variable data region. All slot and bitmap
+// offsets are relative to the row base, so a nested RowWriter can share
+// the parent's buffer and produce a self-contained row.
+//
+// Reset rebases the writer at the buffer's current writer index and must
+// be called before writing each row. Writers are not goroutine-safe.
+type RowWriter struct {
+	schema      *Schema
+	buf         *fory.ByteBuffer
+	base        int
+	numFields   int
+	bitmapWidth int
+	fixedSize   int
+}
+
+func NewRowWriter(schema *Schema) *RowWriter {
+	return NewRowWriterWithBuffer(schema, fory.NewByteBuffer(nil))
+}
+
+// NewRowWriterWithBuffer shares an existing buffer, e.g. to write a
+// nested struct row into its parent's variable data region.
+func NewRowWriterWithBuffer(schema *Schema, buf *fory.ByteBuffer) *RowWriter {
+	n := schema.NumFields()
+	bitmapWidth := bitmapWidthInBytes(n)
+	return &RowWriter{
+		schema:      schema,
+		buf:         buf,
+		numFields:   n,
+		bitmapWidth: bitmapWidth,
+		fixedSize:   bitmapWidth + n*8,
+	}
+}
+
+func (w *RowWriter) Schema() *Schema          { return w.schema }
+func (w *RowWriter) Buffer() *fory.ByteBuffer { return w.buf }
+
+// Reset rebases the writer at the current writer index and zeroes the
+// whole fixed region. The buffer reuses dirty capacity after shrinking,
+// so zeroing here is what makes null slots and padding deterministic;
+// it also lets narrow fixed-width writes store just the value.
+func (w *RowWriter) Reset() {
+	w.base = w.buf.WriterIndex()
+	w.buf.Reserve(w.fixedSize)
+	data := w.buf.GetData()
+	clear(data[w.base : w.base+w.fixedSize])
+	w.buf.SetWriterIndex(w.base + w.fixedSize)
+}
+
+// Size returns the bytes written for this row so far.
+func (w *RowWriter) Size() int { return w.buf.WriterIndex() - w.base }
+
+// ToBytes returns a view of the row bytes; it stays valid only until the
+// buffer is written to or reset again.
+func (w *RowWriter) ToBytes() []byte {
+	return w.buf.GetByteSlice(w.base, w.buf.WriterIndex())
+}
+
+func (w *RowWriter) slot(i int) int {
+	if uint(i) >= uint(w.numFields) {
+		panic(fmt.Sprintf("row: field index %d out of range [0, %d)", i, w.numFields))
+	}
+	return w.base + w.bitmapWidth + i*8
+}
+
+// SetNullAt marks field i null and zeroes its slot.
+func (w *RowWriter) SetNullAt(i int) {
+	slot := w.slot(i)
+	data := w.buf.GetData()
+	setBit(data[w.base:w.base+w.bitmapWidth], i)
+	binary.LittleEndian.PutUint64(data[slot:], 0)
+}
+
+func (w *RowWriter) SetNotNullAt(i int) {
+	w.slot(i) // bounds check
+	clearBit(w.buf.GetData()[w.base:w.base+w.bitmapWidth], i)
+}
+
+func (w *RowWriter) WriteBool(i int, v bool) {
+	slot := w.slot(i)
+	if v {
+		w.buf.GetData()[slot] = 1
+	} else {
+		w.buf.GetData()[slot] = 0
+	}
+}
+
+func (w *RowWriter) WriteInt8(i int, v int8) {
+	w.buf.GetData()[w.slot(i)] = byte(v)
+}
+
+func (w *RowWriter) WriteInt16(i int, v int16) {
+	binary.LittleEndian.PutUint16(w.buf.GetData()[w.slot(i):], uint16(v))
+}
+
+func (w *RowWriter) WriteInt32(i int, v int32) {
+	binary.LittleEndian.PutUint32(w.buf.GetData()[w.slot(i):], uint32(v))
+}
+
+func (w *RowWriter) WriteInt64(i int, v int64) {
+	binary.LittleEndian.PutUint64(w.buf.GetData()[w.slot(i):], uint64(v))
+}
+
+func (w *RowWriter) WriteFloat32(i int, v float32) {
+	binary.LittleEndian.PutUint32(w.buf.GetData()[w.slot(i):], math.Float32bits(v))
+}
+
+func (w *RowWriter) WriteFloat64(i int, v float64) {
+	binary.LittleEndian.PutUint64(w.buf.GetData()[w.slot(i):], math.Float64bits(v))
+}
+
+func (w *RowWriter) WriteDate(i int, d fory.Date) error {
+	days, err := fory.DateToEpochDay(d)
+	if err != nil {
+		return err
+	}
+	if days < math.MinInt32 || days > math.MaxInt32 {
+		return fmt.Errorf("row: date %v out of date32 range", d)
+	}
+	w.WriteInt32(i, int32(days))
+	return nil
+}
+
+func (w *RowWriter) WriteTimestamp(i int, t time.Time) {
+	w.WriteInt64(i, t.UnixMicro())
+}
+
+func (w *RowWriter) WriteDuration(i int, d time.Duration) {
+	w.WriteInt64(i, d.Microseconds())
+}
+
+func (w *RowWriter) WriteString(i int, s string) error {
+	start := appendStringRegion(w.buf, s)

Review Comment:
   Reject strings that are not valid UTF-8
   
   A Go string may contain arbitrary bytes, but the Row Format string type is UTF-8. Writing string([]byte{0xff}) succeeds here; Java decodes it with a replacement character, so the value changes on cross-language round-trip. Validate UTF-8 in both row and array writers and cover nested/list/map strings.



##########
go/fory/row/encoder.go:
##########
@@ -0,0 +1,490 @@
+// 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"
+	"fmt"
+	"reflect"
+	"time"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+// Encoder converts values of struct type T to and from row bytes using
+// a codec tree compiled once with reflection at construction time. The
+// schema is inferred per InferSchema.
+//
+// An Encoder owns a reusable write buffer and is NOT goroutine-safe;
+// create one Encoder per goroutine. Decoding is safe on corrupt or
+// untrusted input.
+type Encoder[T any] struct {
+	codec *structCodec
+	hash  int64
+	buf   *fory.ByteBuffer
+}
+
+func NewEncoder[T any]() (*Encoder[T], error) {
+	t := reflect.TypeOf((*T)(nil)).Elem()
+	if t.Kind() != reflect.Struct {
+		return nil, fmt.Errorf("row: Encoder requires a struct type, got %v", t)
+	}
+	buf := fory.NewByteBuffer(nil)
+	codec, err := newStructCodec(t, buf)
+	if err != nil {
+		return nil, err
+	}
+	return &Encoder[T]{codec: codec, hash: ComputeSchemaHash(codec.schema), buf: buf}, nil
+}
+
+func (e *Encoder[T]) Schema() *Schema { return e.codec.schema }
+
+// SchemaHash returns the cross-language schema hash used by Encode and
+// Decode to detect out-of-sync struct definitions.
+func (e *Encoder[T]) SchemaHash() int64 { return e.hash }
+
+// ToRow serializes v to row bytes (no framing, just the row).
+func (e *Encoder[T]) ToRow(v *T) ([]byte, error) {

Review Comment:
   Return argument errors for nil public inputs
   
   ToRow(nil) and Encode(nil) panic at reflect.Value.Elem instead of using their error result. FromRowInto with a nil output is mislabeled as corrupt data and can even succeed for an empty struct; InferSchema(nil) and SchemaToBytes(nil) have analogous behavior. Validate nil at each public entry point and return a direct argument error.



##########
go/fory/row/encoder.go:
##########
@@ -0,0 +1,490 @@
+// 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"
+	"fmt"
+	"reflect"
+	"time"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+// Encoder converts values of struct type T to and from row bytes using
+// a codec tree compiled once with reflection at construction time. The
+// schema is inferred per InferSchema.
+//
+// An Encoder owns a reusable write buffer and is NOT goroutine-safe;
+// create one Encoder per goroutine. Decoding is safe on corrupt or
+// untrusted input.
+type Encoder[T any] struct {
+	codec *structCodec
+	hash  int64
+	buf   *fory.ByteBuffer
+}
+
+func NewEncoder[T any]() (*Encoder[T], error) {
+	t := reflect.TypeOf((*T)(nil)).Elem()
+	if t.Kind() != reflect.Struct {
+		return nil, fmt.Errorf("row: Encoder requires a struct type, got %v", t)
+	}
+	buf := fory.NewByteBuffer(nil)
+	codec, err := newStructCodec(t, buf)
+	if err != nil {
+		return nil, err
+	}
+	return &Encoder[T]{codec: codec, hash: ComputeSchemaHash(codec.schema), buf: buf}, nil
+}
+
+func (e *Encoder[T]) Schema() *Schema { return e.codec.schema }
+
+// SchemaHash returns the cross-language schema hash used by Encode and

Review Comment:
   Describe SchemaHash as a type-shape fingerprint
   
   The compatible hash folds recursive type IDs only; it does not cover field names, nullability, decimal parameters, or reordering of same-typed fields. It therefore cannot generally detect out-of-sync struct definitions. Keep the wire-compatible algorithm, but narrow this documentation and the mismatch error to the actual type-shape check.



##########
go/fory/row/encoder.go:
##########
@@ -0,0 +1,490 @@
+// 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"
+	"fmt"
+	"reflect"
+	"time"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+// Encoder converts values of struct type T to and from row bytes using
+// a codec tree compiled once with reflection at construction time. The
+// schema is inferred per InferSchema.
+//
+// An Encoder owns a reusable write buffer and is NOT goroutine-safe;
+// create one Encoder per goroutine. Decoding is safe on corrupt or
+// untrusted input.
+type Encoder[T any] struct {
+	codec *structCodec
+	hash  int64
+	buf   *fory.ByteBuffer
+}
+
+func NewEncoder[T any]() (*Encoder[T], error) {

Review Comment:
   Add the Go Row Format public documentation surface
   
   This adds a complete public package, but the specification, xlang guide, README, and Go guide still list only Java/C++/Python and do not explain NewEncoder, ToRow versus Encode, trusted-data scope, nullable carriers, supported types, thread safety, or zero-copy lifetimes. Update the support matrix and add a runnable Go example before exposing the API.



##########
go/fory/row/datatype.go:
##########
@@ -0,0 +1,313 @@
+// 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 (
+	"fmt"
+	"strings"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+// DataType describes the row-format type of a field.
+//
+// Type ids are the cross-language Fory type ids shared with the Java,
+// C++, and Python row-format implementations; they appear verbatim in
+// the serialized schema wire format.
+type DataType interface {
+	TypeID() fory.TypeId
+	// ByteWidth returns the fixed storage width in bytes, or -1 for
+	// variable-width types stored through an offset+size slot.
+	ByteWidth() int
+	String() string
+}
+
+// Cross-language child field names for matching Java DataTypes.
+const (
+	listItemName = "item"
+	mapKeyName   = "key"
+	mapValueName = "value"
+)
+
+type BoolType struct{}
+
+func (BoolType) TypeID() fory.TypeId { return fory.BOOL }
+func (BoolType) ByteWidth() int      { return 1 }
+func (BoolType) String() string      { return "bool" }
+
+type Int8Type struct{}
+
+func (Int8Type) TypeID() fory.TypeId { return fory.INT8 }
+func (Int8Type) ByteWidth() int      { return 1 }
+func (Int8Type) String() string      { return "int8" }
+
+type Int16Type struct{}
+
+func (Int16Type) TypeID() fory.TypeId { return fory.INT16 }
+func (Int16Type) ByteWidth() int      { return 2 }
+func (Int16Type) String() string      { return "int16" }
+
+type Int32Type struct{}
+
+func (Int32Type) TypeID() fory.TypeId { return fory.INT32 }
+func (Int32Type) ByteWidth() int      { return 4 }
+func (Int32Type) String() string      { return "int32" }
+
+type Int64Type struct{}
+
+func (Int64Type) TypeID() fory.TypeId { return fory.INT64 }
+func (Int64Type) ByteWidth() int      { return 8 }
+func (Int64Type) String() string      { return "int64" }
+
+// Float16Type exists so schemas received from other languages parse,
+// the Go writer, reader, and encoder do not support it yet.
+type Float16Type struct{}
+
+func (Float16Type) TypeID() fory.TypeId { return fory.FLOAT16 }
+func (Float16Type) ByteWidth() int      { return 2 }
+func (Float16Type) String() string      { return "float16" }
+
+type Float32Type struct{}
+
+func (Float32Type) TypeID() fory.TypeId { return fory.FLOAT32 }
+func (Float32Type) ByteWidth() int      { return 4 }
+func (Float32Type) String() string      { return "float32" }
+
+type Float64Type struct{}
+
+func (Float64Type) TypeID() fory.TypeId { return fory.FLOAT64 }
+func (Float64Type) ByteWidth() int      { return 8 }
+func (Float64Type) String() string      { return "float64" }
+
+// StringType values are UTF-8 bytes in the variable data region,
+// stored as (offset + size) in the fixed slot region.
+type StringType struct{}
+
+func (StringType) TypeID() fory.TypeId { return fory.STRING }
+func (StringType) ByteWidth() int      { return -1 }
+func (StringType) String() string      { return "string" }
+
+type BinaryType struct{}
+
+func (BinaryType) TypeID() fory.TypeId { return fory.BINARY }
+func (BinaryType) ByteWidth() int      { return -1 }
+func (BinaryType) String() string      { return "binary" }
+
+// Date32Type values are days since the Unix epoch (int32).
+type Date32Type struct{}
+
+func (Date32Type) TypeID() fory.TypeId { return fory.DATE }
+func (Date32Type) ByteWidth() int      { return 4 }
+func (Date32Type) String() string      { return "date32" }
+
+// TimestampType values are microseconds since the Unix epoch (int64).
+type TimestampType struct{}
+
+func (TimestampType) TypeID() fory.TypeId { return fory.TIMESTAMP }
+func (TimestampType) ByteWidth() int      { return 8 }
+func (TimestampType) String() string      { return "timestamp" }
+
+// DurationType values are microseconds (int64).
+type DurationType struct{}
+
+func (DurationType) TypeID() fory.TypeId { return fory.DURATION }
+func (DurationType) ByteWidth() int      { return 8 }
+func (DurationType) String() string      { return "duration" }
+
+// DecimalType exists so schemas received from other languages parse; the
+// Go writer, reader, and encoder do not support it yet. Use it as a
+// value, not a pointer, so schema equality compares precision and scale.
+type DecimalType struct {
+	Precision int
+	Scale     int
+}
+
+func (DecimalType) TypeID() fory.TypeId { return fory.DECIMAL }
+func (DecimalType) ByteWidth() int      { return -1 }
+func (t DecimalType) String() string {
+	return fmt.Sprintf("decimal(%d, %d)", t.Precision, t.Scale)
+}
+
+// ListType is a variable-length sequence of Elem values.
+type ListType struct {
+	Elem Field
+}
+
+func (*ListType) TypeID() fory.TypeId { return fory.LIST }
+func (*ListType) ByteWidth() int      { return -1 }
+func (t *ListType) String() string    { return "list<" + t.Elem.Type.String() + ">" }
+
+// MapType stores keys and values as two adjacent arrays.
+type MapType struct {
+	Key   Field
+	Value Field
+}
+
+func (*MapType) TypeID() fory.TypeId { return fory.MAP }
+func (*MapType) ByteWidth() int      { return -1 }
+func (t *MapType) String() string {
+	return "map<" + t.Key.Type.String() + ", " + t.Value.Type.String() + ">"
+}
+
+// StructType is a nested row with its own bitmap, slots, and variable
+// data region.
+type StructType struct {
+	Fields []Field
+}
+
+func (*StructType) TypeID() fory.TypeId { return fory.STRUCT }
+func (*StructType) ByteWidth() int      { return -1 }
+func (t *StructType) String() string {
+	var sb strings.Builder
+	sb.WriteString("struct<")
+	for i, f := range t.Fields {
+		if i > 0 {
+			sb.WriteString(", ")
+		}
+		sb.WriteString(f.Name)
+		sb.WriteString(": ")
+		sb.WriteString(f.Type.String())
+	}
+	sb.WriteString(">")
+	return sb.String()
+}
+
+// Field is a named, optionally nullable slot in a schema or composite type.
+type Field struct {
+	Name     string
+	Type     DataType
+	Nullable bool
+}
+
+func NewField(name string, dataType DataType, nullable bool) Field {
+	return Field{Name: name, Type: dataType, Nullable: nullable}
+}
+
+func (f Field) Equal(o Field) bool {
+	return f.Name == o.Name && f.Nullable == o.Nullable && dataTypeEqual(f.Type, o.Type)
+}
+
+// List returns a list type with the cross-language element field name
+// "item" and nullable elements. Build ListType directly for
+// non-nullable elements.
+func List(elem DataType) *ListType {
+	return &ListType{Elem: Field{Name: listItemName, Type: elem, Nullable: true}}
+}
+
+// Map returns a map type with the cross-language field names
+// "key"/"value". Keys are always non-nullable.
+func Map(key, value DataType) *MapType {
+	return &MapType{
+		Key:   Field{Name: mapKeyName, Type: key, Nullable: false},
+		Value: Field{Name: mapValueName, Type: value, Nullable: true},
+	}
+}
+
+func Struct(fields []Field) *StructType {
+	return &StructType{Fields: fields}
+}
+
+func dataTypeEqual(a, b DataType) bool {
+	switch at := a.(type) {
+	case *ListType:
+		bt, ok := b.(*ListType)
+		return ok && at.Elem.Equal(bt.Elem)
+	case *MapType:
+		bt, ok := b.(*MapType)
+		return ok && at.Key.Equal(bt.Key) && at.Value.Equal(bt.Value)
+	case *StructType:
+		bt, ok := b.(*StructType)
+		if !ok || len(at.Fields) != len(bt.Fields) {
+			return false
+		}
+		for i := range at.Fields {
+			if !at.Fields[i].Equal(bt.Fields[i]) {
+				return false
+			}
+		}
+		return true
+	default:
+		// Primitive types are empty structs and DecimalType is a
+		// comparable value, so interface equality is exact.
+		return a == b
+	}
+}
+
+// Schema describes the fields of a top-level row.
+type Schema struct {
+	fields []Field
+	byName map[string]int
+}
+
+// NewSchema builds a schema from fields in their schema-declared order,
+// which fixes the field slot layout. For duplicate names, FieldIndex
+// resolves to the first occurrence.
+func NewSchema(fields []Field) *Schema {
+	byName := make(map[string]int, len(fields))
+	for i, f := range fields {
+		if _, ok := byName[f.Name]; !ok {

Review Comment:
   Make duplicate field-name handling cross-language deterministic
   
   Go intentionally keeps the first duplicate name, while Java Schema and StructType keep the last. Identical schema bytes can therefore resolve a name to different ordinals across runtimes. Reject duplicate names during schema construction and parsing, or align the lookup rule and test it cross-language.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org


Re: [PR] feat(go): support native fory row format [fory]

Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang merged PR #3901:
URL: https://github.com/apache/fory/pull/3901


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org


Re: [PR] feat(go): support native fory row format [fory]

Posted by "wenshao (via GitHub)" <gi...@apache.org>.
wenshao commented on code in PR #3901:
URL: https://github.com/apache/fory/pull/3901#discussion_r3696496166


##########
go/fory/row/infer.go:
##########
@@ -0,0 +1,252 @@
+// 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 (
+	"fmt"
+	"reflect"
+	"sort"
+	"strings"
+	"time"
+	"unicode"
+	"unicode/utf8"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+var (
+	goDateType     = reflect.TypeOf(fory.Date{})
+	goTimeType     = reflect.TypeOf(time.Time{})
+	goDurationType = reflect.TypeOf(time.Duration(0))
+)
+
+// InferSchema infers the row schema for a struct type or pointer to
+// struct, including every exported field not tagged `fory:"ignore"`.
+//
+// Fields are sorted by their lowerCamel name and named by its
+// snake_case form (UserName -> user_name), matching Java's schema
+// inference so both languages derive identical schemas.
+//
+// Type mapping:
+//   - bool, int8/16/32/64, float32/64: same-width row types; int maps to int64
+//   - string, []byte: string and binary, nullable
+//   - slices, maps, nested structs: list, map, and struct, nullable
+//   - *T: the row type of T, nullable
+//   - fory.Date, time.Time, time.Duration: date32, timestamp, duration
+//
+// Unsigned integers, fixed-size arrays, nested pointers, and pointer
+// map keys are unsupported and return an error.
+func InferSchema(t reflect.Type) (*Schema, error) {
+	layout, err := inferStructLayout(t, nil)
+	if err != nil {
+		return nil, err
+	}
+	return layout.schema, nil
+}
+
+// structLayout maps schema ordinals back to Go struct field indexes.
+type structLayout struct {
+	schema  *Schema
+	indexes []int // schema ordinal -> reflect struct field index
+}
+
+func inferStructLayout(t reflect.Type, path []reflect.Type) (*structLayout, error) {
+	if t.Kind() == reflect.Ptr {
+		t = t.Elem()
+	}
+	if t.Kind() != reflect.Struct || t == goDateType || t == goTimeType {
+		return nil, fmt.Errorf("row: schema inference expects a struct type, got %v", t)
+	}
+	for _, seen := range path {
+		if seen == t {
+			return nil, fmt.Errorf("row: circular reference through type %v", t)
+		}
+	}
+	path = append(path, t)
+
+	type member struct {
+		lowerCamel string
+		goIndex    int
+		fieldType  reflect.Type
+	}
+	var members []member
+	for i := 0; i < t.NumField(); i++ {
+		f := t.Field(i)
+		if f.PkgPath != "" {
+			continue
+		}
+		ignored, err := hasIgnoreTag(f.Tag.Get("fory"))
+		if err != nil {
+			return nil, fmt.Errorf("%w (field %s of %v)", err, f.Name, t)
+		}
+		if ignored {
+			continue
+		}
+		members = append(members, member{lowerFirst(f.Name), i, f.Type})
+	}
+	// Java sorts by the lowerCamel member name before converting names
+	// to snake_case; matching that order is required for schema and
+	// row-layout compatibility.
+	sort.Slice(members, func(a, b int) bool { return members[a].lowerCamel < members[b].lowerCamel })
+
+	layout := &structLayout{indexes: make([]int, 0, len(members))}
+	fields := make([]Field, 0, len(members))
+	for _, m := range members {
+		f, err := inferField(lowerCamelToLowerUnderscore(m.lowerCamel), m.fieldType, path)
+		if err != nil {
+			return nil, fmt.Errorf("%w (field %s of %v)", err, t.Field(m.goIndex).Name, t)
+		}
+		fields = append(fields, f)
+		layout.indexes = append(layout.indexes, m.goIndex)
+	}
+	layout.schema = NewSchema(fields)
+	return layout, nil
+}
+
+func inferField(name string, t reflect.Type, path []reflect.Type) (Field, error) {
+	if t.Kind() == reflect.Ptr {
+		if t.Elem().Kind() == reflect.Ptr {
+			return Field{}, fmt.Errorf("row: nested pointer type %v is unsupported", t)
+		}
+		inner, err := inferField(name, t.Elem(), path)
+		if err != nil {
+			return Field{}, err
+		}
+		inner.Nullable = true
+		return inner, nil
+	}
+	switch t {
+	case goDateType:
+		return Field{Name: name, Type: Date32Type{}}, nil
+	case goTimeType:
+		return Field{Name: name, Type: TimestampType{}}, nil
+	case goDurationType:
+		return Field{Name: name, Type: DurationType{}}, nil
+	}
+	switch t.Kind() {
+	case reflect.Bool:
+		return Field{Name: name, Type: BoolType{}}, nil
+	case reflect.Int8:
+		return Field{Name: name, Type: Int8Type{}}, nil
+	case reflect.Int16:
+		return Field{Name: name, Type: Int16Type{}}, nil
+	case reflect.Int32:
+		return Field{Name: name, Type: Int32Type{}}, nil
+	case reflect.Int64, reflect.Int:
+		// `int` maps to int64 so the width never depends on the platform.
+		return Field{Name: name, Type: Int64Type{}}, nil
+	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
+		return Field{}, fmt.Errorf("row: unsigned type %v is unsupported, use a signed type", t)
+	case reflect.Float32:
+		return Field{Name: name, Type: Float32Type{}}, nil
+	case reflect.Float64:
+		return Field{Name: name, Type: Float64Type{}}, nil
+	case reflect.String:
+		return Field{Name: name, Type: StringType{}, Nullable: true}, nil
+	case reflect.Slice:
+		if t.Elem().Kind() == reflect.Uint8 {
+			return Field{Name: name, Type: BinaryType{}, Nullable: true}, nil
+		}
+		elem, err := inferField(listItemName, t.Elem(), path)
+		if err != nil {
+			return Field{}, err
+		}
+		return Field{Name: name, Type: &ListType{Elem: elem}, Nullable: true}, nil
+	case reflect.Map:
+		if t.Key().Kind() == reflect.Ptr {
+			return Field{}, fmt.Errorf("row: pointer map key type %v is unsupported", t)
+		}
+		key, err := inferField(mapKeyName, t.Key(), path)
+		if err != nil {
+			return Field{}, err
+		}
+		key.Nullable = false
+		value, err := inferField(mapValueName, t.Elem(), path)
+		if err != nil {
+			return Field{}, err
+		}
+		return Field{Name: name, Type: &MapType{Key: key, Value: value}, Nullable: true}, nil
+	case reflect.Struct:
+		layout, err := inferStructLayout(t, path)
+		if err != nil {
+			return Field{}, err
+		}
+		return Field{Name: name, Type: &StructType{Fields: layout.schema.Fields()}, Nullable: true}, nil
+	default:
+		return Field{}, fmt.Errorf("row: type %v is unsupported in row format", t)
+	}
+}
+
+// hasIgnoreTag mirrors the ignore semantics of the core fory tag
+// parser (parseFieldTag in field_spec.go): a whole tag of "-" or an
+// ignore/ignore=true part skips the field, ignore=false keeps it, and
+// any other ignore value is an error. Other tag keys are not used by
+// the row format.
+func hasIgnoreTag(tag string) (bool, error) {
+	if tag == "-" {
+		return true, nil
+	}
+	ignore := false
+	for _, part := range strings.Split(tag, ",") {
+		part = strings.TrimSpace(part)
+		if part == "ignore" {
+			ignore = true
+		} else if strings.HasPrefix(part, "ignore=") {
+			switch strings.TrimPrefix(part, "ignore=") {
+			case "true":

Review Comment:
   **[Suggestion]** `hasIgnoreTag` accepts only literal `"true"`/`"false"` while the core tag parser `parseFieldTag` delegates to `parseBoolStrict`, which accepts `true/1/yes/false/0/no` case-insensitively with trimming. The doc comment claims this function mirrors the core parser. — Failure scenario: a struct tagged `fory:"ignore=1"` serializes correctly through the core fory serializer, but `InferSchema` returns an error for the same tag.
   
   ```suggestion
   		} else if strings.HasPrefix(part, "ignore=") {
   			switch strings.ToLower(strings.TrimSpace(strings.TrimPrefix(part, "ignore="))) {
   			case "true", "1", "yes":
   ```
   
   _— Qwen3-235B-A22B via Qwen Code /review_



##########
go/fory/row/row.go:
##########
@@ -0,0 +1,370 @@
+// 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"
+	"fmt"
+	"math"
+	"time"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+// Row is a zero-copy reader over one row's bytes. Getters read directly
+// from the underlying data without deserializing other fields; nested
+// values are sub-slice views, never copies (except String).
+//
+// Fixed-width getters return the zero value for null fields; use
+// IsNullAt to distinguish. Variable-width getters return nil (or "")
+// for null fields. An out-of-range field index panics; offsets in
+// corrupt or untrusted data may panic on slice bounds, so wrap
+// untrusted decoding with recover.
+type Row struct {
+	fields      []Field
+	data        []byte
+	bitmapWidth int
+}
+
+func NewRow(schema *Schema, data []byte) *Row {
+	return newRow(schema.Fields(), data)
+}
+
+func newRow(fields []Field, data []byte) *Row {
+	return &Row{fields: fields, data: data, bitmapWidth: bitmapWidthInBytes(len(fields))}
+}
+
+func (r *Row) NumFields() int { return len(r.fields) }
+func (r *Row) SizeBytes() int { return len(r.data) }
+
+// Data returns the underlying row bytes; callers must not modify them.
+func (r *Row) Data() []byte { return r.data }
+
+func (r *Row) IsNullAt(i int) bool {
+	if uint(i) >= uint(len(r.fields)) {
+		panic(fmt.Sprintf("row: field index %d out of range [0, %d)", i, len(r.fields)))
+	}
+	return getBit(r.data, i)
+}
+
+func (r *Row) slot(i int) int { return r.bitmapWidth + i*8 }
+
+func (r *Row) Bool(i int) bool {
+	return !r.IsNullAt(i) && r.data[r.slot(i)] != 0
+}
+
+func (r *Row) Int8(i int) int8 {
+	if r.IsNullAt(i) {
+		return 0
+	}
+	return int8(r.data[r.slot(i)])
+}
+
+func (r *Row) Int16(i int) int16 {
+	if r.IsNullAt(i) {
+		return 0
+	}
+	return int16(binary.LittleEndian.Uint16(r.data[r.slot(i):]))
+}
+
+func (r *Row) Int32(i int) int32 {
+	if r.IsNullAt(i) {
+		return 0
+	}
+	return int32(binary.LittleEndian.Uint32(r.data[r.slot(i):]))
+}
+
+func (r *Row) Int64(i int) int64 {
+	if r.IsNullAt(i) {
+		return 0
+	}
+	return int64(binary.LittleEndian.Uint64(r.data[r.slot(i):]))
+}
+
+func (r *Row) Float32(i int) float32 {
+	if r.IsNullAt(i) {
+		return 0
+	}
+	return math.Float32frombits(binary.LittleEndian.Uint32(r.data[r.slot(i):]))
+}
+
+func (r *Row) Float64(i int) float64 {
+	if r.IsNullAt(i) {
+		return 0
+	}
+	return math.Float64frombits(binary.LittleEndian.Uint64(r.data[r.slot(i):]))
+}
+
+func (r *Row) Date(i int) fory.Date {
+	if r.IsNullAt(i) {
+		return fory.Date{}
+	}
+	return dateFromDays(int32(binary.LittleEndian.Uint32(r.data[r.slot(i):])))
+}
+
+func (r *Row) Timestamp(i int) time.Time {
+	if r.IsNullAt(i) {
+		return time.Time{}
+	}
+	return time.UnixMicro(r.Int64(i))
+}
+
+func (r *Row) Duration(i int) time.Duration {
+	return time.Duration(r.Int64(i)) * time.Microsecond
+}
+
+// varData returns the value bytes of variable-width field i, or nil if
+// the field is null. An empty value returns an empty non-nil slice.
+func (r *Row) varData(i int) []byte {
+	if r.IsNullAt(i) {
+		return nil
+	}
+	offset, size := decodeOffsetAndSize(binary.LittleEndian.Uint64(r.data[r.slot(i):]))
+	return boundedSlice(r.data, offset, size)
+}
+
+// Binary returns a zero-copy view of field i's bytes.
+func (r *Row) Binary(i int) []byte { return r.varData(i) }
+
+func (r *Row) String(i int) string { return string(r.varData(i)) }
+
+func (r *Row) Struct(i int) *Row {
+	data := r.varData(i)
+	if data == nil {
+		return nil
+	}
+	structType := r.fields[i].Type.(*StructType)
+	return newRow(structType.Fields, data)
+}
+
+func (r *Row) Array(i int) *ArrayData {
+	data := r.varData(i)
+	if data == nil {
+		return nil
+	}
+	listType := r.fields[i].Type.(*ListType)
+	return NewArrayData(listType.Elem, data)
+}
+
+func (r *Row) Map(i int) *MapData {
+	data := r.varData(i)
+	if data == nil {
+		return nil
+	}
+	return NewMapData(r.fields[i].Type.(*MapType), data)
+}
+
+// ArrayData is a zero-copy reader over one array's bytes, with the same
+// null and bounds semantics as Row.
+type ArrayData struct {
+	elem        Field
+	data        []byte
+	numElements int
+	elemSize    int
+	headerBytes int
+}
+
+func NewArrayData(elem Field, data []byte) *ArrayData {
+	numElements := int(binary.LittleEndian.Uint64(data))
+	elemSize := elem.Type.ByteWidth()
+	if elemSize < 0 {
+		elemSize = 8
+	}
+	return &ArrayData{
+		elem:        elem,
+		data:        data,
+		numElements: numElements,
+		elemSize:    elemSize,
+		headerBytes: 8 + bitmapWidthInBytes(numElements),
+	}
+}
+
+func (a *ArrayData) NumElements() int { return a.numElements }
+func (a *ArrayData) SizeBytes() int   { return len(a.data) }
+
+// validateBounds rejects arrays whose declared element count is not
+// covered by the available bytes, so decoders can check before
+// allocating from an attacker-declared count.
+func (a *ArrayData) validateBounds() error {
+	// The coarse count check involves no arithmetic, so it cannot be
+	// defeated by overflow: every element occupies at least one data
+	// byte. It also bounds numElements low enough that headerBytes
+	// (computed in NewArrayData) and the product below are exact.
+	if a.numElements < 0 || a.numElements > len(a.data) {
+		return fmt.Errorf("row: array declares %d elements but holds only %d bytes", a.numElements, len(a.data))
+	}
+	need := int64(a.headerBytes) + int64(a.numElements)*int64(a.elemSize)
+	if need > int64(len(a.data)) {
+		return fmt.Errorf("row: array declares %d elements needing %d bytes but holds only %d", a.numElements, need, len(a.data))
+	}
+	return nil
+}
+
+func (a *ArrayData) IsNullAt(i int) bool {
+	if uint(i) >= uint(a.numElements) {
+		panic(fmt.Sprintf("row: array index %d out of range [0, %d)", i, a.numElements))
+	}
+	return getBit(a.data[8:a.headerBytes], i)
+}
+
+func (a *ArrayData) slot(i int) int { return a.headerBytes + i*a.elemSize }
+
+func (a *ArrayData) Bool(i int) bool {
+	return !a.IsNullAt(i) && a.data[a.slot(i)] != 0
+}
+
+func (a *ArrayData) Int8(i int) int8 {
+	if a.IsNullAt(i) {
+		return 0
+	}
+	return int8(a.data[a.slot(i)])
+}
+
+func (a *ArrayData) Int16(i int) int16 {
+	if a.IsNullAt(i) {
+		return 0
+	}
+	return int16(binary.LittleEndian.Uint16(a.data[a.slot(i):]))
+}
+
+func (a *ArrayData) Int32(i int) int32 {
+	if a.IsNullAt(i) {
+		return 0
+	}
+	return int32(binary.LittleEndian.Uint32(a.data[a.slot(i):]))
+}
+
+func (a *ArrayData) Int64(i int) int64 {
+	if a.IsNullAt(i) {
+		return 0
+	}
+	return int64(binary.LittleEndian.Uint64(a.data[a.slot(i):]))
+}
+
+func (a *ArrayData) Float32(i int) float32 {
+	if a.IsNullAt(i) {
+		return 0
+	}
+	return math.Float32frombits(binary.LittleEndian.Uint32(a.data[a.slot(i):]))
+}
+
+func (a *ArrayData) Float64(i int) float64 {
+	if a.IsNullAt(i) {
+		return 0
+	}
+	return math.Float64frombits(binary.LittleEndian.Uint64(a.data[a.slot(i):]))
+}
+
+func (a *ArrayData) Date(i int) fory.Date {
+	if a.IsNullAt(i) {
+		return fory.Date{}
+	}
+	return dateFromDays(int32(binary.LittleEndian.Uint32(a.data[a.slot(i):])))
+}
+
+func (a *ArrayData) Timestamp(i int) time.Time {
+	if a.IsNullAt(i) {
+		return time.Time{}
+	}
+	return time.UnixMicro(a.Int64(i))
+}
+
+func (a *ArrayData) Duration(i int) time.Duration {
+	return time.Duration(a.Int64(i)) * time.Microsecond
+}
+
+func (a *ArrayData) varData(i int) []byte {
+	if a.IsNullAt(i) {
+		return nil
+	}
+	offset, size := decodeOffsetAndSize(binary.LittleEndian.Uint64(a.data[a.slot(i):]))
+	return boundedSlice(a.data, offset, size)
+}
+
+// Binary returns a zero-copy view of element i's bytes.
+func (a *ArrayData) Binary(i int) []byte { return a.varData(i) }
+
+func (a *ArrayData) String(i int) string { return string(a.varData(i)) }
+
+func (a *ArrayData) Struct(i int) *Row {
+	data := a.varData(i)
+	if data == nil {
+		return nil
+	}
+	return newRow(a.elem.Type.(*StructType).Fields, data)
+}
+
+func (a *ArrayData) Array(i int) *ArrayData {
+	data := a.varData(i)
+	if data == nil {
+		return nil
+	}
+	return NewArrayData(a.elem.Type.(*ListType).Elem, data)
+}
+
+func (a *ArrayData) Map(i int) *MapData {
+	data := a.varData(i)
+	if data == nil {
+		return nil
+	}
+	return NewMapData(a.elem.Type.(*MapType), data)
+}
+
+// MapData is a zero-copy reader over one map's bytes: the keys array
+// and values array views share the map's underlying data.
+type MapData struct {
+	keys   *ArrayData
+	values *ArrayData
+}
+
+func NewMapData(mapType *MapType, data []byte) *MapData {
+	keysSize := int(binary.LittleEndian.Uint64(data))
+	keysData := boundedSlice(data, 8, keysSize)
+	valuesData := boundedSlice(data, 8+keysSize, len(data)-8-keysSize)
+	return &MapData{
+		keys:   NewArrayData(mapType.Key, keysData),
+		values: NewArrayData(mapType.Value, valuesData),
+	}
+}
+
+// boundedSlice checks declared bounds against the data LENGTH
+// and caps the view so nested
+// readers cannot reach outside it either. Out-of-bounds panics are
+// converted to errors by the encoder's decode entry points.
+func boundedSlice(data []byte, offset, size int) []byte {
+	end := offset + size
+	if offset < 0 || size < 0 || end > len(data) {

Review Comment:
   **[Suggestion]** `boundedSlice`'s bounds check is defeated by signed integer overflow: `end := offset + size` wraps negative when `size` is near `MaxInt64` (reachable via `NewMapData`'s attacker-controlled uint64→int cast). All three guards pass on the negative `end`, and the function reaches a generic runtime panic instead of the intended descriptive one. Go's runtime prevents memory-safety issues, but the defensive boundary is silently bypassed. — Failure scenario: a crafted `keysSize = MaxInt64` makes `end = 8 + MaxInt64` overflow to negative; `data[8:negative]` triggers an uninformative panic.
   
   ```suggestion
   	end := offset + size
   	if offset < 0 || size < 0 || end < offset || end > len(data) {
   ```
   
   _— Qwen3-235B-A22B via Qwen Code /review_



##########
go/fory/row/infer.go:
##########
@@ -0,0 +1,252 @@
+// 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 (
+	"fmt"
+	"reflect"
+	"sort"
+	"strings"
+	"time"
+	"unicode"
+	"unicode/utf8"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+var (
+	goDateType     = reflect.TypeOf(fory.Date{})
+	goTimeType     = reflect.TypeOf(time.Time{})
+	goDurationType = reflect.TypeOf(time.Duration(0))
+)
+
+// InferSchema infers the row schema for a struct type or pointer to
+// struct, including every exported field not tagged `fory:"ignore"`.
+//
+// Fields are sorted by their lowerCamel name and named by its
+// snake_case form (UserName -> user_name), matching Java's schema
+// inference so both languages derive identical schemas.
+//
+// Type mapping:
+//   - bool, int8/16/32/64, float32/64: same-width row types; int maps to int64
+//   - string, []byte: string and binary, nullable
+//   - slices, maps, nested structs: list, map, and struct, nullable
+//   - *T: the row type of T, nullable
+//   - fory.Date, time.Time, time.Duration: date32, timestamp, duration
+//
+// Unsigned integers, fixed-size arrays, nested pointers, and pointer
+// map keys are unsupported and return an error.
+func InferSchema(t reflect.Type) (*Schema, error) {
+	layout, err := inferStructLayout(t, nil)
+	if err != nil {
+		return nil, err
+	}
+	return layout.schema, nil
+}
+
+// structLayout maps schema ordinals back to Go struct field indexes.
+type structLayout struct {
+	schema  *Schema
+	indexes []int // schema ordinal -> reflect struct field index
+}
+
+func inferStructLayout(t reflect.Type, path []reflect.Type) (*structLayout, error) {
+	if t.Kind() == reflect.Ptr {
+		t = t.Elem()
+	}
+	if t.Kind() != reflect.Struct || t == goDateType || t == goTimeType {
+		return nil, fmt.Errorf("row: schema inference expects a struct type, got %v", t)
+	}
+	for _, seen := range path {
+		if seen == t {
+			return nil, fmt.Errorf("row: circular reference through type %v", t)
+		}
+	}
+	path = append(path, t)
+
+	type member struct {
+		lowerCamel string
+		goIndex    int
+		fieldType  reflect.Type
+	}
+	var members []member
+	for i := 0; i < t.NumField(); i++ {
+		f := t.Field(i)
+		if f.PkgPath != "" {
+			continue
+		}
+		ignored, err := hasIgnoreTag(f.Tag.Get("fory"))
+		if err != nil {
+			return nil, fmt.Errorf("%w (field %s of %v)", err, f.Name, t)
+		}
+		if ignored {
+			continue
+		}
+		members = append(members, member{lowerFirst(f.Name), i, f.Type})
+	}
+	// Java sorts by the lowerCamel member name before converting names
+	// to snake_case; matching that order is required for schema and
+	// row-layout compatibility.
+	sort.Slice(members, func(a, b int) bool { return members[a].lowerCamel < members[b].lowerCamel })
+
+	layout := &structLayout{indexes: make([]int, 0, len(members))}
+	fields := make([]Field, 0, len(members))
+	for _, m := range members {
+		f, err := inferField(lowerCamelToLowerUnderscore(m.lowerCamel), m.fieldType, path)
+		if err != nil {
+			return nil, fmt.Errorf("%w (field %s of %v)", err, t.Field(m.goIndex).Name, t)
+		}
+		fields = append(fields, f)
+		layout.indexes = append(layout.indexes, m.goIndex)
+	}
+	layout.schema = NewSchema(fields)
+	return layout, nil
+}
+
+func inferField(name string, t reflect.Type, path []reflect.Type) (Field, error) {
+	if t.Kind() == reflect.Ptr {
+		if t.Elem().Kind() == reflect.Ptr {
+			return Field{}, fmt.Errorf("row: nested pointer type %v is unsupported", t)
+		}
+		inner, err := inferField(name, t.Elem(), path)
+		if err != nil {
+			return Field{}, err
+		}
+		inner.Nullable = true
+		return inner, nil
+	}
+	switch t {
+	case goDateType:
+		return Field{Name: name, Type: Date32Type{}}, nil
+	case goTimeType:
+		return Field{Name: name, Type: TimestampType{}}, nil
+	case goDurationType:
+		return Field{Name: name, Type: DurationType{}}, nil
+	}
+	switch t.Kind() {
+	case reflect.Bool:
+		return Field{Name: name, Type: BoolType{}}, nil
+	case reflect.Int8:
+		return Field{Name: name, Type: Int8Type{}}, nil
+	case reflect.Int16:
+		return Field{Name: name, Type: Int16Type{}}, nil
+	case reflect.Int32:
+		return Field{Name: name, Type: Int32Type{}}, nil
+	case reflect.Int64, reflect.Int:
+		// `int` maps to int64 so the width never depends on the platform.
+		return Field{Name: name, Type: Int64Type{}}, nil
+	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
+		return Field{}, fmt.Errorf("row: unsigned type %v is unsupported, use a signed type", t)
+	case reflect.Float32:
+		return Field{Name: name, Type: Float32Type{}}, nil
+	case reflect.Float64:
+		return Field{Name: name, Type: Float64Type{}}, nil
+	case reflect.String:
+		return Field{Name: name, Type: StringType{}, Nullable: true}, nil
+	case reflect.Slice:
+		if t.Elem().Kind() == reflect.Uint8 {
+			return Field{Name: name, Type: BinaryType{}, Nullable: true}, nil
+		}
+		elem, err := inferField(listItemName, t.Elem(), path)
+		if err != nil {
+			return Field{}, err
+		}
+		return Field{Name: name, Type: &ListType{Elem: elem}, Nullable: true}, nil

Review Comment:
   **[Suggestion]** `inferField` leaves the list-element `Nullable` flag at its zero value `false` for value-type elements (e.g. `[]int32`), diverging from Java's `TypeInference` (which produces `Nullable: true` for `List<Integer>`) and Go's own `List()`/`Map()` factories (which always set `Nullable: true`). The nullable flag is encoded as bit 6 of the field header in `SchemaToBytes`, so schema bytes differ. The cross-language test fixture uses only pointer elements (`[]*string`), masking this divergence. — Failure scenario: adding a `[]int32` field to the xlang fixture would fail both `parsed.Equal(encoder.Schema())` and `bytes.Equal(reencoded, schemaBytes)`.
   
   ```suggestion
   		elem, err := inferField(listItemName, t.Elem(), path)
   		if err != nil {
   			return Field{}, err
   		}
   		elem.Nullable = true
   		return Field{Name: name, Type: &ListType{Elem: elem}, Nullable: true}, nil
   ```
   
   _— Qwen3-235B-A22B via Qwen Code /review_



##########
go/fory/row/encoder.go:
##########
@@ -0,0 +1,490 @@
+// 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"
+	"fmt"
+	"reflect"
+	"time"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+// Encoder converts values of struct type T to and from row bytes using
+// a codec tree compiled once with reflection at construction time. The
+// schema is inferred per InferSchema.
+//
+// An Encoder owns a reusable write buffer and is NOT goroutine-safe;
+// create one Encoder per goroutine. Decoding is safe on corrupt or
+// untrusted input.
+type Encoder[T any] struct {
+	codec *structCodec
+	hash  int64
+	buf   *fory.ByteBuffer
+}
+
+func NewEncoder[T any]() (*Encoder[T], error) {
+	t := reflect.TypeOf((*T)(nil)).Elem()
+	if t.Kind() != reflect.Struct {
+		return nil, fmt.Errorf("row: Encoder requires a struct type, got %v", t)
+	}
+	buf := fory.NewByteBuffer(nil)
+	codec, err := newStructCodec(t, buf)
+	if err != nil {
+		return nil, err
+	}
+	return &Encoder[T]{codec: codec, hash: ComputeSchemaHash(codec.schema), buf: buf}, nil
+}
+
+func (e *Encoder[T]) Schema() *Schema { return e.codec.schema }
+
+// SchemaHash returns the cross-language schema hash used by Encode and
+// Decode to detect out-of-sync struct definitions.
+func (e *Encoder[T]) SchemaHash() int64 { return e.hash }
+
+// ToRow serializes v to row bytes (no framing, just the row).
+func (e *Encoder[T]) ToRow(v *T) ([]byte, error) {
+	e.buf.SetWriterIndex(0)
+	if err := e.codec.writeStruct(reflect.ValueOf(v).Elem()); err != nil {
+		return nil, err
+	}
+	out := make([]byte, e.buf.WriterIndex())
+	copy(out, e.buf.GetData()[:e.buf.WriterIndex()])
+	return out, nil
+}
+
+// FromRow deserializes row bytes produced with this schema.
+func (e *Encoder[T]) FromRow(data []byte) (T, error) {
+	var out T
+	err := e.FromRowInto(data, &out)
+	return out, err
+}
+
+func (e *Encoder[T]) FromRowInto(data []byte, out *T) (err error) {
+	defer func() {
+		if p := recover(); p != nil {
+			err = fmt.Errorf("row: corrupt row data: %v", p)
+		}
+	}()
+	return e.codec.readStruct(NewRow(e.codec.schema, data), reflect.ValueOf(out).Elem())
+}
+
+// Encode frames the row for cross-language exchange: an int64
+// little-endian schema hash followed by the row bytes, matching the
+// Java and Python row encoders.
+func (e *Encoder[T]) Encode(v *T) ([]byte, error) {
+	rowBytes, err := e.ToRow(v)
+	if err != nil {
+		return nil, err
+	}
+	out := make([]byte, 8+len(rowBytes))
+	binary.LittleEndian.PutUint64(out, uint64(e.hash))
+	copy(out[8:], rowBytes)
+	return out, nil
+}
+
+// Decode verifies the schema hash and deserializes the row.
+func (e *Encoder[T]) Decode(data []byte) (T, error) {
+	var out T
+	if len(data) < 8 {
+		return out, fmt.Errorf("row: encoded data of %d bytes is too short for the schema hash", len(data))
+	}
+	hash := int64(binary.LittleEndian.Uint64(data))
+	if hash != e.hash {
+		return out, fmt.Errorf("row: schema hash mismatch: data has %d, encoder expects %d; writer and reader struct definitions are out of sync", hash, e.hash)
+	}
+	err := e.FromRowInto(data[8:], &out)
+	return out, err
+}
+
+// slotWriter is the shared write surface of RowWriter and ArrayWriter,
+// letting one value codec serve struct fields and array elements.
+type slotWriter interface {
+	SetNullAt(i int)
+	WriteBool(i int, v bool)
+	WriteInt8(i int, v int8)
+	WriteInt16(i int, v int16)
+	WriteInt32(i int, v int32)
+	WriteInt64(i int, v int64)
+	WriteFloat32(i int, v float32)
+	WriteFloat64(i int, v float64)
+	WriteDate(i int, d fory.Date) error
+	WriteTimestamp(i int, t time.Time)
+	WriteDuration(i int, d time.Duration)
+	WriteString(i int, s string) error
+	WriteBytes(i int, b []byte) error
+	SetOffsetAndSize(i, absStart, size int) error
+}
+
+// valueReader is the shared read surface of Row and ArrayData.
+type valueReader interface {
+	IsNullAt(i int) bool
+	Bool(i int) bool
+	Int8(i int) int8
+	Int16(i int) int16
+	Int32(i int) int32
+	Int64(i int) int64
+	Float32(i int) float32
+	Float64(i int) float64
+	Date(i int) fory.Date
+	Timestamp(i int) time.Time
+	Duration(i int) time.Duration
+	String(i int) string
+	Binary(i int) []byte
+	Struct(i int) *Row
+	Array(i int) *ArrayData
+	Map(i int) *MapData
+}
+
+// valueCodec reads or writes one value at slot/element i. Write
+// functions handle nil for nilable Go types; read functions overwrite
+// the target even when the field is null, so reused targets stay clean.
+type valueCodec struct {
+	write func(w slotWriter, i int, v reflect.Value) error
+	read  func(g valueReader, i int, v reflect.Value) error
+}
+
+// structCodec writes and reads one struct level; nested structs hold
+// their own child codec with a child RowWriter sharing the buffer.
+type structCodec struct {
+	schema *Schema
+	writer *RowWriter
+	fields []fieldCodec
+}
+
+type fieldCodec struct {
+	goIndex int
+	codec   valueCodec
+}
+
+func newStructCodec(t reflect.Type, buf *fory.ByteBuffer) (*structCodec, error) {
+	layout, err := inferStructLayout(t, nil)
+	if err != nil {
+		return nil, err
+	}
+	sc := &structCodec{
+		schema: layout.schema,
+		writer: NewRowWriterWithBuffer(layout.schema, buf),
+		fields: make([]fieldCodec, 0, layout.schema.NumFields()),
+	}
+	for ordinal, goIndex := range layout.indexes {
+		codec, err := newValueCodec(t.Field(goIndex).Type, layout.schema.Field(ordinal).Type, buf)
+		if err != nil {
+			return nil, err
+		}
+		sc.fields = append(sc.fields, fieldCodec{goIndex: goIndex, codec: codec})
+	}
+	return sc, nil
+}
+
+func (sc *structCodec) writeStruct(v reflect.Value) error {
+	sc.writer.Reset()
+	for ordinal := range sc.fields {
+		fc := &sc.fields[ordinal]
+		if err := fc.codec.write(sc.writer, ordinal, v.Field(fc.goIndex)); err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
+func (sc *structCodec) readStruct(r *Row, v reflect.Value) error {
+	for ordinal := range sc.fields {
+		fc := &sc.fields[ordinal]
+		if err := fc.codec.read(r, ordinal, v.Field(fc.goIndex)); err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
+func newValueCodec(goType reflect.Type, dataType DataType, buf *fory.ByteBuffer) (valueCodec, error) {
+	if goType.Kind() == reflect.Ptr {
+		elemType := goType.Elem()
+		inner, err := newValueCodec(elemType, dataType, buf)
+		if err != nil {
+			return valueCodec{}, err
+		}
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error {
+				if v.IsNil() {
+					w.SetNullAt(i)
+					return nil
+				}
+				return inner.write(w, i, v.Elem())
+			},
+			read: func(g valueReader, i int, v reflect.Value) error {
+				if g.IsNullAt(i) {
+					v.Set(reflect.Zero(goType))
+					return nil
+				}
+				p := reflect.New(elemType)
+				if err := inner.read(g, i, p.Elem()); err != nil {
+					return err
+				}
+				v.Set(p)
+				return nil
+			},
+		}, nil
+	}
+
+	switch dt := dataType.(type) {
+	case BoolType:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error { w.WriteBool(i, v.Bool()); return nil },
+			read:  func(g valueReader, i int, v reflect.Value) error { v.SetBool(g.Bool(i)); return nil },
+		}, nil
+	case Int8Type:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error { w.WriteInt8(i, int8(v.Int())); return nil },
+			read:  func(g valueReader, i int, v reflect.Value) error { v.SetInt(int64(g.Int8(i))); return nil },
+		}, nil
+	case Int16Type:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error { w.WriteInt16(i, int16(v.Int())); return nil },
+			read:  func(g valueReader, i int, v reflect.Value) error { v.SetInt(int64(g.Int16(i))); return nil },
+		}, nil
+	case Int32Type:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error { w.WriteInt32(i, int32(v.Int())); return nil },
+			read:  func(g valueReader, i int, v reflect.Value) error { v.SetInt(int64(g.Int32(i))); return nil },
+		}, nil
+	case Int64Type:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error { w.WriteInt64(i, v.Int()); return nil },
+			read:  func(g valueReader, i int, v reflect.Value) error { v.SetInt(g.Int64(i)); return nil },
+		}, nil
+	case Float32Type:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error { w.WriteFloat32(i, float32(v.Float())); return nil },
+			read:  func(g valueReader, i int, v reflect.Value) error { v.SetFloat(float64(g.Float32(i))); return nil },
+		}, nil
+	case Float64Type:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error { w.WriteFloat64(i, v.Float()); return nil },
+			read:  func(g valueReader, i int, v reflect.Value) error { v.SetFloat(g.Float64(i)); return nil },
+		}, nil
+	case Date32Type:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error { return w.WriteDate(i, v.Interface().(fory.Date)) },
+			read:  func(g valueReader, i int, v reflect.Value) error { v.Set(reflect.ValueOf(g.Date(i))); return nil },
+		}, nil
+	case TimestampType:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error {
+				w.WriteTimestamp(i, v.Interface().(time.Time))
+				return nil
+			},
+			read: func(g valueReader, i int, v reflect.Value) error { v.Set(reflect.ValueOf(g.Timestamp(i))); return nil },
+		}, nil
+	case DurationType:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error {
+				w.WriteDuration(i, time.Duration(v.Int()))
+				return nil
+			},
+			read: func(g valueReader, i int, v reflect.Value) error { v.SetInt(int64(g.Duration(i))); return nil },
+		}, nil
+	case StringType:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error { return w.WriteString(i, v.String()) },
+			read:  func(g valueReader, i int, v reflect.Value) error { v.SetString(g.String(i)); return nil },
+		}, nil
+	case BinaryType:
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error {
+				if v.IsNil() {
+					w.SetNullAt(i)
+					return nil
+				}
+				return w.WriteBytes(i, v.Bytes())
+			},
+			read: func(g valueReader, i int, v reflect.Value) error {
+				b := g.Binary(i)
+				if b == nil {
+					v.SetBytes(nil)
+					return nil
+				}
+				// Copy so the decoded struct never aliases the input.
+				owned := make([]byte, len(b))
+				copy(owned, b)
+				v.SetBytes(owned)
+				return nil
+			},
+		}, nil
+	case *ListType:
+		return newListCodec(goType, dt, buf)
+	case *MapType:
+		return newMapCodec(goType, dt, buf)
+	case *StructType:
+		child, err := newStructCodec(goType, buf)
+		if err != nil {
+			return valueCodec{}, err
+		}
+		return valueCodec{
+			write: func(w slotWriter, i int, v reflect.Value) error {
+				start := child.writer.buf.WriterIndex()
+				if err := child.writeStruct(v); err != nil {
+					return err
+				}
+				return w.SetOffsetAndSize(i, start, child.writer.buf.WriterIndex()-start)
+			},
+			read: func(g valueReader, i int, v reflect.Value) error {
+				nested := g.Struct(i)
+				if nested == nil {
+					v.Set(reflect.Zero(goType))
+					return nil
+				}
+				return child.readStruct(nested, v)
+			},
+		}, nil
+	default:
+		return valueCodec{}, fmt.Errorf("row: data type %s is not supported by the encoder", dataType)
+	}
+}
+
+func newListCodec(goType reflect.Type, listType *ListType, buf *fory.ByteBuffer) (valueCodec, error) {
+	elemCodec, err := newValueCodec(goType.Elem(), listType.Elem.Type, buf)
+	if err != nil {
+		return valueCodec{}, err
+	}
+	writer := NewArrayWriter(listType.Elem, buf)
+	return valueCodec{
+		write: func(w slotWriter, i int, v reflect.Value) error {
+			if v.IsNil() {
+				w.SetNullAt(i)
+				return nil
+			}
+			n := v.Len()
+			start := buf.WriterIndex()
+			if err := writer.Reset(n); err != nil {
+				return err
+			}
+			for j := 0; j < n; j++ {
+				if err := elemCodec.write(writer, j, v.Index(j)); err != nil {
+					return err
+				}
+			}
+			return w.SetOffsetAndSize(i, start, buf.WriterIndex()-start)
+		},
+		read: func(g valueReader, i int, v reflect.Value) error {
+			arr := g.Array(i)
+			if arr == nil {
+				v.Set(reflect.Zero(goType))
+				return nil
+			}
+			if err := arr.validateBounds(); err != nil {
+				return err
+			}
+			n := arr.NumElements()
+			s := reflect.MakeSlice(goType, n, n)
+			for j := 0; j < n; j++ {
+				if err := elemCodec.read(arr, j, s.Index(j)); err != nil {
+					return err
+				}
+			}
+			v.Set(s)
+			return nil
+		},
+	}, nil
+}
+
+func newMapCodec(goType reflect.Type, mapType *MapType, buf *fory.ByteBuffer) (valueCodec, error) {
+	keyCodec, err := newValueCodec(goType.Key(), mapType.Key.Type, buf)
+	if err != nil {
+		return valueCodec{}, err
+	}
+	valueCodecImpl, err := newValueCodec(goType.Elem(), mapType.Value.Type, buf)
+	if err != nil {
+		return valueCodec{}, err
+	}
+	mapWriter := NewMapWriter(buf)
+	keysWriter := NewArrayWriter(mapType.Key, buf)
+	valuesWriter := NewArrayWriter(mapType.Value, buf)
+	return valueCodec{
+		write: func(w slotWriter, i int, v reflect.Value) error {
+			if v.IsNil() {
+				w.SetNullAt(i)
+				return nil
+			}
+			n := v.Len()
+			start := buf.WriterIndex()
+			mapWriter.Reset()
+			// Go map iteration order changes between iterations, so
+			// snapshot entries once to keep keys and values aligned.
+			keys := make([]reflect.Value, 0, n)
+			values := make([]reflect.Value, 0, n)
+			iter := v.MapRange()
+			for iter.Next() {
+				keys = append(keys, iter.Key())
+				values = append(values, iter.Value())
+			}
+			if err := keysWriter.Reset(len(keys)); err != nil {
+				return err
+			}
+			for j, k := range keys {
+				if err := keyCodec.write(keysWriter, j, k); err != nil {
+					return err
+				}
+			}
+			mapWriter.FinishKeys()
+			if err := valuesWriter.Reset(len(values)); err != nil {
+				return err
+			}
+			for j, val := range values {
+				if err := valueCodecImpl.write(valuesWriter, j, val); err != nil {
+					return err
+				}
+			}
+			return w.SetOffsetAndSize(i, start, buf.WriterIndex()-start)
+		},
+		read: func(g valueReader, i int, v reflect.Value) error {
+			md := g.Map(i)
+			if md == nil {
+				v.Set(reflect.Zero(goType))
+				return nil
+			}
+			keys, values := md.Keys(), md.Values()
+			if err := keys.validateBounds(); err != nil {
+				return err
+			}
+			if err := values.validateBounds(); err != nil {
+				return err
+			}
+			n := keys.NumElements()
+			if values.NumElements() != n {
+				return fmt.Errorf("row: map has %d keys but %d values", n, values.NumElements())
+			}
+			m := reflect.MakeMapWithSize(goType, n)
+			for j := 0; j < n; j++ {
+				key := reflect.New(goType.Key()).Elem()
+				if err := keyCodec.read(keys, j, key); err != nil {
+					return err
+				}
+				value := reflect.New(goType.Elem()).Elem()

Review Comment:
   **[Suggestion]** `newMapCodec`'s read loop allocates two heap objects per map entry via `reflect.New(...).Elem()` inside the loop. Since `SetMapIndex` copies the key and value, and the read functions fully overwrite their targets (per the `valueCodec` doc contract), both allocations can be hoisted before the loop. — Concrete cost: deserializing a map with N entries performs 2N short-lived heap allocations that immediately become garbage, adding GC pressure proportional to map size.
   
   ```suggestion
   			key := reflect.New(goType.Key()).Elem()
   			value := reflect.New(goType.Elem()).Elem()
   			for j := 0; j < n; j++ {
   				if err := keyCodec.read(keys, j, key); err != nil {
   					return err
   				}
   ```
   
   _— Qwen3-235B-A22B via Qwen Code /review_



##########
go/fory/row/writer.go:
##########
@@ -0,0 +1,381 @@
+// 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"
+	"fmt"
+	"math"
+	"time"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+const maxArrayDataBytes = math.MaxInt32 - 15
+
+// RowWriter writes one row into a ByteBuffer: null bitmap, fixed 8-byte
+// field slots, then the variable data region. All slot and bitmap
+// offsets are relative to the row base, so a nested RowWriter can share
+// the parent's buffer and produce a self-contained row.
+//
+// Reset rebases the writer at the buffer's current writer index and must
+// be called before writing each row. Writers are not goroutine-safe.
+type RowWriter struct {
+	schema      *Schema
+	buf         *fory.ByteBuffer
+	base        int
+	numFields   int
+	bitmapWidth int
+	fixedSize   int
+}
+
+func NewRowWriter(schema *Schema) *RowWriter {
+	return NewRowWriterWithBuffer(schema, fory.NewByteBuffer(nil))
+}
+
+// NewRowWriterWithBuffer shares an existing buffer, e.g. to write a
+// nested struct row into its parent's variable data region.
+func NewRowWriterWithBuffer(schema *Schema, buf *fory.ByteBuffer) *RowWriter {
+	n := schema.NumFields()
+	bitmapWidth := bitmapWidthInBytes(n)
+	return &RowWriter{
+		schema:      schema,
+		buf:         buf,
+		numFields:   n,
+		bitmapWidth: bitmapWidth,
+		fixedSize:   bitmapWidth + n*8,
+	}
+}
+
+func (w *RowWriter) Schema() *Schema          { return w.schema }
+func (w *RowWriter) Buffer() *fory.ByteBuffer { return w.buf }
+
+// Reset rebases the writer at the current writer index and zeroes the
+// whole fixed region. The buffer reuses dirty capacity after shrinking,
+// so zeroing here is what makes null slots and padding deterministic;
+// it also lets narrow fixed-width writes store just the value.
+func (w *RowWriter) Reset() {
+	w.base = w.buf.WriterIndex()
+	w.buf.Reserve(w.fixedSize)
+	data := w.buf.GetData()
+	clear(data[w.base : w.base+w.fixedSize])
+	w.buf.SetWriterIndex(w.base + w.fixedSize)
+}
+
+// Size returns the bytes written for this row so far.
+func (w *RowWriter) Size() int { return w.buf.WriterIndex() - w.base }
+
+// ToBytes returns a view of the row bytes; it stays valid only until the
+// buffer is written to or reset again.
+func (w *RowWriter) ToBytes() []byte {
+	return w.buf.GetByteSlice(w.base, w.buf.WriterIndex())
+}
+
+func (w *RowWriter) slot(i int) int {
+	if uint(i) >= uint(w.numFields) {
+		panic(fmt.Sprintf("row: field index %d out of range [0, %d)", i, w.numFields))
+	}
+	return w.base + w.bitmapWidth + i*8
+}
+
+// SetNullAt marks field i null and zeroes its slot.
+func (w *RowWriter) SetNullAt(i int) {
+	slot := w.slot(i)
+	data := w.buf.GetData()
+	setBit(data[w.base:w.base+w.bitmapWidth], i)
+	binary.LittleEndian.PutUint64(data[slot:], 0)
+}
+
+func (w *RowWriter) SetNotNullAt(i int) {
+	w.slot(i) // bounds check
+	clearBit(w.buf.GetData()[w.base:w.base+w.bitmapWidth], i)
+}
+
+func (w *RowWriter) WriteBool(i int, v bool) {
+	slot := w.slot(i)
+	if v {
+		w.buf.GetData()[slot] = 1
+	} else {
+		w.buf.GetData()[slot] = 0
+	}
+}
+
+func (w *RowWriter) WriteInt8(i int, v int8) {
+	w.buf.GetData()[w.slot(i)] = byte(v)
+}
+
+func (w *RowWriter) WriteInt16(i int, v int16) {
+	binary.LittleEndian.PutUint16(w.buf.GetData()[w.slot(i):], uint16(v))
+}
+
+func (w *RowWriter) WriteInt32(i int, v int32) {
+	binary.LittleEndian.PutUint32(w.buf.GetData()[w.slot(i):], uint32(v))
+}
+
+func (w *RowWriter) WriteInt64(i int, v int64) {
+	binary.LittleEndian.PutUint64(w.buf.GetData()[w.slot(i):], uint64(v))
+}
+
+func (w *RowWriter) WriteFloat32(i int, v float32) {
+	binary.LittleEndian.PutUint32(w.buf.GetData()[w.slot(i):], math.Float32bits(v))
+}
+
+func (w *RowWriter) WriteFloat64(i int, v float64) {
+	binary.LittleEndian.PutUint64(w.buf.GetData()[w.slot(i):], math.Float64bits(v))
+}
+
+func (w *RowWriter) WriteDate(i int, d fory.Date) error {
+	days, err := fory.DateToEpochDay(d)
+	if err != nil {
+		return err
+	}
+	if days < math.MinInt32 || days > math.MaxInt32 {
+		return fmt.Errorf("row: date %v out of date32 range", d)
+	}
+	w.WriteInt32(i, int32(days))
+	return nil
+}
+
+func (w *RowWriter) WriteTimestamp(i int, t time.Time) {
+	w.WriteInt64(i, t.UnixMicro())
+}
+
+func (w *RowWriter) WriteDuration(i int, d time.Duration) {
+	w.WriteInt64(i, d.Microseconds())
+}
+
+func (w *RowWriter) WriteString(i int, s string) error {
+	start := appendStringRegion(w.buf, s)
+	return w.SetOffsetAndSize(i, start, len(s))
+}
+
+func (w *RowWriter) WriteBytes(i int, b []byte) error {
+	start := appendBytesRegion(w.buf, b)
+	return w.SetOffsetAndSize(i, start, len(b))
+}
+
+// SetOffsetAndSize patches field i's slot with the row-relative offset
+// and byte size of a value already appended to the variable data region.
+// Use it after writing a nested struct, array, or map at absStart.
+// Offsets and sizes beyond 32 bits are rejected: the wire format packs
+// both into one slot, and truncating would corrupt the row silently.
+func (w *RowWriter) SetOffsetAndSize(i, absStart, size int) error {
+	rel := absStart - w.base
+	if uint64(rel) > math.MaxUint32 || uint64(size) > math.MaxUint32 {
+		return fmt.Errorf("row: value at offset %d with %d bytes exceeds the 32-bit wire format limit", rel, size)
+	}
+	binary.LittleEndian.PutUint64(w.buf.GetData()[w.slot(i):], uint64(rel)<<32|uint64(size))
+	return nil
+}
+
+// ArrayWriter writes one array: an 8-byte element count, a null bitmap,
+// then elements at their natural width (variable-width elements use
+// 8-byte offset+size slots). Offsets are relative to the array base.
+type ArrayWriter struct {
+	elem        Field
+	buf         *fory.ByteBuffer
+	base        int
+	elemSize    int
+	numElements int
+	headerBytes int
+}
+
+func NewArrayWriter(elem Field, buf *fory.ByteBuffer) *ArrayWriter {
+	elemSize := elem.Type.ByteWidth()
+	if elemSize < 0 {
+		elemSize = 8
+	}
+	return &ArrayWriter{elem: elem, buf: buf, elemSize: elemSize}
+}
+
+func (w *ArrayWriter) Buffer() *fory.ByteBuffer { return w.buf }
+
+// Reset rebases the writer at the current writer index and writes the
+// array header plus a zeroed element region for numElements elements.
+func (w *ArrayWriter) Reset(numElements int) error {
+	if numElements < 0 {
+		return fmt.Errorf("row: negative array length %d", numElements)
+	}
+	dataBytes := int64(numElements) * int64(w.elemSize)
+	if dataBytes > maxArrayDataBytes {
+		return fmt.Errorf("row: array of %d elements exceeds maximum size", numElements)
+	}

Review Comment:
   **[Suggestion]** The int64 multiplication `int64(numElements) * int64(w.elemSize)` overflows for extreme `numElements`, wrapping `dataBytes` negative and bypassing the `> maxArrayDataBytes` guard. The trigger is physically unallocatable, but the explicit guard is provably defeated. — Failure scenario: `numElements > MaxInt64/8` wraps `dataBytes` negative, producing an obscure runtime panic instead of the intended clean error.
   
   ```suggestion
   	if int64(numElements) > maxArrayDataBytes/int64(w.elemSize) {
   		return fmt.Errorf("row: array of %d elements exceeds maximum size", numElements)
   	}
   	dataBytes := int64(numElements) * int64(w.elemSize)
   ```
   
   _— Qwen3-235B-A22B via Qwen Code /review_



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org


Re: [PR] feat(go): support native fory row format [fory]

Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on code in PR #3901:
URL: https://github.com/apache/fory/pull/3901#discussion_r3704757339


##########
go/fory/row/row.go:
##########
@@ -0,0 +1,370 @@
+// 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"
+	"fmt"
+	"math"
+	"time"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+// Row is a zero-copy reader over one row's bytes. Getters read directly
+// from the underlying data without deserializing other fields; nested
+// values are sub-slice views, never copies (except String).
+//
+// Fixed-width getters return the zero value for null fields; use
+// IsNullAt to distinguish. Variable-width getters return nil (or "")
+// for null fields. An out-of-range field index panics; offsets in
+// corrupt or untrusted data may panic on slice bounds, so wrap
+// untrusted decoding with recover.
+type Row struct {
+	fields      []Field
+	data        []byte
+	bitmapWidth int
+}
+
+func NewRow(schema *Schema, data []byte) *Row {
+	return newRow(schema.Fields(), data)
+}
+
+func newRow(fields []Field, data []byte) *Row {
+	return &Row{fields: fields, data: data, bitmapWidth: bitmapWidthInBytes(len(fields))}
+}
+
+func (r *Row) NumFields() int { return len(r.fields) }
+func (r *Row) SizeBytes() int { return len(r.data) }
+
+// Data returns the underlying row bytes; callers must not modify them.
+func (r *Row) Data() []byte { return r.data }
+
+func (r *Row) IsNullAt(i int) bool {
+	if uint(i) >= uint(len(r.fields)) {
+		panic(fmt.Sprintf("row: field index %d out of range [0, %d)", i, len(r.fields)))
+	}
+	return getBit(r.data, i)
+}
+
+func (r *Row) slot(i int) int { return r.bitmapWidth + i*8 }
+
+func (r *Row) Bool(i int) bool {
+	return !r.IsNullAt(i) && r.data[r.slot(i)] != 0
+}
+
+func (r *Row) Int8(i int) int8 {
+	if r.IsNullAt(i) {
+		return 0
+	}
+	return int8(r.data[r.slot(i)])
+}
+
+func (r *Row) Int16(i int) int16 {
+	if r.IsNullAt(i) {
+		return 0
+	}
+	return int16(binary.LittleEndian.Uint16(r.data[r.slot(i):]))
+}
+
+func (r *Row) Int32(i int) int32 {
+	if r.IsNullAt(i) {
+		return 0
+	}
+	return int32(binary.LittleEndian.Uint32(r.data[r.slot(i):]))
+}
+
+func (r *Row) Int64(i int) int64 {
+	if r.IsNullAt(i) {
+		return 0
+	}
+	return int64(binary.LittleEndian.Uint64(r.data[r.slot(i):]))
+}
+
+func (r *Row) Float32(i int) float32 {
+	if r.IsNullAt(i) {
+		return 0
+	}
+	return math.Float32frombits(binary.LittleEndian.Uint32(r.data[r.slot(i):]))
+}
+
+func (r *Row) Float64(i int) float64 {
+	if r.IsNullAt(i) {
+		return 0
+	}
+	return math.Float64frombits(binary.LittleEndian.Uint64(r.data[r.slot(i):]))
+}
+
+func (r *Row) Date(i int) fory.Date {
+	if r.IsNullAt(i) {
+		return fory.Date{}
+	}
+	return dateFromDays(int32(binary.LittleEndian.Uint32(r.data[r.slot(i):])))
+}
+
+func (r *Row) Timestamp(i int) time.Time {
+	if r.IsNullAt(i) {
+		return time.Time{}
+	}
+	return time.UnixMicro(r.Int64(i))
+}
+
+func (r *Row) Duration(i int) time.Duration {
+	return time.Duration(r.Int64(i)) * time.Microsecond

Review Comment:
   Check the duration carrier range before multiplying
   
   The wire stores an int64 number of microseconds, but time.Duration stores int64 nanoseconds. Multiplication silently wraps for otherwise valid wire values; MaxInt64 microseconds becomes negative. Reject values outside MinInt64/1000 through MaxInt64/1000 before conversion, including the ArrayData path.



##########
go/fory/row/writer.go:
##########
@@ -0,0 +1,381 @@
+// 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"
+	"fmt"
+	"math"
+	"time"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+const maxArrayDataBytes = math.MaxInt32 - 15
+
+// RowWriter writes one row into a ByteBuffer: null bitmap, fixed 8-byte
+// field slots, then the variable data region. All slot and bitmap
+// offsets are relative to the row base, so a nested RowWriter can share
+// the parent's buffer and produce a self-contained row.
+//
+// Reset rebases the writer at the buffer's current writer index and must
+// be called before writing each row. Writers are not goroutine-safe.
+type RowWriter struct {
+	schema      *Schema
+	buf         *fory.ByteBuffer
+	base        int
+	numFields   int
+	bitmapWidth int
+	fixedSize   int
+}
+
+func NewRowWriter(schema *Schema) *RowWriter {
+	return NewRowWriterWithBuffer(schema, fory.NewByteBuffer(nil))
+}
+
+// NewRowWriterWithBuffer shares an existing buffer, e.g. to write a
+// nested struct row into its parent's variable data region.
+func NewRowWriterWithBuffer(schema *Schema, buf *fory.ByteBuffer) *RowWriter {
+	n := schema.NumFields()
+	bitmapWidth := bitmapWidthInBytes(n)
+	return &RowWriter{
+		schema:      schema,
+		buf:         buf,
+		numFields:   n,
+		bitmapWidth: bitmapWidth,
+		fixedSize:   bitmapWidth + n*8,
+	}
+}
+
+func (w *RowWriter) Schema() *Schema          { return w.schema }
+func (w *RowWriter) Buffer() *fory.ByteBuffer { return w.buf }
+
+// Reset rebases the writer at the current writer index and zeroes the
+// whole fixed region. The buffer reuses dirty capacity after shrinking,
+// so zeroing here is what makes null slots and padding deterministic;
+// it also lets narrow fixed-width writes store just the value.
+func (w *RowWriter) Reset() {
+	w.base = w.buf.WriterIndex()
+	w.buf.Reserve(w.fixedSize)
+	data := w.buf.GetData()
+	clear(data[w.base : w.base+w.fixedSize])
+	w.buf.SetWriterIndex(w.base + w.fixedSize)
+}
+
+// Size returns the bytes written for this row so far.
+func (w *RowWriter) Size() int { return w.buf.WriterIndex() - w.base }
+
+// ToBytes returns a view of the row bytes; it stays valid only until the
+// buffer is written to or reset again.
+func (w *RowWriter) ToBytes() []byte {
+	return w.buf.GetByteSlice(w.base, w.buf.WriterIndex())
+}
+
+func (w *RowWriter) slot(i int) int {
+	if uint(i) >= uint(w.numFields) {
+		panic(fmt.Sprintf("row: field index %d out of range [0, %d)", i, w.numFields))
+	}
+	return w.base + w.bitmapWidth + i*8
+}
+
+// SetNullAt marks field i null and zeroes its slot.
+func (w *RowWriter) SetNullAt(i int) {
+	slot := w.slot(i)
+	data := w.buf.GetData()
+	setBit(data[w.base:w.base+w.bitmapWidth], i)
+	binary.LittleEndian.PutUint64(data[slot:], 0)
+}
+
+func (w *RowWriter) SetNotNullAt(i int) {
+	w.slot(i) // bounds check
+	clearBit(w.buf.GetData()[w.base:w.base+w.bitmapWidth], i)
+}
+
+func (w *RowWriter) WriteBool(i int, v bool) {
+	slot := w.slot(i)
+	if v {
+		w.buf.GetData()[slot] = 1
+	} else {
+		w.buf.GetData()[slot] = 0
+	}
+}
+
+func (w *RowWriter) WriteInt8(i int, v int8) {
+	w.buf.GetData()[w.slot(i)] = byte(v)
+}
+
+func (w *RowWriter) WriteInt16(i int, v int16) {
+	binary.LittleEndian.PutUint16(w.buf.GetData()[w.slot(i):], uint16(v))
+}
+
+func (w *RowWriter) WriteInt32(i int, v int32) {
+	binary.LittleEndian.PutUint32(w.buf.GetData()[w.slot(i):], uint32(v))
+}
+
+func (w *RowWriter) WriteInt64(i int, v int64) {
+	binary.LittleEndian.PutUint64(w.buf.GetData()[w.slot(i):], uint64(v))
+}
+
+func (w *RowWriter) WriteFloat32(i int, v float32) {
+	binary.LittleEndian.PutUint32(w.buf.GetData()[w.slot(i):], math.Float32bits(v))
+}
+
+func (w *RowWriter) WriteFloat64(i int, v float64) {
+	binary.LittleEndian.PutUint64(w.buf.GetData()[w.slot(i):], math.Float64bits(v))
+}
+
+func (w *RowWriter) WriteDate(i int, d fory.Date) error {
+	days, err := fory.DateToEpochDay(d)
+	if err != nil {
+		return err
+	}
+	if days < math.MinInt32 || days > math.MaxInt32 {
+		return fmt.Errorf("row: date %v out of date32 range", d)
+	}
+	w.WriteInt32(i, int32(days))
+	return nil
+}
+
+func (w *RowWriter) WriteTimestamp(i int, t time.Time) {

Review Comment:
   Use a checked timestamp-to-microseconds conversion
   
   time.Time.UnixMicro has undefined results when the timestamp cannot be represented as int64 microseconds. This writer currently succeeds with a corrupted value for sufficiently large years. Make timestamp writes return an error and use checked seconds/nanoseconds arithmetic in both RowWriter and ArrayWriter.



##########
go/fory/row/infer.go:
##########
@@ -0,0 +1,252 @@
+// 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 (
+	"fmt"
+	"reflect"
+	"sort"
+	"strings"
+	"time"
+	"unicode"
+	"unicode/utf8"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+var (
+	goDateType     = reflect.TypeOf(fory.Date{})
+	goTimeType     = reflect.TypeOf(time.Time{})
+	goDurationType = reflect.TypeOf(time.Duration(0))
+)
+
+// InferSchema infers the row schema for a struct type or pointer to
+// struct, including every exported field not tagged `fory:"ignore"`.
+//
+// Fields are sorted by their lowerCamel name and named by its
+// snake_case form (UserName -> user_name), matching Java's schema
+// inference so both languages derive identical schemas.
+//
+// Type mapping:
+//   - bool, int8/16/32/64, float32/64: same-width row types; int maps to int64
+//   - string, []byte: string and binary, nullable
+//   - slices, maps, nested structs: list, map, and struct, nullable
+//   - *T: the row type of T, nullable
+//   - fory.Date, time.Time, time.Duration: date32, timestamp, duration
+//
+// Unsigned integers, fixed-size arrays, nested pointers, and pointer
+// map keys are unsupported and return an error.
+func InferSchema(t reflect.Type) (*Schema, error) {
+	layout, err := inferStructLayout(t, nil)
+	if err != nil {
+		return nil, err
+	}
+	return layout.schema, nil
+}
+
+// structLayout maps schema ordinals back to Go struct field indexes.
+type structLayout struct {
+	schema  *Schema
+	indexes []int // schema ordinal -> reflect struct field index
+}
+
+func inferStructLayout(t reflect.Type, path []reflect.Type) (*structLayout, error) {
+	if t.Kind() == reflect.Ptr {
+		t = t.Elem()
+	}
+	if t.Kind() != reflect.Struct || t == goDateType || t == goTimeType {
+		return nil, fmt.Errorf("row: schema inference expects a struct type, got %v", t)
+	}
+	for _, seen := range path {
+		if seen == t {
+			return nil, fmt.Errorf("row: circular reference through type %v", t)
+		}
+	}
+	path = append(path, t)
+
+	type member struct {
+		lowerCamel string
+		goIndex    int
+		fieldType  reflect.Type
+	}
+	var members []member
+	for i := 0; i < t.NumField(); i++ {
+		f := t.Field(i)
+		if f.PkgPath != "" {
+			continue
+		}
+		ignored, err := hasIgnoreTag(f.Tag.Get("fory"))
+		if err != nil {
+			return nil, fmt.Errorf("%w (field %s of %v)", err, f.Name, t)
+		}
+		if ignored {
+			continue
+		}
+		members = append(members, member{lowerFirst(f.Name), i, f.Type})
+	}
+	// Java sorts by the lowerCamel member name before converting names
+	// to snake_case; matching that order is required for schema and
+	// row-layout compatibility.
+	sort.Slice(members, func(a, b int) bool { return members[a].lowerCamel < members[b].lowerCamel })
+
+	layout := &structLayout{indexes: make([]int, 0, len(members))}
+	fields := make([]Field, 0, len(members))
+	for _, m := range members {
+		f, err := inferField(lowerCamelToLowerUnderscore(m.lowerCamel), m.fieldType, path)
+		if err != nil {
+			return nil, fmt.Errorf("%w (field %s of %v)", err, t.Field(m.goIndex).Name, t)
+		}
+		fields = append(fields, f)
+		layout.indexes = append(layout.indexes, m.goIndex)
+	}
+	layout.schema = NewSchema(fields)
+	return layout, nil
+}
+
+func inferField(name string, t reflect.Type, path []reflect.Type) (Field, error) {
+	if t.Kind() == reflect.Ptr {
+		if t.Elem().Kind() == reflect.Ptr {
+			return Field{}, fmt.Errorf("row: nested pointer type %v is unsupported", t)
+		}
+		inner, err := inferField(name, t.Elem(), path)
+		if err != nil {
+			return Field{}, err
+		}
+		inner.Nullable = true
+		return inner, nil
+	}
+	switch t {
+	case goDateType:
+		return Field{Name: name, Type: Date32Type{}}, nil

Review Comment:
   Align temporal field nullability with Java
   
   Go infers Date and Timestamp value carriers as non-nullable, whereas Java infers LocalDate, Timestamp, and Instant fields as nullable. Their schema bytes therefore differ, while the type-only hash does not detect the difference; a Java null then becomes a zero Date or year-1 Time in Go. Align the inferred field metadata and require pointer carriers when null must be preserved.



##########
go/fory/row/writer.go:
##########
@@ -0,0 +1,381 @@
+// 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"
+	"fmt"
+	"math"
+	"time"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+const maxArrayDataBytes = math.MaxInt32 - 15
+
+// RowWriter writes one row into a ByteBuffer: null bitmap, fixed 8-byte
+// field slots, then the variable data region. All slot and bitmap
+// offsets are relative to the row base, so a nested RowWriter can share
+// the parent's buffer and produce a self-contained row.
+//
+// Reset rebases the writer at the buffer's current writer index and must
+// be called before writing each row. Writers are not goroutine-safe.
+type RowWriter struct {
+	schema      *Schema
+	buf         *fory.ByteBuffer
+	base        int
+	numFields   int
+	bitmapWidth int
+	fixedSize   int
+}
+
+func NewRowWriter(schema *Schema) *RowWriter {
+	return NewRowWriterWithBuffer(schema, fory.NewByteBuffer(nil))
+}
+
+// NewRowWriterWithBuffer shares an existing buffer, e.g. to write a
+// nested struct row into its parent's variable data region.
+func NewRowWriterWithBuffer(schema *Schema, buf *fory.ByteBuffer) *RowWriter {
+	n := schema.NumFields()
+	bitmapWidth := bitmapWidthInBytes(n)
+	return &RowWriter{
+		schema:      schema,
+		buf:         buf,
+		numFields:   n,
+		bitmapWidth: bitmapWidth,
+		fixedSize:   bitmapWidth + n*8,
+	}
+}
+
+func (w *RowWriter) Schema() *Schema          { return w.schema }
+func (w *RowWriter) Buffer() *fory.ByteBuffer { return w.buf }
+
+// Reset rebases the writer at the current writer index and zeroes the
+// whole fixed region. The buffer reuses dirty capacity after shrinking,
+// so zeroing here is what makes null slots and padding deterministic;
+// it also lets narrow fixed-width writes store just the value.
+func (w *RowWriter) Reset() {
+	w.base = w.buf.WriterIndex()
+	w.buf.Reserve(w.fixedSize)
+	data := w.buf.GetData()
+	clear(data[w.base : w.base+w.fixedSize])
+	w.buf.SetWriterIndex(w.base + w.fixedSize)
+}
+
+// Size returns the bytes written for this row so far.
+func (w *RowWriter) Size() int { return w.buf.WriterIndex() - w.base }
+
+// ToBytes returns a view of the row bytes; it stays valid only until the
+// buffer is written to or reset again.
+func (w *RowWriter) ToBytes() []byte {
+	return w.buf.GetByteSlice(w.base, w.buf.WriterIndex())
+}
+
+func (w *RowWriter) slot(i int) int {
+	if uint(i) >= uint(w.numFields) {
+		panic(fmt.Sprintf("row: field index %d out of range [0, %d)", i, w.numFields))
+	}
+	return w.base + w.bitmapWidth + i*8
+}
+
+// SetNullAt marks field i null and zeroes its slot.
+func (w *RowWriter) SetNullAt(i int) {
+	slot := w.slot(i)
+	data := w.buf.GetData()
+	setBit(data[w.base:w.base+w.bitmapWidth], i)
+	binary.LittleEndian.PutUint64(data[slot:], 0)
+}
+
+func (w *RowWriter) SetNotNullAt(i int) {
+	w.slot(i) // bounds check
+	clearBit(w.buf.GetData()[w.base:w.base+w.bitmapWidth], i)
+}
+
+func (w *RowWriter) WriteBool(i int, v bool) {
+	slot := w.slot(i)
+	if v {
+		w.buf.GetData()[slot] = 1
+	} else {
+		w.buf.GetData()[slot] = 0
+	}
+}
+
+func (w *RowWriter) WriteInt8(i int, v int8) {
+	w.buf.GetData()[w.slot(i)] = byte(v)
+}
+
+func (w *RowWriter) WriteInt16(i int, v int16) {
+	binary.LittleEndian.PutUint16(w.buf.GetData()[w.slot(i):], uint16(v))
+}
+
+func (w *RowWriter) WriteInt32(i int, v int32) {
+	binary.LittleEndian.PutUint32(w.buf.GetData()[w.slot(i):], uint32(v))
+}
+
+func (w *RowWriter) WriteInt64(i int, v int64) {
+	binary.LittleEndian.PutUint64(w.buf.GetData()[w.slot(i):], uint64(v))
+}
+
+func (w *RowWriter) WriteFloat32(i int, v float32) {
+	binary.LittleEndian.PutUint32(w.buf.GetData()[w.slot(i):], math.Float32bits(v))
+}
+
+func (w *RowWriter) WriteFloat64(i int, v float64) {
+	binary.LittleEndian.PutUint64(w.buf.GetData()[w.slot(i):], math.Float64bits(v))
+}
+
+func (w *RowWriter) WriteDate(i int, d fory.Date) error {
+	days, err := fory.DateToEpochDay(d)
+	if err != nil {
+		return err
+	}
+	if days < math.MinInt32 || days > math.MaxInt32 {
+		return fmt.Errorf("row: date %v out of date32 range", d)
+	}
+	w.WriteInt32(i, int32(days))
+	return nil
+}
+
+func (w *RowWriter) WriteTimestamp(i int, t time.Time) {
+	w.WriteInt64(i, t.UnixMicro())
+}
+
+func (w *RowWriter) WriteDuration(i int, d time.Duration) {
+	w.WriteInt64(i, d.Microseconds())
+}
+
+func (w *RowWriter) WriteString(i int, s string) error {
+	start := appendStringRegion(w.buf, s)
+	return w.SetOffsetAndSize(i, start, len(s))
+}
+
+func (w *RowWriter) WriteBytes(i int, b []byte) error {
+	start := appendBytesRegion(w.buf, b)
+	return w.SetOffsetAndSize(i, start, len(b))
+}
+
+// SetOffsetAndSize patches field i's slot with the row-relative offset
+// and byte size of a value already appended to the variable data region.
+// Use it after writing a nested struct, array, or map at absStart.
+// Offsets and sizes beyond 32 bits are rejected: the wire format packs
+// both into one slot, and truncating would corrupt the row silently.
+func (w *RowWriter) SetOffsetAndSize(i, absStart, size int) error {
+	rel := absStart - w.base
+	if uint64(rel) > math.MaxUint32 || uint64(size) > math.MaxUint32 {
+		return fmt.Errorf("row: value at offset %d with %d bytes exceeds the 32-bit wire format limit", rel, size)
+	}
+	binary.LittleEndian.PutUint64(w.buf.GetData()[w.slot(i):], uint64(rel)<<32|uint64(size))
+	return nil
+}
+
+// ArrayWriter writes one array: an 8-byte element count, a null bitmap,
+// then elements at their natural width (variable-width elements use
+// 8-byte offset+size slots). Offsets are relative to the array base.
+type ArrayWriter struct {
+	elem        Field
+	buf         *fory.ByteBuffer
+	base        int
+	elemSize    int
+	numElements int
+	headerBytes int
+}
+
+func NewArrayWriter(elem Field, buf *fory.ByteBuffer) *ArrayWriter {
+	elemSize := elem.Type.ByteWidth()
+	if elemSize < 0 {
+		elemSize = 8
+	}
+	return &ArrayWriter{elem: elem, buf: buf, elemSize: elemSize}
+}
+
+func (w *ArrayWriter) Buffer() *fory.ByteBuffer { return w.buf }
+
+// Reset rebases the writer at the current writer index and writes the
+// array header plus a zeroed element region for numElements elements.
+func (w *ArrayWriter) Reset(numElements int) error {
+	if numElements < 0 {
+		return fmt.Errorf("row: negative array length %d", numElements)
+	}
+	dataBytes := int64(numElements) * int64(w.elemSize)

Review Comment:
   Check the array limit before multiplying
   
   With an 8-byte element, Reset(math.MaxInt) overflows dataBytes to a negative value, bypasses the maximum-size check, and later panics while computing or reserving the region. Compare numElements against maxArrayDataBytes/elemSize before multiplying and test that extreme input returns an error.



##########
go/fory/row/schema_bytes.go:
##########
@@ -0,0 +1,351 @@
+// 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 (
+	"fmt"
+	"math"
+
+	fory "github.com/apache/fory/go/fory"
+	"github.com/apache/fory/go/fory/meta"
+)
+
+// Schema wire format, byte-compatible with Java SchemaEncoder:
+//
+//	| version (1 byte) | num_fields (var uint32 small7) | fields... |
+//
+// Field: | header (1 byte) | name size (varint, only if large) | name
+// bytes | type info |. Header bits 0-1 hold the index into
+// fieldNameEncodings, bits 2-5 hold nameSize-1 (15 means a varint with
+// nameSize-15 follows), bit 6 is the nullable flag. Type info is the
+// type id byte, plus precision+scale for DECIMAL, a recursive element
+// field for LIST, recursive key and value fields for MAP, and a field
+// count plus recursive fields for STRUCT.
+const (
+	schemaVersion          = 1
+	fieldNameSizeThreshold = 15
+	// Caps readField/readType recursion.
+	maxSchemaNestingDepth = 64
+)
+
+// Header bits 0-1 store the INDEX into this table (matching Java
+// SchemaEncoder.FIELD_NAME_ENCODINGS order), not the meta.Encoding
+// wire values, which differ.
+var fieldNameEncodings = [3]meta.Encoding{
+	meta.UTF_8,
+	meta.ALL_TO_LOWER_SPECIAL,
+	meta.LOWER_UPPER_DIGIT_SPECIAL,
+}
+
+// Stateless after construction, safe for concurrent use.
+var (
+	fieldNameEncoder = meta.NewEncoder('$', '_')
+	fieldNameDecoder = meta.NewDecoder('$', '_')
+)
+
+func encodingIndexOf(encoding meta.Encoding) int {
+	for i, e := range fieldNameEncodings {
+		if e == encoding {
+			return i
+		}
+	}
+	return -1
+}
+
+// SchemaToBytes serializes a schema to the cross-language wire format.
+func SchemaToBytes(s *Schema) ([]byte, error) {
+	buf := fory.NewByteBuffer(nil)
+	if err := SchemaToBuffer(s, buf); err != nil {
+		return nil, err
+	}
+	return buf.GetByteSlice(0, buf.WriterIndex()), nil
+}
+
+// SchemaToBuffer serializes a schema into an existing buffer.
+func SchemaToBuffer(s *Schema, buf *fory.ByteBuffer) error {
+	buf.WriteByte_(schemaVersion)
+	buf.WriteVarUint32Small7(uint32(s.NumFields()))
+	for _, f := range s.Fields() {
+		if err := writeSchemaField(buf, f); err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
+func writeSchemaField(buf *fory.ByteBuffer, f Field) error {
+	if f.Name == "" {
+		return fmt.Errorf("row: schema field with empty name")
+	}
+	encoding := fieldNameEncoder.ComputeEncodingWith(f.Name, fieldNameEncodings[:])
+	metaString, err := fieldNameEncoder.EncodeWithEncoding(f.Name, encoding)
+	if err != nil {
+		return fmt.Errorf("row: encoding field name %q: %w", f.Name, err)
+	}
+	nameBytes := metaString.GetEncodedBytes()
+	nameSize := len(nameBytes)
+	encodingIndex := encodingIndexOf(metaString.GetEncoding())
+	if encodingIndex < 0 {
+		return fmt.Errorf("row: field name %q got unsupported encoding %d", f.Name, metaString.GetEncoding())
+	}
+
+	header := encodingIndex & 0x03
+	bigSize := nameSize > fieldNameSizeThreshold
+	if bigSize {
+		header |= fieldNameSizeThreshold << 2
+	} else {
+		header |= (nameSize - 1) << 2
+	}
+	if f.Nullable {
+		header |= 0x40
+	}
+	buf.WriteByte_(byte(header))
+	if bigSize {
+		buf.WriteVarUint32Small7(uint32(nameSize - fieldNameSizeThreshold))
+	}
+	buf.WriteBinary(nameBytes)
+	return writeSchemaType(buf, f.Type)
+}
+
+func writeSchemaType(buf *fory.ByteBuffer, dataType DataType) error {

Review Comment:
   Validate schema types, depth, and cycles before writing
   
   The writer can emit a 65-level schema that its own reader rejects, and a self-referential ListType or StructType recurses until a fatal stack overflow. Dynamic type handling is also incomplete: *DecimalType writes only the type ID, out-of-range precision/scale is silently truncated to byte, and an external composite DataType can omit child metadata. Perform an exhaustive validation pass before writing any bytes.



##########
go/fory/row/infer.go:
##########
@@ -0,0 +1,252 @@
+// 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 (
+	"fmt"
+	"reflect"
+	"sort"
+	"strings"
+	"time"
+	"unicode"
+	"unicode/utf8"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+var (
+	goDateType     = reflect.TypeOf(fory.Date{})
+	goTimeType     = reflect.TypeOf(time.Time{})
+	goDurationType = reflect.TypeOf(time.Duration(0))
+)
+
+// InferSchema infers the row schema for a struct type or pointer to
+// struct, including every exported field not tagged `fory:"ignore"`.
+//
+// Fields are sorted by their lowerCamel name and named by its
+// snake_case form (UserName -> user_name), matching Java's schema
+// inference so both languages derive identical schemas.
+//
+// Type mapping:
+//   - bool, int8/16/32/64, float32/64: same-width row types; int maps to int64
+//   - string, []byte: string and binary, nullable
+//   - slices, maps, nested structs: list, map, and struct, nullable
+//   - *T: the row type of T, nullable
+//   - fory.Date, time.Time, time.Duration: date32, timestamp, duration
+//
+// Unsigned integers, fixed-size arrays, nested pointers, and pointer
+// map keys are unsupported and return an error.
+func InferSchema(t reflect.Type) (*Schema, error) {
+	layout, err := inferStructLayout(t, nil)
+	if err != nil {
+		return nil, err
+	}
+	return layout.schema, nil
+}
+
+// structLayout maps schema ordinals back to Go struct field indexes.
+type structLayout struct {
+	schema  *Schema
+	indexes []int // schema ordinal -> reflect struct field index
+}
+
+func inferStructLayout(t reflect.Type, path []reflect.Type) (*structLayout, error) {
+	if t.Kind() == reflect.Ptr {
+		t = t.Elem()
+	}
+	if t.Kind() != reflect.Struct || t == goDateType || t == goTimeType {
+		return nil, fmt.Errorf("row: schema inference expects a struct type, got %v", t)
+	}
+	for _, seen := range path {
+		if seen == t {
+			return nil, fmt.Errorf("row: circular reference through type %v", t)
+		}
+	}
+	path = append(path, t)
+
+	type member struct {
+		lowerCamel string
+		goIndex    int
+		fieldType  reflect.Type
+	}
+	var members []member
+	for i := 0; i < t.NumField(); i++ {
+		f := t.Field(i)
+		if f.PkgPath != "" {
+			continue
+		}
+		ignored, err := hasIgnoreTag(f.Tag.Get("fory"))
+		if err != nil {
+			return nil, fmt.Errorf("%w (field %s of %v)", err, f.Name, t)
+		}
+		if ignored {
+			continue
+		}
+		members = append(members, member{lowerFirst(f.Name), i, f.Type})
+	}
+	// Java sorts by the lowerCamel member name before converting names
+	// to snake_case; matching that order is required for schema and
+	// row-layout compatibility.
+	sort.Slice(members, func(a, b int) bool { return members[a].lowerCamel < members[b].lowerCamel })
+
+	layout := &structLayout{indexes: make([]int, 0, len(members))}
+	fields := make([]Field, 0, len(members))
+	for _, m := range members {
+		f, err := inferField(lowerCamelToLowerUnderscore(m.lowerCamel), m.fieldType, path)
+		if err != nil {
+			return nil, fmt.Errorf("%w (field %s of %v)", err, t.Field(m.goIndex).Name, t)
+		}
+		fields = append(fields, f)
+		layout.indexes = append(layout.indexes, m.goIndex)
+	}
+	layout.schema = NewSchema(fields)
+	return layout, nil
+}
+
+func inferField(name string, t reflect.Type, path []reflect.Type) (Field, error) {
+	if t.Kind() == reflect.Ptr {

Review Comment:
   Reject pointers to already-nilable carriers
   
   For *[]T, *map[K]V, and *[]byte, a nil outer pointer and a non-nil pointer to a nil container both produce the same row null bit. Decode always returns a nil outer pointer, so valid Go values cannot round-trip. Reject these double-nilable shapes unless the format gains a separate representation for both states.



##########
go/fory/row/infer.go:
##########
@@ -0,0 +1,252 @@
+// 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 (
+	"fmt"
+	"reflect"
+	"sort"
+	"strings"
+	"time"
+	"unicode"
+	"unicode/utf8"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+var (
+	goDateType     = reflect.TypeOf(fory.Date{})
+	goTimeType     = reflect.TypeOf(time.Time{})
+	goDurationType = reflect.TypeOf(time.Duration(0))
+)
+
+// InferSchema infers the row schema for a struct type or pointer to
+// struct, including every exported field not tagged `fory:"ignore"`.
+//
+// Fields are sorted by their lowerCamel name and named by its
+// snake_case form (UserName -> user_name), matching Java's schema
+// inference so both languages derive identical schemas.
+//
+// Type mapping:
+//   - bool, int8/16/32/64, float32/64: same-width row types; int maps to int64
+//   - string, []byte: string and binary, nullable
+//   - slices, maps, nested structs: list, map, and struct, nullable
+//   - *T: the row type of T, nullable
+//   - fory.Date, time.Time, time.Duration: date32, timestamp, duration
+//
+// Unsigned integers, fixed-size arrays, nested pointers, and pointer
+// map keys are unsupported and return an error.
+func InferSchema(t reflect.Type) (*Schema, error) {
+	layout, err := inferStructLayout(t, nil)
+	if err != nil {
+		return nil, err
+	}
+	return layout.schema, nil
+}
+
+// structLayout maps schema ordinals back to Go struct field indexes.
+type structLayout struct {
+	schema  *Schema
+	indexes []int // schema ordinal -> reflect struct field index
+}
+
+func inferStructLayout(t reflect.Type, path []reflect.Type) (*structLayout, error) {
+	if t.Kind() == reflect.Ptr {
+		t = t.Elem()
+	}
+	if t.Kind() != reflect.Struct || t == goDateType || t == goTimeType {
+		return nil, fmt.Errorf("row: schema inference expects a struct type, got %v", t)
+	}
+	for _, seen := range path {
+		if seen == t {
+			return nil, fmt.Errorf("row: circular reference through type %v", t)
+		}
+	}
+	path = append(path, t)
+
+	type member struct {
+		lowerCamel string
+		goIndex    int
+		fieldType  reflect.Type
+	}
+	var members []member
+	for i := 0; i < t.NumField(); i++ {
+		f := t.Field(i)
+		if f.PkgPath != "" {
+			continue
+		}
+		ignored, err := hasIgnoreTag(f.Tag.Get("fory"))
+		if err != nil {
+			return nil, fmt.Errorf("%w (field %s of %v)", err, f.Name, t)
+		}
+		if ignored {
+			continue
+		}
+		members = append(members, member{lowerFirst(f.Name), i, f.Type})
+	}
+	// Java sorts by the lowerCamel member name before converting names
+	// to snake_case; matching that order is required for schema and
+	// row-layout compatibility.
+	sort.Slice(members, func(a, b int) bool { return members[a].lowerCamel < members[b].lowerCamel })
+
+	layout := &structLayout{indexes: make([]int, 0, len(members))}
+	fields := make([]Field, 0, len(members))
+	for _, m := range members {
+		f, err := inferField(lowerCamelToLowerUnderscore(m.lowerCamel), m.fieldType, path)
+		if err != nil {
+			return nil, fmt.Errorf("%w (field %s of %v)", err, t.Field(m.goIndex).Name, t)
+		}
+		fields = append(fields, f)
+		layout.indexes = append(layout.indexes, m.goIndex)
+	}
+	layout.schema = NewSchema(fields)
+	return layout, nil
+}
+
+func inferField(name string, t reflect.Type, path []reflect.Type) (Field, error) {
+	if t.Kind() == reflect.Ptr {
+		if t.Elem().Kind() == reflect.Ptr {
+			return Field{}, fmt.Errorf("row: nested pointer type %v is unsupported", t)
+		}
+		inner, err := inferField(name, t.Elem(), path)
+		if err != nil {
+			return Field{}, err
+		}
+		inner.Nullable = true
+		return inner, nil
+	}
+	switch t {
+	case goDateType:
+		return Field{Name: name, Type: Date32Type{}}, nil
+	case goTimeType:
+		return Field{Name: name, Type: TimestampType{}}, nil
+	case goDurationType:
+		return Field{Name: name, Type: DurationType{}}, nil
+	}
+	switch t.Kind() {
+	case reflect.Bool:
+		return Field{Name: name, Type: BoolType{}}, nil
+	case reflect.Int8:
+		return Field{Name: name, Type: Int8Type{}}, nil
+	case reflect.Int16:
+		return Field{Name: name, Type: Int16Type{}}, nil
+	case reflect.Int32:
+		return Field{Name: name, Type: Int32Type{}}, nil
+	case reflect.Int64, reflect.Int:
+		// `int` maps to int64 so the width never depends on the platform.
+		return Field{Name: name, Type: Int64Type{}}, nil
+	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
+		return Field{}, fmt.Errorf("row: unsigned type %v is unsupported, use a signed type", t)
+	case reflect.Float32:
+		return Field{Name: name, Type: Float32Type{}}, nil
+	case reflect.Float64:
+		return Field{Name: name, Type: Float64Type{}}, nil
+	case reflect.String:
+		return Field{Name: name, Type: StringType{}, Nullable: true}, nil
+	case reflect.Slice:
+		if t.Elem().Kind() == reflect.Uint8 {
+			return Field{Name: name, Type: BinaryType{}, Nullable: true}, nil
+		}
+		elem, err := inferField(listItemName, t.Elem(), path)
+		if err != nil {
+			return Field{}, err
+		}
+		return Field{Name: name, Type: &ListType{Elem: elem}, Nullable: true}, nil
+	case reflect.Map:
+		if t.Key().Kind() == reflect.Ptr {

Review Comment:
   Require map keys to preserve Go equality
   
   Rejecting only a direct pointer key is insufficient. A comparable struct key can contain ignored or unexported equality state, causing distinct keys to encode identically and one entry to overwrite the other on decode; pointer identity nested inside a struct is also not preserved. Recursively restrict keys to shapes whose encoded fields fully determine Go ==.



##########
go/fory/row/datatype.go:
##########
@@ -0,0 +1,313 @@
+// 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 (
+	"fmt"
+	"strings"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+// DataType describes the row-format type of a field.
+//
+// Type ids are the cross-language Fory type ids shared with the Java,
+// C++, and Python row-format implementations; they appear verbatim in
+// the serialized schema wire format.
+type DataType interface {
+	TypeID() fory.TypeId
+	// ByteWidth returns the fixed storage width in bytes, or -1 for
+	// variable-width types stored through an offset+size slot.
+	ByteWidth() int
+	String() string
+}
+
+// Cross-language child field names for matching Java DataTypes.
+const (
+	listItemName = "item"
+	mapKeyName   = "key"
+	mapValueName = "value"
+)
+
+type BoolType struct{}
+
+func (BoolType) TypeID() fory.TypeId { return fory.BOOL }
+func (BoolType) ByteWidth() int      { return 1 }
+func (BoolType) String() string      { return "bool" }
+
+type Int8Type struct{}
+
+func (Int8Type) TypeID() fory.TypeId { return fory.INT8 }
+func (Int8Type) ByteWidth() int      { return 1 }
+func (Int8Type) String() string      { return "int8" }
+
+type Int16Type struct{}
+
+func (Int16Type) TypeID() fory.TypeId { return fory.INT16 }
+func (Int16Type) ByteWidth() int      { return 2 }
+func (Int16Type) String() string      { return "int16" }
+
+type Int32Type struct{}
+
+func (Int32Type) TypeID() fory.TypeId { return fory.INT32 }
+func (Int32Type) ByteWidth() int      { return 4 }
+func (Int32Type) String() string      { return "int32" }
+
+type Int64Type struct{}
+
+func (Int64Type) TypeID() fory.TypeId { return fory.INT64 }
+func (Int64Type) ByteWidth() int      { return 8 }
+func (Int64Type) String() string      { return "int64" }
+
+// Float16Type exists so schemas received from other languages parse,
+// the Go writer, reader, and encoder do not support it yet.
+type Float16Type struct{}
+
+func (Float16Type) TypeID() fory.TypeId { return fory.FLOAT16 }
+func (Float16Type) ByteWidth() int      { return 2 }
+func (Float16Type) String() string      { return "float16" }
+
+type Float32Type struct{}
+
+func (Float32Type) TypeID() fory.TypeId { return fory.FLOAT32 }
+func (Float32Type) ByteWidth() int      { return 4 }
+func (Float32Type) String() string      { return "float32" }
+
+type Float64Type struct{}
+
+func (Float64Type) TypeID() fory.TypeId { return fory.FLOAT64 }
+func (Float64Type) ByteWidth() int      { return 8 }
+func (Float64Type) String() string      { return "float64" }
+
+// StringType values are UTF-8 bytes in the variable data region,
+// stored as (offset + size) in the fixed slot region.
+type StringType struct{}
+
+func (StringType) TypeID() fory.TypeId { return fory.STRING }
+func (StringType) ByteWidth() int      { return -1 }
+func (StringType) String() string      { return "string" }
+
+type BinaryType struct{}
+
+func (BinaryType) TypeID() fory.TypeId { return fory.BINARY }
+func (BinaryType) ByteWidth() int      { return -1 }
+func (BinaryType) String() string      { return "binary" }
+
+// Date32Type values are days since the Unix epoch (int32).
+type Date32Type struct{}
+
+func (Date32Type) TypeID() fory.TypeId { return fory.DATE }
+func (Date32Type) ByteWidth() int      { return 4 }
+func (Date32Type) String() string      { return "date32" }
+
+// TimestampType values are microseconds since the Unix epoch (int64).
+type TimestampType struct{}
+
+func (TimestampType) TypeID() fory.TypeId { return fory.TIMESTAMP }
+func (TimestampType) ByteWidth() int      { return 8 }
+func (TimestampType) String() string      { return "timestamp" }
+
+// DurationType values are microseconds (int64).
+type DurationType struct{}
+
+func (DurationType) TypeID() fory.TypeId { return fory.DURATION }
+func (DurationType) ByteWidth() int      { return 8 }
+func (DurationType) String() string      { return "duration" }
+
+// DecimalType exists so schemas received from other languages parse; the
+// Go writer, reader, and encoder do not support it yet. Use it as a
+// value, not a pointer, so schema equality compares precision and scale.
+type DecimalType struct {
+	Precision int
+	Scale     int
+}
+
+func (DecimalType) TypeID() fory.TypeId { return fory.DECIMAL }
+func (DecimalType) ByteWidth() int      { return -1 }
+func (t DecimalType) String() string {
+	return fmt.Sprintf("decimal(%d, %d)", t.Precision, t.Scale)
+}
+
+// ListType is a variable-length sequence of Elem values.
+type ListType struct {
+	Elem Field
+}
+
+func (*ListType) TypeID() fory.TypeId { return fory.LIST }
+func (*ListType) ByteWidth() int      { return -1 }
+func (t *ListType) String() string    { return "list<" + t.Elem.Type.String() + ">" }
+
+// MapType stores keys and values as two adjacent arrays.
+type MapType struct {
+	Key   Field
+	Value Field
+}
+
+func (*MapType) TypeID() fory.TypeId { return fory.MAP }
+func (*MapType) ByteWidth() int      { return -1 }
+func (t *MapType) String() string {
+	return "map<" + t.Key.Type.String() + ", " + t.Value.Type.String() + ">"
+}
+
+// StructType is a nested row with its own bitmap, slots, and variable
+// data region.
+type StructType struct {
+	Fields []Field
+}
+
+func (*StructType) TypeID() fory.TypeId { return fory.STRUCT }
+func (*StructType) ByteWidth() int      { return -1 }
+func (t *StructType) String() string {
+	var sb strings.Builder
+	sb.WriteString("struct<")
+	for i, f := range t.Fields {
+		if i > 0 {
+			sb.WriteString(", ")
+		}
+		sb.WriteString(f.Name)
+		sb.WriteString(": ")
+		sb.WriteString(f.Type.String())
+	}
+	sb.WriteString(">")
+	return sb.String()
+}
+
+// Field is a named, optionally nullable slot in a schema or composite type.
+type Field struct {
+	Name     string
+	Type     DataType
+	Nullable bool
+}
+
+func NewField(name string, dataType DataType, nullable bool) Field {
+	return Field{Name: name, Type: dataType, Nullable: nullable}
+}
+
+func (f Field) Equal(o Field) bool {
+	return f.Name == o.Name && f.Nullable == o.Nullable && dataTypeEqual(f.Type, o.Type)
+}
+
+// List returns a list type with the cross-language element field name
+// "item" and nullable elements. Build ListType directly for
+// non-nullable elements.
+func List(elem DataType) *ListType {
+	return &ListType{Elem: Field{Name: listItemName, Type: elem, Nullable: true}}
+}
+
+// Map returns a map type with the cross-language field names
+// "key"/"value". Keys are always non-nullable.
+func Map(key, value DataType) *MapType {
+	return &MapType{
+		Key:   Field{Name: mapKeyName, Type: key, Nullable: false},
+		Value: Field{Name: mapValueName, Type: value, Nullable: true},
+	}
+}
+
+func Struct(fields []Field) *StructType {
+	return &StructType{Fields: fields}
+}
+
+func dataTypeEqual(a, b DataType) bool {
+	switch at := a.(type) {
+	case *ListType:
+		bt, ok := b.(*ListType)
+		return ok && at.Elem.Equal(bt.Elem)
+	case *MapType:
+		bt, ok := b.(*MapType)
+		return ok && at.Key.Equal(bt.Key) && at.Value.Equal(bt.Value)
+	case *StructType:
+		bt, ok := b.(*StructType)
+		if !ok || len(at.Fields) != len(bt.Fields) {
+			return false
+		}
+		for i := range at.Fields {
+			if !at.Fields[i].Equal(bt.Fields[i]) {
+				return false
+			}
+		}
+		return true
+	default:
+		// Primitive types are empty structs and DecimalType is a
+		// comparable value, so interface equality is exact.
+		return a == b
+	}
+}
+
+// Schema describes the fields of a top-level row.
+type Schema struct {
+	fields []Field
+	byName map[string]int
+}
+
+// NewSchema builds a schema from fields in their schema-declared order,
+// which fixes the field slot layout. For duplicate names, FieldIndex
+// resolves to the first occurrence.
+func NewSchema(fields []Field) *Schema {
+	byName := make(map[string]int, len(fields))
+	for i, f := range fields {
+		if _, ok := byName[f.Name]; !ok {
+			byName[f.Name] = i
+		}
+	}
+	return &Schema{fields: fields, byName: byName}

Review Comment:
   Copy schema fields before caching their indexes
   
   NewSchema stores the caller's slice after building byName. Mutating the original fields slice can therefore make Field(i) report a new name while FieldIndex still resolves the old one. Defensively copy the input, including nested schema state that participates in cached encoder/hash behavior.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org


Re: [PR] feat(go): support native fory row format [fory]

Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on code in PR #3901:
URL: https://github.com/apache/fory/pull/3901#discussion_r3704755586


##########
go/fory/row/encoder.go:
##########
@@ -0,0 +1,490 @@
+// 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"
+	"fmt"
+	"reflect"
+	"time"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+// Encoder converts values of struct type T to and from row bytes using
+// a codec tree compiled once with reflection at construction time. The
+// schema is inferred per InferSchema.
+//
+// An Encoder owns a reusable write buffer and is NOT goroutine-safe;
+// create one Encoder per goroutine. Decoding is safe on corrupt or

Review Comment:
   Keep Encoder decoding on the trusted-data boundary
   
   The repository security model classifies Row Format as a trusted in-memory format unless a runtime explicitly exposes an untrusted API. This sentence promotes Decode and FromRow into that stronger contract, but the implementation does not provide the allocation and work limits required for hostile bytes. Since this package is intended for trusted data, remove the corrupt/untrusted safety promise and document that callers must provide trusted, schema-matched row bytes.



##########
go/fory/row/schema_bytes.go:
##########
@@ -0,0 +1,351 @@
+// 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 (
+	"fmt"
+	"math"
+
+	fory "github.com/apache/fory/go/fory"
+	"github.com/apache/fory/go/fory/meta"
+)
+
+// Schema wire format, byte-compatible with Java SchemaEncoder:
+//
+//	| version (1 byte) | num_fields (var uint32 small7) | fields... |
+//
+// Field: | header (1 byte) | name size (varint, only if large) | name
+// bytes | type info |. Header bits 0-1 hold the index into
+// fieldNameEncodings, bits 2-5 hold nameSize-1 (15 means a varint with
+// nameSize-15 follows), bit 6 is the nullable flag. Type info is the
+// type id byte, plus precision+scale for DECIMAL, a recursive element
+// field for LIST, recursive key and value fields for MAP, and a field
+// count plus recursive fields for STRUCT.
+const (
+	schemaVersion          = 1
+	fieldNameSizeThreshold = 15
+	// Caps readField/readType recursion.
+	maxSchemaNestingDepth = 64
+)
+
+// Header bits 0-1 store the INDEX into this table (matching Java
+// SchemaEncoder.FIELD_NAME_ENCODINGS order), not the meta.Encoding
+// wire values, which differ.
+var fieldNameEncodings = [3]meta.Encoding{
+	meta.UTF_8,
+	meta.ALL_TO_LOWER_SPECIAL,
+	meta.LOWER_UPPER_DIGIT_SPECIAL,
+}
+
+// Stateless after construction, safe for concurrent use.
+var (
+	fieldNameEncoder = meta.NewEncoder('$', '_')
+	fieldNameDecoder = meta.NewDecoder('$', '_')
+)
+
+func encodingIndexOf(encoding meta.Encoding) int {
+	for i, e := range fieldNameEncodings {
+		if e == encoding {
+			return i
+		}
+	}
+	return -1
+}
+
+// SchemaToBytes serializes a schema to the cross-language wire format.
+func SchemaToBytes(s *Schema) ([]byte, error) {
+	buf := fory.NewByteBuffer(nil)
+	if err := SchemaToBuffer(s, buf); err != nil {
+		return nil, err
+	}
+	return buf.GetByteSlice(0, buf.WriterIndex()), nil
+}
+
+// SchemaToBuffer serializes a schema into an existing buffer.
+func SchemaToBuffer(s *Schema, buf *fory.ByteBuffer) error {
+	buf.WriteByte_(schemaVersion)
+	buf.WriteVarUint32Small7(uint32(s.NumFields()))
+	for _, f := range s.Fields() {
+		if err := writeSchemaField(buf, f); err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
+func writeSchemaField(buf *fory.ByteBuffer, f Field) error {
+	if f.Name == "" {
+		return fmt.Errorf("row: schema field with empty name")
+	}
+	encoding := fieldNameEncoder.ComputeEncodingWith(f.Name, fieldNameEncodings[:])
+	metaString, err := fieldNameEncoder.EncodeWithEncoding(f.Name, encoding)
+	if err != nil {
+		return fmt.Errorf("row: encoding field name %q: %w", f.Name, err)
+	}
+	nameBytes := metaString.GetEncodedBytes()
+	nameSize := len(nameBytes)
+	encodingIndex := encodingIndexOf(metaString.GetEncoding())
+	if encodingIndex < 0 {
+		return fmt.Errorf("row: field name %q got unsupported encoding %d", f.Name, metaString.GetEncoding())
+	}
+
+	header := encodingIndex & 0x03
+	bigSize := nameSize > fieldNameSizeThreshold
+	if bigSize {
+		header |= fieldNameSizeThreshold << 2
+	} else {
+		header |= (nameSize - 1) << 2
+	}
+	if f.Nullable {
+		header |= 0x40
+	}
+	buf.WriteByte_(byte(header))
+	if bigSize {
+		buf.WriteVarUint32Small7(uint32(nameSize - fieldNameSizeThreshold))
+	}
+	buf.WriteBinary(nameBytes)
+	return writeSchemaType(buf, f.Type)
+}
+
+func writeSchemaType(buf *fory.ByteBuffer, dataType DataType) error {
+	buf.WriteByte_(byte(dataType.TypeID()))
+	switch t := dataType.(type) {
+	case DecimalType:
+		buf.WriteByte_(byte(t.Precision))
+		buf.WriteByte_(byte(t.Scale))
+	case *ListType:
+		return writeSchemaField(buf, t.Elem)
+	case *MapType:
+		if err := writeSchemaField(buf, t.Key); err != nil {
+			return err
+		}
+		return writeSchemaField(buf, t.Value)
+	case *StructType:
+		buf.WriteVarUint32Small7(uint32(len(t.Fields)))
+		for _, f := range t.Fields {
+			if err := writeSchemaField(buf, f); err != nil {
+				return err
+			}
+		}
+	}
+	return nil
+}
+
+// SchemaFromBytes deserializes a schema from the cross-language wire
+// format. It is safe on untrusted input: declared sizes are checked

Review Comment:
   Do not expose schema parsing as untrusted-safe
   
   This comment explicitly makes SchemaFromBytes an untrusted deserialization API, which is broader than the trusted-only Row Format boundary. It is also stronger than the implementation: nested struct counts can allocate from the same unread tail at every level. Remove the untrusted-input claim and document trusted schema bytes as the precondition.



##########
go/fory/row/row.go:
##########
@@ -0,0 +1,370 @@
+// 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"
+	"fmt"
+	"math"
+	"time"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+// Row is a zero-copy reader over one row's bytes. Getters read directly
+// from the underlying data without deserializing other fields; nested
+// values are sub-slice views, never copies (except String).
+//
+// Fixed-width getters return the zero value for null fields; use
+// IsNullAt to distinguish. Variable-width getters return nil (or "")
+// for null fields. An out-of-range field index panics; offsets in
+// corrupt or untrusted data may panic on slice bounds, so wrap

Review Comment:
   Keep raw Row views trusted-only
   
   Suggesting that callers can make arbitrary untrusted rows safe by wrapping getters with recover is misleading: recover handles ordinary panics, not resource exhaustion, and these zero-copy views do not fully validate the row graph. State that Row and its nested views require trusted, schema-matched bytes instead.



##########
go/fory/row/encoder.go:
##########
@@ -0,0 +1,490 @@
+// 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"
+	"fmt"
+	"reflect"
+	"time"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+// Encoder converts values of struct type T to and from row bytes using
+// a codec tree compiled once with reflection at construction time. The
+// schema is inferred per InferSchema.
+//
+// An Encoder owns a reusable write buffer and is NOT goroutine-safe;
+// create one Encoder per goroutine. Decoding is safe on corrupt or
+// untrusted input.
+type Encoder[T any] struct {
+	codec *structCodec
+	hash  int64
+	buf   *fory.ByteBuffer
+}
+
+func NewEncoder[T any]() (*Encoder[T], error) {
+	t := reflect.TypeOf((*T)(nil)).Elem()
+	if t.Kind() != reflect.Struct {
+		return nil, fmt.Errorf("row: Encoder requires a struct type, got %v", t)
+	}
+	buf := fory.NewByteBuffer(nil)
+	codec, err := newStructCodec(t, buf)
+	if err != nil {
+		return nil, err
+	}
+	return &Encoder[T]{codec: codec, hash: ComputeSchemaHash(codec.schema), buf: buf}, nil
+}
+
+func (e *Encoder[T]) Schema() *Schema { return e.codec.schema }
+
+// SchemaHash returns the cross-language schema hash used by Encode and
+// Decode to detect out-of-sync struct definitions.
+func (e *Encoder[T]) SchemaHash() int64 { return e.hash }
+
+// ToRow serializes v to row bytes (no framing, just the row).
+func (e *Encoder[T]) ToRow(v *T) ([]byte, error) {
+	e.buf.SetWriterIndex(0)
+	if err := e.codec.writeStruct(reflect.ValueOf(v).Elem()); err != nil {
+		return nil, err
+	}
+	out := make([]byte, e.buf.WriterIndex())
+	copy(out, e.buf.GetData()[:e.buf.WriterIndex()])
+	return out, nil
+}
+
+// FromRow deserializes row bytes produced with this schema.
+func (e *Encoder[T]) FromRow(data []byte) (T, error) {
+	var out T
+	err := e.FromRowInto(data, &out)
+	return out, err
+}
+
+func (e *Encoder[T]) FromRowInto(data []byte, out *T) (err error) {
+	defer func() {
+		if p := recover(); p != nil {
+			err = fmt.Errorf("row: corrupt row data: %v", p)
+		}
+	}()
+	return e.codec.readStruct(NewRow(e.codec.schema, data), reflect.ValueOf(out).Elem())
+}
+
+// Encode frames the row for cross-language exchange: an int64
+// little-endian schema hash followed by the row bytes, matching the
+// Java and Python row encoders.
+func (e *Encoder[T]) Encode(v *T) ([]byte, error) {
+	rowBytes, err := e.ToRow(v)
+	if err != nil {
+		return nil, err
+	}
+	out := make([]byte, 8+len(rowBytes))
+	binary.LittleEndian.PutUint64(out, uint64(e.hash))
+	copy(out[8:], rowBytes)
+	return out, nil
+}
+
+// Decode verifies the schema hash and deserializes the row.
+func (e *Encoder[T]) Decode(data []byte) (T, error) {
+	var out T
+	if len(data) < 8 {
+		return out, fmt.Errorf("row: encoded data of %d bytes is too short for the schema hash", len(data))
+	}
+	hash := int64(binary.LittleEndian.Uint64(data))
+	if hash != e.hash {
+		return out, fmt.Errorf("row: schema hash mismatch: data has %d, encoder expects %d; writer and reader struct definitions are out of sync", hash, e.hash)
+	}
+	err := e.FromRowInto(data[8:], &out)
+	return out, err
+}
+
+// slotWriter is the shared write surface of RowWriter and ArrayWriter,
+// letting one value codec serve struct fields and array elements.
+type slotWriter interface {
+	SetNullAt(i int)
+	WriteBool(i int, v bool)
+	WriteInt8(i int, v int8)
+	WriteInt16(i int, v int16)
+	WriteInt32(i int, v int32)
+	WriteInt64(i int, v int64)
+	WriteFloat32(i int, v float32)
+	WriteFloat64(i int, v float64)
+	WriteDate(i int, d fory.Date) error
+	WriteTimestamp(i int, t time.Time)
+	WriteDuration(i int, d time.Duration)
+	WriteString(i int, s string) error
+	WriteBytes(i int, b []byte) error
+	SetOffsetAndSize(i, absStart, size int) error
+}
+
+// valueReader is the shared read surface of Row and ArrayData.
+type valueReader interface {
+	IsNullAt(i int) bool
+	Bool(i int) bool
+	Int8(i int) int8
+	Int16(i int) int16
+	Int32(i int) int32
+	Int64(i int) int64
+	Float32(i int) float32
+	Float64(i int) float64
+	Date(i int) fory.Date
+	Timestamp(i int) time.Time
+	Duration(i int) time.Duration
+	String(i int) string
+	Binary(i int) []byte
+	Struct(i int) *Row
+	Array(i int) *ArrayData
+	Map(i int) *MapData
+}
+
+// valueCodec reads or writes one value at slot/element i. Write
+// functions handle nil for nilable Go types; read functions overwrite
+// the target even when the field is null, so reused targets stay clean.
+type valueCodec struct {
+	write func(w slotWriter, i int, v reflect.Value) error
+	read  func(g valueReader, i int, v reflect.Value) error
+}
+
+// structCodec writes and reads one struct level; nested structs hold
+// their own child codec with a child RowWriter sharing the buffer.
+type structCodec struct {
+	schema *Schema
+	writer *RowWriter
+	fields []fieldCodec
+}
+
+type fieldCodec struct {
+	goIndex int
+	codec   valueCodec
+}
+
+func newStructCodec(t reflect.Type, buf *fory.ByteBuffer) (*structCodec, error) {
+	layout, err := inferStructLayout(t, nil)
+	if err != nil {
+		return nil, err
+	}
+	sc := &structCodec{
+		schema: layout.schema,
+		writer: NewRowWriterWithBuffer(layout.schema, buf),
+		fields: make([]fieldCodec, 0, layout.schema.NumFields()),
+	}
+	for ordinal, goIndex := range layout.indexes {
+		codec, err := newValueCodec(t.Field(goIndex).Type, layout.schema.Field(ordinal).Type, buf)

Review Comment:
   Reject null when the Go carrier cannot represent it
   
   The codec is built from Field.Type only, so nullability and the target carrier capability are lost. A frame produced for [](*int32){nil} has the same type hash as []int32 and decodes successfully as {0}; nullable strings and value structs similarly become empty values. Pass the Field/nullability into codec construction and return an error when a non-pointer carrier receives null.



##########
go/fory/row/infer.go:
##########
@@ -0,0 +1,252 @@
+// 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 (
+	"fmt"
+	"reflect"
+	"sort"
+	"strings"
+	"time"
+	"unicode"
+	"unicode/utf8"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+var (
+	goDateType     = reflect.TypeOf(fory.Date{})
+	goTimeType     = reflect.TypeOf(time.Time{})
+	goDurationType = reflect.TypeOf(time.Duration(0))
+)
+
+// InferSchema infers the row schema for a struct type or pointer to
+// struct, including every exported field not tagged `fory:"ignore"`.
+//
+// Fields are sorted by their lowerCamel name and named by its
+// snake_case form (UserName -> user_name), matching Java's schema
+// inference so both languages derive identical schemas.
+//
+// Type mapping:
+//   - bool, int8/16/32/64, float32/64: same-width row types; int maps to int64
+//   - string, []byte: string and binary, nullable
+//   - slices, maps, nested structs: list, map, and struct, nullable
+//   - *T: the row type of T, nullable
+//   - fory.Date, time.Time, time.Duration: date32, timestamp, duration
+//
+// Unsigned integers, fixed-size arrays, nested pointers, and pointer
+// map keys are unsupported and return an error.
+func InferSchema(t reflect.Type) (*Schema, error) {
+	layout, err := inferStructLayout(t, nil)
+	if err != nil {
+		return nil, err
+	}
+	return layout.schema, nil
+}
+
+// structLayout maps schema ordinals back to Go struct field indexes.
+type structLayout struct {
+	schema  *Schema
+	indexes []int // schema ordinal -> reflect struct field index
+}
+
+func inferStructLayout(t reflect.Type, path []reflect.Type) (*structLayout, error) {
+	if t.Kind() == reflect.Ptr {
+		t = t.Elem()
+	}
+	if t.Kind() != reflect.Struct || t == goDateType || t == goTimeType {
+		return nil, fmt.Errorf("row: schema inference expects a struct type, got %v", t)
+	}
+	for _, seen := range path {
+		if seen == t {
+			return nil, fmt.Errorf("row: circular reference through type %v", t)
+		}
+	}
+	path = append(path, t)
+
+	type member struct {
+		lowerCamel string
+		goIndex    int
+		fieldType  reflect.Type
+	}
+	var members []member
+	for i := 0; i < t.NumField(); i++ {
+		f := t.Field(i)
+		if f.PkgPath != "" {
+			continue
+		}
+		ignored, err := hasIgnoreTag(f.Tag.Get("fory"))
+		if err != nil {
+			return nil, fmt.Errorf("%w (field %s of %v)", err, f.Name, t)
+		}
+		if ignored {
+			continue
+		}
+		members = append(members, member{lowerFirst(f.Name), i, f.Type})
+	}
+	// Java sorts by the lowerCamel member name before converting names
+	// to snake_case; matching that order is required for schema and
+	// row-layout compatibility.
+	sort.Slice(members, func(a, b int) bool { return members[a].lowerCamel < members[b].lowerCamel })
+
+	layout := &structLayout{indexes: make([]int, 0, len(members))}
+	fields := make([]Field, 0, len(members))
+	for _, m := range members {
+		f, err := inferField(lowerCamelToLowerUnderscore(m.lowerCamel), m.fieldType, path)
+		if err != nil {
+			return nil, fmt.Errorf("%w (field %s of %v)", err, t.Field(m.goIndex).Name, t)
+		}
+		fields = append(fields, f)
+		layout.indexes = append(layout.indexes, m.goIndex)
+	}
+	layout.schema = NewSchema(fields)
+	return layout, nil
+}
+
+func inferField(name string, t reflect.Type, path []reflect.Type) (Field, error) {
+	if t.Kind() == reflect.Ptr {
+		if t.Elem().Kind() == reflect.Ptr {
+			return Field{}, fmt.Errorf("row: nested pointer type %v is unsupported", t)
+		}
+		inner, err := inferField(name, t.Elem(), path)
+		if err != nil {
+			return Field{}, err
+		}
+		inner.Nullable = true
+		return inner, nil
+	}
+	switch t {
+	case goDateType:
+		return Field{Name: name, Type: Date32Type{}}, nil
+	case goTimeType:
+		return Field{Name: name, Type: TimestampType{}}, nil
+	case goDurationType:
+		return Field{Name: name, Type: DurationType{}}, nil
+	}
+	switch t.Kind() {
+	case reflect.Bool:
+		return Field{Name: name, Type: BoolType{}}, nil
+	case reflect.Int8:
+		return Field{Name: name, Type: Int8Type{}}, nil
+	case reflect.Int16:
+		return Field{Name: name, Type: Int16Type{}}, nil
+	case reflect.Int32:
+		return Field{Name: name, Type: Int32Type{}}, nil
+	case reflect.Int64, reflect.Int:
+		// `int` maps to int64 so the width never depends on the platform.
+		return Field{Name: name, Type: Int64Type{}}, nil
+	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
+		return Field{}, fmt.Errorf("row: unsigned type %v is unsupported, use a signed type", t)
+	case reflect.Float32:
+		return Field{Name: name, Type: Float32Type{}}, nil
+	case reflect.Float64:
+		return Field{Name: name, Type: Float64Type{}}, nil
+	case reflect.String:
+		return Field{Name: name, Type: StringType{}, Nullable: true}, nil
+	case reflect.Slice:
+		if t.Elem().Kind() == reflect.Uint8 {
+			return Field{Name: name, Type: BinaryType{}, Nullable: true}, nil
+		}
+		elem, err := inferField(listItemName, t.Elem(), path)
+		if err != nil {
+			return Field{}, err
+		}
+		return Field{Name: name, Type: &ListType{Elem: elem}, Nullable: true}, nil
+	case reflect.Map:
+		if t.Key().Kind() == reflect.Ptr {
+			return Field{}, fmt.Errorf("row: pointer map key type %v is unsupported", t)
+		}
+		key, err := inferField(mapKeyName, t.Key(), path)
+		if err != nil {
+			return Field{}, err
+		}
+		key.Nullable = false
+		value, err := inferField(mapValueName, t.Elem(), path)

Review Comment:
   Canonicalize map value nullability during inference
   
   For map[string]int32 this leaves the value field non-nullable, while both Java MapType and the Go schema parser canonicalize map values as nullable. As a result, SchemaFromBytes(SchemaToBytes(inferred)) is not equal to the inferred schema and Java emits different schema bytes. Force the canonical map child fields here and add a primitive-valued map round-trip/interoperability case.



##########
go/fory/row/infer.go:
##########
@@ -0,0 +1,252 @@
+// 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 (
+	"fmt"
+	"reflect"
+	"sort"
+	"strings"
+	"time"
+	"unicode"
+	"unicode/utf8"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+var (
+	goDateType     = reflect.TypeOf(fory.Date{})
+	goTimeType     = reflect.TypeOf(time.Time{})
+	goDurationType = reflect.TypeOf(time.Duration(0))
+)
+
+// InferSchema infers the row schema for a struct type or pointer to
+// struct, including every exported field not tagged `fory:"ignore"`.
+//
+// Fields are sorted by their lowerCamel name and named by its
+// snake_case form (UserName -> user_name), matching Java's schema
+// inference so both languages derive identical schemas.
+//
+// Type mapping:
+//   - bool, int8/16/32/64, float32/64: same-width row types; int maps to int64
+//   - string, []byte: string and binary, nullable
+//   - slices, maps, nested structs: list, map, and struct, nullable
+//   - *T: the row type of T, nullable
+//   - fory.Date, time.Time, time.Duration: date32, timestamp, duration
+//
+// Unsigned integers, fixed-size arrays, nested pointers, and pointer
+// map keys are unsupported and return an error.
+func InferSchema(t reflect.Type) (*Schema, error) {
+	layout, err := inferStructLayout(t, nil)
+	if err != nil {
+		return nil, err
+	}
+	return layout.schema, nil
+}
+
+// structLayout maps schema ordinals back to Go struct field indexes.
+type structLayout struct {
+	schema  *Schema
+	indexes []int // schema ordinal -> reflect struct field index
+}
+
+func inferStructLayout(t reflect.Type, path []reflect.Type) (*structLayout, error) {
+	if t.Kind() == reflect.Ptr {
+		t = t.Elem()
+	}
+	if t.Kind() != reflect.Struct || t == goDateType || t == goTimeType {
+		return nil, fmt.Errorf("row: schema inference expects a struct type, got %v", t)
+	}
+	for _, seen := range path {
+		if seen == t {
+			return nil, fmt.Errorf("row: circular reference through type %v", t)
+		}
+	}
+	path = append(path, t)
+
+	type member struct {
+		lowerCamel string
+		goIndex    int
+		fieldType  reflect.Type
+	}
+	var members []member
+	for i := 0; i < t.NumField(); i++ {
+		f := t.Field(i)
+		if f.PkgPath != "" {
+			continue
+		}
+		ignored, err := hasIgnoreTag(f.Tag.Get("fory"))
+		if err != nil {
+			return nil, fmt.Errorf("%w (field %s of %v)", err, f.Name, t)
+		}
+		if ignored {
+			continue
+		}
+		members = append(members, member{lowerFirst(f.Name), i, f.Type})
+	}
+	// Java sorts by the lowerCamel member name before converting names
+	// to snake_case; matching that order is required for schema and
+	// row-layout compatibility.
+	sort.Slice(members, func(a, b int) bool { return members[a].lowerCamel < members[b].lowerCamel })
+
+	layout := &structLayout{indexes: make([]int, 0, len(members))}
+	fields := make([]Field, 0, len(members))
+	for _, m := range members {
+		f, err := inferField(lowerCamelToLowerUnderscore(m.lowerCamel), m.fieldType, path)
+		if err != nil {
+			return nil, fmt.Errorf("%w (field %s of %v)", err, t.Field(m.goIndex).Name, t)
+		}
+		fields = append(fields, f)
+		layout.indexes = append(layout.indexes, m.goIndex)
+	}
+	layout.schema = NewSchema(fields)
+	return layout, nil
+}
+
+func inferField(name string, t reflect.Type, path []reflect.Type) (Field, error) {
+	if t.Kind() == reflect.Ptr {
+		if t.Elem().Kind() == reflect.Ptr {
+			return Field{}, fmt.Errorf("row: nested pointer type %v is unsupported", t)
+		}
+		inner, err := inferField(name, t.Elem(), path)
+		if err != nil {
+			return Field{}, err
+		}
+		inner.Nullable = true
+		return inner, nil
+	}
+	switch t {
+	case goDateType:
+		return Field{Name: name, Type: Date32Type{}}, nil
+	case goTimeType:
+		return Field{Name: name, Type: TimestampType{}}, nil
+	case goDurationType:
+		return Field{Name: name, Type: DurationType{}}, nil
+	}
+	switch t.Kind() {
+	case reflect.Bool:
+		return Field{Name: name, Type: BoolType{}}, nil
+	case reflect.Int8:
+		return Field{Name: name, Type: Int8Type{}}, nil
+	case reflect.Int16:
+		return Field{Name: name, Type: Int16Type{}}, nil
+	case reflect.Int32:
+		return Field{Name: name, Type: Int32Type{}}, nil
+	case reflect.Int64, reflect.Int:
+		// `int` maps to int64 so the width never depends on the platform.
+		return Field{Name: name, Type: Int64Type{}}, nil
+	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
+		return Field{}, fmt.Errorf("row: unsigned type %v is unsupported, use a signed type", t)
+	case reflect.Float32:
+		return Field{Name: name, Type: Float32Type{}}, nil
+	case reflect.Float64:
+		return Field{Name: name, Type: Float64Type{}}, nil
+	case reflect.String:
+		return Field{Name: name, Type: StringType{}, Nullable: true}, nil
+	case reflect.Slice:
+		if t.Elem().Kind() == reflect.Uint8 {

Review Comment:
   Align []byte with the Java row carrier
   
   Go infers []byte as BINARY/raw bytes, but the current Java row TypeInference treats ordinary byte[] as LIST<int8>; only BinaryArray maps to BINARY. Common Go and Java models therefore get different hashes and incompatible raw layouts. Align Java row inference with the shared byte[] mapping, or define and test the exact Java carrier required for this Go type.



##########
go/fory/row/schema_bytes.go:
##########
@@ -0,0 +1,351 @@
+// 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 (
+	"fmt"
+	"math"
+
+	fory "github.com/apache/fory/go/fory"
+	"github.com/apache/fory/go/fory/meta"
+)
+
+// Schema wire format, byte-compatible with Java SchemaEncoder:
+//
+//	| version (1 byte) | num_fields (var uint32 small7) | fields... |
+//
+// Field: | header (1 byte) | name size (varint, only if large) | name
+// bytes | type info |. Header bits 0-1 hold the index into
+// fieldNameEncodings, bits 2-5 hold nameSize-1 (15 means a varint with
+// nameSize-15 follows), bit 6 is the nullable flag. Type info is the
+// type id byte, plus precision+scale for DECIMAL, a recursive element
+// field for LIST, recursive key and value fields for MAP, and a field
+// count plus recursive fields for STRUCT.
+const (
+	schemaVersion          = 1
+	fieldNameSizeThreshold = 15
+	// Caps readField/readType recursion.
+	maxSchemaNestingDepth = 64
+)
+
+// Header bits 0-1 store the INDEX into this table (matching Java
+// SchemaEncoder.FIELD_NAME_ENCODINGS order), not the meta.Encoding
+// wire values, which differ.
+var fieldNameEncodings = [3]meta.Encoding{
+	meta.UTF_8,
+	meta.ALL_TO_LOWER_SPECIAL,
+	meta.LOWER_UPPER_DIGIT_SPECIAL,
+}
+
+// Stateless after construction, safe for concurrent use.
+var (
+	fieldNameEncoder = meta.NewEncoder('$', '_')
+	fieldNameDecoder = meta.NewDecoder('$', '_')
+)
+
+func encodingIndexOf(encoding meta.Encoding) int {
+	for i, e := range fieldNameEncodings {
+		if e == encoding {
+			return i
+		}
+	}
+	return -1
+}
+
+// SchemaToBytes serializes a schema to the cross-language wire format.
+func SchemaToBytes(s *Schema) ([]byte, error) {
+	buf := fory.NewByteBuffer(nil)
+	if err := SchemaToBuffer(s, buf); err != nil {
+		return nil, err
+	}
+	return buf.GetByteSlice(0, buf.WriterIndex()), nil
+}
+
+// SchemaToBuffer serializes a schema into an existing buffer.
+func SchemaToBuffer(s *Schema, buf *fory.ByteBuffer) error {
+	buf.WriteByte_(schemaVersion)
+	buf.WriteVarUint32Small7(uint32(s.NumFields()))
+	for _, f := range s.Fields() {
+		if err := writeSchemaField(buf, f); err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
+func writeSchemaField(buf *fory.ByteBuffer, f Field) error {
+	if f.Name == "" {
+		return fmt.Errorf("row: schema field with empty name")
+	}
+	encoding := fieldNameEncoder.ComputeEncodingWith(f.Name, fieldNameEncodings[:])
+	metaString, err := fieldNameEncoder.EncodeWithEncoding(f.Name, encoding)
+	if err != nil {
+		return fmt.Errorf("row: encoding field name %q: %w", f.Name, err)
+	}
+	nameBytes := metaString.GetEncodedBytes()
+	nameSize := len(nameBytes)
+	encodingIndex := encodingIndexOf(metaString.GetEncoding())
+	if encodingIndex < 0 {
+		return fmt.Errorf("row: field name %q got unsupported encoding %d", f.Name, metaString.GetEncoding())
+	}
+
+	header := encodingIndex & 0x03
+	bigSize := nameSize > fieldNameSizeThreshold
+	if bigSize {
+		header |= fieldNameSizeThreshold << 2
+	} else {
+		header |= (nameSize - 1) << 2
+	}
+	if f.Nullable {
+		header |= 0x40
+	}
+	buf.WriteByte_(byte(header))
+	if bigSize {
+		buf.WriteVarUint32Small7(uint32(nameSize - fieldNameSizeThreshold))
+	}
+	buf.WriteBinary(nameBytes)
+	return writeSchemaType(buf, f.Type)
+}
+
+func writeSchemaType(buf *fory.ByteBuffer, dataType DataType) error {
+	buf.WriteByte_(byte(dataType.TypeID()))
+	switch t := dataType.(type) {
+	case DecimalType:
+		buf.WriteByte_(byte(t.Precision))
+		buf.WriteByte_(byte(t.Scale))
+	case *ListType:
+		return writeSchemaField(buf, t.Elem)
+	case *MapType:
+		if err := writeSchemaField(buf, t.Key); err != nil {
+			return err
+		}
+		return writeSchemaField(buf, t.Value)
+	case *StructType:
+		buf.WriteVarUint32Small7(uint32(len(t.Fields)))
+		for _, f := range t.Fields {
+			if err := writeSchemaField(buf, f); err != nil {
+				return err
+			}
+		}
+	}
+	return nil
+}
+
+// SchemaFromBytes deserializes a schema from the cross-language wire
+// format. It is safe on untrusted input: declared sizes are checked
+// against remaining bytes before any allocation.
+func SchemaFromBytes(data []byte) (*Schema, error) {
+	r := &schemaReader{buf: fory.NewByteBuffer(data), size: len(data)}
+	version := r.buf.ReadUint8(&r.err)
+	if err := r.err.CheckError(); err != nil {
+		return nil, err
+	}
+	if version != schemaVersion {
+		return nil, fmt.Errorf("row: unsupported schema version %d, expected %d", version, schemaVersion)
+	}
+	numFields := int(r.buf.ReadVarUint32Small7(&r.err))

Review Comment:
   Validate wire integers before narrowing to int
   
   On 32-bit Go, converting the uint32 count first can produce a negative int that bypasses the remaining-byte check and reaches make with a negative capacity. Array counts and map key sizes have the same truncation problem, and INT64 decoded into a Go int can silently narrow. Validate against MaxInt before conversion, check reflect.Value.OverflowInt, and make the new tests cross-compile on 386 instead of using untyped 1<<40/1<<33 int arguments.



##########
java/fory-format/src/test/java/org/apache/fory/format/GoCrossLanguageTest.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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 org.apache.fory.format;
+
+import com.google.common.collect.ImmutableMap;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import org.apache.fory.format.encoder.Encoders;
+import org.apache.fory.format.encoder.RowEncoder;
+import org.apache.fory.format.row.binary.BinaryRow;
+import org.apache.fory.format.type.DataTypes;
+import org.apache.fory.memory.MemoryBuffer;
+import org.apache.fory.memory.MemoryUtils;
+import org.apache.fory.test.TestUtils;
+import org.testng.Assert;
+import org.testng.SkipException;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+/**
+ * Row format cross-language tests against a Go peer built from {@code go/fory/tests/row_xlang}.
+ * Data shapes are shared with {@link CrossLanguageTest} so the Java, Python, and Go peers exercise
+ * the same schemas.
+ */
+@Test
+public class GoCrossLanguageTest {
+  private static final boolean IS_WINDOWS =
+      System.getProperty("os.name").toLowerCase().contains("windows");
+  private static final String GO_BINARY = IS_WINDOWS ? "row_xlang_bin.exe" : "row_xlang_bin";
+
+  @BeforeClass
+  public void ensureGoPeerReady() {
+    String enabled = System.getenv("FORY_GO_JAVA_CI");
+    if (!"1".equals(enabled)) {
+      throw new SkipException("Skipping GoCrossLanguageTest: FORY_GO_JAVA_CI not set to 1");
+    }
+    boolean goInstalled = true;
+    try {
+      Process process = new ProcessBuilder("go", "version").start();
+      if (process.waitFor() != 0) {
+        goInstalled = false;
+      }
+    } catch (IOException | InterruptedException e) {
+      goInstalled = false;
+      if (e instanceof InterruptedException) {
+        Thread.currentThread().interrupt();
+      }
+    }
+    if (!goInstalled) {
+      throw new SkipException("Skipping GoCrossLanguageTest: go not installed");
+    }
+    List<String> buildCommand =
+        Arrays.asList("go", "build", "-o", "tests/" + GO_BINARY, "./tests/row_xlang");
+    boolean buildSuccess =
+        TestUtils.executeCommand(
+            buildCommand, 120, Collections.emptyMap(), new File("../../go/fory"));
+    if (!buildSuccess || !new File("../../go/fory/tests/" + GO_BINARY).exists()) {
+      throw new SkipException("Skipping GoCrossLanguageTest: failed to build " + GO_BINARY);

Review Comment:
   Fail the enabled cross-language test when the Go peer cannot build
   
   FORY_GO_JAVA_CI=1 explicitly opts into this suite, but a missing Go toolchain, failed go build, or missing binary is still converted to SkipException. The normal Go task does not compile tests/row_xlang, so a peer-only compile regression can leave CI green. Once enabled, every setup failure should fail the test; only the non-opted-in case should skip.



##########
go/fory/row/infer.go:
##########
@@ -0,0 +1,252 @@
+// 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 (
+	"fmt"
+	"reflect"
+	"sort"
+	"strings"
+	"time"
+	"unicode"
+	"unicode/utf8"
+
+	fory "github.com/apache/fory/go/fory"
+)
+
+var (
+	goDateType     = reflect.TypeOf(fory.Date{})
+	goTimeType     = reflect.TypeOf(time.Time{})
+	goDurationType = reflect.TypeOf(time.Duration(0))
+)
+
+// InferSchema infers the row schema for a struct type or pointer to
+// struct, including every exported field not tagged `fory:"ignore"`.
+//
+// Fields are sorted by their lowerCamel name and named by its
+// snake_case form (UserName -> user_name), matching Java's schema
+// inference so both languages derive identical schemas.
+//
+// Type mapping:
+//   - bool, int8/16/32/64, float32/64: same-width row types; int maps to int64
+//   - string, []byte: string and binary, nullable
+//   - slices, maps, nested structs: list, map, and struct, nullable
+//   - *T: the row type of T, nullable
+//   - fory.Date, time.Time, time.Duration: date32, timestamp, duration
+//
+// Unsigned integers, fixed-size arrays, nested pointers, and pointer
+// map keys are unsupported and return an error.
+func InferSchema(t reflect.Type) (*Schema, error) {
+	layout, err := inferStructLayout(t, nil)
+	if err != nil {
+		return nil, err
+	}
+	return layout.schema, nil
+}
+
+// structLayout maps schema ordinals back to Go struct field indexes.
+type structLayout struct {
+	schema  *Schema
+	indexes []int // schema ordinal -> reflect struct field index
+}
+
+func inferStructLayout(t reflect.Type, path []reflect.Type) (*structLayout, error) {
+	if t.Kind() == reflect.Ptr {
+		t = t.Elem()
+	}
+	if t.Kind() != reflect.Struct || t == goDateType || t == goTimeType {
+		return nil, fmt.Errorf("row: schema inference expects a struct type, got %v", t)
+	}
+	for _, seen := range path {
+		if seen == t {
+			return nil, fmt.Errorf("row: circular reference through type %v", t)
+		}
+	}
+	path = append(path, t)
+
+	type member struct {
+		lowerCamel string
+		goIndex    int
+		fieldType  reflect.Type
+	}
+	var members []member
+	for i := 0; i < t.NumField(); i++ {
+		f := t.Field(i)
+		if f.PkgPath != "" {
+			continue
+		}
+		ignored, err := hasIgnoreTag(f.Tag.Get("fory"))
+		if err != nil {
+			return nil, fmt.Errorf("%w (field %s of %v)", err, f.Name, t)
+		}
+		if ignored {
+			continue
+		}
+		members = append(members, member{lowerFirst(f.Name), i, f.Type})
+	}
+	// Java sorts by the lowerCamel member name before converting names
+	// to snake_case; matching that order is required for schema and
+	// row-layout compatibility.
+	sort.Slice(members, func(a, b int) bool { return members[a].lowerCamel < members[b].lowerCamel })
+
+	layout := &structLayout{indexes: make([]int, 0, len(members))}
+	fields := make([]Field, 0, len(members))
+	for _, m := range members {
+		f, err := inferField(lowerCamelToLowerUnderscore(m.lowerCamel), m.fieldType, path)
+		if err != nil {
+			return nil, fmt.Errorf("%w (field %s of %v)", err, t.Field(m.goIndex).Name, t)
+		}
+		fields = append(fields, f)
+		layout.indexes = append(layout.indexes, m.goIndex)
+	}
+	layout.schema = NewSchema(fields)
+	return layout, nil
+}
+
+func inferField(name string, t reflect.Type, path []reflect.Type) (Field, error) {
+	if t.Kind() == reflect.Ptr {
+		if t.Elem().Kind() == reflect.Ptr {
+			return Field{}, fmt.Errorf("row: nested pointer type %v is unsupported", t)
+		}
+		inner, err := inferField(name, t.Elem(), path)
+		if err != nil {
+			return Field{}, err
+		}
+		inner.Nullable = true
+		return inner, nil
+	}
+	switch t {
+	case goDateType:
+		return Field{Name: name, Type: Date32Type{}}, nil
+	case goTimeType:
+		return Field{Name: name, Type: TimestampType{}}, nil
+	case goDurationType:
+		return Field{Name: name, Type: DurationType{}}, nil
+	}
+	switch t.Kind() {
+	case reflect.Bool:
+		return Field{Name: name, Type: BoolType{}}, nil
+	case reflect.Int8:
+		return Field{Name: name, Type: Int8Type{}}, nil
+	case reflect.Int16:
+		return Field{Name: name, Type: Int16Type{}}, nil
+	case reflect.Int32:
+		return Field{Name: name, Type: Int32Type{}}, nil
+	case reflect.Int64, reflect.Int:
+		// `int` maps to int64 so the width never depends on the platform.
+		return Field{Name: name, Type: Int64Type{}}, nil
+	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
+		return Field{}, fmt.Errorf("row: unsigned type %v is unsupported, use a signed type", t)
+	case reflect.Float32:
+		return Field{Name: name, Type: Float32Type{}}, nil
+	case reflect.Float64:
+		return Field{Name: name, Type: Float64Type{}}, nil
+	case reflect.String:
+		return Field{Name: name, Type: StringType{}, Nullable: true}, nil
+	case reflect.Slice:
+		if t.Elem().Kind() == reflect.Uint8 {
+			return Field{Name: name, Type: BinaryType{}, Nullable: true}, nil
+		}
+		elem, err := inferField(listItemName, t.Elem(), path)

Review Comment:
   Detect recursive slice and map aliases
   
   The cycle check is only entered for structs. Valid declarations such as type L []L and type M map[string]M recurse indefinitely through inferField and eventually overflow the stack. Track every composite type in the active path and return the same cycle error used for structs.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org


Re: [PR] feat(go): support native fory row format [fory]

Posted by "ayush00git (via GitHub)" <gi...@apache.org>.
ayush00git commented on PR #3901:
URL: https://github.com/apache/fory/pull/3901#issuecomment-5159476265

   @chaokunyang ready for review


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org


Re: [PR] feat(go): support native fory row format [fory]

Posted by "ayush00git (via GitHub)" <gi...@apache.org>.
ayush00git commented on PR #3901:
URL: https://github.com/apache/fory/pull/3901#issuecomment-5381714382

   @chaokunyang have a look 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org