Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Kanwulf
Starting Member
11 Posts |
Posted - 2010-03-05 : 05:09:28
|
I have this simple UDA:using System;using System.Data;using System.Data.SqlClient;using System.Data.SqlTypes;using Microsoft.SqlServer.Server;using System.IO;using System.Text;[Serializable][Microsoft.SqlServer.Server.SqlUserDefinedAggregate( Format.UserDefined, MaxByteSize = 8000 )]public struct MY_AVG : IBinarySerialize{ private int m_uCount; private SqlDecimal m_uAvg; private SqlDecimal m_uValue; public void Init() { m_uCount = 0; m_uValue = 0; } public void Accumulate(SqlDecimal Value) { if (!Value.IsNull) { ++m_uCount; //number of values // m_uValue = (SqlDecimal)Value; m_uValue += Value; //m_uValue += (SqlDecimal)Value; } } public void Merge(MY_AVG Group) { this.m_uValue = Group.m_uValue; this.m_uCount = Group.m_uCount; } public SqlString Terminate() { return m_uAvg.ToSqlString(); } public void Read(BinaryReader r) { m_uAvg = r.ReadDecimal(); //precision 1 means m_sMedian = NULL if (m_uAvg.Precision == 1) { m_uAvg = new SqlDecimal(); } } public void Write(BinaryWriter w) { if (m_uCount != 0) { //m_uAvg = SqlDecimal.Divide(this.m_uValue, this.m_uCount); m_uAvg = this.m_uValue / this.m_uCount; w.Write((Decimal)this.m_uAvg); } }}I execute the following query:select dbo.MY_AVG(F585850) FROM T585879_1900_1 which returns NULL. The input table doesn't have NULL values in F585850 column. This query will use Merge method to gather all streams and produce the final result.If I use the MAXDOP hint the result returned is correct (200001)select dbo.MY_AVG(F585850) FROM T585879_1900_1 OPTION (MAXDOP 1)Obviously when using MAXDOP, the uda's Merge method is not called. Now, I set some breakpoints in the Merge method and I could notice that the m_uValue of the Group object is always NULL. The code is very simple.Any idea how can i fix this (without maxdop).Any suggestion would be appreciated.Ionut Hrubaru |
|
Kanwulf
Starting Member
11 Posts |
|
|
|
|