| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2026 Tiger Data, Inc. | ||
| 3 | * Licensed under the PostgreSQL License. See LICENSE for details. | ||
| 4 | * | ||
| 5 | * typeinfo.c - type descriptors for prism-indexed columns | ||
| 6 | * | ||
| 7 | * One descriptor per indexable type, handed to the access method by the | ||
| 8 | * opclass. See typeinfo.h for why the opclass is the source. | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include <postgres.h> | ||
| 12 | |||
| 13 | #include <access/relation.h> | ||
| 14 | #include <fmgr.h> | ||
| 15 | #include <utils/relcache.h> | ||
| 16 | |||
| 17 | #include "support_pg.h" | ||
| 18 | #include "typeinfo.h" | ||
| 19 | #include "types/vec16.h" | ||
| 20 | #include "types/vec32.h" | ||
| 21 | |||
| 22 | /* ---------------------------------------------------------------- | ||
| 23 | * Datum unwrapping | ||
| 24 | * ---------------------------------------------------------------- */ | ||
| 25 | |||
| 26 | static const void * | ||
| 27 | 780389 | vector_unwrap(Datum d, Dimension *dim) | |
| 28 | { | ||
| 29 | 780389 | Vec32 *v = DatumGetVec32(d); | |
| 30 | |||
| 31 | 780389 | *dim = (Dimension)v->dim; | |
| 32 | 780389 | return VEC32_DATA(v); | |
| 33 | } | ||
| 34 | |||
| 35 | static const void * | ||
| 36 | 13051 | halfvec_unwrap(Datum d, Dimension *dim) | |
| 37 | { | ||
| 38 | 13051 | Vec16 *v = DatumGetVec16(d); | |
| 39 | |||
| 40 | 13051 | *dim = (Dimension)v->dim; | |
| 41 | 13051 | return VEC16_DATA(v); | |
| 42 | } | ||
| 43 | |||
| 44 | /* ---------------------------------------------------------------- | ||
| 45 | * Descriptors | ||
| 46 | * | ||
| 47 | * vec16 costs one widening pass per value read and buys a heap half the | ||
| 48 | * size, which is what an exact rerank reads: at 768d a vector row is 3080 | ||
| 49 | * bytes and fits 2 to an 8 kB page against vec16's 1544 and 5. | ||
| 50 | * ---------------------------------------------------------------- */ | ||
| 51 | |||
| 52 | static const PrismIndexTypeInfo type_info_vector = { | ||
| 53 | .name = "vec32", | ||
| 54 | .max_dimensions = VEC32_MAX_DIM, | ||
| 55 | .centroid_format = PRISM_CENTROID_FMT_FLOAT, | ||
| 56 | .ops = &vs_f32_type_ops, | ||
| 57 | .unwrap = vector_unwrap, | ||
| 58 | }; | ||
| 59 | |||
| 60 | static const PrismIndexTypeInfo type_info_halfvec = { | ||
| 61 | .name = "vec16", | ||
| 62 | /* vec16_typmod_in enforces the same ceiling as vec32. */ | ||
| 63 | .max_dimensions = VEC32_MAX_DIM, | ||
| 64 | .centroid_format = PRISM_CENTROID_FMT_HALF, | ||
| 65 | .ops = &vs_f16_type_ops, | ||
| 66 | .unwrap = halfvec_unwrap, | ||
| 67 | }; | ||
| 68 | |||
| 69 | /* ---------------------------------------------------------------- | ||
| 70 | * Opclass support functions (PRISM_TYPE_INFO_PROC) | ||
| 71 | * ---------------------------------------------------------------- */ | ||
| 72 | |||
| 73 | 8 | PG_FUNCTION_INFO_V1(prism_vec32_support); | |
| 74 | |||
| 75 | Datum | ||
| 76 | ✗ | prism_vec32_support(PG_FUNCTION_ARGS) | |
| 77 | { | ||
| 78 | ✗ | PG_RETURN_POINTER(&type_info_vector); | |
| 79 | } | ||
| 80 | |||
| 81 | 25 | PG_FUNCTION_INFO_V1(prism_vec16_support); | |
| 82 | |||
| 83 | Datum | ||
| 84 | 38 | prism_vec16_support(PG_FUNCTION_ARGS) | |
| 85 | { | ||
| 86 | 38 | PG_RETURN_POINTER(&type_info_halfvec); | |
| 87 | } | ||
| 88 | |||
| 89 | const PrismIndexTypeInfo * | ||
| 90 | 670 | prism_index_type_info(Relation index) | |
| 91 | { | ||
| 92 | /* | ||
| 93 | * Optional, pgvector-style: an opclass with no descriptor indexes | ||
| 94 | * `vec32`. That leaves the vec32 opclasses' SQL untouched, and an index | ||
| 95 | * built before this support function existed keeps working. | ||
| 96 | */ | ||
| 97 |
2/2✓ Branch 1 taken 38 times.
✓ Branch 2 taken 632 times.
|
670 | if (!OidIsValid(index_getprocid(index, 1, PRISM_TYPE_INFO_PROC))) |
| 98 | return &type_info_vector; | ||
| 99 | |||
| 100 | 38 | FmgrInfo *procinfo = index_getprocinfo(index, 1, PRISM_TYPE_INFO_PROC); | |
| 101 | |||
| 102 | 38 | return (const PrismIndexTypeInfo *)DatumGetPointer( | |
| 103 | FunctionCall0Coll(procinfo, InvalidOid)); | ||
| 104 | } | ||
| 105 |