Skip to content

Commit

Permalink
Fix ColumnSize evaluation in .NET connector
Browse files Browse the repository at this point in the history
Summary: 'ColumnSize' of the text type starting from version 7.8 is received in characters (previously in bytes) (see the diff, https://grizzly.internal.memcompute.com/D54237)

Test Plan: https://app.circleci.com/pipelines/github/memsql/SingleStoreNETConnector/217/workflows/9fb63d7c-cf09-49b1-9495-be75cb69493e

Reviewers: pmishchenko-ua

Reviewed By: pmishchenko-ua

Subscribers: engineering-list

JIRA Issues: PLAT-6219

Differential Revision: https://grizzly.internal.memcompute.com/D56875
  • Loading branch information
okramarenko committed Jun 6, 2022
1 parent f872e4e commit 3876fc6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/SingleStoreConnector/SingleStoreDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public ReadOnlyCollection<DbColumn> GetColumnSchema()
var hasNoSchema = columnDefinitions is null || m_resultSet!.ContainsCommandParameters;
return hasNoSchema ? new List<DbColumn>().AsReadOnly() :
columnDefinitions!
.Select((c, n) => (DbColumn) new SingleStoreDbColumn(n, c, Connection!.AllowZeroDateTime, GetResultSet().ColumnTypes![n]))
.Select((c, n) => (DbColumn) new SingleStoreDbColumn(n, c, Connection!.AllowZeroDateTime, GetResultSet().ColumnTypes![n], Connection.Session.S2ServerVersion.Version))
.ToList().AsReadOnly();
}

Expand Down
23 changes: 17 additions & 6 deletions src/SingleStoreConnector/SingleStoreDbColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,30 @@ namespace SingleStoreConnector
{
public sealed class SingleStoreDbColumn : System.Data.Common.DbColumn
{
internal SingleStoreDbColumn(int ordinal, ColumnDefinitionPayload column, bool allowZeroDateTime, SingleStoreDbType mySqlDbType)
internal SingleStoreDbColumn(int ordinal, ColumnDefinitionPayload column, bool allowZeroDateTime, SingleStoreDbType mySqlDbType, Version serverVersion)
{
var columnTypeMetadata = TypeMapper.Instance.GetColumnTypeMetadata(mySqlDbType);

var type = columnTypeMetadata.DbTypeMapping.ClrType;

if (mySqlDbType == SingleStoreDbType.JSON || mySqlDbType == SingleStoreDbType.LongBlob)
ColumnSize = int.MaxValue;
// starting from 7.8 SingleStore returns number of characters (not amount of bytes)
// for text types (e.g. Text, TinyText, MediumText, LongText)
// (see https://grizzly.internal.memcompute.com/D54237)
if (serverVersion >= new Version(7, 8, 0) &&
mySqlDbType is SingleStoreDbType.LongText or SingleStoreDbType.MediumText or SingleStoreDbType.Text or SingleStoreDbType.TinyText)
{
// overflow may occur here for SingleStoreDbType.LongText
ColumnSize = (int)column.ColumnLength;
}
else
{
if (mySqlDbType == SingleStoreDbType.JSON || mySqlDbType == SingleStoreDbType.LongBlob)
ColumnSize = int.MaxValue;
else

// overflow may occur here
ColumnSize = (int) (column.ColumnLength / ProtocolUtility.GetBytesPerCharacter(column.CharacterSet));
// overflow may occur here
ColumnSize = (int) (column.ColumnLength / ProtocolUtility.GetBytesPerCharacter(column.CharacterSet));
}

// if overflow occured, i.e. when column.ColumnLength > int.MaxValue and char size was 1,
// we set ColumnSize to max
Expand All @@ -69,7 +81,6 @@ internal SingleStoreDbColumn(int ordinal, ColumnDefinitionPayload column, bool a
BaseTableName = column.PhysicalTable;
ColumnName = column.Name;
ColumnOrdinal = ordinal;

DataType = (allowZeroDateTime && type == typeof(DateTime)) ? typeof(SingleStoreDateTime) : type;
DataTypeName = columnTypeMetadata.SimpleDataTypeName;
if (mySqlDbType == SingleStoreDbType.String)
Expand Down
2 changes: 1 addition & 1 deletion tests/SideBySide/BulkLoaderSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ public void BulkCopyColumnMappings()
Assert.Equal(3, result.RowsInserted);
Assert.Empty(result.Warnings);

using var reader = connection.ExecuteReader(@"select * from bulk_copy_column_mapping;");
using var reader = connection.ExecuteReader(@"select * from bulk_copy_column_mapping order by intvalue;");
Assert.True(reader.Read());
Assert.Equal(101, reader.GetValue(0));
Assert.Equal("A", reader.GetValue(1));
Expand Down

0 comments on commit 3876fc6

Please sign in to comment.