Skip to content

Commit

Permalink
Fix parsing issue for last line if contains quote
Browse files Browse the repository at this point in the history
For example, the "Execution time" line was not parsed correctly when
surrounded by quotes and on the very last line (no line break after)

Fixes #573
  • Loading branch information
pgiraud committed Aug 21, 2024
1 parent d469b9c commit 09bee7a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
45 changes: 45 additions & 0 deletions src/services/__tests__/16-quote-last-line.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { PlanService } from "@/services/plan-service"
import type { IPlanContent } from "@/interfaces"

describe("PlanService", () => {
test("Parses 'Execution time line", () => {
const planService = new PlanService()

// tslint:disable:max-line-length
const source = `
"Aggregate (cost=24729994.62..24729994.63 rows=1 width=8) (actual time=126922.755..126980.072 rows=1 loops=1)"
" Buffers: shared hit=31033317 read=640327, temp read=271916 written=271928"
"Planning:"
" Buffers: shared hit=227 read=9"
"Planning Time: 14.620 ms"
"JIT:"
" Functions: 829"
" Options: Inlining true, Optimization true, Expressions true, Deforming true"
" Timing: Generation 113.965 ms, Inlining 706.405 ms, Optimization 7838.603 ms, Emission 7380.843 ms, Total 16039.815 ms"
"Execution Time: 127241.780 ms"`

const plan = planService.fromSource(source) as IPlanContent
expect(plan?.["Execution Time"]).toEqual(127241.78)
})
test("Parses 'Execution time line", () => {
// Same plan but with a line break at the end
const planService = new PlanService()

// tslint:disable:max-line-length
const source = `
"Aggregate (cost=24729994.62..24729994.63 rows=1 width=8) (actual time=126922.755..126980.072 rows=1 loops=1)"
" Buffers: shared hit=31033317 read=640327, temp read=271916 written=271928"
"Planning:"
" Buffers: shared hit=227 read=9"
"Planning Time: 14.620 ms"
"JIT:"
" Functions: 829"
" Options: Inlining true, Optimization true, Expressions true, Deforming true"
" Timing: Generation 113.965 ms, Inlining 706.405 ms, Optimization 7838.603 ms, Emission 7380.843 ms, Total 16039.815 ms"
"Execution Time: 127241.780 ms"
`

const plan = planService.fromSource(source) as IPlanContent
expect(plan?.["Execution Time"]).toEqual(127241.78)
})
})
2 changes: 1 addition & 1 deletion src/services/plan-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ export class PlanService {
source = source.replace(/^()+\r?\n/gm, "")

// Remove quotes around lines, both ' and "
source = source.replace(/^(["'])(.*)\1\r?\n/gm, "$2\n")
source = source.replace(/^(["'])(.*)\1\r?/gm, "$2\n")

// Remove "+" line continuations
source = source.replace(/\s*\+\r?\n/g, "\n")
Expand Down

0 comments on commit 09bee7a

Please sign in to comment.