Skip to content

Commit e3983f6

Browse files
author
anju15bharti
committed
BABEL-5975: Handle long identifiers for Babelfish
Signed-off-by: anju15bharti <abanju@amazon.com>
1 parent b154696 commit e3983f6

9 files changed

Lines changed: 59 additions & 12 deletions

File tree

src/backend/access/nbtree/nbtinsert.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "postgres.h"
1717

1818
#include "access/nbtree.h"
19+
#include "executor/executor.h"
1920
#include "access/nbtxlog.h"
2021
#include "access/transam.h"
2122
#include "access/xloginsert.h"
@@ -663,14 +664,22 @@ _bt_check_unique(Relation rel, BTInsertState insertstate, Relation heapRel,
663664
key_desc = BuildIndexValueDescription(rel, values,
664665
isnull);
665666

666-
ereport(ERROR,
667-
(errcode(ERRCODE_UNIQUE_VIOLATION),
668-
errmsg("duplicate key value violates unique constraint \"%s\"",
669-
RelationGetRelationName(rel)),
670-
key_desc ? errdetail("Key %s already exists.",
671-
key_desc) : 0,
672-
errtableconstraint(heapRel,
673-
RelationGetRelationName(rel))));
667+
{
668+
const char *conname = RelationGetRelationName(rel);
669+
670+
if (bbf_get_original_index_name_hook)
671+
conname = bbf_get_original_index_name_hook(conname);
672+
if (bbf_get_original_constraint_name_hook && strlen(conname) >= NAMEDATALEN - 1)
673+
conname = bbf_get_original_constraint_name_hook(conname);
674+
ereport(ERROR,
675+
(errcode(ERRCODE_UNIQUE_VIOLATION),
676+
errmsg("duplicate key value violates unique constraint \"%s\"",
677+
conname),
678+
key_desc ? errdetail("Key %s already exists.",
679+
key_desc) : 0,
680+
errtableconstraint(heapRel,
681+
RelationGetRelationName(rel))));
682+
}
674683
}
675684
}
676685
else if (all_dead && (!inposting ||

src/backend/catalog/genbki.pl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
PROCNAMENSPSIGNATURE
6969
SYSNAMESPACENAME
7070
AUTHIDUSEREXTROLENAME
71+
IDENTMAPPINGNAME
7172
);
7273

7374
foreach my $header (@ARGV)

src/backend/commands/view.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "catalog/namespace.h"
2020
#include "commands/tablecmds.h"
2121
#include "commands/view.h"
22+
#include "parser/parser.h"
23+
#include "parser/scansup.h"
2224
#include "nodes/makefuncs.h"
2325
#include "nodes/nodeFuncs.h"
2426
#include "parser/analyze.h"
@@ -70,6 +72,12 @@ DefineVirtualRelation(RangeVar *relation, List *tlist, bool replace,
7072
exprTypmod((Node *) tle->expr),
7173
exprCollation((Node *) tle->expr));
7274

75+
/* For TSQL, MD5-truncate long column names for attname storage */
76+
if (sql_dialect == SQL_DIALECT_TSQL && def->colname &&
77+
strlen(def->colname) >= NAMEDATALEN)
78+
truncate_identifier(def->colname,
79+
strlen(def->colname), false);
80+
7381
if (inherit_view_constraints_from_table_hook)
7482
(*inherit_view_constraints_from_table_hook) (def, tle->resorigtbl, tle->resorigcol);
7583

src/backend/executor/execMain.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
#include "utils/rls.h"
6666
#include "utils/snapmgr.h"
6767

68+
bbf_get_original_constraint_name_hook_type bbf_get_original_constraint_name_hook = NULL;
69+
bbf_get_original_index_name_hook_type bbf_get_original_index_name_hook = NULL;
70+
6871

6972
/* Hooks for plugins to get control in ExecutorStart/Run/Finish/End */
7073
ExecutorStart_hook_type ExecutorStart_hook = NULL;
@@ -2096,12 +2099,17 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
20962099
tupdesc,
20972100
modifiedCols,
20982101
64);
2102+
{
2103+
const char *display_name = failed;
2104+
if (bbf_get_original_constraint_name_hook)
2105+
display_name = bbf_get_original_constraint_name_hook(failed);
20992106
ereport(ERROR,
21002107
(errcode(ERRCODE_CHECK_VIOLATION),
21012108
errmsg("new row for relation \"%s\" violates check constraint \"%s\"",
2102-
RelationGetRelationName(orig_rel), failed),
2109+
RelationGetRelationName(orig_rel), display_name),
21032110
val_desc ? errdetail("Failing row contains %s.", val_desc) : 0,
21042111
errtableconstraint(orig_rel, failed)));
2112+
}
21052113
}
21062114
}
21072115
}

src/backend/parser/parse_clause.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2115,7 +2115,7 @@ findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist,
21152115
((!tle_name_comparison_hook &&
21162116
strcmp(tle->resname, name) == 0) ||
21172117
(tle_name_comparison_hook &&
2118-
(*tle_name_comparison_hook)(tle->resname, name))))
2118+
(*tle_name_comparison_hook)(tle->resname, name, pstate->p_sourcetext, location))))
21192119
{
21202120
if (target_result != NULL)
21212121
{

src/backend/rewrite/rewriteDefine.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include "parser/parse_utilcmd.h"
2929
#include "rewrite/rewriteDefine.h"
3030
#include "rewrite/rewriteManip.h"
31+
#include "parser/parser.h"
32+
#include "parser/scansup.h"
3133
#include "rewrite/rewriteSupport.h"
3234
#include "utils/acl.h"
3335
#include "utils/builtins.h"
@@ -562,7 +564,11 @@ checkRuleResultList(List *targetList, TupleDesc resultDesc, bool isSelect,
562564
errmsg("cannot create a RETURNING list for a relation containing dropped columns")));
563565

564566
/* Check name match if required; no need for two error texts here */
565-
if (requireColumnNameMatch && strcmp(tle->resname, attname) != 0)
567+
if (requireColumnNameMatch && strcmp(tle->resname, attname) != 0 &&
568+
!(sql_dialect == SQL_DIALECT_TSQL &&
569+
strlen(tle->resname) >= NAMEDATALEN &&
570+
strlen(attname) < NAMEDATALEN &&
571+
pg_strcasecmp(downcase_truncate_identifier(tle->resname, strlen(tle->resname), false), attname) == 0))
566572
ereport(ERROR,
567573
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
568574
errmsg("SELECT rule's target entry %d has different column name from column \"%s\"",

src/bin/pg_dump/dump_babel_utils.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,7 @@ setBabelfishDependenciesForLogicalDatabaseDump(Archive *fout)
10561056
* sys.babelfish_namespace_ext
10571057
* sys.babelfish_extended_properties
10581058
* sys.babelfish_schema_permissions
1059+
* sys.babelfish_identifier_mapping
10591060
* sys.babelfish_partition_function
10601061
* sys.babelfish_partition_scheme
10611062
* sys.babelfish_partition_depend
@@ -1065,6 +1066,7 @@ setBabelfishDependenciesForLogicalDatabaseDump(Archive *fout)
10651066
"FROM pg_class "
10661067
"WHERE relname in ('babelfish_schema_permissions', "
10671068
"'babelfish_namespace_ext', "
1069+
"'babelfish_identifier_mapping', "
10681070
"'babelfish_partition_function', "
10691071
"'babelfish_partition_scheme', "
10701072
"'babelfish_partition_depend', "
@@ -1137,6 +1139,12 @@ addFromClauseForLogicalDatabaseDump(PQExpBuffer buf, TableInfo *tbinfo)
11371139
"ON a.nspname = b.nspname "
11381140
"WHERE b.dbid = %d",
11391141
fmtQualifiedDumpable(tbinfo), bbf_db_id);
1142+
else if (strcmp(tbinfo->dobj.name, "babelfish_identifier_mapping") == 0)
1143+
appendPQExpBuffer(buf, " FROM ONLY %s a "
1144+
"INNER JOIN sys.babelfish_namespace_ext b "
1145+
"ON a.nspname = b.nspname "
1146+
"WHERE b.dbid = %d",
1147+
fmtQualifiedDumpable(tbinfo), bbf_db_id);
11401148
else if(strcmp(tbinfo->dobj.name, "babelfish_authid_user_ext") == 0)
11411149
{
11421150
appendPQExpBuffer(buf, " FROM ONLY %s a "
@@ -1200,6 +1208,7 @@ addFromClauseForPhysicalDatabaseDump(PQExpBuffer buf, TableInfo *tbinfo)
12001208
fmtQualifiedDumpable(tbinfo), babel_init_user);
12011209
else if(strcmp(tbinfo->dobj.name, "babelfish_domain_mapping") == 0 ||
12021210
strcmp(tbinfo->dobj.name, "babelfish_function_ext") == 0 ||
1211+
strcmp(tbinfo->dobj.name, "babelfish_identifier_mapping") == 0 ||
12031212
strcmp(tbinfo->dobj.name, "babelfish_view_def") == 0 ||
12041213
strcmp(tbinfo->dobj.name, "babelfish_server_options") == 0 ||
12051214
strcmp(tbinfo->dobj.name, "babelfish_extended_properties") == 0 ||

src/include/executor/executor.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ extern PGDLLEXPORT TriggerRecuresiveCheck_hook_type TriggerRecuresiveCheck_hook;
101101
typedef bool (*bbfViewHasInsteadofTrigger_hook_type) (Relation view, CmdType event);
102102
extern PGDLLIMPORT bbfViewHasInsteadofTrigger_hook_type bbfViewHasInsteadofTrigger_hook;
103103

104+
typedef const char *(*bbf_get_original_constraint_name_hook_type)(const char *conname);
105+
extern PGDLLIMPORT bbf_get_original_constraint_name_hook_type bbf_get_original_constraint_name_hook;
106+
107+
typedef const char *(*bbf_get_original_index_name_hook_type)(const char *idxname);
108+
extern PGDLLIMPORT bbf_get_original_index_name_hook_type bbf_get_original_index_name_hook;
109+
104110
typedef Datum (*adjust_numeric_result_hook_type) (Plan *plan, Node *expr, Datum result, bool result_isnull, Oid result_type, int32 result_typmod);
105111
extern PGDLLIMPORT adjust_numeric_result_hook_type adjust_numeric_result_hook;
106112

src/include/parser/parse_clause.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extern List *addTargetToSortList(ParseState *pstate, TargetEntry *tle,
5151
extern Index assignSortGroupRef(TargetEntry *tle, List *tlist);
5252
extern bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList);
5353

54-
typedef bool (*tle_name_comparison_hook_type)(const char *tlename, const char *identifier);
54+
typedef bool (*tle_name_comparison_hook_type)(const char *tlename, const char *identifier, const char *sourcetext, int location);
5555
extern PGDLLEXPORT tle_name_comparison_hook_type tle_name_comparison_hook;
5656

5757
typedef void (*sortby_nulls_hook_type)(SortGroupClause *sortcl, bool reverse);

0 commit comments

Comments
 (0)