Skip to content

Commit

Permalink
Merge branch 'babelfish-for-postgresql:BABEL_4_X_DEV' into babel_4939
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavJ23 authored Jan 17, 2025
2 parents 7e5d728 + ac5c94a commit 1bcc0d9
Show file tree
Hide file tree
Showing 55 changed files with 1,131 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,20 @@ ALTER OPERATOR FAMILY fixeddecimal_ops USING hash ADD
CREATE DOMAIN sys.MONEY as sys.FIXEDDECIMAL CHECK (VALUE >= -922337203685477.5808 AND VALUE <= 922337203685477.5807);
CREATE DOMAIN sys.SMALLMONEY as sys.FIXEDDECIMAL CHECK (VALUE >= -214748.3648 AND VALUE <= 214748.3647);

-- Define modulo operator directly on MONEY type.
-- Otherwise the operator between Integer and SMALLMONEY will tend to choose the fixeddecimal version,
-- which will return the result in MONEY type.
CREATE FUNCTION sys.fixeddecimalmod(sys.MONEY, sys.MONEY)
RETURNS sys.MONEY
AS 'babelfishpg_money', 'fixeddecimalmod'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE OPERATOR sys.% (
LEFTARG = sys.MONEY,
RIGHTARG = sys.MONEY,
PROCEDURE = fixeddecimalmod
);

--
-- Cross type operators with int8
--
Expand Down Expand Up @@ -1645,6 +1659,11 @@ RETURNS sys.SMALLMONEY
AS 'babelfishpg_money', 'fixeddecimaldiv'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE FUNCTION sys.fixeddecimalmod(sys.SMALLMONEY, sys.SMALLMONEY)
RETURNS sys.SMALLMONEY
AS 'babelfishpg_money', 'fixeddecimalmod'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE OPERATOR sys.+ (
LEFTARG = sys.SMALLMONEY,
RIGHTARG = sys.SMALLMONEY,
Expand Down Expand Up @@ -1676,6 +1695,12 @@ CREATE OPERATOR sys./ (
PROCEDURE = fixeddecimaldiv
);

CREATE OPERATOR sys.% (
LEFTARG = sys.SMALLMONEY,
RIGHTARG = sys.SMALLMONEY,
PROCEDURE = fixeddecimalmod
);

CREATE FUNCTION sys.fixeddecimalint8pl(sys.SMALLMONEY, INT8)
RETURNS sys.SMALLMONEY
AS 'babelfishpg_money', 'fixeddecimalint8pl'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,37 @@ EXCEPTION WHEN duplicate_object THEN
END;
$$;

CREATE OR REPLACE FUNCTION sys.fixeddecimalmod(sys.MONEY, sys.MONEY)
RETURNS sys.MONEY
AS 'babelfishpg_money', 'fixeddecimalmod'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_operator WHERE oprleft = 'sys.money'::pg_catalog.regtype and oprright = 'sys.money'::pg_catalog.regtype and oprnamespace = 'sys'::regnamespace and oprname = '%' and oprresult != 0) THEN
CREATE OPERATOR sys.% (
LEFTARG = sys.MONEY,
RIGHTARG = sys.MONEY,
PROCEDURE = fixeddecimalmod
);
END IF;
END $$;

CREATE OR REPLACE FUNCTION sys.fixeddecimalmod(sys.SMALLMONEY, sys.SMALLMONEY)
RETURNS sys.SMALLMONEY
AS 'babelfishpg_money', 'fixeddecimalmod'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_operator WHERE oprleft = 'sys.smallmoney'::pg_catalog.regtype and oprright = 'sys.smallmoney'::pg_catalog.regtype and oprnamespace = 'sys'::regnamespace and oprname = '%' and oprresult != 0) THEN
CREATE OPERATOR sys.% (
LEFTARG = sys.SMALLMONEY,
RIGHTARG = sys.SMALLMONEY,
PROCEDURE = fixeddecimalmod
);
END IF;
END $$;

-- Reset search_path to not affect any subsequent scripts
SELECT set_config('search_path', trim(leading 'sys, ' from current_setting('search_path')), false);
27 changes: 27 additions & 0 deletions contrib/babelfishpg_money/fixeddecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ PG_FUNCTION_INFO_V1(fixeddecimalpl);
PG_FUNCTION_INFO_V1(fixeddecimalmi);
PG_FUNCTION_INFO_V1(fixeddecimalmul);
PG_FUNCTION_INFO_V1(fixeddecimaldiv);
PG_FUNCTION_INFO_V1(fixeddecimalmod);
PG_FUNCTION_INFO_V1(fixeddecimalabs);
PG_FUNCTION_INFO_V1(fixeddecimallarger);
PG_FUNCTION_INFO_V1(fixeddecimalsmaller);
Expand Down Expand Up @@ -1772,6 +1773,32 @@ fixeddecimaldiv(PG_FUNCTION_ARGS)
PG_RETURN_INT64((int64) result);
}

Datum
fixeddecimalmod(PG_FUNCTION_ARGS)
{
int64 dividend = PG_GETARG_INT64(0);
int64 divisor = PG_GETARG_INT64(1);

if (divisor == 0)
{
ereport(ERROR,
(errcode(ERRCODE_DIVISION_BY_ZERO),
errmsg("division by zero")));
/* ensure compiler realizes we mustn't reach the division (gcc bug) */
PG_RETURN_NULL();
}

/*
» * Some machines throw a floating-point exception for INT_MIN % -1, which
» * is a bit silly since the correct answer is perfectly well-defined,
» * namely zero. Refer to function int4mod in Postgres.
» */
if (divisor == -1)
PG_RETURN_INT64(0);

PG_RETURN_INT64(dividend % divisor);
}

/* fixeddecimalabs()
* Absolute value
*/
Expand Down
10 changes: 5 additions & 5 deletions contrib/babelfishpg_tds/src/backend/tds/tdsutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -971,9 +971,9 @@ is_babelfish_role(const char *role)
if (OidIsValid(bbf_master_guest_oid)
&& OidIsValid(bbf_tempdb_guest_oid)
&& OidIsValid(bbf_msdb_guest_oid)
&& is_member_of_role(role_oid, bbf_master_guest_oid)
&& is_member_of_role(role_oid, bbf_tempdb_guest_oid)
&& is_member_of_role(role_oid, bbf_msdb_guest_oid))
&& (is_member_of_role(role_oid, bbf_master_guest_oid)
|| is_member_of_role(role_oid, bbf_tempdb_guest_oid)
|| is_member_of_role(role_oid, bbf_msdb_guest_oid)))
return true;

return false;
Expand Down Expand Up @@ -1242,7 +1242,7 @@ handle_grant_role(GrantRoleStmt *grant_stmt)
continue;

roleid = get_role_oid(rolename, false);
if (OidIsValid(roleid) && IS_DEFAULT_BBF_SERVER_ROLE(rolename))
if (OidIsValid(roleid) && is_babelfish_role(rolename))
check_babelfish_alterrole_restictions(false);
}

Expand All @@ -1254,7 +1254,7 @@ handle_grant_role(GrantRoleStmt *grant_stmt)
Oid roleid;

roleid = get_rolespec_oid(rolespec, false);
if (OidIsValid(roleid) && IS_DEFAULT_BBF_SERVER_ROLE(rolespec->rolename))
if (OidIsValid(roleid) && is_babelfish_role(rolespec->rolename))
check_babelfish_alterrole_restictions(false);
}

Expand Down
6 changes: 0 additions & 6 deletions contrib/babelfishpg_tds/src/include/tds_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,6 @@ extern ProcessUtility_hook_type next_ProcessUtility;
#define BABELFISH_SECURITYADMIN "securityadmin"
#define BABELFISH_DBCREATOR "dbcreator"

#define IS_DEFAULT_BBF_SERVER_ROLE(rolename) \
((strlen(rolename) == 13 && strncmp(rolename, BABELFISH_SECURITYADMIN, 13) == 0) || \
(strlen(rolename) == 14 && strncmp(rolename, BABELFISH_ROLE_ADMIN, 14) == 0) || \
(strlen(rolename) == 9 && strncmp(rolename, BABELFISH_DBCREATOR, 9) == 0) || \
(strlen(rolename) == 8 && strncmp(rolename, BABELFISH_SYSADMIN, 8) == 0))

/* Functions in backend/tds/tdscomm.c */
extern void TdsSetMessageType(uint8_t msgType);
extern void TdsCommInit(uint32_t bufferSize,
Expand Down
36 changes: 28 additions & 8 deletions contrib/babelfishpg_tsql/src/tsqlIface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2936,34 +2936,54 @@ class tsqlBuilder : public tsqlCommonMutator
clear_rewritten_query_fragment();
}

PLtsql_expr *rewrite_if_condition(TSqlParser::Search_conditionContext *ctx)
PLtsql_expr *rewrite_if_condition(TSqlParser::Search_conditionContext *ctx, PLtsql_expr *expr)
{
PLtsql_expr *expr = makeTsqlExpr(ctx, false);
PLtsql_expr_query_mutator mutator(expr, ctx);
add_rewritten_query_fragment_to_mutator(&mutator);
mutator.run();
if (!expr)
expr = makeTsqlExpr(ctx, false);
if (statementMutator)
{
add_rewritten_query_fragment_to_mutator(statementMutator.get());
statementMutator->run();
statementMutator = nullptr;
}
else
{
PLtsql_expr_query_mutator mutator(expr, ctx);
add_rewritten_query_fragment_to_mutator(&mutator);
mutator.run();
}
clear_rewritten_query_fragment();

/* Now we can prepend SELECT to rewritten search_condition */
expr->query = strdup((std::string("SELECT ") + std::string(expr->query)).c_str());
return expr;
}

void enterSearch_condition(TSqlParser::Search_conditionContext *ctx) override
{
if (((TSqlParser::Cfl_statementContext *) ctx->parent->parent)->if_statement())
{
PLtsql_stmt_if *fragment = (PLtsql_stmt_if *) getPLtsql_fragment(ctx->parent->parent);
fragment->cond = makeTsqlExpr(ctx, false);
clear_rewritten_query_fragment();
statementMutator = std::make_unique<PLtsql_expr_query_mutator>(fragment->cond, ctx);
}
}

void exitSearch_condition(TSqlParser::Search_conditionContext *ctx) override
{
if (!ctx->parent || !ctx->parent->parent)
return;

if (((TSqlParser::Cfl_statementContext *) ctx->parent->parent)->if_statement())
{

PLtsql_stmt_if *fragment = (PLtsql_stmt_if *) getPLtsql_fragment(ctx->parent->parent);
fragment->cond = rewrite_if_condition(ctx);
fragment->cond = rewrite_if_condition(ctx, fragment->cond);
}
else if (((TSqlParser::Cfl_statementContext *) ctx->parent->parent)->while_statement())
{
PLtsql_stmt_while *fragment = (PLtsql_stmt_while *) getPLtsql_fragment(ctx->parent->parent);
fragment->cond = rewrite_if_condition(ctx);
fragment->cond = rewrite_if_condition(ctx, fragment->cond);
}
}
};
Expand Down
11 changes: 11 additions & 0 deletions test/JDBC/expected/BABEL-2514.out
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,14 @@ int#!#int

drop view if exists babel_2514_complex_view;
go

IF EXISTS
(
SELECT tblnam = a.name FROM sysobjects a where a.name = 'tablename'
) select 'Exists'; ELSE select 'Not Exists';
GO
~~START~~
varchar
Not Exists
~~END~~

Loading

0 comments on commit 1bcc0d9

Please sign in to comment.