Skip to content

Commit 03576af

Browse files
praveenbingokou
authored andcommitted
ARROW-5824: [Gandiva][C++] Fix decimal null literals.
- Type was hard coded as 0,0 - Use the input type for literals instead. Author: Praveen <praveen@dremio.com> Closes #4780 from praveenbingo/ARROW-5824 and squashes the following commits: 2fa0eeb <Praveen> Address review comments. d56918c <Praveen> Fix lint issues. 41b1b0f <Praveen> ARROW-5824: Fix decimal null literals.
1 parent 643989e commit 03576af

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

cpp/src/gandiva/tests/decimal_test.cc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,4 +787,60 @@ TEST_F(TestDecimal, TestHash64WithSeed) {
787787
// hash with, without seed are not equal
788788
EXPECT_NE(int64_arr_WS->Value(4), int64_arr->Value(4));
789789
}
790+
791+
TEST_F(TestDecimal, TestNullDecimalConstant) {
792+
// schema for input fields
793+
constexpr int32_t precision = 36;
794+
constexpr int32_t scale = 18;
795+
auto decimal_type = std::make_shared<arrow::Decimal128Type>(precision, scale);
796+
auto field_b = field("b", decimal_type);
797+
auto field_c = field("c", arrow::boolean());
798+
auto schema = arrow::schema({field_b, field_c});
799+
800+
// output fields
801+
auto field_result = field("res", decimal_type);
802+
803+
// build expression.
804+
// if (c)
805+
// null
806+
// else
807+
// b
808+
auto node_a = TreeExprBuilder::MakeNull(decimal_type);
809+
auto node_b = TreeExprBuilder::MakeField(field_b);
810+
auto node_c = TreeExprBuilder::MakeField(field_c);
811+
auto if_node = TreeExprBuilder::MakeIf(node_c, node_a, node_b, decimal_type);
812+
813+
auto expr = TreeExprBuilder::MakeExpression(if_node, field_result);
814+
815+
// Build a projector for the expressions.
816+
std::shared_ptr<Projector> projector;
817+
Status status = Projector::Make(schema, {expr}, TestConfiguration(), &projector);
818+
DCHECK_OK(status);
819+
820+
// Create a row-batch with some sample data
821+
int num_records = 4;
822+
823+
auto array_b =
824+
MakeArrowArrayDecimal(decimal_type, MakeDecimalVector({"2", "3", "4", "5"}, scale),
825+
{true, true, true, true});
826+
827+
auto array_c = MakeArrowArrayBool({true, false, true, false}, {true, true, true, true});
828+
829+
// expected output
830+
auto exp =
831+
MakeArrowArrayDecimal(decimal_type, MakeDecimalVector({"0", "3", "3", "5"}, scale),
832+
{false, true, false, true});
833+
834+
// prepare input record batch
835+
auto in_batch = arrow::RecordBatch::Make(schema, num_records, {array_b, array_c});
836+
837+
// Evaluate expression
838+
arrow::ArrayVector outputs;
839+
status = projector->Evaluate(*in_batch, pool_, &outputs);
840+
DCHECK_OK(status);
841+
842+
// Validate results
843+
EXPECT_ARROW_ARRAY_EQUALS(exp, outputs.at(0));
844+
}
845+
790846
} // namespace gandiva

cpp/src/gandiva/tree_expr_builder.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ NodePtr TreeExprBuilder::MakeNull(DataTypePtr data_type) {
9999
case arrow::Type::TIMESTAMP:
100100
return std::make_shared<LiteralNode>(data_type, LiteralHolder((int64_t)0), true);
101101
case arrow::Type::DECIMAL: {
102-
DecimalScalar128 literal(0, 0);
102+
std::shared_ptr<arrow::DecimalType> decimal_type =
103+
arrow::internal::checked_pointer_cast<arrow::DecimalType>(data_type);
104+
DecimalScalar128 literal(decimal_type->precision(), decimal_type->scale());
103105
return std::make_shared<LiteralNode>(data_type, LiteralHolder(literal), true);
104106
}
105107
default:

0 commit comments

Comments
 (0)