1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* tlist.c--
|
1997-09-07 07:04:48 +02:00
|
|
|
* Target list manipulation routines
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1997-09-08 23:56:23 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/tlist.c,v 1.7 1997/09/08 21:45:55 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "nodes/relation.h"
|
|
|
|
#include "nodes/primnodes.h"
|
|
|
|
#include "nodes/pg_list.h"
|
|
|
|
#include "nodes/nodeFuncs.h"
|
|
|
|
#include "utils/elog.h"
|
|
|
|
#include "utils/lsyscache.h"
|
|
|
|
|
|
|
|
#include "optimizer/internal.h"
|
|
|
|
#include "optimizer/var.h"
|
|
|
|
#include "optimizer/tlist.h"
|
|
|
|
#include "optimizer/clauses.h"
|
|
|
|
|
|
|
|
#include "nodes/makefuncs.h"
|
|
|
|
#include "parser/catalog_utils.h"
|
|
|
|
|
1997-09-08 23:56:23 +02:00
|
|
|
static Node *flatten_tlistentry(Node *tlistentry, List *flat_tlist);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/*****************************************************************************
|
1997-09-07 07:04:48 +02:00
|
|
|
* ---------- RELATION node target list routines ----------
|
1996-07-09 08:22:35 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1996-07-09 08:22:35 +02:00
|
|
|
* tlistentry-member--
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* RETURNS: the leftmost member of sequence "targetlist" that satisfies
|
1997-09-07 07:04:48 +02:00
|
|
|
* the predicate "var_equal"
|
1996-07-09 08:22:35 +02:00
|
|
|
* MODIFIES: nothing
|
|
|
|
* REQUIRES: test = function which can operate on a lispval union
|
1997-09-07 07:04:48 +02:00
|
|
|
* var = valid var-node
|
|
|
|
* targetlist = valid sequence
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
TargetEntry *
|
1997-09-08 23:56:23 +02:00
|
|
|
tlistentry_member(Var *var, List *targetlist)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (var)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *temp = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(temp, targetlist)
|
|
|
|
{
|
|
|
|
if (var_equal(var,
|
|
|
|
get_expr(lfirst(temp))))
|
|
|
|
return ((TargetEntry *) lfirst(temp));
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
return (NULL);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1996-07-09 08:22:35 +02:00
|
|
|
* matching_tlvar--
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* RETURNS: var node in a target list which is var_equal to 'var',
|
1997-09-07 07:04:48 +02:00
|
|
|
* if one exists.
|
1996-07-09 08:22:35 +02:00
|
|
|
* REQUIRES: "test" operates on lispval unions,
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
Expr *
|
1997-09-08 23:56:23 +02:00
|
|
|
matching_tlvar(Var *var, List *targetlist)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
TargetEntry *tlentry;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
tlentry = tlistentry_member(var, targetlist);
|
|
|
|
if (tlentry)
|
|
|
|
return ((Expr *) get_expr(tlentry));
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return ((Expr *) NULL);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1996-07-09 08:22:35 +02:00
|
|
|
* add_tl_element--
|
1997-09-07 07:04:48 +02:00
|
|
|
* Creates a targetlist entry corresponding to the supplied var node
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* 'var' and adds the new targetlist entry to the targetlist field of
|
1997-09-07 07:04:48 +02:00
|
|
|
* 'rel'
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* RETURNS: nothing
|
|
|
|
* MODIFIES: vartype and varid fields of leftmost varnode that matches
|
1997-09-07 07:04:48 +02:00
|
|
|
* argument "var" (sometimes).
|
1996-07-09 08:22:35 +02:00
|
|
|
* CREATES: new var-node iff no matching var-node exists in targetlist
|
|
|
|
*/
|
|
|
|
void
|
1997-09-08 23:56:23 +02:00
|
|
|
add_tl_element(Rel *rel, Var *var)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Expr *oldvar = (Expr *) NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
oldvar = matching_tlvar(var, rel->targetlist);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If 'var' is not already in 'rel's target list, add a new node.
|
|
|
|
*/
|
|
|
|
if (oldvar == NULL)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *tlist = rel->targetlist;
|
|
|
|
Var *newvar = makeVar(var->varno,
|
|
|
|
var->varattno,
|
|
|
|
var->vartype,
|
|
|
|
var->varno,
|
|
|
|
var->varoattno);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
rel->targetlist =
|
|
|
|
lappend(tlist,
|
|
|
|
create_tl_element(newvar,
|
|
|
|
length(tlist) + 1));
|
|
|
|
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1996-07-09 08:22:35 +02:00
|
|
|
* create_tl_element--
|
1997-09-07 07:04:48 +02:00
|
|
|
* Creates a target list entry node and its associated (resdom var) pair
|
|
|
|
* with its resdom number equal to 'resdomno' and the joinlist field set
|
|
|
|
* to 'joinlist'.
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* RETURNS: newly created tlist-entry
|
|
|
|
* CREATES: new targetlist entry (always).
|
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
TargetEntry *
|
1997-09-08 23:56:23 +02:00
|
|
|
create_tl_element(Var *var, int resdomno)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
TargetEntry *tlelement = makeNode(TargetEntry);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
tlelement->resdom =
|
|
|
|
makeResdom(resdomno,
|
|
|
|
var->vartype,
|
|
|
|
get_typlen(var->vartype),
|
|
|
|
NULL,
|
|
|
|
(Index) 0,
|
|
|
|
(Oid) 0,
|
|
|
|
0);
|
|
|
|
tlelement->expr = (Node *) var;
|
|
|
|
|
|
|
|
return (tlelement);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1996-07-09 08:22:35 +02:00
|
|
|
* get-actual-tlist--
|
1997-09-07 07:04:48 +02:00
|
|
|
* Returns the targetlist elements from a relation tlist.
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
List *
|
1997-09-08 23:56:23 +02:00
|
|
|
get_actual_tlist(List *tlist)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* this function is not making sense. - ay 10/94
|
|
|
|
*/
|
|
|
|
#if 0
|
1997-09-08 04:41:22 +02:00
|
|
|
List *element = NIL;
|
|
|
|
List *result = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (tlist == NULL)
|
|
|
|
{
|
|
|
|
elog(DEBUG, "calling get_actual_tlist with empty tlist");
|
|
|
|
return (NIL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX - it is unclear to me what exactly get_entry should be doing,
|
|
|
|
* as it is unclear to me the exact relationship between "TL" "TLE"
|
|
|
|
* and joinlists
|
|
|
|
*/
|
|
|
|
|
|
|
|
foreach(element, tlist)
|
|
|
|
result = lappend(result, lfirst((List *) lfirst(element)));
|
|
|
|
|
|
|
|
return (result);
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
return tlist;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
1997-09-07 07:04:48 +02:00
|
|
|
* ---------- GENERAL target list routines ----------
|
1996-07-09 08:22:35 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1996-07-09 08:22:35 +02:00
|
|
|
* tlist-member--
|
1997-09-07 07:04:48 +02:00
|
|
|
* Determines whether a var node is already contained within a
|
|
|
|
* target list.
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'var' is the var node
|
|
|
|
* 'tlist' is the target list
|
|
|
|
* 'dots' is t if we must match dotfields to determine uniqueness
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns the resdom entry of the matching var node.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
Resdom *
|
1997-09-08 23:56:23 +02:00
|
|
|
tlist_member(Var *var, List *tlist)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *i = NIL;
|
|
|
|
TargetEntry *temp_tle = (TargetEntry *) NULL;
|
|
|
|
TargetEntry *tl_elt = (TargetEntry *) NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (var)
|
|
|
|
{
|
|
|
|
foreach(i, tlist)
|
|
|
|
{
|
|
|
|
temp_tle = (TargetEntry *) lfirst(i);
|
|
|
|
if (var_equal(var, get_expr(temp_tle)))
|
|
|
|
{
|
|
|
|
tl_elt = temp_tle;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tl_elt != NULL)
|
|
|
|
return (tl_elt->resdom);
|
|
|
|
else
|
|
|
|
return ((Resdom *) NULL);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
return ((Resdom *) NULL);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Routine to get the resdom out of a targetlist.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
Resdom *
|
1997-09-08 23:56:23 +02:00
|
|
|
tlist_resdom(List *tlist, Resdom *resnode)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Resdom *resdom = (Resdom *) NULL;
|
|
|
|
List *i = NIL;
|
|
|
|
TargetEntry *temp_tle = (TargetEntry *) NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(i, tlist)
|
|
|
|
{
|
|
|
|
temp_tle = (TargetEntry *) lfirst(i);
|
|
|
|
resdom = temp_tle->resdom;
|
|
|
|
/* Since resnos are supposed to be unique */
|
|
|
|
if (resnode->resno == resdom->resno)
|
|
|
|
return (resdom);
|
|
|
|
}
|
|
|
|
return ((Resdom *) NULL);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1996-07-09 08:22:35 +02:00
|
|
|
* match_varid--
|
1997-09-07 07:04:48 +02:00
|
|
|
* Searches a target list for an entry with some desired varid.
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'varid' is the desired id
|
|
|
|
* 'tlist' is the target list that is searched
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns the target list entry (resdom var) of the matching var.
|
|
|
|
*
|
|
|
|
* Now checks to make sure array references (in addition to range
|
|
|
|
* table indices) are identical - retrieve (a.b[1],a.b[2]) should
|
|
|
|
* not be turned into retrieve (a.b[1],a.b[1]).
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* [what used to be varid is now broken up into two fields varnoold and
|
1997-09-07 07:04:48 +02:00
|
|
|
* varoattno. Also, nested attnos are long gone. - ay 2/95]
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
TargetEntry *
|
1997-09-08 23:56:23 +02:00
|
|
|
match_varid(Var *test_var, List *tlist)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *tl;
|
|
|
|
Oid type_var;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
type_var = (Oid) test_var->vartype;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
foreach(tl, tlist)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
TargetEntry *entry;
|
|
|
|
Var *tlvar;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
entry = lfirst(tl);
|
|
|
|
tlvar = get_expr(entry);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!IsA(tlvar, Var))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we test the original varno (instead of varno which might be
|
|
|
|
* changed to INNER/OUTER.
|
|
|
|
*/
|
|
|
|
if (tlvar->varnoold == test_var->varnoold &&
|
|
|
|
tlvar->varoattno == test_var->varoattno)
|
|
|
|
{
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (tlvar->vartype == type_var)
|
|
|
|
return (entry);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return (NULL);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1996-07-09 08:22:35 +02:00
|
|
|
* new-unsorted-tlist--
|
1997-09-07 07:04:48 +02:00
|
|
|
* Creates a copy of a target list by creating new resdom nodes
|
|
|
|
* without sort information.
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'targetlist' is the target list to be copied.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns the resulting target list.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
List *
|
1997-09-08 23:56:23 +02:00
|
|
|
new_unsorted_tlist(List *targetlist)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *new_targetlist = (List *) copyObject((Node *) targetlist);
|
|
|
|
List *x = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(x, new_targetlist)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
TargetEntry *tle = (TargetEntry *) lfirst(x);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
tle->resdom->reskey = 0;
|
|
|
|
tle->resdom->reskeyop = (Oid) 0;
|
|
|
|
}
|
|
|
|
return (new_targetlist);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1996-07-09 08:22:35 +02:00
|
|
|
* copy-vars--
|
1997-09-07 07:04:48 +02:00
|
|
|
* Replaces the var nodes in the first target list with those from
|
|
|
|
* the second target list. The two target lists are assumed to be
|
|
|
|
* identical except their actual resdoms and vars are different.
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'target' is the target list to be replaced
|
|
|
|
* 'source' is the target list to be copied
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns a new target list.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
List *
|
1997-09-08 23:56:23 +02:00
|
|
|
copy_vars(List *target, List *source)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *result = NIL;
|
|
|
|
List *src = NIL;
|
|
|
|
List *dest = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
for (src = source, dest = target; src != NIL &&
|
|
|
|
dest != NIL; src = lnext(src), dest = lnext(dest))
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
TargetEntry *temp = MakeTLE(((TargetEntry *) lfirst(dest))->resdom,
|
|
|
|
(Node *) get_expr(lfirst(src)));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
result = lappend(result, temp);
|
|
|
|
}
|
|
|
|
return (result);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1996-07-09 08:22:35 +02:00
|
|
|
* flatten-tlist--
|
1997-09-07 07:04:48 +02:00
|
|
|
* Create a target list that only contains unique variables.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* 'tlist' is the current target list
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns the "flattened" new target list.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
List *
|
1997-09-08 23:56:23 +02:00
|
|
|
flatten_tlist(List *tlist)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int last_resdomno = 1;
|
|
|
|
List *new_tlist = NIL;
|
|
|
|
List *tlist_vars = NIL;
|
|
|
|
List *temp;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(temp, tlist)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
TargetEntry *temp_entry = NULL;
|
|
|
|
List *vars;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
temp_entry = lfirst(temp);
|
|
|
|
vars = pull_var_clause((Node *) get_expr(temp_entry));
|
|
|
|
if (vars != NULL)
|
|
|
|
{
|
|
|
|
tlist_vars = nconc(tlist_vars, vars);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(temp, tlist_vars)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Var *var = lfirst(temp);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (!(tlist_member(var, new_tlist)))
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Resdom *r;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
r = makeResdom(last_resdomno,
|
|
|
|
var->vartype,
|
|
|
|
get_typlen(var->vartype),
|
|
|
|
NULL,
|
|
|
|
(Index) 0,
|
|
|
|
(Oid) 0,
|
|
|
|
0);
|
|
|
|
last_resdomno++;
|
|
|
|
new_tlist = lappend(new_tlist, MakeTLE(r, (Node *) var));
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return new_tlist;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1996-07-09 08:22:35 +02:00
|
|
|
* flatten-tlist-vars--
|
1997-09-07 07:04:48 +02:00
|
|
|
* Redoes the target list of a query with no nested attributes by
|
|
|
|
* replacing vars within computational expressions with vars from
|
|
|
|
* the 'flattened' target list of the query.
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'full-tlist' is the actual target list
|
|
|
|
* 'flat-tlist' is the flattened (var-only) target list
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns the modified actual target list.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
List *
|
1997-09-08 23:56:23 +02:00
|
|
|
flatten_tlist_vars(List *full_tlist, List *flat_tlist)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *x = NIL;
|
|
|
|
List *result = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(x, full_tlist)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
TargetEntry *tle = lfirst(x);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
result =
|
|
|
|
lappend(result,
|
|
|
|
MakeTLE(tle->resdom,
|
|
|
|
flatten_tlistentry((Node *) get_expr(tle),
|
|
|
|
flat_tlist)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (result);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
1996-07-09 08:22:35 +02:00
|
|
|
* flatten-tlistentry--
|
1997-09-07 07:04:48 +02:00
|
|
|
* Replaces vars within a target list entry with vars from a flattened
|
|
|
|
* target list.
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* 'tlistentry' is the target list entry to be modified
|
|
|
|
* 'flat-tlist' is the flattened target list
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Returns the (modified) target_list entry from the target list.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static Node *
|
1997-09-08 23:56:23 +02:00
|
|
|
flatten_tlistentry(Node *tlistentry, List *flat_tlist)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (tlistentry == NULL)
|
|
|
|
{
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (IsA(tlistentry, Var))
|
|
|
|
{
|
|
|
|
|
|
|
|
return
|
|
|
|
((Node *) get_expr(match_varid((Var *) tlistentry,
|
|
|
|
flat_tlist)));
|
|
|
|
}
|
|
|
|
else if (IsA(tlistentry, Iter))
|
|
|
|
{
|
|
|
|
|
|
|
|
((Iter *) tlistentry)->iterexpr =
|
|
|
|
flatten_tlistentry((Node *) ((Iter *) tlistentry)->iterexpr,
|
|
|
|
flat_tlist);
|
|
|
|
return tlistentry;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (single_node(tlistentry))
|
|
|
|
{
|
|
|
|
|
|
|
|
return tlistentry;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (is_funcclause(tlistentry))
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Expr *expr = (Expr *) tlistentry;
|
|
|
|
List *temp_result = NIL;
|
|
|
|
List *elt = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(elt, expr->args)
|
|
|
|
temp_result = lappend(temp_result,
|
|
|
|
flatten_tlistentry(lfirst(elt), flat_tlist));
|
|
|
|
|
|
|
|
return
|
|
|
|
((Node *) make_funcclause((Func *) expr->oper, temp_result));
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (IsA(tlistentry, Aggreg))
|
|
|
|
{
|
|
|
|
|
|
|
|
return tlistentry;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (IsA(tlistentry, ArrayRef))
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
ArrayRef *aref = (ArrayRef *) tlistentry;
|
|
|
|
List *temp = NIL;
|
|
|
|
List *elt = NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(elt, aref->refupperindexpr)
|
|
|
|
temp = lappend(temp, flatten_tlistentry(lfirst(elt), flat_tlist));
|
|
|
|
aref->refupperindexpr = temp;
|
|
|
|
|
|
|
|
temp = NIL;
|
|
|
|
foreach(elt, aref->reflowerindexpr)
|
|
|
|
temp = lappend(temp, flatten_tlistentry(lfirst(elt), flat_tlist));
|
|
|
|
aref->reflowerindexpr = temp;
|
|
|
|
|
|
|
|
aref->refexpr =
|
|
|
|
flatten_tlistentry(aref->refexpr, flat_tlist);
|
|
|
|
|
|
|
|
aref->refassgnexpr =
|
|
|
|
flatten_tlistentry(aref->refassgnexpr, flat_tlist);
|
|
|
|
|
|
|
|
return tlistentry;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Expr *expr = (Expr *) tlistentry;
|
|
|
|
Var *left =
|
1997-09-07 07:04:48 +02:00
|
|
|
(Var *) flatten_tlistentry((Node *) get_leftop(expr),
|
|
|
|
flat_tlist);
|
1997-09-08 04:41:22 +02:00
|
|
|
Var *right =
|
1997-09-07 07:04:48 +02:00
|
|
|
(Var *) flatten_tlistentry((Node *) get_rightop(expr),
|
|
|
|
flat_tlist);
|
|
|
|
|
|
|
|
return ((Node *)
|
|
|
|
make_opclause((Oper *) expr->oper, left, right));
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
TargetEntry *
|
1997-09-08 23:56:23 +02:00
|
|
|
MakeTLE(Resdom *resdom, Node *expr)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
TargetEntry *rt = makeNode(TargetEntry);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
rt->resdom = resdom;
|
|
|
|
rt->expr = expr;
|
|
|
|
return rt;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
Var *
|
1997-09-08 23:56:23 +02:00
|
|
|
get_expr(TargetEntry *tle)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
Assert(tle != NULL);
|
|
|
|
Assert(tle->expr != NULL);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return ((Var *) tle->expr);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AddGroupAttrToTlist -
|
1997-09-07 07:04:48 +02:00
|
|
|
* append the group attribute to the target list if it's not already
|
|
|
|
* in there.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-08-20 16:54:35 +02:00
|
|
|
#ifdef NOT_USED
|
1996-07-09 08:22:35 +02:00
|
|
|
void
|
1997-09-08 23:56:23 +02:00
|
|
|
AddGroupAttrToTlist(List *tlist, List *grpCl)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *gl;
|
|
|
|
int last_resdomno = length(tlist) + 1;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
foreach(gl, grpCl)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
GroupClause *gc = (GroupClause *) lfirst(gl);
|
|
|
|
Var *var = gc->grpAttr;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (!(tlist_member(var, tlist)))
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Resdom *r;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
r = makeResdom(last_resdomno,
|
|
|
|
var->vartype,
|
|
|
|
get_typlen(var->vartype),
|
|
|
|
NULL,
|
|
|
|
(Index) 0,
|
|
|
|
(Oid) 0,
|
|
|
|
0);
|
|
|
|
last_resdomno++;
|
|
|
|
tlist = lappend(tlist, MakeTLE(r, (Node *) var));
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-08-19 23:40:56 +02:00
|
|
|
#endif
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/* was ExecTargetListLength() in execQual.c,
|
1996-07-09 08:22:35 +02:00
|
|
|
moved here to reduce dependencies on the executor module */
|
|
|
|
int
|
1997-09-08 23:56:23 +02:00
|
|
|
exec_tlist_length(List *targetlist)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int len;
|
|
|
|
List *tl;
|
|
|
|
TargetEntry *curTle;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
len = 0;
|
|
|
|
foreach(tl, targetlist)
|
|
|
|
{
|
|
|
|
curTle = lfirst(tl);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (curTle->resdom != NULL)
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|