This is an automated email from the ASF dual-hosted git repository.
kou pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/main by this push:
new c5800ec2a03 GH-55613: [C++][Gandiva] Fix wrong results for negative
integer constants (#55607)
c5800ec2a03 is described below
commit c5800ec2a03341138c0ba5965e2d13d4281b7efb
Author: Dmitry Chirkov <[email protected]>
AuthorDate: Fri Nov 27 06:56:26 2026 -0800
GH-55613: [C++][Gandiva] Fix wrong results for negative integer constants
(#55607)
### Rationale for this change
`LLVMTypes::int_constant` builds `llvm::APInt(N, val)` without the signed
flag. For a negative value narrower than 64 bits, the `APInt` has bits set
above its width, and an `i128` value is not sign-extended. The effect depends
on the LLVM version:
- LLVM 21, optimization on: Gandiva returns wrong results. For example `if
(k == 0) 10 else -5` returns `-5` for every row.
- LLVM 23: assertion `Value is not an N-bit unsigned value` in the `APInt`
constructor.
- LLVM 18: the constructor always clears the unused bits, so 16, 32 and 64
bit constants are correct. An `i128` constant is wrong on every version.
The same fix exists in the Dremio fork (dremio/arrow 3950ea6e41).
### What changes are included in this PR?
The fix is a one-line change in `llvm_types.h`: pass
`std::is_signed<ctype>::value` as the signed flag of `APInt` in
`LLVMTypes::int_constant`.
The PR has two commits: the tests first, then the fix.
### Are these changes tested?
Yes. Two new tests:
- `TestLLVMTypes.SignedIntegerConstants` checks the 8, 16, 32, 64 and 128
bit constants. Before the fix it fails on every LLVM version tested (18, 21,
23).
- `TestIfExpr.TestNegativeConstants` evaluates `if (k == 0) 10 else -5` and
`if (k == 0) -5 else 10` for `int16`, `int32` and `int64`, with optimization
off and on. Before the fix it returns wrong results on LLVM 21 and aborts on
LLVM 23. It passes on LLVM 18, where the `APInt` constructor already clears the
unused bits.
Both pass with the fix. The CI run on the tests-only commit shows the
results per platform.
### Are there any user-facing changes?
No.
**This PR contains a "Critical Fix".** It fixes a bug that produced
incorrect data on recent LLVM versions.
### Was AI used for this PR?
AI tools helped investigate the bug and write the tests and description.
**PR code and description written by:**
- [x] Human
- [x] AI
**Reviewed before submission by:**
- [x] Human
- [ ] AI
- [ ] Not reviewed
* GitHub Issue: #55613
Lead-authored-by: Dmitry Chirkov <[email protected]>
Co-authored-by: Logan Riggs <[email protected]>
Signed-off-by: Sutou Kouhei <[email protected]>
---
cpp/src/gandiva/llvm_types.h | 6 ++++-
cpp/src/gandiva/llvm_types_test.cc | 17 +++++++++++++
cpp/src/gandiva/tests/if_expr_test.cc | 46 +++++++++++++++++++++++++++++++++++
3 files changed, 68 insertions(+), 1 deletion(-)
diff --git a/cpp/src/gandiva/llvm_types.h b/cpp/src/gandiva/llvm_types.h
index 447989b52e0..c55f35e77b7 100644
--- a/cpp/src/gandiva/llvm_types.h
+++ b/cpp/src/gandiva/llvm_types.h
@@ -18,6 +18,7 @@
#pragma once
#include <map>
+#include <type_traits>
#include <vector>
#include "arrow/util/logging.h"
@@ -73,7 +74,10 @@ class GANDIVA_EXPORT LLVMTypes {
template <typename ctype, size_t N = (sizeof(ctype) * CHAR_BIT)>
llvm::Constant* int_constant(ctype val) {
- return llvm::ConstantInt::get(context_, llvm::APInt(N, val));
+ // Preserve signedness so negative narrow values are canonical APInts and
+ // negative values widened to i128 are sign-extended.
+ return llvm::ConstantInt::get(context_,
+ llvm::APInt(N, val,
std::is_signed<ctype>::value));
}
llvm::Constant* i1_constant(bool val) { return int_constant<bool, 1>(val); }
diff --git a/cpp/src/gandiva/llvm_types_test.cc
b/cpp/src/gandiva/llvm_types_test.cc
index 66696830618..45c53e0aec8 100644
--- a/cpp/src/gandiva/llvm_types_test.cc
+++ b/cpp/src/gandiva/llvm_types_test.cc
@@ -58,4 +58,21 @@ TEST_F(TestLLVMTypes, TestNotFound) {
EXPECT_EQ(types_->DataVecType(arrow::null()), nullptr);
}
+TEST_F(TestLLVMTypes, SignedIntegerConstants) {
+ auto value = [](llvm::Constant* constant) -> const llvm::APInt& {
+ return llvm::cast<llvm::ConstantInt>(constant)->getValue();
+ };
+ EXPECT_EQ(255U, value(types_->i8_constant(-1)).getZExtValue());
+ EXPECT_EQ(32768U, value(types_->i16_constant(-32768)).getZExtValue());
+ EXPECT_EQ(static_cast<uint32_t>(-447906508),
+ value(types_->i32_constant(-447906508)).getZExtValue());
+ EXPECT_EQ(int64_t{-447906508},
value(types_->i32_constant(-447906508)).getSExtValue());
+ EXPECT_TRUE(value(types_->i64_constant(-1)).isAllOnes());
+ EXPECT_TRUE(value(types_->i128_constant(-1)).isAllOnes());
+ EXPECT_EQ(int64_t{-447906508},
value(types_->i128_constant(-447906508)).getSExtValue());
+ EXPECT_EQ(0U, value(types_->i128_constant(1)).getHiBits(64).getZExtValue());
+ EXPECT_TRUE(value(types_->true_constant()).isOne());
+ EXPECT_TRUE(value(types_->false_constant()).isZero());
+}
+
} // namespace gandiva
diff --git a/cpp/src/gandiva/tests/if_expr_test.cc
b/cpp/src/gandiva/tests/if_expr_test.cc
index 54b6d43b4df..13c7190a90a 100644
--- a/cpp/src/gandiva/tests/if_expr_test.cc
+++ b/cpp/src/gandiva/tests/if_expr_test.cc
@@ -375,4 +375,50 @@ TEST_F(TestIfExpr, TestBigNested) {
EXPECT_ARROW_ARRAY_EQUALS(exp, outputs.at(0));
}
+namespace {
+
+template <typename ArrowType, typename CType>
+void CheckNegativeConstantResults(arrow::MemoryPool* pool, bool optimize) {
+ auto type = arrow::TypeTraits<ArrowType>::type_singleton();
+ SCOPED_TRACE(type->ToString());
+ auto key_field = field("k", type);
+ auto schema = arrow::schema({key_field});
+ auto key = TreeExprBuilder::MakeField(key_field);
+ auto is_zero = TreeExprBuilder::MakeFunction(
+ "equal", {key, TreeExprBuilder::MakeLiteral(CType{0})}, boolean());
+ auto ten = TreeExprBuilder::MakeLiteral(CType{10});
+ auto minus_five = TreeExprBuilder::MakeLiteral(CType{-5});
+
+ // if (k == 0) 10 else -5, and if (k == 0) -5 else 10
+ auto negative_else = TreeExprBuilder::MakeIf(is_zero, ten, minus_five, type);
+ auto negative_then = TreeExprBuilder::MakeIf(is_zero, minus_five, ten, type);
+
+ std::shared_ptr<Projector> projector;
+ ASSERT_OK(Projector::Make(
+ schema,
+ {TreeExprBuilder::MakeExpression(negative_else, field("negative_else",
type)),
+ TreeExprBuilder::MakeExpression(negative_then, field("negative_then",
type))},
+ ConfigurationBuilder().build(optimize), &projector));
+
+ auto input = MakeArrowArray<ArrowType, CType>({0, 1, 2});
+ auto batch = arrow::RecordBatch::Make(schema, 3, {input});
+ arrow::ArrayVector outputs;
+ ASSERT_OK(projector->Evaluate(*batch, pool, &outputs));
+ EXPECT_ARROW_ARRAY_EQUALS((MakeArrowArray<ArrowType, CType>({10, -5, -5})),
+ outputs.at(0));
+ EXPECT_ARROW_ARRAY_EQUALS((MakeArrowArray<ArrowType, CType>({-5, 10, 10})),
+ outputs.at(1));
+}
+
+} // namespace
+
+TEST_F(TestIfExpr, TestNegativeConstants) {
+ for (bool optimize : {false, true}) {
+ SCOPED_TRACE(optimize);
+ CheckNegativeConstantResults<arrow::Int32Type, int32_t>(pool_, optimize);
+ CheckNegativeConstantResults<arrow::Int16Type, int16_t>(pool_, optimize);
+ CheckNegativeConstantResults<arrow::Int64Type, int64_t>(pool_, optimize);
+ }
+}
+
} // namespace gandiva