Skip to content

Commit 2298bc5

Browse files
committed
Adding sample for shortest path
Added a sample for shortest path. This is a new feature introduced in SQL Server 2019 CTP3.1
1 parent b9253c6 commit 2298bc5

4 files changed

Lines changed: 192 additions & 0 deletions

File tree

61.6 KB
Loading
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# SHORTEST_PATH
2+
SQL Server (starting with SQL Server 2017) and Azure SQL Database now let you create a graph database, to hold your entities and complex many to many relationships. There are several examples on github which demonstrate how the new graph features work. This example shows how you can use SHORTEST_PATH function to write a shortest path or arbitrary length traversal query. This feature is available for public preview with SQL Server 2019 CTP3.1
3+
4+
5+
To demonstrate the functionality, we will be using AdventureWorks2014 as our sample database.
6+
7+
## Contents
8+
[About this sample](#about-this-sample)<br/>
9+
[Before you begin](#before-you-begin)<br/>
10+
[Run this sample](#run-this-sample)<br/>
11+
[Related links](#related-links)
12+
13+
## About this sample
14+
1. **Applies to:**
15+
- SQL Server 2019 CTP3.1 (or higher)
16+
2. **Demos:**
17+
- Build and populate graph node and edge tables
18+
- Use SHORTEST_PATH function to compute shortest path
19+
- between 2 given nodes.
20+
- starting from a given node to all other nodes in the graph.
21+
- starting from a given node to all other nodes, which are 1 to 3 hops away from the start node.
22+
3. **Workload:** Queries executed on [AdventureWorks2014](https://github.com/Microsoft/sql-server-samples/releases/download/adventureworks/AdventureWorks2014.bak)
23+
4. **Programming Language:** T-SQL
24+
5. **Author:** Shreya Verma
25+
26+
## Before you begin
27+
To run these demo scripts, you need the following prerequisites.
28+
29+
**Account and Software prerequisites:**
30+
31+
1. SQL Server 2019 CTP3.1 (or higher)
32+
2. SQL Server Management Studio 18.x (or higher)
33+
34+
## Run This Sample
35+
36+
### Setup
37+
38+
#### SQL Server Setup
39+
40+
1. Download [**AdventureWorks2014.bak**](https://github.com/Microsoft/sql-server-samples/releases/download/adventureworks/AdventureWorks2014.bak)
41+
42+
2. Launch SQL Server Management Studio, connect to your SQL Server instance (2019) and restore **AdventureWorks2014.bak**. This document describes how to [Restore a Database Backup Using SSMS](https://docs.microsoft.com/en-us/sql/relational-databases/backup-restore/restore-a-database-backup-using-ssms).
43+
44+
45+
#### Running the Sample Scripts
46+
1. Once the database is restored, run the *setup-BOMGraph.sql* script to create the necessary graph node and edge tables. We will be using these tables to run our sample queries. The setup file creates Product node table and IsPartOf edge table, which represents the BOM for products in AdventureWorks Cycles manufacturing pipeline.
47+
48+
![AdventureWorksBOM](AdventureWorksBOM.png)
49+
50+
51+
2. Run the *ShortestPath.sql* script to run some SHORTEST_PATH queries. The script has following example queries:
52+
1. Starting from a given node, find shortest path to all the other nodes in the graph.
53+
2. Starting from a given node, find shortest path to all the other nodes in the graph, which are 1 -3 hops away from the start node.
54+
3. Find shortest path between 2 given nodes (start and end node)
55+
56+
57+
## Related Links
58+
59+
For more information about Graph DB in SQL Server 2017, see these articles:
60+
61+
1. [Graph processing with SQL Server and Azure SQL Database](https://docs.microsoft.com/en-us/sql/relational-databases/graphs/sql-graph-overview)
62+
63+
2. [SQL Graph Architecture](https://docs.microsoft.com/en-us/sql/relational-databases/graphs/sql-graph-architecture)
64+
65+
3. [Using MATCH in MERGE DML](https://blogs.msdn.microsoft.com/sqlserverstorageengine/2018/07/16/match-support-in-merge-dml-for-graph-tables/)
66+
4. [Edge Constraints on graph tables](https://blogs.msdn.microsoft.com/sqlserverstorageengine/2018/09/28/public-preview-of-graph-edge-constraints-on-sql-server-2019/)
67+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
USE AdventureWorks2014
2+
GO
3+
4+
------------------------------------------------------------------------------------
5+
-- Find all assemblies that require a Bearing Ball in the bill of materials
6+
-- Or, Starting with a given node, find shortest paths to all the others nodes in
7+
-- the graph
8+
------------------------------------------------------------------------------------
9+
10+
SELECT
11+
P1.ProductID,
12+
P1.Name,
13+
STRING_AGG(P2.Name,'->') WITHIN GROUP (GRAPH PATH) AS [Assembly],
14+
LAST_VALUE(P2.ProductID) WITHIN GROUP (GRAPH PATH) AS [Final ProductID]
15+
FROM
16+
PRODUCT P1,
17+
PRODUCT FOR PATH P2,
18+
ISPARTOF FOR PATH IPO
19+
WHERE
20+
MATCH(SHORTEST_PATH(P1(-(IPO)->P2)+))
21+
AND P1.ProductID = 2
22+
ORDER BY P1.ProductID
23+
24+
------------------------------------------------------------------------------------
25+
-- Find all products which are up to 3 levels away from Bearing Ball in BOM hierarchy
26+
-- Or, find shortest path from a given start node to all other nodes in the graph,
27+
-- which are 1-3 hops away from the start node.
28+
------------------------------------------------------------------------------------
29+
30+
SELECT
31+
P1.ProductID,
32+
P1.Name,
33+
STRING_AGG(P2.Name,'->') WITHIN GROUP (GRAPH PATH) AS [Assembly],
34+
LAST_VALUE(P2.ProductID) WITHIN GROUP (GRAPH PATH) AS [Final ProductID],
35+
COUNT(P2.ProductID) WITHIN GROUP (GRAPH PATH) AS Levels
36+
FROM
37+
PRODUCT P1,
38+
PRODUCT FOR PATH P2,
39+
ISPARTOF FOR PATH IPO
40+
WHERE
41+
MATCH(SHORTEST_PATH(P1(-(IPO)->P2){1,3}))
42+
AND P1.ProductID = 2
43+
ORDER BY P1.ProductID
44+
45+
46+
------------------------------------------------------------------------------------
47+
-- Find shortest path between 2 given nodes (start node and end node)
48+
-- Note that, to apply filter on the end node, we have to use a subquery
49+
-- but the filter applied on end node in outer query is pushed down while
50+
-- computing the shortest path.
51+
------------------------------------------------------------------------------------
52+
53+
SELECT
54+
ProductID, Name, [Assembly], FinalProductID, Levels
55+
FROM (
56+
SELECT
57+
P1.ProductID,
58+
P1.Name,
59+
STRING_AGG(P2.Name,'->') WITHIN GROUP (GRAPH PATH) AS [Assembly],
60+
LAST_VALUE(P2.ProductID) WITHIN GROUP (GRAPH PATH) AS FinalProductID,
61+
COUNT(P2.ProductID) WITHIN GROUP (GRAPH PATH) AS Levels
62+
FROM
63+
PRODUCT P1,
64+
PRODUCT FOR PATH P2,
65+
ISPARTOF FOR PATH IPO
66+
WHERE
67+
MATCH(SHORTEST_PATH(P1(-(IPO)->P2)+))
68+
AND P1.ProductID = 2
69+
) AS Q
70+
WHERE Q.FinalProductID = 768
71+
72+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
USE AdventureWorks2014
2+
GO
3+
4+
-- create product node
5+
CREATE TABLE dbo.Product (
6+
ProductID INT,
7+
Name NVARCHAR(50),
8+
ProductNumber NVARCHAR(25),
9+
StandardCost money,
10+
ListPrice money,
11+
DaysToManu INT,
12+
Weight decimal(8,2),
13+
SellStartDate datetime,
14+
SellEndDate datetime,
15+
) AS NODE;
16+
GO
17+
18+
-- populate data into product node from Production.Product table
19+
INSERT INTO dbo.Product
20+
SELECT
21+
p.ProductID,
22+
p.Name,
23+
p.ProductNumber,
24+
p.StandardCost,
25+
p.ListPrice,
26+
p.Weight,
27+
p.DaysToManufacture,
28+
p.SellStartDate,
29+
p.SellEndDate
30+
FROM Production.Product p;
31+
GO
32+
33+
-- create IsPartOf edge
34+
CREATE TABLE IsPartOf (PerAssemblyQty decimal(8,2)) AS EDGE;
35+
go
36+
37+
-- Insert into isPartOf edge BOM hierarchy links
38+
INSERT INTO IsPartOf
39+
(
40+
$from_id,
41+
$to_id,
42+
PerAssemblyQty
43+
)
44+
SELECT
45+
P.$node_id,
46+
PP.$node_id,
47+
BOM.PerAssemblyQty
48+
FROM
49+
dbo.Product AS P
50+
JOIN
51+
Production.BillOfMaterials AS BOM ON P.ProductId = BOM.ComponentID
52+
JOIN
53+
dbo.Product AS PP ON PP.ProductID = BOM.ProductAssemblyID

0 commit comments

Comments
 (0)