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 +#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); } 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(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 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 +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