Skip to content

Commit

Permalink
fix insert exec with type cast (babelfish-for-postgresql#256)
Browse files Browse the repository at this point in the history
Previously insert exec implement didn't consider the type cast during execution, this fix has add a implicit type cast between insert execution and exec execution if the type is mismatched.

Task: BABEL-2999, BABEL-4426
Signed-off-by: Zhibai Song <szh@amazon.com>
  • Loading branch information
forestkeeper authored and Jason Teng committed Nov 16, 2023
1 parent 3377d2d commit d3ff0bc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/backend/access/common/attmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "access/htup_details.h"
#include "utils/builtins.h"

called_from_tsql_insert_exec_hook_type called_from_tsql_insert_exec_hook = NULL;

static bool check_attrmap_match(TupleDesc indesc,
TupleDesc outdesc,
Expand Down Expand Up @@ -114,8 +115,10 @@ build_attrmap_by_position(TupleDesc indesc,
nincols++;

/* Found matching column, now check type */
if (atttypid != att->atttypid ||
(atttypmod != att->atttypmod && atttypmod >= 0))
/* skip check type if it's tsql insert exec */
if ((atttypid != att->atttypid ||
(atttypmod != att->atttypmod && atttypmod >= 0)) &&
!(called_from_tsql_insert_exec_hook && called_from_tsql_insert_exec_hook()))
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg_internal("%s", _(msg)),
Expand Down Expand Up @@ -303,6 +306,14 @@ check_attrmap_match(TupleDesc indesc,
if (inatt->atthasmissing)
return false;

/**
* in tsql insert exec, we need a cast
*/
if (called_from_tsql_insert_exec_hook && called_from_tsql_insert_exec_hook()
&& (inatt->atttypid != outatt->atttypid ||
inatt->atttypmod != outatt->atttypmod))
return false;

if (attrMap->attnums[i] == (i + 1))
continue;

Expand Down
30 changes: 30 additions & 0 deletions src/backend/access/common/tupconvert.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "access/tupconvert.h"
#include "executor/tuptable.h"

exec_tsql_cast_value_hook_type exec_tsql_cast_value_hook = NULL;

/*
* The conversion setup routines have the following common API:
Expand Down Expand Up @@ -160,6 +161,35 @@ execute_attr_map_tuple(HeapTuple tuple, TupleConversionMap *map)
{
int j = attrMap->attnums[i];

/**
* if it's tsql insert exec, we'll consider value cast
*/
if (called_from_tsql_insert_exec_hook && called_from_tsql_insert_exec_hook())
{
Oid intypeid;
Oid outtypeid;
int inttypemod;
int outtypemod;

Form_pg_attribute outatt = TupleDescAttr(map->outdesc, i);
Form_pg_attribute inatt = TupleDescAttr(map->indesc, i);

outtypeid = outatt->atttypid;
outtypemod = outatt->atttypmod;
intypeid = inatt->atttypid;
inttypemod = inatt->atttypmod;

if (intypeid != outtypeid ||
inttypemod != outtypemod)
{
outisnull[i] = inisnull[j];
outvalues[i] = exec_tsql_cast_value_hook(
invalues[j], &outisnull[i],
intypeid, inttypemod,
outtypeid, outtypemod);
continue;
}
}
outvalues[i] = invalues[j];
outisnull[i] = inisnull[j];
}
Expand Down
2 changes: 2 additions & 0 deletions src/include/access/attmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ extern AttrMap *build_attrmap_by_name_if_req(TupleDesc indesc,
extern AttrMap *build_attrmap_by_position(TupleDesc indesc,
TupleDesc outdesc,
const char *msg);
typedef bool (*called_from_tsql_insert_exec_hook_type)();
extern PGDLLIMPORT called_from_tsql_insert_exec_hook_type called_from_tsql_insert_exec_hook;

#endif /* ATTMAP_H */
6 changes: 6 additions & 0 deletions src/include/access/tupconvert.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@ extern Bitmapset *execute_attr_map_cols(AttrMap *attrMap, Bitmapset *inbitmap);

extern void free_conversion_map(TupleConversionMap *map);

typedef Datum (*exec_tsql_cast_value_hook_type)(Datum value, bool *isnull,
Oid valtype, int32 valtypmod,
Oid reqtype, int32 reqtypmod);
extern PGDLLIMPORT exec_tsql_cast_value_hook_type exec_tsql_cast_value_hook;


#endif /* TUPCONVERT_H */

0 comments on commit d3ff0bc

Please sign in to comment.