@@ -408,11 +408,12 @@ private boolean isFloat32NaN(Value value) {
408408 * Converts the list of mutations to the corresponding protobuf mutations and returns a random
409409 * mutation from the available list based on the following heuristics:
410410 *
411- * <p>1. Prefer mutations other than INSERT, as INSERT mutations may contain autogenerated columns
412- * whose information is unavailable on the client.
413- *
414- * <p>2. If the list only contains INSERT mutations, select the one with the highest number of
415- * values.
411+ * <ol>
412+ * <li>1. Prefer mutations other than INSERT, as INSERT mutations may contain autogenerated
413+ * columns whose information is unavailable on the client.
414+ * <li>If the list only contains INSERT mutations, select the one with the highest number of
415+ * values.
416+ * </ol>
416417 */
417418 static com .google .spanner .v1 .Mutation toProtoAndReturnRandomMutation (
418419 Iterable <Mutation > mutations , List <com .google .spanner .v1 .Mutation > out ) {
@@ -437,10 +438,12 @@ static com.google.spanner.v1.Mutation toProtoAndReturnRandomMutation(
437438 if (proto != null ) {
438439 com .google .spanner .v1 .Mutation builtMutation = proto .build ();
439440 out .add (builtMutation );
440- if (checkIfInsertMutationWithLargeValue (builtMutation , largestInsertMutation )) {
441+ // Skip tracking the largest insert mutation if there are mutations other than INSERT.
442+ if (allMutationsExcludingInsert .isEmpty ()
443+ && checkIfInsertMutationWithLargeValue (builtMutation , largestInsertMutation )) {
441444 largestInsertMutation = builtMutation ;
442445 }
443- addMutationsExcludingInsert (builtMutation , allMutationsExcludingInsert );
446+ maybeAddMutationToListExcludingInserts (builtMutation , allMutationsExcludingInsert );
444447 }
445448 proto = com .google .spanner .v1 .Mutation .newBuilder ();
446449 com .google .spanner .v1 .Mutation .Delete .Builder delete =
@@ -464,10 +467,12 @@ static com.google.spanner.v1.Mutation toProtoAndReturnRandomMutation(
464467 if (proto != null ) {
465468 com .google .spanner .v1 .Mutation builtMutation = proto .build ();
466469 out .add (builtMutation );
467- if (checkIfInsertMutationWithLargeValue (builtMutation , largestInsertMutation )) {
470+ // Skip tracking the largest insert mutation if there are mutations other than INSERT.
471+ if (allMutationsExcludingInsert .isEmpty ()
472+ && checkIfInsertMutationWithLargeValue (builtMutation , largestInsertMutation )) {
468473 largestInsertMutation = builtMutation ;
469474 }
470- addMutationsExcludingInsert (builtMutation , allMutationsExcludingInsert );
475+ maybeAddMutationToListExcludingInserts (builtMutation , allMutationsExcludingInsert );
471476 }
472477 proto = com .google .spanner .v1 .Mutation .newBuilder ();
473478 switch (mutation .operation ) {
@@ -496,10 +501,12 @@ static com.google.spanner.v1.Mutation toProtoAndReturnRandomMutation(
496501 if (proto != null ) {
497502 com .google .spanner .v1 .Mutation builtMutation = proto .build ();
498503 out .add (proto .build ());
499- if (checkIfInsertMutationWithLargeValue (builtMutation , largestInsertMutation )) {
504+ // Skip tracking the largest insert mutation if there are mutations other than INSERT.
505+ if (allMutationsExcludingInsert .isEmpty ()
506+ && checkIfInsertMutationWithLargeValue (builtMutation , largestInsertMutation )) {
500507 largestInsertMutation = builtMutation ;
501508 }
502- addMutationsExcludingInsert (builtMutation , allMutationsExcludingInsert );
509+ maybeAddMutationToListExcludingInserts (builtMutation , allMutationsExcludingInsert );
503510 }
504511
505512 // Select a random mutation based on the heuristic.
@@ -518,7 +525,7 @@ private static boolean checkIfInsertMutationWithLargeValue(
518525 com .google .spanner .v1 .Mutation largestInsertMutation ) {
519526 // If largestInsertMutation is a default instance of Mutation, replace it with the current
520527 // INSERT mutation, even if it contains zero values.
521- if (!largestInsertMutation .hasInsert ()) {
528+ if (mutation . hasInsert () && !largestInsertMutation .hasInsert ()) {
522529 return true ;
523530 }
524531 return mutation .hasInsert ()
@@ -527,7 +534,7 @@ private static boolean checkIfInsertMutationWithLargeValue(
527534 }
528535
529536 // Stores all mutations that are not of type INSERT.
530- private static void addMutationsExcludingInsert (
537+ private static void maybeAddMutationToListExcludingInserts (
531538 com .google .spanner .v1 .Mutation mutation ,
532539 List <com .google .spanner .v1 .Mutation > allMutationsExcludingInsert ) {
533540 if (!mutation .hasInsert ()) {
0 commit comments