|
1 | 1 | # 10 pts
|
| 2 | +# This is something done by some TAs in some start-ups before. Once they got a wrong result |
| 3 | +# in a query, they split the query into parts so as to find which one goes wrong... |
| 4 | +# |
| 5 | +# This ultimate goal is to run an extra-complex query to compute the shortest path within x |
| 6 | +# steps from each of the vertex. We will start from the table scan. |
| 7 | + |
| 8 | +statement ok |
| 9 | +CREATE TABLE graph(src int, dst int, src_label VARCHAR(8), dst_label VARCHAR(8), distance int); |
| 10 | + |
| 11 | +statement ok |
| 12 | +INSERT INTO graph SELECT * FROM __mock_graph; |
| 13 | + |
| 14 | +query rowsort |
| 15 | +select count(distance) from __mock_graph; |
| 16 | +---- |
| 17 | +90 |
| 18 | + |
| 19 | +query rowsort |
| 20 | +-- sanity check |
| 21 | +select count(distance), sum(distance) from ( |
| 22 | + -- find shortest path within 1 neighbor |
| 23 | + select src, dst, src_label, dst_label, min(distance) as distance from ( |
| 24 | + -- with in all the paths |
| 25 | + select |
| 26 | + left_graph.src as src, |
| 27 | + right_graph.dst as dst, |
| 28 | + left_graph.src_label as src_label, |
| 29 | + right_graph.dst_label as dst_label, |
| 30 | + (left_graph.distance + right_graph.distance) as distance |
| 31 | + from |
| 32 | + graph left_graph inner join graph right_graph |
| 33 | + on left_graph.dst = right_graph.src |
| 34 | + ) group by src, dst, src_label, dst_label |
| 35 | +); |
| 36 | +---- |
| 37 | +100 200 |
| 38 | + |
| 39 | +statement ok |
| 40 | +select * from ( |
| 41 | +select |
| 42 | + left_graph.src as src, |
| 43 | + right_graph.dst as dst, |
| 44 | + left_graph.src_label as src_label, |
| 45 | + right_graph.dst_label as dst_label, |
| 46 | + (left_graph.distance + right_graph.distance) as distance |
| 47 | +from ( |
| 48 | + -- find shortest path within 1 neighbor |
| 49 | + select src, dst, src_label, dst_label, min(distance) as distance from ( |
| 50 | + -- with in all the paths |
| 51 | + select |
| 52 | + left_graph.src as src, |
| 53 | + right_graph.dst as dst, |
| 54 | + left_graph.src_label as src_label, |
| 55 | + right_graph.dst_label as dst_label, |
| 56 | + (left_graph.distance + right_graph.distance) as distance |
| 57 | + from |
| 58 | + graph left_graph inner join graph right_graph |
| 59 | + on left_graph.dst = right_graph.src |
| 60 | + ) group by src, dst, src_label, dst_label |
| 61 | +) left_graph inner join graph right_graph on left_graph.dst = right_graph.src |
| 62 | +) order by src, dst; |
| 63 | + |
| 64 | +query rowsort |
| 65 | +-- sanity check |
| 66 | +select count(distance), sum(distance) from ( |
| 67 | + -- find shortest path within 2 neighbors |
| 68 | + select src, dst, src_label, dst_label, min(distance) as distance from ( |
| 69 | + -- with in all the paths |
| 70 | + select |
| 71 | + left_graph.src as src, |
| 72 | + right_graph.dst as dst, |
| 73 | + left_graph.src_label as src_label, |
| 74 | + right_graph.dst_label as dst_label, |
| 75 | + (left_graph.distance + right_graph.distance) as distance |
| 76 | + from ( |
| 77 | + -- find shortest path within 1 neighbor |
| 78 | + select src, dst, src_label, dst_label, min(distance) as distance from ( |
| 79 | + -- with in all the paths |
| 80 | + select |
| 81 | + left_graph.src as src, |
| 82 | + right_graph.dst as dst, |
| 83 | + left_graph.src_label as src_label, |
| 84 | + right_graph.dst_label as dst_label, |
| 85 | + (left_graph.distance + right_graph.distance) as distance |
| 86 | + from |
| 87 | + graph left_graph inner join graph right_graph |
| 88 | + on left_graph.dst = right_graph.src |
| 89 | + ) group by src, dst, src_label, dst_label |
| 90 | + ) left_graph inner join graph right_graph on left_graph.dst = right_graph.src |
| 91 | + ) group by src, dst, src_label, dst_label |
| 92 | +); |
| 93 | +---- |
| 94 | +100 300 |
| 95 | + |
| 96 | +query rowsort |
| 97 | +-- sanity check |
| 98 | +select count(distance), sum(distance) from ( |
| 99 | + -- find shortest path within 3 neighbors |
| 100 | + select src, dst, src_label, dst_label, min(distance) as distance from ( |
| 101 | + -- with in all the paths |
| 102 | + select |
| 103 | + left_graph.src as src, |
| 104 | + right_graph.dst as dst, |
| 105 | + left_graph.src_label as src_label, |
| 106 | + right_graph.dst_label as dst_label, |
| 107 | + (left_graph.distance + right_graph.distance) as distance |
| 108 | + from ( |
| 109 | + -- find shortest path within 2 neighbors |
| 110 | + select src, dst, src_label, dst_label, min(distance) as distance from ( |
| 111 | + -- with in all the paths |
| 112 | + select |
| 113 | + left_graph.src as src, |
| 114 | + right_graph.dst as dst, |
| 115 | + left_graph.src_label as src_label, |
| 116 | + right_graph.dst_label as dst_label, |
| 117 | + (left_graph.distance + right_graph.distance) as distance |
| 118 | + from ( |
| 119 | + -- find shortest path within 1 neighbor |
| 120 | + select src, dst, src_label, dst_label, min(distance) as distance from ( |
| 121 | + -- with in all the paths |
| 122 | + select |
| 123 | + left_graph.src as src, |
| 124 | + right_graph.dst as dst, |
| 125 | + left_graph.src_label as src_label, |
| 126 | + right_graph.dst_label as dst_label, |
| 127 | + (left_graph.distance + right_graph.distance) as distance |
| 128 | + from |
| 129 | + graph left_graph inner join graph right_graph |
| 130 | + on left_graph.dst = right_graph.src |
| 131 | + ) group by src, dst, src_label, dst_label |
| 132 | + ) left_graph inner join graph right_graph on left_graph.dst = right_graph.src |
| 133 | + ) group by src, dst, src_label, dst_label |
| 134 | + ) left_graph inner join graph right_graph on left_graph.dst = right_graph.src |
| 135 | + ) group by src, dst, src_label, dst_label |
| 136 | +); |
| 137 | +---- |
| 138 | +100 400 |
| 139 | + |
| 140 | +query |
| 141 | +select * from ( |
| 142 | + select src, dst, src_label, dst_label, min(distance) as distance from ( |
| 143 | + -- with in all the paths |
| 144 | + select |
| 145 | + left_graph.src as src, |
| 146 | + right_graph.dst as dst, |
| 147 | + left_graph.src_label as src_label, |
| 148 | + right_graph.dst_label as dst_label, |
| 149 | + (left_graph.distance + right_graph.distance) as distance |
| 150 | + from ( |
| 151 | + -- find shortest path within 2 neighbors |
| 152 | + select src, dst, src_label, dst_label, min(distance) as distance from ( |
| 153 | + -- with in all the paths |
| 154 | + select |
| 155 | + left_graph.src as src, |
| 156 | + right_graph.dst as dst, |
| 157 | + left_graph.src_label as src_label, |
| 158 | + right_graph.dst_label as dst_label, |
| 159 | + (left_graph.distance + right_graph.distance) as distance |
| 160 | + from ( |
| 161 | + -- find shortest path within 1 neighbor |
| 162 | + select src, dst, src_label, dst_label, min(distance) as distance from ( |
| 163 | + -- with in all the paths |
| 164 | + select |
| 165 | + left_graph.src as src, |
| 166 | + right_graph.dst as dst, |
| 167 | + left_graph.src_label as src_label, |
| 168 | + right_graph.dst_label as dst_label, |
| 169 | + (left_graph.distance + right_graph.distance) as distance |
| 170 | + from |
| 171 | + graph left_graph inner join graph right_graph |
| 172 | + on left_graph.dst = right_graph.src |
| 173 | + ) group by src, dst, src_label, dst_label |
| 174 | + ) left_graph inner join graph right_graph on left_graph.dst = right_graph.src |
| 175 | + ) group by src, dst, src_label, dst_label |
| 176 | + ) left_graph inner join graph right_graph on left_graph.dst = right_graph.src |
| 177 | + ) group by src, dst, src_label, dst_label |
| 178 | +) where src = 0 order by dst limit 10; |
| 179 | +---- |
| 180 | +0 0 000 000 4 |
| 181 | +0 1 000 001 4 |
| 182 | +0 2 000 002 4 |
| 183 | +0 3 000 003 4 |
| 184 | +0 4 000 004 4 |
| 185 | +0 5 000 005 4 |
| 186 | +0 6 000 006 4 |
| 187 | +0 7 000 007 4 |
| 188 | +0 8 000 008 4 |
| 189 | +0 9 000 009 4 |
| 190 | + |
| 191 | +# In the future, we will construct some custom graphs with different link weight. |
| 192 | +# But for now, that's the end of the test! |
| 193 | +# TODO: recursive CTE 🤤 |
0 commit comments