diff options
author | Andrew Dunstan | 2023-03-13 14:01:56 +0000 |
---|---|---|
committer | Andrew Dunstan | 2023-03-13 14:01:56 +0000 |
commit | 9f8377f7a27910bf0f35bf5169a8046731948a79 (patch) | |
tree | e0a19449e4cebb8923dfcd1bba95a1d3363faa32 /src/bin | |
parent | 7b14e20b12cc8358cad9bdd05dd6b7de7f73c431 (diff) |
Add a DEFAULT option to COPY FROM
This allows for a string which if an input field matches causes the
column's default value to be inserted. The advantage of this is that
the default can be inserted in some rows and not others, for which
non-default data is available.
The file_fdw extension is also modified to take allow use of this
option.
Israel Barth Rubio
Discussion: https://postgr.es/m/CAO_rXXAcqesk6DsvioOZ5zmeEmpUN5ktZf-9=9yu+DTr0Xr8Uw@mail.gmail.com
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/psql/t/001_basic.pl | 25 | ||||
-rw-r--r-- | src/bin/psql/t/010_tab_completion.pl | 8 | ||||
-rw-r--r-- | src/bin/psql/tab-complete.c | 2 |
3 files changed, 34 insertions, 1 deletions
diff --git a/src/bin/psql/t/001_basic.pl b/src/bin/psql/t/001_basic.pl index 0167cb58a2..0f394420b2 100644 --- a/src/bin/psql/t/001_basic.pl +++ b/src/bin/psql/t/001_basic.pl @@ -325,4 +325,29 @@ is($row_count, '10', 'client-side error commits transaction, no ON_ERROR_STOP and multiple -c switches' ); +# Test \copy from with DEFAULT option +$node->safe_psql( + 'postgres', + "CREATE TABLE copy_default ( + id integer PRIMARY KEY, + text_value text NOT NULL DEFAULT 'test', + ts_value timestamp without time zone NOT NULL DEFAULT '2022-07-05' + )" +); + +my $copy_default_sql_file = "$tempdir/copy_default.csv"; +append_to_file($copy_default_sql_file, "1,value,2022-07-04\n"); +append_to_file($copy_default_sql_file, "2,placeholder,2022-07-03\n"); +append_to_file($copy_default_sql_file, "3,placeholder,placeholder\n"); + +psql_like( + $node, + "\\copy copy_default from $copy_default_sql_file with (format 'csv', default 'placeholder'); + SELECT * FROM copy_default", + qr/1\|value\|2022-07-04 00:00:00 +2|test|2022-07-03 00:00:00 +3|test|2022-07-05 00:00:00/, + '\copy from with DEFAULT' +); + done_testing(); diff --git a/src/bin/psql/t/010_tab_completion.pl b/src/bin/psql/t/010_tab_completion.pl index 7746c75e0c..55a88f9812 100644 --- a/src/bin/psql/t/010_tab_completion.pl +++ b/src/bin/psql/t/010_tab_completion.pl @@ -442,6 +442,14 @@ check_completion("blarg \t\t", qr//, "check completion failure path"); clear_query(); +# check COPY FROM with DEFAULT option +check_completion( + "COPY foo FROM stdin WITH ( DEF\t)", + qr/DEFAULT /, + "COPY FROM with DEFAULT completion"); + +clear_line(); + # send psql an explicit \q to shut it down, else pty won't close properly $timer->start($PostgreSQL::Test::Utils::timeout_default); $in .= "\\q\n"; diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 8f12af799b..42e87b9e49 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -2857,7 +2857,7 @@ psql_completion(const char *text, int start, int end) else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(")) COMPLETE_WITH("FORMAT", "FREEZE", "DELIMITER", "NULL", "HEADER", "QUOTE", "ESCAPE", "FORCE_QUOTE", - "FORCE_NOT_NULL", "FORCE_NULL", "ENCODING"); + "FORCE_NOT_NULL", "FORCE_NULL", "ENCODING", "DEFAULT"); /* Complete COPY <sth> FROM|TO filename WITH (FORMAT */ else if (Matches("COPY|\\copy", MatchAny, "FROM|TO", MatchAny, "WITH", "(", "FORMAT")) |