Skip to content

Commit 5216ba7

Browse files
committed
[feature](paimon) Route Paimon writes by native bucket semantics
Issue Number: close apache#65086 Related PR: apache#66685 Problem Summary: Forward-port external sink hash partitioning, Paimon fixed-bucket routing, native row hashing, and writer assignment so distributed Doris writers match Paimon bucket ownership. Paimon writes now use native Paimon-compatible routing for fixed bucket tables. - Test: Static validation - BE clang-format/check-format and FE checkstyle; full build and tests are deferred until all forward-port picks are complete - Behavior changed: Yes, changes Paimon writer distribution - Does this need documentation: No
1 parent e2561c2 commit 5216ba7

13 files changed

Lines changed: 1179 additions & 142 deletions
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#include "exec/partitioner/external/paimon_fixed_bucket_partition_function.h"
18+
19+
#include "common/status.h"
20+
#include "exec/partitioner/external/paimon_native_row_hash.h"
21+
22+
namespace doris {
23+
#include "common/compile_check_begin.h"
24+
25+
PaimonFixedBucketPartitionFunction::PaimonFixedBucketPartitionFunction(
26+
HashValType partition_count, TPaimonFixedBucketInfo fixed_bucket_info)
27+
: PaimonRowHashPartitionFunction(partition_count),
28+
_fixed_bucket_info(std::move(fixed_bucket_info)) {}
29+
30+
Status PaimonFixedBucketPartitionFunction::init(const std::vector<TExpr>& texprs) {
31+
RETURN_IF_ERROR(PaimonRowHashPartitionFunction::init(texprs));
32+
if (_fixed_bucket_info.num_buckets <= 0) {
33+
return Status::InvalidArgument("Paimon fixed-bucket count must be positive");
34+
}
35+
RETURN_IF_ERROR(_validate_field_indexes(_fixed_bucket_info.partition_field_indexes, false));
36+
return _validate_field_indexes(_fixed_bucket_info.bucket_field_indexes, true);
37+
}
38+
39+
Status PaimonFixedBucketPartitionFunction::get_partitions(
40+
RuntimeState* /*state*/, Block* block, size_t partition_count,
41+
std::vector<HashValType>& partitions) const {
42+
if (partition_count != _partition_count) {
43+
return Status::InvalidArgument("Paimon writer count {} does not match planned count {}",
44+
partition_count, _partition_count);
45+
}
46+
const size_t rows = block->rows();
47+
if (rows == 0) {
48+
partitions.clear();
49+
return Status::OK();
50+
}
51+
52+
std::vector<ColumnWithTypeAndName> fields;
53+
RETURN_IF_ERROR(_evaluate_fields(block, fields));
54+
std::vector<int32_t> partition_hashes;
55+
std::vector<int32_t> bucket_hashes;
56+
RETURN_IF_ERROR(
57+
_hash_fields(_fixed_bucket_info.partition_field_indexes, fields, partition_hashes));
58+
RETURN_IF_ERROR(_hash_fields(_fixed_bucket_info.bucket_field_indexes, fields, bucket_hashes));
59+
partitions.resize(rows);
60+
for (size_t row = 0; row < rows; ++row) {
61+
auto bucket =
62+
paimon_native::default_bucket(bucket_hashes[row], _fixed_bucket_info.num_buckets);
63+
if (!bucket.has_value()) {
64+
return Status::InternalError("Failed to compute Paimon fixed bucket");
65+
}
66+
auto channel = paimon_native::fixed_bucket_channel(partition_hashes[row], *bucket,
67+
_partition_count);
68+
if (!channel.has_value()) {
69+
return Status::InternalError("Failed to compute Paimon fixed-bucket writer");
70+
}
71+
partitions[row] = *channel;
72+
}
73+
return Status::OK();
74+
}
75+
76+
Status PaimonFixedBucketPartitionFunction::clone(
77+
RuntimeState* state, std::unique_ptr<PartitionFunction>& function) const {
78+
auto cloned = std::make_unique<PaimonFixedBucketPartitionFunction>(_partition_count,
79+
_fixed_bucket_info);
80+
RETURN_IF_ERROR(_clone_expr_ctxs(state, cloned->_field_expr_ctxs));
81+
function = std::move(cloned);
82+
return Status::OK();
83+
}
84+
85+
#include "common/compile_check_end.h"
86+
} // namespace doris
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#pragma once
19+
20+
#include <gen_cpp/Partitions_types.h>
21+
22+
#include "exec/partitioner/external/paimon_row_hash_partition_function.h"
23+
24+
namespace doris {
25+
#include "common/compile_check_begin.h"
26+
27+
// Stateless native implementation of Paimon FixedBucketWriteSelector for the
28+
// explicitly supported primitive routing types.
29+
class PaimonFixedBucketPartitionFunction final : public PaimonRowHashPartitionFunction {
30+
public:
31+
PaimonFixedBucketPartitionFunction(HashValType partition_count,
32+
TPaimonFixedBucketInfo fixed_bucket_info);
33+
34+
Status init(const std::vector<TExpr>& texprs) override;
35+
Status get_partitions(RuntimeState* state, Block* block, size_t partition_count,
36+
std::vector<HashValType>& partitions) const override;
37+
Status clone(RuntimeState* state, std::unique_ptr<PartitionFunction>& function) const override;
38+
39+
private:
40+
TPaimonFixedBucketInfo _fixed_bucket_info;
41+
};
42+
43+
#include "common/compile_check_end.h"
44+
} // namespace doris
Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#include "exec/partitioner/external/paimon_native_row_hash.h"
19+
20+
#include <algorithm>
21+
#include <bit>
22+
#include <cstring>
23+
#include <limits>
24+
25+
namespace doris::paimon_native {
26+
namespace {
27+
28+
constexpr uint32_t MURMUR_C1 = 0xcc9e2d51U;
29+
constexpr uint32_t MURMUR_C2 = 0x1b873593U;
30+
constexpr uint32_t MURMUR_SEED = 42U;
31+
32+
uint32_t rotate_left(uint32_t value, int distance) {
33+
return (value << distance) | (value >> (32 - distance));
34+
}
35+
36+
uint32_t mix_k1(uint32_t value) {
37+
value *= MURMUR_C1;
38+
value = rotate_left(value, 15);
39+
value *= MURMUR_C2;
40+
return value;
41+
}
42+
43+
uint32_t mix_h1(uint32_t hash, uint32_t value) {
44+
hash ^= value;
45+
hash = rotate_left(hash, 13);
46+
return hash * 5U + 0xe6546b64U;
47+
}
48+
49+
uint32_t fmix(uint32_t hash, size_t length) {
50+
hash ^= static_cast<uint32_t>(length);
51+
hash ^= hash >> 16;
52+
hash *= 0x85ebca6bU;
53+
hash ^= hash >> 13;
54+
hash *= 0xc2b2ae35U;
55+
hash ^= hash >> 16;
56+
return hash;
57+
}
58+
59+
template <typename T>
60+
void put_native(std::vector<uint8_t>* bytes, size_t offset, T value) {
61+
std::memcpy(bytes->data() + offset, &value, sizeof(value));
62+
}
63+
64+
size_t round_to_word(size_t size) {
65+
return (size + 7U) & ~size_t {7U};
66+
}
67+
68+
} // namespace
69+
70+
BinaryRowEncoder::BinaryRowEncoder(size_t arity)
71+
: _arity(arity),
72+
_null_bits_size((arity + 63U + 8U) / 64U * 8U),
73+
_fixed_size(_null_bits_size + arity * 8U),
74+
_cursor(_fixed_size),
75+
_bytes(_fixed_size, 0) {}
76+
77+
void BinaryRowEncoder::reset() {
78+
_cursor = _fixed_size;
79+
_bytes.assign(_fixed_size, 0);
80+
}
81+
82+
bool BinaryRowEncoder::_valid_position(size_t position) const {
83+
return position < _arity;
84+
}
85+
86+
size_t BinaryRowEncoder::_field_offset(size_t position) const {
87+
return _null_bits_size + position * 8U;
88+
}
89+
90+
void BinaryRowEncoder::_set_null_bit(size_t position) {
91+
const size_t bit_index = position + 8U;
92+
_bytes[bit_index >> 3U] |= static_cast<uint8_t>(1U << (bit_index & 7U));
93+
}
94+
95+
void BinaryRowEncoder::_ensure_capacity(size_t size) {
96+
if (_bytes.size() < size) {
97+
_bytes.resize(size, 0);
98+
}
99+
}
100+
101+
void BinaryRowEncoder::_set_offset_and_size(size_t position, uint32_t offset, uint32_t size) {
102+
uint64_t offset_and_size = (static_cast<uint64_t>(offset) << 32U) | size;
103+
put_native(&_bytes, _field_offset(position), offset_and_size);
104+
}
105+
106+
bool BinaryRowEncoder::set_null(size_t position) {
107+
if (!_valid_position(position)) {
108+
return false;
109+
}
110+
_set_null_bit(position);
111+
std::fill_n(_bytes.begin() + _field_offset(position), 8, uint8_t {0});
112+
return true;
113+
}
114+
115+
bool BinaryRowEncoder::write_boolean(size_t position, bool value) {
116+
return write_tinyint(position, value ? 1 : 0);
117+
}
118+
119+
bool BinaryRowEncoder::write_tinyint(size_t position, int8_t value) {
120+
if (!_valid_position(position)) {
121+
return false;
122+
}
123+
put_native(&_bytes, _field_offset(position), value);
124+
return true;
125+
}
126+
127+
bool BinaryRowEncoder::write_smallint(size_t position, int16_t value) {
128+
if (!_valid_position(position)) {
129+
return false;
130+
}
131+
put_native(&_bytes, _field_offset(position), value);
132+
return true;
133+
}
134+
135+
bool BinaryRowEncoder::write_int(size_t position, int32_t value) {
136+
if (!_valid_position(position)) {
137+
return false;
138+
}
139+
put_native(&_bytes, _field_offset(position), value);
140+
return true;
141+
}
142+
143+
bool BinaryRowEncoder::write_bigint(size_t position, int64_t value) {
144+
if (!_valid_position(position)) {
145+
return false;
146+
}
147+
put_native(&_bytes, _field_offset(position), value);
148+
return true;
149+
}
150+
151+
bool BinaryRowEncoder::write_float(size_t position, float value) {
152+
if (!_valid_position(position)) {
153+
return false;
154+
}
155+
put_native(&_bytes, _field_offset(position), value);
156+
return true;
157+
}
158+
159+
bool BinaryRowEncoder::write_double(size_t position, double value) {
160+
if (!_valid_position(position)) {
161+
return false;
162+
}
163+
put_native(&_bytes, _field_offset(position), value);
164+
return true;
165+
}
166+
167+
bool BinaryRowEncoder::_write_bytes(size_t position, std::string_view bytes) {
168+
if (!_valid_position(position) || bytes.size() > std::numeric_limits<uint32_t>::max()) {
169+
return false;
170+
}
171+
if (bytes.size() <= 7U) {
172+
uint64_t inline_value = (static_cast<uint64_t>(bytes.size()) | 0x80U) << 56U;
173+
for (size_t index = 0; index < bytes.size(); ++index) {
174+
if constexpr (std::endian::native == std::endian::little) {
175+
inline_value |= static_cast<uint64_t>(static_cast<uint8_t>(bytes[index]))
176+
<< (index * 8U);
177+
} else {
178+
inline_value |= static_cast<uint64_t>(static_cast<uint8_t>(bytes[index]))
179+
<< ((6U - index) * 8U);
180+
}
181+
}
182+
put_native(&_bytes, _field_offset(position), inline_value);
183+
return true;
184+
}
185+
186+
const size_t rounded_size = round_to_word(bytes.size());
187+
if (rounded_size > std::numeric_limits<uint32_t>::max() ||
188+
_cursor > std::numeric_limits<uint32_t>::max() - rounded_size) {
189+
return false;
190+
}
191+
_ensure_capacity(_cursor + rounded_size);
192+
std::fill_n(_bytes.begin() + _cursor, rounded_size, uint8_t {0});
193+
std::memcpy(_bytes.data() + _cursor, bytes.data(), bytes.size());
194+
_set_offset_and_size(position, static_cast<uint32_t>(_cursor),
195+
static_cast<uint32_t>(bytes.size()));
196+
_cursor += rounded_size;
197+
return true;
198+
}
199+
200+
bool BinaryRowEncoder::write_string(size_t position, std::string_view utf8) {
201+
return _write_bytes(position, utf8);
202+
}
203+
204+
bool BinaryRowEncoder::write_binary(size_t position, std::string_view bytes) {
205+
return _write_bytes(position, bytes);
206+
}
207+
208+
int32_t BinaryRowEncoder::hash() const {
209+
return binary_row_hash(std::string_view(reinterpret_cast<const char*>(_bytes.data()), _cursor));
210+
}
211+
212+
int32_t binary_row_hash(std::string_view bytes) {
213+
uint32_t hash = MURMUR_SEED;
214+
size_t offset = 0;
215+
const size_t aligned_length = bytes.size() - bytes.size() % 4U;
216+
for (; offset < aligned_length; offset += 4U) {
217+
uint32_t word;
218+
std::memcpy(&word, bytes.data() + offset, sizeof(word));
219+
hash = mix_h1(hash, mix_k1(word));
220+
}
221+
for (; offset < bytes.size(); ++offset) {
222+
int32_t signed_byte = static_cast<int8_t>(bytes[offset]);
223+
hash = mix_h1(hash, mix_k1(static_cast<uint32_t>(signed_byte)));
224+
}
225+
uint32_t mixed = fmix(hash, bytes.size());
226+
int32_t result;
227+
std::memcpy(&result, &mixed, sizeof(result));
228+
return result;
229+
}
230+
231+
std::optional<uint32_t> default_bucket(int32_t bucket_key_hash, int32_t num_buckets) {
232+
if (num_buckets <= 0) {
233+
return std::nullopt;
234+
}
235+
int32_t remainder = bucket_key_hash % num_buckets;
236+
return static_cast<uint32_t>(remainder < 0 ? -remainder : remainder);
237+
}
238+
239+
std::optional<uint32_t> fixed_bucket_channel(int32_t partition_hash, uint32_t bucket,
240+
uint32_t num_channels) {
241+
if (num_channels == 0) {
242+
return std::nullopt;
243+
}
244+
if (partition_hash == std::numeric_limits<int32_t>::min()) {
245+
partition_hash = std::numeric_limits<int32_t>::max();
246+
}
247+
uint32_t start_channel =
248+
static_cast<uint32_t>(partition_hash < 0 ? -partition_hash : partition_hash) %
249+
num_channels;
250+
return static_cast<uint32_t>((static_cast<uint64_t>(start_channel) + bucket) % num_channels);
251+
}
252+
253+
} // namespace doris::paimon_native

0 commit comments

Comments
 (0)