From 8b00d7dc45304e7b869f643891ae7d2e0d3b8999 Mon Sep 17 00:00:00 2001 From: Ben Baumgold Date: Sat, 27 Mar 2021 23:32:03 -0400 Subject: [PATCH 1/4] #70 fix bug preventing large floats from being displayed --- Project.toml | 2 +- src/TableView.jl | 11 +++++++++-- test/runtests.jl | 9 +++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 419534c..8e7d87b 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "TableView" uuid = "40c74d1a-b44c-5b06-a7c1-6cbea58ea978" -version = "0.6.7" +version = "0.6.8" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/src/TableView.jl b/src/TableView.jl index f6edf01..0ea81e0 100644 --- a/src/TableView.jl +++ b/src/TableView.jl @@ -7,7 +7,6 @@ using Observables: @map export showtable const ag_grid_imports = [] -const js_max_safe_int = Int128(2^53-1) function __init__() version = readchomp(joinpath(@__DIR__, "..", "ag-grid.version")) @@ -259,6 +258,14 @@ function _showtable_async!(w, schema, names, types, rows, coldefs, tablelength, onimport(w, handler) end +function _is_javascript_safe(x::Integer) + -Int128(2^53)-1 < x < Int128(2^53)-1 +end + +function _is_javascript_safe(x::AbstractFloat) + -2^53-1 < x < 2^53-1 +end + # directly write JSON instead of allocating temporary dicts etc function table2json(schema, rows, types; requested = nothing) io = IOBuffer() @@ -273,7 +280,7 @@ function table2json(schema, rows, types; requested = nothing) columnwriter = JSON.Writer.CompactContext(io) JSON.begin_object(columnwriter) Tables.eachcolumn(schema, row) do val, ind, name - if val isa Real && isfinite(val) && -js_max_safe_int < trunc(Int128, val) < js_max_safe_int + if val isa Real && isfinite(val) && _is_javascript_safe(val) JSON.show_pair(columnwriter, ser, name, val) elseif val === nothing || val === missing JSON.show_pair(columnwriter, ser, name, repr(val)) diff --git a/test/runtests.jl b/test/runtests.jl index cd933c2..3948cab 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -42,6 +42,15 @@ end @test firstrow["b"] == "9007199254740992" @test firstrow["c"] == "18014398509481984" end +@testset "large floats" begin + rows = Tables.table([1.0e50 1.0e100]) + names = [:a, :b] + types = [Float64, Float64] + json = TableView.table2json(Tables.Schema(names, types), rows, types) + firstrow = JSON.parse(json)[1] + @test firstrow["a"] == "1.0e50" + @test firstrow["b"] == "1.0e100" +end @testset "normal array" begin array = rand(10, 10) @test showtable(array) isa WebIO.Scope From 479d25dab8ea8dd64902e583771ce25e00bc5d6f Mon Sep 17 00:00:00 2001 From: Ben Baumgold Date: Sun, 28 Mar 2021 08:53:15 -0400 Subject: [PATCH 2/4] #70 prevent overflow --- src/TableView.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TableView.jl b/src/TableView.jl index 0ea81e0..74a650b 100644 --- a/src/TableView.jl +++ b/src/TableView.jl @@ -259,11 +259,11 @@ function _showtable_async!(w, schema, names, types, rows, coldefs, tablelength, end function _is_javascript_safe(x::Integer) - -Int128(2^53)-1 < x < Int128(2^53)-1 + -(Int64(2)^53-1) < x < Int64(2)^53-1 end function _is_javascript_safe(x::AbstractFloat) - -2^53-1 < x < 2^53-1 + -(Float64(2)^53-1) < x < Float64(2)^53-1 end # directly write JSON instead of allocating temporary dicts etc From 945c23210d19ab685f913522b7ae60f9214e2504 Mon Sep 17 00:00:00 2001 From: Ben Baumgold Date: Sun, 28 Mar 2021 08:55:34 -0400 Subject: [PATCH 3/4] #70 shorter --- src/TableView.jl | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/TableView.jl b/src/TableView.jl index 74a650b..6ce21e6 100644 --- a/src/TableView.jl +++ b/src/TableView.jl @@ -258,13 +258,8 @@ function _showtable_async!(w, schema, names, types, rows, coldefs, tablelength, onimport(w, handler) end -function _is_javascript_safe(x::Integer) - -(Int64(2)^53-1) < x < Int64(2)^53-1 -end - -function _is_javascript_safe(x::AbstractFloat) - -(Float64(2)^53-1) < x < Float64(2)^53-1 -end +_is_javascript_safe(x::Integer) = -(Int64(2)^53-1) < x < Int64(2)^53-1 +_is_javascript_safe(x::AbstractFloat) = -(Float64(2)^53-1) < x < Float64(2)^53-1 # directly write JSON instead of allocating temporary dicts etc function table2json(schema, rows, types; requested = nothing) From e76dd5c1b4cf26f3b59d1de3e609d385c799f7ad Mon Sep 17 00:00:00 2001 From: Ben Baumgold Date: Sun, 28 Mar 2021 10:34:43 -0400 Subject: [PATCH 4/4] #70 more clear --- src/TableView.jl | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/TableView.jl b/src/TableView.jl index 6ce21e6..aa3fd7b 100644 --- a/src/TableView.jl +++ b/src/TableView.jl @@ -258,8 +258,17 @@ function _showtable_async!(w, schema, names, types, rows, coldefs, tablelength, onimport(w, handler) end -_is_javascript_safe(x::Integer) = -(Int64(2)^53-1) < x < Int64(2)^53-1 -_is_javascript_safe(x::AbstractFloat) = -(Float64(2)^53-1) < x < Float64(2)^53-1 +function _is_javascript_safe(x::Integer) + min_safe_int = -(Int64(2)^53-1) + max_safe_int = Int64(2)^53-1 + min_safe_int < x < max_safe_int +end + +function _is_javascript_safe(x::AbstractFloat) + min_safe_float = -(Float64(2)^53-1) + max_safe_float = Float64(2)^53-1 + min_safe_float < x < max_safe_float +end # directly write JSON instead of allocating temporary dicts etc function table2json(schema, rows, types; requested = nothing)