Skip to content

Commit 3935a70

Browse files
committed
Fix for -2**63
1 parent 671f05e commit 3935a70

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

ydb/library/yql/minikql/invoke_builtins/mkql_builtins_mul.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,20 @@ struct TNumMulInterval {
4343
if (rv == 0 || lv == 0) {
4444
return NUdf::TUnboxedValuePod(ret);
4545
}
46+
i64 i64Max = std::numeric_limits<i64>::max();
4647
if constexpr (std::is_same_v<ui64, typename TLeft::TLayout>) {
47-
if (left.Get<ui64>() > static_cast<ui64>(std::numeric_limits<i64>::max())) {
48+
if (left.Get<ui64>() >= static_cast<ui64>(i64Max)) {
4849
return NUdf::TUnboxedValuePod();
4950
}
5051
}
5152
if constexpr (std::is_same_v<ui64, typename TRight::TLayout>) {
52-
if (right.Get<ui64>() > static_cast<ui64>(std::numeric_limits<i64>::max())) {
53+
if (right.Get<ui64>() >= static_cast<ui64>(i64Max)) {
5354
return NUdf::TUnboxedValuePod();
5455
}
5556
}
56-
i64 lvAbs = (lv >= 0) ? lv : -lv;
57-
i64 rvAbs = (rv >= 0) ? rv : -rv;
58-
if (rvAbs != 0 && (std::numeric_limits<i64>::max() / rvAbs < lvAbs)) {
57+
auto div = i64Max / rv;
58+
auto divAbs = (div >= 0) ? div : -div;
59+
if ((lv >= 0) ? (lv > divAbs) : (lv < -divAbs)) {
5960
return NUdf::TUnboxedValuePod();
6061
}
6162
return IsBadInterval<TOutput>(ret) ? NUdf::TUnboxedValuePod() : NUdf::TUnboxedValuePod(ret);
@@ -85,23 +86,26 @@ struct TNumMulInterval {
8586

8687
block = bbMain;
8788

88-
const auto lhsAbs = SelectInst::Create(
89-
CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGE, lhs, zero, "lhsPos", block),
90-
lhs,
91-
BinaryOperator::CreateNeg(lhs, "lhsNeg", block),
92-
"lhsAbs", block);
93-
const auto rhsAbs = SelectInst::Create(
94-
CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGE, rhs, zero, "rhsPos", block),
95-
rhs,
96-
BinaryOperator::CreateNeg(rhs, "rhsNeg", block),
97-
"rhsAbs", block);
9889
const auto i64Max = ConstantInt::get(Type::getInt64Ty(context), std::numeric_limits<i64>::max());
99-
const auto div = BinaryOperator::CreateSDiv(i64Max, rhsAbs, "div", block);
100-
const auto mulOverflow = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGT, lhsAbs, div, "mulOverflow", block);
90+
const auto div = BinaryOperator::CreateSDiv(i64Max, rhs, "div", block);
91+
const auto divAbs = SelectInst::Create(
92+
CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGE, div, zero, "divPos", block),
93+
div,
94+
BinaryOperator::CreateNeg(div, "divNeg", block),
95+
"divAbs", block);
96+
const auto divAbsNeg = BinaryOperator::CreateNeg(divAbs, "divAbsNeg", block);
97+
98+
const auto mulOverflow = SelectInst::Create(
99+
CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGE, lhs, zero, "lhsPos", block),
100+
CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGT, lhs, divAbs, "lhsDiv", block),
101+
CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, lhs, divAbsNeg, "lhsDivAbsNeg", block),
102+
"mulOverflow", block);
103+
101104
const auto i64Overflow = BinaryOperator::CreateOr(
102105
GenIsInt64Overflow<typename TLeft::TLayout>(lv, context, block),
103106
GenIsInt64Overflow<typename TRight::TLayout>(rv, context, block),
104107
"i64Overflow", block);
108+
105109
const auto bad = BinaryOperator::CreateOr(
106110
BinaryOperator::CreateOr(i64Overflow, mulOverflow, "overflow", block),
107111
GenIsBadInterval<TOutput>(mul, context, block),

ydb/library/yql/tests/sql/hybrid_file/part7/canondata/result.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,9 @@
519519
],
520520
"test.test[bigdate-table_arithmetic_mul_div-default.txt-Debug]": [
521521
{
522-
"checksum": "1491822b38492114abf81faa61617ed7",
522+
"checksum": "e4b3b10b8e5b4327fbf645fdd5d954ef",
523523
"size": 30172,
524-
"uri": "https://{canondata_backend}/1881367/9f7e60d9f0b51c840a9c35afc7adcd160edd3a99/resource.tar.gz#test.test_bigdate-table_arithmetic_mul_div-default.txt-Debug_/opt.yql_patched"
524+
"uri": "https://{canondata_backend}/1946324/f50f0ddca01e8f702eb5a24b0934473d036b5007/resource.tar.gz#test.test_bigdate-table_arithmetic_mul_div-default.txt-Debug_/opt.yql_patched"
525525
}
526526
],
527527
"test.test[bigdate-table_arithmetic_mul_div-default.txt-Plan]": [

ydb/library/yql/tests/sql/yt_native_file/part6/canondata/result.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,9 @@
421421
],
422422
"test.test[bigdate-table_arithmetic_mul_div-default.txt-Debug]": [
423423
{
424-
"checksum": "0bd968024604066c96e2e0ca355b839c",
424+
"checksum": "428a68b06c3b405e0087538758ca7e4c",
425425
"size": 24400,
426-
"uri": "https://{canondata_backend}/1899731/7e12bbd97e6ef42ea9512918c50ff003ad37ded8/resource.tar.gz#test.test_bigdate-table_arithmetic_mul_div-default.txt-Debug_/opt.yql"
426+
"uri": "https://{canondata_backend}/1917492/30b5bc2d6861ed60772eb6885e2fd9071a8898c0/resource.tar.gz#test.test_bigdate-table_arithmetic_mul_div-default.txt-Debug_/opt.yql"
427427
}
428428
],
429429
"test.test[bigdate-table_arithmetic_mul_div-default.txt-Plan]": [
@@ -435,9 +435,9 @@
435435
],
436436
"test.test[bigdate-table_arithmetic_mul_div-default.txt-Results]": [
437437
{
438-
"checksum": "94ea4fca6d839c1bf7e0da1f0df49929",
439-
"size": 662551,
440-
"uri": "https://{canondata_backend}/1899731/7e12bbd97e6ef42ea9512918c50ff003ad37ded8/resource.tar.gz#test.test_bigdate-table_arithmetic_mul_div-default.txt-Results_/results.txt"
438+
"checksum": "785fb2e27cc00e47a21316d75d79a7b5",
439+
"size": 662203,
440+
"uri": "https://{canondata_backend}/1917492/30b5bc2d6861ed60772eb6885e2fd9071a8898c0/resource.tar.gz#test.test_bigdate-table_arithmetic_mul_div-default.txt-Results_/results.txt"
441441
}
442442
],
443443
"test.test[bigdate-table_arithmetic_narrow-default.txt-Debug]": [

0 commit comments

Comments
 (0)