Skip to content

Commit 499b8ab

Browse files
committed
expression: shield constant control dead string arms
1 parent df990c8 commit 499b8ab

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

pkg/expression/builtin_control.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,7 @@ func (c *caseWhenFunctionClass) getFunction(ctx BuildContext, args []Expression)
328328
selectedBranch := args[selectedBranchIdx]
329329
selectedBranchTp := selectedBranch.GetType(ctx.GetEvalCtx())
330330
_, selectedBranchIsConstant := selectedBranch.(*Constant)
331-
if selectedBranchTp.EvalType() != types.ETString &&
332-
selectedBranchTp.GetType() != mysql.TypeNull &&
331+
if selectedBranchTp.GetType() != mysql.TypeNull &&
333332
!selectedBranchIsConstant {
334333
// Do not let unreachable CASE result arms force casts or warnings on the selected branch.
335334
rewriteCaseWhenDeadBranches(args, selectedBranchIdx, selectedBranchTp.Clone())
@@ -766,7 +765,6 @@ func (c *ifFunctionClass) getFunction(ctx BuildContext, args []Expression) (sig
766765
selectedBranchTp := selectedBranch.GetType(ctx.GetEvalCtx())
767766
_, selectedBranchIsConstant := selectedBranch.(*Constant)
768767
if retTp.EvalType() == types.ETString &&
769-
selectedBranchTp.EvalType() != types.ETString &&
770768
selectedBranchTp.GetType() != mysql.TypeNull &&
771769
!selectedBranchIsConstant {
772770
retTp = selectedBranchTp.Clone()

pkg/expression/typeinfer_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,13 +844,17 @@ func (s *InferTypeSuite) createTestCase4ControlFuncs() []typeInferTestCase {
844844
{"if(null, 'dead', c_float_d)", mysql.TypeFloat, charset.CharsetBin, 0, 12, types.UnspecifiedLength},
845845
{"if(1, c_float_d, 'dead')", mysql.TypeFloat, charset.CharsetBin, 0, 12, types.UnspecifiedLength},
846846
{"if(0, 'dead', c_float_d)", mysql.TypeFloat, charset.CharsetBin, 0, 12, types.UnspecifiedLength},
847+
{"if(1, c_char, _binary'dead')", mysql.TypeString, charset.CharsetUTF8MB4, 0, 20, 0},
848+
{"if(0, _binary'dead', c_char)", mysql.TypeString, charset.CharsetUTF8MB4, 0, 20, 0},
847849
{"if(c_int_d, c_time_d, c_datetime)", mysql.TypeDatetime, charset.CharsetUTF8MB4, mysql.BinaryFlag, 22, 2}, // TODO: should not be BinaryFlag
848850
{"if(c_int_d, c_time, c_json)", mysql.TypeLongBlob, charset.CharsetUTF8MB4, 0, math.MaxUint32, types.UnspecifiedLength},
849851
{"if(null, null, null)", mysql.TypeNull, charset.CharsetBin, mysql.BinaryFlag, 0, 0},
850852
{"case when c_int_d then c_char else c_varchar end", mysql.TypeVarchar, charset.CharsetUTF8MB4, 0, 20, types.UnspecifiedLength},
851853
{"case when c_int_d > 1 then c_double_d else c_bchar end", mysql.TypeString, charset.CharsetUTF8MB4, mysql.BinaryFlag, 22, types.UnspecifiedLength},
852854
{"case when null then 'dead' else c_float_d end", mysql.TypeFloat, charset.CharsetBin, 0, 12, types.UnspecifiedLength},
853855
{"case when 1 then c_float_d else 'dead' end", mysql.TypeFloat, charset.CharsetBin, 0, 12, types.UnspecifiedLength},
856+
{"case when 1 then c_char else _binary'dead' end", mysql.TypeString, charset.CharsetUTF8MB4, 0, 20, 0},
857+
{"case when null then _binary'dead' else c_char end", mysql.TypeString, charset.CharsetUTF8MB4, 0, 20, 0},
854858
{"case when c_int_d > 2 then c_double_d when c_int_d < 1 then c_decimal else c_double_d end", mysql.TypeDouble, charset.CharsetBin, mysql.BinaryFlag, 22, types.UnspecifiedLength},
855859
{"case when c_double_d > 2 then c_decimal else 1 end", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag, 6, 3},
856860
{"case when c_time is not null then c_time else c_date end", mysql.TypeDatetime, charset.CharsetBin, mysql.BinaryFlag, mysql.MaxDatetimeWidthNoFsp + 3 + 1, 3},

0 commit comments

Comments
 (0)