mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 02:48:50 +00:00
Fix PostgreSQL TIME and TIMETZ binary format handling (#22354)
## Summary - Fixes binary format handling for PostgreSQL TIME and TIMETZ data types - Resolves issue where time values were returned as garbled binary data with null bytes ## Problem When PostgreSQL returns TIME or TIMETZ columns in binary format, Bun.sql was not properly converting them from their binary representation (microseconds since midnight) to readable time strings. This resulted in corrupted output like `\u0000\u0000\u0000\u0000\u0076` instead of proper time values like `09:00:00`. ## Solution Added proper binary format decoding for: - **TIME (OID 1083)**: Converts 8 bytes of microseconds since midnight to `HH:MM:SS.ffffff` format - **TIMETZ (OID 1266)**: Converts 8 bytes of microseconds + 4 bytes of timezone offset to `HH:MM:SS.ffffff±HH:MM` format ## Changes - Added binary format handling in `src/sql/postgres/DataCell.zig` for TIME and TIMETZ types - Added `InvalidTimeFormat` error to `AnyPostgresError` error set - Properly formats microseconds with trailing zero removal - Handles timezone offsets correctly (PostgreSQL uses negative values for positive UTC offsets) ## Test plan Added comprehensive tests in `test/js/bun/sql/postgres-time.test.ts`: - [x] TIME and TIMETZ column values with various formats - [x] NULL handling - [x] Array types (TIME[] and TIMETZ[]) - [x] JSONB structures containing time strings - [x] Verification that no binary/null bytes appear in output All tests pass locally with PostgreSQL. 🤖 Generated with [Claude Code](https://claude.ai/code) --------- Co-authored-by: Claude Bot <claude-bot@bun.sh> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -12,6 +12,7 @@ pub const AnyPostgresError = error{
|
||||
InvalidQueryBinding,
|
||||
InvalidServerKey,
|
||||
InvalidServerSignature,
|
||||
InvalidTimeFormat,
|
||||
JSError,
|
||||
MultidimensionalArrayNotSupportedYet,
|
||||
NullsInArrayNotSupportedYet,
|
||||
@@ -90,6 +91,7 @@ pub fn postgresErrorToJS(globalObject: *jsc.JSGlobalObject, message: ?[]const u8
|
||||
error.InvalidQueryBinding => "ERR_POSTGRES_INVALID_QUERY_BINDING",
|
||||
error.InvalidServerKey => "ERR_POSTGRES_INVALID_SERVER_KEY",
|
||||
error.InvalidServerSignature => "ERR_POSTGRES_INVALID_SERVER_SIGNATURE",
|
||||
error.InvalidTimeFormat => "ERR_POSTGRES_INVALID_TIME_FORMAT",
|
||||
error.MultidimensionalArrayNotSupportedYet => "ERR_POSTGRES_MULTIDIMENSIONAL_ARRAY_NOT_SUPPORTED_YET",
|
||||
error.NullsInArrayNotSupportedYet => "ERR_POSTGRES_NULLS_IN_ARRAY_NOT_SUPPORTED_YET",
|
||||
error.Overflow => "ERR_POSTGRES_OVERFLOW",
|
||||
|
||||
@@ -601,6 +601,38 @@ pub fn fromBytes(binary: bool, bigint: bool, oid: types.Tag, bytes: []const u8,
|
||||
return SQLDataCell{ .tag = .date, .value = .{ .date = try str.parseDate(globalObject) } };
|
||||
}
|
||||
},
|
||||
.time, .timetz => |tag| {
|
||||
if (bytes.len == 0) {
|
||||
return SQLDataCell{ .tag = .null, .value = .{ .null = 0 } };
|
||||
}
|
||||
if (binary) {
|
||||
if (tag == .time and bytes.len == 8) {
|
||||
// PostgreSQL sends time as microseconds since midnight in binary format
|
||||
const microseconds = @byteSwap(@as(i64, @bitCast(bytes[0..8].*)));
|
||||
|
||||
// Use C++ helper for formatting
|
||||
var buffer: [32]u8 = undefined;
|
||||
const len = Postgres__formatTime(microseconds, &buffer, buffer.len);
|
||||
|
||||
return SQLDataCell{ .tag = .string, .value = .{ .string = bun.String.cloneUTF8(buffer[0..len]).value.WTFStringImpl }, .free_value = 1 };
|
||||
} else if (tag == .timetz and bytes.len == 12) {
|
||||
// PostgreSQL sends timetz as microseconds since midnight (8 bytes) + timezone offset in seconds (4 bytes)
|
||||
const microseconds = @byteSwap(@as(i64, @bitCast(bytes[0..8].*)));
|
||||
const tz_offset_seconds = @byteSwap(@as(i32, @bitCast(bytes[8..12].*)));
|
||||
|
||||
// Use C++ helper for formatting with timezone
|
||||
var buffer: [48]u8 = undefined;
|
||||
const len = Postgres__formatTimeTz(microseconds, tz_offset_seconds, &buffer, buffer.len);
|
||||
|
||||
return SQLDataCell{ .tag = .string, .value = .{ .string = bun.String.cloneUTF8(buffer[0..len]).value.WTFStringImpl }, .free_value = 1 };
|
||||
} else {
|
||||
return error.InvalidBinaryData;
|
||||
}
|
||||
} else {
|
||||
// Text format - just return as string
|
||||
return SQLDataCell{ .tag = .string, .value = .{ .string = if (bytes.len > 0) bun.String.cloneUTF8(bytes).value.WTFStringImpl else null }, .free_value = 1 };
|
||||
}
|
||||
},
|
||||
|
||||
.bytea => {
|
||||
if (binary) {
|
||||
@@ -951,6 +983,10 @@ pub const Putter = struct {
|
||||
|
||||
const debug = bun.Output.scoped(.Postgres, .visible);
|
||||
|
||||
// External C++ formatting functions
|
||||
extern fn Postgres__formatTime(microseconds: i64, buffer: [*]u8, bufferSize: usize) usize;
|
||||
extern fn Postgres__formatTimeTz(microseconds: i64, tzOffsetSeconds: i32, buffer: [*]u8, bufferSize: usize) usize;
|
||||
|
||||
const PostgresCachedStructure = @import("../shared/CachedStructure.zig");
|
||||
const protocol = @import("./PostgresProtocol.zig");
|
||||
const std = @import("std");
|
||||
|
||||
Reference in New Issue
Block a user