After testing this feature, I noticed that FORCE_NULL and
FORCE_NOT_NULL can both be specified with COPY on the same column.
This does not seem correct. The attached patch adds some more error
handling, and a regression test case for that.
Regards,
--
Michael
diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index 70ee7e5..540da91 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -1457,6 +1457,26 @@ BeginCopy(bool is_from,
}
}
+ /*
+ * Check if both force_null and force_not_null are used on the same
+ * columns.
+ */
+ if (cstate->force_null && cstate->force_notnull)
+ {
+ int i;
+
+ for (i = 0; i < num_phys_attrs; i++)
+ {
+ if (cstate->force_notnull_flags[i] &&
+ cstate->force_null_flags[i])
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("conflicting or
redundant options"),
+ errhint("\"force_not_null\"
and \"force_null\" specified for the same column \"%s\"",
+
NameStr(tupDesc->attrs[i]->attname))));
+ }
+ }
+
/* Use client encoding when ENCODING option is not specified. */
if (cstate->file_encoding < 0)
cstate->file_encoding = pg_get_client_encoding();
diff --git a/src/test/regress/expected/copy2.out
b/src/test/regress/expected/copy2.out
index 76dea28..5341b09 100644
--- a/src/test/regress/expected/copy2.out
+++ b/src/test/regress/expected/copy2.out
@@ -418,6 +418,12 @@ ERROR: null value in column "b" violates not-null
constraint
DETAIL: Failing row contains (3, null, , null, null).
CONTEXT: COPY forcetest, line 1: "3,,"""
ROLLBACK;
+-- FORCE_NULL and FORCE_NOT_NULL cannot be used on the same column
+BEGIN;
+COPY forcetest (a) FROM STDIN WITH (FORMAT csv, FORCE_NULL(a),
FORCE_NOT_NULL(a));
+ERROR: conflicting or redundant options
+HINT: "force_not_null" and "force_null" specified for the same column "a"
+ROLLBACK;
-- should fail with "not referenced by COPY" error
BEGIN;
COPY forcetest (d, e) FROM STDIN WITH (FORMAT csv, FORCE_NOT_NULL(b));
diff --git a/src/test/regress/sql/copy2.sql b/src/test/regress/sql/copy2.sql
index e2be21f..91dc902 100644
--- a/src/test/regress/sql/copy2.sql
+++ b/src/test/regress/sql/copy2.sql
@@ -299,6 +299,10 @@ COPY forcetest (a, b, c) FROM STDIN WITH (FORMAT csv,
FORCE_NULL(b), FORCE_NOT_N
3,,""
\.
ROLLBACK;
+-- FORCE_NULL and FORCE_NOT_NULL cannot be used on the same column
+BEGIN;
+COPY forcetest (a) FROM STDIN WITH (FORMAT csv, FORCE_NULL(a),
FORCE_NOT_NULL(a));
+ROLLBACK;
-- should fail with "not referenced by COPY" error
BEGIN;
COPY forcetest (d, e) FROM STDIN WITH (FORMAT csv, FORCE_NOT_NULL(b));
--
Sent via pgsql-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers