static TupleDesc ExecTypeFromTLInternal(List *targetList, bool hasoid, bool skipjunk) { TupleDesc typeInfo; ListCell *l; int len; int cur_resno = 1; if (skipjunk) len = ExecCleanTargetListLength(targetList); else len = ExecTargetListLength(targetList); typeInfo = CreateTemplateTupleDesc(len, hasoid); foreach(l, targetList) { TargetEntry *tle = lfirst(l); if (skipjunk && tle->resjunk) continue; TupleDescInitEntry(typeInfo, cur_resno++, tle->resname, exprType((Node *) tle->expr), exprTypmod((Node *) tle->expr), 0); }
/*--------------------------------------------------------------------- * DefineVirtualRelation * * Create the "view" relation. `DefineRelation' does all the work, * we just provide the correct arguments ... at least when we're * creating a view. If we're updating an existing view, we have to * work harder. *--------------------------------------------------------------------- */ static ObjectAddress DefineVirtualRelation(RangeVar *relation, List *tlist, bool replace, List *options) { Oid viewOid; LOCKMODE lockmode; CreateStmt *createStmt = makeNode(CreateStmt); List *attrList; ListCell *t; /* * create a list of ColumnDef nodes based on the names and types of the * (non-junk) targetlist items from the view's SELECT list. */ attrList = NIL; foreach(t, tlist) { TargetEntry *tle = lfirst(t); if (!tle->resjunk) { ColumnDef *def = makeNode(ColumnDef); def->colname = pstrdup(tle->resname); def->typeName = makeTypeNameFromOid(exprType((Node *) tle->expr), exprTypmod((Node *) tle->expr)); def->inhcount = 0; def->is_local = true; def->is_not_null = false; def->is_from_type = false; def->storage = 0; def->raw_default = NULL; def->cooked_default = NULL; def->collClause = NULL; def->collOid = exprCollation((Node *) tle->expr); def->location = -1; /* * It's possible that the column is of a collatable type but the * collation could not be resolved, so double-check. */ if (type_is_collatable(exprType((Node *) tle->expr))) { if (!OidIsValid(def->collOid)) ereport(ERROR, (errcode(ERRCODE_INDETERMINATE_COLLATION), errmsg("could not determine which collation to use for view column \"%s\"", def->colname), errhint("Use the COLLATE clause to set the collation explicitly."))); } else Assert(!OidIsValid(def->collOid)); def->constraints = NIL; attrList = lappend(attrList, def); } }
/* * makeVarFromTargetEntry - * convenience function to create a same-level Var node from a * TargetEntry */ Var * makeVarFromTargetEntry(Index varno, TargetEntry *tle) { return makeVar(varno, tle->resno, exprType((Node *) tle->expr), exprTypmod((Node *) tle->expr), exprCollation((Node *) tle->expr), 0); }
/* * Get the actual type OID of a specific function argument (counting from 0), * but working from the calling expression tree instead of FmgrInfo * * Returns InvalidOid if information is not available */ static int32 get_call_expr_argtypmod(Node *expr, int argnum) { List *args; int32 argtypmod; if (expr == NULL) return -1; if (IsA(expr, FuncExpr)) args = ((FuncExpr *) expr)->args; else if (IsA(expr, OpExpr)) args = ((OpExpr *) expr)->args; else if (IsA(expr, DistinctExpr)) args = ((DistinctExpr *) expr)->args; else if (IsA(expr, ScalarArrayOpExpr)) // See below // args = ((ScalarArrayOpExpr *) expr)->args; elog(ERROR, "castnig a variant as part of an ANY/ALL [array] expression is not supported" ); else if (IsA(expr, ArrayCoerceExpr)) // See below // args = list_make1(((ArrayCoerceExpr *) expr)->arg); elog(ERROR, "castnig a variant as part of an array cast is not supported" ); else if (IsA(expr, NullIfExpr)) args = ((NullIfExpr *) expr)->args; else if (IsA(expr, WindowFunc)) args = ((WindowFunc *) expr)->args; else return -1; if (argnum < 0 || argnum >= list_length(args)) return -1; argtypmod = exprTypmod((Node *) list_nth(args, argnum)); /* * get_call_expr_argtyp has these hacks in place. I don't know if they're * needed for typmods or not. For now, punt. * * special hack for ScalarArrayOpExpr and ArrayCoerceExpr: what the * underlying function will actually get passed is the element type of the * array. */ if (IsA(expr, ScalarArrayOpExpr) && argnum == 1) // argtypmod = get_base_element_type(argtypmod); elog(ERROR, "castnig a variant as part of an ANY/ALL [array] expression is not supported" ); else if (IsA(expr, ArrayCoerceExpr) && argnum == 0) // argtypmod = get_base_element_type(argtypmod); elog(ERROR, "castnig a variant as part of an array cast is not supported" ); return argtypmod; }
/* * create_ctas_nodata * * Create CTAS or materialized view when WITH NO DATA is used, starting from * the targetlist of the SELECT or view definition. */ static ObjectAddress create_ctas_nodata(List *tlist, IntoClause *into) { List *attrList; ListCell *t, *lc; /* * Build list of ColumnDefs from non-junk elements of the tlist. If a * column name list was specified in CREATE TABLE AS, override the column * names in the query. (Too few column names are OK, too many are not.) */ attrList = NIL; lc = list_head(into->colNames); foreach(t, tlist) { TargetEntry *tle = (TargetEntry *) lfirst(t); if (!tle->resjunk) { ColumnDef *col; char *colname; if (lc) { colname = strVal(lfirst(lc)); lc = lnext(lc); } else colname = tle->resname; col = makeColumnDef(colname, exprType((Node *) tle->expr), exprTypmod((Node *) tle->expr), exprCollation((Node *) tle->expr)); /* * It's possible that the column is of a collatable type but the * collation could not be resolved, so double-check. (We must * check this here because DefineRelation would adopt the type's * default collation rather than complaining.) */ if (!OidIsValid(col->collOid) && type_is_collatable(col->typeName->typeOid)) ereport(ERROR, (errcode(ERRCODE_INDETERMINATE_COLLATION), errmsg("no collation was derived for column \"%s\" with collatable type %s", col->colname, format_type_be(col->typeName->typeOid)), errhint("Use the COLLATE clause to set the collation explicitly."))); attrList = lappend(attrList, col); } }
/*--------------------------------------------------------------------- * DefineVirtualRelation * * Create a view relation and use the rules system to store the query * for the view. *--------------------------------------------------------------------- */ static ObjectAddress DefineVirtualRelation(RangeVar *relation, List *tlist, bool replace, List *options, Query *viewParse) { Oid viewOid; LOCKMODE lockmode; CreateStmt *createStmt = makeNode(CreateStmt); List *attrList; ListCell *t; /* * create a list of ColumnDef nodes based on the names and types of the * (non-junk) targetlist items from the view's SELECT list. */ attrList = NIL; foreach(t, tlist) { TargetEntry *tle = (TargetEntry *) lfirst(t); if (!tle->resjunk) { ColumnDef *def = makeColumnDef(tle->resname, exprType((Node *) tle->expr), exprTypmod((Node *) tle->expr), exprCollation((Node *) tle->expr)); /* * It's possible that the column is of a collatable type but the * collation could not be resolved, so double-check. */ if (type_is_collatable(exprType((Node *) tle->expr))) { if (!OidIsValid(def->collOid)) ereport(ERROR, (errcode(ERRCODE_INDETERMINATE_COLLATION), errmsg("could not determine which collation to use for view column \"%s\"", def->colname), errhint("Use the COLLATE clause to set the collation explicitly."))); } else Assert(!OidIsValid(def->collOid)); attrList = lappend(attrList, def); } }
/* * 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 * varno: varno to use in generated Vars * 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, Index varno, bool hack_constants, List *input_tlist, List *refnames_tlist) { List *tlist = NIL; int resno = 1; ListCell *i, *j, *k; TargetEntry *tle; Node *expr; j = list_head(input_tlist); k = list_head(refnames_tlist); foreach(i, colTypes) { Oid colType = lfirst_oid(i); TargetEntry *inputtle = (TargetEntry *) lfirst(j); TargetEntry *reftle = (TargetEntry *) lfirst(k); Assert(inputtle->resno == resno); Assert(reftle->resno == resno); Assert(!inputtle->resjunk); Assert(!reftle->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(varno, inputtle->resno, exprType((Node *) inputtle->expr), exprTypmod((Node *) inputtle->expr), 0); if (exprType(expr) != colType) { expr = coerce_to_common_type(NULL, /* no UNKNOWNs here */ expr, colType, "UNION/INTERSECT/EXCEPT"); } tle = makeTargetEntry((Expr *) expr, (AttrNumber) resno++, pstrdup(reftle->resname), false); tlist = lappend(tlist, tle); j = lnext(j); k = lnext(k); }