diff options
| author | Tom Lane <tgl@sss.pgh.pa.us> | 2011-03-19 20:29:08 -0400 |
|---|---|---|
| committer | Tom Lane <tgl@sss.pgh.pa.us> | 2011-03-19 20:30:08 -0400 |
| commit | b310b6e31ce5aa9e456c43c0e8e93248b0c84c02 (patch) | |
| tree | e5168fcfdb231a9889e87e309f38a9e0f05a7896 /src/backend/commands | |
| parent | 025f4c72f029242a6aaf3f14bb6d7da4ce070f72 (diff) | |
| download | postgresql-b310b6e31ce5aa9e456c43c0e8e93248b0c84c02.tar.gz | |
Revise collation derivation method and expression-tree representation.
All expression nodes now have an explicit output-collation field, unless
they are known to only return a noncollatable data type (such as boolean
or record). Also, nodes that can invoke collation-aware functions store
a separate field that is the collation value to pass to the function.
This avoids confusion that arises when a function has collatable inputs
and noncollatable output type, or vice versa.
Also, replace the parser's on-the-fly collation assignment method with
a post-pass over the completed expression tree. This allows us to use
a more complex (and hopefully more nearly spec-compliant) assignment
rule without paying for it in extra storage in every expression node.
Fix assorted bugs in the planner's handling of collations by making
collation one of the defining properties of an EquivalenceClass and
by converting CollateExprs into discardable RelabelType nodes during
expression preprocessing.
Diffstat (limited to 'src/backend/commands')
| -rw-r--r-- | src/backend/commands/analyze.c | 4 | ||||
| -rw-r--r-- | src/backend/commands/functioncmds.c | 2 | ||||
| -rw-r--r-- | src/backend/commands/indexcmds.c | 25 | ||||
| -rw-r--r-- | src/backend/commands/prepare.c | 4 | ||||
| -rw-r--r-- | src/backend/commands/tablecmds.c | 4 | ||||
| -rw-r--r-- | src/backend/commands/typecmds.c | 7 | ||||
| -rw-r--r-- | src/backend/commands/view.c | 16 |
7 files changed, 52 insertions, 10 deletions
diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index a9acc7c303..774bb04471 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -1931,7 +1931,7 @@ compute_minimal_stats(VacAttrStatsP stats, fmgr_info(mystats->eqfunc, &f_cmpeq); /* We always use the default collation for statistics */ - fmgr_info_collation(DEFAULT_COLLATION_OID, &f_cmpeq); + fmgr_info_set_collation(DEFAULT_COLLATION_OID, &f_cmpeq); for (i = 0; i < samplerows; i++) { @@ -2254,7 +2254,7 @@ compute_scalar_stats(VacAttrStatsP stats, SelectSortFunction(mystats->ltopr, false, &cmpFn, &cmpFlags); fmgr_info(cmpFn, &f_cmpfn); /* We always use the default collation for statistics */ - fmgr_info_collation(DEFAULT_COLLATION_OID, &f_cmpfn); + fmgr_info_set_collation(DEFAULT_COLLATION_OID, &f_cmpfn); /* Initial scan to find sortable values */ for (i = 0; i < samplerows; i++) diff --git a/src/backend/commands/functioncmds.c b/src/backend/commands/functioncmds.c index a8ef947240..c8cbe035f0 100644 --- a/src/backend/commands/functioncmds.c +++ b/src/backend/commands/functioncmds.c @@ -51,6 +51,7 @@ #include "miscadmin.h" #include "optimizer/var.h" #include "parser/parse_coerce.h" +#include "parser/parse_collate.h" #include "parser/parse_expr.h" #include "parser/parse_func.h" #include "parser/parse_type.h" @@ -334,6 +335,7 @@ examine_parameter_list(List *parameters, Oid languageOid, def = transformExpr(pstate, fp->defexpr); def = coerce_to_specific_type(pstate, def, toid, "DEFAULT"); + assign_expr_collations(pstate, def); /* * Make sure no variables are referred to. diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index c8e21b68f5..163980bbfa 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -883,17 +883,34 @@ ComputeIndexAttrs(IndexInfo *indexInfo, } /* - * Collation override + * Apply collation override if any */ if (attribute->collation) + attcollation = get_collation_oid(attribute->collation, false); + + /* + * Check we have a collation iff it's a collatable type. The only + * expected failures here are (1) COLLATE applied to a noncollatable + * type, or (2) index expression had an unresolved collation. But + * we might as well code this to be a complete consistency check. + */ + if (type_is_collatable(atttype)) + { + if (!OidIsValid(attcollation)) + ereport(ERROR, + (errcode(ERRCODE_INDETERMINATE_COLLATION), + errmsg("no collation was derived for the index expression"), + errhint("Use the COLLATE clause to set the collation explicitly."))); + } + else { - if (!type_is_collatable(atttype)) + if (OidIsValid(attcollation)) ereport(ERROR, - (errcode(ERRCODE_SYNTAX_ERROR), + (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("collations are not supported by type %s", format_type_be(atttype)))); - attcollation = get_collation_oid(attribute->collation, false); } + collationOidP[attn] = attcollation; /* diff --git a/src/backend/commands/prepare.c b/src/backend/commands/prepare.c index 6115e8678c..adbf5872f3 100644 --- a/src/backend/commands/prepare.c +++ b/src/backend/commands/prepare.c @@ -23,6 +23,7 @@ #include "nodes/nodeFuncs.h" #include "parser/analyze.h" #include "parser/parse_coerce.h" +#include "parser/parse_collate.h" #include "parser/parse_expr.h" #include "parser/parse_type.h" #include "rewrite/rewriteHandler.h" @@ -368,6 +369,9 @@ EvaluateParams(PreparedStatement *pstmt, List *params, format_type_be(expected_type_id)), errhint("You will need to rewrite or cast the expression."))); + /* Take care of collations in the finished expression. */ + assign_expr_collations(pstate, expr); + lfirst(l) = expr; i++; } diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index ee34cfa97e..f677a8e8bd 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -58,6 +58,7 @@ #include "optimizer/clauses.h" #include "parser/parse_clause.h" #include "parser/parse_coerce.h" +#include "parser/parse_collate.h" #include "parser/parse_expr.h" #include "parser/parse_oper.h" #include "parser/parse_relation.h" @@ -6598,6 +6599,9 @@ ATPrepAlterColumnType(List **wqueue, errmsg("column \"%s\" cannot be cast to type %s", colName, format_type_be(targettype)))); + /* Fix collations after all else */ + assign_expr_collations(pstate, transform); + /* * Add a work queue item to make ATRewriteTable update the column * contents. diff --git a/src/backend/commands/typecmds.c b/src/backend/commands/typecmds.c index ee3bca17d1..4c06d898a8 100644 --- a/src/backend/commands/typecmds.c +++ b/src/backend/commands/typecmds.c @@ -54,6 +54,7 @@ #include "optimizer/planner.h" #include "optimizer/var.h" #include "parser/parse_coerce.h" +#include "parser/parse_collate.h" #include "parser/parse_expr.h" #include "parser/parse_func.h" #include "parser/parse_type.h" @@ -2341,6 +2342,7 @@ domainAddConstraint(Oid domainOid, Oid domainNamespace, Oid baseTypeOid, domVal = makeNode(CoerceToDomainValue); domVal->typeId = baseTypeOid; domVal->typeMod = typMod; + domVal->collation = get_typcollation(baseTypeOid); domVal->location = -1; /* will be set when/if used */ pstate->p_value_substitute = (Node *) domVal; @@ -2353,6 +2355,11 @@ domainAddConstraint(Oid domainOid, Oid domainNamespace, Oid baseTypeOid, expr = coerce_to_boolean(pstate, expr, "CHECK"); /* + * Fix up collation information. + */ + assign_expr_collations(pstate, expr); + + /* * Make sure no outside relations are referred to. */ if (list_length(pstate->p_rtable) != 0) diff --git a/src/backend/commands/view.c b/src/backend/commands/view.c index 794a56e84d..250d02605c 100644 --- a/src/backend/commands/view.c +++ b/src/backend/commands/view.c @@ -129,14 +129,22 @@ DefineVirtualRelation(const RangeVar *relation, List *tlist, bool replace) def->raw_default = NULL; def->cooked_default = NULL; def->collClause = NULL; + def->collOid = exprCollation((Node *) tle->expr); /* - * XXX Temporary kluge to make regression tests pass. We should - * be able to trust the result of exprCollation more than this. + * It's possible that the column is of a collatable type but the + * collation could not be resolved, so double-check. */ if (type_is_collatable(exprType((Node *) tle->expr))) - def->collOid = exprCollation((Node *) tle->expr); + { + if (!OidIsValid(def->collOid)) + ereport(ERROR, + (errcode(ERRCODE_INDETERMINATE_COLLATION), + errmsg("no collation was derived for view column \"%s\"", + def->colname), + errhint("Use the COLLATE clause to set the collation explicitly."))); + } else - def->collOid = InvalidOid; + Assert(!OidIsValid(def->collOid)); def->constraints = NIL; attrList = lappend(attrList, def); |
