Skip to content
Permalink
Browse files
add metadata to show method (#217)
  • Loading branch information
ericphanson committed Jun 22, 2021
1 parent 631f4da commit a43a235fea9c87130a49f9a99fbf952202e355fc
Showing 3 changed files with 68 additions and 0 deletions.
@@ -93,6 +93,7 @@ include("eltypes.jl")
include("table.jl")
include("write.jl")
include("append.jl")
include("show.jl")

const LZ4_FRAME_COMPRESSOR = LZ4FrameCompressor[]
const ZSTD_COMPRESSOR = ZstdCompressor[]
@@ -0,0 +1,37 @@
# 2-arg show: show schema and list # of metadata entries if non-zero
function Base.show(io::IO, table::Table)
ncols = length(Tables.columnnames(table))
print(io, "$(typeof(table)) with $(Tables.rowcount(table)) rows, $(ncols) columns,")
meta = getmetadata(table)
if meta !== nothing && !isempty(meta)
print(io, " ", length(meta), " metadata entries,")
end
sch = Tables.schema(table)
print(io, " and schema:\n")
show(IOContext(io, :print_schema_header => false), sch)
return nothing
end

# 3-arg show: show schema and show metadata entries adaptively according to `displaysize`
function Base.show(io::IO, mime::MIME"text/plain", table::Table)
limit = get(io, :limit, false)::Bool
display_rows, display_cols = displaysize(io)
ncols = length(Tables.columnnames(table))
meta = getmetadata(table)
if meta !== nothing
display_rows -= 1 # decrement for metadata header line
display_rows -= min(length(meta), 2) # decrement so we can show at least 2 lines of metadata
end
print(io, "$(typeof(table)) with $(Tables.rowcount(table)) rows, $(ncols) columns, and ")
sch = Tables.schema(table)
print(io, "schema:\n")
schema_context = IOContext(io, :print_schema_header => false, :displaysize => (max(display_rows, 3), display_cols))
schema_str = sprint(show, mime, sch; context=schema_context)
print(io, schema_str)
display_rows -= (count("\n", schema_str) + 1) # decrement for number of lines printed
if meta !== nothing
print(io, "\n\nwith metadata given by a ")
show(IOContext(io, :displaysize => (max(display_rows, 5), display_cols)), mime, meta)
end
return nothing
end
@@ -15,6 +15,7 @@
# limitations under the License.

using Test, Arrow, Tables, Dates, PooledArrays, TimeZones, UUIDs, CategoricalArrays, DataAPI
using Random: randstring

include(joinpath(dirname(pathof(Arrow)), "ArrowTypes/test/tests.jl"))
include(joinpath(dirname(pathof(Arrow)), "../test/testtables.jl"))
@@ -355,6 +356,35 @@ tbl = Arrow.Table(Arrow.tobuffer(t))
@test eltype(tbl.col1) == VersionNumber
end

@testset "`show`" begin
table = (; a = 1:5, b = fill(1.0, 5))
arrow_table = Arrow.Table(Arrow.tobuffer(table))
# 2 and 3-arg show with no metadata
for outer str in (sprint(show, arrow_table),
sprint(show, MIME"text/plain"(), arrow_table))
@test length(str) < 100
@test occursin("5 rows", str)
@test occursin("2 columns", str)
@test occursin("Int", str)
@test occursin("Float64", str)
@test !occursin("metadata entries", str)
end

# 2-arg show with metadata
big_dict = Dict((randstring(rand(5:10)) => randstring(rand(1:3)) for _ = 1:100))
Arrow.setmetadata!(arrow_table, big_dict)
str2 = sprint(show, arrow_table)
@test length(str2) > length(str)
@test length(str2) < 200
@test occursin("metadata entries", str2)

# 3-arg show with metadata
str3 = sprint(show, MIME"text/plain"(), arrow_table; context = IOContext(IOBuffer(), :displaysize => (24, 100), :limit=>true))
@test length(str3) < 1000
# some but not too many `=>`'s for printing the metadata
@test 5 < length(collect(eachmatch(r"=>", str3))) < 20

end
end # @testset "misc"

end

0 comments on commit a43a235

Please sign in to comment.