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

[Bug]: Scale is ignored for decimal columns in prepared statements #2678

Open
1 task done
staticlibs opened this issue Jun 22, 2024 · 0 comments
Open
1 task done

[Bug]: Scale is ignored for decimal columns in prepared statements #2678

staticlibs opened this issue Jun 22, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@staticlibs
Copy link
Contributor

What happened?

The following snippet returns 123.45 on Babelfish and 123.46 on MSSQL:

create table tab1(num decimal(5, 2))
declare @p1 int
set @p1 = null
exec sp_prepare @p1 output, N'@P0 decimal(38,3)', N'insert into tab1(num) values(@P0)', 1
exec sp_execute @p1, 123.456
select * from tab1

If we use numeric instead of decimal in either table column definition or in sp_prepare
second argument (parameters definition) - then Babelfish correctly returns 123.46,
both following examples work correctly on Babelfish:

create table tab1(num numeric(5, 2))
declare @p1 int
set @p1 = null
exec sp_prepare @p1 output, N'@P0 decimal(38,3)', N'insert into tab1(num) values(@P0)', 1
exec sp_execute @p1, 123.456
select * from tab1
create table tab1(num decimal(5, 2))
declare @p1 int
set @p1 = null
exec sp_prepare @p1 output, N'@P0 numeric(38,3)', N'insert into tab1(num) values(@P0)', 1
exec sp_execute @p1, 123.456
select * from tab1

The problem happens on insert, the scale value 2 included in column type modifier is ignored
if column is declared as decimal. I can see that sys.decimal is defined as a DOMAIN and
AFAIU Postgres does not support type modifiers for domains.

The problem with sp_prepare + sp_execute happens the same way for both T-SQL and TDS RPC invocations.

The problem also happens with sp_executesql when it is called in T-SQL:

create table tab1(num decimal(5, 2))
exec sp_executesql N'insert into tab1(num) values(@P0) ', N'@P0 decimal(38,3)', 123.456
select * from tab1

But it does NOT happen when sp_executesql is called with TDS RPC. This code path is used with prepared statements in mssql-jdbc driver. Because, unlike sp_prepare + sp_execute calls, with sp_executesql actual parameter values are passed in the same call, their type ID is set to numeric instead of decimal and this type ID is preserved in pltsql_declare_variable() and then is made available to executor from exec_eval_datum().

As a workaround, it is possible to rewrite decimal to numeric in read_param_def() before passing type names to type IDs lookup. This fixes the problem for jTDS driver (that uses sp_prepare + sp_execute RPC for prepared statements). But I assume the problem requires more general solution so do not propose it as a fix.

Version

BABEL_4_X_DEV (Default)

Extension

babelfishpg_tsql (Default)

Which flavor of Linux are you using when you see the bug?

Fedora

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct.
@staticlibs staticlibs added the bug Something isn't working label Jun 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant