Skip to content

Commit

Permalink
Explode column types and generate converters (dotnet#2857)
Browse files Browse the repository at this point in the history
* Explode column types and generate converters

* Clean this

* sq

* sq

* Cherry pick for next commit

* sq

* Undo unnecessary change
  • Loading branch information
Prashanth Govindarajan authored Mar 4, 2020
1 parent 355d3fb commit 9e10004
Show file tree
Hide file tree
Showing 22 changed files with 1,772 additions and 381 deletions.
23 changes: 23 additions & 0 deletions src/Microsoft.Data.Analysis/BooleanDataFrameColumn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Data.Analysis
{
public partial class BooleanDataFrameColumn : PrimitiveDataFrameColumn<bool>
{
public BooleanDataFrameColumn(string name, IEnumerable<bool?> values) : base(name, values) { }

public BooleanDataFrameColumn(string name, IEnumerable<bool> values) : base(name, values) { }

public BooleanDataFrameColumn(string name, long length = 0) : base(name, length) { }

public BooleanDataFrameColumn(string name, ReadOnlyMemory<byte> buffer, ReadOnlyMemory<byte> nullBitMap, int length = 0, int nullCount = 0) : base(name, buffer, nullBitMap, length, nullCount) { }

internal BooleanDataFrameColumn(string name, PrimitiveColumnContainer<bool> values) : base(name, values) { }
}
}
23 changes: 23 additions & 0 deletions src/Microsoft.Data.Analysis/ByteDataFrameColumn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Data.Analysis
{
public partial class ByteDataFrameColumn : PrimitiveDataFrameColumn<byte>
{
public ByteDataFrameColumn(string name, IEnumerable<byte?> values) : base(name, values) { }

public ByteDataFrameColumn(string name, IEnumerable<byte> values) : base(name, values) { }

public ByteDataFrameColumn(string name, long length = 0) : base(name, length) { }

public ByteDataFrameColumn(string name, ReadOnlyMemory<byte> buffer, ReadOnlyMemory<byte> nullBitMap, int length = 0, int nullCount = 0) : base(name, buffer, nullBitMap, length, nullCount) { }

internal ByteDataFrameColumn(string name, PrimitiveColumnContainer<byte> values) : base(name, values) { }
}
}
23 changes: 23 additions & 0 deletions src/Microsoft.Data.Analysis/CharDataFrameColumn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Data.Analysis
{
public partial class CharDataFrameColumn : PrimitiveDataFrameColumn<char>
{
public CharDataFrameColumn(string name, IEnumerable<char?> values) : base(name, values) { }

public CharDataFrameColumn(string name, IEnumerable<char> values) : base(name, values) { }

public CharDataFrameColumn(string name, long length = 0) : base(name, length) { }

public CharDataFrameColumn(string name, ReadOnlyMemory<byte> buffer, ReadOnlyMemory<byte> nullBitMap, int length = 0, int nullCount = 0) : base(name, buffer, nullBitMap, length, nullCount) { }

internal CharDataFrameColumn(string name, PrimitiveColumnContainer<char> values) : base(name, values) { }
}
}
74 changes: 74 additions & 0 deletions src/Microsoft.Data.Analysis/ColumnArithmeticTemplate.ttinclude
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,56 @@
return $"{keyword} (typeof(T) == typeof({type.TypeName}))";
}

// A way to discern implicit conversions. For ex: short has primitivitty 2. int has primitivitty 3. primitivitty(short) + primitivitty(int) > 2 * primitivitty(short) implying that a conversion has to take place
public Dictionary<string, int> primitiveTypeToPrimitivityLevelMap = new Dictionary<string, int> {
{"byte", 1},
{"sbyte", 1},
{"short", 2},
{"ushort", 2},
{"int", 3},
{"uint", 3},
{"long", 4},
{"ulong", 4},
{"float", 5},
{"double", 6},
{"decimal", 7}
};

public string GetCapitalizedPrimitiveTypes(string type)
{
string typeFirstCharUpper;
if (type.First() == 'u' || type == "sbyte")
{
typeFirstCharUpper = type[0].ToString().ToUpper() + type[1].ToString().ToUpper() + type.Substring(2);
}
else
{
typeFirstCharUpper = type[0].ToString().ToUpper() + type.Substring(1);
}
return typeFirstCharUpper;
}

public bool IsMixedSignedAndUnsignedTypePair(string t1, string t2)
{
if (t1 == "byte" && t2 == "sbyte")
{
return true;
}
if (t2 == "byte" && t1 == "sbyte")
{
return true;
}
if (("u" + t1) == t2)
{
return true;
}
if (("u" + t2) == t1)
{
return true;
}
return false;
}

public TypeConfiguration[] typeConfiguration = new []
{
new TypeConfiguration("bool", oneLiteral:"true", zeroLiteral:"false", supportsNumeric: false, unsupportedMethods: new[] {"LeftShift", "RightShift"}),
Expand All @@ -55,6 +105,30 @@
new TypeConfiguration("ushort", classPrefix:"UShort", unsupportedMethods: new[] {"UnaryMinus", "All", "Any"})
};

public string GetBinaryOperationReturnType(TypeConfiguration t1, TypeConfiguration t2)
{
int t1Level;
if (!primitiveTypeToPrimitivityLevelMap.TryGetValue(t1.TypeName, out t1Level))
{
throw new Exception("Unknown type");
}
int t2Level;
if (!primitiveTypeToPrimitivityLevelMap.TryGetValue(t2.TypeName, out t2Level))
{
throw new Exception("Unknown type");
}
if (t1Level + t2Level <= 2 * t1Level)
{
return t1.TypeName;
}
if (t1Level + t2Level <= 2 * t2Level)
{
return t2.TypeName;
}
throw new Exception("Bug in GetBinaryOperationReturnType");
return "";
}

public enum MethodType
{
Unary,
Expand Down
Loading

0 comments on commit 9e10004

Please sign in to comment.