arrow-julia icon indicating copy to clipboard operation
arrow-julia copied to clipboard

[ArrowTypes] add `@arrow_record`?

Open ericphanson opened this issue 2 years ago • 0 comments

@kleinschmidt and I came up with

using Arrow: ArrowTypes
macro arrow_record(T1)
    T = esc(T1)
    name = :(Symbol("JuliaLang.", @__MODULE__, ".", string(parentmodule($T), '.', nameof($T))))
    return quote
       ArrowTypes.arrowname(::Type{$T}) = $name
       ArrowTypes.ArrowType(::Type{$T}) = fieldtypes($T)
       ArrowTypes.toarrow(obj::$T) = ntuple(i -> getfield(obj, i), fieldcount($T))
       ArrowTypes.JuliaType(::Val{$name}, ::Any) = $T
       ArrowTypes.fromarrow(::Type{$T}, args...) = $T(args...)
    end
end

as a quick way to declare that a non-parametrized concrete type should be serialized like a "record" (a la StructTypes.StructType). (Or maybe it should be called @arrow_fields or something?). In other words, we want it to just serialize out as a tuple of its fields with a metadata tag. This could be used as, e.g.

using Arrow

struct Foo
    bar::String
    x::Int
end

@arrow_record Foo

my_foo =  Foo("hi", 1)
table = [(; foo_col=my_foo)]
roundtripped_table = Arrow.Table(Arrow.tobuffer(table))
using Test
@test roundtripped_table.foo_col[1] == my_foo # true

ericphanson avatar May 17 '22 21:05 ericphanson