// GetCRC32Checksum returns checksum code of some data by given condition
GetCRC32Checksum returns checksum code of some data by given condition
// GetCRC32Checksum returns checksum code of some data by given condition
func GetCRC32Checksum(ctx context.Context, db *sql.DB, schemaName string, tbInfo *model.TableInfo, orderKeys []string, limitRange string, args []interface{}) (string, error) {
	isTiDB, err := IsTiDB(ctx, db)
	if err != nil {
		return "", errors.Trace(err)
	}
	columnNames := make([]string, 0, len(tbInfo.Columns))
	for _, col := range tbInfo.Columns {
		columnNames = append(columnNames, col.Name.O)
	}
	var query string
	if isTiDB {
		query = fmt.Sprintf("SELECT CRC32(GROUP_CONCAT(row SEPARATOR ' + ')) AS checksum FROM (SELECT CONCAT_WS(',',%s) AS row FROM (SELECT * FROM `%s`.`%s` WHERE %s ORDER BY %s) AS tmp) AS rows ORDER BY row;",
			strings.Join(columnNames, ", "), schemaName, tbInfo.Name.O, limitRange, strings.Join(orderKeys, ","))
	} else {
		query = fmt.Sprintf("SELECT CRC32(GROUP_CONCAT(CONCAT_WS(',',%s) ORDER BY %s ASC SEPARATOR ' + ')) AS checksum FROM `%s`.`%s` WHERE %s;",
			strings.Join(columnNames, ", "), strings.Join(orderKeys, ","), schemaName, tbInfo.Name.O, limitRange)
	}
	log.Debugf("checksum sql: %s, args: %v", query, args)
	var checksum sql.NullString
	err = db.QueryRowContext(ctx, query, args...).Scan(&checksum)
	if err != nil {
		return "", errors.Trace(err)
	}
	if !checksum.Valid {
		log.Warnf("get empty checksum by query %s, args %v", query, args)
		return "", nil
	}
	return checksum.String, nil
}
func GetCRC32Checksum ( ctx context . Context , db * sql . DB , schemaName string , tbInfo * model . TableInfo , orderKeys [ ] string , limitRange string , args [ ] interface { } ) ( string , error ) { isTiDB , err := IsTiDB ( ctx , db ) if err != nil { return " " , errors . Trace ( err ) } columnNames := make ( [ ] string , 0 , len ( tbInfo . Columns ) ) for _ , col := range tbInfo . Columns { columnNames = append ( columnNames , col . Name . O ) } var query string if isTiDB { query = fmt . Sprintf ( " " , strings . Join ( columnNames , " " ) , schemaName , tbInfo . Name . O , limitRange , strings . Join ( orderKeys , " " ) ) } else { query = fmt . Sprintf ( " " , strings . Join ( columnNames , " " ) , strings . Join ( orderKeys , " " ) , schemaName , tbInfo . Name . O , limitRange ) } log . Debugf ( " " , query , args ) var checksum sql . NullString err = db . QueryRowContext ( ctx , query , args ... ) . Scan ( & checksum ) if err != nil { return " " , errors . Trace ( err ) } if ! checksum . Valid { log . Warnf ( " " , query , args ) return " " , nil } return checksum . String , nil }
GetCRC32Checksum returns checksum code of some data by given condition
GetCRC32Checksum returns checksum code of some data by given condition
/*
		TODO: use same sql to calculate CRC32 checksum in TiDB and MySQL when TiDB support ORDER BY in GROUP_CONTACT.

		calculate CRC32 checksum example:

		in TiDB:
		mysql> SELECT CRC32(GROUP_CONCAT(row SEPARATOR ' + ')) AS checksum
			> FROM (SELECT CONCAT_WS(",",a,b,c) AS row
			> FROM (SELECT * FROM test.test WHERE `a` >= 0 AND `a` < 10 AND true ORDER BY a) AS tmp) AS rows ORDER BY row;
		+------------+
		| checksum   |
		+------------+
		| 1171947116 |
		+------------+

		in MySQL:
		mysql> SELECT CRC32(GROUP_CONCAT(CONCAT_WS(',', a, b, c ) ORDER BY a ASC SEPARATOR ' + ')) AS checksum
			> FROM test.test WHERE `a` >= 0 AND `a` < 10 AND true;
		+------------+
		| checksum   |
		+------------+
		| 1171947116 |
		+------------+

		Notice: in the older tidb version, tidb will get different checksum with mysql, can see this issue pingcap/tidb#7446
	*/ // if don't have any data, the checksum will be `NULL`