Skip to content

Commit

Permalink
ddl: tiny refine the checkForNullValue function. (pingcap#11833)
Browse files Browse the repository at this point in the history
change "select count(*) from `%s`.`%s` where `%s` is null limit 1"
to "select 1 from `%s`.`%s` where `%s` is null limit 1"

The later would return when the first row meets the condition, thus scanning the whole table is avoided
  • Loading branch information
jiyfhust authored and tiancaiamao committed Aug 23, 2019
1 parent 0ff5b50 commit 295033f
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,12 +473,12 @@ func (w *worker) doModifyColumn(t *meta.Meta, job *model.Job, newCol *model.Colu
// checkForNullValue ensure there are no null values of the column of this table.
// `isDataTruncated` indicates whether the new field and the old field type are the same, in order to be compatible with mysql.
func checkForNullValue(ctx sessionctx.Context, isDataTruncated bool, schema, table, oldCol, newCol model.CIStr) error {
sql := fmt.Sprintf("select count(*) from `%s`.`%s` where `%s` is null limit 1;", schema.L, table.L, oldCol.L)
sql := fmt.Sprintf("select 1 from `%s`.`%s` where `%s` is null limit 1;", schema.L, table.L, oldCol.L)
rows, _, err := ctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(ctx, sql)
if err != nil {
return errors.Trace(err)
}
rowCount := rows[0].GetInt64(0)
rowCount := len(rows)
if rowCount != 0 {
if isDataTruncated {
return errInvalidUseOfNull
Expand Down

0 comments on commit 295033f

Please sign in to comment.