diff --git a/src/tables.jl b/src/tables.jl index f8f4fce..cb4fc92 100644 --- a/src/tables.jl +++ b/src/tables.jl @@ -160,7 +160,17 @@ function Tables.getcolumn(r::Row, ::Type{T}, i::Int, nm::Symbol) where {T} end function Tables.getcolumn(r::Row, i::Int) - Tables.getcolumn(r, getquery(r).types[i], i, getquery(r).names[i]) + q = getquery(r) + T = q.types[i] + n = getfield(r, :rownumber) + # A call that dispatches on a runtime `Union` type searches the method cache for every value, + # so the column types that declared types and raw BLOBs give get static calls. + T === Union{Missing,Int64} && return getvalue(q, i, n, Union{Missing,Int64}) + T === Union{Missing,Float64} && return getvalue(q, i, n, Union{Missing,Float64}) + T === Union{Missing,String} && return getvalue(q, i, n, Union{Missing,String}) + T === Union{Missing,Vector{UInt8}} && return getvalue(q, i, n, Union{Missing,Vector{UInt8}}) + T === Any && return getvalue(q, i, n, Any) + return getvalue(q, i, n, T) end # non-strict values don't depend on the column type; skip dispatching on it Tables.getcolumn(r::Row{false}, i::Int) = getvalue(getquery(r), i, getfield(r, :rownumber), Any) diff --git a/test/runtests.jl b/test/runtests.jl index e24ae2f..64c8399 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1347,6 +1347,26 @@ end @test isequal(tbl.x, [1, "txt", UInt8[0x01, 0x02], missing]) end + @testset "Strict rows read each column type by index and by name" begin + db = SQLite.DB() + DBInterface.execute(db, "CREATE TABLE t (i INTEGER, f REAL, s TEXT, b BLOB, n NUMERIC, a BLOB)") + DBInterface.execute(db, "INSERT INTO t VALUES (1, 1.5, 'x', x'01', 2, 7), (NULL, NULL, NULL, NULL, 3, 'any')") + stmt = DBInterface.prepare(db, "SELECT * FROM t ORDER BY rowid") + rows = [(Tables.getcolumn(r, 1), r.f, r.s, r.b, r.n, r.a) for r in DBInterface.execute(stmt, (); strict = true)] + @test isequal(rows, [(1, 1.5, "x", UInt8[1], 2.0, 7), (missing, missing, missing, missing, 3.0, "any")]) + @test rows[1][1] isa Int64 && rows[1][5] isa Float64 + # a serialized Julia type takes the dynamically dispatched read + SQLite.load!((d = [Date(2020, 1, 2), missing],), db, "dates") + dq = DBInterface.execute(DBInterface.prepare(db, "SELECT d FROM dates ORDER BY rowid"), (); strict = true) + @test isequal([r.d for r in dq], [Date(2020, 1, 2), missing]) + DBInterface.execute(db, "INSERT INTO t (i, f, s) VALUES (2.5, 'y', x'02')") + for col in ("i", "f", "s") + q = DBInterface.execute(DBInterface.prepare(db, "SELECT $col FROM t ORDER BY rowid"), (); strict = true) + @test_throws ArgumentError [Tables.getcolumn(r, 1) for r in q] + end + close(db) + end + @testset "Strict values of another storage class convert only when exact" begin db = SQLite.DB() strict(sql) = columntable(DBInterface.execute(DBInterface.prepare(db, sql), (); strict = true))