1
0
mirror of https://github.com/postgres/postgres.git synced 2025-10-24 01:29:19 +03:00

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.
This commit is contained in:
Tom Lane
2011-03-19 20:29:08 -04:00
parent 025f4c72f0
commit b310b6e31c
73 changed files with 2365 additions and 1084 deletions

View File

@@ -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++)

View File

@@ -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.

View File

@@ -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 (!type_is_collatable(atttype))
if (!OidIsValid(attcollation))
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_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 (OidIsValid(attcollation))
ereport(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;
/*

View File

@@ -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++;
}

View File

@@ -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.

View File

@@ -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;
@@ -2352,6 +2354,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.
*/

View File

@@ -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);