@@ -8,6 +8,7 @@ package execbuilder
88import (
99 "context"
1010
11+ "github.com/cockroachdb/cockroach/pkg/sql/appstatspb"
1112 "github.com/cockroachdb/cockroach/pkg/sql/opt"
1213 "github.com/cockroachdb/cockroach/pkg/sql/opt/exec"
1314 "github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
@@ -689,16 +690,7 @@ func (b *Builder) buildExistsSubquery(
689690
690691 // Create a plan generator that can plan the single statement
691692 // representing the subquery, and wrap the routine in a COALESCE.
692- planGen := b .buildRoutinePlanGenerator (
693- params ,
694- stmts ,
695- stmtProps ,
696- nil , /* stmtStr */
697- make ([]string , len (stmts )),
698- true , /* allowOuterWithRefs */
699- wrapRootExpr ,
700- 0 , /* resultBufferID */
701- )
693+ planGen := b .buildRoutinePlanGenerator (params , stmts , stmtProps , nil , make ([]string , len (stmts )), nil , true , wrapRootExpr , 0 )
702694 return tree .NewTypedCoalesceExpr (tree.TypedExprs {
703695 tree .NewTypedRoutineExpr (
704696 "exists" ,
@@ -816,16 +808,7 @@ func (b *Builder) buildSubquery(
816808
817809 // Create a tree.RoutinePlanFn that can plan the single statement
818810 // representing the subquery.
819- planGen := b .buildRoutinePlanGenerator (
820- params ,
821- stmts ,
822- stmtProps ,
823- nil , /* stmtStr */
824- make ([]string , len (stmts )),
825- true , /* allowOuterWithRefs */
826- nil , /* wrapRootExpr */
827- 0 , /* resultBufferID */
828- )
811+ planGen := b .buildRoutinePlanGenerator (params , stmts , stmtProps , nil , make ([]string , len (stmts )), nil , true , nil , 0 )
829812 _ , tailCall := b .tailCalls [subquery ]
830813 return tree .NewTypedRoutineExpr (
831814 "subquery" ,
@@ -900,7 +883,7 @@ func (b *Builder) buildSubquery(
900883 if err != nil {
901884 return err
902885 }
903- err = fn (plan , "" /* stmtForDistSQLDiagram */ , true /* isFinalPlan */ )
886+ err = fn (plan , tree. RoutineBodyStmt {} /* stmtForDistSQLDiagram */ , true /* isFinalPlan */ )
904887 if err != nil {
905888 return err
906889 }
@@ -1011,16 +994,7 @@ func (b *Builder) buildUDF(ctx *buildScalarCtx, scalar opt.ScalarExpr) (tree.Typ
1011994 }
1012995
1013996 // Create a tree.RoutinePlanFn that can plan the statements in the UDF body.
1014- planGen := b .buildRoutinePlanGenerator (
1015- udf .Def .Params ,
1016- udf .Def .Body ,
1017- udf .Def .BodyProps ,
1018- udf .Def .BodyStmts ,
1019- udf .Def .BodyTags ,
1020- false , /* allowOuterWithRefs */
1021- nil , /* wrapRootExpr */
1022- udf .Def .ResultBufferID ,
1023- )
997+ planGen := b .buildRoutinePlanGenerator (udf .Def .Params , udf .Def .Body , udf .Def .BodyProps , udf .Def .BodyStmts , udf .Def .BodyTags , udf .Def .BodyASTs , false , nil , udf .Def .ResultBufferID )
1024998
1025999 // Enable stepping for volatile functions so that statements within the UDF
10261000 // see mutations made by the invoking statement and by previously executed
@@ -1085,16 +1059,7 @@ func (b *Builder) initRoutineExceptionHandler(
10851059 Actions : make ([]* tree.RoutineExpr , len (exceptionBlock .Actions )),
10861060 }
10871061 for i , action := range exceptionBlock .Actions {
1088- actionPlanGen := b .buildRoutinePlanGenerator (
1089- action .Params ,
1090- action .Body ,
1091- action .BodyProps ,
1092- action .BodyStmts ,
1093- action .BodyTags ,
1094- false , /* allowOuterWithRefs */
1095- nil , /* wrapRootExpr */
1096- 0 , /* resultBufferID */
1097- )
1062+ actionPlanGen := b .buildRoutinePlanGenerator (action .Params , action .Body , action .BodyProps , action .BodyStmts , action .BodyTags , nil , false , nil , 0 )
10981063 // Build a routine with no arguments for the exception handler. The actual
10991064 // arguments will be supplied when (if) the handler is invoked.
11001065 exceptionHandler .Actions [i ] = tree .NewTypedRoutineExpr (
@@ -1141,6 +1106,7 @@ func (b *Builder) buildRoutinePlanGenerator(
11411106 stmtProps []* physical.Required ,
11421107 stmtStr []string ,
11431108 stmtTags []string ,
1109+ stmtASTs []tree.Statement ,
11441110 allowOuterWithRefs bool ,
11451111 wrapRootExpr wrapRootExprFn ,
11461112 resultBufferID memo.RoutineResultBufferID ,
@@ -1207,15 +1173,42 @@ func (b *Builder) buildRoutinePlanGenerator(
12071173 dbName := b .evalCtx .SessionData ().Database
12081174 appName := b .evalCtx .SessionData ().ApplicationName
12091175
1176+ format := tree .FmtHideConstants | tree .FmtFlags (tree .QueryFormattingForFingerprintsMask .Get (& b .evalCtx .Settings .SV ))
12101177 for i := range stmts {
1178+ var bodyStmt tree.RoutineBodyStmt
12111179 stmt := stmts [i ]
12121180 props := stmtProps [i ]
12131181 var tag string
1182+ var fingerprint string
1183+ var summary string
1184+ var fpId appstatspb.StmtFingerprintID
1185+ var stmtString string
1186+ var stmtType tree.StatementType
12141187 // Theoretically, stmts and stmtTags should have the same length,
12151188 // but just to avoid an out-of-bounds panic, we have this check.
12161189 if i < len (stmtTags ) {
12171190 tag = stmtTags [i ]
12181191 }
1192+ if i < len (stmtStr ) {
1193+ stmtString = stmtStr [i ]
1194+ }
1195+ if i < len (stmtASTs ) {
1196+ fingerprint = tree .FormatStatementHideConstants (stmtASTs [i ], format )
1197+ summary = tree .FormatStatementSummary (stmtASTs [i ], format )
1198+ fpId = appstatspb .ConstructStatementFingerprintID (fingerprint , b .evalCtx .TxnImplicit , dbName )
1199+ stmtType = stmtASTs [i ].StatementType ()
1200+ }
1201+
1202+ bodyStmt = tree.RoutineBodyStmt {
1203+ FingerprintStr : fingerprint ,
1204+ SummaryStr : summary ,
1205+ StmtType : stmtType ,
1206+ StmtString : stmtString ,
1207+ DbName : dbName ,
1208+ AppName : appName ,
1209+ FingerprintId : uint64 (fpId ),
1210+ }
1211+
12191212 o .Init (ctx , b .evalCtx , b .catalog )
12201213 f := o .Factory ()
12211214
@@ -1319,12 +1312,8 @@ func (b *Builder) buildRoutinePlanGenerator(
13191312 return expectedLazyRoutineError ("subquery" )
13201313 }
13211314 }
1322- var stmtForDistSQLDiagram string
1323- if i < len (stmtStr ) {
1324- stmtForDistSQLDiagram = stmtStr [i ]
1325- }
13261315 incrementRoutineStmtCounter (b .evalCtx .StartedRoutineStatementCounters , dbName , appName , tag )
1327- err = fn (plan , stmtForDistSQLDiagram , isFinalPlan )
1316+ err = fn (plan , bodyStmt , isFinalPlan )
13281317 if err != nil {
13291318 return err
13301319 }
0 commit comments