Skip to content

Commit

Permalink
Revert "Ignore extra COLLATE default which can come as part of defaul…
Browse files Browse the repository at this point in the history
…t value of column and ignore explicit COLLATE default on view def in dump (yugabyte#70)" (yugabyte#97)

This reverts commit e16b166a9c40cdac44ce9cbba685dacce0443e25. This commit reverts short term fix made for  BABEL-3801 via babelfish-for-postgresql/postgresql_modified_for_babelfish#70 except the change related to data  type _CI_SYSNAME as it would be required for long term fix. 

Long term fix for BABEL-3801 is introduced in BABEL_2_X_DEV__PG_14_X through engine PR: babelfish-for-postgresql/postgresql_modified_for_babelfish#96 and extension PR: babelfish-for-postgresql/babelfish_extensions#1222

Task: BABEL-3895
Signed-off-by: Dipesh Dhameliya <[email protected]>
  • Loading branch information
Deepesh125 authored and abhinab-yb committed Nov 14, 2024
1 parent 21e41fb commit daab9a4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 114 deletions.
118 changes: 11 additions & 107 deletions src/postgres/src/bin/pg_dump/dump_babel_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,45 +124,6 @@ bbf_selectDumpableCast(CastInfo *cast)
cast->dobj.dump = DUMP_COMPONENT_NONE;
}

static char *
babelfishRemoveSubstr(char *src, const char *substr)
{
char *str;
char *result = pstrdup("");
int substr_len = strlen(substr);

if (!strstr(src, substr))
return src;

while ((str = strstr(src, substr)) != NULL)
{
char *tmp = psprintf("%s%s", result, pnstrdup(src, str - src));
if (result)
{
pfree(result);
result = tmp;
}

/* skip substr part */
src = str + substr_len;

if (!src)
break;
}

if (src)
{
char *tmp = psprintf("%s%s", result, src);
if (result)
{
pfree(result);
result = tmp;
}
}

return result;
}

/*
* T-SQL allows an empty/space-only string as a default constraint of
* NUMERIC column in CREATE TABLE statement. However, it will eventually
Expand All @@ -178,64 +139,24 @@ babelfishRemoveSubstr(char *src, const char *substr)
void
fixTsqlDefaultExpr(Archive *fout, AttrDefInfo *attrDefInfo)
{
char *source = attrDefInfo->adef_expr;
char *runtimeErrFunc = "babelfish_runtime_error";
char *runtimeErrStr = "'An empty or space-only string cannot be converted into numeric/decimal data type'";
char *atttypname;
char *adef_expr;
const char *defaultCollation = "COLLATE \"default\"";

if (!isBabelfishDatabase(fout) || attrDefInfo->adnum < 1)
if (!isBabelfishDatabase(fout) ||
!strstr(source, runtimeErrStr) ||
strstr(source, runtimeErrFunc) ||
attrDefInfo->adnum < 1)
return;

/* Handle default value for "numeric" and "decimal") */
atttypname = attrDefInfo->adtable->atttypnames[attrDefInfo->adnum - 1];
if (strstr(atttypname, "decimal") || strstr(atttypname, "numeric"))
{
char *runtimeErrFunc = "babelfish_runtime_error";
char *runtimeErrStr = "'An empty or space-only string cannot be converted into numeric/decimal data type'";
adef_expr = attrDefInfo->adef_expr;

if (!strstr(adef_expr, runtimeErrStr) || strstr(adef_expr, runtimeErrFunc))
return;

/* Replace the default expr to runtime error function */
free(adef_expr);
attrDefInfo->adef_expr = psprintf("(sys.%s(%s::text))::integer", runtimeErrFunc, runtimeErrStr);
return;
}

/*
* Remove COLLATE "default" from the end for default value clause of the column because
* it does not matter if we specify such clause for default value and column's collation
* will be used at the end.
*/
adef_expr = babelfishRemoveSubstr(attrDefInfo->adef_expr, defaultCollation);

if (adef_expr != attrDefInfo->adef_expr)
{
free(attrDefInfo->adef_expr);
attrDefInfo->adef_expr = adef_expr;
}
}

/*
* fixTsqlCheckConstraint - In upgraded Babelfish database from v2.1.0/2.2.0 to v2.3.0 may contain extra
* COLLATE "default" clause around check constraint which is kind of redundant in T-SQL objects.
* So this helper function takes care of it during dump and restore.
*/
void
fixTsqlCheckConstraint(Archive *fout, ConstraintInfo *constrs)
{
const char *defaultCollation = "COLLATE \"default\"";
char *condef;

if (!isBabelfishDatabase(fout))
if (!strstr(atttypname, "decimal") && !strstr(atttypname, "numeric"))
return;

condef = babelfishRemoveSubstr(constrs->condef, defaultCollation);
if (condef != constrs->condef)
{
pfree(constrs->condef);
constrs->condef = condef;
}
/* Replace the default expr to runtime error function */
free(source);
attrDefInfo->adef_expr = psprintf("(sys.%s(%s::text))::integer", runtimeErrFunc, runtimeErrStr);
}

/*
Expand Down Expand Up @@ -563,20 +484,3 @@ dumpBabelfishSpecificConfig(Archive *AH, const char *dbname, PQExpBuffer outbuf)
pfree(current_server_collation_name);
}
}

/*
* babelfish_handle_view_def - Any upgraded Babelfish database from 2.1.0/2.2.0 to 2.3.0
* may dump extra COLLATE "default" clause around const clause inside view definition which
* is kind of unnecessary for T-SQL objects as well as for sys views. So this function removes
* such clause from view definition.
*/
char *
babelfish_handle_view_def(Archive *fout, char *view_def)
{
const char *defaultCollation = "COLLATE \"default\"";

if (!isBabelfishDatabase(fout))
return view_def;

return babelfishRemoveSubstr(view_def, defaultCollation);
}
2 changes: 0 additions & 2 deletions src/postgres/src/bin/pg_dump/dump_babel_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
extern char *getMinOid(Archive *fout);
extern void bbf_selectDumpableCast(CastInfo *cast);
extern void fixTsqlDefaultExpr(Archive *fout, AttrDefInfo *attrDefInfo);
extern void fixTsqlCheckConstraint(Archive *fout, ConstraintInfo *constrs);
extern bool isBabelfishDatabase(Archive *fout);
extern void fixOprRegProc(Archive *fout, const OprInfo *oprinfo, const char *oprleft, const char *oprright, char **oprregproc);
extern void fixTsqlTableTypeDependency(Archive *fout, DumpableObject *func, DumpableObject *tabletype, char deptype);
Expand All @@ -33,6 +32,5 @@ extern int getTsqlTvfType(Archive *fout, const FuncInfo *finfo, char prokind, bo
extern void fixAttoptionsBbfOriginalName(Archive *fout, Oid relOid, const TableInfo *tbinfo, int idx);
extern void setOrResetPltsqlFuncRestoreGUCs(Archive *fout, PQExpBuffer q, const FuncInfo *finfo, char prokind, bool proretset, bool is_set);
extern void dumpBabelfishSpecificConfig(Archive *AH, const char *dbname, PQExpBuffer outbuf);
extern char *babelfish_handle_view_def(Archive *fout, char *view_def);

#endif
6 changes: 1 addition & 5 deletions src/postgres/src/bin/pg_dump/pg_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -8875,9 +8875,6 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
constrs[j].condeferred = false;
constrs[j].conislocal = (PQgetvalue(res, j, i_conislocal)[0] == 't');

/* Babelfish-specific logic for check constraint */
fixTsqlCheckConstraint(fout, &constrs[j]);

/*
* An unvalidated constraint needs to be dumped separately, so
* that potentially-violating existing data is loaded before
Expand Down Expand Up @@ -15384,8 +15381,7 @@ createViewAsClause(Archive *fout, const TableInfo *tbinfo)

/* Strip off the trailing semicolon so that other things may follow. */
Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');

appendBinaryPQExpBuffer(result, babelfish_handle_view_def(fout, PQgetvalue(res, 0, 0)), len - 1);
appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);

PQclear(res);
destroyPQExpBuffer(query);
Expand Down

0 comments on commit daab9a4

Please sign in to comment.