Skip to content

Fix NumberToBytesConverter for empty arrays - #21269

Closed
bdebaere wants to merge 2 commits into
dotnet:masterfrom
bdebaere:fix.empty.byte.conversion
Closed

bdebaere wants to merge 2 commits into
dotnet:masterfrom
bdebaere:fix.empty.byte.conversion

Conversation

@bdebaere

Copy link
Copy Markdown

Introduces a check on Array.Length equalling zero to avoid an IndexOutOfRangeException when called on a null navigation property.

@ajcvickers ajcvickers self-assigned this Jun 15, 2020
@bdebaere

Copy link
Copy Markdown
Author

@ajcvickers Build failing due to "##[error]The job running on agent NetCorePublic-Pool 239 ran longer than the maximum time of 60 minutes. For more information, see https://go.microsoft.com/fwlink/?linkid=2077134", rerun?

@ajcvickers

Copy link
Copy Markdown
Contributor

@bdebaere We discussed this and don't think that this is the correct behavior. When converting to a byte, we expect exactly one byte, for a short, exactly 2 bytes, for an int, exactly 4 bytes, and so on. If anything is needed in this space it is throwing a better exception message when the byte array is the wrong size.t

@bdebaere

Copy link
Copy Markdown
Author

@ajcvickers It's not about the byte array being the wrong size. It's about the byte array being empty. See #12518 for an example of when this happens.

@smitpatel

Copy link
Copy Markdown
Contributor

@bdebaere - Passing empty array to the converter was a bug in EF Core query pipeline which has already been fixed. So the case in the issue #12518 does not arise anymore.

@bdebaere

bdebaere commented Jun 15, 2020 •

Copy link
Copy Markdown
Author

@smitpatel Is it fixed in 3.1? I'm running that version and getting the bug. Same with other array properties like string[]. If you need a repro, let me know.

@smitpatel

Copy link
Copy Markdown
Contributor

I ran the repro code in the issue and it worked for me. Share your repro code if it is still failing.
Regardless of outcome of that, the bug is that query should not pass empty array to these value converters. So fix would be to avoid query passing empty array and not change value converter to allow empty arrays.

@bdebaere bdebaere closed this Jun 15, 2020
@bdebaere bdebaere reopened this Jun 16, 2020
@bdebaere

Copy link
Copy Markdown
Author

@ajcvickers @smitpatel
The bug is the following. In the BufferedDataReader.BufferedDataRecord.ReadRow method the following check is performed if (!(_tempNulls[_currentRowNumber * _nullCount + nullIndex] = _underlyingReader.IsDBNull(i))). For a reason unknown to me IsDBNull always returns false when the data type is timestamp (rowversion), see my StackOverflow post about it at https://stackoverflow.com/questions/62410830/timestamp-isdbnull.
What this means is that, in the case of timestamp its value will always be Array.Empty, see screenshot below.
afbeelding

Unless there's something obvious I'm missing here which would render this all wrong, in which case I apologize in advance, these are the paths I currently see:

  1. Extend the IsDBNull check with a type check on rowversion, could be based on EntityFrameworkCore metadata or DbDataReader.GetDataTypeName (the last one seems rather dirty);
  2. Add a value converter specifically for rowversion which can deal with empty arrays instead of the default NumberToBytesConverter;
  3. Adjust the NumberToBytesConverter to be able to handle empty arrays, as is done currently.

Do let me know which one of the above paths you wish to take, or if you wish to take a different path, so that I can adjust my pull request.

@ErikEJ

ErikEJ commented Jun 16, 2020

Copy link
Copy Markdown
Contributor

But your column is declared as "NOT NULL" !? Have you tried:

Have you tried:

CREATE TABLE [Entity] (
    [Id] UNIQUEIDENTIFIER NOT NULL,
    [RowVersion] TIMESTAMP NULL
)

@bdebaere

Copy link
Copy Markdown
Author

@ErikEJ Yes, I have tried that. Same result: 0x.

@ErikEJ

ErikEJ commented Jun 16, 2020

Copy link
Copy Markdown
Contributor

OK, rowversion is never null, and always gets a value assigned by the database engine: A nonnullable rowversion column is semantically equivalent to a binary(8) column. A nullable rowversion column is semantically equivalent to a varbinary(8) column.

@smitpatel

Copy link
Copy Markdown
Contributor

Duplicate of #20633 #20364

@smitpatel smitpatel closed this Jun 16, 2020
@smitpatel smitpatel reopened this Jun 16, 2020
@bdebaere

Copy link
Copy Markdown
Author

@ErikEJ If you change the type to BINARY(8) NOT NULL, you will notice that the behavior is as expected: NULL is returned, and ISDBNull is true. It's only rowversion/timestamp that has this issue.

@smitpatel

Copy link
Copy Markdown
Contributor

@bdebaere - Please file a new issue if you are seeing something wrong with rowversion. Or use #12518 if you want to discuss about bug where empty array is being read by query pipeline incorrectly.

I am closing out this PR as this is not the change we want to make and it is unnecessarily running builds.

@smitpatel smitpatel closed this Jun 16, 2020
@bdebaere bdebaere changed the title Fix NumberToBytesConvert for empty arrays Fix NumberToBytesConverter for empty arrays Jun 16, 2020
@bdebaere

Copy link
Copy Markdown
Author

@smitpatel Then what is the change you want to see? All you have to do is tell me, and I can start working on it.

@smitpatel

Copy link
Copy Markdown
Contributor

Don't read default value when reading values from DataReader, rather let it fail when assigning values so assigning null value to non-null will throw exception. Alongside update BufferedDataReader to accommodate that null value could come in even though the reader column was expecting a non-null value since DbDataReader does not care about nullability of values either.

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.

4 participants