Skip to content

Commit

Permalink
set right null flag for constant value
Browse files Browse the repository at this point in the history
  • Loading branch information
fzhedu committed Mar 8, 2021
1 parent 5a0ead5 commit d8a1eb9
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 121 deletions.
2 changes: 1 addition & 1 deletion cmd/explaintest/r/explain.result
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ drop view if exists v;
create view v as select cast(replace(substring_index(substring_index("",',',1),':',-1),'"','') as CHAR(32)) as event_id;
desc v;
Field Type Null Key Default Extra
event_id varchar(32) YES NULL
event_id varchar(32) NO NULL
13 changes: 7 additions & 6 deletions cmd/explaintest/r/explain_easy.result
Original file line number Diff line number Diff line change
Expand Up @@ -613,14 +613,15 @@ HashJoin 8002.00 root right outer join, equal:[eq(test.t.nb, test.t.nb)]
explain format = 'brief' select ifnull(t.a, 1) in (select count(*) from t s , t t1 where s.a = t.a and s.a = t1.a) from t;
id estRows task access object operator info
Projection 10000.00 root Column#14
└─Apply 10000.00 root CARTESIAN left outer semi join, other cond:eq(ifnull(test.t.a, 1), Column#13)
├─TableReader(Build) 10000.00 root data:TableFullScan
│ └─TableFullScan 10000.00 cop[tikv] table:t keep order:false, stats:pseudo
└─HashAgg(Probe) 1.00 root funcs:count(Column#15)->Column#13
└─Apply 10000.00 root left outer semi join, equal:[eq(Column#15, Column#13)]
├─Projection(Build) 10000.00 root test.t.a, ifnull(test.t.a, 1)->Column#15
│ └─TableReader 10000.00 root data:TableFullScan
│ └─TableFullScan 10000.00 cop[tikv] table:t keep order:false, stats:pseudo
└─HashAgg(Probe) 1.00 root funcs:count(Column#17)->Column#13
└─HashJoin 9.99 root inner join, equal:[eq(test.t.a, test.t.a)]
├─HashAgg(Build) 7.99 root group by:test.t.a, funcs:count(Column#16)->Column#15, funcs:firstrow(test.t.a)->test.t.a
├─HashAgg(Build) 7.99 root group by:test.t.a, funcs:count(Column#18)->Column#17, funcs:firstrow(test.t.a)->test.t.a
│ └─TableReader 7.99 root data:HashAgg
│ └─HashAgg 7.99 cop[tikv] group by:test.t.a, funcs:count(1)->Column#16
│ └─HashAgg 7.99 cop[tikv] group by:test.t.a, funcs:count(1)->Column#18
│ └─Selection 9.99 cop[tikv] eq(test.t.a, test.t.a), not(isnull(test.t.a))
│ └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo
└─TableReader(Probe) 9.99 root data:Selection
Expand Down
15 changes: 13 additions & 2 deletions expression/constant_fold.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package expression
import (
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
Expand Down Expand Up @@ -207,14 +208,24 @@ func foldConstant(expr Expression) (Expression, bool) {
return expr, isDeferredConst
}
value, err := x.Eval(chunk.Row{})
retType := x.RetType.Clone()
if !hasNullArg {
// set right not null flag for constant value
switch value.Kind() {
case types.KindNull:
retType.Flag &= ^mysql.NotNullFlag
default:
retType.Flag |= mysql.NotNullFlag
}
}
if err != nil {
logutil.BgLogger().Debug("fold expression to constant", zap.String("expression", x.ExplainInfo()), zap.Error(err))
return expr, isDeferredConst
}
if isDeferredConst {
return &Constant{Value: value, RetType: x.RetType, DeferredExpr: x}, true
return &Constant{Value: value, RetType: retType, DeferredExpr: x}, true
}
return &Constant{Value: value, RetType: x.RetType}, false
return &Constant{Value: value, RetType: retType}, false
case *Constant:
if x.ParamMarker != nil {
return &Constant{
Expand Down
3 changes: 0 additions & 3 deletions expression/expr_to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ func (pc PbConverter) ExprToPB(expr Expression) *tipb.Expr {
if pbExpr == nil {
return nil
}
if !x.Value.IsNull() {
pbExpr.FieldType.Flag |= uint32(mysql.NotNullFlag)
}
return pbExpr
case *CorrelatedColumn:
return pc.conOrCorColToPBExpr(expr)
Expand Down
1 change: 1 addition & 0 deletions expression/expr_to_pb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func (s *testEvaluatorSuite) TestLikeFunc2Pb(c *C) {
client := new(mock.Client)

retTp := types.NewFieldType(mysql.TypeString)
retTp.Flag |= mysql.NotNullFlag
retTp.Charset = charset.CharsetUTF8
retTp.Collate = charset.CollationUTF8
args := []Expression{
Expand Down
Loading

0 comments on commit d8a1eb9

Please sign in to comment.