@@ -678,7 +678,7 @@ func (b *plpgsqlBuilder) buildPLpgSQLStatements(stmts []ast.Statement, s *scope)
678678 if b .options .insideDataSource && b .setReturnType .Family () == types .TupleFamily {
679679 retNextScope = b .ob .expandRoutineTupleIntoCols (retNextScope )
680680 }
681- b .appendBodyStmtFromScope (& retCon , retNextScope , "" /* stmtTag */ )
681+ b .appendBodyStmtFromScope (& retCon , retNextScope , nil /* stmt */ )
682682 b .appendPlpgSQLStmts (& retCon , stmts [i + 1 :])
683683 return b .callContinuation (& retCon , s )
684684
@@ -720,7 +720,7 @@ func (b *plpgsqlBuilder) buildPLpgSQLStatements(stmts []ast.Statement, s *scope)
720720 if ! b .options .insideDataSource && b .setReturnType .Family () == types .TupleFamily {
721721 retQueryScope = b .ob .combineRoutineColsIntoTuple (retQueryScope )
722722 }
723- b .appendBodyStmtFromScope (& retCon , retQueryScope , t .SqlStmt . StatementTag () )
723+ b .appendBodyStmtFromScope (& retCon , retQueryScope , t .SqlStmt )
724724 b .appendPlpgSQLStmts (& retCon , stmts [i + 1 :])
725725 return b .callContinuation (& retCon , s )
726726
@@ -949,7 +949,7 @@ func (b *plpgsqlBuilder) buildPLpgSQLStatements(stmts []ast.Statement, s *scope)
949949 // crdb_internal.plpgsql_raise builtin function.
950950 con := b .makeContinuation ("_stmt_raise" )
951951 con .def .Volatility = volatility .Volatile
952- b .appendBodyStmtFromScope (& con , b .buildPLpgSQLRaise (con .s , b .getRaiseArgs (con .s , t )), "" /* stmtTag */ )
952+ b .appendBodyStmtFromScope (& con , b .buildPLpgSQLRaise (con .s , b .getRaiseArgs (con .s , t )), nil /* stmt */ )
953953 b .appendPlpgSQLStmts (& con , stmts [i + 1 :])
954954 return b .callContinuation (& con , s )
955955
@@ -970,7 +970,7 @@ func (b *plpgsqlBuilder) buildPLpgSQLStatements(stmts []ast.Statement, s *scope)
970970 if len (t .Target ) == 0 {
971971 // When there is no INTO target, build the SQL statement into a body
972972 // statement that is only executed for its side effects.
973- b .appendBodyStmtFromScope (& execCon , stmtScope , t .SqlStmt . StatementTag () )
973+ b .appendBodyStmtFromScope (& execCon , stmtScope , t .SqlStmt )
974974 b .appendPlpgSQLStmts (& execCon , stmts [i + 1 :])
975975 return b .callContinuation (& execCon , s )
976976 }
@@ -1029,7 +1029,7 @@ func (b *plpgsqlBuilder) buildPLpgSQLStatements(stmts []ast.Statement, s *scope)
10291029 intoScope = b .callContinuation (& retCon , intoScope )
10301030
10311031 // Step 3: call the INTO continuation from the parent scope.
1032- b .appendBodyStmtFromScope (& execCon , intoScope , t .SqlStmt . StatementTag () )
1032+ b .appendBodyStmtFromScope (& execCon , intoScope , t .SqlStmt )
10331033 return b .callContinuation (& execCon , s )
10341034
10351035 case * ast.Open :
@@ -1069,7 +1069,7 @@ func (b *plpgsqlBuilder) buildPLpgSQLStatements(stmts []ast.Statement, s *scope)
10691069 // Cursors with mutations are invalid.
10701070 panic (cursorMutationErr )
10711071 }
1072- b .appendBodyStmtFromScope (& openCon , openScope , query . StatementTag () )
1072+ b .appendBodyStmtFromScope (& openCon , openScope , query )
10731073 b .appendPlpgSQLStmts (& openCon , stmts [i + 1 :])
10741074
10751075 // Build a statement to generate a unique name for the cursor if one
@@ -1079,7 +1079,7 @@ func (b *plpgsqlBuilder) buildPLpgSQLStatements(stmts []ast.Statement, s *scope)
10791079 nameCon := b .makeContinuation ("_gen_cursor_name" )
10801080 nameCon .def .Volatility = volatility .Volatile
10811081 nameScope := b .buildCursorNameGen (& nameCon , t .CurVar )
1082- b .appendBodyStmtFromScope (& nameCon , b .callContinuation (& openCon , nameScope ), "" /* stmtTag */ )
1082+ b .appendBodyStmtFromScope (& nameCon , b .callContinuation (& openCon , nameScope ), nil /* stmt */ )
10831083 return b .callContinuation (& nameCon , s )
10841084
10851085 case * ast.Close :
@@ -1119,7 +1119,7 @@ func (b *plpgsqlBuilder) buildPLpgSQLStatements(stmts []ast.Statement, s *scope)
11191119 closeScope := closeCon .s .push ()
11201120 b .ob .synthesizeColumn (closeScope , closeColName , types .Int , nil /* expr */ , closeCall )
11211121 b .ob .constructProjectForScope (closeCon .s , closeScope )
1122- b .appendBodyStmtFromScope (& closeCon , closeScope , "" /* stmtTag */ )
1122+ b .appendBodyStmtFromScope (& closeCon , closeScope , nil /* stmt */ )
11231123 b .appendPlpgSQLStmts (& closeCon , stmts [i + 1 :])
11241124 return b .callContinuation (& closeCon , s )
11251125
@@ -1143,7 +1143,7 @@ func (b *plpgsqlBuilder) buildPLpgSQLStatements(stmts []ast.Statement, s *scope)
11431143 fetchCon .def .Volatility = volatility .Volatile
11441144 fetchScope := b .buildFetch (fetchCon .s , t )
11451145 if t .IsMove {
1146- b .appendBodyStmtFromScope (& fetchCon , fetchScope , "" /* stmtTag */ )
1146+ b .appendBodyStmtFromScope (& fetchCon , fetchScope , nil /* stmt */ )
11471147 b .appendPlpgSQLStmts (& fetchCon , stmts [i + 1 :])
11481148 return b .callContinuation (& fetchCon , s )
11491149 }
@@ -1174,7 +1174,7 @@ func (b *plpgsqlBuilder) buildPLpgSQLStatements(stmts []ast.Statement, s *scope)
11741174 intoScope = b .callContinuation (& retCon , intoScope )
11751175
11761176 // Add the built statement to the FETCH continuation.
1177- b .appendBodyStmtFromScope (& fetchCon , intoScope , "" /* stmtTag */ )
1177+ b .appendBodyStmtFromScope (& fetchCon , intoScope , nil /* stmt */ )
11781178 return b .callContinuation (& fetchCon , s )
11791179
11801180 case * ast.Null :
@@ -1276,7 +1276,7 @@ func (b *plpgsqlBuilder) buildPLpgSQLStatements(stmts []ast.Statement, s *scope)
12761276 if len (target ) == 0 {
12771277 // When there is no INTO target, build the nested procedure call into a
12781278 // body statement that is only executed for its side effects.
1279- b .appendBodyStmtFromScope (& callCon , callScope , "" /* stmtTag */ )
1279+ b .appendBodyStmtFromScope (& callCon , callScope , nil /* stmt */ )
12801280 b .appendPlpgSQLStmts (& callCon , stmts [i + 1 :])
12811281 return b .callContinuation (& callCon , s )
12821282 }
@@ -1291,7 +1291,7 @@ func (b *plpgsqlBuilder) buildPLpgSQLStatements(stmts []ast.Statement, s *scope)
12911291 intoScope = b .callContinuation (& retCon , intoScope )
12921292
12931293 // Add the built statement to the CALL continuation.
1294- b .appendBodyStmtFromScope (& callCon , intoScope , "" /* stmtTag */ )
1294+ b .appendBodyStmtFromScope (& callCon , intoScope , nil /* stmt */ )
12951295 return b .callContinuation (& callCon , s )
12961296
12971297 case * ast.DoBlock :
@@ -1307,7 +1307,7 @@ func (b *plpgsqlBuilder) buildPLpgSQLStatements(stmts []ast.Statement, s *scope)
13071307 doCon := b .makeContinuation ("_stmt_do" )
13081308 doCon .def .Volatility = volatility .Volatile
13091309 bodyScope := b .ob .buildPLpgSQLDoBody (t )
1310- b .appendBodyStmtFromScope (& doCon , bodyScope , "" /* stmtTag */ )
1310+ b .appendBodyStmtFromScope (& doCon , bodyScope , nil /* stmt */ )
13111311 b .appendPlpgSQLStmts (& doCon , stmts [i + 1 :])
13121312 return b .callContinuation (& doCon , s )
13131313
@@ -1457,7 +1457,7 @@ func (b *plpgsqlBuilder) handleIntForLoop(
14571457 )
14581458 // Call recursively into the loop body continuation.
14591459 incScope = b .callContinuation (& loopCon , incScope )
1460- b .appendBodyStmtFromScope (& incrementCon , incScope , "" /* stmtTag */ )
1460+ b .appendBodyStmtFromScope (& incrementCon , incScope , nil /* stmt */ )
14611461
14621462 // Notably, we call the loop body continuation here, rather than the
14631463 // increment continuation, because the counter should not be incremented
@@ -2021,7 +2021,7 @@ func (b *plpgsqlBuilder) buildEndOfFunctionRaise(con *continuation) {
20212021 pgcode .RoutineExceptionFunctionExecutedNoReturnStatement .String (), /* code */
20222022 )
20232023 con .def .Volatility = volatility .Volatile
2024- b .appendBodyStmtFromScope (con , b .buildPLpgSQLRaise (con .s , args ), "" /* stmtTag */ )
2024+ b .appendBodyStmtFromScope (con , b .buildPLpgSQLRaise (con .s , args ), nil /* stmt */ )
20252025
20262026 // Build a dummy statement that returns NULL. It won't be executed, but
20272027 // ensures that the continuation routine's return type is correct.
@@ -2030,7 +2030,7 @@ func (b *plpgsqlBuilder) buildEndOfFunctionRaise(con *continuation) {
20302030 typedNull := b .ob .factory .ConstructNull (b .returnType )
20312031 b .ob .synthesizeColumn (eofScope , eofColName , b .returnType , nil /* expr */ , typedNull )
20322032 b .ob .constructProjectForScope (con .s , eofScope )
2033- b .appendBodyStmtFromScope (con , eofScope , "" /* stmtTag */ )
2033+ b .appendBodyStmtFromScope (con , eofScope , nil /* stmt */ )
20342034}
20352035
20362036// addOneRowCheck handles INTO STRICT, where a SQL statement is required to
@@ -2280,8 +2280,10 @@ func (b *plpgsqlBuilder) makeContinuationWithTyp(
22802280// appendBodyStmtFromScope is separate from makeContinuation to allow recursive
22812281// routine definitions, which need to push the continuation before it is
22822282// finished. The separation also allows for appending multiple body statements.
2283+ // If the continuation does not map to an explicit statement in the body, stmt
2284+ // may be nil.
22832285func (b * plpgsqlBuilder ) appendBodyStmtFromScope (
2284- con * continuation , bodyScope * scope , stmtTag string ,
2286+ con * continuation , bodyScope * scope , stmt tree. Statement ,
22852287) {
22862288 // Set the volatility of the continuation routine to the least restrictive
22872289 // volatility level in the Relational properties of the body statements.
@@ -2291,8 +2293,11 @@ func (b *plpgsqlBuilder) appendBodyStmtFromScope(
22912293 con .def .Volatility = vol
22922294 }
22932295 con .def .Body = append (con .def .Body , bodyExpr )
2294- con .def .BodyTags = append (con .def .BodyTags , stmtTag )
22952296 con .def .BodyProps = append (con .def .BodyProps , bodyScope .makePhysicalProps ())
2297+ if stmt != nil {
2298+ con .def .BodyTags = append (con .def .BodyTags , stmt .StatementTag ())
2299+ con .def .BodyASTs = append (con .def .BodyASTs , stmt )
2300+ }
22962301}
22972302
22982303// appendPlpgSQLStmts builds the given PLpgSQL statements into a relational
@@ -2302,7 +2307,7 @@ func (b *plpgsqlBuilder) appendPlpgSQLStmts(con *continuation, stmts []ast.State
23022307 // Make sure to push s before constructing the continuation scope to ensure
23032308 // that the parameter columns are not projected.
23042309 continuationScope := b .buildPLpgSQLStatements (stmts , con .s .push ())
2305- b .appendBodyStmtFromScope (con , continuationScope , "" /* stmtTag */ )
2310+ b .appendBodyStmtFromScope (con , continuationScope , nil /* stmt */ )
23062311}
23072312
23082313// callContinuation adds a column that projects the result of calling the
0 commit comments