Skip to content

Commit

Permalink
Only skip on positive non-zero column lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
NinoFloris committed Nov 22, 2023
1 parent a3cc514 commit fa8d14b
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Npgsql/NpgsqlDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1926,6 +1926,8 @@ int SeekToColumnNonSequential(int ordinal, DataFormat dataFormat, bool resumable
{
columnLength = buffer.ReadInt32();
_column++;
Debug.Assert(columnLength >= -1);
if (columnLength > 0)
buffer.Skip(columnLength);
}
columnLength = buffer.ReadInt32();
Expand Down Expand Up @@ -1963,7 +1965,7 @@ int SeekBackwards()
for (var lastColumnRead = _columns.Count; lastColumnRead < ordinal; lastColumnRead++)
{
(Buffer.ReadPosition, var lastLen) = _columns[lastColumnRead - 1];
if (lastLen is not -1)
if (lastLen > 0)
buffer.Skip(lastLen);
var len = Buffer.ReadInt32();
_columns.Add((Buffer.ReadPosition, len));
Expand Down Expand Up @@ -2027,31 +2029,33 @@ async ValueTask<int> Core(bool async, bool commit, int ordinal, DataFormat dataF
}

// Seek to the requested column
int columnLength;
var buffer = Buffer;
// Written as a while to be able to increment _column directly after reading into it.
while (_column < ordinal - 1)
{
await buffer.Ensure(4, async).ConfigureAwait(false);
var len = buffer.ReadInt32();
columnLength = buffer.ReadInt32();
_column++;
if (len != -1)
Debug.Assert(columnLength >= -1);
if (columnLength > 0)
{
try
{
await buffer.Skip(len, async).ConfigureAwait(false);
await buffer.Skip(columnLength, async).ConfigureAwait(false);
}
catch
{
// Leave the reader in a recoverable state.
// Resumable: true causes commit to consume without error next time.
PgReader.Init(len, dataFormat, resumable: true);
PgReader.Init(columnLength, dataFormat, resumable: true);
throw;
}
}
}
await buffer.Ensure(4, async).ConfigureAwait(false);
var columnLength = buffer.ReadInt32();
columnLength = buffer.ReadInt32();
_column = ordinal;
PgReader.Init(columnLength, dataFormat, resumableOp);
Expand All @@ -2076,6 +2080,7 @@ bool TrySeekBuffered(int ordinal, out int columnLength)
return false;
columnLength = buffer.ReadInt32();
_column++;
Debug.Assert(columnLength >= -1);
if (columnLength > 0)
{
if (buffer.ReadBytesLeft < columnLength)
Expand Down

0 comments on commit fa8d14b

Please sign in to comment.