@@ -42,24 +42,102 @@ Result<std::unique_ptr<RowCompactedSerializer>> RowCompactedSerializer::Create(
4242 schema, std::move (getters), std::move (writers), std::move (readers), pool));
4343}
4444
45+ Result<int32_t > RowCompactedSerializer::CompareField (const FieldInfo& field_info,
46+ RowReader* reader1, RowReader* reader2) {
47+ auto type = field_info.type_id ;
48+ switch (type) {
49+ case arrow::Type::type::BOOL : {
50+ auto val1 = reader1->ReadValue <bool >();
51+ auto val2 = reader2->ReadValue <bool >();
52+ return val1 == val2 ? 0 : (val1 < val2 ? -1 : 1 );
53+ }
54+ case arrow::Type::type::INT8 : {
55+ auto val1 = reader1->ReadValue <char >();
56+ auto val2 = reader2->ReadValue <char >();
57+ return val1 == val2 ? 0 : (val1 < val2 ? -1 : 1 );
58+ }
59+ case arrow::Type::type::INT16 : {
60+ auto val1 = reader1->ReadValue <int16_t >();
61+ auto val2 = reader2->ReadValue <int16_t >();
62+ return val1 == val2 ? 0 : (val1 < val2 ? -1 : 1 );
63+ }
64+ case arrow::Type::type::INT32 :
65+ case arrow::Type::type::DATE32 : {
66+ auto val1 = reader1->ReadValue <int32_t >();
67+ auto val2 = reader2->ReadValue <int32_t >();
68+ return val1 == val2 ? 0 : (val1 < val2 ? -1 : 1 );
69+ }
70+ case arrow::Type::type::INT64 : {
71+ auto val1 = reader1->ReadValue <int64_t >();
72+ auto val2 = reader2->ReadValue <int64_t >();
73+ return val1 == val2 ? 0 : (val1 < val2 ? -1 : 1 );
74+ }
75+ case arrow::Type::type::FLOAT : {
76+ auto val1 = reader1->ReadValue <float >();
77+ auto val2 = reader2->ReadValue <float >();
78+ return FieldsComparator::CompareFloatingPoint (val1, val2);
79+ }
80+ case arrow::Type::type::DOUBLE : {
81+ auto val1 = reader1->ReadValue <double >();
82+ auto val2 = reader2->ReadValue <double >();
83+ return FieldsComparator::CompareFloatingPoint (val1, val2);
84+ }
85+ case arrow::Type::type::STRING :
86+ case arrow::Type::type::BINARY : {
87+ PAIMON_ASSIGN_OR_RAISE (std::string_view val1, reader1->ReadStringView ());
88+ PAIMON_ASSIGN_OR_RAISE (std::string_view val2, reader2->ReadStringView ());
89+ int32_t cmp = val1.compare (val2);
90+ return cmp == 0 ? 0 : (cmp > 0 ? 1 : -1 );
91+ }
92+ case arrow::Type::type::TIMESTAMP : {
93+ PAIMON_ASSIGN_OR_RAISE (Timestamp val1, reader1->ReadTimestamp (field_info.precision ));
94+ PAIMON_ASSIGN_OR_RAISE (Timestamp val2, reader2->ReadTimestamp (field_info.precision ));
95+ return val1 == val2 ? 0 : (val1 < val2 ? -1 : 1 );
96+ }
97+ case arrow::Type::type::DECIMAL : {
98+ PAIMON_ASSIGN_OR_RAISE (Decimal val1,
99+ reader1->ReadDecimal (field_info.precision , field_info.scale ));
100+ PAIMON_ASSIGN_OR_RAISE (Decimal val2,
101+ reader2->ReadDecimal (field_info.precision , field_info.scale ));
102+ int32_t cmp = val1.CompareTo (val2);
103+ return cmp == 0 ? 0 : (cmp > 0 ? 1 : -1 );
104+ }
105+ default :
106+ return Status::NotImplemented (
107+ fmt::format (" Do not support comparing type {} in CompareField" ,
108+ static_cast <int32_t >(field_info.type_id )));
109+ }
110+ }
111+
45112Result<MemorySlice::SliceComparator> RowCompactedSerializer::CreateSliceComparator (
46113 const std::shared_ptr<arrow::Schema>& schema, const std::shared_ptr<MemoryPool>& pool) {
47114 int32_t bit_set_in_bytes = RowCompactedSerializer::CalculateBitSetInBytes (schema->num_fields ());
48115 auto row_reader1 = std::make_shared<RowReader>(bit_set_in_bytes, pool);
49116 auto row_reader2 = std::make_shared<RowReader>(bit_set_in_bytes, pool);
50- std::vector<RowCompactedSerializer::FieldReader> readers (schema-> num_fields ());
51- std::vector<FieldsComparator::VariantComparatorFunc> comparators (schema->num_fields ());
117+
118+ std::vector<FieldInfo> field_infos (schema->num_fields ());
52119 for (int32_t i = 0 ; i < schema->num_fields (); i++) {
53120 auto field_type = schema->field (i)->type ();
54- PAIMON_ASSIGN_OR_RAISE (readers[i], CreateFieldReader (field_type, pool));
55- PAIMON_ASSIGN_OR_RAISE (comparators[i], FieldsComparator::CompareVariant (i, field_type));
121+ field_infos[i].type_id = field_type->id ();
122+ if (field_type->id () == arrow::Type::type::TIMESTAMP ) {
123+ auto timestamp_type =
124+ arrow::internal::checked_pointer_cast<arrow::TimestampType>(field_type);
125+ assert (timestamp_type);
126+ field_infos[i].precision = DateTimeUtils::GetPrecisionFromType (timestamp_type);
127+ } else if (field_type->id () == arrow::Type::type::DECIMAL ) {
128+ auto decimal_type =
129+ arrow::internal::checked_pointer_cast<arrow::Decimal128Type>(field_type);
130+ assert (decimal_type);
131+ field_infos[i].precision = decimal_type->precision ();
132+ field_infos[i].scale = decimal_type->scale ();
133+ }
56134 }
57- auto comparator = [row_reader1, row_reader2, readers, comparators](
58- const std::shared_ptr<MemorySlice>& slice1,
59- const std::shared_ptr< MemorySlice> & slice2) -> Result<int32_t > {
60- row_reader1->PointTo (slice1-> GetSegment (), slice1-> Offset ());
61- row_reader2->PointTo (slice2-> GetSegment (), slice2-> Offset ());
62- for (int32_t i = 0 ; i < static_cast <int32_t >(readers .size ()); i++) {
135+
136+ auto comparator = [row_reader1, row_reader2, field_infos](
137+ const MemorySlice& slice1, const MemorySlice & slice2) -> Result<int32_t > {
138+ row_reader1->PointTo (slice1. GetSegment (), slice1. Offset ());
139+ row_reader2->PointTo (slice2. GetSegment (), slice2. Offset ());
140+ for (int32_t i = 0 ; i < static_cast <int32_t >(field_infos .size ()); i++) {
63141 bool is_null1 = row_reader1->IsNullAt (i);
64142 bool is_null2 = row_reader2->IsNullAt (i);
65143 if (!is_null1 || !is_null2) {
@@ -68,9 +146,9 @@ Result<MemorySlice::SliceComparator> RowCompactedSerializer::CreateSliceComparat
68146 } else if (is_null2) {
69147 return 1 ;
70148 } else {
71- PAIMON_ASSIGN_OR_RAISE (VariantType field1, readers[i](i, row_reader1. get ()));
72- PAIMON_ASSIGN_OR_RAISE (VariantType field2, readers[i](i, row_reader2. get ()));
73- int32_t comp = comparators [i](field1, field2 );
149+ PAIMON_ASSIGN_OR_RAISE (
150+ int32_t comp,
151+ CompareField (field_infos [i], row_reader1. get (), row_reader2. get ()) );
74152 if (comp != 0 ) {
75153 return comp;
76154 }
@@ -79,8 +157,7 @@ Result<MemorySlice::SliceComparator> RowCompactedSerializer::CreateSliceComparat
79157 }
80158 return 0 ;
81159 };
82- return std::function<Result<int32_t >(const std::shared_ptr<MemorySlice>&,
83- const std::shared_ptr<MemorySlice>&)>(comparator);
160+ return std::function<Result<int32_t >(const MemorySlice&, const MemorySlice&)>(comparator);
84161}
85162
86163Result<std::shared_ptr<Bytes>> RowCompactedSerializer::SerializeToBytes (const InternalRow& row) {
@@ -110,6 +187,7 @@ Result<std::unique_ptr<InternalRow>> RowCompactedSerializer::Deserialize(
110187 PAIMON_ASSIGN_OR_RAISE (VariantType field, readers_[i](i, row_reader_.get ()));
111188 row->SetField (i, field);
112189 }
190+ row->AddDataHolder (bytes);
113191 return row;
114192}
115193
@@ -175,7 +253,7 @@ Result<RowCompactedSerializer::FieldReader> RowCompactedSerializer::CreateFieldR
175253 }
176254 case arrow::Type::type::STRING : {
177255 field_reader = [](int32_t pos, RowReader* reader) -> Result<VariantType> {
178- PAIMON_ASSIGN_OR_RAISE (VariantType value, reader->ReadString ());
256+ PAIMON_ASSIGN_OR_RAISE (VariantType value, reader->ReadStringView ());
179257 return value;
180258 };
181259 break ;
@@ -497,9 +575,9 @@ Result<const RowKind*> RowCompactedSerializer::RowReader::ReadRowKind() const {
497575 return RowKind::FromByteValue (static_cast <int8_t >(b));
498576}
499577
500- Result<BinaryString > RowCompactedSerializer::RowReader::ReadString () {
578+ Result<std::string_view > RowCompactedSerializer::RowReader::ReadStringView () {
501579 PAIMON_ASSIGN_OR_RAISE (int32_t length, ReadUnsignedInt ());
502- BinaryString str = BinaryString::FromAddress (segment_, position_, length);
580+ std::string_view str (segment_. GetArray ()-> data () + position_, length);
503581 position_ += length;
504582 return str;
505583}
0 commit comments