1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-09 06:21:09 +03:00

Add 'missing_ok' argument to build_attrmap_by_name

When it's given as true, return a 0 in the position of the missing
column rather than raising an error.

This is currently unused, but it allows us to reimplement column
permission checking in a subsequent commit.  It seems worth breaking
into a separate commit because it affects unrelated code.

Author: Amit Langote <amitlangote09@gmail.com>
Discussion: https://postgr.es/m/CA+HiwqFfiai=qBxPDTjaio_ZcaqUKh+FC=prESrB8ogZgFNNNQ@mail.gmail.com
This commit is contained in:
Alvaro Herrera
2022-11-29 09:39:36 +01:00
parent 00ae5d6f58
commit ad86d159b6
10 changed files with 55 additions and 28 deletions

View File

@@ -169,10 +169,15 @@ build_attrmap_by_position(TupleDesc indesc,
* and output columns by name. (Dropped columns are ignored in both input and
* output.) This is normally a subroutine for convert_tuples_by_name in
* tupconvert.c, but can be used standalone.
*
* If 'missing_ok' is true, a column from 'outdesc' not being present in
* 'indesc' is not flagged as an error; AttrMap.attnums[] entry for such an
* outdesc column will be 0 in that case.
*/
AttrMap *
build_attrmap_by_name(TupleDesc indesc,
TupleDesc outdesc)
TupleDesc outdesc,
bool missing_ok)
{
AttrMap *attrMap;
int outnatts;
@@ -235,7 +240,7 @@ build_attrmap_by_name(TupleDesc indesc,
break;
}
}
if (attrMap->attnums[i] == 0)
if (attrMap->attnums[i] == 0 && !missing_ok)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("could not convert row type"),
@@ -257,12 +262,13 @@ build_attrmap_by_name(TupleDesc indesc,
*/
AttrMap *
build_attrmap_by_name_if_req(TupleDesc indesc,
TupleDesc outdesc)
TupleDesc outdesc,
bool missing_ok)
{
AttrMap *attrMap;
/* Verify compatibility and prepare attribute-number map */
attrMap = build_attrmap_by_name(indesc, outdesc);
attrMap = build_attrmap_by_name(indesc, outdesc, missing_ok);
/* Check if the map has a one-to-one match */
if (check_attrmap_match(indesc, outdesc, attrMap))

View File

@@ -107,7 +107,7 @@ convert_tuples_by_name(TupleDesc indesc,
int n = outdesc->natts;
/* Verify compatibility and prepare attribute-number map */
attrMap = build_attrmap_by_name_if_req(indesc, outdesc);
attrMap = build_attrmap_by_name_if_req(indesc, outdesc, false);
if (attrMap == NULL)
{