Skip to content

Commit 9041cbf

Browse files
authored
Add reading metadata from Arrow.Table (#481)
Partially solves #337 for `Arrow.Table`.
1 parent fc8b899 commit 9041cbf

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

src/table.jl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,53 @@ See [the official Arrow documentation for more details on custom application met
363363
getmetadata(t::Table) = getfield(t, :metadata)[]
364364
getmetadata(::Any) = nothing
365365

366+
DataAPI.metadatasupport(::Type{Table}) = (read=true, write=false)
367+
DataAPI.colmetadatasupport(::Type{Table}) = (read=true, write=false)
368+
369+
function DataAPI.metadata(t::Table, key::AbstractString; style::Bool=false)
370+
meta = getmetadata(t)[key]
371+
return style ? (meta, :default) : meta
372+
end
373+
374+
function DataAPI.metadata(t::Table, key::AbstractString, default; style::Bool=false)
375+
meta = getmetadata(t)
376+
if meta !== nothing
377+
haskey(meta, key) && return style ? meta[key] : (meta[key], :default)
378+
end
379+
return style ? (default, :default) : default
380+
end
381+
382+
function DataAPI.metadatakeys(t::Table)
383+
meta = getmetadata(t)
384+
meta === nothing && return ()
385+
return keys(meta)
386+
end
387+
388+
function DataAPI.colmetadata(t::Table, col, key::AbstractString; style::Bool=false)
389+
meta = getmetadata(t[col])[key]
390+
return style ? (meta, :default) : meta
391+
end
392+
393+
function DataAPI.colmetadata(t::Table, col, key::AbstractString, default; style::Bool=false)
394+
meta = getmetadata(t[col])
395+
if meta !== nothing
396+
haskey(meta, key) && return style ? (meta[key], :default) : meta[key]
397+
end
398+
return style ? (default, :default) : default
399+
end
400+
401+
function DataAPI.colmetadatakeys(t::Table, col)
402+
meta = getmetadata(t[col])
403+
meta === nothing && return ()
404+
return keys(meta)
405+
end
406+
407+
function DataAPI.colmetadatakeys(t::Table)
408+
return (col => DataAPI.colmetadatakeys(t, col) for
409+
col in Tables.columnnames(t) if
410+
getmetadata(t[col]) !== nothing)
411+
end
412+
366413
Tables.istable(::Table) = true
367414
Tables.columnaccess(::Table) = true
368415
Tables.columns(t::Table) = Tables.CopiedColumns(t)

test/runtests.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,4 +1070,44 @@ end
10701070
@test tbl.col16[1] == Dates.Time(0, 0, 0)
10711071
end
10721072
end # @testset "misc"
1073+
1074+
@testset "DataAPI.metadata" begin
1075+
df = DataFrame(a=1, b=2, c=3)
1076+
for i in 1:2
1077+
io = IOBuffer()
1078+
if i == 1 # skip writing metadata in the first iteration
1079+
Arrow.write(io, df)
1080+
else
1081+
Arrow.write(io, df, metadata=metadata(df), colmetadata=colmetadata(df))
1082+
end
1083+
seekstart(io)
1084+
tbl = Arrow.Table(io)
1085+
1086+
@test DataAPI.metadatasupport(typeof(tbl)) == (read=true, write=false)
1087+
@test metadata(tbl) == metadata(df)
1088+
@test metadata(tbl; style=true) == metadata(df; style=true)
1089+
@test_throws Exception metadata(tbl, "xyz")
1090+
@test metadata(tbl, "xyz", "something") == "something"
1091+
@test metadata(tbl, "xyz", "something"; style=true) == ("something", :default)
1092+
@test metadatakeys(tbl) == metadatakeys(df)
1093+
1094+
@test DataAPI.colmetadatasupport(typeof(tbl)) == (read=true, write=false)
1095+
@test colmetadata(tbl) == colmetadata(df)
1096+
@test colmetadata(tbl; style=true) == colmetadata(df; style=true)
1097+
@test_throws MethodError colmetadata(tbl, "xyz")
1098+
@test_throws KeyError colmetadata(tbl, :xyz)
1099+
@test colmetadata(tbl, :b) == colmetadata(df, :b)
1100+
@test_throws MethodError colmetadata(tbl, :b, "xyz")
1101+
@test colmetadata(tbl, :b, "xyz", "something") == "something"
1102+
@test colmetadata(tbl, :b, "xyz", "something"; style=true) == ("something", :default)
1103+
@test Set(colmetadatakeys(tbl)) == Set(colmetadatakeys(df))
1104+
1105+
# add metadata for the second iteration
1106+
metadata!(df, "tkey", "tvalue")
1107+
metadata!(df, "tkey2", "tvalue2")
1108+
colmetadata!(df, :a, "ackey", "acvalue")
1109+
colmetadata!(df, :a, "ackey2", "acvalue2")
1110+
colmetadata!(df, :c, "cckey", "ccvalue")
1111+
end
1112+
end # @testset "DataAPI.metadata"
10731113
end

0 commit comments

Comments
 (0)