Skip to content

Commit

Permalink
add missing return detection (#769)
Browse files Browse the repository at this point in the history
* add missing return detection

fixed several bugs

* fixed failing test

* added sanity check test

* prevent error when looping on null arrays

* fix select count(*) ordering bug

* add comment
  • Loading branch information
brennanjl authored May 24, 2024
1 parent a8b624b commit 327a34f
Show file tree
Hide file tree
Showing 5 changed files with 675 additions and 126 deletions.
3 changes: 2 additions & 1 deletion internal/engine/generate/plpgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,8 @@ func (p *procedureGenerator) VisitLoopTermSQL(p0 *parse.LoopTermSQL) any {
}

func (p *procedureGenerator) VisitLoopTermVariable(p0 *parse.LoopTermVariable) any {
return fmt.Sprintf("ARRAY %s", p0.Variable.Accept(p).(string))
// we use coalesce here so that we do not error when looping on null arrays
return fmt.Sprintf("ARRAY COALESCE(%s, '{}')", p0.Variable.Accept(p).(string))
}

func (p *procedureGenerator) VisitProcedureStmtIf(p0 *parse.ProcedureStmtIf) any {
Expand Down
32 changes: 32 additions & 0 deletions internal/engine/integration/procedure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,38 @@ func Test_Procedures(t *testing.T) {
}`,
outputs: [][]any{{int64(11)}},
},
{
name: "return next from a non-table",
procedure: `procedure return_next($vals int[]) public view returns table(val int) {
for $i in $vals {
return next $i*2;
}
}`,
inputs: []any{[]int64{1, 2, 3}},
outputs: [][]any{{int64(2)}, {int64(4)}, {int64(6)}},
},
{
name: "table return with no hits doesn't return postgres no-return error",
procedure: `procedure return_next($vals int[]) public view returns table(val int) {
for $i in $vals {
error('unreachable');
}
}`,
inputs: []any{[]int64{}},
outputs: [][]any{},
},
{
name: "loop over null array",
procedure: `procedure loop_over_null() public view returns (count int) {
$vals int[];
$count := 0;
for $i in $vals {
$count := $count + 1;
}
return $count;
}`,
outputs: [][]any{{int64(0)}},
},
}

for _, test := range tests {
Expand Down
Loading

0 comments on commit 327a34f

Please sign in to comment.