feat: add duckdb functions for working with the narinfo dataset
Browse files- duckdb/init.sql +64 -0
duckdb/init.sql
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-- nixbase32: Encode binary blob to Nix base32 string
|
| 2 |
+
--
|
| 3 |
+
-- Nix uses a custom base32 alphabet: 0123456789abcdfghijklmnpqrsvwxyz
|
| 4 |
+
-- (excludes e, o, t, u)
|
| 5 |
+
--
|
| 6 |
+
-- The encoding is little-endian
|
| 7 |
+
-- Output length: ceil(input_bytes * 8 / 5)
|
| 8 |
+
-- 20 bytes (SHA1) -> 32 characters
|
| 9 |
+
-- 32 bytes (SHA256) -> 52 characters
|
| 10 |
+
--
|
| 11 |
+
-- Usage:
|
| 12 |
+
-- SELECT nixbase32(file_hash) FROM narinfos;
|
| 13 |
+
-- SELECT nixbase32(unhex('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'));
|
| 14 |
+
CREATE OR REPLACE MACRO nixbase32(input) AS (
|
| 15 |
+
SELECT string_agg(
|
| 16 |
+
'0123456789abcdfghijklmnpqrsvwxyz'[
|
| 17 |
+
((
|
| 18 |
+
(COALESCE(byte_arr[bit_pos // 8 + 1], 0) >> (bit_pos % 8)) |
|
| 19 |
+
(CASE WHEN (bit_pos % 8) > 3
|
| 20 |
+
THEN COALESCE(byte_arr[bit_pos // 8 + 2], 0) << (8 - (bit_pos % 8))
|
| 21 |
+
ELSE 0 END)
|
| 22 |
+
) & 31) + 1
|
| 23 |
+
],
|
| 24 |
+
'' ORDER BY n
|
| 25 |
+
)
|
| 26 |
+
FROM (
|
| 27 |
+
SELECT
|
| 28 |
+
n,
|
| 29 |
+
(out_len - 1 - n) * 5 as bit_pos,
|
| 30 |
+
byte_arr
|
| 31 |
+
FROM (
|
| 32 |
+
SELECT
|
| 33 |
+
((octet_length(input)::bigint * 8 + 4) // 5)::bigint as out_len,
|
| 34 |
+
list_transform(
|
| 35 |
+
generate_series(0::bigint, octet_length(input)::bigint - 1),
|
| 36 |
+
i -> (instr('0123456789abcdef', lower(hex(input))[i*2+1]) - 1) * 16 +
|
| 37 |
+
(instr('0123456789abcdef', lower(hex(input))[i*2+2]) - 1)
|
| 38 |
+
) as byte_arr
|
| 39 |
+
) params,
|
| 40 |
+
generate_series(0::bigint, out_len - 1) AS t(n)
|
| 41 |
+
)
|
| 42 |
+
);
|
| 43 |
+
|
| 44 |
+
-- narURL: Construct NAR URL path from file hash and compression type
|
| 45 |
+
--
|
| 46 |
+
-- Reconstructs the URL field from a narinfo, which is derived from:
|
| 47 |
+
-- nar/<nixbase32(file_hash)>.nar[.<compression_ext>]
|
| 48 |
+
--
|
| 49 |
+
-- Compression mapping:
|
| 50 |
+
-- '' or 'none' -> .nar
|
| 51 |
+
-- 'bzip2' -> .nar.bz2
|
| 52 |
+
-- 'xz' -> .nar.xz
|
| 53 |
+
-- 'zstd' -> .nar.zstd
|
| 54 |
+
--
|
| 55 |
+
-- Usage:
|
| 56 |
+
-- SELECT narURL(file_hash, compression) FROM narinfos;
|
| 57 |
+
CREATE OR REPLACE MACRO narURL(file_hash, compression) AS (
|
| 58 |
+
'nar/' || nixbase32(file_hash) || '.nar' ||
|
| 59 |
+
CASE
|
| 60 |
+
WHEN compression = '' OR compression = 'none' THEN ''
|
| 61 |
+
WHEN compression = 'bzip2' THEN '.bz2'
|
| 62 |
+
ELSE '.' || compression
|
| 63 |
+
END
|
| 64 |
+
);
|