Skip to content

Commit ba62076

Browse files
committed
Improve error checking of CREATE COLLATION options.
Check for conflicting or redundant options, as we do for most other commands. Specifying any option more than once is at best redundant, and quite likely indicates a bug in the user's code. While at it, improve the error for conflicting locale options by adding detail text (the same as for CREATE DATABASE). Bharath Rupireddy, reviewed by Vignesh C. Some additional hacking by me. Discussion: https://postgr.es/m/CALj2ACWtL6fTLdyF4R_YkPtf1YEDb6FUoD5DGAki3rpD+sWqiA@mail.gmail.com
1 parent 8589299 commit ba62076

File tree

3 files changed

+80
-5
lines changed

3 files changed

+80
-5
lines changed

src/backend/commands/collationcmds.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,22 @@ DefineCollation(ParseState *pstate, List *names, List *parameters, bool if_not_e
108108
parser_errposition(pstate, defel->location)));
109109
break;
110110
}
111-
111+
if (*defelp != NULL)
112+
errorConflictingDefElem(defel, pstate);
112113
*defelp = defel;
113114
}
114115

115-
if ((localeEl && (lccollateEl || lcctypeEl))
116-
|| (fromEl && list_length(parameters) != 1))
116+
if (localeEl && (lccollateEl || lcctypeEl))
117+
ereport(ERROR,
118+
errcode(ERRCODE_SYNTAX_ERROR),
119+
errmsg("conflicting or redundant options"),
120+
errdetail("LOCALE cannot be specified together with LC_COLLATE or LC_CTYPE."));
121+
122+
if (fromEl && list_length(parameters) != 1)
117123
ereport(ERROR,
118-
(errcode(ERRCODE_SYNTAX_ERROR),
119-
errmsg("conflicting or redundant options")));
124+
errcode(ERRCODE_SYNTAX_ERROR),
125+
errmsg("conflicting or redundant options"),
126+
errdetail("FROM cannot be specified together with any other options."));
120127

121128
if (fromEl)
122129
{

src/test/regress/expected/collate.out

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,53 @@ View definition:
701701
SELECT ss.c1 + 1 AS c1p
702702
FROM ( SELECT 4 AS c1) ss;
703703

704+
-- Check conflicting or redundant options in CREATE COLLATION
705+
-- LC_COLLATE
706+
CREATE COLLATION coll_dup_chk (LC_COLLATE = "POSIX", LC_COLLATE = "NONSENSE", LC_CTYPE = "POSIX");
707+
ERROR: conflicting or redundant options
708+
LINE 1: ...ATE COLLATION coll_dup_chk (LC_COLLATE = "POSIX", LC_COLLATE...
709+
^
710+
-- LC_CTYPE
711+
CREATE COLLATION coll_dup_chk (LC_CTYPE = "POSIX", LC_CTYPE = "NONSENSE", LC_COLLATE = "POSIX");
712+
ERROR: conflicting or redundant options
713+
LINE 1: ...REATE COLLATION coll_dup_chk (LC_CTYPE = "POSIX", LC_CTYPE =...
714+
^
715+
-- PROVIDER
716+
CREATE COLLATION coll_dup_chk (PROVIDER = icu, PROVIDER = NONSENSE, LC_COLLATE = "POSIX", LC_CTYPE = "POSIX");
717+
ERROR: conflicting or redundant options
718+
LINE 1: CREATE COLLATION coll_dup_chk (PROVIDER = icu, PROVIDER = NO...
719+
^
720+
-- LOCALE
721+
CREATE COLLATION case_sensitive (LOCALE = '', LOCALE = "NONSENSE");
722+
ERROR: conflicting or redundant options
723+
LINE 1: CREATE COLLATION case_sensitive (LOCALE = '', LOCALE = "NONS...
724+
^
725+
-- DETERMINISTIC
726+
CREATE COLLATION coll_dup_chk (DETERMINISTIC = TRUE, DETERMINISTIC = NONSENSE, LOCALE = '');
727+
ERROR: conflicting or redundant options
728+
LINE 1: ...ATE COLLATION coll_dup_chk (DETERMINISTIC = TRUE, DETERMINIS...
729+
^
730+
-- VERSION
731+
CREATE COLLATION coll_dup_chk (VERSION = '1', VERSION = "NONSENSE", LOCALE = '');
732+
ERROR: conflicting or redundant options
733+
LINE 1: CREATE COLLATION coll_dup_chk (VERSION = '1', VERSION = "NON...
734+
^
735+
-- LOCALE conflicts with LC_COLLATE and LC_CTYPE
736+
CREATE COLLATION coll_dup_chk (LC_COLLATE = "POSIX", LC_CTYPE = "POSIX", LOCALE = '');
737+
ERROR: conflicting or redundant options
738+
DETAIL: LOCALE cannot be specified together with LC_COLLATE or LC_CTYPE.
739+
-- LOCALE conflicts with LC_COLLATE
740+
CREATE COLLATION coll_dup_chk (LC_COLLATE = "POSIX", LOCALE = '');
741+
ERROR: conflicting or redundant options
742+
DETAIL: LOCALE cannot be specified together with LC_COLLATE or LC_CTYPE.
743+
-- LOCALE conflicts with LC_CTYPE
744+
CREATE COLLATION coll_dup_chk (LC_CTYPE = "POSIX", LOCALE = '');
745+
ERROR: conflicting or redundant options
746+
DETAIL: LOCALE cannot be specified together with LC_COLLATE or LC_CTYPE.
747+
-- FROM conflicts with any other option
748+
CREATE COLLATION coll_dup_chk (FROM = "C", VERSION = "1");
749+
ERROR: conflicting or redundant options
750+
DETAIL: FROM cannot be specified together with any other options.
704751
--
705752
-- Clean up. Many of these table names will be re-used if the user is
706753
-- trying to run any platform-specific collation tests later, so we

src/test/regress/sql/collate.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,27 @@ SELECT c1+1 AS c1p FROM
272272
(SELECT ('4' COLLATE "C")::INT AS c1) ss;
273273
\d+ collate_on_int
274274

275+
-- Check conflicting or redundant options in CREATE COLLATION
276+
-- LC_COLLATE
277+
CREATE COLLATION coll_dup_chk (LC_COLLATE = "POSIX", LC_COLLATE = "NONSENSE", LC_CTYPE = "POSIX");
278+
-- LC_CTYPE
279+
CREATE COLLATION coll_dup_chk (LC_CTYPE = "POSIX", LC_CTYPE = "NONSENSE", LC_COLLATE = "POSIX");
280+
-- PROVIDER
281+
CREATE COLLATION coll_dup_chk (PROVIDER = icu, PROVIDER = NONSENSE, LC_COLLATE = "POSIX", LC_CTYPE = "POSIX");
282+
-- LOCALE
283+
CREATE COLLATION case_sensitive (LOCALE = '', LOCALE = "NONSENSE");
284+
-- DETERMINISTIC
285+
CREATE COLLATION coll_dup_chk (DETERMINISTIC = TRUE, DETERMINISTIC = NONSENSE, LOCALE = '');
286+
-- VERSION
287+
CREATE COLLATION coll_dup_chk (VERSION = '1', VERSION = "NONSENSE", LOCALE = '');
288+
-- LOCALE conflicts with LC_COLLATE and LC_CTYPE
289+
CREATE COLLATION coll_dup_chk (LC_COLLATE = "POSIX", LC_CTYPE = "POSIX", LOCALE = '');
290+
-- LOCALE conflicts with LC_COLLATE
291+
CREATE COLLATION coll_dup_chk (LC_COLLATE = "POSIX", LOCALE = '');
292+
-- LOCALE conflicts with LC_CTYPE
293+
CREATE COLLATION coll_dup_chk (LC_CTYPE = "POSIX", LOCALE = '');
294+
-- FROM conflicts with any other option
295+
CREATE COLLATION coll_dup_chk (FROM = "C", VERSION = "1");
275296

276297
--
277298
-- Clean up. Many of these table names will be re-used if the user is

0 commit comments

Comments
 (0)