forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apacheGH-39577: [C++] Fix tail-word access cross buffer boundary in `…
…CompareBinaryColumnToRow` (apache#39606) ### Rationale for this change Default buffer alignment (64b) doesn't guarantee the safety of tail-word access in `KeyCompare::CompareBinaryColumnToRow`. Comment apache#39577 (comment) is a concrete example. ### What changes are included in this PR? Make `KeyCompare::CompareBinaryColumnToRow` tail-word safe. ### Are these changes tested? UT included. ### Are there any user-facing changes? No. * Closes: apache#39577 Authored-by: zanmato1984 <[email protected]> Signed-off-by: Antoine Pitrou <[email protected]>
- Loading branch information
1 parent
980e7d7
commit cd3321b
Showing
3 changed files
with
118 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#include <numeric> | ||
|
||
#include "arrow/compute/row/compare_internal.h" | ||
#include "arrow/testing/gtest_util.h" | ||
|
||
namespace arrow { | ||
namespace compute { | ||
|
||
using arrow::bit_util::BytesForBits; | ||
using arrow::internal::CpuInfo; | ||
using arrow::util::MiniBatch; | ||
using arrow::util::TempVectorStack; | ||
|
||
// Specialized case for GH-39577. | ||
TEST(KeyCompare, CompareColumnsToRowsCuriousFSB) { | ||
int fsb_length = 9; | ||
MemoryPool* pool = default_memory_pool(); | ||
TempVectorStack stack; | ||
ASSERT_OK(stack.Init(pool, 8 * MiniBatch::kMiniBatchLength * sizeof(uint64_t))); | ||
|
||
int num_rows = 7; | ||
auto column_right = ArrayFromJSON(fixed_size_binary(fsb_length), R"([ | ||
"000000000", | ||
"111111111", | ||
"222222222", | ||
"333333333", | ||
"444444444", | ||
"555555555", | ||
"666666666"])"); | ||
ExecBatch batch_right({column_right}, num_rows); | ||
|
||
std::vector<KeyColumnMetadata> column_metadatas_right; | ||
ASSERT_OK(ColumnMetadatasFromExecBatch(batch_right, &column_metadatas_right)); | ||
|
||
RowTableMetadata table_metadata_right; | ||
table_metadata_right.FromColumnMetadataVector(column_metadatas_right, sizeof(uint64_t), | ||
sizeof(uint64_t)); | ||
|
||
std::vector<KeyColumnArray> column_arrays_right; | ||
ASSERT_OK(ColumnArraysFromExecBatch(batch_right, &column_arrays_right)); | ||
|
||
RowTableImpl row_table; | ||
ASSERT_OK(row_table.Init(pool, table_metadata_right)); | ||
|
||
RowTableEncoder row_encoder; | ||
row_encoder.Init(column_metadatas_right, sizeof(uint64_t), sizeof(uint64_t)); | ||
row_encoder.PrepareEncodeSelected(0, num_rows, column_arrays_right); | ||
|
||
std::vector<uint16_t> row_ids_right(num_rows); | ||
std::iota(row_ids_right.begin(), row_ids_right.end(), 0); | ||
ASSERT_OK(row_encoder.EncodeSelected(&row_table, num_rows, row_ids_right.data())); | ||
|
||
auto column_left = ArrayFromJSON(fixed_size_binary(fsb_length), R"([ | ||
"000000000", | ||
"111111111", | ||
"222222222", | ||
"333333333", | ||
"444444444", | ||
"555555555", | ||
"777777777"])"); | ||
ExecBatch batch_left({column_left}, num_rows); | ||
std::vector<KeyColumnArray> column_arrays_left; | ||
ASSERT_OK(ColumnArraysFromExecBatch(batch_left, &column_arrays_left)); | ||
|
||
std::vector<uint32_t> row_ids_left(num_rows); | ||
std::iota(row_ids_left.begin(), row_ids_left.end(), 0); | ||
|
||
LightContext ctx{CpuInfo::GetInstance()->hardware_flags(), &stack}; | ||
|
||
{ | ||
uint32_t num_rows_no_match; | ||
std::vector<uint16_t> row_ids_out(num_rows); | ||
KeyCompare::CompareColumnsToRows(num_rows, NULLPTR, row_ids_left.data(), &ctx, | ||
&num_rows_no_match, row_ids_out.data(), | ||
column_arrays_left, row_table, true, NULLPTR); | ||
ASSERT_EQ(num_rows_no_match, 1); | ||
ASSERT_EQ(row_ids_out[0], 6); | ||
} | ||
|
||
{ | ||
std::vector<uint8_t> match_bitvector(BytesForBits(num_rows)); | ||
KeyCompare::CompareColumnsToRows(num_rows, NULLPTR, row_ids_left.data(), &ctx, | ||
NULLPTR, NULLPTR, column_arrays_left, row_table, | ||
true, match_bitvector.data()); | ||
for (int i = 0; i < num_rows; ++i) { | ||
SCOPED_TRACE(i); | ||
ASSERT_EQ(arrow::bit_util::GetBit(match_bitvector.data(), i), i != 6); | ||
} | ||
} | ||
} | ||
|
||
} // namespace compute | ||
} // namespace arrow |