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
50 changes: 26 additions & 24 deletions cpp/src/arrow/compute/row/compare_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,11 @@ void KeyCompare::CompareBinaryColumnToRowHelper(
}

template <bool use_selection>
void KeyCompare::CompareBinaryColumnToRow(uint32_t offset_within_row,
uint32_t num_rows_to_compare,
const uint16_t* sel_left_maybe_null,
const uint32_t* left_to_right_map,
LightContext* ctx, const KeyColumnArray& col,
const RowTableImpl& rows,
uint8_t* match_bytevector) {
void KeyCompare::CompareBinaryColumnToRow(
uint32_t offset_within_row, uint32_t num_rows_to_compare,
const uint16_t* sel_left_maybe_null, const uint32_t* left_to_right_map,
LightContext* ctx, const KeyColumnArray& col, const RowTableImpl& rows,
bool are_cols_in_encoding_order, uint8_t* match_bytevector) {
uint32_t num_processed = 0;
#if defined(ARROW_HAVE_RUNTIME_AVX2)
if (ctx->has_avx2()) {
Expand Down Expand Up @@ -165,10 +163,11 @@ void KeyCompare::CompareBinaryColumnToRow(uint32_t offset_within_row,
CompareBinaryColumnToRowHelper<use_selection>(
offset_within_row, num_processed, num_rows_to_compare, sel_left_maybe_null,
left_to_right_map, ctx, col, rows, match_bytevector,
[](const uint8_t* left_base, const uint8_t* right_base, uint32_t irow_left,
uint32_t offset_right) {
util::CheckAlignment<uint16_t>(left_base);
util::CheckAlignment<uint16_t>(right_base + offset_right);
[=](const uint8_t* left_base, const uint8_t* right_base, uint32_t irow_left,
uint32_t offset_right) {
util::CheckAlignment<uint16_t>(left_base, are_cols_in_encoding_order);
util::CheckAlignment<uint16_t>(right_base + offset_right,
are_cols_in_encoding_order);
uint16_t left = reinterpret_cast<const uint16_t*>(left_base)[irow_left];
uint16_t right = *reinterpret_cast<const uint16_t*>(right_base + offset_right);
return left == right ? 0xff : 0;
Expand All @@ -177,10 +176,11 @@ void KeyCompare::CompareBinaryColumnToRow(uint32_t offset_within_row,
CompareBinaryColumnToRowHelper<use_selection>(
offset_within_row, num_processed, num_rows_to_compare, sel_left_maybe_null,
left_to_right_map, ctx, col, rows, match_bytevector,
[](const uint8_t* left_base, const uint8_t* right_base, uint32_t irow_left,
uint32_t offset_right) {
util::CheckAlignment<uint32_t>(left_base);
util::CheckAlignment<uint32_t>(right_base + offset_right);
[=](const uint8_t* left_base, const uint8_t* right_base, uint32_t irow_left,
uint32_t offset_right) {
util::CheckAlignment<uint32_t>(left_base, are_cols_in_encoding_order);
util::CheckAlignment<uint32_t>(right_base + offset_right,
are_cols_in_encoding_order);
uint32_t left = reinterpret_cast<const uint32_t*>(left_base)[irow_left];
uint32_t right = *reinterpret_cast<const uint32_t*>(right_base + offset_right);
return left == right ? 0xff : 0;
Expand All @@ -189,10 +189,11 @@ void KeyCompare::CompareBinaryColumnToRow(uint32_t offset_within_row,
CompareBinaryColumnToRowHelper<use_selection>(
offset_within_row, num_processed, num_rows_to_compare, sel_left_maybe_null,
left_to_right_map, ctx, col, rows, match_bytevector,
[](const uint8_t* left_base, const uint8_t* right_base, uint32_t irow_left,
uint32_t offset_right) {
util::CheckAlignment<uint64_t>(left_base);
util::CheckAlignment<uint64_t>(right_base + offset_right);
[=](const uint8_t* left_base, const uint8_t* right_base, uint32_t irow_left,
uint32_t offset_right) {
util::CheckAlignment<uint64_t>(left_base, are_cols_in_encoding_order);
util::CheckAlignment<uint64_t>(right_base + offset_right,
are_cols_in_encoding_order);
uint64_t left = reinterpret_cast<const uint64_t*>(left_base)[irow_left];
uint64_t right = *reinterpret_cast<const uint64_t*>(right_base + offset_right);
return left == right ? 0xff : 0;
Expand All @@ -201,8 +202,8 @@ void KeyCompare::CompareBinaryColumnToRow(uint32_t offset_within_row,
CompareBinaryColumnToRowHelper<use_selection>(
offset_within_row, num_processed, num_rows_to_compare, sel_left_maybe_null,
left_to_right_map, ctx, col, rows, match_bytevector,
[&col](const uint8_t* left_base, const uint8_t* right_base, uint32_t irow_left,
uint32_t offset_right) {
[=, &col](const uint8_t* left_base, const uint8_t* right_base, uint32_t irow_left,
uint32_t offset_right) {
uint32_t length = col.metadata().fixed_length;

// Non-zero length guarantees no underflow
Expand All @@ -212,7 +213,8 @@ void KeyCompare::CompareBinaryColumnToRow(uint32_t offset_within_row,

const uint64_t* key_left_ptr =
reinterpret_cast<const uint64_t*>(left_base + irow_left * length);
util::CheckAlignment<uint64_t>(right_base + offset_right);
util::CheckAlignment<uint64_t>(right_base + offset_right,
are_cols_in_encoding_order);
const uint64_t* key_right_ptr =
reinterpret_cast<const uint64_t*>(right_base + offset_right);
uint64_t result_or = 0;
Expand Down Expand Up @@ -370,7 +372,7 @@ void KeyCompare::CompareColumnsToRows(
if (sel_left_maybe_null) {
CompareBinaryColumnToRow<true>(
offset_within_row, num_rows_to_compare, sel_left_maybe_null,
left_to_right_map, ctx, col, rows,
left_to_right_map, ctx, col, rows, are_cols_in_encoding_order,
is_first_column ? match_bytevector_A : match_bytevector_B);
NullUpdateColumnToRow<true>(
static_cast<uint32_t>(icol), num_rows_to_compare, sel_left_maybe_null,
Expand All @@ -380,7 +382,7 @@ void KeyCompare::CompareColumnsToRows(
// Version without using selection vector
CompareBinaryColumnToRow<false>(
offset_within_row, num_rows_to_compare, sel_left_maybe_null,
left_to_right_map, ctx, col, rows,
left_to_right_map, ctx, col, rows, are_cols_in_encoding_order,
is_first_column ? match_bytevector_A : match_bytevector_B);
NullUpdateColumnToRow<false>(
static_cast<uint32_t>(icol), num_rows_to_compare, sel_left_maybe_null,
Expand Down
12 changes: 5 additions & 7 deletions cpp/src/arrow/compute/row/compare_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,11 @@ class ARROW_EXPORT KeyCompare {
const RowTableImpl& rows, uint8_t* match_bytevector, COMPARE_FN compare_fn);

template <bool use_selection>
static void CompareBinaryColumnToRow(uint32_t offset_within_row,
uint32_t num_rows_to_compare,
const uint16_t* sel_left_maybe_null,
const uint32_t* left_to_right_map,
LightContext* ctx, const KeyColumnArray& col,
const RowTableImpl& rows,
uint8_t* match_bytevector);
static void CompareBinaryColumnToRow(
uint32_t offset_within_row, uint32_t num_rows_to_compare,
const uint16_t* sel_left_maybe_null, const uint32_t* left_to_right_map,
LightContext* ctx, const KeyColumnArray& col, const RowTableImpl& rows,
bool are_cols_in_encoding_order, uint8_t* match_bytevector);

template <bool use_selection, bool is_first_varbinary_col>
static void CompareVarBinaryColumnToRowHelper(
Expand Down
63 changes: 63 additions & 0 deletions cpp/src/arrow/compute/row/compare_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,68 @@ TEST(KeyCompare, CompareColumnsToRowsTempStackUsage) {
}
}

TEST(KeyCompare, CompareColumnsWithEncodingOrder) {
const int num_rows = 5;

const auto i32_col = ArrayFromJSON(int32(), "[0, 1, 2, 3, 4]");
const auto i64_col = ArrayFromJSON(int64(), "[7, 8, 9, 10, 11]");

std::vector<ExecBatch> batches = {ExecBatch({i32_col, i64_col}, num_rows),
ExecBatch({i64_col, i32_col}, num_rows)};
int batch_idx = 0;
for (const auto& batch : batches) {
SCOPED_TRACE("batch idx = " + std::to_string(batch_idx));

MemoryPool* pool = default_memory_pool();
TempVectorStack stack;
ASSERT_OK(stack.Init(pool, KeyCompare::CompareColumnsToRowsTempStackUsage(num_rows)));

std::vector<KeyColumnMetadata> r_col_metas;
ASSERT_OK(ColumnMetadatasFromExecBatch(batch, &r_col_metas));

RowTableMetadata r_table_meta;
r_table_meta.FromColumnMetadataVector(r_col_metas, sizeof(uint64_t),
sizeof(uint64_t));

std::vector<KeyColumnArray> r_column_arrays;
ASSERT_OK(ColumnArraysFromExecBatch(batch, &r_column_arrays));

RowTableImpl row_table;
ASSERT_OK(row_table.Init(pool, r_table_meta));

RowTableEncoder row_encoder;
row_encoder.Init(r_col_metas, sizeof(uint64_t), sizeof(uint64_t));
row_encoder.PrepareEncodeSelected(0, num_rows, r_column_arrays);

std::vector<uint16_t> r_row_ids(num_rows);
std::iota(r_row_ids.begin(), r_row_ids.end(), 0);
ASSERT_OK(row_encoder.EncodeSelected(&row_table, num_rows, r_row_ids.data()));

std::vector<KeyColumnArray> l_column_arrays;
// Input left batch should always be 'i64_col,i32_col' order.
ASSERT_OK(ColumnArraysFromExecBatch(batches[1], &l_column_arrays));

std::vector<uint32_t> l_row_ids(num_rows);
std::iota(l_row_ids.begin(), l_row_ids.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, l_row_ids.data(), &ctx, &num_rows_no_match, row_ids_out.data(),
l_column_arrays, row_table, r_table_meta.are_cols_sorted, NULLPTR);
// The data of these two batches are the same, their comparison results
// should be the same regardless of whether are_cols_sorted is true or false.
ASSERT_EQ(num_rows_no_match, 0);
if (batch_idx == 0) {
ASSERT_EQ(row_encoder.row_metadata().are_cols_sorted, true);
} else if (batch_idx == 1) {
ASSERT_EQ(row_encoder.row_metadata().are_cols_sorted, false);
}
batch_idx++;
}
}

} // namespace compute
} // namespace arrow
2 changes: 1 addition & 1 deletion cpp/src/arrow/compute/row/grouper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ struct GrouperFastImpl : public Grouper {
num_keys_to_compare, selection_may_be_null, group_ids,
&impl_ptr->encode_ctx_, out_num_keys_mismatch, out_selection_mismatch,
impl_ptr->encoder_.batch_all_cols(), impl_ptr->rows_,
/* are_cols_in_encoding_order=*/true);
impl_ptr->rows_.metadata().are_cols_sorted);
};
impl->map_append_impl_ = [impl_ptr](int num_keys, const uint16_t* selection, void*) {
RETURN_NOT_OK(impl_ptr->encoder_.EncodeSelected(&impl_ptr->rows_minibatch_,
Expand Down
7 changes: 7 additions & 0 deletions cpp/src/arrow/compute/row/row_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,16 @@ void RowTableMetadata::FromColumnMetadataVector(
}
return left < right;
});
are_cols_sorted = false;
inverse_column_order.resize(num_cols);
for (uint32_t i = 0; i < num_cols; ++i) {
inverse_column_order[column_order[i]] = i;
// Check whether the column_order has changed due to sorting,
// and the sorted column order will be used first for better
// performance in grouper's compare.
if (inverse_column_order[i] != column_order[i] && are_cols_sorted == false) {
are_cols_sorted = true;
}
}

row_alignment = in_row_alignment;
Expand Down
5 changes: 5 additions & 0 deletions cpp/src/arrow/compute/row/row_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ struct ARROW_EXPORT RowTableMetadata {
/// Offsets within a row to fields in their encoding order.
std::vector<uint32_t> column_offsets;

/// \brief True if columns are sorted based on the size in bytes of
/// their fixed-length part. This is to make the memory access of
/// each individual column within the encoded row alignment-friendly
bool are_cols_sorted;

/// Rounding up offset to the nearest multiple of alignment value.
/// Alignment must be a power of 2.
static inline uint32_t padding_for_alignment(uint32_t offset, int required_alignment) {
Expand Down
6 changes: 4 additions & 2 deletions cpp/src/arrow/compute/util_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ namespace arrow {
namespace util {

template <typename T>
void CheckAlignment(const void* ptr) {
ARROW_DCHECK(reinterpret_cast<uint64_t>(ptr) % sizeof(T) == 0);
void CheckAlignment(const void* ptr, bool do_check = true) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the columns of the RowTable are not sorted during comparing(that is are_cols_sorted=false), then there is no need to check memory alignment.

if (ARROW_PREDICT_TRUE(do_check)) {
ARROW_DCHECK(reinterpret_cast<uint64_t>(ptr) % sizeof(T) == 0);
}
}

/// Storage used to allocate temporary vectors of a batch size.
Expand Down