From a096c46ec5e9d8cacc114a179ef4724d5b182d41 Mon Sep 17 00:00:00 2001 From: "tender.wang" Date: Sun, 7 Apr 2024 17:09:54 +0800 Subject: [PATCH] Fix RowExpr with constant value as COALESCE argument. Since d575347, Const expr returning RECORDOID, the result of type will be TYPEFUNC_COMPOSITE not TYPEFUNC_RECORD. It will trigger Assert failed in this test case. Before d575347, the test case will report error in tupledesc_match(), I move that ereport code the place before calling expandTupleDesc(). --- src/backend/parser/parse_relation.c | 10 ++++++++++ src/test/regress/expected/rowtypes.out | 4 ++++ src/test/regress/sql/rowtypes.sql | 2 ++ 3 files changed, 16 insertions(+) diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c index 7ca793a369..e2ffbf55f5 100644 --- a/src/backend/parser/parse_relation.c +++ b/src/backend/parser/parse_relation.c @@ -2749,6 +2749,16 @@ expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, { /* Composite data type, e.g. a table's row type */ Assert(tupdesc); + + if (tupdesc->natts < rtfunc->funccolcount) + ereport(ERROR, + (errcode(ERRCODE_DATATYPE_MISMATCH), + errmsg("function return row and query-specified return row do not match"), + errdetail_plural("Returned row contains %d attribute, but query expects %d.", + "Returned row contains %d attributes, but query expects %d.", + tupdesc->natts, + tupdesc->natts, rtfunc->funccolcount))); + expandTupleDesc(tupdesc, rte->eref, rtfunc->funccolcount, atts_done, rtindex, sublevels_up, location, diff --git a/src/test/regress/expected/rowtypes.out b/src/test/regress/expected/rowtypes.out index b400b58f76..8bd5806535 100644 --- a/src/test/regress/expected/rowtypes.out +++ b/src/test/regress/expected/rowtypes.out @@ -100,6 +100,10 @@ SELECT * FROM pg_input_error_info('(1,1e400)', 'complex'); "1e400" is out of range for type double precision | | | 22003 (1 row) +SELECT * FROM generate_series(1, 1), + COALESCE(row(1)) AS (a int, b int); -- bad +ERROR: function return row and query-specified return row do not match +DETAIL: Returned row contains 1 attribute, but query expects 2. create temp table quadtable(f1 int, q quad); insert into quadtable values (1, ((3.3,4.4),(5.5,6.6))); insert into quadtable values (2, ((null,4.4),(5.5,6.6))); diff --git a/src/test/regress/sql/rowtypes.sql b/src/test/regress/sql/rowtypes.sql index fd47dc9e0f..94baae2b6b 100644 --- a/src/test/regress/sql/rowtypes.sql +++ b/src/test/regress/sql/rowtypes.sql @@ -37,6 +37,8 @@ SELECT pg_input_is_valid('(1,2', 'complex'); SELECT pg_input_is_valid('(1,zed)', 'complex'); SELECT * FROM pg_input_error_info('(1,zed)', 'complex'); SELECT * FROM pg_input_error_info('(1,1e400)', 'complex'); +SELECT * FROM generate_series(1, 1), + COALESCE(row(1)) AS (a int, b int); -- bad create temp table quadtable(f1 int, q quad); -- 2.25.1