From 46308eebe7ccd2d8d9474a79284d147d964c48d3 Mon Sep 17 00:00:00 2001 From: Dmitry Chirkov Date: Sat, 14 Nov 2026 12:44:51 -0800 Subject: [PATCH 1/2] GH-55613: [C++][Gandiva] Add tests for negative integer constants Add tests for the LLVM integer constants and for IF with negative results. The constants test fails on every LLVM version tested. The IF test fails or aborts on recent LLVM and passes on LLVM 18, until the next commit. --- cpp/src/gandiva/llvm_types_test.cc | 17 ++++++++++ cpp/src/gandiva/tests/if_expr_test.cc | 46 +++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/cpp/src/gandiva/llvm_types_test.cc b/cpp/src/gandiva/llvm_types_test.cc index 666968306182..45c53e0aec83 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(constant)->getValue(); + }; + EXPECT_EQ(255U, value(types_->i8_constant(-1)).getZExtValue()); + EXPECT_EQ(32768U, value(types_->i16_constant(-32768)).getZExtValue()); + EXPECT_EQ(static_cast(-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 54b6d43b4df1..13c7190a90a5 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 +void CheckNegativeConstantResults(arrow::MemoryPool* pool, bool optimize) { + auto type = arrow::TypeTraits::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; + 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({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({10, -5, -5})), + outputs.at(0)); + EXPECT_ARROW_ARRAY_EQUALS((MakeArrowArray({-5, 10, 10})), + outputs.at(1)); +} + +} // namespace + +TEST_F(TestIfExpr, TestNegativeConstants) { + for (bool optimize : {false, true}) { + SCOPED_TRACE(optimize); + CheckNegativeConstantResults(pool_, optimize); + CheckNegativeConstantResults(pool_, optimize); + CheckNegativeConstantResults(pool_, optimize); + } +} + } // namespace gandiva From fea0deb489712e30727d95dc07090c9b1b12215a Mon Sep 17 00:00:00 2001 From: Dmitry Chirkov Date: Sat, 14 Nov 2026 12:44:54 -0800 Subject: [PATCH 2/2] GH-55613: [C++][Gandiva] Pass signedness to APInt in LLVMTypes::int_constant LLVMTypes::int_constant built llvm::APInt(N, val) without the signed flag. For a negative value narrower than 64 bits the APInt had bits set above its width, which gives wrong results or an assertion on recent LLVM. An i128 value was not sign-extended. Co-authored-by: Logan Riggs --- cpp/src/gandiva/llvm_types.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cpp/src/gandiva/llvm_types.h b/cpp/src/gandiva/llvm_types.h index 447989b52e03..c55f35e77b77 100644 --- a/cpp/src/gandiva/llvm_types.h +++ b/cpp/src/gandiva/llvm_types.h @@ -18,6 +18,7 @@ #pragma once #include +#include #include #include "arrow/util/logging.h" @@ -73,7 +74,10 @@ class GANDIVA_EXPORT LLVMTypes { template 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::value)); } llvm::Constant* i1_constant(bool val) { return int_constant(val); }