/* * create_tl_element-- * 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'. * * RETURNS: newly created tlist-entry * CREATES: new targetlist entry (always). */ TargetEntry* create_tl_element(Var *var, int resdomno) { TargetEntry *tlelement= makeNode(TargetEntry); tlelement->resdom = makeResdom(resdomno, var->vartype, get_typlen(var->vartype), NULL, (Index)0, (Oid)0, 0); tlelement->expr = (Node*)var; return(tlelement); }
/* * Generate targetlist for a set-operation plan node * * colTypes: column datatypes for non-junk columns * flag: -1 if no flag column needed, 0 or 1 to create a const flag column * hack_constants: true to copy up constants (see comments in code) * input_tlist: targetlist of this node's input node * refnames_tlist: targetlist to take column names from */ static List * generate_setop_tlist(List *colTypes, int flag, bool hack_constants, List *input_tlist, List *refnames_tlist) { List *tlist = NIL; int resno = 1; List *i; Resdom *resdom; Node *expr; foreach(i, colTypes) { Oid colType = lfirsto(i); TargetEntry *inputtle = (TargetEntry *) lfirst(input_tlist); TargetEntry *reftle = (TargetEntry *) lfirst(refnames_tlist); int32 colTypmod; Assert(inputtle->resdom->resno == resno); Assert(reftle->resdom->resno == resno); Assert(!inputtle->resdom->resjunk); Assert(!reftle->resdom->resjunk); /* * Generate columns referencing input columns and having * appropriate data types and column names. Insert datatype * coercions where necessary. * * HACK: constants in the input's targetlist are copied up as-is * rather than being referenced as subquery outputs. This is * mainly to ensure that when we try to coerce them to the output * column's datatype, the right things happen for UNKNOWN * constants. But do this only at the first level of * subquery-scan plans; we don't want phony constants appearing in * the output tlists of upper-level nodes! */ if (hack_constants && inputtle->expr && IsA(inputtle->expr, Const)) expr = (Node *) inputtle->expr; else expr = (Node *) makeVar(0, inputtle->resdom->resno, inputtle->resdom->restype, inputtle->resdom->restypmod, 0); if (inputtle->resdom->restype == colType) { /* no coercion needed, and believe the input typmod */ colTypmod = inputtle->resdom->restypmod; } else { expr = coerce_to_common_type(NULL, /* no UNKNOWNs here */ expr, colType, "UNION/INTERSECT/EXCEPT"); colTypmod = -1; } resdom = makeResdom((AttrNumber) resno++, colType, colTypmod, pstrdup(reftle->resdom->resname), false); tlist = lappend(tlist, makeTargetEntry(resdom, (Expr *) expr)); input_tlist = lnext(input_tlist); refnames_tlist = lnext(refnames_tlist); }