Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplemented sys.babelfish_concat_wrapper in C to improve the performance #1911

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions contrib/babelfishpg_common/sql/string_operators.sql
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
-- Wrap built-in CONCAT function to accept two text arguments.
-- This is necessary because CONCAT accepts arguments of type VARIADIC "any".
-- CONCAT also automatically handles NULL which || does not.
CREATE OR REPLACE FUNCTION sys.babelfish_concat_wrapper(leftarg text, rightarg text) RETURNS TEXT AS
$$
SELECT
CASE WHEN (current_setting('babelfishpg_tsql.concat_null_yields_null') = 'on') THEN
CASE
WHEN leftarg IS NULL OR rightarg IS NULL THEN NULL
ELSE CONCAT(leftarg, rightarg)
END
ELSE
CONCAT(leftarg, rightarg)
END
$$
LANGUAGE SQL STABLE;
CREATE OR REPLACE FUNCTION sys.babelfish_concat_wrapper(leftarg text, rightarg text) RETURNS TEXT
AS 'babelfishpg_tsql', 'babelfish_concat_wrapper'
LANGUAGE C STABLE PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.babelfish_concat_wrapper_outer(leftarg text, rightarg text) RETURNS sys.varchar(8000) AS
$$
Expand Down
46 changes: 45 additions & 1 deletion contrib/babelfishpg_tsql/runtime/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ PG_FUNCTION_INFO_V1(object_schema_name);
PG_FUNCTION_INFO_V1(parsename);
PG_FUNCTION_INFO_V1(pg_extension_config_remove);
PG_FUNCTION_INFO_V1(objectproperty_internal);
PG_FUNCTION_INFO_V1(babelfish_concat_wrapper);

void *string_to_tsql_varchar(const char *input_str);
void *get_servername_internal(void);
Expand Down Expand Up @@ -181,13 +182,56 @@ const char *bbf_servicename = "MSSQLSERVER";
char *bbf_language = "us_english";
#define MD5_HASH_LEN 32


Datum
trancount(PG_FUNCTION_ARGS)
{
PG_RETURN_UINT32(NestedTranCount);
}

Datum
babelfish_concat_wrapper(PG_FUNCTION_ARGS)
{
text *arg1, *arg2, *new_text;
int32 arg1_size, arg2_size, new_text_size;
bool first_param = PG_ARGISNULL(0);
bool second_param = PG_ARGISNULL(1);

if (pltsql_concat_null_yields_null)
{
if(first_param || second_param)
{
PG_RETURN_NULL(); // If any is NULL, return NULL
}
}
else
{
if (first_param && second_param)
{
PG_RETURN_NULL(); // If both are NULL, return NULL
}
else if (second_param)
{
PG_RETURN_TEXT_P(PG_GETARG_TEXT_PP(0)); // If only the second string is NULL, return the first string
}
else if (first_param)
{
PG_RETURN_TEXT_P(PG_GETARG_TEXT_PP(1)); // If only the first string is NULL, return the second string
}
}
arg1 = PG_GETARG_TEXT_PP(0);
arg2 = PG_GETARG_TEXT_PP(1);
arg1_size = VARSIZE_ANY_EXHDR(arg1);
arg2_size = VARSIZE_ANY_EXHDR(arg2);
new_text_size = arg1_size + arg2_size + VARHDRSZ;
ParikshitSarode marked this conversation as resolved.
Show resolved Hide resolved
new_text = (text *) palloc(new_text_size);

SET_VARSIZE(new_text, new_text_size);
memcpy(VARDATA(new_text), VARDATA_ANY(arg1), arg1_size);
memcpy(VARDATA(new_text) + arg1_size, VARDATA_ANY(arg2), arg2_size);
ParikshitSarode marked this conversation as resolved.
Show resolved Hide resolved
PG_RETURN_TEXT_P(new_text);
}


Datum
procid(PG_FUNCTION_ARGS)
{
Expand Down
Loading