From f40368a8773852e65ea69811d4b138b3c65a505e Mon Sep 17 00:00:00 2001 From: Jarrett Revels Date: Tue, 27 Jul 2021 09:20:30 -0400 Subject: [PATCH] support Legolas.Row (de)serialization as an Arrow column element (#16) --- Project.toml | 4 ++-- src/rows.jl | 16 ++++++++++++++++ test/runtests.jl | 2 ++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 87c7ab9..3ef3e49 100644 --- a/Project.toml +++ b/Project.toml @@ -1,14 +1,14 @@ name = "Legolas" uuid = "741b9549-f6ed-4911-9fbf-4a1c0c97f0cd" authors = ["Beacon Biosignals, Inc."] -version = "0.2.1" +version = "0.2.2" [deps] Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45" Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" [compat] -Arrow = "1.5" +Arrow = "1.6.2" DataFrames = "1" Tables = "1.4" julia = "1.3" diff --git a/src/rows.jl b/src/rows.jl index dca3eb7..3c993b0 100644 --- a/src/rows.jl +++ b/src/rows.jl @@ -277,3 +277,19 @@ macro row(schema_expr, fields...) Legolas.Row{$schema_type} end end + +# Support (de)serialization as an Arrow column value via Arrow.ArrowTypes overloads. +# +# Note that this only really works in relatively simple cases; rely on this at your own peril. +# See https://github.com/JuliaData/Arrow.jl/issues/230 for more details. +# +# Note also that the limited support here that DOES work participates in SemVer, +# e.g. if we break this in future Legolas versions we should treat it as a breaking +# change and bump version numbers accordingly. + +const LEGOLAS_ROW_ARROW_NAME = Symbol("JuliaLang.Legolas.Row") +Arrow.ArrowTypes.arrowname(::Type{<:Legolas.Row}) = LEGOLAS_ROW_ARROW_NAME +Arrow.ArrowTypes.ArrowType(::Type{Legolas.Row{_,F}}) where {_,F} = Tuple{String,Int,F} +Arrow.ArrowTypes.toarrow(row::Legolas.Row{S}) where {S} = (String(Legolas.schema_name(S)), Legolas.schema_version(S), getfield(row, :fields)) +Arrow.ArrowTypes.JuliaType(::Val{LEGOLAS_ROW_ARROW_NAME}, ::Any) = Legolas.Row +Arrow.ArrowTypes.fromarrow(::Type{<:Legolas.Row}, name, version, fields) = Legolas.Row(Legolas.Schema(name, version), fields) diff --git a/test/runtests.jl b/test/runtests.jl index c7dd5b2..5036087 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -122,6 +122,8 @@ end @test r[1] === 3 @test string(r) == "Row(Schema(\"bar@1\"), (z = 3, x = 1, y = 2))" + tbl = Arrow.Table(Arrow.tobuffer((x=[r],))) + @test r === tbl.x[1] long_row = Row(Schema("bar", 1), (x=1, y=2, z=zeros(100, 100))) @test length(sprint(show, long_row; context=(:limit => true))) < 200