mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-22 20:15:46 +08:00
Compare commits
4 Commits
ivfflat-st
...
tinyint
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d977caa47d | ||
|
|
cc87960109 | ||
|
|
c6d1d8bc2c | ||
|
|
4914511cf6 |
8
.github/workflows/build.yml
vendored
8
.github/workflows/build.yml
vendored
@@ -8,8 +8,6 @@ jobs:
|
|||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
include:
|
||||||
- postgres: 17
|
|
||||||
os: ubuntu-22.04
|
|
||||||
- postgres: 16
|
- postgres: 16
|
||||||
os: ubuntu-22.04
|
os: ubuntu-22.04
|
||||||
- postgres: 15
|
- postgres: 15
|
||||||
@@ -23,7 +21,7 @@ jobs:
|
|||||||
- postgres: 11
|
- postgres: 11
|
||||||
os: ubuntu-20.04
|
os: ubuntu-20.04
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v3
|
||||||
- uses: ankane/setup-postgres@v1
|
- uses: ankane/setup-postgres@v1
|
||||||
with:
|
with:
|
||||||
postgres-version: ${{ matrix.postgres }}
|
postgres-version: ${{ matrix.postgres }}
|
||||||
@@ -45,7 +43,7 @@ jobs:
|
|||||||
runs-on: macos-latest
|
runs-on: macos-latest
|
||||||
if: ${{ !startsWith(github.ref_name, 'windows') }}
|
if: ${{ !startsWith(github.ref_name, 'windows') }}
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v3
|
||||||
- uses: ankane/setup-postgres@v1
|
- uses: ankane/setup-postgres@v1
|
||||||
with:
|
with:
|
||||||
postgres-version: 14
|
postgres-version: 14
|
||||||
@@ -67,7 +65,7 @@ jobs:
|
|||||||
runs-on: windows-latest
|
runs-on: windows-latest
|
||||||
if: ${{ !startsWith(github.ref_name, 'mac') }}
|
if: ${{ !startsWith(github.ref_name, 'mac') }}
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v3
|
||||||
- uses: ankane/setup-postgres@v1
|
- uses: ankane/setup-postgres@v1
|
||||||
with:
|
with:
|
||||||
postgres-version: 14
|
postgres-version: 14
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
## 0.5.1 (2023-10-10)
|
## 0.5.1 (unreleased)
|
||||||
|
|
||||||
- Improved performance of HNSW index builds
|
- Added check for MVCC-compliant snapshot for HNSW index scans
|
||||||
- Added check for MVCC-compliant snapshot for index scans
|
- Improved performance of index scans for IVFFlat after updates and deletes
|
||||||
|
|
||||||
## 0.5.0 (2023-08-28)
|
## 0.5.0 (2023-08-28)
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"name": "vector",
|
"name": "vector",
|
||||||
"abstract": "Open-source vector similarity search for Postgres",
|
"abstract": "Open-source vector similarity search for Postgres",
|
||||||
"description": "Supports L2 distance, inner product, and cosine distance",
|
"description": "Supports L2 distance, inner product, and cosine distance",
|
||||||
"version": "0.5.1",
|
"version": "0.5.0",
|
||||||
"maintainer": [
|
"maintainer": [
|
||||||
"Andrew Kane <andrew@ankane.org>"
|
"Andrew Kane <andrew@ankane.org>"
|
||||||
],
|
],
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
"vector": {
|
"vector": {
|
||||||
"file": "sql/vector.sql",
|
"file": "sql/vector.sql",
|
||||||
"docfile": "README.md",
|
"docfile": "README.md",
|
||||||
"version": "0.5.1",
|
"version": "0.5.0",
|
||||||
"abstract": "Open-source vector similarity search for Postgres"
|
"abstract": "Open-source vector similarity search for Postgres"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
6
Makefile
6
Makefile
@@ -1,10 +1,10 @@
|
|||||||
EXTENSION = vector
|
EXTENSION = vector
|
||||||
EXTVERSION = 0.5.1
|
EXTVERSION = 0.5.0
|
||||||
|
|
||||||
MODULE_big = vector
|
MODULE_big = vector
|
||||||
DATA = $(wildcard sql/*--*.sql)
|
DATA = $(wildcard sql/*--*.sql)
|
||||||
OBJS = src/hnsw.o src/hnswbuild.o src/hnswinsert.o src/hnswscan.o src/hnswutils.o src/hnswvacuum.o src/ivfbuild.o src/ivfflat.o src/ivfinsert.o src/ivfkmeans.o src/ivfscan.o src/ivfutils.o src/ivfvacuum.o src/vector.o
|
OBJS = src/hnsw.o src/hnswbuild.o src/hnswinsert.o src/hnswscan.o src/hnswutils.o src/hnswvacuum.o src/ivfbuild.o src/ivfflat.o src/ivfinsert.o src/ivfkmeans.o src/ivfscan.o src/ivfutils.o src/ivfvacuum.o src/tinyint.o src/vector.o
|
||||||
HEADERS = src/vector.h
|
HEADERS = src/tinyint.h src/vector.h
|
||||||
|
|
||||||
TESTS = $(wildcard test/sql/*.sql)
|
TESTS = $(wildcard test/sql/*.sql)
|
||||||
REGRESS = $(patsubst test/sql/%.sql,%,$(TESTS))
|
REGRESS = $(patsubst test/sql/%.sql,%,$(TESTS))
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
EXTENSION = vector
|
EXTENSION = vector
|
||||||
EXTVERSION = 0.5.1
|
EXTVERSION = 0.5.0
|
||||||
|
|
||||||
OBJS = src\hnsw.obj src\hnswbuild.obj src\hnswinsert.obj src\hnswscan.obj src\hnswutils.obj src\hnswvacuum.obj src\ivfbuild.obj src\ivfflat.obj src\ivfinsert.obj src\ivfkmeans.obj src\ivfscan.obj src\ivfutils.obj src\ivfvacuum.obj src\vector.obj
|
OBJS = src\hnsw.obj src\hnswbuild.obj src\hnswinsert.obj src\hnswscan.obj src\hnswutils.obj src\hnswvacuum.obj src\ivfbuild.obj src\ivfflat.obj src\ivfinsert.obj src\ivfkmeans.obj src\ivfscan.obj src\ivfutils.obj src\ivfvacuum.obj src\tinyint.obj src\vector.obj
|
||||||
HEADERS = src\vector.h
|
HEADERS = src\tinyint.h src\vector.h
|
||||||
|
|
||||||
REGRESS = btree cast copy functions input ivfflat_cosine ivfflat_ip ivfflat_l2 ivfflat_options ivfflat_unlogged
|
REGRESS = btree cast copy functions input ivfflat_cosine ivfflat_ip ivfflat_l2 ivfflat_options ivfflat_unlogged
|
||||||
REGRESS_OPTS = --inputdir=test --load-extension=$(EXTENSION)
|
REGRESS_OPTS = --inputdir=test --load-extension=$(EXTENSION)
|
||||||
@@ -56,7 +56,7 @@ install:
|
|||||||
copy $(EXTENSION).control "$(SHAREDIR)\extension"
|
copy $(EXTENSION).control "$(SHAREDIR)\extension"
|
||||||
copy sql\$(EXTENSION)--*.sql "$(SHAREDIR)\extension"
|
copy sql\$(EXTENSION)--*.sql "$(SHAREDIR)\extension"
|
||||||
mkdir "$(INCLUDEDIR_SERVER)\extension\$(EXTENSION)"
|
mkdir "$(INCLUDEDIR_SERVER)\extension\$(EXTENSION)"
|
||||||
copy $(HEADERS) "$(INCLUDEDIR_SERVER)\extension\$(EXTENSION)"
|
for %f in ($(HEADERS)) do copy %f "$(INCLUDEDIR_SERVER)\extension\$(EXTENSION)"
|
||||||
|
|
||||||
installcheck:
|
installcheck:
|
||||||
"$(BINDIR)\pg_regress" --bindir="$(BINDIR)" $(REGRESS_OPTS) $(REGRESS)
|
"$(BINDIR)\pg_regress" --bindir="$(BINDIR)" $(REGRESS_OPTS) $(REGRESS)
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ Compile and install the extension (supports Postgres 11+)
|
|||||||
|
|
||||||
```sh
|
```sh
|
||||||
cd /tmp
|
cd /tmp
|
||||||
git clone --branch v0.5.1 https://github.com/pgvector/pgvector.git
|
git clone --branch v0.5.0 https://github.com/pgvector/pgvector.git
|
||||||
cd pgvector
|
cd pgvector
|
||||||
make
|
make
|
||||||
make install # may need sudo
|
make install # may need sudo
|
||||||
@@ -509,7 +509,7 @@ Then use `nmake` to build:
|
|||||||
|
|
||||||
```cmd
|
```cmd
|
||||||
set "PGROOT=C:\Program Files\PostgreSQL\15"
|
set "PGROOT=C:\Program Files\PostgreSQL\15"
|
||||||
git clone --branch v0.5.1 https://github.com/pgvector/pgvector.git
|
git clone --branch v0.5.0 https://github.com/pgvector/pgvector.git
|
||||||
cd pgvector
|
cd pgvector
|
||||||
nmake /F Makefile.win
|
nmake /F Makefile.win
|
||||||
nmake /F Makefile.win install
|
nmake /F Makefile.win install
|
||||||
@@ -530,7 +530,7 @@ This adds pgvector to the [Postgres image](https://hub.docker.com/_/postgres) (r
|
|||||||
You can also build the image manually:
|
You can also build the image manually:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
git clone --branch v0.5.1 https://github.com/pgvector/pgvector.git
|
git clone --branch v0.5.0 https://github.com/pgvector/pgvector.git
|
||||||
cd pgvector
|
cd pgvector
|
||||||
docker build --build-arg PG_MAJOR=15 -t myuser/pgvector .
|
docker build --build-arg PG_MAJOR=15 -t myuser/pgvector .
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,2 +1,67 @@
|
|||||||
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
|
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
|
||||||
\echo Use "ALTER EXTENSION vector UPDATE TO '0.5.1'" to load this file. \quit
|
\echo Use "ALTER EXTENSION vector UPDATE TO '0.5.1'" to load this file. \quit
|
||||||
|
|
||||||
|
-- tinyint
|
||||||
|
|
||||||
|
CREATE TYPE tinyint;
|
||||||
|
|
||||||
|
CREATE FUNCTION tinyint_in(cstring, oid, integer) RETURNS tinyint
|
||||||
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE FUNCTION tinyint_out(tinyint) RETURNS cstring
|
||||||
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE FUNCTION tinyint_recv(internal, oid, integer) RETURNS tinyint
|
||||||
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE FUNCTION tinyint_send(tinyint) RETURNS bytea
|
||||||
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE TYPE tinyint (
|
||||||
|
INPUT = tinyint_in,
|
||||||
|
OUTPUT = tinyint_out,
|
||||||
|
RECEIVE = tinyint_recv,
|
||||||
|
SEND = tinyint_send,
|
||||||
|
INTERNALLENGTH = 1,
|
||||||
|
PASSEDBYVALUE,
|
||||||
|
ALIGNMENT = char
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE FUNCTION integer_to_tinyint(integer, integer, boolean) RETURNS tinyint
|
||||||
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE FUNCTION numeric_to_tinyint(numeric, integer, boolean) RETURNS tinyint
|
||||||
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE CAST (integer AS tinyint)
|
||||||
|
WITH FUNCTION integer_to_tinyint(integer, integer, boolean) AS IMPLICIT;
|
||||||
|
|
||||||
|
CREATE CAST (numeric AS tinyint)
|
||||||
|
WITH FUNCTION numeric_to_tinyint(numeric, integer, boolean) AS IMPLICIT;
|
||||||
|
|
||||||
|
CREATE FUNCTION l2_distance(tinyint[], tinyint[]) RETURNS float8
|
||||||
|
AS 'MODULE_PATHNAME', 'tinyint_l2_distance' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE FUNCTION inner_product(tinyint[], tinyint[]) RETURNS float8
|
||||||
|
AS 'MODULE_PATHNAME', 'tinyint_inner_product' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE FUNCTION cosine_distance(tinyint[], tinyint[]) RETURNS float8
|
||||||
|
AS 'MODULE_PATHNAME', 'tinyint_cosine_distance' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE FUNCTION tinyint_negative_inner_product(tinyint[], tinyint[]) RETURNS float8
|
||||||
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE OPERATOR <-> (
|
||||||
|
LEFTARG = tinyint[], RIGHTARG = tinyint[], PROCEDURE = l2_distance,
|
||||||
|
COMMUTATOR = '<->'
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE OPERATOR <#> (
|
||||||
|
LEFTARG = tinyint[], RIGHTARG = tinyint[], PROCEDURE = tinyint_negative_inner_product,
|
||||||
|
COMMUTATOR = '<#>'
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE OPERATOR <=> (
|
||||||
|
LEFTARG = tinyint[], RIGHTARG = tinyint[], PROCEDURE = cosine_distance,
|
||||||
|
COMMUTATOR = '<=>'
|
||||||
|
);
|
||||||
|
|||||||
@@ -290,3 +290,68 @@ CREATE OPERATOR CLASS vector_cosine_ops
|
|||||||
OPERATOR 1 <=> (vector, vector) FOR ORDER BY float_ops,
|
OPERATOR 1 <=> (vector, vector) FOR ORDER BY float_ops,
|
||||||
FUNCTION 1 vector_negative_inner_product(vector, vector),
|
FUNCTION 1 vector_negative_inner_product(vector, vector),
|
||||||
FUNCTION 2 vector_norm(vector);
|
FUNCTION 2 vector_norm(vector);
|
||||||
|
|
||||||
|
-- tinyint
|
||||||
|
|
||||||
|
CREATE TYPE tinyint;
|
||||||
|
|
||||||
|
CREATE FUNCTION tinyint_in(cstring, oid, integer) RETURNS tinyint
|
||||||
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE FUNCTION tinyint_out(tinyint) RETURNS cstring
|
||||||
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE FUNCTION tinyint_recv(internal, oid, integer) RETURNS tinyint
|
||||||
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE FUNCTION tinyint_send(tinyint) RETURNS bytea
|
||||||
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE TYPE tinyint (
|
||||||
|
INPUT = tinyint_in,
|
||||||
|
OUTPUT = tinyint_out,
|
||||||
|
RECEIVE = tinyint_recv,
|
||||||
|
SEND = tinyint_send,
|
||||||
|
INTERNALLENGTH = 1,
|
||||||
|
PASSEDBYVALUE,
|
||||||
|
ALIGNMENT = char
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE FUNCTION integer_to_tinyint(integer, integer, boolean) RETURNS tinyint
|
||||||
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE FUNCTION numeric_to_tinyint(numeric, integer, boolean) RETURNS tinyint
|
||||||
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE CAST (integer AS tinyint)
|
||||||
|
WITH FUNCTION integer_to_tinyint(integer, integer, boolean) AS IMPLICIT;
|
||||||
|
|
||||||
|
CREATE CAST (numeric AS tinyint)
|
||||||
|
WITH FUNCTION numeric_to_tinyint(numeric, integer, boolean) AS IMPLICIT;
|
||||||
|
|
||||||
|
CREATE FUNCTION l2_distance(tinyint[], tinyint[]) RETURNS float8
|
||||||
|
AS 'MODULE_PATHNAME', 'tinyint_l2_distance' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE FUNCTION inner_product(tinyint[], tinyint[]) RETURNS float8
|
||||||
|
AS 'MODULE_PATHNAME', 'tinyint_inner_product' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE FUNCTION cosine_distance(tinyint[], tinyint[]) RETURNS float8
|
||||||
|
AS 'MODULE_PATHNAME', 'tinyint_cosine_distance' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE FUNCTION tinyint_negative_inner_product(tinyint[], tinyint[]) RETURNS float8
|
||||||
|
AS 'MODULE_PATHNAME' LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
|
||||||
|
|
||||||
|
CREATE OPERATOR <-> (
|
||||||
|
LEFTARG = tinyint[], RIGHTARG = tinyint[], PROCEDURE = l2_distance,
|
||||||
|
COMMUTATOR = '<->'
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE OPERATOR <#> (
|
||||||
|
LEFTARG = tinyint[], RIGHTARG = tinyint[], PROCEDURE = tinyint_negative_inner_product,
|
||||||
|
COMMUTATOR = '<#>'
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE OPERATOR <=> (
|
||||||
|
LEFTARG = tinyint[], RIGHTARG = tinyint[], PROCEDURE = cosine_distance,
|
||||||
|
COMMUTATOR = '<=>'
|
||||||
|
);
|
||||||
|
|||||||
@@ -112,13 +112,11 @@ typedef struct HnswCandidate
|
|||||||
{
|
{
|
||||||
HnswElement element;
|
HnswElement element;
|
||||||
float distance;
|
float distance;
|
||||||
bool closer;
|
|
||||||
} HnswCandidate;
|
} HnswCandidate;
|
||||||
|
|
||||||
typedef struct HnswNeighborArray
|
typedef struct HnswNeighborArray
|
||||||
{
|
{
|
||||||
int length;
|
int length;
|
||||||
bool closerSet;
|
|
||||||
HnswCandidate *items;
|
HnswCandidate *items;
|
||||||
} HnswNeighborArray;
|
} HnswNeighborArray;
|
||||||
|
|
||||||
|
|||||||
115
src/hnswutils.c
115
src/hnswutils.c
@@ -139,7 +139,6 @@ HnswInitNeighbors(HnswElement element, int m)
|
|||||||
a = &element->neighbors[lc];
|
a = &element->neighbors[lc];
|
||||||
a->length = 0;
|
a->length = 0;
|
||||||
a->items = palloc(sizeof(HnswCandidate) * lm);
|
a->items = palloc(sizeof(HnswCandidate) * lm);
|
||||||
a->closerSet = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -693,34 +692,6 @@ HnswSearchLayer(Datum q, List *ep, int ef, int lc, Relation index, FmgrInfo *pro
|
|||||||
return w;
|
return w;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Compare candidate distances
|
|
||||||
*/
|
|
||||||
static int
|
|
||||||
#if PG_VERSION_NUM >= 130000
|
|
||||||
CompareCandidateDistances(const ListCell *a, const ListCell *b)
|
|
||||||
#else
|
|
||||||
CompareCandidateDistances(const void *a, const void *b)
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
HnswCandidate *hca = lfirst((ListCell *) a);
|
|
||||||
HnswCandidate *hcb = lfirst((ListCell *) b);
|
|
||||||
|
|
||||||
if (hca->distance < hcb->distance)
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
if (hca->distance > hcb->distance)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (hca->element < hcb->element)
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
if (hca->element > hcb->element)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Calculate the distance between elements
|
* Calculate the distance between elements
|
||||||
*/
|
*/
|
||||||
@@ -777,77 +748,33 @@ CheckElementCloser(HnswCandidate * e, List *r, int lc, FmgrInfo *procinfo, Oid c
|
|||||||
* Algorithm 4 from paper
|
* Algorithm 4 from paper
|
||||||
*/
|
*/
|
||||||
static List *
|
static List *
|
||||||
SelectNeighbors(List *c, int m, int lc, FmgrInfo *procinfo, Oid collation, HnswElement e2, HnswCandidate * newCandidate, HnswCandidate * *pruned, bool sortCandidates)
|
SelectNeighbors(List *c, int m, int lc, FmgrInfo *procinfo, Oid collation, HnswCandidate * *pruned)
|
||||||
{
|
{
|
||||||
List *r = NIL;
|
List *r = NIL;
|
||||||
List *w = list_copy(c);
|
List *w = list_copy(c);
|
||||||
pairingheap *wd;
|
pairingheap *wd;
|
||||||
bool mustCalculate = !e2->neighbors[lc].closerSet;
|
|
||||||
List *added = NIL;
|
|
||||||
bool removedAny = false;
|
|
||||||
|
|
||||||
if (list_length(w) <= m)
|
if (list_length(w) <= m)
|
||||||
return w;
|
return w;
|
||||||
|
|
||||||
wd = pairingheap_allocate(CompareNearestCandidates, NULL);
|
wd = pairingheap_allocate(CompareNearestCandidates, NULL);
|
||||||
|
|
||||||
/* Ensure order of candidates is deterministic for closer caching */
|
|
||||||
if (sortCandidates)
|
|
||||||
list_sort(w, CompareCandidateDistances);
|
|
||||||
|
|
||||||
while (list_length(w) > 0 && list_length(r) < m)
|
while (list_length(w) > 0 && list_length(r) < m)
|
||||||
{
|
{
|
||||||
/* Assumes w is already ordered desc */
|
/* Assumes w is already ordered desc */
|
||||||
HnswCandidate *e = llast(w);
|
HnswCandidate *e = llast(w);
|
||||||
|
bool closer;
|
||||||
|
|
||||||
w = list_delete_last(w);
|
w = list_delete_last(w);
|
||||||
|
|
||||||
/* Use previous state of r and wd to skip work when possible */
|
closer = CheckElementCloser(e, r, lc, procinfo, collation);
|
||||||
if (mustCalculate)
|
|
||||||
e->closer = CheckElementCloser(e, r, lc, procinfo, collation);
|
|
||||||
else if (list_length(added) > 0)
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
* If the current candidate was closer, we only need to compare it
|
|
||||||
* with the other candidates that we have added.
|
|
||||||
*/
|
|
||||||
if (e->closer)
|
|
||||||
{
|
|
||||||
e->closer = CheckElementCloser(e, added, lc, procinfo, collation);
|
|
||||||
|
|
||||||
if (!e->closer)
|
if (closer)
|
||||||
removedAny = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
* If we have removed any candidates from closer, a candidate
|
|
||||||
* that was not closer earlier might now be.
|
|
||||||
*/
|
|
||||||
if (removedAny)
|
|
||||||
{
|
|
||||||
e->closer = CheckElementCloser(e, r, lc, procinfo, collation);
|
|
||||||
if (e->closer)
|
|
||||||
added = lappend(added, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (e == newCandidate)
|
|
||||||
{
|
|
||||||
e->closer = CheckElementCloser(e, r, lc, procinfo, collation);
|
|
||||||
if (e->closer)
|
|
||||||
added = lappend(added, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e->closer)
|
|
||||||
r = lappend(r, e);
|
r = lappend(r, e);
|
||||||
else
|
else
|
||||||
pairingheap_add(wd, &(CreatePairingHeapNode(e)->ph_node));
|
pairingheap_add(wd, &(CreatePairingHeapNode(e)->ph_node));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Cached value can only be used in future if sorted deterministically */
|
|
||||||
e2->neighbors[lc].closerSet = sortCandidates;
|
|
||||||
|
|
||||||
/* Keep pruned connections */
|
/* Keep pruned connections */
|
||||||
while (!pairingheap_is_empty(wd) && list_length(r) < m)
|
while (!pairingheap_is_empty(wd) && list_length(r) < m)
|
||||||
r = lappend(r, ((HnswPairingHeapNode *) pairingheap_remove_first(wd))->inner);
|
r = lappend(r, ((HnswPairingHeapNode *) pairingheap_remove_first(wd))->inner);
|
||||||
@@ -901,6 +828,28 @@ AddConnections(HnswElement element, List *neighbors, int m, int lc)
|
|||||||
a->items[a->length++] = *((HnswCandidate *) lfirst(lc2));
|
a->items[a->length++] = *((HnswCandidate *) lfirst(lc2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Compare candidate distances
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
#if PG_VERSION_NUM >= 130000
|
||||||
|
CompareCandidateDistances(const ListCell *a, const ListCell *b)
|
||||||
|
#else
|
||||||
|
CompareCandidateDistances(const void *a, const void *b)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
HnswCandidate *hca = lfirst((ListCell *) a);
|
||||||
|
HnswCandidate *hcb = lfirst((ListCell *) b);
|
||||||
|
|
||||||
|
if (hca->distance < hcb->distance)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (hca->distance > hcb->distance)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Update connections
|
* Update connections
|
||||||
*/
|
*/
|
||||||
@@ -954,12 +903,13 @@ HnswUpdateConnection(HnswElement element, HnswCandidate * hc, int m, int lc, int
|
|||||||
{
|
{
|
||||||
List *c = NIL;
|
List *c = NIL;
|
||||||
|
|
||||||
/* Add candidates */
|
/* Add and sort candidates */
|
||||||
for (int i = 0; i < currentNeighbors->length; i++)
|
for (int i = 0; i < currentNeighbors->length; i++)
|
||||||
c = lappend(c, ¤tNeighbors->items[i]);
|
c = lappend(c, ¤tNeighbors->items[i]);
|
||||||
c = lappend(c, &hc2);
|
c = lappend(c, &hc2);
|
||||||
|
list_sort(c, CompareCandidateDistances);
|
||||||
|
|
||||||
SelectNeighbors(c, m, lc, procinfo, collation, hc->element, &hc2, &pruned, true);
|
SelectNeighbors(c, m, lc, procinfo, collation, &pruned);
|
||||||
|
|
||||||
/* Should not happen */
|
/* Should not happen */
|
||||||
if (pruned == NULL)
|
if (pruned == NULL)
|
||||||
@@ -1058,12 +1008,7 @@ HnswInsertElement(HnswElement element, HnswElement entryPoint, Relation index, F
|
|||||||
else
|
else
|
||||||
lw = w;
|
lw = w;
|
||||||
|
|
||||||
/*
|
neighbors = SelectNeighbors(lw, lm, lc, procinfo, collation, NULL);
|
||||||
* Candidates are sorted, but not deterministically. Could set
|
|
||||||
* sortCandidates to true for in-memory builds to enable closer
|
|
||||||
* caching, but there does not seem to be a difference in performance.
|
|
||||||
*/
|
|
||||||
neighbors = SelectNeighbors(lw, lm, lc, procinfo, collation, element, NULL, NULL, false);
|
|
||||||
|
|
||||||
AddConnections(element, neighbors, lm, lc);
|
AddConnections(element, neighbors, lm, lc);
|
||||||
|
|
||||||
|
|||||||
@@ -287,7 +287,7 @@ InsertTuples(Relation index, IvfflatBuildState * buildstate, ForkNumber forkNum)
|
|||||||
#else
|
#else
|
||||||
TupleTableSlot *slot = MakeSingleTupleTableSlot(buildstate->tupdesc);
|
TupleTableSlot *slot = MakeSingleTupleTableSlot(buildstate->tupdesc);
|
||||||
#endif
|
#endif
|
||||||
TupleDesc tupdesc = IvfflatTupleDesc(index);
|
TupleDesc tupdesc = RelationGetDescr(index);
|
||||||
|
|
||||||
UpdateProgress(PROGRESS_CREATEIDX_SUBPHASE, PROGRESS_IVFFLAT_PHASE_LOAD);
|
UpdateProgress(PROGRESS_CREATEIDX_SUBPHASE, PROGRESS_IVFFLAT_PHASE_LOAD);
|
||||||
|
|
||||||
@@ -339,8 +339,6 @@ InsertTuples(Relation index, IvfflatBuildState * buildstate, ForkNumber forkNum)
|
|||||||
/* Set the start and insert pages */
|
/* Set the start and insert pages */
|
||||||
IvfflatUpdateList(index, buildstate->listInfo[i], insertPage, InvalidBlockNumber, startPage, forkNum);
|
IvfflatUpdateList(index, buildstate->listInfo[i], insertPage, InvalidBlockNumber, startPage, forkNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
FreeTupleDesc(tupdesc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -246,6 +246,8 @@ typedef struct IvfflatScanOpaqueData
|
|||||||
int probes;
|
int probes;
|
||||||
int dimensions;
|
int dimensions;
|
||||||
bool first;
|
bool first;
|
||||||
|
Buffer buf;
|
||||||
|
ItemPointerData heaptid;
|
||||||
|
|
||||||
/* Sorting */
|
/* Sorting */
|
||||||
Tuplesortstate *sortstate;
|
Tuplesortstate *sortstate;
|
||||||
@@ -287,7 +289,6 @@ void IvfflatInitPage(Buffer buf, Page page);
|
|||||||
void IvfflatInitRegisterPage(Relation index, Buffer *buf, Page *page, GenericXLogState **state);
|
void IvfflatInitRegisterPage(Relation index, Buffer *buf, Page *page, GenericXLogState **state);
|
||||||
void IvfflatInit(void);
|
void IvfflatInit(void);
|
||||||
PGDLLEXPORT void IvfflatParallelBuildMain(dsm_segment *seg, shm_toc *toc);
|
PGDLLEXPORT void IvfflatParallelBuildMain(dsm_segment *seg, shm_toc *toc);
|
||||||
TupleDesc IvfflatTupleDesc(Relation index);
|
|
||||||
|
|
||||||
/* Index access methods */
|
/* Index access methods */
|
||||||
IndexBuildResult *ivfflatbuild(Relation heap, Relation index, IndexInfo *indexInfo);
|
IndexBuildResult *ivfflatbuild(Relation heap, Relation index, IndexInfo *indexInfo);
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heap_tid, R
|
|||||||
originalInsertPage = insertPage;
|
originalInsertPage = insertPage;
|
||||||
|
|
||||||
/* Form tuple */
|
/* Form tuple */
|
||||||
itup = index_form_tuple(IvfflatTupleDesc(index), &value, isnull);
|
itup = index_form_tuple(RelationGetDescr(index), &value, isnull);
|
||||||
itup->t_tid = *heap_tid;
|
itup->t_tid = *heap_tid;
|
||||||
|
|
||||||
/* Get tuple size */
|
/* Get tuple size */
|
||||||
|
|||||||
@@ -143,6 +143,10 @@ GetScanItems(IndexScanDesc scan, Datum value)
|
|||||||
bool isnull;
|
bool isnull;
|
||||||
ItemId itemid = PageGetItemId(page, offno);
|
ItemId itemid = PageGetItemId(page, offno);
|
||||||
|
|
||||||
|
/* Skip dead tuples */
|
||||||
|
if (scan->ignore_killed_tuples && ItemIdIsDead(itemid))
|
||||||
|
continue;
|
||||||
|
|
||||||
itup = (IndexTuple) PageGetItem(page, itemid);
|
itup = (IndexTuple) PageGetItem(page, itemid);
|
||||||
datum = index_getattr(itup, 1, tupdesc, &isnull);
|
datum = index_getattr(itup, 1, tupdesc, &isnull);
|
||||||
|
|
||||||
@@ -157,6 +161,8 @@ GetScanItems(IndexScanDesc scan, Datum value)
|
|||||||
slot->tts_isnull[0] = false;
|
slot->tts_isnull[0] = false;
|
||||||
slot->tts_values[1] = PointerGetDatum(&itup->t_tid);
|
slot->tts_values[1] = PointerGetDatum(&itup->t_tid);
|
||||||
slot->tts_isnull[1] = false;
|
slot->tts_isnull[1] = false;
|
||||||
|
slot->tts_values[2] = Int32GetDatum((int) searchPage);
|
||||||
|
slot->tts_isnull[2] = false;
|
||||||
ExecStoreVirtualTuple(slot);
|
ExecStoreVirtualTuple(slot);
|
||||||
|
|
||||||
tuplesort_puttupleslot(so->sortstate, slot);
|
tuplesort_puttupleslot(so->sortstate, slot);
|
||||||
@@ -181,6 +187,55 @@ GetScanItems(IndexScanDesc scan, Datum value)
|
|||||||
tuplesort_performsort(so->sortstate);
|
tuplesort_performsort(so->sortstate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Mark prior tuple as dead
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
MarkPriorTupleDead(IndexScanDesc scan)
|
||||||
|
{
|
||||||
|
IvfflatScanOpaque so = (IvfflatScanOpaque) scan->opaque;
|
||||||
|
Buffer buf = so->buf;
|
||||||
|
Page page;
|
||||||
|
OffsetNumber maxoffno;
|
||||||
|
|
||||||
|
/* Safety check */
|
||||||
|
if (!BufferIsValid(so->buf) || !ItemPointerIsValid(&so->heaptid))
|
||||||
|
return;
|
||||||
|
|
||||||
|
/* Only a shared locked is needed for ItemIdMarkDead */
|
||||||
|
LockBuffer(buf, BUFFER_LOCK_SHARE);
|
||||||
|
page = BufferGetPage(buf);
|
||||||
|
maxoffno = PageGetMaxOffsetNumber(page);
|
||||||
|
|
||||||
|
for (OffsetNumber offno = FirstOffsetNumber; offno <= maxoffno; offno = OffsetNumberNext(offno))
|
||||||
|
{
|
||||||
|
ItemId itemid = PageGetItemId(page, offno);
|
||||||
|
IndexTuple itup = (IndexTuple) PageGetItem(page, itemid);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Find tuple. Since buffer has been pinned, tuple cannot have been
|
||||||
|
* vacuumed (and heap TID reused).
|
||||||
|
*/
|
||||||
|
if (ItemPointerEquals(&itup->t_tid, &so->heaptid))
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Make sure tuple has not already been marked dead to avoid extra
|
||||||
|
* WAL if wal_log_hints or data checksums enabled
|
||||||
|
*/
|
||||||
|
if (!ItemIdIsDead(itemid))
|
||||||
|
{
|
||||||
|
ItemIdMarkDead(itemid);
|
||||||
|
MarkBufferDirtyHint(buf, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Unlock buffer */
|
||||||
|
LockBuffer(buf, BUFFER_LOCK_UNLOCK);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Prepare for an index scan
|
* Prepare for an index scan
|
||||||
*/
|
*/
|
||||||
@@ -206,7 +261,9 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
|
|||||||
probes = lists;
|
probes = lists;
|
||||||
|
|
||||||
so = (IvfflatScanOpaque) palloc(offsetof(IvfflatScanOpaqueData, lists) + probes * sizeof(IvfflatScanList));
|
so = (IvfflatScanOpaque) palloc(offsetof(IvfflatScanOpaqueData, lists) + probes * sizeof(IvfflatScanList));
|
||||||
|
so->buf = InvalidBuffer;
|
||||||
so->first = true;
|
so->first = true;
|
||||||
|
ItemPointerSetInvalid(&so->heaptid);
|
||||||
so->probes = probes;
|
so->probes = probes;
|
||||||
so->dimensions = dimensions;
|
so->dimensions = dimensions;
|
||||||
|
|
||||||
@@ -217,12 +274,13 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys)
|
|||||||
|
|
||||||
/* Create tuple description for sorting */
|
/* Create tuple description for sorting */
|
||||||
#if PG_VERSION_NUM >= 120000
|
#if PG_VERSION_NUM >= 120000
|
||||||
so->tupdesc = CreateTemplateTupleDesc(2);
|
so->tupdesc = CreateTemplateTupleDesc(3);
|
||||||
#else
|
#else
|
||||||
so->tupdesc = CreateTemplateTupleDesc(2, false);
|
so->tupdesc = CreateTemplateTupleDesc(3, false);
|
||||||
#endif
|
#endif
|
||||||
TupleDescInitEntry(so->tupdesc, (AttrNumber) 1, "distance", FLOAT8OID, -1, 0);
|
TupleDescInitEntry(so->tupdesc, (AttrNumber) 1, "distance", FLOAT8OID, -1, 0);
|
||||||
TupleDescInitEntry(so->tupdesc, (AttrNumber) 2, "heaptid", TIDOID, -1, 0);
|
TupleDescInitEntry(so->tupdesc, (AttrNumber) 2, "heaptid", TIDOID, -1, 0);
|
||||||
|
TupleDescInitEntry(so->tupdesc, (AttrNumber) 3, "indexblkno", INT4OID, -1, 0);
|
||||||
|
|
||||||
/* Prep sort */
|
/* Prep sort */
|
||||||
so->sortstate = tuplesort_begin_heap(so->tupdesc, 1, attNums, sortOperators, sortCollations, nullsFirstFlags, work_mem, NULL, false);
|
so->sortstate = tuplesort_begin_heap(so->tupdesc, 1, attNums, sortOperators, sortCollations, nullsFirstFlags, work_mem, NULL, false);
|
||||||
@@ -254,6 +312,7 @@ ivfflatrescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
so->first = true;
|
so->first = true;
|
||||||
|
ItemPointerSetInvalid(&so->heaptid);
|
||||||
pairingheap_reset(so->listQueue);
|
pairingheap_reset(so->listQueue);
|
||||||
|
|
||||||
if (keys && scan->numberOfKeys > 0)
|
if (keys && scan->numberOfKeys > 0)
|
||||||
@@ -288,11 +347,6 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir)
|
|||||||
if (scan->orderByData == NULL)
|
if (scan->orderByData == NULL)
|
||||||
elog(ERROR, "cannot scan ivfflat index without order");
|
elog(ERROR, "cannot scan ivfflat index without order");
|
||||||
|
|
||||||
/* Requires MVCC-compliant snapshot as not able to pin during sorting */
|
|
||||||
/* https://www.postgresql.org/docs/current/index-locking.html */
|
|
||||||
if (!IsMVCCSnapshot(scan->xs_snapshot))
|
|
||||||
elog(ERROR, "non-MVCC snapshots are not supported with ivfflat");
|
|
||||||
|
|
||||||
if (scan->orderByData->sk_flags & SK_ISNULL)
|
if (scan->orderByData->sk_flags & SK_ISNULL)
|
||||||
value = PointerGetDatum(InitVector(so->dimensions));
|
value = PointerGetDatum(InitVector(so->dimensions));
|
||||||
else
|
else
|
||||||
@@ -316,10 +370,17 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir)
|
|||||||
if (value != scan->orderByData->sk_argument)
|
if (value != scan->orderByData->sk_argument)
|
||||||
pfree(DatumGetPointer(value));
|
pfree(DatumGetPointer(value));
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Mark prior tuple as dead */
|
||||||
|
if (scan->kill_prior_tuple)
|
||||||
|
MarkPriorTupleDead(scan);
|
||||||
|
}
|
||||||
|
|
||||||
if (tuplesort_gettupleslot(so->sortstate, true, false, so->slot, NULL))
|
if (tuplesort_gettupleslot(so->sortstate, true, false, so->slot, NULL))
|
||||||
{
|
{
|
||||||
ItemPointer heaptid = (ItemPointer) DatumGetPointer(slot_getattr(so->slot, 2, &so->isnull));
|
ItemPointer heaptid = (ItemPointer) DatumGetPointer(slot_getattr(so->slot, 2, &so->isnull));
|
||||||
|
BlockNumber indexblkno = DatumGetInt32(slot_getattr(so->slot, 3, &so->isnull));
|
||||||
|
|
||||||
#if PG_VERSION_NUM >= 120000
|
#if PG_VERSION_NUM >= 120000
|
||||||
scan->xs_heaptid = *heaptid;
|
scan->xs_heaptid = *heaptid;
|
||||||
@@ -327,6 +388,21 @@ ivfflatgettuple(IndexScanDesc scan, ScanDirection dir)
|
|||||||
scan->xs_ctup.t_self = *heaptid;
|
scan->xs_ctup.t_self = *heaptid;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Keep track of info needed to mark tuple as dead */
|
||||||
|
so->heaptid = *heaptid;
|
||||||
|
|
||||||
|
/* Unpin buffer */
|
||||||
|
if (BufferIsValid(so->buf))
|
||||||
|
ReleaseBuffer(so->buf);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* An index scan must maintain a pin on the index page holding the
|
||||||
|
* item last returned by amgettuple
|
||||||
|
*
|
||||||
|
* https://www.postgresql.org/docs/current/index-locking.html
|
||||||
|
*/
|
||||||
|
so->buf = ReadBuffer(scan->indexRelation, indexblkno);
|
||||||
|
|
||||||
scan->xs_recheckorderby = false;
|
scan->xs_recheckorderby = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -342,6 +418,10 @@ ivfflatendscan(IndexScanDesc scan)
|
|||||||
{
|
{
|
||||||
IvfflatScanOpaque so = (IvfflatScanOpaque) scan->opaque;
|
IvfflatScanOpaque so = (IvfflatScanOpaque) scan->opaque;
|
||||||
|
|
||||||
|
/* Release pin */
|
||||||
|
if (BufferIsValid(so->buf))
|
||||||
|
ReleaseBuffer(so->buf);
|
||||||
|
|
||||||
pairingheap_free(so->listQueue);
|
pairingheap_free(so->listQueue);
|
||||||
tuplesort_end(so->sortstate);
|
tuplesort_end(so->sortstate);
|
||||||
|
|
||||||
|
|||||||
@@ -4,10 +4,6 @@
|
|||||||
#include "storage/bufmgr.h"
|
#include "storage/bufmgr.h"
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
|
|
||||||
#if PG_VERSION_NUM < 130000
|
|
||||||
#define TYPSTORAGE_PLAIN 'p'
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Allocate a vector array
|
* Allocate a vector array
|
||||||
*/
|
*/
|
||||||
@@ -242,17 +238,3 @@ IvfflatUpdateList(Relation index, ListInfo listInfo,
|
|||||||
UnlockReleaseBuffer(buf);
|
UnlockReleaseBuffer(buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Get the tuple descriptor
|
|
||||||
*/
|
|
||||||
TupleDesc
|
|
||||||
IvfflatTupleDesc(Relation index)
|
|
||||||
{
|
|
||||||
TupleDesc tupdesc = CreateTupleDescCopyConstr(RelationGetDescr(index));
|
|
||||||
|
|
||||||
/* Prevent compression */
|
|
||||||
TupleDescAttr(tupdesc, 0)->attstorage = TYPSTORAGE_PLAIN;
|
|
||||||
|
|
||||||
return tupdesc;
|
|
||||||
}
|
|
||||||
|
|||||||
294
src/tinyint.c
Normal file
294
src/tinyint.c
Normal file
@@ -0,0 +1,294 @@
|
|||||||
|
#include "postgres.h"
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "fmgr.h"
|
||||||
|
#include "lib/stringinfo.h"
|
||||||
|
#include "libpq/pqformat.h"
|
||||||
|
#include "tinyint.h"
|
||||||
|
#include "utils/array.h"
|
||||||
|
#include "utils/builtins.h"
|
||||||
|
#include "utils/numeric.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Check if array is a vector
|
||||||
|
*/
|
||||||
|
static bool
|
||||||
|
ArrayIsVector(ArrayType *a)
|
||||||
|
{
|
||||||
|
return ARR_NDIM(a) == 1 && !array_contains_nulls(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Check if dimensions are the same
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
CheckDims(ArrayType *a, ArrayType *b)
|
||||||
|
{
|
||||||
|
int dima;
|
||||||
|
int dimb;
|
||||||
|
|
||||||
|
if (!ArrayIsVector(a) || !ArrayIsVector(b))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
dima = ARR_DIMS(a)[0];
|
||||||
|
dimb = ARR_DIMS(b)[0];
|
||||||
|
|
||||||
|
if (dima != dimb)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return dima;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Check range
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
CheckRange(long i)
|
||||||
|
{
|
||||||
|
if (i < INT8_MIN || i > INT8_MAX)
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
||||||
|
errmsg("value \"%ld\" is out of range for type tinyint", i)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Convert textual representation to internal representation
|
||||||
|
*/
|
||||||
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(tinyint_in);
|
||||||
|
Datum
|
||||||
|
tinyint_in(PG_FUNCTION_ARGS)
|
||||||
|
{
|
||||||
|
char *s = PG_GETARG_CSTRING(0);
|
||||||
|
const char *ptr = s;
|
||||||
|
long i;
|
||||||
|
char *end;
|
||||||
|
|
||||||
|
/* skip leading spaces */
|
||||||
|
while (*ptr != '\0' && isspace((unsigned char) *ptr))
|
||||||
|
ptr++;
|
||||||
|
|
||||||
|
if (*ptr == '\0')
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
||||||
|
errmsg("invalid input syntax for type tinyint: \"%s\"", s)));
|
||||||
|
|
||||||
|
i = strtol(ptr, &end, 10);
|
||||||
|
ptr = end;
|
||||||
|
|
||||||
|
if (i < INT8_MIN || i > INT8_MAX)
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
||||||
|
errmsg("value \"%s\" is out of range for type tinyint", s)));
|
||||||
|
|
||||||
|
/* allow trailing whitespace, but not other trailing chars */
|
||||||
|
while (*ptr != '\0' && isspace((unsigned char) *ptr))
|
||||||
|
ptr++;
|
||||||
|
|
||||||
|
if (*ptr != '\0')
|
||||||
|
ereport(ERROR,
|
||||||
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
||||||
|
errmsg("invalid input syntax for type tinyint: \"%s\"", s)));
|
||||||
|
|
||||||
|
PG_RETURN_INT8(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Convert internal representation to textual representation
|
||||||
|
*/
|
||||||
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(tinyint_out);
|
||||||
|
Datum
|
||||||
|
tinyint_out(PG_FUNCTION_ARGS)
|
||||||
|
{
|
||||||
|
int8 num = PG_GETARG_INT8(0);
|
||||||
|
char *result = (char *) palloc(5); /* sign, 3 digits, '\0' */
|
||||||
|
|
||||||
|
pg_ltoa((int32) num, result);
|
||||||
|
PG_RETURN_CSTRING(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Convert external binary representation to internal representation
|
||||||
|
*/
|
||||||
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(tinyint_recv);
|
||||||
|
Datum
|
||||||
|
tinyint_recv(PG_FUNCTION_ARGS)
|
||||||
|
{
|
||||||
|
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
|
||||||
|
|
||||||
|
PG_RETURN_INT8((int8) pq_getmsgint(buf, sizeof(int8)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Convert internal representation to the external binary representation
|
||||||
|
*/
|
||||||
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(tinyint_send);
|
||||||
|
Datum
|
||||||
|
tinyint_send(PG_FUNCTION_ARGS)
|
||||||
|
{
|
||||||
|
int8 arg1 = PG_GETARG_INT8(0);
|
||||||
|
StringInfoData buf;
|
||||||
|
|
||||||
|
pq_begintypsend(&buf);
|
||||||
|
pq_sendint8(&buf, arg1);
|
||||||
|
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Convert integer to tinyint
|
||||||
|
*/
|
||||||
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(integer_to_tinyint);
|
||||||
|
Datum
|
||||||
|
integer_to_tinyint(PG_FUNCTION_ARGS)
|
||||||
|
{
|
||||||
|
int32 i = PG_GETARG_INT32(0);
|
||||||
|
|
||||||
|
CheckRange(i);
|
||||||
|
|
||||||
|
PG_RETURN_INT8(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Convert numeric to tinyint
|
||||||
|
*/
|
||||||
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(numeric_to_tinyint);
|
||||||
|
Datum
|
||||||
|
numeric_to_tinyint(PG_FUNCTION_ARGS)
|
||||||
|
{
|
||||||
|
Numeric num = PG_GETARG_NUMERIC(0);
|
||||||
|
int32 i = numeric_int4_opt_error(num, NULL);
|
||||||
|
|
||||||
|
CheckRange(i);
|
||||||
|
|
||||||
|
PG_RETURN_INT8(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get the L2 distance between tinyint arrays
|
||||||
|
*/
|
||||||
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(tinyint_l2_distance);
|
||||||
|
Datum
|
||||||
|
tinyint_l2_distance(PG_FUNCTION_ARGS)
|
||||||
|
{
|
||||||
|
ArrayType *a = PG_GETARG_ARRAYTYPE_P(0);
|
||||||
|
ArrayType *b = PG_GETARG_ARRAYTYPE_P(1);
|
||||||
|
int8 *ax = (int8 *) ARR_DATA_PTR(a);
|
||||||
|
int8 *bx = (int8 *) ARR_DATA_PTR(b);
|
||||||
|
double distance = 0.0;
|
||||||
|
int dim = CheckDims(a, b);
|
||||||
|
|
||||||
|
/* TODO Decide on error or NULL */
|
||||||
|
if (!dim)
|
||||||
|
PG_RETURN_NULL();
|
||||||
|
|
||||||
|
/* Auto-vectorized */
|
||||||
|
for (int i = 0; i < dim; i++)
|
||||||
|
{
|
||||||
|
double diff = ax[i] - bx[i];
|
||||||
|
|
||||||
|
distance += diff * diff;
|
||||||
|
}
|
||||||
|
|
||||||
|
PG_RETURN_FLOAT8(sqrt(distance));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get the inner product of two tinyint arrays
|
||||||
|
*/
|
||||||
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(tinyint_inner_product);
|
||||||
|
Datum
|
||||||
|
tinyint_inner_product(PG_FUNCTION_ARGS)
|
||||||
|
{
|
||||||
|
ArrayType *a = PG_GETARG_ARRAYTYPE_P(0);
|
||||||
|
ArrayType *b = PG_GETARG_ARRAYTYPE_P(1);
|
||||||
|
int8 *ax = (int8 *) ARR_DATA_PTR(a);
|
||||||
|
int8 *bx = (int8 *) ARR_DATA_PTR(b);
|
||||||
|
double distance = 0.0;
|
||||||
|
int dim = CheckDims(a, b);
|
||||||
|
|
||||||
|
/* TODO Decide on error or NULL */
|
||||||
|
if (!dim)
|
||||||
|
PG_RETURN_NULL();
|
||||||
|
|
||||||
|
/* Auto-vectorized */
|
||||||
|
for (int i = 0; i < dim; i++)
|
||||||
|
distance += ax[i] * bx[i];
|
||||||
|
|
||||||
|
PG_RETURN_FLOAT8(distance);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get the negative inner product of two tinyint arrays
|
||||||
|
*/
|
||||||
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(tinyint_negative_inner_product);
|
||||||
|
Datum
|
||||||
|
tinyint_negative_inner_product(PG_FUNCTION_ARGS)
|
||||||
|
{
|
||||||
|
ArrayType *a = PG_GETARG_ARRAYTYPE_P(0);
|
||||||
|
ArrayType *b = PG_GETARG_ARRAYTYPE_P(1);
|
||||||
|
int8 *ax = (int8 *) ARR_DATA_PTR(a);
|
||||||
|
int8 *bx = (int8 *) ARR_DATA_PTR(b);
|
||||||
|
double distance = 0.0;
|
||||||
|
int dim = CheckDims(a, b);
|
||||||
|
|
||||||
|
/* TODO Decide on error or NULL */
|
||||||
|
if (!dim)
|
||||||
|
PG_RETURN_NULL();
|
||||||
|
|
||||||
|
/* Auto-vectorized */
|
||||||
|
for (int i = 0; i < dim; i++)
|
||||||
|
distance += ax[i] * bx[i];
|
||||||
|
|
||||||
|
PG_RETURN_FLOAT8(distance * -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get the cosine distance between two float2 arrays
|
||||||
|
*/
|
||||||
|
PGDLLEXPORT PG_FUNCTION_INFO_V1(tinyint_cosine_distance);
|
||||||
|
Datum
|
||||||
|
tinyint_cosine_distance(PG_FUNCTION_ARGS)
|
||||||
|
{
|
||||||
|
ArrayType *a = PG_GETARG_ARRAYTYPE_P(0);
|
||||||
|
ArrayType *b = PG_GETARG_ARRAYTYPE_P(1);
|
||||||
|
int8 *ax = (int8 *) ARR_DATA_PTR(a);
|
||||||
|
int8 *bx = (int8 *) ARR_DATA_PTR(b);
|
||||||
|
double distance = 0.0;
|
||||||
|
double norma = 0.0;
|
||||||
|
double normb = 0.0;
|
||||||
|
double similarity;
|
||||||
|
int dim = CheckDims(a, b);
|
||||||
|
|
||||||
|
/* TODO Decide on error or NULL */
|
||||||
|
if (!dim)
|
||||||
|
PG_RETURN_NULL();
|
||||||
|
|
||||||
|
/* Auto-vectorized */
|
||||||
|
for (int i = 0; i < dim; i++)
|
||||||
|
{
|
||||||
|
float axi = ax[i];
|
||||||
|
float bxi = bx[i];
|
||||||
|
|
||||||
|
distance += axi * bxi;
|
||||||
|
norma += axi * axi;
|
||||||
|
normb += bxi * bxi;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Use sqrt(a * b) over sqrt(a) * sqrt(b) */
|
||||||
|
similarity = distance / sqrt(norma * normb);
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
/* /fp:fast may not propagate NaN */
|
||||||
|
if (isnan(similarity))
|
||||||
|
PG_RETURN_FLOAT8(NAN);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Keep in range */
|
||||||
|
if (similarity > 1)
|
||||||
|
similarity = 1;
|
||||||
|
else if (similarity < -1)
|
||||||
|
similarity = -1;
|
||||||
|
|
||||||
|
PG_RETURN_FLOAT8(1 - similarity);
|
||||||
|
}
|
||||||
8
src/tinyint.h
Normal file
8
src/tinyint.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef TINYINT_H
|
||||||
|
#define TINYINT_H
|
||||||
|
|
||||||
|
#define DatumGetInt8(X) ((int8) (X))
|
||||||
|
#define PG_GETARG_INT8(n) DatumGetInt8(PG_GETARG_DATUM(n))
|
||||||
|
#define PG_RETURN_INT8(x) return Int8GetDatum(x)
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -54,85 +54,85 @@ SELECT vector_norm('[3e37,4e37]')::real;
|
|||||||
5e+37
|
5e+37
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
SELECT l2_distance('[0,0]', '[3,4]');
|
SELECT l2_distance('[0,0]'::vector, '[3,4]');
|
||||||
l2_distance
|
l2_distance
|
||||||
-------------
|
-------------
|
||||||
5
|
5
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
SELECT l2_distance('[0,0]', '[0,1]');
|
SELECT l2_distance('[0,0]'::vector, '[0,1]');
|
||||||
l2_distance
|
l2_distance
|
||||||
-------------
|
-------------
|
||||||
1
|
1
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
SELECT l2_distance('[1,2]', '[3]');
|
SELECT l2_distance('[1,2]'::vector, '[3]');
|
||||||
ERROR: different vector dimensions 2 and 1
|
ERROR: different vector dimensions 2 and 1
|
||||||
SELECT l2_distance('[3e38]', '[-3e38]');
|
SELECT l2_distance('[3e38]'::vector, '[-3e38]');
|
||||||
l2_distance
|
l2_distance
|
||||||
-------------
|
-------------
|
||||||
Infinity
|
Infinity
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
SELECT inner_product('[1,2]', '[3,4]');
|
SELECT inner_product('[1,2]'::vector, '[3,4]');
|
||||||
inner_product
|
inner_product
|
||||||
---------------
|
---------------
|
||||||
11
|
11
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
SELECT inner_product('[1,2]', '[3]');
|
SELECT inner_product('[1,2]'::vector, '[3]');
|
||||||
ERROR: different vector dimensions 2 and 1
|
ERROR: different vector dimensions 2 and 1
|
||||||
SELECT inner_product('[3e38]', '[3e38]');
|
SELECT inner_product('[3e38]'::vector, '[3e38]');
|
||||||
inner_product
|
inner_product
|
||||||
---------------
|
---------------
|
||||||
Infinity
|
Infinity
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
SELECT cosine_distance('[1,2]', '[2,4]');
|
SELECT cosine_distance('[1,2]'::vector, '[2,4]');
|
||||||
cosine_distance
|
cosine_distance
|
||||||
-----------------
|
-----------------
|
||||||
0
|
0
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
SELECT cosine_distance('[1,2]', '[0,0]');
|
SELECT cosine_distance('[1,2]'::vector, '[0,0]');
|
||||||
cosine_distance
|
cosine_distance
|
||||||
-----------------
|
-----------------
|
||||||
NaN
|
NaN
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
SELECT cosine_distance('[1,1]', '[1,1]');
|
SELECT cosine_distance('[1,1]'::vector, '[1,1]');
|
||||||
cosine_distance
|
cosine_distance
|
||||||
-----------------
|
-----------------
|
||||||
0
|
0
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
SELECT cosine_distance('[1,0]', '[0,2]');
|
SELECT cosine_distance('[1,0]'::vector, '[0,2]');
|
||||||
cosine_distance
|
cosine_distance
|
||||||
-----------------
|
-----------------
|
||||||
1
|
1
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
SELECT cosine_distance('[1,1]', '[-1,-1]');
|
SELECT cosine_distance('[1,1]'::vector, '[-1,-1]');
|
||||||
cosine_distance
|
cosine_distance
|
||||||
-----------------
|
-----------------
|
||||||
2
|
2
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
SELECT cosine_distance('[1,2]', '[3]');
|
SELECT cosine_distance('[1,2]'::vector, '[3]');
|
||||||
ERROR: different vector dimensions 2 and 1
|
ERROR: different vector dimensions 2 and 1
|
||||||
SELECT cosine_distance('[1,1]', '[1.1,1.1]');
|
SELECT cosine_distance('[1,1]'::vector, '[1.1,1.1]');
|
||||||
cosine_distance
|
cosine_distance
|
||||||
-----------------
|
-----------------
|
||||||
0
|
0
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
SELECT cosine_distance('[1,1]', '[-1.1,-1.1]');
|
SELECT cosine_distance('[1,1]'::vector, '[-1.1,-1.1]');
|
||||||
cosine_distance
|
cosine_distance
|
||||||
-----------------
|
-----------------
|
||||||
2
|
2
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
SELECT cosine_distance('[3e38]', '[3e38]');
|
SELECT cosine_distance('[3e38]'::vector, '[3e38]');
|
||||||
cosine_distance
|
cosine_distance
|
||||||
-----------------
|
-----------------
|
||||||
NaN
|
NaN
|
||||||
|
|||||||
148
test/expected/tinyint.out
Normal file
148
test/expected/tinyint.out
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
SELECT '127'::tinyint;
|
||||||
|
tinyint
|
||||||
|
---------
|
||||||
|
127
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT '128'::tinyint;
|
||||||
|
ERROR: value "128" is out of range for type tinyint
|
||||||
|
LINE 1: SELECT '128'::tinyint;
|
||||||
|
^
|
||||||
|
SELECT '-128'::tinyint;
|
||||||
|
tinyint
|
||||||
|
---------
|
||||||
|
-128
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT '-129'::tinyint;
|
||||||
|
ERROR: value "-129" is out of range for type tinyint
|
||||||
|
LINE 1: SELECT '-129'::tinyint;
|
||||||
|
^
|
||||||
|
SELECT ''::tinyint;
|
||||||
|
ERROR: invalid input syntax for type tinyint: ""
|
||||||
|
LINE 1: SELECT ''::tinyint;
|
||||||
|
^
|
||||||
|
SELECT ' 1'::tinyint;
|
||||||
|
tinyint
|
||||||
|
---------
|
||||||
|
1
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT '1 '::tinyint;
|
||||||
|
tinyint
|
||||||
|
---------
|
||||||
|
1
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT '1a'::tinyint;
|
||||||
|
ERROR: invalid input syntax for type tinyint: "1a"
|
||||||
|
LINE 1: SELECT '1a'::tinyint;
|
||||||
|
^
|
||||||
|
SELECT '{1,2,3}'::tinyint[];
|
||||||
|
tinyint
|
||||||
|
---------
|
||||||
|
{1,2,3}
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT '128'::numeric::tinyint;
|
||||||
|
ERROR: value "128" is out of range for type tinyint
|
||||||
|
SELECT 'NaN'::numeric::tinyint;
|
||||||
|
ERROR: cannot convert NaN to integer
|
||||||
|
SELECT l2_distance('{0,0}'::tinyint[], '{3,4}'::tinyint[]);
|
||||||
|
l2_distance
|
||||||
|
-------------
|
||||||
|
5
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT l2_distance('{0,0}'::tinyint[], '{0,1}'::tinyint[]);
|
||||||
|
l2_distance
|
||||||
|
-------------
|
||||||
|
1
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT l2_distance('{1,2}'::tinyint[], '{3}'::tinyint[]);
|
||||||
|
l2_distance
|
||||||
|
-------------
|
||||||
|
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT l2_distance('{3e38}'::tinyint[], '{-3e38}'::tinyint[]);
|
||||||
|
ERROR: invalid input syntax for type tinyint: "3e38"
|
||||||
|
LINE 1: SELECT l2_distance('{3e38}'::tinyint[], '{-3e38}'::tinyint[]...
|
||||||
|
^
|
||||||
|
SELECT '{0,0}'::tinyint[] <-> '{3,4}'::tinyint[];
|
||||||
|
?column?
|
||||||
|
----------
|
||||||
|
5
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT inner_product('{1,2}'::tinyint[], '{3,4}'::tinyint[]);
|
||||||
|
inner_product
|
||||||
|
---------------
|
||||||
|
11
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT inner_product('{1,2}'::tinyint[], '{3}'::tinyint[]);
|
||||||
|
inner_product
|
||||||
|
---------------
|
||||||
|
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT inner_product('{127}'::tinyint[], '{127}'::tinyint[]);
|
||||||
|
inner_product
|
||||||
|
---------------
|
||||||
|
16129
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT '{1,2}'::tinyint[] <#> '{3,4}'::tinyint[];
|
||||||
|
?column?
|
||||||
|
----------
|
||||||
|
-11
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT cosine_distance('{1,2}'::tinyint[], '{2,4}'::tinyint[]);
|
||||||
|
cosine_distance
|
||||||
|
-----------------
|
||||||
|
0
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT cosine_distance('{1,2}'::tinyint[], '{0,0}'::tinyint[]);
|
||||||
|
cosine_distance
|
||||||
|
-----------------
|
||||||
|
NaN
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT cosine_distance('{1,1}'::tinyint[], '{1,1}'::tinyint[]);
|
||||||
|
cosine_distance
|
||||||
|
-----------------
|
||||||
|
0
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT cosine_distance('{1,0}'::tinyint[], '{0,2}'::tinyint[]);
|
||||||
|
cosine_distance
|
||||||
|
-----------------
|
||||||
|
1
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT cosine_distance('{1,1}'::tinyint[], '{-1,-1}'::tinyint[]);
|
||||||
|
cosine_distance
|
||||||
|
-----------------
|
||||||
|
2
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT cosine_distance('{1,2}'::tinyint[], '{3}'::tinyint[]);
|
||||||
|
cosine_distance
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SELECT cosine_distance('{3e38}'::tinyint[], '{3e38}'::tinyint[]);
|
||||||
|
ERROR: invalid input syntax for type tinyint: "3e38"
|
||||||
|
LINE 1: SELECT cosine_distance('{3e38}'::tinyint[], '{3e38}'::tinyin...
|
||||||
|
^
|
||||||
|
SELECT '{1,2}'::tinyint[] <=> '{2,4}'::tinyint[];
|
||||||
|
?column?
|
||||||
|
----------
|
||||||
|
0
|
||||||
|
(1 row)
|
||||||
|
|
||||||
@@ -13,24 +13,24 @@ SELECT vector_norm('[3,4]');
|
|||||||
SELECT vector_norm('[0,1]');
|
SELECT vector_norm('[0,1]');
|
||||||
SELECT vector_norm('[3e37,4e37]')::real;
|
SELECT vector_norm('[3e37,4e37]')::real;
|
||||||
|
|
||||||
SELECT l2_distance('[0,0]', '[3,4]');
|
SELECT l2_distance('[0,0]'::vector, '[3,4]');
|
||||||
SELECT l2_distance('[0,0]', '[0,1]');
|
SELECT l2_distance('[0,0]'::vector, '[0,1]');
|
||||||
SELECT l2_distance('[1,2]', '[3]');
|
SELECT l2_distance('[1,2]'::vector, '[3]');
|
||||||
SELECT l2_distance('[3e38]', '[-3e38]');
|
SELECT l2_distance('[3e38]'::vector, '[-3e38]');
|
||||||
|
|
||||||
SELECT inner_product('[1,2]', '[3,4]');
|
SELECT inner_product('[1,2]'::vector, '[3,4]');
|
||||||
SELECT inner_product('[1,2]', '[3]');
|
SELECT inner_product('[1,2]'::vector, '[3]');
|
||||||
SELECT inner_product('[3e38]', '[3e38]');
|
SELECT inner_product('[3e38]'::vector, '[3e38]');
|
||||||
|
|
||||||
SELECT cosine_distance('[1,2]', '[2,4]');
|
SELECT cosine_distance('[1,2]'::vector, '[2,4]');
|
||||||
SELECT cosine_distance('[1,2]', '[0,0]');
|
SELECT cosine_distance('[1,2]'::vector, '[0,0]');
|
||||||
SELECT cosine_distance('[1,1]', '[1,1]');
|
SELECT cosine_distance('[1,1]'::vector, '[1,1]');
|
||||||
SELECT cosine_distance('[1,0]', '[0,2]');
|
SELECT cosine_distance('[1,0]'::vector, '[0,2]');
|
||||||
SELECT cosine_distance('[1,1]', '[-1,-1]');
|
SELECT cosine_distance('[1,1]'::vector, '[-1,-1]');
|
||||||
SELECT cosine_distance('[1,2]', '[3]');
|
SELECT cosine_distance('[1,2]'::vector, '[3]');
|
||||||
SELECT cosine_distance('[1,1]', '[1.1,1.1]');
|
SELECT cosine_distance('[1,1]'::vector, '[1.1,1.1]');
|
||||||
SELECT cosine_distance('[1,1]', '[-1.1,-1.1]');
|
SELECT cosine_distance('[1,1]'::vector, '[-1.1,-1.1]');
|
||||||
SELECT cosine_distance('[3e38]', '[3e38]');
|
SELECT cosine_distance('[3e38]'::vector, '[3e38]');
|
||||||
|
|
||||||
SELECT l1_distance('[0,0]', '[3,4]');
|
SELECT l1_distance('[0,0]', '[3,4]');
|
||||||
SELECT l1_distance('[0,0]', '[0,1]');
|
SELECT l1_distance('[0,0]', '[0,1]');
|
||||||
|
|||||||
34
test/sql/tinyint.sql
Normal file
34
test/sql/tinyint.sql
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
SELECT '127'::tinyint;
|
||||||
|
SELECT '128'::tinyint;
|
||||||
|
SELECT '-128'::tinyint;
|
||||||
|
SELECT '-129'::tinyint;
|
||||||
|
|
||||||
|
SELECT ''::tinyint;
|
||||||
|
SELECT ' 1'::tinyint;
|
||||||
|
SELECT '1 '::tinyint;
|
||||||
|
SELECT '1a'::tinyint;
|
||||||
|
|
||||||
|
SELECT '{1,2,3}'::tinyint[];
|
||||||
|
|
||||||
|
SELECT '128'::numeric::tinyint;
|
||||||
|
SELECT 'NaN'::numeric::tinyint;
|
||||||
|
|
||||||
|
SELECT l2_distance('{0,0}'::tinyint[], '{3,4}'::tinyint[]);
|
||||||
|
SELECT l2_distance('{0,0}'::tinyint[], '{0,1}'::tinyint[]);
|
||||||
|
SELECT l2_distance('{1,2}'::tinyint[], '{3}'::tinyint[]);
|
||||||
|
SELECT l2_distance('{3e38}'::tinyint[], '{-3e38}'::tinyint[]);
|
||||||
|
SELECT '{0,0}'::tinyint[] <-> '{3,4}'::tinyint[];
|
||||||
|
|
||||||
|
SELECT inner_product('{1,2}'::tinyint[], '{3,4}'::tinyint[]);
|
||||||
|
SELECT inner_product('{1,2}'::tinyint[], '{3}'::tinyint[]);
|
||||||
|
SELECT inner_product('{127}'::tinyint[], '{127}'::tinyint[]);
|
||||||
|
SELECT '{1,2}'::tinyint[] <#> '{3,4}'::tinyint[];
|
||||||
|
|
||||||
|
SELECT cosine_distance('{1,2}'::tinyint[], '{2,4}'::tinyint[]);
|
||||||
|
SELECT cosine_distance('{1,2}'::tinyint[], '{0,0}'::tinyint[]);
|
||||||
|
SELECT cosine_distance('{1,1}'::tinyint[], '{1,1}'::tinyint[]);
|
||||||
|
SELECT cosine_distance('{1,0}'::tinyint[], '{0,2}'::tinyint[]);
|
||||||
|
SELECT cosine_distance('{1,1}'::tinyint[], '{-1,-1}'::tinyint[]);
|
||||||
|
SELECT cosine_distance('{1,2}'::tinyint[], '{3}'::tinyint[]);
|
||||||
|
SELECT cosine_distance('{3e38}'::tinyint[], '{3e38}'::tinyint[]);
|
||||||
|
SELECT '{1,2}'::tinyint[] <=> '{2,4}'::tinyint[];
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
comment = 'vector data type and ivfflat and hnsw access methods'
|
comment = 'vector data type and ivfflat and hnsw access methods'
|
||||||
default_version = '0.5.1'
|
default_version = '0.5.0'
|
||||||
module_pathname = '$libdir/vector'
|
module_pathname = '$libdir/vector'
|
||||||
relocatable = true
|
relocatable = true
|
||||||
|
|||||||
Reference in New Issue
Block a user