estate->retval = (Datum) 0;
estate->rettupdesc = NULL;
estate->retisnull = true;
+ estate->rettype = InvalidOid;
/*
* Special case path when the RETURN expression is a simple variable
estate->retval = var->value;
estate->retisnull = var->isnull;
estate->rettype = var->datatype->typoid;
+
+ /*
+ * Cope with retistuple case. A PLpgSQL_var could not be
+ * of composite type, so we needn't make any effort to
+ * convert. However, for consistency with the expression
+ * code path, don't throw error if the result is NULL.
+ */
+ if (estate->retistuple && !estate->retisnull)
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("cannot return non-composite value from function returning composite type")));
}
break;
case PLPGSQL_DTYPE_REC:
{
PLpgSQL_rec *rec = (PLpgSQL_rec *) retvar;
+ int32 rettypmod;
if (HeapTupleIsValid(rec->tup))
{
- estate->retval = PointerGetDatum(rec->tup);
- estate->rettupdesc = rec->tupdesc;
- estate->retisnull = false;
+ if (estate->retistuple)
+ {
+ estate->retval = PointerGetDatum(rec->tup);
+ estate->rettupdesc = rec->tupdesc;
+ estate->retisnull = false;
+ }
+ else
+ exec_eval_datum(estate,
+ retvar,
+ &estate->rettype,
+ &rettypmod,
+ &estate->retval,
+ &estate->retisnull);
}
}
break;
case PLPGSQL_DTYPE_ROW:
{
PLpgSQL_row *row = (PLpgSQL_row *) retvar;
+ int32 rettypmod;
- Assert(row->rowtupdesc);
- estate->retval =
- PointerGetDatum(make_tuple_from_row(estate, row,
- row->rowtupdesc));
- if (DatumGetPointer(estate->retval) == NULL) /* should not happen */
- elog(ERROR, "row not compatible with its own tupdesc");
- estate->rettupdesc = row->rowtupdesc;
- estate->retisnull = false;
+ if (estate->retistuple)
+ {
+ HeapTuple tup;
+
+ if (!row->rowtupdesc) /* should not happen */
+ elog(ERROR, "row variable has no tupdesc");
+ tup = make_tuple_from_row(estate, row, row->rowtupdesc);
+ if (tup == NULL) /* should not happen */
+ elog(ERROR, "row not compatible with its own tupdesc");
+ estate->retval = PointerGetDatum(tup);
+ estate->rettupdesc = row->rowtupdesc;
+ estate->retisnull = false;
+ }
+ else
+ exec_eval_datum(estate,
+ retvar,
+ &estate->rettype,
+ &rettypmod,
+ &estate->retval,
+ &estate->retisnull);
}
break;
select compos();
ERROR: cannot return non-composite value from function returning composite type
CONTEXT: PL/pgSQL function compos() line 3 at RETURN
+-- RETURN variable is a different code path ...
+create or replace function compos() returns compostype as $$
+declare x int := 42;
+begin
+ return x;
+end;
+$$ language plpgsql;
+select * from compos();
+ERROR: cannot return non-composite value from function returning composite type
+CONTEXT: PL/pgSQL function compos() line 4 at RETURN
+drop function compos();
+-- test: invalid use of composite variable in scalar-returning function
+create or replace function compos() returns int as $$
+declare
+ v compostype;
+begin
+ v := (1, 'hello');
+ return v;
+end;
+$$ language plpgsql;
+select compos();
+ERROR: invalid input syntax for integer: "(1,hello)"
+CONTEXT: PL/pgSQL function compos() while casting return value to function's return type
+-- test: invalid use of composite expression in scalar-returning function
+create or replace function compos() returns int as $$
+begin
+ return (1, 'hello')::compostype;
+end;
+$$ language plpgsql;
+select compos();
+ERROR: invalid input syntax for integer: "(1,hello)"
+CONTEXT: PL/pgSQL function compos() while casting return value to function's return type
drop function compos();
drop type compostype;
--