From 13fdd9dec2dc0325a6329604edffe701cd68390e Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Thu, 19 Nov 2026 11:03:15 -0700 Subject: [PATCH 1/2] Compare column types by identity when decoding untyped values parse_value runs once per value and picked the decoder with `==` and `<:` on the column's Julia type, each a runtime call; a text value went through about twenty of them. The identity checks now use `===`, a pointer comparison, text columns return first, and the subtype checks only see types no identity check matched. type_info also stops looking up the default registry when the connection's registry has the OID. Co-Authored-By: Claude Opus 5.5 --- src/api/types.jl | 69 +++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/src/api/types.jl b/src/api/types.jl index 9e772ab..bf218ef 100644 --- a/src/api/types.jl +++ b/src/api/types.jl @@ -241,7 +241,7 @@ end default_type_info(oid::Int) = get(DEFAULT_TYPE_REGISTRY, oid, TypeInfo(String, nothing)) -type_info(registry::Dict{Int, TypeInfo}, oid::Int) = get(registry, oid, default_type_info(oid)) +type_info(registry::Dict{Int, TypeInfo}, oid::Int) = get(() -> default_type_info(oid), registry, oid) @inline function juliatype(f, oid::Int, registry::Dict{Int, TypeInfo}) info = type_info(registry, oid) @@ -912,69 +912,72 @@ const FieldBytes = SubArray{UInt8, 1, Vector{UInt8}, Tuple{UnitRange{Int}}, true _field_text(val::String) = val _field_text(val::FieldBytes) = GC.@preserve val unsafe_string(pointer(val), length(val)) +# Runs once per value. `===` on types is a pointer comparison, where `==` and +# `<:` are runtime calls, so the identity checks come first and the subtype +# checks only see types no identity check matched. function parse_value(typeId::Int, val::Union{String, FieldBytes}, registry::Dict{Int, TypeInfo}) info = type_info(registry, typeId) if info.parser !== nothing return info.parser(_field_text(val), registry) end T = info.julia_type - if T == Bool + T === String && return _field_text(val) + if T === Bool return parse_boolean(val) - elseif T == Int16 + elseif T === Int16 return Parsers.parse(Int16, val) - elseif T == Int32 + elseif T === Int32 return Parsers.parse(Int32, val) - elseif T == Int64 + elseif T === Int64 return Parsers.parse(Int64, val) - elseif T == Cuint + elseif T === Cuint return Parsers.parse(Cuint, val) - elseif T == Float32 + elseif T === Float32 return Parsers.parse(Float32, val) - elseif T == Float64 + elseif T === Float64 return Parsers.parse(Float64, val) end # Text, lazy JSON and custom parsers own their bytes. Fixed-width scalar # decoders above can read directly from the row's bounded byte view. val = _field_text(val) - if T == Char + if T === Char return pg_parse_char(val) - elseif T == PGTimestamp + elseif T === PGTimestamp return pg_parse_timestamp(val) - elseif T == DateTime + elseif T === DateTime if typeId == 1184 return parse_timestamptz(val) end return pg_parse_datetime(val) - elseif T == UUID + elseif T === UUID return UUID(val) - elseif T == Date + elseif T === Date return pg_parse_date(val) - elseif T == Time + elseif T === Time return pg_parse_time(val) + elseif T === Vector{UInt8} + return decode_bytea(val) + elseif T === JSONType + return JSON.lazy(val) + elseif T === Vector{String} + return parse_array(val, String) + elseif T === Vector{Int16} + return parse_array(val, Int16) + elseif T === Vector{Int32} + return parse_array(val, Int32) + elseif T === Vector{Int64} + return parse_array(val, Int64) + elseif T === Vector{Float32} + return parse_array(val, Float32) + elseif T === Vector{Float64} + return parse_array(val, Float64) + elseif T === Vector{Bool} + return parse_array(val, Bool) elseif T <: Number || T <: Dates.TimeType # custom numeric/time registrations must supply a parser function: a # runtime-Type Parsers.parse here is unresolvable under --trim throw(PostgresInterfaceError("no parser registered for this type; pass a parser function when registering the type")) - elseif T == Vector{UInt8} - return decode_bytea(val) - elseif T == JSONType - return JSON.lazy(val) elseif T <: AbstractVector - if T == Vector{String} - return parse_array(val, String) - elseif T == Vector{Int16} - return parse_array(val, Int16) - elseif T == Vector{Int32} - return parse_array(val, Int32) - elseif T == Vector{Int64} - return parse_array(val, Int64) - elseif T == Vector{Float32} - return parse_array(val, Float32) - elseif T == Vector{Float64} - return parse_array(val, Float64) - elseif T == Vector{Bool} - return parse_array(val, Bool) - end return parse_array(val, String) end return val From 704333f29ccca8ca1d36f63d9cb07fae3fc874bd Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Thu, 19 Nov 2026 12:33:34 -0700 Subject: [PATCH 2/2] Test parse_value for registrations without a parser Text columns now return before the subtype checks, so no test reached the branches for other registered types; cover them, and the int2[], float4[] and bool[] decoders. Co-Authored-By: Claude Opus 5.5 --- test/runtests.jl | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 2711a38..ed88479 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -963,6 +963,19 @@ include("trim_compile_tests.jl") @test Postgres.API.parse_array_by_oid("{/var/log/x[1].txt,b}", 25, registry) == ["/var/log/x[1].txt", "b"] @test Postgres.API.ArrayParsing.parse_array("{1,2}", Int64) isa Vector{Int64} + @test Postgres.API.parse_value(1005, "{1,-2}", registry) == Int16[1, -2] + @test Postgres.API.parse_value(1021, "{1.5,NaN}", registry) isa Vector{Float32} + @test Postgres.API.parse_value(1000, "{t,f}", registry) == [true, false] + # types registered without a parser: other types get the text, other + # vector types get string elements, and numbers are an error + let reg = Dict(registry) + Postgres.API.register_type!(reg, 990001, Symbol) + Postgres.API.register_type!(reg, 990002, Vector{Symbol}) + Postgres.API.register_type!(reg, 990003, Int8) + @test Postgres.API.parse_value(990001, "abc", reg) == "abc" + @test Postgres.API.parse_value(990002, "{a,b}", reg) == ["a", "b"] + @test_throws Postgres.PostgresInterfaceError Postgres.API.parse_value(990003, "1", reg) + end rng = MersenneTwister(0x5097) for _ in 1:200