vitess.io/vitess@v0.16.2/go/vt/vtgate/engine/compare_utils.go (about)

     1  /*
     2  Copyright 2022 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package engine
    18  
    19  import (
    20  	"encoding/json"
    21  
    22  	"vitess.io/vitess/go/sqltypes"
    23  	"vitess.io/vitess/go/vt/log"
    24  	vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
    25  	"vitess.io/vitess/go/vt/vterrors"
    26  )
    27  
    28  func printMismatch(leftResult, rightResult *sqltypes.Result, leftPrimitive, rightPrimitive Primitive, leftName, rightName string) {
    29  	log.Errorf("Results of %s and %s are not equal. Displaying diff.", rightName, leftName)
    30  
    31  	// get right plan and print it
    32  	rightplan := &Plan{
    33  		Instructions: rightPrimitive,
    34  	}
    35  	rightJSON, _ := json.MarshalIndent(rightplan, "", "  ")
    36  	log.Errorf("%s's plan:\n%s", rightName, string(rightJSON))
    37  
    38  	// get left's plan and print it
    39  	leftplan := &Plan{
    40  		Instructions: leftPrimitive,
    41  	}
    42  	leftJSON, _ := json.MarshalIndent(leftplan, "", "  ")
    43  	log.Errorf("%s's plan:\n%s", leftName, string(leftJSON))
    44  
    45  	log.Errorf("%s's results:\n", rightName)
    46  	log.Errorf("\t[rows affected: %d]\n", rightResult.RowsAffected)
    47  	for _, row := range rightResult.Rows {
    48  		log.Errorf("\t%s", row)
    49  	}
    50  	log.Errorf("%s's results:\n", leftName)
    51  	log.Errorf("\t[rows affected: %d]\n", leftResult.RowsAffected)
    52  	for _, row := range leftResult.Rows {
    53  		log.Errorf("\t%s", row)
    54  	}
    55  	log.Error("End of diff.")
    56  }
    57  
    58  // CompareErrors compares the two errors, and if they don't match, produces an error
    59  func CompareErrors(leftErr, rightErr error, leftName, rightName string) error {
    60  	if leftErr != nil && rightErr != nil {
    61  		if leftErr.Error() == rightErr.Error() {
    62  			return rightErr
    63  		}
    64  		return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "%s and %s failed with different errors: %s: [%s], %s: [%s]", leftName, rightName, leftErr.Error(), rightErr.Error(), leftName, rightName)
    65  	}
    66  	if leftErr == nil && rightErr != nil {
    67  		return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "%s failed while %s did not: %s", rightName, rightErr.Error(), leftName)
    68  	}
    69  	if leftErr != nil && rightErr == nil {
    70  		return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "%s failed while %s did not: %s", leftName, leftErr.Error(), rightName)
    71  	}
    72  	return nil
    73  }