From e43dc91301cfdc744ff8e5f79207783cd4e5b24d Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Thu, 19 Nov 2026 12:23:58 -0700 Subject: [PATCH 1/2] Read strict query rows without dispatching on each column type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reading a strict query row by index or by name (`row.x`, `Tables.getcolumn(row, i)`) called `getvalue` with the column's runtime `Union` type. Julia's fast dispatch cache can't hold such calls, so every value took about 0.5 µs to find its method. Iterating 1M rows of one INTEGER column with `strict=true` took about 860 ms, against 160 ms without `strict`. `getcolumn` now compares the column type with the types that declared column types and raw BLOBs give (Int64, Float64, String, Vector{UInt8}, and Any) and calls `getvalue` statically for those. Other types, such as deserialized Julia values, still dispatch at run time. Column tables and other schema-based sinks already called `getvalue` statically. Co-Authored-By: Claude Opus 5.5 --- src/tables.jl | 12 +++++++++++- test/runtests.jl | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) 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..414287c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1347,6 +1347,22 @@ 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 + 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)) From 27c4ffc74e8c636de6d12738aec68fe9de42b416 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Thu, 19 Nov 2026 12:52:32 -0700 Subject: [PATCH 2/2] Test a strict row read of a serialized Julia type A Date column takes the runtime-dispatched read in getcolumn, the path for column types other than the ones it reads statically. Co-Authored-By: Claude Opus 5.5 --- test/runtests.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 414287c..64c8399 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1355,6 +1355,10 @@ end 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)