From fc874cc3f3477474cc6db3ad9367d9c015a9a168 Mon Sep 17 00:00:00 2001 From: amit Date: Mon, 25 Nov 2019 16:38:32 +0900 Subject: [PATCH 2/2] Move more code to attmap.c --- src/backend/access/common/attmap.c | 287 ++++++++++++++++++++++++++++++++- src/backend/access/common/tupconvert.c | 274 ++----------------------------- src/backend/catalog/partition.c | 2 +- src/backend/catalog/pg_constraint.c | 1 - src/backend/commands/tablecmds.c | 2 +- src/backend/jit/llvm/llvmjit_expr.c | 1 - src/include/access/attmap.h | 12 +- src/include/access/tupconvert.h | 5 - 8 files changed, 305 insertions(+), 279 deletions(-) diff --git a/src/backend/access/common/attmap.c b/src/backend/access/common/attmap.c index 06df1dcfa4..9f47a97662 100644 --- a/src/backend/access/common/attmap.c +++ b/src/backend/access/common/attmap.c @@ -3,10 +3,11 @@ * attmap.c * Attribute mapping support. * - * This file provides utility routines to build and managed attribute - * mappings which are used when working on inheritance and partition trees - * for cloned object creation or tuple attribute comparison for runtime - * operations. + * This file provides utility routines to build and manage attribute mappings + * by comparing input and output TupleDescs. Such mappings are typically used + * by DDL operating on inheritance and partition trees to convert fully + * transformed expression trees from parent rowtype to child rowtype or vice + * versa. They are also used by the tuple conversion routines in tupconvert.c. * * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California @@ -20,6 +21,284 @@ #include "postgres.h" #include "access/attmap.h" +#include "access/htup_details.h" +#include "utils/builtins.h" + +/* + * Return a palloc'd bare attribute map for tuple conversion, matching input + * and output columns by position. (Dropped columns are ignored in both input + * and output.) This is normally a subroutine for convert_tuples_by_position + * in tupconvert.c, but can be used standalone. + * + * Note: the errdetail messages speak of indesc as the "returned" rowtype, + * outdesc as the "expected" rowtype. This is okay for current uses but + * might need generalization in future. + */ +AttrMap * +convert_tuples_by_position_map(TupleDesc indesc, TupleDesc outdesc, + const char *msg) +{ + AttrMap *attrMap; + int nincols; + int noutcols; + int n; + int i; + int j; + bool same; + + /* + * The length is computed as the number of attributes of the expected + * rowtype as it includes dropped attributes in its count. + */ + n = outdesc->natts; + attrMap = make_attrmap(n); + + j = 0; /* j is next physical input attribute */ + nincols = noutcols = 0; /* these count non-dropped attributes */ + same = true; + for (i = 0; i < n; i++) + { + Form_pg_attribute att = TupleDescAttr(outdesc, i); + Oid atttypid; + int32 atttypmod; + + if (att->attisdropped) + continue; /* attrMap->attnums[i] is already 0 */ + noutcols++; + atttypid = att->atttypid; + atttypmod = att->atttypmod; + for (; j < indesc->natts; j++) + { + att = TupleDescAttr(indesc, j); + if (att->attisdropped) + continue; + nincols++; + /* Found matching column, check type */ + if (atttypid != att->atttypid || + (atttypmod != att->atttypmod && atttypmod >= 0)) + ereport(ERROR, + (errcode(ERRCODE_DATATYPE_MISMATCH), + errmsg_internal("%s", _(msg)), + errdetail("Returned type %s does not match expected type %s in column %d.", + format_type_with_typemod(att->atttypid, + att->atttypmod), + format_type_with_typemod(atttypid, + atttypmod), + noutcols))); + attrMap->attnums[i] = (AttrNumber) (j + 1); + j++; + break; + } + if (attrMap->attnums[i] == 0) + same = false; /* we'll complain below */ + } + + /* Check for unused input columns */ + for (; j < indesc->natts; j++) + { + if (TupleDescAttr(indesc, j)->attisdropped) + continue; + nincols++; + same = false; /* we'll complain below */ + } + + /* Report column count mismatch using the non-dropped-column counts */ + if (!same) + ereport(ERROR, + (errcode(ERRCODE_DATATYPE_MISMATCH), + errmsg_internal("%s", _(msg)), + errdetail("Number of returned columns (%d) does not match " + "expected column count (%d).", + nincols, noutcols))); + + /* + * Check to see if the map is one-to-one, in which case we need not do a + * tuple conversion. + */ + if (indesc->natts == outdesc->natts) + { + for (i = 0; i < n; i++) + { + Form_pg_attribute inatt; + Form_pg_attribute outatt; + + if (attrMap->attnums[i] == (i + 1)) + continue; + + /* + * If it's a dropped column and the corresponding input column is + * also dropped, we needn't convert. However, attlen and attalign + * must agree. + */ + inatt = TupleDescAttr(indesc, i); + outatt = TupleDescAttr(outdesc, i); + if (attrMap->attnums[i] == 0 && + inatt->attisdropped && + inatt->attlen == outatt->attlen && + inatt->attalign == outatt->attalign) + continue; + + same = false; + break; + } + } + else + same = false; + + if (same) + { + /* Runtime conversion is not needed */ + free_attrmap(attrMap); + return NULL; + } + + return attrMap; +} + +/* + * Return a palloc'd bare attribute map for tuple conversion, matching input + * 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. + */ +AttrMap * +convert_tuples_by_name_map(TupleDesc indesc, + TupleDesc outdesc) +{ + AttrMap *attrMap; + int outnatts; + int innatts; + int i; + int nextindesc = -1; + + outnatts = outdesc->natts; + innatts = indesc->natts; + + attrMap = make_attrmap(outnatts); + for (i = 0; i < outnatts; i++) + { + Form_pg_attribute outatt = TupleDescAttr(outdesc, i); + char *attname; + Oid atttypid; + int32 atttypmod; + int j; + + if (outatt->attisdropped) + continue; /* attrMap->attnums[i] is already 0 */ + attname = NameStr(outatt->attname); + atttypid = outatt->atttypid; + atttypmod = outatt->atttypmod; + + /* + * Now search for an attribute with the same name in the indesc. It + * seems likely that a partitioned table will have the attributes in + * the same order as the partition, so the search below is optimized + * for that case. It is possible that columns are dropped in one of + * the relations, but not the other, so we use the 'nextindesc' + * counter to track the starting point of the search. If the inner + * loop encounters dropped columns then it will have to skip over + * them, but it should leave 'nextindesc' at the correct position for + * the next outer loop. + */ + for (j = 0; j < innatts; j++) + { + Form_pg_attribute inatt; + + nextindesc++; + if (nextindesc >= innatts) + nextindesc = 0; + + inatt = TupleDescAttr(indesc, nextindesc); + if (inatt->attisdropped) + continue; + if (strcmp(attname, NameStr(inatt->attname)) == 0) + { + /* Found it, check type */ + if (atttypid != inatt->atttypid || atttypmod != inatt->atttypmod) + ereport(ERROR, + (errcode(ERRCODE_DATATYPE_MISMATCH), + errmsg("could not convert row type"), + errdetail("Attribute \"%s\" of type %s does not match corresponding attribute of type %s.", + attname, + format_type_be(outdesc->tdtypeid), + format_type_be(indesc->tdtypeid)))); + attrMap->attnums[i] = inatt->attnum; + break; + } + } + if (attrMap->attnums[i] == 0) + ereport(ERROR, + (errcode(ERRCODE_DATATYPE_MISMATCH), + errmsg("could not convert row type"), + errdetail("Attribute \"%s\" of type %s does not exist in type %s.", + attname, + format_type_be(outdesc->tdtypeid), + format_type_be(indesc->tdtypeid)))); + } + return attrMap; +} + +/* + * Returns mapping created by convert_tuples_by_name_map, or NULL if no + * conversion not required. This is a convenience routine for + * convert_tuples_by_name() in tupconvert.c and other functions. + */ +AttrMap * +convert_tuples_by_name_map_if_req(TupleDesc indesc, + TupleDesc outdesc) +{ + AttrMap *attrMap; + int n = outdesc->natts; + int i; + bool same; + + /* Verify compatibility and prepare attribute-number map */ + attrMap = convert_tuples_by_name_map(indesc, outdesc); + + /* + * Check to see if the map is one-to-one, in which case we need not do a + * tuple conversion. + */ + if (indesc->natts == outdesc->natts) + { + same = true; + for (i = 0; i < n; i++) + { + Form_pg_attribute inatt; + Form_pg_attribute outatt; + + if (attrMap->attnums[i] == (i + 1)) + continue; + + /* + * If it's a dropped column and the corresponding input column is + * also dropped, we needn't convert. However, attlen and attalign + * must agree. + */ + inatt = TupleDescAttr(indesc, i); + outatt = TupleDescAttr(outdesc, i); + if (attrMap->attnums[i] == 0 && + inatt->attisdropped && + inatt->attlen == outatt->attlen && + inatt->attalign == outatt->attalign) + continue; + + same = false; + break; + } + } + else + same = false; + + if (same) + { + /* Runtime conversion is not needed */ + free_attrmap(attrMap); + return NULL; + } + else + return attrMap; +} /* * make_attrmap diff --git a/src/backend/access/common/tupconvert.c b/src/backend/access/common/tupconvert.c index 94b90d6115..824bfabd94 100644 --- a/src/backend/access/common/tupconvert.c +++ b/src/backend/access/common/tupconvert.c @@ -18,20 +18,18 @@ */ #include "postgres.h" -#include "access/htup_details.h" #include "access/tupconvert.h" #include "executor/tuptable.h" -#include "utils/builtins.h" /* * The conversion setup routines have the following common API: * - * The setup routine checks whether the given source and destination tuple - * descriptors are logically compatible. If not, it throws an error. - * If so, it returns NULL if they are physically compatible (ie, no conversion - * is needed), else a TupleConversionMap that can be used by execute_attr_map_tuple - * to perform the conversion. + * The setup routine checks using attmap.c whether the given source and + * destination tuple descriptors are logically compatible. If not, it throws + * an error. If so, it returns NULL if they are physically compatible (ie, no + * conversion is needed), else a TupleConversionMap that can be used by + * execute_attr_map_tuple or execute_attr_map_slot to perform the conversion. * * The TupleConversionMap, if needed, is palloc'd in the caller's memory * context. Also, the given tuple descriptors are referenced by the map, @@ -56,10 +54,6 @@ /* * Set up for tuple conversion, matching input and output columns by * position. (Dropped columns are ignored in both input and output.) - * - * Note: the errdetail messages speak of indesc as the "returned" rowtype, - * outdesc as the "expected" rowtype. This is okay for current uses but - * might need generalization in future. */ TupleConversionMap * convert_tuples_by_position(TupleDesc indesc, @@ -67,119 +61,15 @@ convert_tuples_by_position(TupleDesc indesc, const char *msg) { TupleConversionMap *map; - AttrMap *attrMap; - int nincols; - int noutcols; int n; - int i; - int j; - bool same; + AttrMap *attrMap; /* Verify compatibility and prepare attribute-number map */ - n = outdesc->natts; + attrMap = convert_tuples_by_position_map(indesc, outdesc, msg); - /* - * The length is computed as the number of attributes of the expected - * rowtype as it includes dropped attributes in its count. - */ - attrMap = make_attrmap(n); - - j = 0; /* j is next physical input attribute */ - nincols = noutcols = 0; /* these count non-dropped attributes */ - same = true; - for (i = 0; i < n; i++) + if (attrMap == NULL) { - Form_pg_attribute att = TupleDescAttr(outdesc, i); - Oid atttypid; - int32 atttypmod; - - if (att->attisdropped) - continue; /* attrMap->attnums[i] is already 0 */ - noutcols++; - atttypid = att->atttypid; - atttypmod = att->atttypmod; - for (; j < indesc->natts; j++) - { - att = TupleDescAttr(indesc, j); - if (att->attisdropped) - continue; - nincols++; - /* Found matching column, check type */ - if (atttypid != att->atttypid || - (atttypmod != att->atttypmod && atttypmod >= 0)) - ereport(ERROR, - (errcode(ERRCODE_DATATYPE_MISMATCH), - errmsg_internal("%s", _(msg)), - errdetail("Returned type %s does not match expected type %s in column %d.", - format_type_with_typemod(att->atttypid, - att->atttypmod), - format_type_with_typemod(atttypid, - atttypmod), - noutcols))); - attrMap->attnums[i] = (AttrNumber) (j + 1); - j++; - break; - } - if (attrMap->attnums[i] == 0) - same = false; /* we'll complain below */ - } - - /* Check for unused input columns */ - for (; j < indesc->natts; j++) - { - if (TupleDescAttr(indesc, j)->attisdropped) - continue; - nincols++; - same = false; /* we'll complain below */ - } - - /* Report column count mismatch using the non-dropped-column counts */ - if (!same) - ereport(ERROR, - (errcode(ERRCODE_DATATYPE_MISMATCH), - errmsg_internal("%s", _(msg)), - errdetail("Number of returned columns (%d) does not match " - "expected column count (%d).", - nincols, noutcols))); - - /* - * Check to see if the map is one-to-one, in which case we need not do a - * tuple conversion. - */ - if (indesc->natts == outdesc->natts) - { - for (i = 0; i < n; i++) - { - Form_pg_attribute inatt; - Form_pg_attribute outatt; - - if (attrMap->attnums[i] == (i + 1)) - continue; - - /* - * If it's a dropped column and the corresponding input column is - * also dropped, we needn't convert. However, attlen and attalign - * must agree. - */ - inatt = TupleDescAttr(indesc, i); - outatt = TupleDescAttr(outdesc, i); - if (attrMap->attnums[i] == 0 && - inatt->attisdropped && - inatt->attlen == outatt->attlen && - inatt->attalign == outatt->attalign) - continue; - - same = false; - break; - } - } - else - same = false; - - if (same) - { - /* Runtime conversion is not needed */ - free_attrmap(attrMap); + /* runtime conversion is not needed */ return NULL; } @@ -189,6 +79,7 @@ convert_tuples_by_position(TupleDesc indesc, map->outdesc = outdesc; map->attrMap = attrMap; /* preallocate workspace for Datum arrays */ + n = outdesc->natts + 1; /* +1 for NULL */ map->outvalues = (Datum *) palloc(n * sizeof(Datum)); map->outisnull = (bool *) palloc(n * sizeof(bool)); n = indesc->natts + 1; /* +1 for NULL */ @@ -242,151 +133,6 @@ convert_tuples_by_name(TupleDesc indesc, } /* - * Return a palloc'd bare attribute map for tuple conversion, matching input - * and output columns by name. (Dropped columns are ignored in both input and - * output.) This is normally a subroutine for convert_tuples_by_name, but can - * be used standalone. - */ -AttrMap * -convert_tuples_by_name_map(TupleDesc indesc, - TupleDesc outdesc) -{ - AttrMap *attrMap; - int outnatts; - int innatts; - int i; - int nextindesc = -1; - - outnatts = outdesc->natts; - innatts = indesc->natts; - - attrMap = make_attrmap(outnatts); - for (i = 0; i < outnatts; i++) - { - Form_pg_attribute outatt = TupleDescAttr(outdesc, i); - char *attname; - Oid atttypid; - int32 atttypmod; - int j; - - if (outatt->attisdropped) - continue; /* attrMap->attnums[i] is already 0 */ - attname = NameStr(outatt->attname); - atttypid = outatt->atttypid; - atttypmod = outatt->atttypmod; - - /* - * Now search for an attribute with the same name in the indesc. It - * seems likely that a partitioned table will have the attributes in - * the same order as the partition, so the search below is optimized - * for that case. It is possible that columns are dropped in one of - * the relations, but not the other, so we use the 'nextindesc' - * counter to track the starting point of the search. If the inner - * loop encounters dropped columns then it will have to skip over - * them, but it should leave 'nextindesc' at the correct position for - * the next outer loop. - */ - for (j = 0; j < innatts; j++) - { - Form_pg_attribute inatt; - - nextindesc++; - if (nextindesc >= innatts) - nextindesc = 0; - - inatt = TupleDescAttr(indesc, nextindesc); - if (inatt->attisdropped) - continue; - if (strcmp(attname, NameStr(inatt->attname)) == 0) - { - /* Found it, check type */ - if (atttypid != inatt->atttypid || atttypmod != inatt->atttypmod) - ereport(ERROR, - (errcode(ERRCODE_DATATYPE_MISMATCH), - errmsg("could not convert row type"), - errdetail("Attribute \"%s\" of type %s does not match corresponding attribute of type %s.", - attname, - format_type_be(outdesc->tdtypeid), - format_type_be(indesc->tdtypeid)))); - attrMap->attnums[i] = inatt->attnum; - break; - } - } - if (attrMap->attnums[i] == 0) - ereport(ERROR, - (errcode(ERRCODE_DATATYPE_MISMATCH), - errmsg("could not convert row type"), - errdetail("Attribute \"%s\" of type %s does not exist in type %s.", - attname, - format_type_be(outdesc->tdtypeid), - format_type_be(indesc->tdtypeid)))); - } - return attrMap; -} - -/* - * Returns mapping created by convert_tuples_by_name_map, or NULL if no - * conversion not required. This is a convenience routine for - * convert_tuples_by_name() and other functions. - */ -AttrMap * -convert_tuples_by_name_map_if_req(TupleDesc indesc, - TupleDesc outdesc) -{ - AttrMap *attrMap; - int n = outdesc->natts; - int i; - bool same; - - /* Verify compatibility and prepare attribute-number map */ - attrMap = convert_tuples_by_name_map(indesc, outdesc); - - /* - * Check to see if the map is one-to-one, in which case we need not do a - * tuple conversion. - */ - if (indesc->natts == outdesc->natts) - { - same = true; - for (i = 0; i < n; i++) - { - Form_pg_attribute inatt; - Form_pg_attribute outatt; - - if (attrMap->attnums[i] == (i + 1)) - continue; - - /* - * If it's a dropped column and the corresponding input column is - * also dropped, we needn't convert. However, attlen and attalign - * must agree. - */ - inatt = TupleDescAttr(indesc, i); - outatt = TupleDescAttr(outdesc, i); - if (attrMap->attnums[i] == 0 && - inatt->attisdropped && - inatt->attlen == outatt->attlen && - inatt->attalign == outatt->attalign) - continue; - - same = false; - break; - } - } - else - same = false; - - if (same) - { - /* Runtime conversion is not needed */ - free_attrmap(attrMap); - return NULL; - } - else - return attrMap; -} - -/* * Perform conversion of a tuple according to the map. */ HeapTuple diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c index 2b14e9f31a..7fb545bd2f 100644 --- a/src/backend/catalog/partition.c +++ b/src/backend/catalog/partition.c @@ -14,11 +14,11 @@ */ #include "postgres.h" +#include "access/attmap.h" #include "access/genam.h" #include "access/htup_details.h" #include "access/sysattr.h" #include "access/table.h" -#include "access/tupconvert.h" #include "catalog/indexing.h" #include "catalog/partition.h" #include "catalog/pg_inherits.h" diff --git a/src/backend/catalog/pg_constraint.c b/src/backend/catalog/pg_constraint.c index 56568b0105..25c52134f0 100644 --- a/src/backend/catalog/pg_constraint.c +++ b/src/backend/catalog/pg_constraint.c @@ -18,7 +18,6 @@ #include "access/htup_details.h" #include "access/sysattr.h" #include "access/table.h" -#include "access/tupconvert.h" #include "access/xact.h" #include "catalog/catalog.h" #include "catalog/dependency.h" diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 0f1b1a7cd3..6ba353d48c 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -14,6 +14,7 @@ */ #include "postgres.h" +#include "access/attmap.h" #include "access/genam.h" #include "access/heapam.h" #include "access/heapam_xlog.h" @@ -22,7 +23,6 @@ #include "access/relscan.h" #include "access/sysattr.h" #include "access/tableam.h" -#include "access/tupconvert.h" #include "access/xact.h" #include "access/xlog.h" #include "catalog/catalog.h" diff --git a/src/backend/jit/llvm/llvmjit_expr.c b/src/backend/jit/llvm/llvmjit_expr.c index a9d362100a..ffd887c71a 100644 --- a/src/backend/jit/llvm/llvmjit_expr.c +++ b/src/backend/jit/llvm/llvmjit_expr.c @@ -20,7 +20,6 @@ #include "access/htup_details.h" #include "access/nbtree.h" -#include "access/tupconvert.h" #include "catalog/objectaccess.h" #include "catalog/pg_type.h" #include "executor/execExpr.h" diff --git a/src/include/access/attmap.h b/src/include/access/attmap.h index 8e4882f096..d103d7b6eb 100644 --- a/src/include/access/attmap.h +++ b/src/include/access/attmap.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------- * - * attnum.h + * attmap.h * Definitions for PostgreSQL attribute mappings * * @@ -15,6 +15,7 @@ #define ATTMAP_H #include "access/attnum.h" +#include "access/tupdesc.h" /* * Attribute mapping structure @@ -35,7 +36,14 @@ typedef struct AttrMap int maplen; } AttrMap; -/* utility routines to manipulate attribute maps */ +extern AttrMap *convert_tuples_by_name_map(TupleDesc indesc, + TupleDesc outdesc); +extern AttrMap *convert_tuples_by_name_map_if_req(TupleDesc indesc, + TupleDesc outdesc); +extern AttrMap *convert_tuples_by_position_map(TupleDesc indesc, + TupleDesc outdesc, + const char *msg); + extern AttrMap *make_attrmap(int maplen); extern void free_attrmap(AttrMap *map); diff --git a/src/include/access/tupconvert.h b/src/include/access/tupconvert.h index f48cba967d..5ed7fe6d39 100644 --- a/src/include/access/tupconvert.h +++ b/src/include/access/tupconvert.h @@ -39,11 +39,6 @@ extern TupleConversionMap *convert_tuples_by_position(TupleDesc indesc, extern TupleConversionMap *convert_tuples_by_name(TupleDesc indesc, TupleDesc outdesc); -extern AttrMap *convert_tuples_by_name_map(TupleDesc indesc, - TupleDesc outdesc); -extern AttrMap *convert_tuples_by_name_map_if_req(TupleDesc indesc, - TupleDesc outdesc); - extern HeapTuple execute_attr_map_tuple(HeapTuple tuple, TupleConversionMap *map); extern TupleTableSlot *execute_attr_map_slot(AttrMap *attrMap, TupleTableSlot *in_slot, -- 2.11.0