Skip to content

Commit bb76102

Browse files
authored
Merge branch 'main' into spill-pr1
2 parents 7205637 + 7bae4c5 commit bb76102

30 files changed

Lines changed: 479 additions & 396 deletions

src/paimon/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ set(PAIMON_COMMON_SRCS
7676
common/memory/memory_segment.cpp
7777
common/memory/memory_segment_utils.cpp
7878
common/memory/memory_slice.cpp
79-
common/memory/memory_slice_input.cpp
8079
common/memory/memory_slice_output.cpp
8180
common/metrics/metrics_impl.cpp
8281
common/metrics/histogram.cpp

src/paimon/common/data/abstract_binary_writer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ void AbstractBinaryWriter::Grow(int32_t min_capacity) {
182182
if (new_capacity - min_capacity < 0) {
183183
new_capacity = min_capacity;
184184
}
185-
std::shared_ptr<Bytes> new_bytes = Bytes::CopyOf(*(segment_.GetArray()), new_capacity, pool_);
185+
std::shared_ptr<Bytes> new_bytes =
186+
Bytes::CopyOf(*(segment_.GetOrCreateHeapMemory(pool_)), new_capacity, pool_);
186187
segment_ = MemorySegment::Wrap(new_bytes);
187188
AfterGrow();
188189
}

src/paimon/common/data/binary_string.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,6 @@ bool BinaryString::MatchAtOneSeg(const BinaryString& s, int32_t pos) const {
279279
}
280280

281281
std::string_view BinaryString::GetStringView() const {
282-
const auto* bytes = segment_.GetArray();
283-
assert(bytes);
284-
return std::string_view(bytes->data() + offset_, size_in_bytes_);
282+
return std::string_view(segment_.Data() + offset_, size_in_bytes_);
285283
}
286284
} // namespace paimon

src/paimon/common/data/serializer/row_compacted_serializer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ std::shared_ptr<Bytes> RowCompactedSerializer::RowWriter::CopyBuffer() const {
524524
Status RowCompactedSerializer::RowWriter::WriteUnsignedInt(int32_t value) {
525525
EnsureCapacity(VarLengthIntUtils::kMaxVarIntSize);
526526
PAIMON_ASSIGN_OR_RAISE(int32_t len,
527-
VarLengthIntUtils::EncodeInt(position_, value, buffer_.get()));
527+
VarLengthIntUtils::EncodeInt(value, buffer_->data() + position_));
528528
position_ += len;
529529
return Status::OK();
530530
}
@@ -577,7 +577,7 @@ Result<const RowKind*> RowCompactedSerializer::RowReader::ReadRowKind() const {
577577

578578
Result<std::string_view> RowCompactedSerializer::RowReader::ReadStringView() {
579579
PAIMON_ASSIGN_OR_RAISE(int32_t length, ReadUnsignedInt());
580-
std::string_view str(segment_.GetArray()->data() + position_, length);
580+
std::string_view str(segment_.Data() + position_, length);
581581
position_ += length;
582582
return str;
583583
}

src/paimon/common/data/serializer/row_compacted_serializer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ class RowCompactedSerializer {
157157

158158
private:
159159
Result<int32_t> ReadUnsignedInt() {
160-
const auto* bytes = segment_.GetArray();
161-
return VarLengthIntUtils::DecodeInt(bytes, &position_);
160+
return VarLengthIntUtils::DecodeInt(segment_.Data(), &position_);
162161
}
163162

164163
private:

src/paimon/common/global_index/btree/btree_global_index_reader.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ BTreeGlobalIndexReader::BTreeGlobalIndexReader(
3333
null_bitmap_(std::move(null_bitmap)),
3434
min_key_(min_key),
3535
max_key_(max_key),
36-
key_type_(key_type) {}
36+
key_type_(key_type),
37+
comparator_(KeySerializer::CreateComparator(key_type, pool)) {}
3738

3839
Result<std::shared_ptr<GlobalIndexResult>> BTreeGlobalIndexReader::VisitIsNotNull() {
3940
return std::make_shared<BitmapGlobalIndexResult>(
@@ -260,9 +261,13 @@ Result<RoaringBitmap64> BTreeGlobalIndexReader::RangeQuery(const std::optional<L
260261
// Create an index block iterator to iterate through data blocks
261262
PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<Bytes> from_bytes,
262263
KeySerializer::SerializeKey(from.value(), key_type_, pool_.get()));
264+
PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<Bytes> to_bytes,
265+
KeySerializer::SerializeKey(to.value(), key_type_, pool_.get()));
266+
MemorySlice from_slice = MemorySlice::Wrap(from_bytes);
267+
MemorySlice to_slice = MemorySlice::Wrap(to_bytes);
268+
263269
auto index_iterator = sst_file_reader_->CreateIndexIterator();
264-
PAIMON_ASSIGN_OR_RAISE([[maybe_unused]] bool seek_result,
265-
index_iterator->SeekTo(MemorySlice::Wrap(from_bytes)));
270+
PAIMON_ASSIGN_OR_RAISE([[maybe_unused]] bool seek_result, index_iterator->SeekTo(from_slice));
266271

267272
bool first_block = true;
268273
while (index_iterator->HasNext()) {
@@ -276,33 +281,33 @@ Result<RoaringBitmap64> BTreeGlobalIndexReader::RangeQuery(const std::optional<L
276281

277282
// For the first block, we need to seek within the block to the exact position
278283
if (first_block) {
279-
PAIMON_ASSIGN_OR_RAISE([[maybe_unused]] bool found,
280-
data_iterator->SeekTo(MemorySlice::Wrap(from_bytes)));
284+
PAIMON_ASSIGN_OR_RAISE([[maybe_unused]] bool found, data_iterator->SeekTo(from_slice));
281285
first_block = false;
282286
}
283287

284288
// Iterate through entries in the data block
285289
while (data_iterator->HasNext()) {
286-
PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<BlockEntry> entry, data_iterator->Next());
287-
PAIMON_ASSIGN_OR_RAISE(
288-
Literal key, KeySerializer::DeserializeKey(entry->key, key_type_, pool_.get()));
289-
PAIMON_ASSIGN_OR_RAISE(int32_t cmp_from, key.CompareTo(from.value()));
290+
PAIMON_ASSIGN_OR_RAISE(BlockEntry entry, data_iterator->Next());
291+
292+
// Compare entry key against from bound
293+
PAIMON_ASSIGN_OR_RAISE(int32_t cmp_from, comparator_(entry.key, from_slice));
290294

291295
// Check lower bound
292296
if (!from_inclusive && cmp_from == 0) {
293297
continue;
294298
}
295299

296-
// Check upper bound
297-
PAIMON_ASSIGN_OR_RAISE(int32_t cmp_to, key.CompareTo(to.value()));
300+
// Compare entry key against to bound
301+
PAIMON_ASSIGN_OR_RAISE(int32_t cmp_to, comparator_(entry.key, to_slice));
298302

299303
if (cmp_to > 0 || (!to_inclusive && cmp_to == 0)) {
300304
return result;
301305
}
302306

303-
PAIMON_RETURN_NOT_OK(DeserializeRowIds(entry->value, &result));
307+
PAIMON_RETURN_NOT_OK(DeserializeRowIds(entry.value, &result));
304308
}
305309
}
310+
306311
return result;
307312
}
308313

src/paimon/common/global_index/btree/btree_global_index_reader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "arrow/api.h"
2626
#include "paimon/common/global_index/btree/btree_defs.h"
27+
#include "paimon/common/global_index/btree/key_serializer.h"
2728
#include "paimon/common/sst/sst_file_reader.h"
2829
#include "paimon/global_index/global_index_io_meta.h"
2930
#include "paimon/global_index/global_index_reader.h"
@@ -101,6 +102,7 @@ class BTreeGlobalIndexReader : public GlobalIndexReader,
101102
std::optional<Literal> min_key_;
102103
std::optional<Literal> max_key_;
103104
std::shared_ptr<arrow::DataType> key_type_;
105+
MemorySlice::SliceComparator comparator_;
104106
};
105107

106108
} // namespace paimon

src/paimon/common/global_index/btree/btree_index_meta.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ std::shared_ptr<BTreeIndexMeta> BTreeIndexMeta::Deserialize(const std::shared_pt
2727
auto first_key_len = input.ReadInt();
2828
std::shared_ptr<Bytes> first_key;
2929
if (first_key_len) {
30-
first_key = input.ReadSlice(first_key_len).CopyBytes(pool);
30+
first_key = input.ReadSliceView(first_key_len).CopyBytes(pool);
3131
}
3232
auto last_key_len = input.ReadInt();
3333
std::shared_ptr<Bytes> last_key;
3434
if (last_key_len) {
35-
last_key = input.ReadSlice(last_key_len).CopyBytes(pool);
35+
last_key = input.ReadSliceView(last_key_len).CopyBytes(pool);
3636
}
3737
auto has_nulls = input.ReadByte() == static_cast<int8_t>(1);
3838
return std::make_shared<BTreeIndexMeta>(first_key, last_key, has_nulls);
@@ -62,7 +62,7 @@ std::shared_ptr<Bytes> BTreeIndexMeta::Serialize(paimon::MemoryPool* pool) const
6262
// Write has_nulls
6363
output.WriteValue(static_cast<int8_t>(has_nulls_ ? 1 : 0));
6464

65-
return output.ToSlice().GetHeapMemory();
65+
return output.ToSlice().GetOrCreateHeapMemory(pool);
6666
}
6767

6868
} // namespace paimon

src/paimon/common/global_index/btree/key_serializer.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "paimon/common/memory/memory_slice_output.h"
2222
#include "paimon/common/utils/date_time_utils.h"
2323
#include "paimon/common/utils/field_type_utils.h"
24+
#include "paimon/common/utils/fields_comparator.h"
2425
#include "paimon/common/utils/preconditions.h"
2526
#include "paimon/data/decimal.h"
2627
#include "paimon/data/timestamp.h"
@@ -195,6 +196,66 @@ Result<Literal> KeySerializer::DeserializeKey(const MemorySlice& slice,
195196

196197
MemorySlice::SliceComparator KeySerializer::CreateComparator(
197198
const std::shared_ptr<arrow::DataType>& type, const std::shared_ptr<MemoryPool>& pool) {
199+
// Fast paths for integer and string types: direct value comparison without Literal
200+
// deserialization, avoiding heap allocations entirely.
201+
switch (type->id()) {
202+
case arrow::Type::type::STRING:
203+
return [](const MemorySlice& a, const MemorySlice& b) -> Result<int32_t> {
204+
std::string_view sv_a = a.ReadStringView();
205+
std::string_view sv_b = b.ReadStringView();
206+
int32_t cmp = sv_a.compare(sv_b);
207+
return cmp == 0 ? 0 : (cmp > 0 ? 1 : -1);
208+
};
209+
case arrow::Type::type::BOOL:
210+
case arrow::Type::type::INT8:
211+
return [](const MemorySlice& a, const MemorySlice& b) -> Result<int32_t> {
212+
int8_t va = a.ReadByte(0);
213+
int8_t vb = b.ReadByte(0);
214+
return (va < vb) ? -1 : (va > vb ? 1 : 0);
215+
};
216+
case arrow::Type::type::INT16:
217+
return [](const MemorySlice& a, const MemorySlice& b) -> Result<int32_t> {
218+
int16_t va = a.ReadShort(0);
219+
int16_t vb = b.ReadShort(0);
220+
return (va < vb) ? -1 : (va > vb ? 1 : 0);
221+
};
222+
case arrow::Type::type::INT32:
223+
case arrow::Type::type::DATE32:
224+
return [](const MemorySlice& a, const MemorySlice& b) -> Result<int32_t> {
225+
int32_t va = a.ReadInt(0);
226+
int32_t vb = b.ReadInt(0);
227+
return (va < vb) ? -1 : (va > vb ? 1 : 0);
228+
};
229+
case arrow::Type::type::INT64:
230+
return [](const MemorySlice& a, const MemorySlice& b) -> Result<int32_t> {
231+
int64_t va = a.ReadLong(0);
232+
int64_t vb = b.ReadLong(0);
233+
return (va < vb) ? -1 : (va > vb ? 1 : 0);
234+
};
235+
case arrow::Type::type::FLOAT:
236+
return [](const MemorySlice& a, const MemorySlice& b) -> Result<int32_t> {
237+
int32_t ia = a.ReadInt(0);
238+
int32_t ib = b.ReadInt(0);
239+
float fa, fb;
240+
memcpy(&fa, &ia, sizeof(float));
241+
memcpy(&fb, &ib, sizeof(float));
242+
return FieldsComparator::CompareFloatingPoint(fa, fb);
243+
};
244+
case arrow::Type::type::DOUBLE:
245+
return [](const MemorySlice& a, const MemorySlice& b) -> Result<int32_t> {
246+
int64_t ia = a.ReadLong(0);
247+
int64_t ib = b.ReadLong(0);
248+
double da, db;
249+
memcpy(&da, &ia, sizeof(double));
250+
memcpy(&db, &ib, sizeof(double));
251+
return FieldsComparator::CompareFloatingPoint(da, db);
252+
};
253+
default:
254+
break;
255+
}
256+
257+
// Fallback for complex types (TIMESTAMP, DECIMAL, etc.):
258+
// deserialize to Literal and compare.
198259
return
199260
[pool = pool, type = type](const MemorySlice& a, const MemorySlice& b) -> Result<int32_t> {
200261
PAIMON_ASSIGN_OR_RAISE(Literal la, DeserializeKey(a, type, pool.get()));

src/paimon/common/global_index/btree/lazy_filtered_btree_reader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ Result<std::shared_ptr<GlobalIndexReader>> LazyFilteredBTreeReader::GetOrCreateR
234234

235235
Result<std::shared_ptr<GlobalIndexReader>> LazyFilteredBTreeReader::CreateSingleReader(
236236
const GlobalIndexIOMeta& meta) {
237+
// Create comparator based on field type
237238
auto comparator = KeySerializer::CreateComparator(key_type_, pool_);
238239

239240
// Deserialize min/max keys from meta
@@ -298,7 +299,7 @@ Result<RoaringBitmap64> LazyFilteredBTreeReader::ReadNullBitmap(
298299
auto slice_input = slice.ToInput();
299300

300301
// Read null bitmap data
301-
auto null_bitmap_bytes = slice_input.ReadSlice(block_handle->Size()).CopyBytes(pool_.get());
302+
auto null_bitmap_bytes = slice_input.ReadSliceView(block_handle->Size()).CopyBytes(pool_.get());
302303

303304
// Calculate and verify CRC32C checksum
304305
uint32_t calculated_crc =

0 commit comments

Comments
 (0)