Skip to content

Commit

Permalink
fix failing unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
brennanjl committed May 28, 2024
1 parent ce05ab7 commit 22e53d2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
14 changes: 14 additions & 0 deletions parse/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,12 @@ func (s *sqlAnalyzer) VisitExpressionFunctionCall(p0 *ExpressionFunctionCall) an
}
}

// callers of this visitor know that a nil return means a function does not
// return anything. We explicitly return nil instead of a nil *types.DataType
if returnType == nil {
return nil
}

return cast(p0, returnType)
}

Expand Down Expand Up @@ -2252,6 +2258,14 @@ func (p *procedureAnalyzer) VisitProcedureStmtCall(p0 *ProcedureStmtCall) any {
p.errs.AddErr(p0, ErrViewMutatesState, `view procedure calls non-view procedure "%s"`, p0.Call.FunctionName())
}

// users can discard returns by simply not having receivers.
// if there are no receivers, we can return early.
if len(p0.Receivers) == 0 {
return &procedureStmtResult{
willReturn: exits,
}
}

// we do not have to capture all return values, but we need to ensure
// we do not have more receivers than return values.
if len(p0.Receivers) != len(callReturns) {
Expand Down
2 changes: 1 addition & 1 deletion parse/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var (
return nil, wrapErrArgumentType(types.TextType, args[0])
}

return types.NullType, nil
return nil, nil
},
PGFormat: func(inputs []string, distinct bool, star bool) (string, error) {
if star {
Expand Down
22 changes: 22 additions & 0 deletions parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,28 @@ func Test_Procedure(t *testing.T) {
},
},
},
{
name: "assigning a variable with error is invalid",
proc: `$a := error('error message');`,
err: parse.ErrResultShape,
},
{
// this is a regression test for a previous bug
name: "discarding return values of a function is ok",
proc: `abs(-1);`,
want: &parse.ProcedureParseResult{
AST: []parse.ProcedureStmt{
&parse.ProcedureStmtCall{
Call: &parse.ExpressionFunctionCall{
Name: "abs",
Args: []parse.Expression{
exprLit(-1),
},
},
},
},
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 22e53d2

Please sign in to comment.