Skip to content

Commit

Permalink
[#25990] YSQL: Upgrade to code from PgVector 0.8.0
Browse files Browse the repository at this point in the history
Summary:
This diff updates the `src/postgres/third-party-extensions/pgvector/` folder to include new functions and datatypes from upstream pgvector 0.8.0 (2627c5ff775ae6d7aef0c430121ccf857842d2f2). This change brings in the following new features:

- New Datatypes
-- `sparsevec`
-- `halfvec`
-- Input/output/send/receive/typmod_in functions for the above

- New Functions
-- `l1_distance`
-- `l2_normalize`
-- `hamming_distance`
-- `jaccard_distance`
-- `binary_quantize`
-- `subvector`
-- operators for the above distance functions
-- Common aggregates such as sum/avg for the new datatypes

This change merges code for new functions and datatypes from upstream into its code files. Code files relating to ivfflat have also been deleted from this change as we do not intend on repurposing any of pgvector’s index access methods. For this reason, any upstream code relating to hnsw and ivfflat have not been merged. Also, while upstream pgvector stores all the new vector types (`vector`, `sparsevec`, `halfvec`) in external (TOASTed) format, this change keeps those types in extended storage format since YB doesn't TOAST any values.

The test folder has been updated to match pgvector's test folder with the exception of files related to ivfflat and hnsw. Similarly, perl test files were cleaned out from the `src/postgres/third-party-extensions/pgvector/test/t` directory as they are not used by YB. `copy.sql` and `copy.out` were slightly modified to copy to and from tables in the `/tmp/` directory to be compatible with our build system. Hence, these files are ported as `yb.port.copy.sql` and `yb.port.copy.out`.

Users on an older version of the vector extension must recreate this extension to get onto the newer version. Our initial port of pgvector ported the vector datatype as-is. Later, we modified the vector datatype to contain an embedded vector id in the form of a UUID. An upgrade path to 0.8.0-yb-1.0 is omitted to force users to use this newer version of the vector datatype. From 0.8.0-yb-1.0 going forward, we want to prevent users using the old vector instances which did not have any associated vector ids. This still means though that a followup change has to be made to force users to drop any old version of their vector extension even if they don't choose to try to upgrade to 0.8.0-yb-1.0. Extension creation scripts for older versions of vector have been removed to prevent users from creating these old versions of the extension.

**Note: Upstream pgvector's changes merged into this change removed PG12-related ifdefs as PG12 is EOL. That means that this change cannot be backported to 2024.2 and below.**

Test Plan: ./yb_build.sh --java-test 'org.yb.pgsql.TestPgRegressThirdPartyPgvector'

Reviewers: kramanathan, jason

Reviewed By: jason

Subscribers: mihnea, jason, yql

Differential Revision: https://phorge.dev.yugabyte.com/D41864
  • Loading branch information
Tanuj Nayak committed Mar 4, 2025
1 parent 1713a7e commit 9e694b6
Show file tree
Hide file tree
Showing 77 changed files with 8,667 additions and 4,521 deletions.
293 changes: 51 additions & 242 deletions src/postgres/src/test/regress/expected/copy.out
Original file line number Diff line number Diff line change
@@ -1,242 +1,51 @@
--
-- COPY
--
-- directory paths are passed to us in environment variables
\getenv abs_srcdir PG_ABS_SRCDIR
\getenv abs_builddir PG_ABS_BUILDDIR
--- test copying in CSV mode with various styles
--- of embedded line ending characters
create temp table copytest (
style text,
test text,
filler int);
insert into copytest values('DOS',E'abc\r\ndef',1);
insert into copytest values('Unix',E'abc\ndef',2);
insert into copytest values('Mac',E'abc\rdef',3);
insert into copytest values(E'esc\\ape',E'a\\r\\\r\\\n\\nb',4);
\set filename :abs_builddir '/results/copytest.csv'
copy copytest to :'filename' csv;
create temp table copytest2 (like copytest);
copy copytest2 from :'filename' csv;
select * from copytest except select * from copytest2;
style | test | filler
-------+------+--------
(0 rows)

truncate copytest2;
--- same test but with an escape char different from quote char
copy copytest to :'filename' csv quote '''' escape E'\\';
copy copytest2 from :'filename' csv quote '''' escape E'\\';
select * from copytest except select * from copytest2;
style | test | filler
-------+------+--------
(0 rows)

-- test header line feature
create temp table copytest3 (
c1 int,
"col with , comma" text,
"col with "" quote" int);
copy copytest3 from stdin csv header;
copy copytest3 to stdout csv header;
c1,"col with , comma","col with "" quote"
1,a,1
2,b,2
create temp table copytest4 (
c1 int,
"colname with tab: " text);
copy copytest4 from stdin (header);
copy copytest4 to stdout (header);
c1 colname with tab: \t
1 a
2 b
-- test copy from with a partitioned table
create table parted_copytest (
a int,
b int,
c text
) partition by list (b);
create table parted_copytest_a1 (c text, b int, a int);
create table parted_copytest_a2 (a int, c text, b int);
alter table parted_copytest attach partition parted_copytest_a1 for values in(1);
alter table parted_copytest attach partition parted_copytest_a2 for values in(2);
-- We must insert enough rows to trigger multi-inserts. These are only
-- enabled adaptively when there are few enough partition changes.
insert into parted_copytest select x,1,'One' from generate_series(1,1000) x;
insert into parted_copytest select x,2,'Two' from generate_series(1001,1010) x;
insert into parted_copytest select x,1,'One' from generate_series(1011,1020) x;
\set filename :abs_builddir '/results/parted_copytest.csv'
copy (select * from parted_copytest order by a) to :'filename';
truncate parted_copytest;
copy parted_copytest from :'filename';
-- Ensure COPY FREEZE errors for partitioned tables.
begin;
truncate parted_copytest;
copy parted_copytest from :'filename' (freeze);
ERROR: cannot perform COPY FREEZE on a partitioned table
rollback;
select tableoid::regclass,count(*),sum(a) from parted_copytest
group by tableoid order by tableoid::regclass::name;
tableoid | count | sum
--------------------+-------+--------
parted_copytest_a1 | 1010 | 510655
parted_copytest_a2 | 10 | 10055
(2 rows)

truncate parted_copytest;
-- create before insert row trigger on parted_copytest_a2
create function part_ins_func() returns trigger language plpgsql as $$
begin
return new;
end;
$$;
create trigger part_ins_trig
before insert on parted_copytest_a2
for each row
execute procedure part_ins_func();
copy parted_copytest from :'filename';
select tableoid::regclass,count(*),sum(a) from parted_copytest
group by tableoid order by tableoid::regclass::name;
tableoid | count | sum
--------------------+-------+--------
parted_copytest_a1 | 1010 | 510655
parted_copytest_a2 | 10 | 10055
(2 rows)

truncate table parted_copytest;
create index on parted_copytest (b);
drop trigger part_ins_trig on parted_copytest_a2;
copy parted_copytest from stdin;
-- Ensure index entries were properly added during the copy.
select * from parted_copytest where b = 1;
a | b | c
---+---+------
1 | 1 | str1
(1 row)

select * from parted_copytest where b = 2;
a | b | c
---+---+------
2 | 2 | str2
(1 row)

drop table parted_copytest;
--
-- Progress reporting for COPY
--
create table tab_progress_reporting (
name text,
age int4,
location point,
salary int4,
manager name
);
-- Add a trigger to catch and print the contents of the catalog view
-- pg_stat_progress_copy during data insertion. This allows to test
-- the validation of some progress reports for COPY FROM where the trigger
-- would fire.
create function notice_after_tab_progress_reporting() returns trigger AS
$$
declare report record;
begin
-- The fields ignored here are the ones that may not remain
-- consistent across multiple runs. The sizes reported may differ
-- across platforms, so just check if these are strictly positive.
with progress_data as (
select
relid::regclass::text as relname,
command,
type,
bytes_processed > 0 as has_bytes_processed,
bytes_total > 0 as has_bytes_total,
tuples_processed,
tuples_excluded
from pg_stat_progress_copy
where pid = pg_backend_pid())
select into report (to_jsonb(r)) as value
from progress_data r;

raise info 'progress: %', report.value::text;
return new;
end;
$$ language plpgsql;
create trigger check_after_tab_progress_reporting
after insert on tab_progress_reporting
for each statement
execute function notice_after_tab_progress_reporting();
-- Generate COPY FROM report with PIPE.
copy tab_progress_reporting from stdin;
INFO: progress: {"type": "PIPE", "command": "COPY FROM", "relname": "tab_progress_reporting", "has_bytes_total": false, "tuples_excluded": 0, "tuples_processed": 3, "has_bytes_processed": true}
-- Generate COPY FROM report with FILE, with some excluded tuples.
truncate tab_progress_reporting;
\set filename :abs_srcdir '/data/emp.data'
copy tab_progress_reporting from :'filename'
where (salary < 2000);
INFO: progress: {"type": "FILE", "command": "COPY FROM", "relname": "tab_progress_reporting", "has_bytes_total": true, "tuples_excluded": 1, "tuples_processed": 2, "has_bytes_processed": true}
drop trigger check_after_tab_progress_reporting on tab_progress_reporting;
drop function notice_after_tab_progress_reporting();
drop table tab_progress_reporting;
-- Test header matching feature
create table header_copytest (
a int,
b int,
c text
);
-- Make sure it works with dropped columns
alter table header_copytest drop column c;
alter table header_copytest add column c text;
copy header_copytest to stdout with (header match);
ERROR: cannot use "match" with HEADER in COPY TO
copy header_copytest from stdin with (header wrong_choice);
ERROR: header requires a Boolean value or "match"
-- works
copy header_copytest from stdin with (header match);
copy header_copytest (c, a, b) from stdin with (header match);
copy header_copytest from stdin with (header match, format csv);
-- errors
copy header_copytest (c, b, a) from stdin with (header match);
ERROR: column name mismatch in header line field 1: got "a", expected "c"
CONTEXT: COPY header_copytest, line 1: "a b c"
copy header_copytest from stdin with (header match);
ERROR: column name mismatch in header line field 3: got null value ("\N"), expected "c"
CONTEXT: COPY header_copytest, line 1: "a b \N"
copy header_copytest from stdin with (header match);
ERROR: wrong number of fields in header line: got 2, expected 3
CONTEXT: COPY header_copytest, line 1: "a b"
copy header_copytest from stdin with (header match);
ERROR: wrong number of fields in header line: got 4, expected 3
CONTEXT: COPY header_copytest, line 1: "a b c d"
copy header_copytest from stdin with (header match);
ERROR: column name mismatch in header line field 3: got "d", expected "c"
CONTEXT: COPY header_copytest, line 1: "a b d"
SELECT * FROM header_copytest ORDER BY a;
a | b | c
---+---+-----
1 | 2 | foo
3 | 4 | bar
5 | 6 | baz
(3 rows)

-- Drop an extra column, in the middle of the existing set.
alter table header_copytest drop column b;
-- works
copy header_copytest (c, a) from stdin with (header match);
copy header_copytest (a, c) from stdin with (header match);
-- errors
copy header_copytest from stdin with (header match);
ERROR: wrong number of fields in header line: got 3, expected 2
CONTEXT: COPY header_copytest, line 1: "a ........pg.dropped.2........ c"
copy header_copytest (a, c) from stdin with (header match);
ERROR: wrong number of fields in header line: got 3, expected 2
CONTEXT: COPY header_copytest, line 1: "a c b"
SELECT * FROM header_copytest ORDER BY a;
a | c
---+-----
1 | foo
3 | bar
5 | baz
7 | foo
8 | foo
(5 rows)

drop table header_copytest;
-- vector
CREATE TABLE t (val vector(3));
INSERT INTO t (val) VALUES ('[0,0,0]'), ('[1,2,3]'), ('[1,1,1]'), (NULL);
CREATE TABLE t2 (val vector(3));
\copy t TO 'results/vector.bin' WITH (FORMAT binary)
\copy t2 FROM 'results/vector.bin' WITH (FORMAT binary)
SELECT * FROM t2 ORDER BY val;
val
---------
[0,0,0]
[1,1,1]
[1,2,3]

(4 rows)

DROP TABLE t;
DROP TABLE t2;
-- halfvec
CREATE TABLE t (val halfvec(3));
INSERT INTO t (val) VALUES ('[0,0,0]'), ('[1,2,3]'), ('[1,1,1]'), (NULL);
CREATE TABLE t2 (val halfvec(3));
\copy t TO 'results/halfvec.bin' WITH (FORMAT binary)
\copy t2 FROM 'results/halfvec.bin' WITH (FORMAT binary)
SELECT * FROM t2 ORDER BY val;
val
---------
[0,0,0]
[1,1,1]
[1,2,3]

(4 rows)

DROP TABLE t;
DROP TABLE t2;
-- sparsevec
CREATE TABLE t (val sparsevec(3));
INSERT INTO t (val) VALUES ('{}/3'), ('{1:1,2:2,3:3}/3'), ('{1:1,2:1,3:1}/3'), (NULL);
CREATE TABLE t2 (val sparsevec(3));
\copy t TO 'results/sparsevec.bin' WITH (FORMAT binary)
\copy t2 FROM 'results/sparsevec.bin' WITH (FORMAT binary)
SELECT * FROM t2 ORDER BY val;
val
-----------------
{}/3
{1:1,2:1,3:1}/3
{1:1,2:2,3:3}/3

(4 rows)

DROP TABLE t;
DROP TABLE t2;
Loading

0 comments on commit 9e694b6

Please sign in to comment.