USQL 在用户定义类型上使用 ARRAY_AGG

USQL using ARRAY_AGG on user defined type

UDT

[SqlUserDefinedType(typeof(StudentHistoryFormatter))]
    public struct StudentHistory
    {
        public StudentHistory(int i, double? score, string status):this()
        {
            InstitutionId = i;
            Score = score;
            Status = status;
        }

        int InstitutionId { get; set; }

        double? Score {get; set; }

        string Status { get; set; }

        public string Value()
        {
            return string.Format("{0},{1},{2}", InstitutionId, Score, Status);
        }
    }

为了简单起见,我什至没有将 class 放在命名空间中。 我用 USQL 数据库注册了程序集

USQL

@history =
    EXTRACT InstitutionId int,
            Score double,
            Status string
    FROM @"CoreData\Institution\history.csv"
    USING Extractors.Csv();

@historyMap =
   SELECT InstitutionId,
               ARRAY_AGG<StudentHistory>(new StudentHistory(InstitutionId, Score, Status)) AS History
        FROM @history
        GROUP BY InstitutionId;

错误

Severity Code Description Project File Line Suppression State Error E_CSC_USER_INVALIDCOLUMNTYPE: 'Microsoft.Analytics.Types.Sql.SqlArray' cannot be used as column type. Description: The column type must be a supported scalar, complex or user defined type. Resolution: Ensure the column type is a supported type. For a user defined type, make sure the type is registered, the type name is fully qualified, and the required assembly is referenced by the script.

错误信息有点不清楚。目前,SqlArray 中唯一支持的项类型是 built-in 标量和复杂(SqlArray、SqlMap)类型。 User-defined 当前不支持类型。

您可以通过创建自己的 ArrayofUDT user-defined 类型或将 UDT 序列化为 byte[].

来对其建模

这里是后者的例子 ARRAY_AGG,假设你有一个 ToBinary() 方法允许你将 UDT 转换为 byte[]:

@data = SELECT key, ARRAY_AGG( myUDT.ToBinary() ) AS obj_array FROM @input GROUP BY key;

(有关对象序列化选项的信息,请参阅 How to convert an object to a byte array in C#

然后当你想取回对象时,你可以在对象数组上使用 CROSS APPLY EXPLODE 并使用 byte[] 来重新水合 UDT 实例。 Pseudo-code:

@objects = 
  SELECT myUDT.FromBinary(data) 
  FROM @data CROSS APPLY EXPLODE (obj_array) AS t(data);

请在 http://aka.ms/adlfeedback 为复杂类型的 UDT 提交功能请求。我将针对错误消息提交错误。