Header And Logo

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

Data Structures | Typedefs | Enumerations | Functions

rewriteManip.h File Reference

#include "nodes/parsenodes.h"
Include dependency graph for rewriteManip.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  replace_rte_variables_context

Typedefs

typedef struct
replace_rte_variables_context 
replace_rte_variables_context
typedef Node *(* replace_rte_variables_callback )(Var *var, replace_rte_variables_context *context)
typedef enum
ReplaceVarsNoMatchOption 
ReplaceVarsNoMatchOption

Enumerations

enum  ReplaceVarsNoMatchOption { REPLACEVARS_REPORT_ERROR, REPLACEVARS_CHANGE_VARNO, REPLACEVARS_SUBSTITUTE_NULL }

Functions

void OffsetVarNodes (Node *node, int offset, int sublevels_up)
void ChangeVarNodes (Node *node, int old_varno, int new_varno, int sublevels_up)
void IncrementVarSublevelsUp (Node *node, int delta_sublevels_up, int min_sublevels_up)
void IncrementVarSublevelsUp_rtable (List *rtable, int delta_sublevels_up, int min_sublevels_up)
bool rangeTableEntry_used (Node *node, int rt_index, int sublevels_up)
bool attribute_used (Node *node, int rt_index, int attno, int sublevels_up)
QuerygetInsertSelectQuery (Query *parsetree, Query ***subquery_ptr)
void AddQual (Query *parsetree, Node *qual)
void AddInvertedQual (Query *parsetree, Node *qual)
bool contain_aggs_of_level (Node *node, int levelsup)
int locate_agg_of_level (Node *node, int levelsup)
bool contain_windowfuncs (Node *node)
int locate_windowfunc (Node *node)
bool checkExprHasSubLink (Node *node)
Nodereplace_rte_variables (Node *node, int target_varno, int sublevels_up, replace_rte_variables_callback callback, void *callback_arg, bool *outer_hasSubLinks)
Nodereplace_rte_variables_mutator (Node *node, replace_rte_variables_context *context)
Nodemap_variable_attnos (Node *node, int target_varno, int sublevels_up, const AttrNumber *attno_map, int map_length, bool *found_whole_row)
NodeReplaceVarsFromTargetList (Node *node, int target_varno, int sublevels_up, RangeTblEntry *target_rte, List *targetlist, ReplaceVarsNoMatchOption nomatch_option, int nomatch_varno, bool *outer_hasSubLinks)

Typedef Documentation

Definition at line 22 of file rewriteManip.h.

Definition at line 20 of file rewriteManip.h.


Enumeration Type Documentation

Enumerator:
REPLACEVARS_REPORT_ERROR 
REPLACEVARS_CHANGE_VARNO 
REPLACEVARS_SUBSTITUTE_NULL 

Definition at line 34 of file rewriteManip.h.

{
    REPLACEVARS_REPORT_ERROR,   /* throw error if no match */
    REPLACEVARS_CHANGE_VARNO,   /* change the Var's varno, nothing else */
    REPLACEVARS_SUBSTITUTE_NULL /* replace with a NULL Const */
} ReplaceVarsNoMatchOption;


Function Documentation

void AddInvertedQual ( Query parsetree,
Node qual 
)

Definition at line 1061 of file rewriteManip.c.

References AddQual(), BooleanTest::arg, BooleanTest::booltesttype, makeNode, and NULL.

Referenced by CopyAndAddInvertedQual().

{
    BooleanTest *invqual;

    if (qual == NULL)
        return;

    /* Need not copy input qual, because AddQual will... */
    invqual = makeNode(BooleanTest);
    invqual->arg = (Expr *) qual;
    invqual->booltesttype = IS_NOT_TRUE;

    AddQual(parsetree, (Node *) invqual);
}

void AddQual ( Query parsetree,
Node qual 
)

Definition at line 993 of file rewriteManip.c.

References Assert, checkExprHasSubLink(), CMD_UTILITY, Query::commandType, contain_aggs_of_level(), copyObject(), ereport, errcode(), errmsg(), ERROR, Query::hasSubLinks, IsA, Query::jointree, make_and_qual(), NULL, FromExpr::quals, Query::setOperations, and Query::utilityStmt.

Referenced by AddInvertedQual(), rewriteRuleAction(), and rewriteTargetView().

{
    Node       *copy;

    if (qual == NULL)
        return;

    if (parsetree->commandType == CMD_UTILITY)
    {
        /*
         * There's noplace to put the qual on a utility statement.
         *
         * If it's a NOTIFY, silently ignore the qual; this means that the
         * NOTIFY will execute, whether or not there are any qualifying rows.
         * While clearly wrong, this is much more useful than refusing to
         * execute the rule at all, and extra NOTIFY events are harmless for
         * typical uses of NOTIFY.
         *
         * If it isn't a NOTIFY, error out, since unconditional execution of
         * other utility stmts is unlikely to be wanted.  (This case is not
         * currently allowed anyway, but keep the test for safety.)
         */
        if (parsetree->utilityStmt && IsA(parsetree->utilityStmt, NotifyStmt))
            return;
        else
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
              errmsg("conditional utility statements are not implemented")));
    }

    if (parsetree->setOperations != NULL)
    {
        /*
         * There's noplace to put the qual on a setop statement, either. (This
         * could be fixed, but right now the planner simply ignores any qual
         * condition on a setop query.)
         */
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("conditional UNION/INTERSECT/EXCEPT statements are not implemented")));
    }

    /* INTERSECT want's the original, but we need to copy - Jan */
    copy = copyObject(qual);

    parsetree->jointree->quals = make_and_qual(parsetree->jointree->quals,
                                               copy);

    /*
     * We had better not have stuck an aggregate into the WHERE clause.
     */
    Assert(!contain_aggs_of_level(copy, 0));

    /*
     * Make sure query is marked correctly if added qual has sublinks. Need
     * not search qual when query is already marked.
     */
    if (!parsetree->hasSubLinks)
        parsetree->hasSubLinks = checkExprHasSubLink(copy);
}

bool attribute_used ( Node node,
int  rt_index,
int  attno,
int  sublevels_up 
)

Definition at line 905 of file rewriteManip.c.

References attribute_used_context::attno, attribute_used_walker(), query_or_expression_tree_walker(), attribute_used_context::rt_index, and attribute_used_context::sublevels_up.

Referenced by fireRIRrules(), and matchLocks().

{
    attribute_used_context context;

    context.rt_index = rt_index;
    context.attno = attno;
    context.sublevels_up = sublevels_up;

    /*
     * Must be prepared to start with a Query or a bare expression tree; if
     * it's a Query, we don't want to increment sublevels_up.
     */
    return query_or_expression_tree_walker(node,
                                           attribute_used_walker,
                                           (void *) &context,
                                           0);
}

void ChangeVarNodes ( Node node,
int  old_varno,
int  new_varno,
int  sublevels_up 
)

Definition at line 572 of file rewriteManip.c.

References ChangeVarNodes_walker(), IsA, lfirst, ChangeVarNodes_context::new_index, query_tree_walker(), Query::resultRelation, Query::rowMarks, ChangeVarNodes_context::rt_index, RowMarkClause::rti, and ChangeVarNodes_context::sublevels_up.

Referenced by ApplyRetrieveRule(), CopyAndAddInvertedQual(), get_relation_constraints(), get_relation_info(), inheritance_planner(), rewriteRuleAction(), rewriteTargetView(), and TriggerEnabled().

{
    ChangeVarNodes_context context;

    context.rt_index = rt_index;
    context.new_index = new_index;
    context.sublevels_up = sublevels_up;

    /*
     * Must be prepared to start with a Query or a bare expression tree; if
     * it's a Query, go straight to query_tree_walker to make sure that
     * sublevels_up doesn't get incremented prematurely.
     */
    if (node && IsA(node, Query))
    {
        Query      *qry = (Query *) node;

        /*
         * If we are starting at a Query, and sublevels_up is zero, then we
         * must also fix rangetable indexes in the Query itself --- namely
         * resultRelation and rowMarks entries.  sublevels_up cannot be zero
         * when recursing into a subquery, so there's no need to have the same
         * logic inside ChangeVarNodes_walker.
         */
        if (sublevels_up == 0)
        {
            ListCell   *l;

            if (qry->resultRelation == rt_index)
                qry->resultRelation = new_index;
            foreach(l, qry->rowMarks)
            {
                RowMarkClause *rc = (RowMarkClause *) lfirst(l);

                if (rc->rti == rt_index)
                    rc->rti = new_index;
            }
        }
        query_tree_walker(qry, ChangeVarNodes_walker,
                          (void *) &context, 0);
    }
    else
        ChangeVarNodes_walker(node, &context);
}

bool checkExprHasSubLink ( Node node  ) 

Definition at line 262 of file rewriteManip.c.

References checkExprHasSubLink_walker(), NULL, QTW_IGNORE_RC_SUBQUERIES, and query_or_expression_tree_walker().

Referenced by AddQual(), flatten_join_alias_vars_mutator(), replace_rte_variables_mutator(), and rewriteRuleAction().

{
    /*
     * If a Query is passed, examine it --- but we should not recurse into
     * sub-Queries that are in its rangetable or CTE list.
     */
    return query_or_expression_tree_walker(node,
                                           checkExprHasSubLink_walker,
                                           NULL,
                                           QTW_IGNORE_RC_SUBQUERIES);
}

bool contain_aggs_of_level ( Node node,
int  levelsup 
)

Definition at line 67 of file rewriteManip.c.

References contain_aggs_of_level_walker(), query_or_expression_tree_walker(), and contain_aggs_of_level_context::sublevels_up.

Referenced by AddQual(), checkTargetlistEntrySQL92(), and convert_EXISTS_to_ANY().

{
    contain_aggs_of_level_context context;

    context.sublevels_up = levelsup;

    /*
     * Must be prepared to start with a Query or a bare expression tree; if
     * it's a Query, we don't want to increment sublevels_up.
     */
    return query_or_expression_tree_walker(node,
                                           contain_aggs_of_level_walker,
                                           (void *) &context,
                                           0);
}

bool contain_windowfuncs ( Node node  ) 

Definition at line 182 of file rewriteManip.c.

References contain_windowfuncs_walker(), NULL, and query_or_expression_tree_walker().

Referenced by checkTargetlistEntrySQL92(), contain_window_function(), and transformWindowFuncCall().

{
    /*
     * Must be prepared to start with a Query or a bare expression tree; if
     * it's a Query, we don't want to increment sublevels_up.
     */
    return query_or_expression_tree_walker(node,
                                           contain_windowfuncs_walker,
                                           NULL,
                                           0);
}

Query* getInsertSelectQuery ( Query parsetree,
Query ***  subquery_ptr 
)

Definition at line 938 of file rewriteManip.c.

References Assert, CMD_INSERT, CMD_SELECT, Query::commandType, elog, ERROR, FromExpr::fromlist, IsA, Query::jointree, linitial, list_length(), NULL, PRS2_NEW_VARNO, PRS2_OLD_VARNO, rt_fetch, Query::rtable, RangeTblRef::rtindex, and RangeTblEntry::subquery.

Referenced by DefineQueryRewrite(), InsertRule(), make_ruledef(), rewriteRuleAction(), and transformRuleStmt().

{
    Query      *selectquery;
    RangeTblEntry *selectrte;
    RangeTblRef *rtr;

    if (subquery_ptr)
        *subquery_ptr = NULL;

    if (parsetree == NULL)
        return parsetree;
    if (parsetree->commandType != CMD_INSERT)
        return parsetree;

    /*
     * Currently, this is ONLY applied to rule-action queries, and so we
     * expect to find the OLD and NEW placeholder entries in the given query.
     * If they're not there, it must be an INSERT/SELECT in which they've been
     * pushed down to the SELECT.
     */
    if (list_length(parsetree->rtable) >= 2 &&
        strcmp(rt_fetch(PRS2_OLD_VARNO, parsetree->rtable)->eref->aliasname,
               "old") == 0 &&
        strcmp(rt_fetch(PRS2_NEW_VARNO, parsetree->rtable)->eref->aliasname,
               "new") == 0)
        return parsetree;
    Assert(parsetree->jointree && IsA(parsetree->jointree, FromExpr));
    if (list_length(parsetree->jointree->fromlist) != 1)
        elog(ERROR, "expected to find SELECT subquery");
    rtr = (RangeTblRef *) linitial(parsetree->jointree->fromlist);
    Assert(IsA(rtr, RangeTblRef));
    selectrte = rt_fetch(rtr->rtindex, parsetree->rtable);
    selectquery = selectrte->subquery;
    if (!(selectquery && IsA(selectquery, Query) &&
          selectquery->commandType == CMD_SELECT))
        elog(ERROR, "expected to find SELECT subquery");
    if (list_length(selectquery->rtable) >= 2 &&
        strcmp(rt_fetch(PRS2_OLD_VARNO, selectquery->rtable)->eref->aliasname,
               "old") == 0 &&
        strcmp(rt_fetch(PRS2_NEW_VARNO, selectquery->rtable)->eref->aliasname,
               "new") == 0)
    {
        if (subquery_ptr)
            *subquery_ptr = &(selectrte->subquery);
        return selectquery;
    }
    elog(ERROR, "could not find rule placeholders");
    return NULL;                /* not reached */
}

void IncrementVarSublevelsUp ( Node node,
int  delta_sublevels_up,
int  min_sublevels_up 
)
void IncrementVarSublevelsUp_rtable ( List rtable,
int  delta_sublevels_up,
int  min_sublevels_up 
)
int locate_agg_of_level ( Node node,
int  levelsup 
)

Definition at line 125 of file rewriteManip.c.

References locate_agg_of_level_context::agg_location, locate_agg_of_level_walker(), query_or_expression_tree_walker(), and locate_agg_of_level_context::sublevels_up.

Referenced by check_agg_arguments(), checkTargetlistEntrySQL92(), and parseCheckAggregates().

{
    locate_agg_of_level_context context;

    context.agg_location = -1;  /* in case we find nothing */
    context.sublevels_up = levelsup;

    /*
     * Must be prepared to start with a Query or a bare expression tree; if
     * it's a Query, we don't want to increment sublevels_up.
     */
    (void) query_or_expression_tree_walker(node,
                                           locate_agg_of_level_walker,
                                           (void *) &context,
                                           0);

    return context.agg_location;
}

int locate_windowfunc ( Node node  ) 

Definition at line 220 of file rewriteManip.c.

References locate_windowfunc_walker(), query_or_expression_tree_walker(), and locate_windowfunc_context::win_location.

Referenced by checkTargetlistEntrySQL92(), and transformWindowFuncCall().

{
    locate_windowfunc_context context;

    context.win_location = -1;  /* in case we find nothing */

    /*
     * Must be prepared to start with a Query or a bare expression tree; if
     * it's a Query, we don't want to increment sublevels_up.
     */
    (void) query_or_expression_tree_walker(node,
                                           locate_windowfunc_walker,
                                           (void *) &context,
                                           0);

    return context.win_location;
}

Node* map_variable_attnos ( Node node,
int  target_varno,
int  sublevels_up,
const AttrNumber attno_map,
int  map_length,
bool found_whole_row 
)

Definition at line 1299 of file rewriteManip.c.

References map_variable_attnos_context::attno_map, map_variable_attnos_context::found_whole_row, map_variable_attnos_context::map_length, map_variable_attnos_mutator(), query_or_expression_tree_mutator(), map_variable_attnos_context::sublevels_up, and map_variable_attnos_context::target_varno.

Referenced by generateClonedIndexStmt(), MergeAttributes(), and transformTableLikeClause().

{
    map_variable_attnos_context context;

    context.target_varno = target_varno;
    context.sublevels_up = sublevels_up;
    context.attno_map = attno_map;
    context.map_length = map_length;
    context.found_whole_row = found_whole_row;

    *found_whole_row = false;

    /*
     * Must be prepared to start with a Query or a bare expression tree; if
     * it's a Query, we don't want to increment sublevels_up.
     */
    return query_or_expression_tree_mutator(node,
                                            map_variable_attnos_mutator,
                                            (void *) &context,
                                            0);
}

void OffsetVarNodes ( Node node,
int  offset,
int  sublevels_up 
)

Definition at line 390 of file rewriteManip.c.

References IsA, lfirst, OffsetVarNodes_context::offset, OffsetVarNodes_walker(), query_tree_walker(), Query::resultRelation, Query::rowMarks, RowMarkClause::rti, and OffsetVarNodes_context::sublevels_up.

Referenced by convert_EXISTS_sublink_to_join(), pull_up_simple_subquery(), rewriteRuleAction(), and UpdateRangeTableOfViewParse().

{
    OffsetVarNodes_context context;

    context.offset = offset;
    context.sublevels_up = sublevels_up;

    /*
     * Must be prepared to start with a Query or a bare expression tree; if
     * it's a Query, go straight to query_tree_walker to make sure that
     * sublevels_up doesn't get incremented prematurely.
     */
    if (node && IsA(node, Query))
    {
        Query      *qry = (Query *) node;

        /*
         * If we are starting at a Query, and sublevels_up is zero, then we
         * must also fix rangetable indexes in the Query itself --- namely
         * resultRelation and rowMarks entries.  sublevels_up cannot be zero
         * when recursing into a subquery, so there's no need to have the same
         * logic inside OffsetVarNodes_walker.
         */
        if (sublevels_up == 0)
        {
            ListCell   *l;

            if (qry->resultRelation)
                qry->resultRelation += offset;
            foreach(l, qry->rowMarks)
            {
                RowMarkClause *rc = (RowMarkClause *) lfirst(l);

                rc->rti += offset;
            }
        }
        query_tree_walker(qry, OffsetVarNodes_walker,
                          (void *) &context, 0);
    }
    else
        OffsetVarNodes_walker(node, &context);
}

bool rangeTableEntry_used ( Node node,
int  rt_index,
int  sublevels_up 
)

Definition at line 842 of file rewriteManip.c.

References query_or_expression_tree_walker(), rangeTableEntry_used_walker(), rangeTableEntry_used_context::rt_index, and rangeTableEntry_used_context::sublevels_up.

Referenced by fireRIRrules(), matchLocks(), rewriteRuleAction(), and transformRuleStmt().

{
    rangeTableEntry_used_context context;

    context.rt_index = rt_index;
    context.sublevels_up = sublevels_up;

    /*
     * Must be prepared to start with a Query or a bare expression tree; if
     * it's a Query, we don't want to increment sublevels_up.
     */
    return query_or_expression_tree_walker(node,
                                           rangeTableEntry_used_walker,
                                           (void *) &context,
                                           0);
}

Node* replace_rte_variables ( Node node,
int  target_varno,
int  sublevels_up,
replace_rte_variables_callback  callback,
void *  callback_arg,
bool outer_hasSubLinks 
)

Definition at line 1100 of file rewriteManip.c.

References replace_rte_variables_context::callback, replace_rte_variables_context::callback_arg, elog, ERROR, replace_rte_variables_context::inserted_sublink, IsA, query_or_expression_tree_mutator(), replace_rte_variables_mutator(), replace_rte_variables_context::sublevels_up, and replace_rte_variables_context::target_varno.

Referenced by pullup_replace_vars(), pullup_replace_vars_subquery(), and ReplaceVarsFromTargetList().

{
    Node       *result;
    replace_rte_variables_context context;

    context.callback = callback;
    context.callback_arg = callback_arg;
    context.target_varno = target_varno;
    context.sublevels_up = sublevels_up;

    /*
     * We try to initialize inserted_sublink to true if there is no need to
     * detect new sublinks because the query already has some.
     */
    if (node && IsA(node, Query))
        context.inserted_sublink = ((Query *) node)->hasSubLinks;
    else if (outer_hasSubLinks)
        context.inserted_sublink = *outer_hasSubLinks;
    else
        context.inserted_sublink = false;

    /*
     * Must be prepared to start with a Query or a bare expression tree; if
     * it's a Query, we don't want to increment sublevels_up.
     */
    result = query_or_expression_tree_mutator(node,
                                              replace_rte_variables_mutator,
                                              (void *) &context,
                                              0);

    if (context.inserted_sublink)
    {
        if (result && IsA(result, Query))
            ((Query *) result)->hasSubLinks = true;
        else if (outer_hasSubLinks)
            *outer_hasSubLinks = true;
        else
            elog(ERROR, "replace_rte_variables inserted a SubLink, but has noplace to record it");
    }

    return result;
}

Node* replace_rte_variables_mutator ( Node node,
replace_rte_variables_context context 
)

Definition at line 1147 of file rewriteManip.c.

References replace_rte_variables_context::callback, checkExprHasSubLink(), CurrentOfExpr::cvarno, ereport, errcode(), errmsg(), ERROR, expression_tree_mutator(), Query::hasSubLinks, replace_rte_variables_context::inserted_sublink, IsA, NULL, query_tree_mutator(), replace_rte_variables_mutator(), replace_rte_variables_context::sublevels_up, replace_rte_variables_context::target_varno, Var::varlevelsup, and Var::varno.

Referenced by pullup_replace_vars_callback(), replace_rte_variables(), replace_rte_variables_mutator(), and ReplaceVarsFromTargetList_callback().

{
    if (node == NULL)
        return NULL;
    if (IsA(node, Var))
    {
        Var        *var = (Var *) node;

        if (var->varno == context->target_varno &&
            var->varlevelsup == context->sublevels_up)
        {
            /* Found a matching variable, make the substitution */
            Node       *newnode;

            newnode = (*context->callback) (var, context);
            /* Detect if we are adding a sublink to query */
            if (!context->inserted_sublink)
                context->inserted_sublink = checkExprHasSubLink(newnode);
            return newnode;
        }
        /* otherwise fall through to copy the var normally */
    }
    else if (IsA(node, CurrentOfExpr))
    {
        CurrentOfExpr *cexpr = (CurrentOfExpr *) node;

        if (cexpr->cvarno == context->target_varno &&
            context->sublevels_up == 0)
        {
            /*
             * We get here if a WHERE CURRENT OF expression turns out to apply
             * to a view.  Someday we might be able to translate the
             * expression to apply to an underlying table of the view, but
             * right now it's not implemented.
             */
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                   errmsg("WHERE CURRENT OF on a view is not implemented")));
        }
        /* otherwise fall through to copy the expr normally */
    }
    else if (IsA(node, Query))
    {
        /* Recurse into RTE subquery or not-yet-planned sublink subquery */
        Query      *newnode;
        bool        save_inserted_sublink;

        context->sublevels_up++;
        save_inserted_sublink = context->inserted_sublink;
        context->inserted_sublink = ((Query *) node)->hasSubLinks;
        newnode = query_tree_mutator((Query *) node,
                                     replace_rte_variables_mutator,
                                     (void *) context,
                                     0);
        newnode->hasSubLinks |= context->inserted_sublink;
        context->inserted_sublink = save_inserted_sublink;
        context->sublevels_up--;
        return (Node *) newnode;
    }
    return expression_tree_mutator(node, replace_rte_variables_mutator,
                                   (void *) context);
}

Node* ReplaceVarsFromTargetList ( Node node,
int  target_varno,
int  sublevels_up,
RangeTblEntry target_rte,
List targetlist,
ReplaceVarsNoMatchOption  nomatch_option,
int  nomatch_varno,
bool outer_hasSubLinks 
)