Skip to content

Commit beee2b0

Browse files
committed
feat: sort joins by occurrences
Signed-off-by: Andres Taylor <[email protected]>
1 parent 4ca9d3f commit beee2b0

File tree

2 files changed

+54
-58
lines changed

2 files changed

+54
-58
lines changed

go/summarize/summarize-keys.go

+29-33
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"slices"
2525
"sort"
2626
"strconv"
27-
"strings"
2827
"time"
2928

3029
"vitess.io/vitess/go/slice"
@@ -238,53 +237,50 @@ func renderTablesJoined(md *markdown.MarkDown, summary *keys.Output) {
238237
g.AddJoinPredicate(key, pred)
239238
}
240239
}
241-
ks := slices.Collect(maps.Keys(g))
242-
slices.SortFunc(ks, func(a, b graphKey) int {
243-
if a.Tbl1 == b.Tbl1 {
244-
return strings.Compare(a.Tbl2, b.Tbl2)
245-
}
246-
return strings.Compare(a.Tbl1, b.Tbl1)
247-
})
248240

249241
if len(g) > 0 {
250242
md.PrintHeader("Tables Joined", 2)
251243
}
252244

253-
// we really want the output to be deterministic
254-
tables := slices.Collect(maps.Keys(g))
255-
sort.Slice(tables, func(i, j int) bool {
256-
if tables[i].Tbl1 == tables[j].Tbl1 {
257-
return tables[i].Tbl2 < tables[j].Tbl2
258-
}
259-
return tables[i].Tbl1 < tables[j].Tbl1
260-
})
245+
type foo struct {
246+
Tbl1, Tbl2 string
247+
Occurrences int
248+
predicates []operators.JoinPredicate
249+
}
261250

262-
md.Println("```")
263-
for _, table := range tables {
264-
predicates := g[table]
265-
numberOfPreds := len(predicates)
266-
totalt := 0
251+
var joins []foo
252+
for tables, predicates := range g {
253+
occurrences := 0
267254
for _, count := range predicates {
268-
totalt += count
255+
occurrences += count
269256
}
270-
md.Printf("%s ↔ %s (Occurrences: %d)\n", table.Tbl1, table.Tbl2, totalt)
271-
272-
// we want the output to be deterministic
273-
preds := slices.Collect(maps.Keys(predicates))
274-
sort.Slice(preds, func(i, j int) bool {
275-
return preds[i].String() < preds[j].String()
257+
joinPredicates := slices.Collect(maps.Keys(predicates))
258+
sort.Slice(joinPredicates, func(i, j int) bool {
259+
return joinPredicates[i].String() < joinPredicates[j].String()
260+
})
261+
joins = append(joins, foo{
262+
Tbl1: tables.Tbl1,
263+
Tbl2: tables.Tbl2,
264+
Occurrences: occurrences,
265+
predicates: joinPredicates,
276266
})
267+
}
277268

278-
for _, predicate := range preds {
279-
count := predicates[predicate]
280-
numberOfPreds--
269+
sort.Slice(joins, func(i, j int) bool {
270+
return joins[i].Occurrences > joins[j].Occurrences
271+
})
272+
273+
md.Println("```")
274+
for _, join := range joins {
275+
md.Printf("%s ↔ %s (Occurrences: %d)\n", join.Tbl1, join.Tbl2, join.Occurrences)
276+
for i, pred := range join.predicates {
281277
var s string
282-
if numberOfPreds == 0 {
278+
if i == len(join.predicates)-1 {
283279
s = "└─"
284280
} else {
285281
s = "├─"
286282
}
287-
md.Printf("%s %s %d%%\n", s, predicate.String(), (count*100)/totalt)
283+
md.Printf("%s %s\n", s, pred.String())
288284
}
289285
md.NewLine()
290286
}

go/summarize/testdata/keys-summary.md

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Query Analysis Report
22

3-
**Date of Analysis**: 2024-01-01 01:02:03
4-
**Analyzed File**: `testdata/keys-log.json`
3+
**Date of Analysis**: 2024-11-09 10:28:32
4+
**Analyzed File**: `go/summarize/testdata/keys-log.json`
55

66
## Tables
77
|Table Name|Reads|Writes|
@@ -108,43 +108,43 @@
108108

109109
## Tables Joined
110110
```
111-
customernation (Occurrences: 3)
112-
└─ customer.c_nationkey = nation.n_nationkey 100%
111+
lineitemorders (Occurrences: 10)
112+
└─ lineitem.l_orderkey = orders.o_orderkey
113113
114114
customer ↔ orders (Occurrences: 7)
115-
└─ customer.c_custkey = orders.o_custkey 100%
115+
└─ customer.c_custkey = orders.o_custkey
116116
117-
customer ↔ supplier (Occurrences: 1)
118-
└─ customer.c_nationkey = supplier.s_nationkey 100%
119-
120-
lineitem ↔ lineitem (Occurrences: 2)
121-
├─ lineitem.l_orderkey = lineitem.l_orderkey 50%
122-
└─ lineitem.l_suppkey != lineitem.l_suppkey 50%
117+
nation ↔ supplier (Occurrences: 6)
118+
└─ nation.n_nationkey = supplier.s_nationkey
123119
124-
lineitem ↔ orders (Occurrences: 10)
125-
└─ lineitem.l_orderkey = orders.o_orderkey 100%
120+
lineitem ↔ supplier (Occurrences: 5)
121+
└─ lineitem.l_suppkey = supplier.s_suppkey
126122
127123
lineitem ↔ part (Occurrences: 3)
128-
└─ lineitem.l_partkey = part.p_partkey 100%
124+
└─ lineitem.l_partkey = part.p_partkey
129125
130-
lineitem ↔ partsupp (Occurrences: 2)
131-
├─ lineitem.l_partkey = partsupp.ps_partkey 50%
132-
└─ lineitem.l_suppkey = partsupp.ps_suppkey 50%
126+
customer ↔ nation (Occurrences: 3)
127+
└─ customer.c_nationkey = nation.n_nationkey
133128
134-
lineitem ↔ supplier (Occurrences: 5)
135-
└─ lineitem.l_suppkey = supplier.s_suppkey 100%
129+
lineitem ↔ lineitem (Occurrences: 2)
130+
├─ lineitem.l_orderkey = lineitem.l_orderkey
131+
└─ lineitem.l_suppkey != lineitem.l_suppkey
132+
133+
lineitem ↔ partsupp (Occurrences: 2)
134+
├─ lineitem.l_partkey = partsupp.ps_partkey
135+
└─ lineitem.l_suppkey = partsupp.ps_suppkey
136136
137137
nation ↔ region (Occurrences: 2)
138-
└─ nation.n_regionkey = region.r_regionkey 100%
138+
└─ nation.n_regionkey = region.r_regionkey
139139
140-
nation ↔ supplier (Occurrences: 6)
141-
└─ nation.n_nationkey = supplier.s_nationkey 100%
140+
partsupp ↔ supplier (Occurrences: 1)
141+
└─ partsupp.ps_suppkey = supplier.s_suppkey
142142
143143
part ↔ partsupp (Occurrences: 1)
144-
└─ part.p_partkey = partsupp.ps_partkey 100%
144+
└─ part.p_partkey = partsupp.ps_partkey
145145
146-
partsupp ↔ supplier (Occurrences: 1)
147-
└─ partsupp.ps_suppkey = supplier.s_suppkey 100%
146+
customer ↔ supplier (Occurrences: 1)
147+
└─ customer.c_nationkey = supplier.s_nationkey
148148
149149
```
150150
## Failures

0 commit comments

Comments
 (0)