Skip to content

Commit b993b5e

Browse files
authored
Merge pull request github#12298 from MathiasVP/join-order-metric-query-with-more-rows
QL: Output more rows in the join order query
2 parents 6141c96 + 9ee078d commit b993b5e

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

ql/ql/src/codeql_ql/StructuredLogs.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ module KindPredicatesLog {
273273
then result = this.getPredicateName()
274274
else result = "<Summary event>"
275275
}
276+
277+
Array getRA(string ordering) { result = this.getObject("ra").getArray(ordering) }
276278
}
277279

278280
class SentinelEmpty extends SummaryEvent {
@@ -288,8 +290,12 @@ module KindPredicatesLog {
288290

289291
string getRAReference() { result = this.getString("raReference") }
290292

293+
Array getCounts() { result = this.getArray("counts") }
294+
291295
float getCount(int i) { result = getRanked(this.getArray("counts"), i) }
292296

297+
Array getDuplicationPercentage() { result = this.getArray("duplicationPercentages") }
298+
293299
float getDuplicationPercentage(int i) {
294300
result = getRanked(this.getArray("duplicationPercentages"), i)
295301
}
@@ -327,6 +333,12 @@ module KindPredicatesLog {
327333
string getPosition() { result = this.getString("position") }
328334

329335
Predicate getPredicate() { result = getPredicateFromPosition(this.getPosition()) }
336+
337+
/**
338+
* Gets the RA for this event. Unlike recursive predicates, a COMPUTE_SIMPLE
339+
* event only has one pipeline ordering (and it's named "pipeline").
340+
*/
341+
Array getRA() { result = this.getObject("ra").getArray("pipeline") }
330342
}
331343

332344
/** Gets the `index`'th event that's evaluated by `recursive`. */

ql/ql/src/queries/performance/LargeJoinOrder.ql

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,37 @@ import KindPredicatesLog
1616
float getBadness(ComputeSimple simple) {
1717
exists(float maxTupleCount, float resultSize, float largestDependency, float denom |
1818
resultSize = simple.getResultSize() and
19-
maxTupleCount = max(simple.getPipelineRun().getCount(_)) and
19+
extractInformation(simple, maxTupleCount, _, _, _) and
2020
largestDependency = max(simple.getDependencies().getADependency().getResultSize()) and
2121
denom = resultSize.maximum(largestDependency) and
2222
denom > 0 and // avoid division by zero (which would create a NaN result).
2323
result = maxTupleCount / denom
2424
)
2525
}
2626

27-
from ComputeSimple evt, float f
28-
where f = getBadness(evt) and f > 1.5
29-
select evt, f order by f desc
27+
pragma[nomagic]
28+
predicate hasPipelineRun(ComputeSimple simple, PipeLineRun run) { run = simple.getPipelineRun() }
29+
30+
predicate extractInformation(
31+
ComputeSimple simple, float maxTupleCount, Array tuples, Array duplicationPercentages, Array ra
32+
) {
33+
exists(PipeLineRun run |
34+
hasPipelineRun(simple, run) and
35+
maxTupleCount = max(run.getCount(_)) and
36+
tuples = run.getCounts() and
37+
duplicationPercentages = run.getDuplicationPercentage() and
38+
ra = simple.getRA()
39+
)
40+
}
41+
42+
from
43+
ComputeSimple evt, float badness, float maxTupleCount, Array tuples, Array duplicationPercentages,
44+
Array ra, int index
45+
where
46+
badness = getBadness(evt) and
47+
badness > 1.5 and
48+
extractInformation(evt, maxTupleCount, tuples, duplicationPercentages, ra)
49+
select evt.getPredicateName() as predicate_name, badness, index,
50+
tuples.getFloat(index) as tuple_count,
51+
duplicationPercentages.getFloat(index) as duplication_percentage, ra.getString(index) order by
52+
badness desc

0 commit comments

Comments
 (0)