Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

insert varchar column mismatch through INSERT INTO EXEC into a table #2017

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions contrib/babelfishpg_tsql/src/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ static bool plsql_TriggerRecursiveCheck(ResultRelInfo *resultRelInfo);
static bool bbf_check_rowcount_hook(int es_processed);

static char *get_local_schema_for_bbf_functions(Oid proc_nsp_oid);
extern bool called_from_tsql_insert_exec();

/*****************************************
* Replication Hooks
Expand Down Expand Up @@ -231,6 +232,7 @@ static set_local_schema_for_func_hook_type prev_set_local_schema_for_func_hook =
static bbf_get_sysadmin_oid_hook_type prev_bbf_get_sysadmin_oid_hook = NULL;
/* TODO: do we need to use variable to store hook value before transfrom pivot? No other function uses the same hook, should be redundant */
static transform_pivot_clause_hook_type pre_transform_pivot_clause_hook = NULL;
static called_from_tsql_insert_exec_hook_type pre_called_from_tsql_insert_exec_hook = NULL;

/*****************************************
* Install / Uninstall
Expand Down Expand Up @@ -396,6 +398,9 @@ InstallExtendedHooks(void)

prev_optimize_explicit_cast_hook = optimize_explicit_cast_hook;
optimize_explicit_cast_hook = optimize_explicit_cast;

pre_called_from_tsql_insert_exec_hook = called_from_tsql_insert_exec_hook;
called_from_tsql_insert_exec_hook = called_from_tsql_insert_exec;
}

void
Expand Down Expand Up @@ -459,6 +464,7 @@ UninstallExtendedHooks(void)
bbf_get_sysadmin_oid_hook = prev_bbf_get_sysadmin_oid_hook;
transform_pivot_clause_hook = pre_transform_pivot_clause_hook;
optimize_explicit_cast_hook = prev_optimize_explicit_cast_hook;
called_from_tsql_insert_exec_hook = pre_called_from_tsql_insert_exec_hook;
}

/*****************************************
Expand Down
67 changes: 62 additions & 5 deletions contrib/babelfishpg_tsql/src/pl_exec-2.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include "funcapi.h"

#include "access/table.h"
#include "access/attmap.h"
#include "catalog/namespace.h"
#include "catalog/pg_attribute.h"
#include "catalog/pg_language.h"
#include "commands/proclang.h"
#include "executor/tstoreReceiver.h"
Expand Down Expand Up @@ -64,6 +66,7 @@ static bool is_char_identstart(char c);
static bool is_char_identpart(char c);

void read_param_def(InlineCodeBlockArgs *args, const char *paramdefstr);
bool called_from_tsql_insert_exec(void);
void cache_inline_args(PLtsql_function *func, InlineCodeBlockArgs *args);
InlineCodeBlockArgs *create_args(int numargs);
InlineCodeBlockArgs *clone_inline_args(InlineCodeBlockArgs *args);
Expand Down Expand Up @@ -101,6 +104,7 @@ extern SPIPlanPtr prepare_stmt_exec(PLtsql_execstate *estate, PLtsql_function *f
extern int sp_prepare_count;

BulkCopyStmt *cstmt = NULL;
bool called_from_tsql_insert_execute = false;

int insert_bulk_rows_per_batch = DEFAULT_INSERT_BULK_ROWS_PER_BATCH;
int insert_bulk_kilobytes_per_batch = DEFAULT_INSERT_BULK_PACKET_SIZE;
Expand All @@ -112,7 +116,8 @@ static bool prev_insert_bulk_keep_nulls = false;

/* return a underlying node if n is implicit casting and underlying node is a certain type of node */
static Node *get_underlying_node_from_implicit_casting(Node *n, NodeTag underlying_nodetype);

static HeapTuple exec_cast_tuple(HeapTuple tuple, TupleConversionMap *tupleCastMap);

/*
* The pltsql_proc_return_code global variable is used to record the
* return code (RETURN 41 + 1) of the most recently completed procedure
Expand Down Expand Up @@ -2984,6 +2989,13 @@ exec_stmt_grantdb(PLtsql_execstate *estate, PLtsql_stmt_grantdb *stmt)
return PLTSQL_RC_OK;
}

bool called_from_tsql_insert_exec()
{
if (sql_dialect != SQL_DIALECT_TSQL)
return false;
return called_from_tsql_insert_execute;
}

/*
* For naked SELECT stmt in INSERT ... EXECUTE, instead of pushing the result to
* the client, we accumulate the result in estate->tuple_store (similar to
Expand All @@ -3007,10 +3019,11 @@ exec_stmt_insert_execute_select(PLtsql_execstate *estate, PLtsql_expr *query)
/* Use eval_mcontext for tuple conversion work */
oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));

called_from_tsql_insert_execute = true;
tupmap = convert_tuples_by_position(portal->tupDesc,
estate->tuple_store_desc,
gettext_noop("structure of query does not match function result type"));

called_from_tsql_insert_execute = false;
while (true)
{
uint64 i;
Expand All @@ -3028,7 +3041,7 @@ exec_stmt_insert_execute_select(PLtsql_execstate *estate, PLtsql_expr *query)
HeapTuple tuple = SPI_tuptable->vals[i];

if (tupmap)
tuple = execute_attr_map_tuple(tuple, tupmap);
tuple = exec_cast_tuple(tuple, tupmap);
tuplestore_puttuple(estate->tuple_store, tuple);
if (tupmap)
heap_freetuple(tuple);
Expand All @@ -3047,6 +3060,52 @@ exec_stmt_insert_execute_select(PLtsql_execstate *estate, PLtsql_expr *query)
return PLTSQL_RC_OK;
}

static HeapTuple exec_cast_tuple(HeapTuple tuple, TupleConversionMap *tupleCastMap)
{
AttrMap *attrMap = tupleCastMap->attrMap;
Oid intypeid;
Oid outtypeid;
int inttypemod;
int outtypemod;
Datum *invalues = tupleCastMap->invalues;
bool *inisnull = tupleCastMap->inisnull;
Datum *outvalues = tupleCastMap->outvalues;
bool *outisnull = tupleCastMap->outisnull;
int i;

/*
* Extract all the values of the old tuple, offsetting the arrays so that
* invalues[0] is left NULL and invalues[1] is the first source attribute;
* this exactly matches the numbering convention in attrMap.
*/
heap_deform_tuple(tuple, tupleCastMap->indesc, invalues + 1, inisnull + 1);

/*
* Transpose into proper fields of the new tuple.
*/
Assert(attrMap->maplen == tupleCastMap->outdesc->natts);
for (i = 0; i < attrMap->maplen; i++)
{
int j = attrMap->attnums[i];
Form_pg_attribute outatt = TupleDescAttr(tupleCastMap->outdesc, i);
Form_pg_attribute inatt = TupleDescAttr(tupleCastMap->indesc, i);

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

outisnull[i] = inisnull[j];
outvalues[i] = exec_cast_value(get_current_tsql_estate(),
forestkeeper marked this conversation as resolved.
Show resolved Hide resolved
invalues[j], &outisnull[i],
intypeid, inttypemod,
outtypeid, outtypemod
);
}
// now form the new tuple
return heap_form_tuple(tupleCastMap->outdesc, outvalues, outisnull);
}

int
exec_stmt_insert_bulk(PLtsql_execstate *estate, PLtsql_stmt_insert_bulk *stmt)
{
Expand Down Expand Up @@ -3118,7 +3177,6 @@ exec_stmt_insert_bulk(PLtsql_execstate *estate, PLtsql_stmt_insert_bulk *stmt)
return PLTSQL_RC_OK;
}


int exec_stmt_dbcc(PLtsql_execstate *estate, PLtsql_stmt_dbcc *stmt)
{
switch (stmt->dbcc_stmt_type)
Expand All @@ -3132,7 +3190,6 @@ int exec_stmt_dbcc(PLtsql_execstate *estate, PLtsql_stmt_dbcc *stmt)
return PLTSQL_RC_OK;
}


void exec_stmt_dbcc_checkident(PLtsql_stmt_dbcc *stmt)
{
struct dbcc_checkident dbcc_stmt = stmt->dbcc_stmt_data.dbcc_checkident;
Expand Down
1 change: 0 additions & 1 deletion contrib/babelfishpg_tsql/src/pl_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -4347,7 +4347,6 @@ pltsql_estate_setup(PLtsql_execstate *estate,
estate->insert_exec = (func->fn_prokind == PROKIND_PROCEDURE ||
strcmp(func->fn_signature, "inline_code_block") == 0)
&& rsi;

estate->pivot_number = 0;
estate->pivot_parsetree_list = NIL;

Expand Down
2 changes: 2 additions & 0 deletions contrib/babelfishpg_tsql/src/pl_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -4783,6 +4783,7 @@ pltsql_inline_handler(PG_FUNCTION_ARGS)
rsinfo.isDone = ExprSingleResult;
rsinfo.setResult = NULL;
rsinfo.setDesc = NULL;
ReleaseTupleDesc(reldesc);
}

/* And run the function */
Expand Down Expand Up @@ -4849,6 +4850,7 @@ pltsql_inline_handler(PG_FUNCTION_ARGS)
dest->receiveSlot(slot, dest);
ExecClearTuple(slot);
}
ReleaseTupleDesc(rsinfo.expectedDesc);
ExecDropSingleTupleTableSlot(slot);
}

Expand Down
29 changes: 29 additions & 0 deletions test/JDBC/expected/BABEL-2999-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
drop table if exists t2_BABEL2999;
GO

drop table t1_BABEL2999;
GO

drop table t3_BABEL2999;
GO

drop table t3_BABEL2999_2;
GO

drop procedure p1_BABEL2999;
GO

drop procedure p2_BABEL2999
GO

drop procedure p3_BABEL2999
GO

drop table t4_BABEL2999;
GO

drop table t5_BABEL2999;
GO

drop table t6_BABEL2999
GO
32 changes: 32 additions & 0 deletions test/JDBC/expected/BABEL-2999-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
drop table if exists t1_BABEL2999;
GO

create table t1_BABEL2999(b varchar(10));
GO

create table t2_BABEL2999(b int);
GO

create table t3_BABEL2999(b varchar);
GO

create procedure p1_BABEL2999 as select 'abc';
GO

create procedure p2_BABEL2999 as select 555;
GO

create table t3_BABEL2999_2(a int, b datetime, c varchar(20))
GO

create procedure p3_BABEL2999 as select '123', 123, 123;
GO

create table t4_BABEL2999( a binary(30), b varbinary(30), c varchar(30), d datetime, e smalldatetime)
GO

create table t5_BABEL2999( a decimal, b numeric)
GO

create table t6_BABEL2999( a int, b tinyint, c smallint)
GO
Loading
Loading