Skip to content

Commit 20a1e56

Browse files
authored
Merge pull request #3 from yorek/master
updated documentation
2 parents cba3faa + b0b6e74 commit 20a1e56

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

samples/features/sql-clr/TransitiveClosure/README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Implementing Transitive Closure Clustering in SQL Server using CLR UDF
22
SQL Database don't have built-in support for transitive closure clustering, so the only workaround is to implement this algorithm in .Net framework and expose it as T-SQL function.
3+
A discussion on the problem, the algorithm and a pure T-SQL based solution can be found here:
4+
- [Transitive Closure Clustering with SQL Server, UDA and JSON](https://medium.com/@mauridb/transitive-closure-clustering-with-sql-server-uda-and-json-dade18953fd2)
5+
- [T-SQL Puzzle Challenge Grouping Connected Items](http://www.itprotoday.com/microsoft-sql-server/t-sql-puzzle-challenge-grouping-connected-items)
6+
37
This code sample demonstrates how to create CLR User-Defined aggregate that implements clustering.
48

59
### Contents
@@ -17,7 +21,7 @@ This code sample demonstrates how to create CLR User-Defined aggregate that impl
1721
2. **Key features:**
1822
- CLR, JSON
1923
3. **Programming Language:** .NET C#
20-
4. **Author:** Davide Mauri, Jovan Popovic [jovanpop-msft]
24+
4. **Author:** [Davide Mauri](https://github.com/yorek), Jovan Popovic [jovanpop-msft]
2125

2226
<a name=build-functions></a>
2327

@@ -65,30 +69,31 @@ Once you create the assembly and expose the aggregate, you can use it to cluster
6569
declare @edges table(n1 int, n2 int);
6670
6771
insert into @edges
68-
values (1,2),(2,3),(3,4),(4,5),(2,21),(2,22),
69-
              (7,8),(8,9),(9,10);
72+
values
73+
(1,2),(2,3),(3,4),(4,5),(2,21),(2,22),
74+
(7,8),(8,9),(9,10);
7075
7176
select TC.CLUSTERING(n1,n2)
7277
from @edges;
7378
```
7479
The result will be JSON document that groups the numbers that belong to the same cluster.
7580
```javascript
7681
{
77-
"0":[1,2,3,4,5,21,22],
78-
"1":[7,8,9,10]
82+
"0":[1,2,3,4,5,21,22],
83+
"1":[7,8,9,10]
7984
}
8085
```
8186
You can transform this JSON document into relational formatusing **OPENJSON** function:
8287
```
8388
select cluster = [key], elements = value
8489
from openjson(
85-
       (select TC.CLUSTERING(n1,n2) from @edges)
90+
(select TC.CLUSTERING(n1,n2) from @edges)
8691
);
8792
```
8893
The result of this query is:
8994

9095
|cluster|elements|
91-
|----|---|
96+
|---|---|
9297
|0|[1,2,3,4,5,21,22]|
9398
|1|[7,8,9,10]|
9499

samples/features/sql-clr/TransitiveClosure/TransitiveClosureAggregate.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ namespace TransitiveClosure
1414
public class Group: IEnumerable<int>
1515
{
1616
private int? _groupRoot = null;
17-
17+
1818
private Dictionary<int, bool> _group = new Dictionary<int, bool>();
19-
2019
public Dictionary<int, bool>.KeyCollection Elements => _group.Keys;
21-
2220
public int Count => _group.Keys.Count;
2321

2422
public bool ContainsElement(int element)
@@ -89,13 +87,12 @@ public override string ToString()
8987
public class GroupSet: IEnumerable<Group>
9088
{
9189
private List<Group> _groupSet = new List<Group>();
90+
// This keeps about the distinct numbers and in which group they are
9291
private Dictionary<int, Group> _numbers = new Dictionary<int, Group>();
9392
private int _merges = 0;
9493

9594
public int Groups => _groupSet.Count;
96-
9795
public int Numbers => _numbers.Count;
98-
9996
public int Merges => _merges;
10097

10198
public void Add(Group group)
@@ -212,9 +209,7 @@ public class Aggregate : IBinarySerialize
212209
private GroupSet _groupSet;
213210

214211
public int Groups => _groupSet.Groups;
215-
216212
public int Numbers => _groupSet.Numbers;
217-
218213
public int Merges => _groupSet.Merges;
219214

220215
public void Init()

0 commit comments

Comments
 (0)