Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cpp/src/gandiva/llvm_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include <map>
#include <type_traits>
#include <vector>

#include "arrow/util/logging.h"
Expand Down Expand Up @@ -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); }
Expand Down
17 changes: 17 additions & 0 deletions cpp/src/gandiva/llvm_types_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
46 changes: 46 additions & 0 deletions cpp/src/gandiva/tests/if_expr_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading