Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/BABEL_3_X_DEV' into bugs-fix-4…
Browse files Browse the repository at this point in the history
…379-4383
  • Loading branch information
roshan0708 committed Nov 22, 2023
2 parents 1515691 + 7fc889c commit 14d82ce
Show file tree
Hide file tree
Showing 75 changed files with 1,787 additions and 3,148 deletions.
1 change: 1 addition & 0 deletions contrib/babelfishpg_tds/error_mapping.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ XX000 ERRCODE_INTERNAL_ERROR "CREATE FUNCTION failed because a column name is no
42883 ERRCODE_UNDEFINED_FUNCTION "could not identify an equality operator for type %s" SQL_ERROR_306 16
42883 ERRCODE_UNDEFINED_FUNCTION "could not identify an ordering operator for type %s" SQL_ERROR_306 16
42883 ERRCODE_UNDEFINED_FUNCTION "%s %s expects parameter \"%s\", which was not supplied." SQL_ERROR_201 16
42883 ERRCODE_UNDEFINED_FUNCTION "Procedure or function \'%s\' expects parameter \'%s\', which was not supplied." SQL_ERROR_201 16
42883 ERRCODE_UNDEFINED_FUNCTION "%s %s has no parameters and arguments were supplied." SQL_ERROR_8146 16
42883 ERRCODE_UNDEFINED_FUNCTION "%s %s has too many arguments specified." SQL_ERROR_8144 16
42883 ERRCODE_UNDEFINED_FUNCTION "\"%s\" is not an parameter for %s %s." SQL_ERROR_8145 16
Expand Down
10 changes: 9 additions & 1 deletion contrib/babelfishpg_tds/src/backend/tds/tdsprinttup.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ TdsPrinttupStartup(DestReceiver *self, int operation, TupleDesc typeinfo)
{
DR_printtup *myState = (DR_printtup *) self;
Portal portal = myState->portal;
PlannedStmt *plannedStmt = PortalGetPrimaryStmt(portal);
List *targetList = NIL;

if (portal->strategy != PORTAL_MULTI_QUERY)
{
targetList = FetchStatementTargetList((Node *) plannedStmt);
}

/*
* Create I/O buffer to be used for all messages. This cannot be inside
Expand All @@ -78,7 +85,8 @@ TdsPrinttupStartup(DestReceiver *self, int operation, TupleDesc typeinfo)
ALLOCSET_DEFAULT_SIZES);

TdsSendRowDescription(typeinfo,
FetchPortalTargetList(portal),
plannedStmt,
targetList,
portal->formats);
return;
}
Expand Down
156 changes: 139 additions & 17 deletions contrib/babelfishpg_tds/src/backend/tds/tdsresponse.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "miscadmin.h"
#include "nodes/pathnodes.h"
#include "parser/parse_coerce.h"
#include "parser/parsetree.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
Expand Down Expand Up @@ -128,6 +129,8 @@ static void FillTabNameWithoutNumParts(StringInfo buf, uint8 numParts, TdsRelati
static void SetTdsEstateErrorData(void);
static void ResetTdsEstateErrorData(void);
static void SetAttributesForColmetada(TdsColumnMetaData *col);
static int32 resolve_numeric_typmod_from_exp(Plan *plan, Node *expr);
static int32 resolve_numeric_typmod_outer_var(Plan *plan, AttrNumber attno);

static inline void
SendPendingDone(bool more)
Expand Down Expand Up @@ -401,9 +404,112 @@ PrintTupPrepareInfo(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
}
}

static int32
resolve_numeric_typmod_from_append_or_mergeappend(Plan *plan, AttrNumber attno)
{
ListCell *lc;
int32 max_precision = 0,
max_scale = 0,
precision = 0,
scale = 0,
integralDigitCount = 0,
typmod = -1,
result_typmod = -1;
List *planlist = NIL;
if (IsA(plan, Append))
{
planlist = ((Append *) plan)->appendplans;
}
else if(IsA(plan, MergeAppend))
{
planlist = ((MergeAppend *) plan)->mergeplans;
}

Assert(planlist != NIL);
foreach(lc, planlist)
{
TargetEntry *tle;
Plan *outerplan = (Plan *) lfirst(lc);

/* if outerplan is SubqueryScan then use actual subplan */
if (IsA(outerplan, SubqueryScan))
outerplan = ((SubqueryScan *)outerplan)->subplan;

tle = get_tle_by_resno(outerplan->targetlist, attno);
if (IsA(tle->expr, Var))
{
Var *var = (Var *)tle->expr;
if (var->varno == OUTER_VAR)
{
typmod = resolve_numeric_typmod_outer_var(outerplan, var->varattno);
}
else
{
typmod = resolve_numeric_typmod_from_exp(outerplan, (Node *)tle->expr);
}
}
else
{
typmod = resolve_numeric_typmod_from_exp(outerplan, (Node *)tle->expr);
}
if (typmod == -1)
continue;
scale = (typmod - VARHDRSZ) & 0xffff;
precision = ((typmod - VARHDRSZ) >> 16) & 0xffff;
integralDigitCount = Max(precision - scale, max_precision - max_scale);
max_scale = Max(max_scale, scale);
max_precision = integralDigitCount + max_scale;
/*
* If max_precision is more than TDS_MAX_NUM_PRECISION then adjust precision
* to TDS_MAX_NUM_PRECISION at the cost of scale.
*/
if (max_precision > TDS_MAX_NUM_PRECISION)
{
max_scale = Max(0, max_scale - (max_precision - TDS_MAX_NUM_PRECISION));
max_precision = TDS_MAX_NUM_PRECISION;
}
result_typmod = ((max_precision << 16) | max_scale) + VARHDRSZ;
}
/* If max_precision is still default then use tds specific defaults */
if (result_typmod == -1)
{
result_typmod = ((tds_default_numeric_precision << 16) | tds_default_numeric_scale) + VARHDRSZ;
}
return result_typmod;
}

static int32
resolve_numeric_typmod_outer_var(Plan *plan, AttrNumber attno)
{
TargetEntry *tle;
Plan *outerplan = NULL;

if (IsA(plan, Append) || IsA(plan, MergeAppend))
return resolve_numeric_typmod_from_append_or_mergeappend(plan, attno);
else
outerplan = outerPlan(plan);

/* if outerplan is SubqueryScan then use actual subplan */
if (IsA(outerplan, SubqueryScan))
outerplan = ((SubqueryScan *)outerplan)->subplan;

/* outerplan must not be NULL */
Assert(outerplan);
tle = get_tle_by_resno(outerplan->targetlist, attno);
if (IsA(tle->expr, Var))
{
Var *var = (Var *)tle->expr;
if (var->varno == OUTER_VAR)
{
return resolve_numeric_typmod_outer_var(outerplan, var->varattno);
}
}
return resolve_numeric_typmod_from_exp(outerplan, (Node *)tle->expr);
}

/* look for a typmod to return from a numeric expression */
static int32
resolve_numeric_typmod_from_exp(Node *expr)
resolve_numeric_typmod_from_exp(Plan *plan, Node *expr)
{
if (expr == NULL)
return -1;
Expand Down Expand Up @@ -434,6 +540,12 @@ resolve_numeric_typmod_from_exp(Node *expr)
{
Var *var = (Var *) expr;

/* If this var referes to tuple returned by its outer plan then find the original tle from it */
if (var->varno == OUTER_VAR)
{
Assert(plan);
return (resolve_numeric_typmod_outer_var(plan, var->varattno));
}
return var->vartypmod;
}
case T_OpExpr:
Expand Down Expand Up @@ -464,8 +576,8 @@ resolve_numeric_typmod_from_exp(Node *expr)
{
arg1 = linitial(op->args);
arg2 = lsecond(op->args);
typmod1 = resolve_numeric_typmod_from_exp(arg1);
typmod2 = resolve_numeric_typmod_from_exp(arg2);
typmod1 = resolve_numeric_typmod_from_exp(plan, arg1);
typmod2 = resolve_numeric_typmod_from_exp(plan, arg2);
scale1 = (typmod1 - VARHDRSZ) & 0xffff;
precision1 = ((typmod1 - VARHDRSZ) >> 16) & 0xffff;
scale2 = (typmod2 - VARHDRSZ) & 0xffff;
Expand All @@ -474,7 +586,7 @@ resolve_numeric_typmod_from_exp(Node *expr)
else if (list_length(op->args) == 1)
{
arg1 = linitial(op->args);
typmod1 = resolve_numeric_typmod_from_exp(arg1);
typmod1 = resolve_numeric_typmod_from_exp(plan, arg1);
scale1 = (typmod1 - VARHDRSZ) & 0xffff;
precision1 = ((typmod1 - VARHDRSZ) >> 16) & 0xffff;
scale2 = 0;
Expand Down Expand Up @@ -545,7 +657,7 @@ resolve_numeric_typmod_from_exp(Node *expr)
scale = Min(precision, TDS_MAX_NUM_PRECISION) - integralDigitCount;

/*
* precisionn adjustment to TDS_MAX_NUM_PRECISION
* precision adjustment to TDS_MAX_NUM_PRECISION
*/
if (precision > TDS_MAX_NUM_PRECISION)
precision = TDS_MAX_NUM_PRECISION;
Expand Down Expand Up @@ -653,7 +765,7 @@ resolve_numeric_typmod_from_exp(Node *expr)
Assert(nullif->args != NIL);

arg1 = linitial(nullif->args);
return resolve_numeric_typmod_from_exp(arg1);
return resolve_numeric_typmod_from_exp(plan, arg1);
}
case T_CoalesceExpr:
{
Expand All @@ -676,7 +788,7 @@ resolve_numeric_typmod_from_exp(Node *expr)
foreach(lc, coale->args)
{
arg = lfirst(lc);
arg_typmod = resolve_numeric_typmod_from_exp(arg);
arg_typmod = resolve_numeric_typmod_from_exp(plan, arg);
/* return -1 if we fail to resolve one of the arg's typmod */
if (arg_typmod == -1)
return -1;
Expand Down Expand Up @@ -717,7 +829,7 @@ resolve_numeric_typmod_from_exp(Node *expr)
{
casewhen = lfirst(lc);
casewhen_result = (Node *) casewhen->result;
typmod = resolve_numeric_typmod_from_exp(casewhen_result);
typmod = resolve_numeric_typmod_from_exp(plan, casewhen_result);

/*
* return -1 if we fail to resolve one of the result's
Expand Down Expand Up @@ -752,7 +864,7 @@ resolve_numeric_typmod_from_exp(Node *expr)
Assert(aggref->args != NIL);

te = (TargetEntry *) linitial(aggref->args);
typmod = resolve_numeric_typmod_from_exp((Node *) te->expr);
typmod = resolve_numeric_typmod_from_exp(plan, (Node *) te->expr);
aggFuncName = get_func_name(aggref->aggfnoid);

scale = (typmod - VARHDRSZ) & 0xffff;
Expand Down Expand Up @@ -798,7 +910,7 @@ resolve_numeric_typmod_from_exp(Node *expr)
{
PlaceHolderVar *phv = (PlaceHolderVar *) expr;

return resolve_numeric_typmod_from_exp((Node *) phv->phexpr);
return resolve_numeric_typmod_from_exp(plan, (Node *) phv->phexpr);
}
case T_RelabelType:
{
Expand All @@ -807,7 +919,7 @@ resolve_numeric_typmod_from_exp(Node *expr)
if (rlt->resulttypmod != -1)
return rlt->resulttypmod;
else
return resolve_numeric_typmod_from_exp((Node *) rlt->arg);
return resolve_numeric_typmod_from_exp(plan, (Node *) rlt->arg);
}
/* TODO handle more Expr types if needed */
default:
Expand Down Expand Up @@ -1562,8 +1674,8 @@ TdsGetGenericTypmod(Node *expr)
* for a relation. (used for keyset and dynamic cursors)
*/
void
PrepareRowDescription(TupleDesc typeinfo, List *targetlist, int16 *formats,
bool extendedInfo, bool fetchPkeys)
PrepareRowDescription(TupleDesc typeinfo, PlannedStmt *plannedstmt, List *targetlist,
int16 *formats, bool extendedInfo, bool fetchPkeys)
{
int natts = typeinfo->natts;
int attno;
Expand Down Expand Up @@ -1782,7 +1894,16 @@ PrepareRowDescription(TupleDesc typeinfo, List *targetlist, int16 *formats,
* than -1.
*/
if (atttypmod == -1 && tle != NULL)
atttypmod = resolve_numeric_typmod_from_exp((Node *) tle->expr);
{
if (!plannedstmt || !plannedstmt->planTree)
{
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("Internal error detected while calculating the precision of numeric expression"),
errhint("plannedstmt is NULL while calculating the precision of numeric expression when it contains outer var")));
}
atttypmod = resolve_numeric_typmod_from_exp(plannedstmt->planTree, (Node *) tle->expr);
}

/*
* Get the precision and scale out of the typmod value if
Expand Down Expand Up @@ -2558,7 +2679,7 @@ TdsSendInfoOrError(int token, int number, int state, int class,
}

void
TdsSendRowDescription(TupleDesc typeinfo,
TdsSendRowDescription(TupleDesc typeinfo, PlannedStmt *plannedstmt,
List *targetlist, int16 *formats)
{
TDSRequest request = TdsRequestCtrl->request;
Expand All @@ -2567,7 +2688,7 @@ TdsSendRowDescription(TupleDesc typeinfo,
Assert(typeinfo != NULL);

/* Prepare the column metadata first */
PrepareRowDescription(typeinfo, targetlist, formats, false, false);
PrepareRowDescription(typeinfo, plannedstmt, targetlist, formats, false, false);

/*
* If fNoMetadata flags is set in RPC header flag, the server doesn't need
Expand Down Expand Up @@ -3293,7 +3414,8 @@ TDSStatementExceptionCallback(PLtsql_execstate *estate, PLtsql_stmt *stmt, bool
void
SendColumnMetadata(TupleDesc typeinfo, List *targetlist, int16 *formats)
{
TdsSendRowDescription(typeinfo, targetlist, formats);
/* This will only be used for sp_preapre request hence do not need to pass plannedstmt */
TdsSendRowDescription(typeinfo, NULL, targetlist, formats);
TdsPrintTupShutdown();
}

Expand Down
13 changes: 9 additions & 4 deletions contrib/babelfishpg_tds/src/backend/tds/tdsrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2334,10 +2334,15 @@ static void
SendCursorResponse(TDSRequestSP req)
{
int cmd_type = TDS_CMD_UNKNOWN;
Portal portal;

/* fetch the portal */
portal = GetPortalFromCursorHandle(req->cursorHandle, false);
Portal portal = GetPortalFromCursorHandle(req->cursorHandle, false);
PlannedStmt *plannedStmt = PortalGetPrimaryStmt(portal);
List *targetList = NIL;

if (portal->strategy != PORTAL_MULTI_QUERY)
{
targetList = FetchStatementTargetList((Node *) plannedStmt);
}

/*
* If we are in aborted transaction state, we can't run
Expand Down Expand Up @@ -2366,7 +2371,7 @@ SendCursorResponse(TDSRequestSP req)
* break the protocol. We also need to fetch the primary keys for dynamic
* and keyset cursors (XXX: these cursors are not yet implemented).
*/
PrepareRowDescription(portal->tupDesc, FetchPortalTargetList(portal),
PrepareRowDescription(portal->tupDesc, plannedStmt, targetList,
portal->formats, true,
(req->scrollopt & (SP_CURSOR_SCROLLOPT_DYNAMIC | SP_CURSOR_SCROLLOPT_KEYSET)));

Expand Down
6 changes: 3 additions & 3 deletions contrib/babelfishpg_tds/src/include/tds_response.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ extern void TdsSendDone(int tag, int status,
extern void SendColumnMetadataToken(int natts, bool sendRowStat);
extern void SendTabNameToken(void);
extern void SendColInfoToken(int natts, bool sendRowStat);
extern void PrepareRowDescription(TupleDesc typeinfo, List *targetlist, int16 *formats,
bool extendedInfo, bool fetchPkeys);
extern void PrepareRowDescription(TupleDesc typeinfo, PlannedStmt *plannedstmt, List *targetlist,
int16 *formats, bool extendedInfo, bool fetchPkeys);
extern void SendReturnValueTokenInternal(ParameterToken token, uint8 status,
FmgrInfo *finfo, Datum datum, bool isNull,
bool forceCoercion);
Expand All @@ -85,7 +85,7 @@ extern void TdsSendEnvChangeBinary(int envid,
void *old, int old_nbytes);
extern void TdsSendReturnStatus(int status);
extern void TdsSendHandle(void);
extern void TdsSendRowDescription(TupleDesc typeinfo,
extern void TdsSendRowDescription(TupleDesc typeinfo, PlannedStmt *PlannedStmt,
List *targetlist, int16 *formats);
extern bool TdsPrintTup(TupleTableSlot *slot, DestReceiver *self);
extern void TdsPrintTupShutdown(void);
Expand Down
11 changes: 0 additions & 11 deletions contrib/babelfishpg_tsql/sql/ownership.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ CREATE TABLE sys.babelfish_sysdatabases (

GRANT SELECT on sys.babelfish_sysdatabases TO PUBLIC;

-- BABELFISH_SCHEMA_PERMISSIONS
CREATE TABLE sys.babelfish_schema_permissions (
dbid smallint NOT NULL,
schema_name NAME NOT NULL,
object_name NAME NOT NULL,
permission NAME NOT NULL,
grantee NAME NOT NULL,
object_type NAME,
PRIMARY KEY(dbid, schema_name, object_name, permission, grantee)
);

-- BABELFISH_FUNCTION_EXT
CREATE TABLE sys.babelfish_function_ext (
nspname NAME NOT NULL,
Expand Down
Loading

0 comments on commit 14d82ce

Please sign in to comment.