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

Function MakeMisalignedDecimalOutput

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

Create a misaligned output buffer for decimal128

Source from the content-addressed store, hash-verified

157
158// Create a misaligned output buffer for decimal128
159std::shared_ptr<arrow::ArrayData> MakeMisalignedDecimalOutput(
160 const std::shared_ptr<arrow::Decimal128Type>& type, int64_t num_records,
161 int alignment_offset) {
162 // Allocate data buffer with extra space for misalignment
163 int64_t data_size = num_records * 16; // 16 bytes per Decimal128
164 int64_t buffer_size = data_size + 16; // Extra space for offset
165
166 std::shared_ptr<arrow::Buffer> buffer;
167 ARROW_EXPECT_OK(arrow::AllocateBuffer(buffer_size).Value(&buffer));
168
169 uint8_t* raw_data = const_cast<uint8_t*>(buffer->data());
170 uintptr_t addr = reinterpret_cast<uintptr_t>(raw_data);
171
172 // Find offset to get to 8-byte aligned but not 16-byte aligned address
173 int offset_to_8 = (8 - (addr % 8)) % 8;
174 int current_16_alignment = (addr + offset_to_8) % 16;
175
176 int final_offset;
177 if (alignment_offset == 8) {
178 if (current_16_alignment == 0) {
179 final_offset = offset_to_8 + 8;
180 } else {
181 final_offset = offset_to_8;
182 }
183 } else {
184 final_offset = (16 - (addr % 16)) % 16;
185 }
186
187 // Verify alignment
188 uintptr_t data_addr = reinterpret_cast<uintptr_t>(raw_data + final_offset);
189 EXPECT_EQ(data_addr % 8, 0) << "Data should be 8-byte aligned";
190 if (alignment_offset == 8) {
191 EXPECT_NE(data_addr % 16, 0) << "Data should NOT be 16-byte aligned";
192 }
193
194 auto sliced_buffer = arrow::SliceBuffer(buffer, final_offset, data_size);
195
196 // Create validity buffer
197 int64_t bitmap_size = (num_records + 7) / 8;
198 std::shared_ptr<arrow::Buffer> validity_buffer;
199 ARROW_EXPECT_OK(arrow::AllocateBuffer(bitmap_size).Value(&validity_buffer));
200 memset(const_cast<uint8_t*>(validity_buffer->data()), 0xFF, validity_buffer->size());
201
202 return arrow::ArrayData::Make(type, num_records, {validity_buffer, sliced_buffer});
203}
204
205// Test that decimal STORES work correctly with 8-byte aligned (but not 16-byte aligned)
206// output

Callers 1

TEST_FFunction · 0.85

Calls 6

SliceBufferFunction · 0.85
AllocateBufferFunction · 0.50
MakeFunction · 0.50
ValueMethod · 0.45
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected