This accompanies select_common_type() and select_common_collation().
Typmods were previously combined using hand-coded logic in several
places. The logic in select_common_typmod() isn't very exciting, but
it makes the code more compact and readable in a few locations, and in
the future we can perhaps do more complicated things if desired.
As a small enhancement, the type unification of the direct and
aggregate arguments of hypothetical-set aggregates now unifies the
typmod as well using this new function, instead of just dropping it.
Reviewed-by: Heikki Linnakangas <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/
97df3af9-8b5e-fb7f-a029-
3eb7e80d7af9@2ndquadrant.com
for (i = 0; i < sublist_length; i++)
{
Oid coltype;
- int32 coltypmod = -1;
+ int32 coltypmod;
Oid colcoll;
- bool first = true;
coltype = select_common_type(pstate, colexprs[i], "VALUES", NULL);
col = coerce_to_common_type(pstate, col, coltype, "VALUES");
lfirst(lc) = (void *) col;
- if (first)
- {
- coltypmod = exprTypmod(col);
- first = false;
- }
- else
- {
- /* As soon as we see a non-matching typmod, fall back to -1 */
- if (coltypmod >= 0 && coltypmod != exprTypmod(col))
- coltypmod = -1;
- }
}
+ coltypmod = select_common_typmod(pstate, colexprs[i], coltype);
colcoll = select_common_collation(pstate, colexprs[i], true);
coltypes = lappend_oid(coltypes, coltype);
Node *rcolnode = (Node *) rtle->expr;
Oid lcoltype = exprType(lcolnode);
Oid rcoltype = exprType(rcolnode);
- int32 lcoltypmod = exprTypmod(lcolnode);
- int32 rcoltypmod = exprTypmod(rcolnode);
Node *bestexpr;
int bestlocation;
Oid rescoltype;
context,
&bestexpr);
bestlocation = exprLocation(bestexpr);
- /* if same type and same typmod, use typmod; else default */
- if (lcoltype == rcoltype && lcoltypmod == rcoltypmod)
- rescoltypmod = lcoltypmod;
- else
- rescoltypmod = -1;
/*
* Verify the coercions are actually possible. If not, we'd fail
rtle->expr = (Expr *) rcolnode;
}
+ rescoltypmod = select_common_typmod(pstate,
+ list_make2(lcolnode, rcolnode),
+ rescoltype);
+
/*
* Select common collation. A common collation is required for
* all set operators except UNION ALL; see SQL:2008 7.13 <query
*r_node,
*res_node;
- /*
- * Choose output type if input types are dissimilar.
- */
- outcoltype = l_colvar->vartype;
- outcoltypmod = l_colvar->vartypmod;
- if (outcoltype != r_colvar->vartype)
- {
- outcoltype = select_common_type(pstate,
+ outcoltype = select_common_type(pstate,
+ list_make2(l_colvar, r_colvar),
+ "JOIN/USING",
+ NULL);
+ outcoltypmod = select_common_typmod(pstate,
list_make2(l_colvar, r_colvar),
- "JOIN/USING",
- NULL);
- outcoltypmod = -1; /* ie, unknown */
- }
- else if (outcoltypmod != r_colvar->vartypmod)
- {
- /* same type, but not same typmod */
- outcoltypmod = -1; /* ie, unknown */
- }
+ outcoltype);
/*
* Insert coercion functions if needed. Note that a difference in typmod
return node;
}
+/*
+ * select_common_typmod()
+ * Determine the common typmod of a list of input expressions.
+ *
+ * common_type is the selected common type of the expressions, typically
+ * computed using select_common_type().
+ */
+int32
+select_common_typmod(ParseState *pstate, List *exprs, Oid common_type)
+{
+ ListCell *lc;
+ bool first = true;
+ int32 result = -1;
+
+ foreach(lc, exprs)
+ {
+ Node *expr = (Node *) lfirst(lc);
+
+ /* Types must match */
+ if (exprType(expr) != common_type)
+ return -1;
+ else if (first)
+ {
+ result = exprTypmod(expr);
+ first = false;
+ }
+ else
+ {
+ /* As soon as we see a non-matching typmod, fall back to -1 */
+ if (result != exprTypmod(expr))
+ return -1;
+ }
+ }
+
+ return result;
+}
+
/*
* check_generic_type_consistency()
* Are the actual arguments potentially compatible with a
ListCell *harg = list_nth_cell(fargs, hargpos);
ListCell *aarg = list_nth_cell(fargs, aargpos);
Oid commontype;
+ int32 commontypmod;
/* A mismatch means AggregateCreate didn't check properly ... */
if (declared_arg_types[hargpos] != declared_arg_types[aargpos])
list_make2(lfirst(aarg), lfirst(harg)),
"WITHIN GROUP",
NULL);
+ commontypmod = select_common_typmod(pstate,
+ list_make2(lfirst(aarg), lfirst(harg)),
+ commontype);
/*
* Perform the coercions. We don't need to worry about NamedArgExprs
lfirst(harg) = coerce_type(pstate,
(Node *) lfirst(harg),
actual_arg_types[hargpos],
- commontype, -1,
+ commontype, commontypmod,
COERCION_IMPLICIT,
COERCE_IMPLICIT_CAST,
-1);
lfirst(aarg) = coerce_type(pstate,
(Node *) lfirst(aarg),
actual_arg_types[aargpos],
- commontype, -1,
+ commontype, commontypmod,
COERCION_IMPLICIT,
COERCE_IMPLICIT_CAST,
-1);
Oid targetTypeId,
const char *context);
+extern int32 select_common_typmod(ParseState *pstate, List *exprs, Oid common_type);
+
extern bool check_generic_type_consistency(const Oid *actual_arg_types,
const Oid *declared_arg_types,
int nargs);