SQL CLR 聚合在应用于大量数据时无法正确终止



我已经创建并使用了很多次连接值的SQL CLR聚合 - 它还按指定数字对值进行排序,并使用用户输入分隔符来连接它们。

我对大量数据使用了相同的聚合,并注意到没有使用分隔符 - 值是串联的,但没有分隔符。

经过大量测试,我发现在Terminate方法中,分隔符丢失/未读取。我使用硬编码分隔符仔细检查了这一点 - 一切正常。

我想我的ReadWrite方法(在处理大量数据时使用(有问题,但无法理解是什么。

以下是函数代码:

[Serializable]
[
Microsoft.SqlServer.Server.SqlUserDefinedAggregate
(
Microsoft.SqlServer.Server.Format.UserDefined,
IsInvariantToNulls = true,
IsInvariantToDuplicates = false,
IsInvariantToOrder = false,
IsNullIfEmpty = false,
MaxByteSize = -1
)
]
/// <summary>
/// Concatenates <int, string, string> values defining order using the specified number and using the given delimiter
/// </summary>
public class ConcatenateWithOrderAndDelimiter : Microsoft.SqlServer.Server.IBinarySerialize
{
private List<Tuple<int, string>> intermediateResult;
private string delimiter;
private bool isDelimiterNotDefined;
public void Init()
{
this.delimiter = ",";
this.isDelimiterNotDefined = true;
this.intermediateResult = new List<Tuple<int, string>>();
}
public void Accumulate(SqlInt32 position, SqlString text, SqlString delimiter)
{
if (this.isDelimiterNotDefined)
{
this.delimiter = delimiter.IsNull ? "," : delimiter.Value;
this.isDelimiterNotDefined = false;
}
if (!(position.IsNull || text.IsNull))
{
this.intermediateResult.Add(new Tuple<int, string>(position.Value, text.Value));
}
}
public void Merge(ConcatenateWithOrderAndDelimiter other)
{
this.intermediateResult.AddRange(other.intermediateResult);
}
public SqlString Terminate()
{
this.intermediateResult.Sort();
return new SqlString(String.Join(this.delimiter, this.intermediateResult.Select(tuple => tuple.Item2)));
}
public void Read(BinaryReader r)
{
if (r == null) throw new ArgumentNullException("r");
int count = r.ReadInt32();
this.intermediateResult = new List<Tuple<int, string>>(count);
for (int i = 0; i < count; i++)
{
this.intermediateResult.Add(new Tuple<int, string>(r.ReadInt32(), r.ReadString()));
}
this.delimiter = r.ReadString();
}
public void Write(BinaryWriter w)
{
if (w == null) throw new ArgumentNullException("w");
w.Write(this.intermediateResult.Count);
foreach (Tuple<int, string> record in this.intermediateResult)
{
w.Write(record.Item1);
w.Write(record.Item2);
}
w.Write(this.delimiter);
}
}

仅当使用并行性并且特定组分布在 1 个以上的线程上时,才会调用Merge()方法。在这种情况下,已调用Init(),并且 0 个或多个实例Accumulate()

因此,在并行的情况下,如果已调用Init()但尚未调用Accumulate(),则delimiter中的值将是Init()方法中设置的值。问题中的代码显示它被设置为,,但我怀疑这是后来在试图解决这个问题时添加的。当然,这假设逗号作为分隔符传入Accumulate()。或者,也许逗号总是在Init()中设置为默认值,但另一个字符是通过Accumulate()传入的,并且没有通过最终输出(问题中没有显示对 UDA 的特定调用,也不是不正确的输出,所以这里有一些歧义(。

虽然另一个答案中显示的修复似乎有效,但它不是一个通用的修复,因为可能存在当前对象至少被调用Accumulate()一次的情况,但合并到这个中的"其他"对象仍然是空的(也许没有匹配的行,或者调用Accumulate()时值未存储在本地的其他原因(。在这种情况下,当前对象将具有所需的分隔符,但"其他"对象仍将具有默认值。理想的解决方案是将isDelimiterNotDefined的值存储在Write()方法中,在Read()方法中再次将其取出,并将本地值与Merge()方法中的other.isDelimiterNotDefined进行比较,以便您可以确定是否应保留delimiter的本地值或其他值(取决于设置/定义哪个值(。

我发现了这个问题。这是在Merge方法中。它是:

public void Merge(ConcatenateWithOrderAndDelimiter other)
{
this.intermediateResult.AddRange(other.intermediateResult);
}

我把它改成:

public void Merge(ConcatenateWithOrderAndDelimiter other)
{
this.intermediateResult.AddRange(other.intermediateResult);
this.delimiter = other.delimiter;
}

似乎当数据被merge时,分隔符没有初始化。我想在上面的上下文中,所有this属性都是空的。

无论如何,我不会接受这个答案,因为如果有人能够解释内部发生的事情,那将是有帮助的。

最新更新