Skip to content

Commit d84ed36

Browse files
authored
expresssion: fix div result type infer bug if a integer type has minimum display length (#56158) (#56211)
close #55837
1 parent fa40bca commit d84ed36

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

pkg/expression/builtin_arithmetic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,6 @@ func (c *arithmeticDivideFunctionClass) getFunction(ctx sessionctx.Context, args
653653
if err := c.verifyArgs(args); err != nil {
654654
return nil, err
655655
}
656-
lhsTp, rhsTp := args[0].GetType(), args[1].GetType()
657656
lhsEvalTp, rhsEvalTp := numericContextResultType(args[0]), numericContextResultType(args[1])
658657
if lhsEvalTp == types.ETReal || rhsEvalTp == types.ETReal {
659658
bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETReal, types.ETReal, types.ETReal)
@@ -669,6 +668,7 @@ func (c *arithmeticDivideFunctionClass) getFunction(ctx sessionctx.Context, args
669668
if err != nil {
670669
return nil, err
671670
}
671+
lhsTp, rhsTp := args[0].GetType(), args[1].GetType()
672672
c.setType4DivDecimal(bf.tp, lhsTp, rhsTp)
673673
sig := &builtinArithmeticDivideDecimalSig{bf}
674674
sig.setPbCode(tipb.ScalarFuncSig_DivideDecimal)

pkg/expression/typeinfer_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -753,19 +753,19 @@ func (s *InferTypeSuite) createTestCase4ArithmeticFuncs() []typeInferTestCase {
753753
{"c_double_d * c_char", mysql.TypeDouble, charset.CharsetBin, mysql.BinaryFlag, types.UnspecifiedLength, types.UnspecifiedLength},
754754
{"c_double_d * c_enum", mysql.TypeDouble, charset.CharsetBin, mysql.BinaryFlag, types.UnspecifiedLength, types.UnspecifiedLength},
755755

756-
{"c_int_d / c_int_d", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag, 16, 4},
757-
{"c_int_d / c_bigint_d", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag, 16, 4},
756+
{"c_int_d / c_int_d", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag, 15, 4},
757+
{"c_int_d / c_bigint_d", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag, 15, 4},
758758
{"c_int_d / c_char", mysql.TypeDouble, charset.CharsetBin, mysql.BinaryFlag, mysql.MaxRealWidth, types.UnspecifiedLength},
759-
{"c_int_d / c_time_d", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag, 16, 4},
759+
{"c_int_d / c_time_d", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag, 15, 4},
760760
{"c_int_d / c_double_d", mysql.TypeDouble, charset.CharsetBin, mysql.BinaryFlag, mysql.MaxRealWidth, types.UnspecifiedLength},
761-
{"c_int_d / c_decimal", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag, 19, 4},
761+
{"c_int_d / c_decimal", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag, 18, 4},
762762
{"c_datetime / c_decimal", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag, 29, 6}, // TODO: flen should be 25.
763763
{"c_bigint_d / c_decimal", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag, 28, 4}, // TODO: flen should be 28.
764764
{"c_double_d / c_decimal", mysql.TypeDouble, charset.CharsetBin, mysql.BinaryFlag, mysql.MaxRealWidth, types.UnspecifiedLength},
765765
{"c_double_d / c_char", mysql.TypeDouble, charset.CharsetBin, mysql.BinaryFlag, mysql.MaxRealWidth, types.UnspecifiedLength},
766766
{"c_double_d / c_enum", mysql.TypeDouble, charset.CharsetBin, mysql.BinaryFlag, mysql.MaxRealWidth, types.UnspecifiedLength},
767767
{"2/3", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag | mysql.NotNullFlag, 6, 4},
768-
{"-2/3", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag | mysql.NotNullFlag, 7, 4},
768+
{"-2/3", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag | mysql.NotNullFlag, 6, 4},
769769

770770
{"c_int_d DIV c_int_d", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag, mysql.MaxIntWidth, 0},
771771
{"c_uint_d DIV c_uint_d", mysql.TypeLonglong, charset.CharsetBin, mysql.UnsignedFlag | mysql.BinaryFlag, mysql.MaxIntWidth, 0},

tests/integrationtest/r/executor/issues.result

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,3 +780,9 @@ SELECT count(`t`.`c`) FROM (`s`) JOIN `t` GROUP BY `t`.`c`;
780780
count(`t`.`c`)
781781
170
782782
set @@tidb_max_chunk_size = default;
783+
CREATE TABLE test_55837 (col1 int(4) NOT NULL, col2 bigint(4) NOT NULL, KEY col2_index (col2));
784+
insert into test_55837 values(0,1725292800),(0,1725292800);
785+
select from_unixtime( if(col2 >9999999999, col2/1000, col2), '%Y-%m-%d %H:%i:%s') as result from test_55837;
786+
result
787+
2024-09-03 00:00:00
788+
2024-09-03 00:00:00

tests/integrationtest/t/executor/issues.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,3 +582,8 @@ insert into s values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),
582582
SELECT /*+ stream_agg()*/ count(`t`.`c`) FROM (`s`) JOIN `t` GROUP BY `t`.`c`;
583583
SELECT count(`t`.`c`) FROM (`s`) JOIN `t` GROUP BY `t`.`c`;
584584
set @@tidb_max_chunk_size = default;
585+
586+
# TestIssue55837
587+
CREATE TABLE test_55837 (col1 int(4) NOT NULL, col2 bigint(4) NOT NULL, KEY col2_index (col2));
588+
insert into test_55837 values(0,1725292800),(0,1725292800);
589+
select from_unixtime( if(col2 >9999999999, col2/1000, col2), '%Y-%m-%d %H:%i:%s') as result from test_55837;

0 commit comments

Comments
 (0)