Skip to content

Commit bb692a7

Browse files
authored
Merge pull request github#12299 from github/calumgrant/qlql
Add classes and predicates to StructuredLogs
2 parents b993b5e + 41d88a4 commit bb692a7

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

ql/ql/src/codeql_ql/StructuredLogs.qll

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
private import ql
22
private import codeql_ql.ast.internal.TreeSitter
3+
private import experimental.RA
34

45
/** Gets a timestamp corresponding to the number of seconds since the date Semmle was founded. */
56
bindingset[d, h, m, s, ms]
@@ -67,6 +68,8 @@ class Array extends JSON::Array {
6768
float getFloat(int i) { result = this.getChild(i).(JSON::Number).getValue().toFloat() }
6869

6970
Array getArray(int i) { result = this.getChild(i) }
71+
72+
int getLength() { result = count(this.getChild(_)) }
7073
}
7174

7275
/**
@@ -78,6 +81,10 @@ private float getRanked(Array a, int i) {
7881
result = rank[i + 1](int j, float f | f = a.getFloat(j) and f >= 0 | f order by j)
7982
}
8083

84+
private string getRankedLine(Array a, int i) {
85+
result = rank[i + 1](int j, string s | s = a.getString(j) and s != "" | s order by j)
86+
}
87+
8188
module EvaluatorLog {
8289
class Entry extends Object { }
8390

@@ -274,7 +281,32 @@ module KindPredicatesLog {
274281
else result = "<Summary event>"
275282
}
276283

277-
Array getRA(string ordering) { result = this.getObject("ra").getArray(ordering) }
284+
RA getRA() { result = this.getObject("ra") }
285+
}
286+
287+
class PipeLine extends Array {
288+
RA ra;
289+
string raReference;
290+
291+
RA getRA() { result = ra }
292+
293+
string getRAReference() { result = raReference }
294+
295+
PipeLine() { this = ra.getArray(raReference) }
296+
297+
string getLineOfRA(int n) { result = getRankedLine(this, n) }
298+
299+
RAExpr getExpr(int n) { result.getPredicate() = this and result.getLine() = n }
300+
}
301+
302+
class RA extends Object {
303+
SummaryEvent evt;
304+
305+
SummaryEvent getEvent() { result = evt }
306+
307+
RA() { evt.getObject("ra") = this }
308+
309+
PipeLine getPipeLine(string name) { result = this.getArray(name) }
278310
}
279311

280312
class SentinelEmpty extends SummaryEvent {
@@ -290,6 +322,23 @@ module KindPredicatesLog {
290322

291323
string getRAReference() { result = this.getString("raReference") }
292324

325+
PipeLine getPipeLine() {
326+
exists(SummaryEvent evt | runs.getEvent() = evt |
327+
result = evt.getRA().getPipeLine(pragma[only_bind_into](this.getRAReference()))
328+
)
329+
}
330+
331+
float getCount(int i, string raLine) {
332+
result = this.getCount(i) and
333+
raLine = this.getPipeLine().getLineOfRA(pragma[only_bind_into](i))
334+
}
335+
336+
float getCountAndExpr(int i, RAExpr raExpr) {
337+
result = this.getCount(i) and
338+
raExpr.getPredicate() = this.getPipeLine() and
339+
raExpr.getLine() = i
340+
}
341+
293342
Array getCounts() { result = this.getArray("counts") }
294343

295344
float getCount(int i) { result = getRanked(this.getArray("counts"), i) }
@@ -338,7 +387,7 @@ module KindPredicatesLog {
338387
* Gets the RA for this event. Unlike recursive predicates, a COMPUTE_SIMPLE
339388
* event only has one pipeline ordering (and it's named "pipeline").
340389
*/
341-
Array getRA() { result = this.getObject("ra").getArray("pipeline") }
390+
PipeLine getPipeLine() { result = this.getObject("ra").getArray("pipeline") }
342391
}
343392

344393
/** Gets the `index`'th event that's evaluated by `recursive`. */
@@ -481,4 +530,6 @@ module KindPredicatesLog {
481530
class Extensional extends SummaryEvent {
482531
Extensional() { evaluationStrategy = "EXTENSIONAL" }
483532
}
533+
534+
class RAExpr = RAParser<PipeLine>::RAExpr;
484535
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Finds evaluations with very large tuple counts somewhere
3+
*/
4+
5+
import ql
6+
import codeql_ql.StructuredLogs
7+
8+
float maxTupleCount(KindPredicatesLog::SummaryEvent evt) {
9+
result = max(KindPredicatesLog::PipeLineRuns r | r.getEvent() = evt | r.getRun(_).getCount(_))
10+
}
11+
12+
int maxPipeLineLength(KindPredicatesLog::SummaryEvent evt) {
13+
result = max(evt.getRA().getPipeLine(_).getLength())
14+
}
15+
16+
from KindPredicatesLog::SummaryEvent evt
17+
select evt, evt.getResultSize(), evt.getMillis() as ms, maxTupleCount(evt) as mc,
18+
maxPipeLineLength(evt) as len order by mc desc

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ predicate extractInformation(
3535
maxTupleCount = max(run.getCount(_)) and
3636
tuples = run.getCounts() and
3737
duplicationPercentages = run.getDuplicationPercentage() and
38-
ra = simple.getRA()
38+
ra = simple.getPipeLine()
3939
)
4040
}
4141

0 commit comments

Comments
 (0)