@@ -949,75 +949,110 @@ func (pgb *standardPlanGraphBuilder) handleInsert(pbi planbuilderinput.PlanBuild
949949 if isPhysicalTable {
950950 bldrInput .SetIsTargetPhysicalTable (true )
951951 }
952- var bldr primitivebuilder.Builder
953- if len (node .SelectExprs ) > 0 {
954- // Two cases:
955- // 1. Synchronous. Equivalent to select.
956- // 2. Asynchronous. Whole other story.
957- tableMeta , tableMetaExists := bldrInput .GetTableMetadata ()
958- if ! tableMetaExists {
959- return fmt .Errorf ("could not obtain table metadata for node '%s'" , node .Action )
960- }
961- rc , rcErr := tableinsertioncontainer .NewTableInsertionContainer (
962- tableMeta ,
963- handlerCtx .GetSQLEngine (),
964- handlerCtx .GetTxnCounterMgr (),
952+ return pgb .handleMutationOperation (
953+ handlerCtx ,
954+ pbi ,
955+ primitiveGenerator ,
956+ node ,
957+ tbl ,
958+ selectPrimitiveNode ,
959+ bldrInput ,
960+ isAwait ,
961+ )
962+ }
963+ pr := primitive .NewGenericPrimitive (nil , nil , nil , primitive_context .NewPrimitiveContext ())
964+ pgb .planGraphHolder .CreatePrimitiveNode (pr )
965+ return nil
966+ }
967+
968+ func (pgb * standardPlanGraphBuilder ) handleMutationOperation (
969+ handlerCtx handler.HandlerContext ,
970+ pbi planbuilderinput.PlanBuilderInput ,
971+ primitiveGenerator primitivegenerator.PrimitiveGenerator ,
972+ node sqlparser.SQLNode ,
973+ tbl tablemetadata.ExtendedTableMetadata ,
974+ selectPrimitiveNode primitivegraph.PrimitiveNode ,
975+ bldrInput builder_input.BuilderInput ,
976+ isAwait bool ,
977+ ) error {
978+ var returningExpressions sqlparser.SelectExprs
979+ var inputAction string
980+ var bldr primitivebuilder.Builder
981+ switch n := node .(type ) {
982+ case * sqlparser.Insert :
983+ returningExpressions = n .SelectExprs
984+ inputAction = n .Action
985+ case * sqlparser.Update :
986+ returningExpressions = n .SelectExprs
987+ inputAction = n .Action
988+ default :
989+ return fmt .Errorf ("unsupported mutation operation of type '%T'" , node )
990+ }
991+ //nolint:nestif // acceptable complexity
992+ if len (returningExpressions ) > 0 {
993+ // Two cases:
994+ // 1. Synchronous. Equivalent to select.
995+ // 2. Asynchronous. Whole other story.
996+ tableMeta , tableMetaExists := bldrInput .GetTableMetadata ()
997+ if ! tableMetaExists {
998+ return fmt .Errorf ("could not obtain table metadata for node '%s'" , inputAction )
999+ }
1000+ rc , rcErr := tableinsertioncontainer .NewTableInsertionContainer (
1001+ tableMeta ,
1002+ handlerCtx .GetSQLEngine (),
1003+ handlerCtx .GetTxnCounterMgr (),
1004+ )
1005+ if rcErr != nil {
1006+ return rcErr
1007+ }
1008+ bldrInput .SetTableInsertionContainer (rc )
1009+ bldrInput .SetIsReturning (true )
1010+ if ! isAwait {
1011+ bldr = primitivebuilder .NewSingleAcquireAndSelect (
1012+ bldrInput ,
1013+ primitiveGenerator .GetPrimitiveComposer ().GetInsertPreparedStatementCtx (),
1014+ primitiveGenerator .GetPrimitiveComposer ().GetSelectPreparedStatementCtx (),
1015+ nil ,
9651016 )
966- if rcErr != nil {
967- return rcErr
968- }
969- bldrInput .SetTableInsertionContainer (rc )
970- bldrInput .SetIsReturning (true )
971- if ! isAwait {
972- bldr = primitivebuilder .NewSingleAcquireAndSelect (
973- bldrInput ,
974- primitiveGenerator .GetPrimitiveComposer ().GetInsertPreparedStatementCtx (),
975- primitiveGenerator .GetPrimitiveComposer ().GetSelectPreparedStatementCtx (),
976- nil ,
977- )
978- } else {
979- bldrInput .SetIsAwait (true )
980- bldrInput .SetIsReturning (true )
981- bldrInput .SetInsertCtx (primitiveGenerator .GetPrimitiveComposer ().GetInsertPreparedStatementCtx ())
982- lhsBldr := primitivebuilder .NewInsertOrUpdate (
983- bldrInput ,
984- )
985- newBldrInput := builder_input .NewBuilderInput (
986- pgb .planGraphHolder ,
987- handlerCtx ,
988- tbl ,
989- )
990- newBldrInput .SetParserNode (node )
991- newBldrInput .SetAnnotatedAST (pbi .GetAnnotatedAST ())
992- newBldrInput .SetTxnCtrlCtrs (pbi .GetTxnCtrlCtrs ())
993- newBldrInput .SetTableInsertionContainer (rc )
994- newBldrInput .SetDependencyNode (selectPrimitiveNode )
995- newBldrInput .SetIsAwait (isAwait )
996- rhsBldr := primitivebuilder .NewSingleSelect (
997- pgb .planGraphHolder , handlerCtx , primitiveGenerator .GetPrimitiveComposer ().GetSelectPreparedStatementCtx (),
998- []tableinsertioncontainer.TableInsertionContainer {rc },
999- nil ,
1000- streaming .NewNopMapStream (),
1001- )
1002- bldr = primitivebuilder .NewDependencySubDAGBuilder (
1003- pgb .planGraphHolder ,
1004- []primitivebuilder.Builder {lhsBldr },
1005- rhsBldr ,
1006- )
1007- }
10081017 } else {
1009- bldr = primitivebuilder .NewInsertOrUpdate (
1018+ bldrInput .SetIsAwait (true )
1019+ bldrInput .SetIsReturning (true )
1020+ bldrInput .SetInsertCtx (primitiveGenerator .GetPrimitiveComposer ().GetInsertPreparedStatementCtx ())
1021+ lhsBldr := primitivebuilder .NewInsertOrUpdate (
10101022 bldrInput ,
10111023 )
1024+ newBldrInput := builder_input .NewBuilderInput (
1025+ pgb .planGraphHolder ,
1026+ handlerCtx ,
1027+ tbl ,
1028+ )
1029+ newBldrInput .SetParserNode (node )
1030+ newBldrInput .SetAnnotatedAST (pbi .GetAnnotatedAST ())
1031+ newBldrInput .SetTxnCtrlCtrs (pbi .GetTxnCtrlCtrs ())
1032+ newBldrInput .SetTableInsertionContainer (rc )
1033+ newBldrInput .SetDependencyNode (selectPrimitiveNode )
1034+ newBldrInput .SetIsAwait (isAwait )
1035+ rhsBldr := primitivebuilder .NewSingleSelect (
1036+ pgb .planGraphHolder , handlerCtx , primitiveGenerator .GetPrimitiveComposer ().GetSelectPreparedStatementCtx (),
1037+ []tableinsertioncontainer.TableInsertionContainer {rc },
1038+ nil ,
1039+ streaming .NewNopMapStream (),
1040+ )
1041+ bldr = primitivebuilder .NewDependencySubDAGBuilder (
1042+ pgb .planGraphHolder ,
1043+ []primitivebuilder.Builder {lhsBldr },
1044+ rhsBldr ,
1045+ )
10121046 }
1013- err = bldr .Build ()
1014- if err != nil {
1015- return err
1016- }
1017- return nil
1047+ } else {
1048+ bldr = primitivebuilder .NewInsertOrUpdate (
1049+ bldrInput ,
1050+ )
1051+ }
1052+ err := bldr .Build ()
1053+ if err != nil {
1054+ return err
10181055 }
1019- pr := primitive .NewGenericPrimitive (nil , nil , nil , primitive_context .NewPrimitiveContext ())
1020- pgb .planGraphHolder .CreatePrimitiveNode (pr )
10211056 return nil
10221057}
10231058
@@ -1072,14 +1107,17 @@ func (pgb *standardPlanGraphBuilder) handleUpdate(pbi planbuilderinput.PlanBuild
10721107 bldrInput .SetTxnCtrlCtrs (pbi .GetTxnCtrlCtrs ())
10731108 bldrInput .SetIsTargetPhysicalTable (true )
10741109 }
1075- bldr := primitivebuilder .NewInsertOrUpdate (
1110+ isAwait := primitiveGenerator .GetPrimitiveComposer ().IsAwait ()
1111+ return pgb .handleMutationOperation (
1112+ handlerCtx ,
1113+ pbi ,
1114+ primitiveGenerator ,
1115+ node ,
1116+ tbl ,
1117+ selectPrimitiveNode ,
10761118 bldrInput ,
1119+ isAwait ,
10771120 )
1078- err = bldr .Build ()
1079- if err != nil {
1080- return err
1081- }
1082- return nil
10831121 }
10841122 pr := primitive .NewGenericPrimitive (nil , nil , nil , primitive_context .NewPrimitiveContext ())
10851123 pgb .planGraphHolder .CreatePrimitiveNode (pr )
0 commit comments