Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 089aac6

Browse files
committed
Fix validation of COPY FORCE_NOT_NULL/FORCE_NULL for the all-column cases
This commit adds missing checks for COPY FORCE_NOT_NULL and FORCE_NULL when applied to all columns via "*". These options now correctly require CSV mode and are disallowed in COPY TO, making their behavior consistent with FORCE_QUOTE. Some regression tests are added to verify the correct behavior for the all-columns case, including FORCE_QUOTE, which was not tested. Backpatch down to 17, where support for the all-column grammar with FORCE_NOT_NULL and FORCE_NULL has been added. Author: Joel Jacobson Reviewed-by: Zhang Mingli Discussion: https://postgr.es/m/65030d1d-5f90-4fa4-92eb-f5f50389858e@app.fastmail.com Backpatch-through: 17
1 parent 03bf0d9 commit 089aac6

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/backend/commands/copy.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -805,12 +805,14 @@ ProcessCopyOptions(ParseState *pstate,
805805
"COPY FROM")));
806806

807807
/* Check force_notnull */
808-
if (!opts_out->csv_mode && opts_out->force_notnull != NIL)
808+
if (!opts_out->csv_mode && (opts_out->force_notnull != NIL ||
809+
opts_out->force_notnull_all))
809810
ereport(ERROR,
810811
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
811812
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
812813
errmsg("COPY %s requires CSV mode", "FORCE_NOT_NULL")));
813-
if (opts_out->force_notnull != NIL && !is_from)
814+
if ((opts_out->force_notnull != NIL || opts_out->force_notnull_all) &&
815+
!is_from)
814816
ereport(ERROR,
815817
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
816818
/*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
@@ -819,13 +821,15 @@ ProcessCopyOptions(ParseState *pstate,
819821
"COPY TO")));
820822

821823
/* Check force_null */
822-
if (!opts_out->csv_mode && opts_out->force_null != NIL)
824+
if (!opts_out->csv_mode && (opts_out->force_null != NIL ||
825+
opts_out->force_null_all))
823826
ereport(ERROR,
824827
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
825828
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
826829
errmsg("COPY %s requires CSV mode", "FORCE_NULL")));
827830

828-
if (opts_out->force_null != NIL && !is_from)
831+
if ((opts_out->force_null != NIL || opts_out->force_null_all) &&
832+
!is_from)
829833
ereport(ERROR,
830834
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
831835
/*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,

src/test/regress/expected/copy2.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,28 @@ LINE 1: COPY x from stdin (on_error unsupported);
9898
^
9999
COPY x from stdin (format TEXT, force_quote(a));
100100
ERROR: COPY FORCE_QUOTE requires CSV mode
101+
COPY x from stdin (format TEXT, force_quote *);
102+
ERROR: COPY FORCE_QUOTE requires CSV mode
101103
COPY x from stdin (format CSV, force_quote(a));
102104
ERROR: COPY FORCE_QUOTE cannot be used with COPY FROM
105+
COPY x from stdin (format CSV, force_quote *);
106+
ERROR: COPY FORCE_QUOTE cannot be used with COPY FROM
103107
COPY x from stdin (format TEXT, force_not_null(a));
104108
ERROR: COPY FORCE_NOT_NULL requires CSV mode
109+
COPY x from stdin (format TEXT, force_not_null *);
110+
ERROR: COPY FORCE_NOT_NULL requires CSV mode
105111
COPY x to stdout (format CSV, force_not_null(a));
106112
ERROR: COPY FORCE_NOT_NULL cannot be used with COPY TO
113+
COPY x to stdout (format CSV, force_not_null *);
114+
ERROR: COPY FORCE_NOT_NULL cannot be used with COPY TO
107115
COPY x from stdin (format TEXT, force_null(a));
108116
ERROR: COPY FORCE_NULL requires CSV mode
117+
COPY x from stdin (format TEXT, force_null *);
118+
ERROR: COPY FORCE_NULL requires CSV mode
109119
COPY x to stdout (format CSV, force_null(a));
110120
ERROR: COPY FORCE_NULL cannot be used with COPY TO
121+
COPY x to stdout (format CSV, force_null *);
122+
ERROR: COPY FORCE_NULL cannot be used with COPY TO
111123
COPY x to stdout (format BINARY, on_error unsupported);
112124
ERROR: COPY ON_ERROR cannot be used with COPY TO
113125
LINE 1: COPY x to stdout (format BINARY, on_error unsupported);

src/test/regress/sql/copy2.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,17 @@ COPY x from stdin (format BINARY, null 'x');
7575
COPY x from stdin (format BINARY, on_error ignore);
7676
COPY x from stdin (on_error unsupported);
7777
COPY x from stdin (format TEXT, force_quote(a));
78+
COPY x from stdin (format TEXT, force_quote *);
7879
COPY x from stdin (format CSV, force_quote(a));
80+
COPY x from stdin (format CSV, force_quote *);
7981
COPY x from stdin (format TEXT, force_not_null(a));
82+
COPY x from stdin (format TEXT, force_not_null *);
8083
COPY x to stdout (format CSV, force_not_null(a));
84+
COPY x to stdout (format CSV, force_not_null *);
8185
COPY x from stdin (format TEXT, force_null(a));
86+
COPY x from stdin (format TEXT, force_null *);
8287
COPY x to stdout (format CSV, force_null(a));
88+
COPY x to stdout (format CSV, force_null *);
8389
COPY x to stdout (format BINARY, on_error unsupported);
8490
COPY x from stdin (log_verbosity unsupported);
8591
COPY x from stdin with (reject_limit 1);

0 commit comments

Comments
 (0)