1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-09 06:21:09 +03:00

Aggregates can be polymorphic, using polymorphic implementation functions.

It also works to create a non-polymorphic aggregate from polymorphic
functions, should you want to do that.  Regression test added, docs still
lacking.  By Joe Conway, with some kibitzing from Tom Lane.
This commit is contained in:
Tom Lane
2003-07-01 19:10:53 +00:00
parent 02b5d8e371
commit e3b1b6c0cd
15 changed files with 1300 additions and 83 deletions

View File

@@ -8,18 +8,21 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/parse_agg.c,v 1.53 2003/06/06 15:04:02 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/parser/parse_agg.c,v 1.54 2003/07/01 19:10:52 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "nodes/makefuncs.h"
#include "nodes/params.h"
#include "optimizer/clauses.h"
#include "optimizer/tlist.h"
#include "optimizer/var.h"
#include "parser/parse_agg.h"
#include "parser/parsetree.h"
#include "rewrite/rewriteManip.h"
#include "utils/lsyscache.h"
typedef struct
@@ -312,3 +315,91 @@ check_ungrouped_columns_walker(Node *node,
return expression_tree_walker(node, check_ungrouped_columns_walker,
(void *) context);
}
/*
* Create expression trees for the transition and final functions
* of an aggregate. These are needed so that polymorphic functions
* can be used within an aggregate --- without the expression trees,
* such functions would not know the datatypes they are supposed to use.
* (The trees will never actually be executed, however, so we can skimp
* a bit on correctness.)
*
* agg_input_type, agg_state_type, agg_result_type identify the input,
* transition, and result types of the aggregate. These should all be
* resolved to actual types (ie, none should ever be ANYARRAY or ANYELEMENT).
*
* transfn_oid and finalfn_oid identify the funcs to be called; the latter
* may be InvalidOid.
*
* Pointers to the constructed trees are returned into *transfnexpr and
* *finalfnexpr. The latter is set to NULL if there's no finalfn.
*/
void
build_aggregate_fnexprs(Oid agg_input_type,
Oid agg_state_type,
Oid agg_result_type,
Oid transfn_oid,
Oid finalfn_oid,
Expr **transfnexpr,
Expr **finalfnexpr)
{
Oid transfn_arg_types[FUNC_MAX_ARGS];
int transfn_nargs;
Param *arg0;
Param *arg1;
List *args;
/* get the transition function signature (only need nargs) */
(void) get_func_signature(transfn_oid, transfn_arg_types, &transfn_nargs);
/*
* Build arg list to use in the transfn FuncExpr node. We really
* only care that transfn can discover the actual argument types
* at runtime using get_fn_expr_argtype(), so it's okay to use
* Param nodes that don't correspond to any real Param.
*/
arg0 = makeNode(Param);
arg0->paramkind = PARAM_EXEC;
arg0->paramid = -1;
arg0->paramtype = agg_state_type;
if (transfn_nargs == 2)
{
arg1 = makeNode(Param);
arg1->paramkind = PARAM_EXEC;
arg1->paramid = -1;
arg1->paramtype = agg_input_type;
args = makeList2(arg0, arg1);
}
else
{
args = makeList1(arg0);
}
*transfnexpr = (Expr *) makeFuncExpr(transfn_oid,
agg_state_type,
args,
COERCE_DONTCARE);
/* see if we have a final function */
if (!OidIsValid(finalfn_oid))
{
*finalfnexpr = NULL;
return;
}
/*
* Build expr tree for final function
*/
arg0 = makeNode(Param);
arg0->paramkind = PARAM_EXEC;
arg0->paramid = -1;
arg0->paramtype = agg_state_type;
args = makeList1(arg0);
*finalfnexpr = (Expr *) makeFuncExpr(finalfn_oid,
agg_result_type,
args,
COERCE_DONTCARE);
}

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/parse_coerce.c,v 2.101 2003/06/27 00:33:25 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/parser/parse_coerce.c,v 2.102 2003/07/01 19:10:53 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -32,8 +32,6 @@
static Node *coerce_type_typmod(Node *node,
Oid targetTypeId, int32 targetTypMod,
CoercionForm cformat, bool isExplicit);
static Node *build_func_call(Oid funcid, Oid rettype, List *args,
CoercionForm fformat);
/*
@@ -275,8 +273,9 @@ coerce_type(ParseState *pstate, Node *node,
*/
Oid baseTypeId = getBaseType(targetTypeId);
result = build_func_call(funcId, baseTypeId, makeList1(node),
cformat);
result = (Node *) makeFuncExpr(funcId, baseTypeId,
makeList1(node),
cformat);
/*
* If domain, coerce to the domain type and relabel with
@@ -534,7 +533,7 @@ coerce_type_typmod(Node *node, Oid targetTypeId, int32 targetTypMod,
args = lappend(args, cons);
}
node = build_func_call(funcId, targetTypeId, args, cformat);
node = (Node *) makeFuncExpr(funcId, targetTypeId, args, cformat);
}
return node;
@@ -935,6 +934,76 @@ enforce_generic_type_consistency(Oid *actual_arg_types,
return rettype;
}
/*
* resolve_generic_type()
* Deduce an individual actual datatype on the assumption that
* the rules for ANYARRAY/ANYELEMENT are being followed.
*
* declared_type is the declared datatype we want to resolve.
* context_actual_type is the actual input datatype to some argument
* that has declared datatype context_declared_type.
*
* If declared_type isn't polymorphic, we just return it. Otherwise,
* context_declared_type must be polymorphic, and we deduce the correct
* return type based on the relationship of the two polymorphic types.
*/
Oid
resolve_generic_type(Oid declared_type,
Oid context_actual_type,
Oid context_declared_type)
{
if (declared_type == ANYARRAYOID)
{
if (context_declared_type == ANYARRAYOID)
{
/* Use actual type, but it must be an array */
Oid array_typelem = get_element_type(context_actual_type);
if (!OidIsValid(array_typelem))
elog(ERROR, "Argument declared ANYARRAY is not an array: %s",
format_type_be(context_actual_type));
return context_actual_type;
}
else if (context_declared_type == ANYELEMENTOID)
{
/* Use the array type corresponding to actual type */
Oid array_typeid = get_array_type(context_actual_type);
if (!OidIsValid(array_typeid))
elog(ERROR, "Cannot find array type for datatype %s",
format_type_be(context_actual_type));
return array_typeid;
}
}
else if (declared_type == ANYELEMENTOID)
{
if (context_declared_type == ANYARRAYOID)
{
/* Use the element type corresponding to actual type */
Oid array_typelem = get_element_type(context_actual_type);
if (!OidIsValid(array_typelem))
elog(ERROR, "Argument declared ANYARRAY is not an array: %s",
format_type_be(context_actual_type));
return array_typelem;
}
else if (context_declared_type == ANYELEMENTOID)
{
/* Use the actual type; it doesn't matter if array or not */
return context_actual_type;
}
}
else
{
/* declared_type isn't polymorphic, so return it as-is */
return declared_type;
}
/* If we get here, declared_type is polymorphic and context isn't */
/* NB: this is a calling-code logic error, not a user error */
elog(ERROR, "Cannot determine ANYARRAY/ANYELEMENT type because context isn't polymorphic");
return InvalidOid; /* keep compiler quiet */
}
/* TypeCategory()
* Assign a category to the specified type OID.
@@ -1417,23 +1486,3 @@ find_typmod_coercion_function(Oid typeId, int *nargs)
return funcid;
}
/*
* Build an expression tree representing a function call.
*
* The argument expressions must have been transformed already.
*/
static Node *
build_func_call(Oid funcid, Oid rettype, List *args, CoercionForm fformat)
{
FuncExpr *funcexpr;
funcexpr = makeNode(FuncExpr);
funcexpr->funcid = funcid;
funcexpr->funcresulttype = rettype;
funcexpr->funcretset = false; /* only possible case here */
funcexpr->funcformat = fformat;
funcexpr->args = args;
return (Node *) funcexpr;
}