Skip to content

Commit e51c992

Browse files
committed
[fix][libexpr] Fix float/double cast to decimal issue.
1 parent 0cc3717 commit e51c992

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/expr/calc/casting.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,12 @@ DecimalP Cast(bool v) {
188188

189189
template <>
190190
DecimalP Cast(float v) {
191-
String const f2s = CastFloat(v);
192-
return DecimalP(*f2s.GetPtr());
191+
return DecimalP(v);
193192
}
194193

195194
template <>
196195
DecimalP Cast(double v) {
197-
String const d2s = CastDouble(v);
198-
return DecimalP(*d2s.GetPtr());
196+
return DecimalP(v);
199197
}
200198

201199
template <>

test/rel/test_rel.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,20 +203,32 @@ INSTANTIATE_TEST_SUITE_P(
203203
new Tuple{nullptr},
204204
}
205205
),
206+
/* create table t1(id int, a double, primary key(id);
207+
* insert into t1 values(1, 5.8);
208+
* select * from t1 where a >= 5.8 and a <= 5.8.
209+
* The expected result is null.
210+
* It is compatible with mysql and dingodb scenario when making decimal pushdown disable.
211+
*/
206212
std::make_tuple(
207213
"713501F0651603352E3892063501F0651603352E3894065200",
208214
MakeDataForDoubleToDecimal(),
209215
Data{
210216
//The origin string is longer than range.
211-
new Tuple{1, 5.8},
217+
nullptr,
212218
}
213219
),
220+
/* create table t1(id int, a double, primary key(id);
221+
* insert into t1 values(1, 5.8);
222+
* select * from t1 where a >= 5.8 and a <= 5.8.
223+
* The expected result is null.
224+
* It is compatible with mysql and dingodb scenario when making decimal pushdown disable.
225+
*/
214226
std::make_tuple(
215227
"713401F0641603352E3892063401F0641603352E3894065200",
216228
MakeDataForFloatToDecimal(),
217229
Data{
218230
//The origin string is longer than range.
219-
new Tuple{1, (float)5.8},
231+
nullptr,
220232
}
221233
),
222234
// PROJECT(input, instr($[1]))

test/types/test_type_decimal.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,10 @@ TEST(TestTypeDecimal, DecimalTest) {
385385
//Test -dec 0.
386386
ASSERT_EQ((-Decimal(std::string("0"))).toLong(), 0);
387387

388+
//Test decimal to double
389+
DecimalP dp = DecimalP((double)32.91);
390+
ASSERT_EQ(dp.GetPtr()->toString(), std::string("32.9099999999999965894"));
391+
388392
//DecimalP p = DecimalP((float)12.34);
389393
ASSERT_EQ(DecimalP((long)12).GetPtr()->toString(10,4), std::string("12.0000"));
390394
ASSERT_EQ(DecimalP((float)12.34).GetPtr()->toString(10,4), std::string("12.3400"));

0 commit comments

Comments
 (0)