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

Improve getting raw/binary values from ResultSet #209

Merged
merged 1 commit into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 28 additions & 11 deletions src/Knet.Kudu.Client/ResultSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,23 +418,28 @@ private decimal ReadDecimalUnsafe(ColumnSchema column, int columnIndex, int rowI
return KuduEncoder.DecodeDecimalUnsafe(_data!, offset, column.Type, scale);
}

internal ReadOnlySpan<byte> GetRawFixed(string columnName, int rowIndex)
internal ReadOnlySpan<byte> GetSpan(string columnName, int rowIndex)
{
int columnIndex = GetColumnIndex(columnName);
return GetRawFixed(columnIndex, rowIndex);
return GetSpan(columnIndex, rowIndex);
}

internal ReadOnlySpan<byte> GetRawFixed(int columnIndex, int rowIndex)
internal ReadOnlySpan<byte> GetSpan(int columnIndex, int rowIndex)
{
var column = CheckFixedLengthType(columnIndex);
var column = GetColumnSchema(columnIndex);

if (IsNullUnsafe(columnIndex, rowIndex))
return default;

int size = column.Size;
int offset = GetStartIndexUnsafe(columnIndex, rowIndex, size);
if (column.IsFixedSize)
{
int size = column.Size;
int offset = GetStartIndexUnsafe(columnIndex, rowIndex, size);

return _data.AsSpan(offset, size);
}

return _data.AsSpan(offset, size);
return ReadBinaryUnsafe(columnIndex, rowIndex);
}

internal string GetString(string columnName, int rowIndex)
Expand Down Expand Up @@ -474,20 +479,32 @@ private string ReadStringUnsafe(int columnIndex, int rowIndex)
return KuduEncoder.DecodeString(_data!, offset, length);
}

internal ReadOnlySpan<byte> GetBinary(string columnName, int rowIndex)
internal byte[] GetBinary(string columnName, int rowIndex)
{
int columnIndex = GetColumnIndex(columnName);
return GetBinary(columnIndex, rowIndex);
}

internal ReadOnlySpan<byte> GetBinary(int columnIndex, int rowIndex)
internal byte[] GetBinary(int columnIndex, int rowIndex)
{
CheckTypeNotNull(columnIndex, rowIndex, KuduType.Binary);
return ReadBinaryUnsafe(columnIndex, rowIndex).ToArray();
}

internal byte[]? GetNullableBinary(string columnName, int rowIndex)
{
int columnIndex = GetColumnIndex(columnName);
return GetNullableBinary(columnIndex, rowIndex);
}

internal byte[]? GetNullableBinary(int columnIndex, int rowIndex)
{
CheckType(columnIndex, KuduType.Binary);

if (IsNullUnsafe(columnIndex, rowIndex))
return default;
return null;

return ReadBinaryUnsafe(columnIndex, rowIndex);
return ReadBinaryUnsafe(columnIndex, rowIndex).ToArray();
}

private ReadOnlySpan<byte> ReadBinaryUnsafe(int columnIndex, int rowIndex)
Expand Down
26 changes: 17 additions & 9 deletions src/Knet.Kudu.Client/RowResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,20 @@ public decimal GetDecimal(int columnIndex) =>
_resultSet.GetNullableDecimal(columnIndex, _index);

/// <summary>
/// Get the raw value of a fixed length data column.
/// Get the raw value of the given column.
/// </summary>
/// <param name="columnName">The column name.</param>
public ReadOnlySpan<byte> GetRawFixed(string columnName) =>
_resultSet.GetRawFixed(columnName, _index);
/// <returns>A zero-copy span of the raw value.</returns>
public ReadOnlySpan<byte> GetSpan(string columnName) =>
_resultSet.GetSpan(columnName, _index);

/// <summary>
/// Get the raw value of a fixed length data column.
/// Get the raw value of the given column.
/// </summary>
/// <param name="columnIndex">The column index.</param>
public ReadOnlySpan<byte> GetRawFixed(int columnIndex) =>
_resultSet.GetRawFixed(columnIndex, _index);
/// <returns>A zero-copy span of the raw value.</returns>
public ReadOnlySpan<byte> GetSpan(int columnIndex) =>
_resultSet.GetSpan(columnIndex, _index);

public string GetString(string columnName) =>
_resultSet.GetString(columnName, _index);
Expand All @@ -170,12 +172,18 @@ public string GetString(int columnIndex) =>
public string? GetNullableString(int columnIndex) =>
_resultSet.GetNullableString(columnIndex, _index);

public ReadOnlySpan<byte> GetBinary(string columnName) =>
public byte[] GetBinary(string columnName) =>
_resultSet.GetBinary(columnName, _index);

public ReadOnlySpan<byte> GetBinary(int columnIndex) =>
public byte[] GetBinary(int columnIndex) =>
_resultSet.GetBinary(columnIndex, _index);

public byte[]? GetNullableBinary(string columnName) =>
_resultSet.GetNullableBinary(columnName, _index);

public byte[]? GetNullableBinary(int columnIndex) =>
_resultSet.GetNullableBinary(columnIndex, _index);

public bool IsNull(string columnName) =>
_resultSet.IsNull(columnName, _index);

Expand Down Expand Up @@ -232,7 +240,7 @@ public override string ToString()
stringBuilder.Append(GetString(i));
break;
case KuduType.Binary:
stringBuilder.Append(BitConverter.ToString(GetBinary(i).ToArray()));
stringBuilder.Append(BitConverter.ToString(GetBinary(i)));
break;
case KuduType.Float:
stringBuilder.Append(GetFloat(i));
Expand Down
2 changes: 1 addition & 1 deletion test/Knet.Kudu.Client.FunctionalTests/KeyEncodingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public async Task TestAllPrimaryKeyTypes()
Assert.Equal(3, row.GetInt32("int32"));
Assert.Equal(4, row.GetInt64("int64"));
Assert.Equal("foo", row.GetString("string"));
Assert.Equal("bar".ToUtf8ByteArray(), row.GetBinary("binary").ToArray());
Assert.Equal("bar".ToUtf8ByteArray(), row.GetBinary("binary"));
Assert.Equal(6, row.GetInt64("timestamp"));
Assert.Equal(DecimalUtil.MaxUnscaledDecimal32, row.GetDecimal("decimal32"));
Assert.Equal(DecimalUtil.MaxUnscaledDecimal64, row.GetDecimal("decimal64"));
Expand Down
39 changes: 22 additions & 17 deletions test/Knet.Kudu.Client.FunctionalTests/RowResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task TestNonNullRows(bool includeNullsInSchema)
{
Assert.Equal(currentRow, row.GetInt32("key"));
Assert.Equal(currentRow, row.GetNullableInt32("key"));
Assert.Equal(KuduEncoder.EncodeInt32(currentRow), row.GetRawFixed("key").ToArray());
Assert.Equal(KuduEncoder.EncodeInt32(currentRow), row.GetSpan("key").ToArray());
Assert.Equal(42, row.GetByte("int8"));
Assert.Equal((byte?)42, row.GetNullableByte("int8"));
Assert.Equal(42, row.GetSByte("int8"));
Expand All @@ -65,7 +65,8 @@ public async Task TestNonNullRows(bool includeNullsInSchema)
Assert.Equal("fun with ütf\0", row.GetNullableString("string"));
Assert.Equal("árvíztűrő ", row.GetString("varchar"));
Assert.Equal("árvíztűrő ", row.GetNullableString("varchar"));
Assert.Equal(new byte[] { 0, 1, 2, 3, 4 }, row.GetBinary("binary").ToArray());
Assert.Equal(new byte[] { 0, 1, 2, 3, 4 }, row.GetBinary("binary"));
Assert.Equal(new byte[] { 0, 1, 2, 3, 4 }, row.GetSpan("binary").ToArray());
Assert.Equal(DateTime.Parse("8/19/2020 7:50 PM").ToUniversalTime(), row.GetDateTime("timestamp"));
Assert.Equal(DateTime.Parse("8/19/2020 7:50 PM").ToUniversalTime(), row.GetNullableDateTime("timestamp"));
Assert.Equal(DateTime.Parse("8/19/2020").ToUniversalTime().Date, row.GetDateTime("date"));
Expand Down Expand Up @@ -141,25 +142,28 @@ public async Task TestNullRows()
Assert.Null(row.GetNullableDouble("double"));
Assert.Null(row.GetNullableString("string"));
Assert.Null(row.GetNullableString("varchar"));
Assert.Equal(0, row.GetBinary("binary").Length);
Assert.Null(row.GetNullableBinary("binary"));
Assert.Null(row.GetNullableDateTime("timestamp"));
Assert.Null(row.GetNullableDateTime("date"));
Assert.Null(row.GetNullableDecimal("decimal32"));
Assert.Null(row.GetNullableDecimal("decimal64"));
Assert.Null(row.GetNullableDecimal("decimal128"));

Assert.Equal(0, row.GetRawFixed("int8").Length);
Assert.Equal(0, row.GetRawFixed("int16").Length);
Assert.Equal(0, row.GetRawFixed("int32").Length);
Assert.Equal(0, row.GetRawFixed("int64").Length);
Assert.Equal(0, row.GetRawFixed("bool").Length);
Assert.Equal(0, row.GetRawFixed("float").Length);
Assert.Equal(0, row.GetRawFixed("double").Length);
Assert.Equal(0, row.GetRawFixed("timestamp").Length);
Assert.Equal(0, row.GetRawFixed("date").Length);
Assert.Equal(0, row.GetRawFixed("decimal32").Length);
Assert.Equal(0, row.GetRawFixed("decimal64").Length);
Assert.Equal(0, row.GetRawFixed("decimal128").Length);
Assert.Equal(0, row.GetSpan("int8").Length);
Assert.Equal(0, row.GetSpan("int16").Length);
Assert.Equal(0, row.GetSpan("int32").Length);
Assert.Equal(0, row.GetSpan("int64").Length);
Assert.Equal(0, row.GetSpan("bool").Length);
Assert.Equal(0, row.GetSpan("float").Length);
Assert.Equal(0, row.GetSpan("double").Length);
Assert.Equal(0, row.GetSpan("string").Length);
Assert.Equal(0, row.GetSpan("varchar").Length);
Assert.Equal(0, row.GetSpan("binary").Length);
Assert.Equal(0, row.GetSpan("timestamp").Length);
Assert.Equal(0, row.GetSpan("date").Length);
Assert.Equal(0, row.GetSpan("decimal32").Length);
Assert.Equal(0, row.GetSpan("decimal64").Length);
Assert.Equal(0, row.GetSpan("decimal128").Length);
currentRow++;
}
}
Expand Down Expand Up @@ -239,7 +243,7 @@ public async Task TestResultSetDispose()

Assert.Throws<ObjectDisposedException>(() => row.GetInt32("key"));
Assert.Throws<ObjectDisposedException>(() => row.GetNullableInt32("key"));
Assert.Throws<ObjectDisposedException>(() => row.GetRawFixed("key"));
Assert.Throws<ObjectDisposedException>(() => row.GetSpan("key"));
Assert.Throws<ObjectDisposedException>(() => row.GetByte("int8"));
Assert.Throws<ObjectDisposedException>(() => row.GetNullableByte("int8"));
Assert.Throws<ObjectDisposedException>(() => row.GetSByte("int8"));
Expand All @@ -260,7 +264,8 @@ public async Task TestResultSetDispose()
Assert.Throws<ObjectDisposedException>(() => row.GetNullableString("string"));
Assert.Throws<ObjectDisposedException>(() => row.GetString("varchar"));
Assert.Throws<ObjectDisposedException>(() => row.GetNullableString("varchar"));
Assert.Throws<ObjectDisposedException>(() => row.GetBinary("binary").ToArray());
Assert.Throws<ObjectDisposedException>(() => row.GetBinary("binary"));
Assert.Throws<ObjectDisposedException>(() => row.GetNullableBinary("binary"));
Assert.Throws<ObjectDisposedException>(() => row.GetDateTime("timestamp"));
Assert.Throws<ObjectDisposedException>(() => row.GetNullableDateTime("timestamp"));
Assert.Throws<ObjectDisposedException>(() => row.GetDateTime("date"));
Expand Down