Skip to content

Commit a509bd4

Browse files
author
anju15bharti
committed
Store original name in reloption for temp table and index on it
1 parent 8872ab4 commit a509bd4

20 files changed

Lines changed: 2456 additions & 221 deletions

contrib/babelfishpg_tsql/runtime/functions.c

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,14 @@
5151
#include "utils/float.h"
5252
#include "utils/xid8.h"
5353
#include "utils/xml.h"
54+
#include "catalog/pg_class_d.h"
5455
#include <math.h>
5556

57+
extern const char *ATTOPTION_BBF_ORIGINAL_TABLE_NAME;
58+
extern char *get_value_by_name_from_array(ArrayType *array, const char *name);
59+
60+
static char *get_orig_temp_table_name(Oid relid);
61+
5662
#include "../src/babelfish_version.h"
5763
#include "../src/datatype_info.h"
5864
#include "../src/pltsql.h"
@@ -1319,7 +1325,19 @@ get_enr_list(PG_FUNCTION_ARGS)
13191325
MemSet(nulls, 0, sizeof(nulls));
13201326

13211327
values[0] = ((EphemeralNamedRelationMetadata) lfirst(lc))->reliddesc;
1322-
values[1] = CStringGetTextDatum(((EphemeralNamedRelationMetadata) lfirst(lc))->name);
1328+
{
1329+
EphemeralNamedRelationMetadata md = (EphemeralNamedRelationMetadata) lfirst(lc);
1330+
const char *name = md->name;
1331+
1332+
/* Use original untruncated name from reloptions if available */
1333+
if (md->enrtype == ENR_TSQL_TEMP)
1334+
{
1335+
char *orig = get_orig_temp_table_name(md->reliddesc);
1336+
if (orig)
1337+
name = orig;
1338+
}
1339+
values[1] = CStringGetTextDatum(name);
1340+
}
13231341

13241342
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
13251343
}
@@ -2798,6 +2816,32 @@ object_id(PG_FUNCTION_ARGS)
27982816
* if there is no such object in specified database, if database id is not provided it will lookup in current database
27992817
* if user don't have right permission
28002818
*/
2819+
/*
2820+
* get_orig_temp_table_name - Get original untruncated name from reloptions.
2821+
* Returns palloc'd string or NULL if not found.
2822+
*/
2823+
static char *
2824+
get_orig_temp_table_name(Oid relid)
2825+
{
2826+
HeapTuple tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
2827+
char *orig = NULL;
2828+
2829+
if (HeapTupleIsValid(tuple))
2830+
{
2831+
Datum datum;
2832+
bool isnull;
2833+
2834+
datum = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_reloptions, &isnull);
2835+
if (!isnull)
2836+
{
2837+
ArrayType *reloptions = DatumGetArrayTypeP(datum);
2838+
orig = get_value_by_name_from_array(reloptions, ATTOPTION_BBF_ORIGINAL_TABLE_NAME);
2839+
}
2840+
ReleaseSysCache(tuple);
2841+
}
2842+
return orig;
2843+
}
2844+
28012845
Datum
28022846
object_name(PG_FUNCTION_ARGS)
28032847
{
@@ -2844,7 +2888,12 @@ object_name(PG_FUNCTION_ARGS)
28442888
enr = GetENRTempTableWithOid(object_id, false);
28452889
if (enr != NULL && enr->md.enrtype == ENR_TSQL_TEMP)
28462890
{
2847-
PG_RETURN_VARCHAR_P((VarChar *) cstring_to_text(enr->md.name));
2891+
const char *name = enr->md.name;
2892+
char *orig = get_orig_temp_table_name(object_id);
2893+
2894+
if (orig)
2895+
name = orig;
2896+
PG_RETURN_VARCHAR_P((VarChar *) cstring_to_text(name));
28482897
}
28492898

28502899
/* search in pg_class by object_id */
@@ -2855,7 +2904,17 @@ object_name(PG_FUNCTION_ARGS)
28552904
if (pg_class_aclcheck(object_id, user_id, ACL_SELECT) == ACLCHECK_OK)
28562905
{
28572906
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
2907+
2908+
if (pg_class->relpersistence == RELPERSISTENCE_TEMP &&
2909+
NameStr(pg_class->relname)[0] == '#')
2910+
{
2911+
char *orig = get_orig_temp_table_name(object_id);
2912+
if (orig)
2913+
result_text = cstring_to_text(orig);
2914+
}
2915+
2916+
if (!result_text)
2917+
result_text = cstring_to_text(NameStr(pg_class->relname));
28592918
schema_id = pg_class->relnamespace;
28602919
}
28612920
ReleaseSysCache(tuple);

contrib/babelfishpg_tsql/src/backend_parser/gram-tsql-rule.y

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3557,6 +3557,11 @@ tsql_IndexStmt:
35573557
n->transformed = false;
35583558
n->if_not_exists = false;
35593559

3560+
if (n->idxname)
3561+
n->options = lappend(n->options,
3562+
makeDefElem("name_location",
3563+
(Node *) makeInteger(@7), @7));
3564+
35603565
tsql_index_nulls_order(n->indexParams, n->accessMethod);
35613566
$$ = (Node *)n;
35623567
}

contrib/babelfishpg_tsql/src/backend_parser/scan-tsql-rule.l

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@
153153
<tsql>{tsql_ttname} {
154154
SET_YYLLOC();
155155
yylval->str = pstrdup(yytext);
156+
if (yyleng >= NAMEDATALEN)
157+
truncate_identifier(yylval->str, yyleng, true);
156158
return IDENT;
157159
}
158160

contrib/babelfishpg_tsql/src/hooks.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2500,7 +2500,8 @@ pltsql_post_transform_table_definition(ParseState *pstate, RangeVar *relation, c
25002500
* Only store original_name if there's a difference, and if the difference
25012501
* is only in capitalization
25022502
*/
2503-
if (strncmp(relname, original_name, strlen(relname)) != 0 && strncasecmp(relname, original_name, strlen(relname)) == 0)
2503+
if ((strncmp(relname, original_name, strlen(relname)) != 0 && strncasecmp(relname, original_name, strlen(relname)) == 0) ||
2504+
(relation->relpersistence == RELPERSISTENCE_TEMP && original_name[0] == '#' && strlen(original_name) >= NAMEDATALEN))
25042505
{
25052506
/*
25062507
* add "ALTER TABLE SET (bbf_original_table_name=<original_name>)" to

contrib/babelfishpg_tsql/src/pl_handler.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5386,6 +5386,34 @@ bbf_ProcessUtility(PlannedStmt *pstmt,
53865386
List *partition_schemes = stmt->excludeOpNames;
53875387

53885388
stmt->excludeOpNames = NIL;
5389+
5390+
/*
5391+
* For indexes on temp tables, extract the full original
5392+
* name from the query string using name_location stored
5393+
* by the parser. Remove name_location from options as
5394+
* it is for internal use only.
5395+
*/
5396+
{
5397+
ListCell *lc;
5398+
foreach(lc, stmt->options)
5399+
{
5400+
DefElem *opt = (DefElem *) lfirst(lc);
5401+
if (strcmp(opt->defname, "name_location") == 0)
5402+
{
5403+
if (stmt->relation->relpersistence == RELPERSISTENCE_TEMP &&
5404+
stmt->relation->relname[0] == '#' && original_name)
5405+
{
5406+
int loc = intVal(opt->arg);
5407+
char *full_name = extract_identifier(queryString + loc, NULL);
5408+
if (full_name)
5409+
original_name = full_name;
5410+
}
5411+
stmt->options = foreach_delete_current(stmt->options, lc);
5412+
break;
5413+
}
5414+
}
5415+
}
5416+
53895417
if (stmt->idxname && !stmt->isconstraint)
53905418
stmt->idxname = construct_unique_index_name(stmt->idxname, stmt->relation->relname);
53915419
/*

0 commit comments

Comments
 (0)