Skip to content

Commit

Permalink
Fix parsing for plans w/ spaces at beginning of Output lines
Browse files Browse the repository at this point in the history
Fixes #611
  • Loading branch information
pgiraud committed Nov 8, 2023
1 parent 8ff8d77 commit fafbab1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/services/__tests__/from-text.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tests.forEach((planTest: string) => {
const planExpect = fs.readFileSync(planExpectFile, { encoding: "utf-8" })

const planService = new PlanService()
const r = planService.fromSource(planText)
const r = planService.fromSource(planService.cleanupSource(planText))
expect(r).toMatchObject(JSON.parse(planExpect))
})
})
Expand Down
11 changes: 11 additions & 0 deletions src/services/__tests__/from-text/38-expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Plan": {
"Node Type": "Nested Loop Join",
"Plans": [{
"Node Type": "Bitmap Heap Scan",
"Plans": [{
"Node Type": "Bitmap Index Scan"
}]
}]
}
}
8 changes: 8 additions & 0 deletions src/services/__tests__/from-text/38-plan
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Not a real plan. Showing how line breaks (with empty space at the begining) in an Output can break parsing.
QUERY PLAN
═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Nested Loop Left Join (cost=11.95..28.52 rows=5 width=157)
-> Bitmap Heap Scan on public.rel_users_exams (cost=11.80..20.27 rows=5 width=52)
Output: rel_users_exams.user_username, rel_users_exams.exam_id,
rel_users_exams.started_at, rel_users_exams.finished_at
-> Bitmap Index Scan on rel_users_exams_pkey (cost=0.00..11.80 rows=5 width=0)
12 changes: 12 additions & 0 deletions src/services/plan-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ export class PlanService {
return closingParIndex != -1 && closingParIndex < openingParIndex
}

const sameIndent = (line1: string, line2: string) => {
return line1.search(/\S/) == line2.search(/\S/)
}

_.each(lines, (line: string) => {
if (countChar(line, /\)/g) > countChar(line, /\(/g)) {
// if there more closing parenthesis this means that it's the
Expand All @@ -483,6 +487,14 @@ export class PlanService {
} else {
out.push(line)
}
} else if (
0 < out.length &&
out[out.length - 1].match(/^\s*Output/i) &&
!sameIndent(out[out.length - 1], line)
) {
// If previous line was Output and current line is not same indent
// (which would mean a new information line)
out[out.length - 1] += line
} else {
out.push(line)
}
Expand Down

0 comments on commit fafbab1

Please sign in to comment.