Skip to content

Commit

Permalink
sqlsmith: allow udf params to be referenced inside udfs
Browse files Browse the repository at this point in the history
Randomly generated UDFs may now reference their parameters inside the
UDF body statements.

Epic: CRDB-20370
Fixes: cockroachdb#90782

Release note: None
  • Loading branch information
rharding6373 committed Apr 13, 2023
1 parent cac2b72 commit b632c26
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/internal/sqlsmith/relational.go
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,6 @@ func (s *Smither) makeCreateFunc() (cf *tree.CreateFunction, ok bool) {
IsSet: setof,
}

// TODO(harding): Allow params to be referenced in the statement body.
paramCnt := s.rnd.Intn(10)
if len(s.types.scalarTypes) == 0 {
paramCnt = 0
Expand All @@ -916,6 +915,7 @@ func (s *Smither) makeCreateFunc() (cf *tree.CreateFunction, ok bool) {
// TODO(100962): Set a param default value sometimes.
params := make(tree.FuncParams, paramCnt)
paramTypes := make(tree.ParamTypes, paramCnt)
refs := make(colRefs, paramCnt)
for i := 0; i < paramCnt; i++ {
// Do not allow collated string types. These are not supported in UDFs.
ptyp := s.randType()
Expand All @@ -931,6 +931,12 @@ func (s *Smither) makeCreateFunc() (cf *tree.CreateFunction, ok bool) {
Name: pname,
Typ: ptyp,
}
refs[i] = &colRef{
typ: ptyp,
item: &tree.ColumnItem{
ColumnName: tree.Name(pname),
},
}
}

// There are up to 5 function options that may be applied to UDFs.
Expand Down Expand Up @@ -1013,11 +1019,11 @@ func (s *Smither) makeCreateFunc() (cf *tree.CreateFunction, ok bool) {
if i == stmtCnt-1 {
// The return type of the last statement should match the function return
// type.
if stmt, _, ok = s.makeSelect([]*types.T{rtyp}, nil /* refs */); !ok {
if stmt, _, ok = s.makeSelect([]*types.T{rtyp}, refs); !ok {
return nil, false
}
} else {
if stmt, _, ok = s.makeSelect(nil /* desiredTypes */, nil /* refs */); !ok {
if stmt, _, ok = s.makeSelect(nil /* desiredTypes */, refs); !ok {
continue
}
}
Expand Down

0 comments on commit b632c26

Please sign in to comment.