Repository navigation
Expanded Type Conversions #118
Closed
SeanTAllen
started this conversation in
Research
Replies: 1 comment
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Analysis of the expanded type conversions listed in the feature roadmap (item 11). The roadmap identified these PostgreSQL types as candidates for explicit handling:
numeric(1700) — arbitrary precisiondate(1082) — ISO formattimestamp(1114) /timestamptz(1184) — ISO formatuuid(2950) — standardized formatjson(114) /jsonb(3802) — JSON textbytea(17) — binary datainterval(1186) — durationvarchar(1043) /bpchar(1042) — character stringsCurrent Behavior
The driver converts 7 PostgreSQL type OIDs to native Pony types (bool, int2, int4, int8, float4, float8) and falls through to
Stringfor everything else. This means all of the types listed above already work — they arrive asStringvalues.Design Question
Should the driver add explicit handling for these types? Three options were considered:
Constrained types from Pony's
constrained_typesstdlib package — e.g., aUUIDconstrained type that validates format at construction. Rejected because the data comes from PostgreSQL, which has already validated it. Re-validating server output forces callers to handle aValidationFailurecase that can't happen.Lightweight wrapper classes — e.g.,
class val UUIDwrapping aStringwith no validation. Gives a distinct type inFieldDataTypesso callers can match onUUIDvsString. Preserves PostgreSQL type identity without re-validating.Leave as
String— the current behavior.Npgsql (F# / .NET) Precedent
Npgsql's approach was studied as a reference. Key findings:
uuidto .NET'sGuid,date/timestamptoDateTime,numerictodecimal,byteatobyte[]— all built-in platform types.NpgsqlDate,NpgsqlTime,NpgsqlTimestamp) because "they were problematic and seldom used." Users preferred built-in types.NpgsqlInterval, which exists because .NET'sTimeSpangenuinely cannot represent PostgreSQL's month/year interval components.NpgsqlDecimalfor arbitrary-precision numeric was rejected — "we don't want to be in the business of maintaining arbitrary-precision decimal implementations."Decision
Follow Npgsql's principle: only add explicit type conversions when there is a representative type in the Pony standard library.
Pony does not have built-in types for UUID, date, timestamp, decimal, interval, or JSON. Creating driver-specific wrapper types for all of these would add API surface the driver has to maintain, with questionable value — Npgsql tried this and walked it back.
The driver's job is to faithfully deliver what PostgreSQL sent. Domain modeling — deciding what
"2024-01-15"or"550e8400-e29b-41d4-a716-446655440000"means in the application — is the caller's responsibility at the application boundary.Result
bytea(17)Array[U8] valnumeric,date,timestamp,timestamptz,uuid,json,jsonb,intervalString(existing fall-through)varchar,bpcharString(existing fall-through)byteais the only type that gets a new conversion.FieldDataTypesgains one new variant:Array[U8] val.All reactions