Skip to content

Commit 7e44df2

Browse files
author
anju15bharti
committed
fix long idenitfier display issue
1 parent 0d5aaf1 commit 7e44df2

25 files changed

Lines changed: 5028 additions & 116 deletions

contrib/babelfishpg_tds/src/backend/tds/err_handler.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "utils/elog.h"
1111
#include "utils/hsearch.h"
1212
#include "utils/palloc.h" /* Needed for pstrdup() */
13+
#include "utils/memutils.h"
14+
#include <dlfcn.h>
1315

1416
#include "src/include/tds_int.h"
1517
#include "src/include/tds_response.h"
@@ -344,8 +346,38 @@ emit_tds_log(ErrorData *edata)
344346
tsql_error_state = 1;
345347
}
346348

347-
TdsSendError(tsql_error_code, tsql_error_state, tsql_error_sev,
348-
edata->message, error_lineno);
349+
{
350+
/* Rewrite truncated identifiers in error message */
351+
char *msg = edata->message;
352+
353+
{
354+
/*
355+
* Rewrite truncated identifiers in error message.
356+
* Truncated names are always [a-z0-9_] with 32-char hex MD5 suffix.
357+
* Search for patterns matching NAMEDATALEN-1 chars that look like
358+
* truncated identifiers and replace with full original from cache.
359+
*/
360+
typedef char *(*rewrite_fn_t)(const char *);
361+
static rewrite_fn_t rewrite_fn = NULL;
362+
static bool rewrite_resolved = false;
363+
364+
if (!rewrite_resolved)
365+
{
366+
rewrite_fn = (rewrite_fn_t) dlsym(RTLD_DEFAULT, "bbf_rewrite_truncated_identifiers");
367+
rewrite_resolved = true;
368+
}
369+
370+
if (rewrite_fn)
371+
{
372+
char *newmsg = rewrite_fn(msg);
373+
if (newmsg)
374+
msg = newmsg;
375+
}
376+
}
377+
378+
TdsSendError(tsql_error_code, tsql_error_state, tsql_error_sev,
379+
msg, error_lineno);
380+
}
349381

350382
/*
351383
* If we've not reached the main query loop yet, flush the error

contrib/babelfishpg_tds/src/backend/tds/tdsresponse.c

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
#include "parser/parse_type.h"
3333
#include "parser/parsetree.h"
3434
#include "utils/fmgroids.h"
35+
#include "utils/builtins.h"
36+
#include "rewrite/rewriteHandler.h"
37+
#include "utils/lsyscache.h"
3538
#include "utils/lsyscache.h"
3639
#include "utils/syscache.h"
3740
#include "utils/memdebug.h"
@@ -1234,7 +1237,6 @@ PrepareRowDescription(TupleDesc typeinfo, PlannedStmt *plannedstmt, List *target
12341237
*/
12351238
SetParamMetadataCommonInfo(col, finfo);
12361239
initStringInfo(&(col->colName));
1237-
appendStringInfoString(&col->colName, NameStr(att->attname));
12381240

12391241
/* Do we have a non-resjunk tlist item? */
12401242
while (tlist_item &&
@@ -1244,13 +1246,85 @@ PrepareRowDescription(TupleDesc typeinfo, PlannedStmt *plannedstmt, List *target
12441246
{
12451247
tle = (TargetEntry *) lfirst(tlist_item);
12461248

1249+
/*
1250+
* Use tle->resname if available and longer than attname,
1251+
* since attname is limited to NAMEDATALEN-1 (63 chars).
1252+
*/
1253+
if (tle->resname && strlen(tle->resname) > strlen(NameStr(att->attname)))
1254+
appendStringInfoString(&col->colName, tle->resname);
1255+
else
1256+
{
1257+
appendStringInfoString(&col->colName, NameStr(att->attname));
1258+
1259+
/* For SELECT *, try attoptions for full original name */
1260+
if (col->colName.len >= NAMEDATALEN - 1 && tle->resorigtbl != InvalidOid && tle->resorigcol > 0)
1261+
{
1262+
HeapTuple atttup = SearchSysCache2(ATTNUM,
1263+
ObjectIdGetDatum(tle->resorigtbl),
1264+
Int16GetDatum(tle->resorigcol));
1265+
if (HeapTupleIsValid(atttup))
1266+
{
1267+
bool isnull;
1268+
Datum opts = SysCacheGetAttr(ATTNUM, atttup, Anum_pg_attribute_attoptions, &isnull);
1269+
if (!isnull)
1270+
{
1271+
ArrayType *arr = DatumGetArrayTypeP(opts);
1272+
Datum *elems;
1273+
int n;
1274+
deconstruct_array(arr, TEXTOID, -1, false, TYPALIGN_INT, &elems, NULL, &n);
1275+
for (int i = 0; i < n; i++)
1276+
{
1277+
char *s = text_to_cstring(DatumGetTextP(elems[i]));
1278+
if (strncmp(s, "bbf_original_name=", 18) == 0)
1279+
{
1280+
resetStringInfo(&col->colName);
1281+
appendStringInfoString(&col->colName, s + 18);
1282+
break;
1283+
}
1284+
}
1285+
}
1286+
ReleaseSysCache(atttup);
1287+
}
1288+
}
1289+
1290+
/* Fallback: for views, lookup pg_rewrite for full column name */
1291+
if (col->colName.len >= NAMEDATALEN - 1 && tle->resorigtbl != InvalidOid)
1292+
{
1293+
char relkind = get_rel_relkind(tle->resorigtbl);
1294+
if (relkind == RELKIND_VIEW)
1295+
{
1296+
Relation vrel = relation_open(tle->resorigtbl, AccessShareLock);
1297+
Query *vquery = get_view_query(vrel);
1298+
ListCell *vlc;
1299+
int vcolno = 0;
1300+
1301+
foreach(vlc, vquery->targetList)
1302+
{
1303+
TargetEntry *vtle = (TargetEntry *) lfirst(vlc);
1304+
if (vtle->resjunk)
1305+
continue;
1306+
vcolno++;
1307+
if (vcolno == tle->resorigcol && vtle->resname &&
1308+
strlen(vtle->resname) > (Size) col->colName.len)
1309+
{
1310+
resetStringInfo(&col->colName);
1311+
appendStringInfoString(&col->colName, vtle->resname);
1312+
break;
1313+
}
1314+
}
1315+
relation_close(vrel, AccessShareLock);
1316+
}
1317+
}
1318+
}
1319+
12471320
col->relOid = tle->resorigtbl;
12481321
col->attrNum = tle->resorigcol;
12491322

12501323
tlist_item = lnext(targetlist, tlist_item);
12511324
}
12521325
else
12531326
{
1327+
appendStringInfoString(&col->colName, NameStr(att->attname));
12541328
/* No info available, so send zeroes */
12551329
col->relOid = 0;
12561330
col->attrNum = 0;

contrib/babelfishpg_tsql/runtime/functions.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2855,7 +2855,32 @@ object_name(PG_FUNCTION_ARGS)
28552855
if (pg_class_aclcheck(object_id, user_id, ACL_SELECT) == ACLCHECK_OK)
28562856
{
28572857
Form_pg_class pg_class = (Form_pg_class) GETSTRUCT(tuple);
2858-
result_text = cstring_to_text(NameStr(pg_class->relname)); // make a copy before releasing syscache
2858+
char *relname = NameStr(pg_class->relname);
2859+
2860+
/* Try to get original name from reloptions */
2861+
if (strlen(relname) >= NAMEDATALEN - 1)
2862+
{
2863+
bool isnull;
2864+
Datum opts = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_reloptions, &isnull);
2865+
if (!isnull)
2866+
{
2867+
ArrayType *arr = DatumGetArrayTypeP(opts);
2868+
Datum *elems;
2869+
int n;
2870+
deconstruct_array(arr, TEXTOID, -1, false, TYPALIGN_INT, &elems, NULL, &n);
2871+
for (int i = 0; i < n; i++)
2872+
{
2873+
char *s = text_to_cstring(DatumGetTextP(elems[i]));
2874+
if (strncmp(s, "bbf_original_rel_name=", 22) == 0)
2875+
{
2876+
result_text = cstring_to_text(s + 22);
2877+
break;
2878+
}
2879+
}
2880+
}
2881+
}
2882+
if (result_text == NULL)
2883+
result_text = cstring_to_text(relname);
28592884
schema_id = pg_class->relnamespace;
28602885
}
28612886
ReleaseSysCache(tuple);

0 commit comments

Comments
 (0)