MCPcopy Create free account
hub / github.com/apache/arrow / TEST_F

Function TEST_F

cpp/src/gandiva/tests/decimal_alignment_test.cc:113–156  ·  view source on GitHub ↗

Test that decimal operations work correctly with 8-byte aligned (but not 16-byte aligned) data

Source from the content-addressed store, hash-verified

111// Test that decimal operations work correctly with 8-byte aligned (but not 16-byte
112// aligned) data
113TEST_F(TestDecimalAlignment, TestMisalignedDecimalSubtract) {
114 constexpr int32_t precision = 38;
115 constexpr int32_t scale = 17;
116 auto decimal_type = std::make_shared<arrow::Decimal128Type>(precision, scale);
117 auto field_a = arrow::field("a", decimal_type);
118 auto field_b = arrow::field("b", decimal_type);
119 auto schema = arrow::schema({field_a, field_b});
120
121 Decimal128TypePtr output_type;
122 auto status = DecimalTypeUtil::GetResultType(
123 DecimalTypeUtil::kOpSubtract, {decimal_type, decimal_type}, &output_type);
124 ASSERT_OK(status);
125
126 auto res = arrow::field("res", output_type);
127 auto node_a = TreeExprBuilder::MakeField(field_a);
128 auto node_b = TreeExprBuilder::MakeField(field_b);
129 auto subtract =
130 TreeExprBuilder::MakeFunction("subtract", {node_a, node_b}, output_type);
131 auto expr = TreeExprBuilder::MakeExpression(subtract, res);
132
133 std::shared_ptr<Projector> projector;
134 status = Projector::Make(schema, {expr}, TestConfiguration(), &projector);
135 ASSERT_OK(status);
136
137 // Create test data
138 std::vector<Decimal128> values_a = {Decimal128(100), Decimal128(200), Decimal128(300)};
139 std::vector<Decimal128> values_b = {Decimal128(10), Decimal128(20), Decimal128(30)};
140
141 // Create arrays with 8-byte alignment (but NOT 16-byte aligned)
142 auto array_a = MakeMisalignedDecimalArray(decimal_type, values_a, 8);
143 auto array_b = MakeMisalignedDecimalArray(decimal_type, values_b, 8);
144
145 auto in_batch = arrow::RecordBatch::Make(schema, 3, {array_a, array_b});
146
147 // This should NOT crash even with misaligned data
148 arrow::ArrayVector outputs;
149 status = projector->Evaluate(*in_batch, pool_, &outputs);
150 ASSERT_OK(status);
151
152 // Verify results: 100-10=90, 200-20=180, 300-30=270
153 auto result = std::dynamic_pointer_cast<arrow::Decimal128Array>(outputs[0]);
154 ASSERT_NE(result, nullptr);
155 EXPECT_EQ(result->length(), 3);
156}
157
158// Create a misaligned output buffer for decimal128
159std::shared_ptr<arrow::ArrayData> MakeMisalignedDecimalOutput(

Callers

nothing calls this directly

Calls 10

TestConfigurationFunction · 0.85
fieldFunction · 0.50
schemaFunction · 0.50
MakeFunctionFunction · 0.50
MakeFunction · 0.50
Decimal128Class · 0.50
EvaluateMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected