Skip to content

Commit

Permalink
data: Add NilAt method to Field and Frame (#1208)
Browse files Browse the repository at this point in the history
NilAt returns true if the element at index idx of the Field is nil.
This is useful since the interface returned by At() will not be nil even if the underlying element is nil (without an type assertion).
  • Loading branch information
kylebrandt authored Jan 28, 2025
1 parent 69da630 commit d477c83
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 0 deletions.
9 changes: 9 additions & 0 deletions data/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ func (f *Field) At(idx int) interface{} {
return f.vector.At(idx)
}

// NilAt returns true if the element at index idx of the Field is nil.
// This is useful since the interface returned by At() will not be nil
// even if the underlying element is nil (without an type assertion).
// It will always return false if the Field is not nullable.
// It can panic if idx is out of range.
func (f *Field) NilAt(idx int) bool {
return f.vector.NilAt(idx)
}

// Len returns the number of elements in the Field.
// It will return 0 if the field is nil.
func (f *Field) Len() int {
Expand Down
13 changes: 13 additions & 0 deletions data/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ func TestFieldLen(t *testing.T) {
require.Equal(t, 0, f.Len())
}

func TestFieldNilAt(t *testing.T) {
f := data.NewField("value", nil, []*float64{nil, float64Ptr(1)})

require.True(t, f.NilAt(0))
require.False(t, f.At(0) == nil) // Why we have NilAt()
require.False(t, f.NilAt(1))

f = data.NewField("value", nil, []string{"", "foo"})
require.False(t, f.NilAt(1))
require.False(t, f.NilAt(2))
require.False(t, f.NilAt(77))
}

func TestField_String(t *testing.T) {
field := data.NewField("value", nil, make([]*string, 3))

Expand Down
8 changes: 8 additions & 0 deletions data/field_type_enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func (v *enumVector) At(i int) interface{} {
return (*v)[i]
}

func (v *enumVector) NilAt(_ int) bool {
return false
}

func (v *enumVector) PointerAt(i int) interface{} {
return &(*v)[i]
}
Expand Down Expand Up @@ -115,6 +119,10 @@ func (v *nullableEnumVector) At(i int) interface{} {
return (*v)[i]
}

func (v *nullableEnumVector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableEnumVector) CopyAt(i int) interface{} {
if (*v)[i] == nil {
var g *EnumItemIndex
Expand Down
6 changes: 6 additions & 0 deletions data/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ func (f *Frame) Rows() int {
return 0
}

// NilAt returns true if the element at fieldIdx and rowIdx is nil.
// It will panic if either fieldIdx or rowIdx are out of range.
func (f *Frame) NilAt(fieldIdx int, rowIdx int) bool {
return f.Fields[fieldIdx].vector.NilAt(rowIdx)
}

// At returns the value of the specified fieldIdx and rowIdx.
// It will panic if either fieldIdx or rowIdx are out of range.
func (f *Frame) At(fieldIdx int, rowIdx int) interface{} {
Expand Down
4 changes: 4 additions & 0 deletions data/generic_nullable_vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func (v *nullablegenVector) Append(i interface{}) {
*v = append(*v, i.(*gen))
}

func (v *nullablegenVector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullablegenVector) At(i int) interface{} {
return (*v)[i]
}
Expand Down
4 changes: 4 additions & 0 deletions data/generic_vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func (v *genVector) Append(i interface{}) {
*v = append(*v, i.(gen))
}

func (v *genVector) NilAt(i int) bool {
return false
}

func (v *genVector) At(i int) interface{} {
return (*v)[i]
}
Expand Down
56 changes: 56 additions & 0 deletions data/nullable_vector.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func (v *nullableUint8Vector) Append(i interface{}) {
*v = append(*v, i.(*uint8))
}

func (v *nullableUint8Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableUint8Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -134,6 +138,10 @@ func (v *nullableUint16Vector) Append(i interface{}) {
*v = append(*v, i.(*uint16))
}

func (v *nullableUint16Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableUint16Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -225,6 +233,10 @@ func (v *nullableUint32Vector) Append(i interface{}) {
*v = append(*v, i.(*uint32))
}

func (v *nullableUint32Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableUint32Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -316,6 +328,10 @@ func (v *nullableUint64Vector) Append(i interface{}) {
*v = append(*v, i.(*uint64))
}

func (v *nullableUint64Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableUint64Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -407,6 +423,10 @@ func (v *nullableInt8Vector) Append(i interface{}) {
*v = append(*v, i.(*int8))
}

func (v *nullableInt8Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableInt8Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -498,6 +518,10 @@ func (v *nullableInt16Vector) Append(i interface{}) {
*v = append(*v, i.(*int16))
}

func (v *nullableInt16Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableInt16Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -589,6 +613,10 @@ func (v *nullableInt32Vector) Append(i interface{}) {
*v = append(*v, i.(*int32))
}

func (v *nullableInt32Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableInt32Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -680,6 +708,10 @@ func (v *nullableInt64Vector) Append(i interface{}) {
*v = append(*v, i.(*int64))
}

func (v *nullableInt64Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableInt64Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -771,6 +803,10 @@ func (v *nullableFloat32Vector) Append(i interface{}) {
*v = append(*v, i.(*float32))
}

func (v *nullableFloat32Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableFloat32Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -862,6 +898,10 @@ func (v *nullableFloat64Vector) Append(i interface{}) {
*v = append(*v, i.(*float64))
}

func (v *nullableFloat64Vector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableFloat64Vector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -953,6 +993,10 @@ func (v *nullableStringVector) Append(i interface{}) {
*v = append(*v, i.(*string))
}

func (v *nullableStringVector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableStringVector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -1044,6 +1088,10 @@ func (v *nullableBoolVector) Append(i interface{}) {
*v = append(*v, i.(*bool))
}

func (v *nullableBoolVector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableBoolVector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -1135,6 +1183,10 @@ func (v *nullableTimeTimeVector) Append(i interface{}) {
*v = append(*v, i.(*time.Time))
}

func (v *nullableTimeTimeVector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableTimeTimeVector) At(i int) interface{} {
return (*v)[i]
}
Expand Down Expand Up @@ -1226,6 +1278,10 @@ func (v *nullableJsonRawMessageVector) Append(i interface{}) {
*v = append(*v, i.(*json.RawMessage))
}

func (v *nullableJsonRawMessageVector) NilAt(i int) bool {
return (*v)[i] == nil
}

func (v *nullableJsonRawMessageVector) At(i int) interface{} {
return (*v)[i]
}
Expand Down
Loading

0 comments on commit d477c83

Please sign in to comment.