Skip to content

Commit

Permalink
Fix protocol violation error happening due to Const node under append…
Browse files Browse the repository at this point in the history
… node (babelfish-for-postgresql#2720) (babelfish-for-postgresql#2734)

Previously, certain driver was running into an protocol violation error when Const node in target list appears under
Append node. This was happening because resolve_numeric_typmod_from_exp was not implemented to handle Const
node properly for decimal data types. This commit fixes that issue by properly handling decimal data type for Const
node under Append node.

Task: BABEL-5086

Signed-off-by: Dipesh Dhameliya <[email protected]>
  • Loading branch information
Deepesh125 authored Jul 11, 2024
1 parent d33f8b4 commit 2c62cf7
Show file tree
Hide file tree
Showing 3 changed files with 583 additions and 9 deletions.
32 changes: 24 additions & 8 deletions contrib/babelfishpg_tds/src/backend/tds/tdsresponse.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/pathnodes.h"
#include "parser/parse_coerce.h"
#include "parser/parse_type.h"
#include "parser/parsetree.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
Expand Down Expand Up @@ -126,6 +128,7 @@ static TdsColumnMetaData *colMetaData = NULL;
static List *relMetaDataInfoList = NULL;

static Oid sys_vector_oid = InvalidOid;
static Oid decimal_oid = InvalidOid;

static void FillTabNameWithNumParts(StringInfo buf, uint8 numParts, TdsRelationMetaDataInfo relMetaDataInfo);
static void FillTabNameWithoutNumParts(StringInfo buf, uint8 numParts, TdsRelationMetaDataInfo relMetaDataInfo);
Expand Down Expand Up @@ -511,6 +514,24 @@ resolve_numeric_typmod_outer_var(Plan *plan, AttrNumber attno)
return resolve_numeric_typmod_from_exp(outerplan, (Node *)tle->expr);
}

/*
* is_numeric_datatype - returns bool if given datatype is numeric or decimal.
*/
static bool
is_numeric_datatype(Oid typid)
{
if (typid == NUMERICOID)
{
return true;
}
if (!OidIsValid(decimal_oid))
{
TypeName *typename = makeTypeNameFromNameList(list_make2(makeString("sys"), makeString("decimal")));
decimal_oid = LookupTypeNameOid(NULL, typename, false);
}
return decimal_oid == typid;
}

/* look for a typmod to return from a numeric expression */
static int32
resolve_numeric_typmod_from_exp(Plan *plan, Node *expr)
Expand All @@ -524,15 +545,10 @@ resolve_numeric_typmod_from_exp(Plan *plan, Node *expr)
Const *con = (Const *) expr;
Numeric num;

/*
* TODO: We used a workaround here, that we will assume typmod
* is 0 if the value we have is not numeric. See walkaround in
* T_FuncExpr part of this function. JIRA: BABEL-1007
*/
if (con->consttype != NUMERICOID || con->constisnull)
if (!is_numeric_datatype(con->consttype) || con->constisnull)
{
return 0;
/* Typmod doesn 't really matter since it' s a const NULL. */
/* typmod is undefined */
return -1;
}
else
{
Expand Down
Loading

0 comments on commit 2c62cf7

Please sign in to comment.