Skip to content

Commit

Permalink
binary and varbinary should be of default length 1 for local var
Browse files Browse the repository at this point in the history
Signed-off-by: Tanzeel Khan <[email protected]>
  • Loading branch information
tanscorpio7 committed Nov 30, 2023
1 parent 83cdbf3 commit ecd616d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
9 changes: 0 additions & 9 deletions contrib/babelfishpg_common/sql/binary.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,6 @@ WITHOUT FUNCTION AS IMPLICIT;
CREATE CAST (sys.BBF_VARBINARY AS sys.BBF_BINARY)
WITHOUT FUNCTION AS IMPLICIT;

-- typmod for sys.BBF_BINARY
CREATE OR REPLACE FUNCTION sys.bbf_binary (sys.BBF_BINARY, integer, boolean)
RETURNS sys.bbf_BINARY
AS 'babelfishpg_common', 'binary'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE CAST (sys.BBF_BINARY AS sys.BBF_BINARY)
WITH FUNCTION sys.bbf_binary (sys.BBF_BINARY, integer, BOOLEAN) AS ASSIGNMENT;

-- casting functions for sys.BINARY
CREATE OR REPLACE FUNCTION sys.varcharbinary(sys.VARCHAR, integer, boolean)
RETURNS sys.BBF_BINARY
Expand Down
8 changes: 8 additions & 0 deletions contrib/babelfishpg_tsql/src/pltsql_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ bool is_tsql_any_char_datatype(Oid oid); /* sys.char / sys.nchar /
* sys.varchar / sys.nvarchar */
bool is_tsql_text_ntext_or_image_datatype(Oid oid);

bool is_tsql_binary_or_varbinary_datatype(Oid oid);

bool
pltsql_createFunction(ParseState *pstate, PlannedStmt *pstmt, const char *queryString, ProcessUtilityContext context,
ParamListInfo params);
Expand Down Expand Up @@ -1082,6 +1084,12 @@ is_tsql_text_ntext_or_image_datatype(Oid oid)
(*common_utility_plugin_ptr->is_tsql_image_datatype) (oid);
}

bool is_tsql_binary_or_varbinary_datatype(Oid oid)
{
return (*common_utility_plugin_ptr->is_tsql_sys_binary_datatype) (oid) ||
(*common_utility_plugin_ptr->is_tsql_sys_varbinary_datatype) (oid);
}

/*
* Try to acquire a lock with no wait
*/
Expand Down
7 changes: 5 additions & 2 deletions contrib/babelfishpg_tsql/src/tsqlIface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ extern "C"
extern PLtsql_type *parse_datatype(const char *string, int location);
extern bool is_tsql_any_char_datatype(Oid oid);
extern bool is_tsql_text_ntext_or_image_datatype(Oid oid);
extern bool is_tsql_binary_or_varbinary_datatype(Oid oid);

extern int CurrentLineNumber;

Expand Down Expand Up @@ -4558,7 +4559,8 @@ makeDeclareStmt(TSqlParser::Declare_statementContext *ctx, std::map<PLtsql_stmt
const char *name = downcase_truncate_identifier(nameStr.c_str(), nameStr.length(), true);
check_dup_declare(name);
PLtsql_type *type = parse_datatype(typeStr.c_str(), 0);
if (type->atttypmod == -1 && is_tsql_any_char_datatype(type->typoid))
if (type->atttypmod == -1 && (is_tsql_any_char_datatype(type->typoid)
|| is_tsql_binary_or_varbinary_datatype(type->typoid)))
{
std::string newTypeStr = typeStr + "(1)"; /* in T-SQL, length-less (N)(VAR)CHAR's length is treated as 1 */
type = parse_datatype(newTypeStr.c_str(), 0);
Expand Down Expand Up @@ -4589,7 +4591,8 @@ makeDeclareStmt(TSqlParser::Declare_statementContext *ctx, std::map<PLtsql_stmt
{
std::string typeStr = ::getFullText(local->data_type());
PLtsql_type *type = parse_datatype(typeStr.c_str(), 0); // FIXME: the second arg should be 'location'
if (type->atttypmod == -1 && is_tsql_any_char_datatype(type->typoid))
if (type->atttypmod == -1 && (is_tsql_any_char_datatype(type->typoid)
|| is_tsql_binary_or_varbinary_datatype(type->typoid)))
{
std::string newTypeStr = typeStr + "(1)"; /* in T-SQL, length-less (N)(VAR)CHAR's length is treated as 1 */
type = parse_datatype(newTypeStr.c_str(), 0);
Expand Down
28 changes: 22 additions & 6 deletions test/JDBC/expected/BABEL_4544.out
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,34 @@ binary#!#int
DROP TABLE babel_4544_t
GO

-- default length should be 1
DECLARE @A VARBINARY = 0x0123456789012345678901234567890123456789
SELECT @A, DATALENGTH(@A)
SELECT DATALENGTH(CAST(@A as BINARY(50))), CAST(@A as BINARY(50)), CAST(@A as VARBINARY(60))
GO
~~START~~
varbinary#!#int
01#!#1
~~END~~

~~START~~
int#!#binary#!#varbinary
50#!#0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000#!#01
~~END~~


DECLARE @A BINARY = 0x0123456789012345678901234567890123456789
SELECT @A, DATALENGTH(@A)
SELECT DATALENGTH(CAST(@A as BINARY(50))), CAST(@A as BINARY(50))
GO
~~START~~
binary#!#int
01#!#20
01#!#1
~~END~~

~~START~~
int#!#binary
50#!#0123456789012345678901234567890123456789000000000000000000000000000000000000000000000000000000000000
50#!#0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
~~END~~


Expand All @@ -136,12 +152,12 @@ SELECT DATALENGTH(CAST(@A as BINARY(50))), CAST(@A as BINARY(50))
GO
~~START~~
int#!#binary
30#!#01
1#!#01
~~END~~

~~START~~
int#!#binary
50#!#0123456789012345678901234567890123456789012345678901234567890000000000000000000000000000000000000000
50#!#0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
~~END~~


Expand All @@ -151,12 +167,12 @@ SELECT DATALENGTH(CAST(@A as BINARY(50))), CAST(@A as BINARY(50))
GO
~~START~~
int#!#binary
40#!#01
1#!#01
~~END~~

~~START~~
int#!#binary
50#!#0123456789012345678901234567890123456789012345678901234567890123456789012345678900000000000000000000
50#!#0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
~~END~~


Expand Down
6 changes: 6 additions & 0 deletions test/JDBC/input/BABEL_4544.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ GO
DROP TABLE babel_4544_t
GO

-- default length should be 1
DECLARE @A VARBINARY = 0x0123456789012345678901234567890123456789
SELECT @A, DATALENGTH(@A)
SELECT DATALENGTH(CAST(@A as BINARY(50))), CAST(@A as BINARY(50)), CAST(@A as VARBINARY(60))
GO

DECLARE @A BINARY = 0x0123456789012345678901234567890123456789
SELECT @A, DATALENGTH(@A)
SELECT DATALENGTH(CAST(@A as BINARY(50))), CAST(@A as BINARY(50))
Expand Down

0 comments on commit ecd616d

Please sign in to comment.