Skip to content

Commit 9c8b8df

Browse files
committed
QL: Output more rows in the join order query.
1 parent 48bf15f commit 9c8b8df

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

ql/ql/src/codeql_ql/StructuredLogs.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ module KindPredicatesLog {
269269
then result = this.getPredicateName()
270270
else result = "<Summary event>"
271271
}
272+
273+
Array getRa(string ordering) { result = this.getObject("ra").getArray(ordering) }
272274
}
273275

274276
class SentinelEmpty extends SummaryEvent {
@@ -284,8 +286,12 @@ module KindPredicatesLog {
284286

285287
string getRAReference() { result = this.getString("raReference") }
286288

289+
Array getCounts() { result = this.getArray("counts") }
290+
287291
float getCount(int i) { result = getRanked(this.getArray("counts"), i) }
288292

293+
Array getDuplicationPercentage() { result = this.getArray("duplicationPercentages") }
294+
289295
float getDuplicationPercentage(int i) {
290296
result = getRanked(this.getArray("duplicationPercentages"), i)
291297
}
@@ -323,6 +329,12 @@ module KindPredicatesLog {
323329
string getPosition() { result = this.getString("position") }
324330

325331
Predicate getPredicate() { result = getPredicateFromPosition(this.getPosition()) }
332+
333+
/**
334+
* Gets the RA for this event. Unlike recursive predicates, a COMPUTE_SIMPLE
335+
* event only has one pipeline ordering (and it's named "pipeline").
336+
*/
337+
Array getRa() { result = this.getObject("ra").getArray("pipeline") }
326338
}
327339

328340
class ComputeRecursive extends SummaryEvent {

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,36 @@ 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, int pipelineIndex, Array tuples,
32+
Array duplicationPercentages, Array ra
33+
) {
34+
exists(PipeLineRun run |
35+
hasPipelineRun(simple, run) and
36+
maxTupleCount = max(run.getCount(_)) and
37+
pragma[only_bind_out](tuples.getFloat(pipelineIndex)) = maxTupleCount and
38+
tuples = run.getCounts() and
39+
duplicationPercentages = run.getDuplicationPercentage() and
40+
ra = simple.getRa()
41+
)
42+
}
43+
44+
from
45+
ComputeSimple evt, float f, float maxTupleCount, int pipelineIndex, Array tuples,
46+
Array duplicationPercentages, Array ra
47+
where
48+
f = getBadness(evt) and
49+
f > 1.5 and
50+
extractInformation(evt, maxTupleCount, pipelineIndex, tuples, duplicationPercentages, ra)
51+
select evt, f, pipelineIndex, tuples, duplicationPercentages, ra order by f desc

0 commit comments

Comments
 (0)