Skip to content

fix uuid toString #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions api/src/values/DuckDBUUIDValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ export class DuckDBUUIDValue {
}

public toString(): string {
// UUID values are stored with their MSB flipped so their numeric ordering matches their string ordering.
const flipped = this.hugeint ^ 0x80000000000000000000000000000000n
// Truncate to 32 hex digits, then prepend with a hex 1, before converting to a hex string.
// This ensures the trailing 32 characters are the hex digits we want, left padded with zeros as needed.
const hex = ((this.hugeint & 0xffffffffffffffffffffffffffffffffn) | 0x100000000000000000000000000000000n).toString(16);
const hex = ((flipped & 0xffffffffffffffffffffffffffffffffn) | 0x100000000000000000000000000000000n).toString(16);
return `${hex.substring(1, 9)}-${hex.substring(9, 13)}-${hex.substring(13, 17)}-${hex.substring(17, 21)}-${hex.substring(21, 33)}`;
}

public static readonly Max = new DuckDBUUIDValue(2n ** 127n - 1n);
public static readonly Min = new DuckDBUUIDValue(-(2n ** 127n));
public static readonly Max = new DuckDBUUIDValue(2n ** 127n - 1n); // 7fffffffffffffffffffffffffffffff
public static readonly Min = new DuckDBUUIDValue(-(2n ** 127n)); // 80000000000000000000000000000000
}

export function uuidValue(hugeint: bigint): DuckDBUUIDValue {
Expand Down
5 changes: 2 additions & 3 deletions api/test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ import {
timestampTZValue,
timestampValue,
unionValue,
uuidValue,
version
} from '../src';

Expand Down Expand Up @@ -920,8 +919,8 @@ describe('api', () => {
assert.equal(unionValue('b', 'duck').toString(), 'duck');

// uuid
assert.equal(uuidValue(0n).toString(), '00000000-0000-0000-0000-000000000000');
assert.equal(uuidValue(2n ** 128n - 1n).toString(), 'ffffffff-ffff-ffff-ffff-ffffffffffff');
assert.equal(DuckDBUUIDValue.Min.toString(), '00000000-0000-0000-0000-000000000000');
assert.equal(DuckDBUUIDValue.Max.toString(), 'ffffffff-ffff-ffff-ffff-ffffffffffff');
});
test('date isFinite', () => {
assert.isTrue(DuckDBDateValue.Epoch.isFinite);
Expand Down