Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IFNULL didn't work as expected. #11332

Closed
AndrewDi opened this issue Jul 19, 2019 · 2 comments
Closed

IFNULL didn't work as expected. #11332

AndrewDi opened this issue Jul 19, 2019 · 2 comments
Assignees
Labels
type/bug This issue is a bug.

Comments

@AndrewDi
Copy link
Contributor

AndrewDi commented Jul 19, 2019

Bug Report

Please answer these questions before submitting your issue. Thanks!

  1. What did you do?
    If possible, provide a recipe for reproducing the error.
tidb>create table task_progress(sum_record bigint(20) not null);
Query OK, 0 rows affected (0.00 sec)

tidb>select ifnull(max(sum_record),0),max(sum_record) from task_progress;
+---------------------------+-----------------+
| ifnull(max(sum_record),0) | max(sum_record) |
+---------------------------+-----------------+
|                      NULL |            NULL |
+---------------------------+-----------------+
1 row in set (0.00 sec)
  1. What did you expect to see?
    0

  2. What did you see instead?
    NULL

  3. What version of TiDB are you using (tidb-server -V or run select tidb_version(); on TiDB)?
    current master branch build

@AndrewDi AndrewDi added the type/bug This issue is a bug. label Jul 19, 2019
@XuHuaiyu
Copy link
Contributor

func (a *baseFuncDesc) typeInfer4MaxMin(ctx sessionctx.Context) {
	_, argIsScalaFunc := a.Args[0].(*expression.ScalarFunction)
	if argIsScalaFunc && a.Args[0].GetType().Tp == mysql.TypeFloat {
		// For scalar function, the result of "float32" is set to the "float64"
		// field in the "Datum". If we do not wrap a cast-as-double function on a.Args[0],
		// error would happen when extracting the evaluation of a.Args[0] to a ProjectionExec.
		tp := types.NewFieldType(mysql.TypeDouble)
		tp.Flen, tp.Decimal = mysql.MaxRealWidth, types.UnspecifiedLength
		types.SetBinChsClnFlag(tp)
		a.Args[0] = expression.BuildCastFunction(ctx, a.Args[0], tp)
	}
	a.RetTp = a.Args[0].GetType() // We should call a.Args[0].GetType().Clone() and reset the `NotNullFlag` for max/min in this line.
	if a.RetTp.Tp == mysql.TypeEnum || a.RetTp.Tp == mysql.TypeSet {
		a.RetTp = &types.FieldType{Tp: mysql.TypeString, Flen: mysql.MaxFieldCharLength}
	}
}

@AndrewDi
Copy link
Contributor Author

fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug This issue is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants