Skip to content

Commit

Permalink
Added escape hatch for IDENTITY() in select into (babelfish-for-postg…
Browse files Browse the repository at this point in the history
…resql#2055)


Need this because identity column will always be the last one in table
if created using a select into statement

Task: BABEL-539
Signed-off-by: Deepakshi Mittal <[email protected]>
  • Loading branch information
deepakshi-mittal authored and Jason Teng committed Dec 21, 2023
1 parent 9b40680 commit 04dea31
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 6 deletions.
37 changes: 34 additions & 3 deletions contrib/babelfishpg_tsql/src/backend_parser/gram-tsql-rule.y
Original file line number Diff line number Diff line change
Expand Up @@ -2017,15 +2017,46 @@ func_expr_common_subexpr:
}
| IDENTITY_P '(' Typename ',' a_expr ',' a_expr ')'
{
$$ = TsqlFunctionIdentityInto($3, $5, $7, @1);
if (escape_hatch_identity_function)
{
$$ = TsqlFunctionIdentityInto($3, $5, $7, @1);
}
else
{
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("IDENTITY() is not currently supported in Babelfish. please use babelfishpg_tsql.escape_hatch_identity_function to ignore"),
parser_errposition(@1)));
}
}
| IDENTITY_P '(' Typename ',' a_expr ')'
{
$$ = TsqlFunctionIdentityInto($3, $5, (Node *)makeIntConst(1, -1), @1);
if (escape_hatch_identity_function)
{
$$ = TsqlFunctionIdentityInto($3, $5, (Node *)makeIntConst(1, -1), @1);
}
else
{
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("IDENTITY() is not currently supported in Babelfish. please use babelfishpg_tsql.escape_hatch_identity_function to ignore"),
parser_errposition(@1)));
}

}
| IDENTITY_P '(' Typename ')'
{
$$ = TsqlFunctionIdentityInto($3, (Node *)makeIntConst(1, -1), (Node *)makeIntConst(1, -1), @1);
if (escape_hatch_identity_function)
{
$$ = TsqlFunctionIdentityInto($3, (Node *)makeIntConst(1, -1), (Node *)makeIntConst(1, -1), @1);
}
else
{
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("IDENTITY() is not currently supported in Babelfish. please use babelfishpg_tsql.escape_hatch_identity_function to ignore"),
parser_errposition(@1)));
}
}
| TSQL_CONTAINS '(' ColId ',' tsql_contains_search_condition ')'
{
Expand Down
12 changes: 12 additions & 0 deletions contrib/babelfishpg_tsql/src/guc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,7 @@ int escape_hatch_checkpoint = EH_IGNORE;
int escape_hatch_set_transaction_isolation_level = EH_STRICT;
int pltsql_isolation_level_repeatable_read = ISOLATION_OFF;
int pltsql_isolation_level_serializable = ISOLATION_OFF;
int escape_hatch_identity_function = EH_STRICT;

void
define_escape_hatch_variables(void)
Expand Down Expand Up @@ -1601,6 +1602,17 @@ define_escape_hatch_variables(void)
PGC_USERSET,
GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE | GUC_DISALLOW_IN_AUTO_FILE,
NULL, NULL, NULL);

/* IDENTITY() in SELECT-INTO */
DefineCustomEnumVariable("babelfishpg_tsql.escape_hatch_identity_function",
gettext_noop("escape hatch for IDENTITY() in SELECT-INTO"),
NULL,
&escape_hatch_identity_function,
EH_STRICT,
escape_hatch_options,
PGC_USERSET,
GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE | GUC_DISALLOW_IN_AUTO_FILE,
NULL, NULL, NULL);
}

void
Expand Down
1 change: 1 addition & 0 deletions contrib/babelfishpg_tsql/src/guc.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extern bool pltsql_allow_fulltext_parser;
extern char *pltsql_psql_logical_babelfish_db_name;
extern int pltsql_isolation_level_repeatable_read;
extern int pltsql_isolation_level_serializable;
extern int escape_hatch_identity_function;

extern void define_custom_variables(void);
extern void pltsql_validate_set_config_function(char *name, char *value);
Expand Down
3 changes: 3 additions & 0 deletions test/JDBC/expected/BABEL-UNSUPPORTED.out
Original file line number Diff line number Diff line change
Expand Up @@ -2028,6 +2028,7 @@ babelfishpg_tsql.escape_hatch_constraint_name_for_default#!#ignore#!#escape hatc
babelfishpg_tsql.escape_hatch_database_misc_options#!#ignore#!#escape hatch for misc options in CREATE/ALTER DATABASE
babelfishpg_tsql.escape_hatch_for_replication#!#strict#!#escape hatch for (NOT) FOR REPLICATION option
babelfishpg_tsql.escape_hatch_fulltext#!#strict#!#escape hatch for fulltext search
babelfishpg_tsql.escape_hatch_identity_function#!#strict#!#escape hatch for IDENTITY() in SELECT-INTO
babelfishpg_tsql.escape_hatch_ignore_dup_key#!#strict#!#escape hatch for ignore_dup_key=on option in CREATE/ALTER TABLE/INDEX
babelfishpg_tsql.escape_hatch_index_clustering#!#ignore#!#escape hatch for CLUSTERED option in CREATE INDEX
babelfishpg_tsql.escape_hatch_index_columnstore#!#strict#!#escape hatch for COLUMNSTORE option in CREATE INDEX
Expand Down Expand Up @@ -2096,6 +2097,7 @@ babelfishpg_tsql.escape_hatch_constraint_name_for_default#!#ignore#!#escape hatc
babelfishpg_tsql.escape_hatch_database_misc_options#!#ignore#!#escape hatch for misc options in CREATE/ALTER DATABASE
babelfishpg_tsql.escape_hatch_for_replication#!#strict#!#escape hatch for (NOT) FOR REPLICATION option
babelfishpg_tsql.escape_hatch_fulltext#!#strict#!#escape hatch for fulltext search
babelfishpg_tsql.escape_hatch_identity_function#!#strict#!#escape hatch for IDENTITY() in SELECT-INTO
babelfishpg_tsql.escape_hatch_ignore_dup_key#!#strict#!#escape hatch for ignore_dup_key=on option in CREATE/ALTER TABLE/INDEX
babelfishpg_tsql.escape_hatch_index_clustering#!#ignore#!#escape hatch for CLUSTERED option in CREATE INDEX
babelfishpg_tsql.escape_hatch_index_columnstore#!#strict#!#escape hatch for COLUMNSTORE option in CREATE INDEX
Expand Down Expand Up @@ -2145,6 +2147,7 @@ babelfishpg_tsql.escape_hatch_constraint_name_for_default#!#ignore#!#escape hatc
babelfishpg_tsql.escape_hatch_database_misc_options#!#ignore#!#escape hatch for misc options in CREATE/ALTER DATABASE
babelfishpg_tsql.escape_hatch_for_replication#!#strict#!#escape hatch for (NOT) FOR REPLICATION option
babelfishpg_tsql.escape_hatch_fulltext#!#strict#!#escape hatch for fulltext search
babelfishpg_tsql.escape_hatch_identity_function#!#strict#!#escape hatch for IDENTITY() in SELECT-INTO
babelfishpg_tsql.escape_hatch_ignore_dup_key#!#strict#!#escape hatch for ignore_dup_key=on option in CREATE/ALTER TABLE/INDEX
babelfishpg_tsql.escape_hatch_index_clustering#!#ignore#!#escape hatch for CLUSTERED option in CREATE INDEX
babelfishpg_tsql.escape_hatch_index_columnstore#!#strict#!#escape hatch for COLUMNSTORE option in CREATE INDEX
Expand Down
21 changes: 21 additions & 0 deletions test/JDBC/expected/BABEL_539-vu-verify.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
EXEC babel_539_prepare_proc
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: IDENTITY() is not currently supported in Babelfish. please use babelfishpg_tsql.escape_hatch_identity_function to ignore)~~


SELECT current_setting('babelfishpg_tsql.escape_hatch_identity_function');
GO
~~START~~
text
strict
~~END~~


EXEC sp_babelfish_configure 'babelfishpg_tsql.escape_hatch_identity_function', 'ignore';
GO

EXEC babel_539_prepare_proc
GO

Expand Down Expand Up @@ -459,3 +477,6 @@ GO

~~ERROR (Message: function IDENTITY_INTO_BIGINT does not exist)~~


EXEC sp_babelfish_configure 'babelfishpg_tsql.escape_hatch_identity_function', 'strict';
GO
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ SELECT * FROM sys_babelfish_configurations_view_vu_prepare_view
GO
~~START~~
int
42
43
~~END~~


EXEC sys_babelfish_configurations_view_vu_prepare_proc
GO
~~START~~
int
42
43
~~END~~


SELECT * FROM sys_babelfish_configurations_view_vu_prepare_func()
GO
~~START~~
int
42
43
~~END~~


Expand Down
12 changes: 12 additions & 0 deletions test/JDBC/input/BABEL_539-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
EXEC babel_539_prepare_proc
GO

SELECT current_setting('babelfishpg_tsql.escape_hatch_identity_function');
GO

EXEC sp_babelfish_configure 'babelfishpg_tsql.escape_hatch_identity_function', 'ignore';
GO

EXEC babel_539_prepare_proc
GO

SELECT id_num, col1, name FROM babel_539NewTable_proc ORDER BY col1;
GO

Expand Down Expand Up @@ -262,4 +271,7 @@ SELECT IDENTITY(int, 21);
GO

SELECT sys.IDENTITY_INTO_BIGINT(20, 1, 1);
GO

EXEC sp_babelfish_configure 'babelfishpg_tsql.escape_hatch_identity_function', 'strict';
GO

0 comments on commit 04dea31

Please sign in to comment.