Header And Logo

PostgreSQL
| The world's most advanced open source database.

Functions

parse_agg.h File Reference

#include "parser/parse_node.h"
Include dependency graph for parse_agg.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void transformAggregateCall (ParseState *pstate, Aggref *agg, List *args, List *aggorder, bool agg_distinct)
void transformWindowFuncCall (ParseState *pstate, WindowFunc *wfunc, WindowDef *windef)
void parseCheckAggregates (ParseState *pstate, Query *qry)
void build_aggregate_fnexprs (Oid *agg_input_types, int agg_num_inputs, Oid agg_state_type, Oid agg_result_type, Oid agg_input_collation, Oid transfn_oid, Oid finalfn_oid, Expr **transfnexpr, Expr **finalfnexpr)

Function Documentation

void build_aggregate_fnexprs ( Oid agg_input_types,
int  agg_num_inputs,
Oid  agg_state_type,
Oid  agg_result_type,
Oid  agg_input_collation,
Oid  transfn_oid,
Oid  finalfn_oid,
Expr **  transfnexpr,
Expr **  finalfnexpr 
)

Definition at line 957 of file parse_agg.c.

References COERCE_EXPLICIT_CALL, i, InvalidOid, lappend(), list_make1, Param::location, makeFuncExpr(), makeNode, OidIsValid, Param::paramcollid, Param::paramid, Param::paramkind, Param::paramtype, and Param::paramtypmod.

Referenced by ExecInitAgg(), and initialize_peragg().

{
    Param      *argp;
    List       *args;
    int         i;

    /*
     * 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.
     */
    argp = makeNode(Param);
    argp->paramkind = PARAM_EXEC;
    argp->paramid = -1;
    argp->paramtype = agg_state_type;
    argp->paramtypmod = -1;
    argp->paramcollid = agg_input_collation;
    argp->location = -1;

    args = list_make1(argp);

    for (i = 0; i < agg_num_inputs; i++)
    {
        argp = makeNode(Param);
        argp->paramkind = PARAM_EXEC;
        argp->paramid = -1;
        argp->paramtype = agg_input_types[i];
        argp->paramtypmod = -1;
        argp->paramcollid = agg_input_collation;
        argp->location = -1;
        args = lappend(args, argp);
    }

    *transfnexpr = (Expr *) makeFuncExpr(transfn_oid,
                                         agg_state_type,
                                         args,
                                         InvalidOid,
                                         agg_input_collation,
                                         COERCE_EXPLICIT_CALL);

    /* see if we have a final function */
    if (!OidIsValid(finalfn_oid))
    {
        *finalfnexpr = NULL;
        return;
    }

    /*
     * Build expr tree for final function
     */
    argp = makeNode(Param);
    argp->paramkind = PARAM_EXEC;
    argp->paramid = -1;
    argp->paramtype = agg_state_type;
    argp->paramtypmod = -1;
    argp->paramcollid = agg_input_collation;
    argp->location = -1;
    args = list_make1(argp);

    *finalfnexpr = (Expr *) makeFuncExpr(finalfn_oid,
                                         agg_result_type,
                                         args,
                                         InvalidOid,
                                         agg_input_collation,
                                         COERCE_EXPLICIT_CALL);
}

void parseCheckAggregates ( ParseState pstate,
Query qry 
)

Definition at line 646 of file parse_agg.c.

References Assert, check_ungrouped_columns(), CurrentMemoryContext, ereport, errcode(), errmsg(), ERROR, flatten_join_alias_vars(), get_sortgroupclause_expr(), Query::groupClause, PlannerInfo::hasJoinRTEs, Query::havingQual, IsA, lcons(), lfirst, locate_agg_of_level(), makeNode, NULL, ParseState::p_hasAggs, ParseState::p_rtable, PlannerInfo::parse, parser_errposition(), PlannerInfo::planner_cxt, RTE_CTE, RTE_JOIN, RangeTblEntry::rtekind, RangeTblEntry::self_reference, and Query::targetList.

Referenced by transformDeleteStmt(), transformSelectStmt(), and transformSetOperationStmt().

{
    List       *groupClauses = NIL;
    bool        have_non_var_grouping;
    List       *func_grouped_rels = NIL;
    ListCell   *l;
    bool        hasJoinRTEs;
    bool        hasSelfRefRTEs;
    PlannerInfo *root;
    Node       *clause;

    /* This should only be called if we found aggregates or grouping */
    Assert(pstate->p_hasAggs || qry->groupClause || qry->havingQual);

    /*
     * Scan the range table to see if there are JOIN or self-reference CTE
     * entries.  We'll need this info below.
     */
    hasJoinRTEs = hasSelfRefRTEs = false;
    foreach(l, pstate->p_rtable)
    {
        RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);

        if (rte->rtekind == RTE_JOIN)
            hasJoinRTEs = true;
        else if (rte->rtekind == RTE_CTE && rte->self_reference)
            hasSelfRefRTEs = true;
    }

    /*
     * Build a list of the acceptable GROUP BY expressions for use by
     * check_ungrouped_columns().
     */
    foreach(l, qry->groupClause)
    {
        SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
        Node       *expr;

        expr = get_sortgroupclause_expr(grpcl, qry->targetList);
        if (expr == NULL)
            continue;           /* probably cannot happen */
        groupClauses = lcons(expr, groupClauses);
    }

    /*
     * If there are join alias vars involved, we have to flatten them to the
     * underlying vars, so that aliased and unaliased vars will be correctly
     * taken as equal.  We can skip the expense of doing this if no rangetable
     * entries are RTE_JOIN kind. We use the planner's flatten_join_alias_vars
     * routine to do the flattening; it wants a PlannerInfo root node, which
     * fortunately can be mostly dummy.
     */
    if (hasJoinRTEs)
    {
        root = makeNode(PlannerInfo);
        root->parse = qry;
        root->planner_cxt = CurrentMemoryContext;
        root->hasJoinRTEs = true;

        groupClauses = (List *) flatten_join_alias_vars(root,
                                                      (Node *) groupClauses);
    }
    else
        root = NULL;            /* keep compiler quiet */

    /*
     * Detect whether any of the grouping expressions aren't simple Vars; if
     * they're all Vars then we don't have to work so hard in the recursive
     * scans.  (Note we have to flatten aliases before this.)
     */
    have_non_var_grouping = false;
    foreach(l, groupClauses)
    {
        if (!IsA((Node *) lfirst(l), Var))
        {
            have_non_var_grouping = true;
            break;
        }
    }

    /*
     * Check the targetlist and HAVING clause for ungrouped variables.
     *
     * Note: because we check resjunk tlist elements as well as regular ones,
     * this will also find ungrouped variables that came from ORDER BY and
     * WINDOW clauses.  For that matter, it's also going to examine the
     * grouping expressions themselves --- but they'll all pass the test ...
     */
    clause = (Node *) qry->targetList;
    if (hasJoinRTEs)
        clause = flatten_join_alias_vars(root, clause);
    check_ungrouped_columns(clause, pstate, qry,
                            groupClauses, have_non_var_grouping,
                            &func_grouped_rels);

    clause = (Node *) qry->havingQual;
    if (hasJoinRTEs)
        clause = flatten_join_alias_vars(root, clause);
    check_ungrouped_columns(clause, pstate, qry,
                            groupClauses, have_non_var_grouping,
                            &func_grouped_rels);

    /*
     * Per spec, aggregates can't appear in a recursive term.
     */
    if (pstate->p_hasAggs && hasSelfRefRTEs)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_RECURSION),
                 errmsg("aggregate functions are not allowed in a recursive query's recursive term"),
                 parser_errposition(pstate,
                                    locate_agg_of_level((Node *) qry, 0))));
}

void transformAggregateCall ( ParseState pstate,
Aggref agg,
List args,
List aggorder,
bool  agg_distinct 
)

Definition at line 77 of file parse_agg.c.

References _, Aggref::aggdistinct, Aggref::agglevelsup, Aggref::aggorder, arg, Aggref::args, Assert, check_agg_arguments(), ereport, errcode(), errdetail(), errmsg(), errmsg_internal(), ERROR, EXPR_KIND_ALTER_COL_TRANSFORM, EXPR_KIND_CHECK_CONSTRAINT, EXPR_KIND_COLUMN_DEFAULT, EXPR_KIND_DISTINCT_ON, EXPR_KIND_DOMAIN_CHECK, EXPR_KIND_EXECUTE_PARAMETER, EXPR_KIND_FROM_FUNCTION, EXPR_KIND_FROM_SUBSELECT, EXPR_KIND_FUNCTION_DEFAULT, EXPR_KIND_GROUP_BY, EXPR_KIND_HAVING, EXPR_KIND_INDEX_EXPRESSION, EXPR_KIND_INDEX_PREDICATE, EXPR_KIND_INSERT_TARGET, EXPR_KIND_JOIN_ON, EXPR_KIND_JOIN_USING, EXPR_KIND_LIMIT, EXPR_KIND_NONE, EXPR_KIND_OFFSET, EXPR_KIND_ORDER_BY, EXPR_KIND_OTHER, EXPR_KIND_RETURNING, EXPR_KIND_SELECT_TARGET, EXPR_KIND_TRIGGER_WHEN, EXPR_KIND_UPDATE_SOURCE, EXPR_KIND_UPDATE_TARGET, EXPR_KIND_VALUES, EXPR_KIND_WHERE, EXPR_KIND_WINDOW_FRAME_RANGE, EXPR_KIND_WINDOW_FRAME_ROWS, EXPR_KIND_WINDOW_ORDER, EXPR_KIND_WINDOW_PARTITION, exprLocation(), exprType(), format_type_be(), get_sortgroupclause_expr(), lappend(), lfirst, Aggref::location, makeTargetEntry(), NULL, OidIsValid, ParseState::p_expr_kind, ParseState::p_hasAggs, ParseState::p_lateral_active, ParseState::p_next_resno, ParseState::parentParseState, ParseExprKindName(), parser_errposition(), SortGroupClause::sortop, transformDistinctClause(), and transformSortClause().

Referenced by ParseFuncOrColumn().

{
    List       *tlist;
    List       *torder;
    List       *tdistinct = NIL;
    AttrNumber  attno;
    int         save_next_resno;
    int         min_varlevel;
    ListCell   *lc;
    const char *err;
    bool        errkind;

    /*
     * Transform the plain list of Exprs into a targetlist.  We don't bother
     * to assign column names to the entries.
     */
    tlist = NIL;
    attno = 1;
    foreach(lc, args)
    {
        Expr       *arg = (Expr *) lfirst(lc);
        TargetEntry *tle = makeTargetEntry(arg, attno++, NULL, false);

        tlist = lappend(tlist, tle);
    }

    /*
     * If we have an ORDER BY, transform it.  This will add columns to the
     * tlist if they appear in ORDER BY but weren't already in the arg list.
     * They will be marked resjunk = true so we can tell them apart from
     * regular aggregate arguments later.
     *
     * We need to mess with p_next_resno since it will be used to number any
     * new targetlist entries.
     */
    save_next_resno = pstate->p_next_resno;
    pstate->p_next_resno = attno;

    torder = transformSortClause(pstate,
                                 aggorder,
                                 &tlist,
                                 EXPR_KIND_ORDER_BY,
                                 true /* fix unknowns */ ,
                                 true /* force SQL99 rules */ );

    /*
     * If we have DISTINCT, transform that to produce a distinctList.
     */
    if (agg_distinct)
    {
        tdistinct = transformDistinctClause(pstate, &tlist, torder, true);

        /*
         * Remove this check if executor support for hashed distinct for
         * aggregates is ever added.
         */
        foreach(lc, tdistinct)
        {
            SortGroupClause *sortcl = (SortGroupClause *) lfirst(lc);

            if (!OidIsValid(sortcl->sortop))
            {
                Node       *expr = get_sortgroupclause_expr(sortcl, tlist);

                ereport(ERROR,
                        (errcode(ERRCODE_UNDEFINED_FUNCTION),
                errmsg("could not identify an ordering operator for type %s",
                       format_type_be(exprType(expr))),
                         errdetail("Aggregates with DISTINCT must be able to sort their inputs."),
                         parser_errposition(pstate, exprLocation(expr))));
            }
        }
    }

    /* Update the Aggref with the transformation results */
    agg->args = tlist;
    agg->aggorder = torder;
    agg->aggdistinct = tdistinct;

    pstate->p_next_resno = save_next_resno;

    /*
     * Check the arguments to compute the aggregate's level and detect
     * improper nesting.
     */
    min_varlevel = check_agg_arguments(pstate, agg->args);
    agg->agglevelsup = min_varlevel;

    /* Mark the correct pstate level as having aggregates */
    while (min_varlevel-- > 0)
        pstate = pstate->parentParseState;
    pstate->p_hasAggs = true;

    /*
     * Check to see if the aggregate function is in an invalid place within
     * its aggregation query.
     *
     * For brevity we support two schemes for reporting an error here: set
     * "err" to a custom message, or set "errkind" true if the error context
     * is sufficiently identified by what ParseExprKindName will return, *and*
     * what it will return is just a SQL keyword.  (Otherwise, use a custom
     * message to avoid creating translation problems.)
     */
    err = NULL;
    errkind = false;
    switch (pstate->p_expr_kind)
    {
        case EXPR_KIND_NONE:
            Assert(false);      /* can't happen */
            break;
        case EXPR_KIND_OTHER:
            /* Accept aggregate here; caller must throw error if wanted */
            break;
        case EXPR_KIND_JOIN_ON:
        case EXPR_KIND_JOIN_USING:
            err = _("aggregate functions are not allowed in JOIN conditions");
            break;
        case EXPR_KIND_FROM_SUBSELECT:
            /* Should only be possible in a LATERAL subquery */
            Assert(pstate->p_lateral_active);
            /* Aggregate scope rules make it worth being explicit here */
            err = _("aggregate functions are not allowed in FROM clause of their own query level");
            break;
        case EXPR_KIND_FROM_FUNCTION:
            err = _("aggregate functions are not allowed in functions in FROM");
            break;
        case EXPR_KIND_WHERE:
            errkind = true;
            break;
        case EXPR_KIND_HAVING:
            /* okay */
            break;
        case EXPR_KIND_WINDOW_PARTITION:
            /* okay */
            break;
        case EXPR_KIND_WINDOW_ORDER:
            /* okay */
            break;
        case EXPR_KIND_WINDOW_FRAME_RANGE:
            err = _("aggregate functions are not allowed in window RANGE");
            break;
        case EXPR_KIND_WINDOW_FRAME_ROWS:
            err = _("aggregate functions are not allowed in window ROWS");
            break;
        case EXPR_KIND_SELECT_TARGET:
            /* okay */
            break;
        case EXPR_KIND_INSERT_TARGET:
        case EXPR_KIND_UPDATE_SOURCE:
        case EXPR_KIND_UPDATE_TARGET:
            errkind = true;
            break;
        case EXPR_KIND_GROUP_BY:
            errkind = true;
            break;
        case EXPR_KIND_ORDER_BY:
            /* okay */
            break;
        case EXPR_KIND_DISTINCT_ON:
            /* okay */
            break;
        case EXPR_KIND_LIMIT:
        case EXPR_KIND_OFFSET:
            errkind = true;
            break;
        case EXPR_KIND_RETURNING:
            errkind = true;
            break;
        case EXPR_KIND_VALUES:
            errkind = true;
            break;
        case EXPR_KIND_CHECK_CONSTRAINT:
        case EXPR_KIND_DOMAIN_CHECK:
            err = _("aggregate functions are not allowed in check constraints");
            break;
        case EXPR_KIND_COLUMN_DEFAULT:
        case EXPR_KIND_FUNCTION_DEFAULT:
            err = _("aggregate functions are not allowed in DEFAULT expressions");
            break;
        case EXPR_KIND_INDEX_EXPRESSION:
            err = _("aggregate functions are not allowed in index expressions");
            break;
        case EXPR_KIND_INDEX_PREDICATE:
            err = _("aggregate functions are not allowed in index predicates");
            break;
        case EXPR_KIND_ALTER_COL_TRANSFORM:
            err = _("aggregate functions are not allowed in transform expressions");
            break;
        case EXPR_KIND_EXECUTE_PARAMETER:
            err = _("aggregate functions are not allowed in EXECUTE parameters");
            break;
        case EXPR_KIND_TRIGGER_WHEN:
            err = _("aggregate functions are not allowed in trigger WHEN conditions");
            break;

            /*
             * There is intentionally no default: case here, so that the
             * compiler will warn if we add a new ParseExprKind without
             * extending this switch.  If we do see an unrecognized value at
             * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
             * which is sane anyway.
             */
    }
    if (err)
        ereport(ERROR,
                (errcode(ERRCODE_GROUPING_ERROR),
                 errmsg_internal("%s", err),
                 parser_errposition(pstate, agg->location)));
    if (errkind)
        ereport(ERROR,
                (errcode(ERRCODE_GROUPING_ERROR),
                 /* translator: %s is name of a SQL construct, eg GROUP BY */
                 errmsg("aggregate functions are not allowed in %s",
                        ParseExprKindName(pstate->p_expr_kind)),
                 parser_errposition(pstate, agg->location)));
}

void transformWindowFuncCall ( ParseState pstate,
WindowFunc wfunc,
WindowDef windef 
)

Definition at line 429 of file parse_agg.c.

References _, WindowFunc::args, Assert, contain_windowfuncs(), WindowDef::endOffset, equal(), ereport, errcode(), errmsg(), errmsg_internal(), ERROR, EXPR_KIND_ALTER_COL_TRANSFORM, EXPR_KIND_CHECK_CONSTRAINT, EXPR_KIND_COLUMN_DEFAULT, EXPR_KIND_DISTINCT_ON, EXPR_KIND_DOMAIN_CHECK, EXPR_KIND_EXECUTE_PARAMETER, EXPR_KIND_FROM_FUNCTION, EXPR_KIND_FROM_SUBSELECT, EXPR_KIND_FUNCTION_DEFAULT, EXPR_KIND_GROUP_BY, EXPR_KIND_HAVING, EXPR_KIND_INDEX_EXPRESSION, EXPR_KIND_INDEX_PREDICATE, EXPR_KIND_INSERT_TARGET, EXPR_KIND_JOIN_ON, EXPR_KIND_JOIN_USING, EXPR_KIND_LIMIT, EXPR_KIND_NONE, EXPR_KIND_OFFSET, EXPR_KIND_ORDER_BY, EXPR_KIND_OTHER, EXPR_KIND_RETURNING, EXPR_KIND_SELECT_TARGET, EXPR_KIND_TRIGGER_WHEN, EXPR_KIND_UPDATE_SOURCE, EXPR_KIND_UPDATE_TARGET, EXPR_KIND_VALUES, EXPR_KIND_WHERE, EXPR_KIND_WINDOW_FRAME_RANGE, EXPR_KIND_WINDOW_FRAME_ROWS, EXPR_KIND_WINDOW_ORDER, EXPR_KIND_WINDOW_PARTITION, FRAMEOPTION_DEFAULTS, WindowDef::frameOptions, lappend(), lfirst, list_length(), locate_windowfunc(), WindowDef::location, WindowFunc::location, WindowDef::name, NIL, NULL, WindowDef::orderClause, ParseState::p_expr_kind, ParseState::p_hasWindowFuncs, ParseState::p_windowdefs, ParseExprKindName(), parser_errposition(), WindowDef::partitionClause, WindowDef::refname, WindowDef::startOffset, and WindowFunc::winref.

Referenced by ParseFuncOrColumn().

{
    const char *err;
    bool        errkind;

    /*
     * A window function call can't contain another one (but aggs are OK). XXX
     * is this required by spec, or just an unimplemented feature?
     */
    if (pstate->p_hasWindowFuncs &&
        contain_windowfuncs((Node *) wfunc->args))
        ereport(ERROR,
                (errcode(ERRCODE_WINDOWING_ERROR),
                 errmsg("window function calls cannot be nested"),
                 parser_errposition(pstate,
                                  locate_windowfunc((Node *) wfunc->args))));

    /*
     * Check to see if the window function is in an invalid place within the
     * query.
     *
     * For brevity we support two schemes for reporting an error here: set
     * "err" to a custom message, or set "errkind" true if the error context
     * is sufficiently identified by what ParseExprKindName will return, *and*
     * what it will return is just a SQL keyword.  (Otherwise, use a custom
     * message to avoid creating translation problems.)
     */
    err = NULL;
    errkind = false;
    switch (pstate->p_expr_kind)
    {
        case EXPR_KIND_NONE:
            Assert(false);      /* can't happen */
            break;
        case EXPR_KIND_OTHER:
            /* Accept window func here; caller must throw error if wanted */
            break;
        case EXPR_KIND_JOIN_ON:
        case EXPR_KIND_JOIN_USING:
            err = _("window functions are not allowed in JOIN conditions");
            break;
        case EXPR_KIND_FROM_SUBSELECT:
            /* can't get here, but just in case, throw an error */
            errkind = true;
            break;
        case EXPR_KIND_FROM_FUNCTION:
            err = _("window functions are not allowed in functions in FROM");
            break;
        case EXPR_KIND_WHERE:
            errkind = true;
            break;
        case EXPR_KIND_HAVING:
            errkind = true;
            break;
        case EXPR_KIND_WINDOW_PARTITION:
        case EXPR_KIND_WINDOW_ORDER:
        case EXPR_KIND_WINDOW_FRAME_RANGE:
        case EXPR_KIND_WINDOW_FRAME_ROWS:
            err = _("window functions are not allowed in window definitions");
            break;
        case EXPR_KIND_SELECT_TARGET:
            /* okay */
            break;
        case EXPR_KIND_INSERT_TARGET:
        case EXPR_KIND_UPDATE_SOURCE:
        case EXPR_KIND_UPDATE_TARGET:
            errkind = true;
            break;
        case EXPR_KIND_GROUP_BY:
            errkind = true;
            break;
        case EXPR_KIND_ORDER_BY:
            /* okay */
            break;
        case EXPR_KIND_DISTINCT_ON:
            /* okay */
            break;
        case EXPR_KIND_LIMIT:
        case EXPR_KIND_OFFSET:
            errkind = true;
            break;
        case EXPR_KIND_RETURNING:
            errkind = true;
            break;
        case EXPR_KIND_VALUES:
            errkind = true;
            break;
        case EXPR_KIND_CHECK_CONSTRAINT:
        case EXPR_KIND_DOMAIN_CHECK:
            err = _("window functions are not allowed in check constraints");
            break;
        case EXPR_KIND_COLUMN_DEFAULT:
        case EXPR_KIND_FUNCTION_DEFAULT:
            err = _("window functions are not allowed in DEFAULT expressions");
            break;
        case EXPR_KIND_INDEX_EXPRESSION:
            err = _("window functions are not allowed in index expressions");
            break;
        case EXPR_KIND_INDEX_PREDICATE:
            err = _("window functions are not allowed in index predicates");
            break;
        case EXPR_KIND_ALTER_COL_TRANSFORM:
            err = _("window functions are not allowed in transform expressions");
            break;
        case EXPR_KIND_EXECUTE_PARAMETER:
            err = _("window functions are not allowed in EXECUTE parameters");
            break;
        case EXPR_KIND_TRIGGER_WHEN:
            err = _("window functions are not allowed in trigger WHEN conditions");
            break;

            /*
             * There is intentionally no default: case here, so that the
             * compiler will warn if we add a new ParseExprKind without
             * extending this switch.  If we do see an unrecognized value at
             * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
             * which is sane anyway.
             */
    }
    if (err)
        ereport(ERROR,
                (errcode(ERRCODE_WINDOWING_ERROR),
                 errmsg_internal("%s", err),
                 parser_errposition(pstate, wfunc->location)));
    if (errkind)
        ereport(ERROR,
                (errcode(ERRCODE_WINDOWING_ERROR),
                 /* translator: %s is name of a SQL construct, eg GROUP BY */
                 errmsg("window functions are not allowed in %s",
                        ParseExprKindName(pstate->p_expr_kind)),
                 parser_errposition(pstate, wfunc->location)));

    /*
     * If the OVER clause just specifies a window name, find that WINDOW
     * clause (which had better be present).  Otherwise, try to match all the
     * properties of the OVER clause, and make a new entry in the p_windowdefs
     * list if no luck.
     */
    if (windef->name)
    {
        Index       winref = 0;
        ListCell   *lc;

        Assert(windef->refname == NULL &&
               windef->partitionClause == NIL &&
               windef->orderClause == NIL &&
               windef->frameOptions == FRAMEOPTION_DEFAULTS);

        foreach(lc, pstate->p_windowdefs)
        {
            WindowDef  *refwin = (WindowDef *) lfirst(lc);

            winref++;
            if (refwin->name && strcmp(refwin->name, windef->name) == 0)
            {
                wfunc->winref = winref;
                break;
            }
        }
        if (lc == NULL)         /* didn't find it? */
            ereport(ERROR,
                    (errcode(ERRCODE_UNDEFINED_OBJECT),
                     errmsg("window \"%s\" does not exist", windef->name),
                     parser_errposition(pstate, windef->location)));
    }
    else
    {
        Index       winref = 0;
        ListCell   *lc;

        foreach(lc, pstate->p_windowdefs)
        {
            WindowDef  *refwin = (WindowDef *) lfirst(lc);

            winref++;
            if (refwin->refname && windef->refname &&
                strcmp(refwin->refname, windef->refname) == 0)
                 /* matched on refname */ ;
            else if (!refwin->refname && !windef->refname)
                 /* matched, no refname */ ;
            else
                continue;
            if (equal(refwin->partitionClause, windef->partitionClause) &&
                equal(refwin->orderClause, windef->orderClause) &&
                refwin->frameOptions == windef->frameOptions &&
                equal(refwin->startOffset, windef->startOffset) &&
                equal(refwin->endOffset, windef->endOffset))
            {
                /* found a duplicate window specification */
                wfunc->winref = winref;
                break;
            }
        }
        if (lc == NULL)         /* didn't find it? */
        {
            pstate->p_windowdefs = lappend(pstate->p_windowdefs, windef);
            wfunc->winref = list_length(pstate->p_windowdefs);
        }
    }

    pstate->p_hasWindowFuncs = true;
}