Skip to content

Data types and parameter fixes - #362

Closed
Emill wants to merge 1 commit into
npgsql:masterfrom
Emill:datatype-and-parameter-fixes
Closed

Emill wants to merge 1 commit into
npgsql:masterfrom
Emill:datatype-and-parameter-fixes

Conversation

@Emill

@Emill Emill commented Sep 21, 2014

Copy link
Copy Markdown
Contributor

Hi. I have created a fix for the parameter issues + BitString issues.

This fix make these behaviours:

The type of the parameter can either be explicitly set or automatically infered when a value is set.
If the type is explicitly set to NpgsqlDbType.Unknown or DbType.Object, no casting will be made. Instead, the value is written as a literal string. For prepared queries, the value is also sent as a literal string (.ToString() on the value), hoping the backend can take care of it.
Otherwise, casting will always be made.

Until a value has been set (or only null has been set before something else), the type is initially NpgsqlDbType.Unknown instead of text, as it was before.

The char datatype if used as parameter type doesn't truncate the string anymore to one character.

I added a "varbit" datatype which maps to BitString.
In the case the user uses "bit(1)", a bool will be returned from the database instead of BitString. Array of bit(1) means a bool[].
Before, BitString didn't work well. If used as parameter, the bit string was truncated to one bit. I've fixed this and also implemented binary encoding for use with prepared queries (didn't work before at all).

Tell me what you think :)
I think this fix makes the parameter handling and data type handling more reasonable.

It should fix #146, #335, #361

Commit log:
BitString works better now.
Bit(1) uses bool and otherwise BitString is used.
Casts are used as long as the type is not explicitly set to NpgsqlDbType.Unknown or DbType.Object. NpgsqlDbType.Unknown is now default for parameters instead of text.
Unknown parameters are always sent as text on prepared queries.
DbType.Time is now mapped to interval.
Output parameters now work for prepared queries.

…s bool and otherwise BitString is used. Casts are used as long as the type is not explicitly set to NpgsqlDbType.Unknown or DbType.Object. NpgsqlDbType.Unknown is now default for parameters instead of text. Unknown parameters are always sent as text on prepared queries. DbType.Time is now mapped to interval. Output parameters now work for prepared queries.
@roji

roji commented Sep 22, 2014

Copy link
Copy Markdown
Member

I'm not expert on the parameter side of things, but this looks like awesome work. @glenebob and probably @franciscojunior should have better background to comment.

If I understand correctly, if the user doesn't explicit set a parameter type, you modify the query text and introduce casting to let the backend know the type. With prepared statements we also have the option of using OIDs for this, right? In this case shouldn't OIDs be prepared over casts?

The bit/bitstring/bool logic seems good to me, I like that a Postgresql array is always mapped to a .NET array, and that everything is nice and static. I'd rename the DbType from Varbit to BitString though; the type isn't inherently variable-length or invariable-length. Postgresql supports both BIT(N) and VARYING BIT(N), and both are represented by the same DbType, so it shouldn't contain "var" in its name. Calling it BitString would also create a nice analogy with String.

One weird thing I found out while playing around though... The current npgsql implementation returns string for CHAR(1), rather than the expected .NET char. This seems to be another thing we should correct, and would make the two systems completely consistent:

Postgresql type .NET type
CHAR(1) char
BIT(1) bool
CHAR(N), VARCHAR(N), TEXT string
BIT(N) BitString
CHAR[] char[]
BIT[] bool[]

What do you think?

@Emill

Emill commented Sep 22, 2014

Copy link
Copy Markdown
Contributor Author

Thanks for feedback.

When sending parameter oid list, we can't specify size, for example bit(3). Therefore we have to use cast. If size is not specified, we could use oid list. I haven't measured performance what is best. It should be semantically equivalent.

Char(1) would be a good idea to use Char instead of String.

@roji

roji commented Sep 23, 2014

Copy link
Copy Markdown
Member

Is the size really an issue? I mean, when you're sending parameters, isn't the OID for bit enough? After all, you're sending one bit type (with whatever size) to go into a column with a bit type (that could be with another size I think)? You're right that there are two OIDs, one for bit and one for varbit, but as a parameter OID bit should always be fine, shouldn't it? In any case I don't understand why the size would be a problem when sending OIDs to the database...

I'm don't know anything about the performance difference, but if it's possible to send OIDs we should try to do it, right? It's less query rewriting at the client and less parsing at the server...

About char(1), do you want to do it as part of this PR?

@roji

roji commented Oct 12, 2014

Copy link
Copy Markdown
Member

@Emill, I think I've merged all my work which touches on the same areas. Sorry about the conflicts you will get when rebasing this... but I think you can continue working. There are some questions above (use OIDs instead of casts, char(1)), it would be good to see this merged.

@franciscojunior

Copy link
Copy Markdown
Member

The bit/bitstring/bool logic seems good to me, I like that a Postgresql array is always mapped to a .NET array, and that everything is nice and static. I'd rename the DbType from Varbit to BitString though; the type isn't inherently variable-length or invariable-length. Postgresql supports both BIT(N) and VARYING BIT(N), and both are represented by the same DbType, so it shouldn't contain "var" in its name. Calling it BitString would also create a nice analogy with String.

+1 to change it to BitString

One weird thing I found out while playing around though... The current npgsql implementation returns string for CHAR(1), rather than the expected .NET char. This seems to be another thing we should correct, and would make the two systems completely consistent:

"There's a previous discussion for That" :)
http://lists.pgfoundry.org/pipermail/npgsql-devel/2008-April/000616.html

http://lists.pgfoundry.org/pipermail/npgsql-devel/2008-April/000634.html

@Emill

Emill commented Oct 15, 2014

Copy link
Copy Markdown
Contributor Author

Anyone who knows how this is done in sql server and mysql?

@franciscojunior

Copy link
Copy Markdown
Member

When sending parameter oid list, we can't specify size, for example bit(3). Therefore we have to use cast. If size is not specified, we could use oid list.

I think we can use the oid regardless of the size. From the documentation, the Parse message only contains support for the type oid. I think postgresql can figure out the sizes later by the parameter value itself.

@roji

roji commented Oct 15, 2014

Copy link
Copy Markdown
Member

@franciscojunior, about the char/string thing - I didn't really understand the problem... the fact that there's a special Postgresql "char" prevents us in some way from mapping char(1) to .NET Char?

If the efficiency argument is the important one (checking each time), it may still be worth looking into, it doesn't sound like serious impact...

@roji

roji commented Oct 15, 2014

Copy link
Copy Markdown
Member

@Emill: good question, I don't know. It would also be good to check the Postgresql JDBC driver...

@roji

roji commented Oct 15, 2014

Copy link
Copy Markdown
Member

The "right" way to check for char(1) or varchar(1) (as opposed to char(2) etc.) seems to be to check the The type modifier in the RowDescription, which corresponds to pg_attribute.atttypmod):

atttypmod records type-specific data supplied at table creation time (for example, the maximum length of a varchar column). It is passed to type-specific input functions and length coercion functions. The value will generally be -1 for types that do not need atttypmod.

@franciscojunior

Copy link
Copy Markdown
Member

@franciscojunior, about the char/string thing - I didn't really understand the problem... the fact that there's a special Postgresql "char" prevents us in some way from mapping char(1) to .NET Char?

I think you are right. I'm reviewing the discussion and the problem is in fact with the type char itself and not char(1).

One problem, though is how would you map the other way around: .Net char to char(1)? .Net char has a size of 16 bits.

@roji

roji commented Oct 15, 2014

Copy link
Copy Markdown
Member

I think it's right to map .NET Char to char(1) - char(1) is unicode (2-byte) so there's no problem. It's just like mapping a string to char(N)

The special 1-byte "char" type can simply be mapped to .NET byte, which is really what it is: according to the docs its an internal enum-like representation...

@franciscojunior

Copy link
Copy Markdown
Member

I think it's right to map .NET Char to char(1) - char(1) is unicode (2-byte) so there's no problem. It's just like mapping a string to char(N)

Hmmmmm, I thought char(1) would be 8-bits. I think at the time of the ASCII encoding it really was. Now that we support exclusively UTF-8 database encoding, a char(1) may not necessarily map to a single byte value :)

@franciscojunior

Copy link
Copy Markdown
Member

I think it's right to map .NET Char to char(1) - char(1) is unicode (2-byte) so there's no problem. It's just like mapping a string to char(N)

The special 1-byte "char" type can simply be mapped to .NET byte, which is really what it is: according to the docs its an internal enum-like representation...

+1 to map char(1) to .Net Char and add the documentation about this possible breaking change.

@franciscojunior

Copy link
Copy Markdown
Member

Anyone who knows how this is done in sql server and mysql?

I also don't know :(

@Emill

Emill commented Oct 18, 2014

Copy link
Copy Markdown
Contributor Author

One weird thing I found out while playing around though... The current npgsql implementation returns string for CHAR(1), rather than the expected .NET char. This seems to be another thing we should correct, and would make the two systems completely consistent.

Well, MS SQL's SqlClient returns a string for CHAR(1), so I've changed my mind and think we should do the same and keep how it is today. Their BIT type is always only one bit, never longer (i.e. there are no BIT(n) or BIT VARYING), therefore their BIT maps to bool. So I think it is good that our BIT(1) maps to bool but BIT VARYING and BIT(n>1) maps to BitString.

The bit/bitstring/bool logic seems good to me, I like that a Postgresql array is always mapped to a .NET array, and that everything is nice and static. I'd rename the DbType from Varbit to BitString though; the type isn't inherently variable-length or invariable-length. Postgresql supports both BIT(N) and VARYING BIT(N), and both are represented by the same DbType, so it shouldn't contain "var" in its name. Calling it BitString would also create a nice analogy with String.

BIT and BIT VARYING are two different data types (and have different OIDs), so I think there should be two different NpgsqlDbTypes. Compare with Char and Varchar: there are an NpgsqlDbType.Char and an NpgsqlDbType.Varchar.

One thing I'm wondering over, in NpgsqlTypesHelper.cs, is there a reason that DbType.StringFixedLength isn't used instead of DbType.String for bpchar?

@roji

roji commented Oct 18, 2014

Copy link
Copy Markdown
Member

Well, MS SQL's SqlClient returns a string for CHAR(1), so I've changed my mind and think we should do the same and keep how it is today. Their BIT type is always only one bit, never longer (i.e. there are no BIT(n) or BIT VARYING), therefore their BIT maps to bool. So I think it is good that our BIT(1) maps to bool but BIT VARYING and BIT(n>1) maps to BitString.

It makes sense to align with SqlClient I guess. But in that case, is there any data type that SqlClient maps to .NET Char?

BIT and BIT VARYING are two different data types (and have different OIDs), so I think there should be two different NpgsqlDbTypes. Compare with Char and Varchar: there are an NpgsqlDbType.Char and an NpgsqlDbType.Varchar.

Hmm... I think the interesting comparison is maybe between VARCHAR and TEXT (CHAR being the single-char type, which seems not to correspond to any Postgresql type currently). I agree that the bit types should behave more or less like the character types, but I'm not sure the importance is of making the (client-side) distinction between VARCHAR and TEXT, it seems to be a purely backend-related storage consideration. But in the interest of not breaking too much etc. I guess we can leave it that way and do the same with the bit types.

One thing I'm wondering over, in NpgsqlTypesHelper.cs, is there a reason that DbType.StringFixedLength isn't used instead of DbType.String for bpchar?

No idea here...

@franciscojunior

Copy link
Copy Markdown
Member

Em sábado, 18 de outubro de 2014, Emill [email protected] escreveu:

One weird thing I found out while playing around though... The current
npgsql implementation returns string for CHAR(1), rather than the expected
.NET char. This seems to be another thing we should correct, and would make
the two systems completely consistent.

Well, MS SQL's SqlClient returns a string for CHAR(1), so I've changed my
mind and think we should do the same and keep how it is today. Their BIT
type is always only one bit, never longer (i.e. there are no BIT(n) or BIT
VARYING), therefore their BIT maps to bool. So I think it is good that our
BIT(1) maps to bool but BIT VARYING and BIT(n>1) maps to BitString.

+1 for this. By mirroring sqlclient behavior we get a more consistent
library.

The bit/bitstring/bool logic seems good to me, I like that a Postgresql
array is always mapped to a .NET array, and that everything is nice and
static. I'd rename the DbType from Varbit to BitString though; the type
isn't inherently variable-length or invariable-length. Postgresql supports
both BIT(N) and VARYING BIT(N), and both are represented by the same
DbType, so it shouldn't contain "var" in its name. Calling it BitString
would also create a nice analogy with String.

BIT and BIT VARYING are two different data types (and have different
OIDs), so I think there should be two different NpgsqlDbTypes. Compare with
Char and Varchar: there are an NpgsqlDbType.Char and an
NpgsqlDbType.Varchar.

I see. Giving what you said, I changed my mind and I think the best name
would be varbit.

One thing I'm wondering over, in NpgsqlTypesHelper.cs, is there a reason
that DbType.StringFixedLength isn't used instead of DbType.String for
bpchar?

I think there is no specific reason for that. As bpchar , if I'm not wrong,
isn't very used inside and outside of Npgsql (internally or by clients), we
simply mapped it to string.

—

Reply to this email directly or view it on GitHub
#362 (comment).

Regards,

Francisco Figueiredo Jr.
Npgsql Lead Developer
http://www.npgsql.org
http://gplus.to/franciscojunior
http://fxjr.blogspot.com
http://twitter.com/franciscojunior

@Emill

Emill commented Oct 18, 2014

Copy link
Copy Markdown
Contributor Author

I think there's kind of a lot of confusion that there are both the "char" datatype and the char datatype, which in postgresql is called bpchar (blank-padded char). When Npgsql retrieves the pg_type table, the one that has its name value set to char is actually the "char" type (i.e. the one with just one signed byte). So in NpgsqlTypesHelper.cs, for types with the name string set to char is the "char" type, if I'm not mistaken.

The problem with the "char" type is that is is just one single signed byte, but it cannot be used as an integer (for example adding an integer to a "char" is not possible), but it can be converted to an int4. When written in text form (or converted to text), it is like in C, i.e. a string with one character. If the value is negative and converted to text, it produces an illegal utf-8 string. I suppose the intention of this data type is to match C's char type. I have no idea whether it is best to map this to .NET byte, char or string, but probably byte or char seems most reasonable. Remember that .NET's byte is unsigned.

@franciscojunior

Copy link
Copy Markdown
Member

Em sábado, 18 de outubro de 2014, Emill [email protected] escreveu:

I think there's kind of a lot of confusion that there are both the "char"
datatype and the char datatype, which in postgresql is called bpchar
(blank-padded char). When Npgsql retrieves the pg_type table, the one that
has its name value set to char is actually the "char" type (i.e. the one
with just one signed byte). So in NpgsqlTypesHelper.cs, for types with the
name string set to char is the "char" type, if I'm not mistaken.

The problem with the "char" type is that is is just one single signed
byte, but it cannot be used as an integer (for example adding an integer to
a "char" is not possible), but it can be converted to an int4. When
written in text form (or converted to text), it is like in C, i.e. a string
with one character. If the value is negative and converted to text, it
produces an illegal utf-8 string. I suppose the intention of this data type
is to match C's char type. I have no idea whether it is best to map this to
.NET byte, char or string, but probably byte or char seems most reasonable.
Remember that .NET's byte is unsigned.

I'll have a look at it, as this mapping was done a long time ago. What you
said makes a lot of sense. At the time we worked with this mapping we paid
very little attention to bpchar data type.

—
Reply to this email directly or view it on GitHub
#362 (comment).

Regards,

Francisco Figueiredo Jr.
Npgsql Lead Developer
http://www.npgsql.org
http://gplus.to/franciscojunior
http://fxjr.blogspot.com
http://twitter.com/franciscojunior

@Emill

Emill commented Oct 18, 2014

Copy link
Copy Markdown
Contributor Author

I just noticed that if you set a SqlParameter's value to a .NET char, the inferred type will be DbType.String and SqlDbType.NVarChar. Also, if you then try to retrieve the value again, you get a string back and not a char ;)

@roji

roji commented Oct 18, 2014

Copy link
Copy Markdown
Member

I just noticed that if you set a SqlParameter's value to a .NET char, the inferred type will be DbType.String and SqlDbType.NVarChar. Also, if you then try to retrieve the value again, you get a string back and not a char ;)

That really sucks.

A part of me wants to "fix" that in Npgsql and have chars roundtrip correctly, although aligning with SqlClient is also an important goal. Thoughts?

@Emill

Emill commented Oct 18, 2014

Copy link
Copy Markdown
Contributor Author

Hmm, according to the test suite, NpgsqlDbType.Char seems to be used to get the bpchar type, but in the type mappings, NpgsqlDbType.Char is actually mapped to the single byte "char" data type, with OID 18. To avoid breakage, I think we should add a new NpgsqlDbType and call it something like SingleChar that should map to "char". Then we can correctly handle this, which will probably help later in #370.

By the way, does anyone know if the "char" datatype widely used?

@roji

roji commented Oct 18, 2014

Copy link
Copy Markdown
Member

By the way, does anyone know if the "char" datatype widely used?

I really don't think so. The Postgresql docs say:

It is internally used in the system catalogs as a simplistic enumeration type.

I think it's definitely OK to introduce breakage around this type in 3.0.

@franciscojunior

Copy link
Copy Markdown
Member

A part of me wants to "fix" that in Npgsql and have chars roundtrip correctly, although aligning with SqlClient is also an important goal. Thoughts?

I'm always in favor of aligning with SqlClient where possible. A lot of Npgsql users is used to work with SqlClient and I think we should mirror its behavior. I know this may sound like a little lack of personality but we would get a more consistent library as many would expect the behavior to be similar.

@franciscojunior

Copy link
Copy Markdown
Member

I really don't think so. The Postgresql docs say:

I also think almost nobody uses the "char" datatype.

I think it's definitely OK to introduce breakage around this type in 3.0.

Agreed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants