Skip to content

Commit 91354dd

Browse files
authored
[refine](type) Ensure that the internal encoding types in Doris are trivially copyable. (#59736)
For example, types like Decimal and DateTime-related types must be guaranteed to be trivial — e.g. using is_trivial_v or is_trivially_destructible_v / is_trivially_copyable_v. That way they can be stored in PODArray, since PODArray copies elements with memcpy.
1 parent 3925593 commit 91354dd

File tree

7 files changed

+93
-5
lines changed

7 files changed

+93
-5
lines changed

be/src/vec/aggregate_functions/aggregate_function_sequence_match.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ struct AggregateFunctionSequenceMatchData final {
123123
void merge(const AggregateFunctionSequenceMatchData& other) {
124124
if (other.events_list.empty()) return;
125125

126-
events_list.insert(std::begin(other.events_list), std::end(other.events_list));
126+
events_list.insert(std::end(events_list), std::begin(other.events_list),
127+
std::end(other.events_list));
127128
sorted = false;
128129
conditions_met |= other.conditions_met;
129130
}
@@ -570,7 +571,7 @@ struct AggregateFunctionSequenceMatchData final {
570571

571572
public:
572573
bool sorted = true;
573-
PODArrayWithStackMemory<TimestampEvents, 64> events_list;
574+
std::vector<TimestampEvents> events_list;
574575
// sequenceMatch conditions met at least once in events_list
575576
std::bitset<MAX_EVENTS> conditions_met;
576577
// sequenceMatch conditions met at least once in the pattern

be/src/vec/aggregate_functions/aggregate_function_window_funnel.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ struct WindowFunnelState {
160160
template <WindowFunnelMode WINDOW_FUNNEL_MODE>
161161
int _match_event_list(size_t& start_row, size_t row_count) const {
162162
int matched_count = 0;
163-
DateValueType start_timestamp;
164163
DateValueType end_timestamp;
165164

166165
if (window < 0) {

be/src/vec/common/pod_array.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,11 @@ class PODArray : public PODArrayBase<sizeof(T), initial_bytes, TAllocator, pad_r
408408
protected:
409409
using Base = PODArrayBase<sizeof(T), initial_bytes, TAllocator, pad_right_, pad_left_>;
410410

411+
static_assert(std::is_trivially_destructible_v<T>,
412+
"PODArray can only be used with POD types or types with trivial destructor");
413+
static_assert(std::is_trivially_copyable_v<T>,
414+
"PODArray can only be used with POD types or types with trivial copy");
415+
411416
T* t_start() { return reinterpret_cast<T*>(this->c_start); }
412417
T* t_end() { return reinterpret_cast<T*>(this->c_end); }
413418

be/src/vec/core/types.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,12 @@ using Decimal64 = Decimal<Int64>;
481481
using Decimal128V2 = Decimal<Int128>;
482482
using Decimal256 = Decimal<wide::Int256>;
483483

484+
static_assert(std::is_trivial_v<Decimal32>, "Decimal32 must be trivial");
485+
static_assert(std::is_trivial_v<Decimal64>, "Decimal64 must be trivial");
486+
static_assert(std::is_trivial_v<Decimal128V2>, "Decimal128V2 must be trivial");
487+
static_assert(std::is_trivial_v<Decimal128V3>, "Decimal128V3 must be trivial");
488+
static_assert(std::is_trivial_v<Decimal256>, "Decimal256 must be trivial");
489+
484490
inline bool operator<(const Decimal256& x, const Decimal256& y) {
485491
return x.value < y.value;
486492
}

be/src/vec/runtime/timestamptz_value.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ bool TimestampTzValue::from_datetime(const DateV2Value<DateTimeV2ValueType>& ori
101101

102102
auto utc_cs = cctz::convert(local_tp, cctz::utc_time_zone());
103103

104-
DateV2Value<DateTimeV2ValueType> utc_dt;
105-
106104
return _utc_dt.check_range_and_set_time((uint16_t)utc_cs.year(), (uint8_t)utc_cs.month(),
107105
(uint8_t)utc_cs.day(), (uint8_t)utc_cs.hour(),
108106
(uint8_t)utc_cs.minute(), (uint8_t)utc_cs.second(),

be/src/vec/runtime/vdatetime_value.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,10 @@ class VecDateTimeValue { // Now this type is a temp solution with little changes
811811
_year(year) {}
812812
};
813813

814+
static_assert(std::is_trivially_destructible_v<VecDateTimeValue>,
815+
"VecDateTimeValue must be trivial destructible");
816+
static_assert(std::is_trivially_copyable_v<VecDateTimeValue>,
817+
"VecDateTimeValue must be trivial copyable");
814818
inline const VecDateTimeValue VecDateTimeValue::FIRST_DAY(false, TYPE_DATETIME, 0, 0, 0, 1, 1, 1);
815819
inline const VecDateTimeValue VecDateTimeValue::DEFAULT_VALUE(false, TYPE_DATETIME, 0, 0, 0, 1970,
816820
1, 1);
@@ -1472,6 +1476,15 @@ class DateV2Value {
14721476
: date_v2_value_(year, month, day, hour, minute, second, microsecond) {}
14731477
};
14741478

1479+
static_assert(std::is_trivially_destructible_v<DateV2Value<DateV2ValueType>>,
1480+
"DateV2Value<DateV2ValueType> must be trivial destructible");
1481+
static_assert(std::is_trivially_destructible_v<DateV2Value<DateTimeV2ValueType>>,
1482+
"DateV2Value<DateTimeV2ValueType> must be trivial destructible");
1483+
static_assert(std::is_trivially_copyable_v<DateV2Value<DateV2ValueType>>,
1484+
"DateV2Value<DateV2ValueType> must be trivial copyable");
1485+
static_assert(std::is_trivially_copyable_v<DateV2Value<DateTimeV2ValueType>>,
1486+
"DateV2Value<DateTimeV2ValueType> must be trivial copyable");
1487+
14751488
template <typename T>
14761489
inline const DateV2Value<T> DateV2Value<T>::FIRST_DAY = DateV2Value<T>(0001, 1, 1, 0, 0, 0, 0);
14771490
template <typename T>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 <gtest/gtest.h>
19+
20+
#include "runtime/decimalv2_value.h"
21+
#include "runtime/primitive_type.h"
22+
#include "testutil/column_helper.h"
23+
#include "vec/columns/column.h"
24+
#include "vec/columns/column_array.h"
25+
#include "vec/columns/column_const.h"
26+
#include "vec/data_types/data_type_number.h"
27+
#include "vec/functions/function.h"
28+
#include "vec/runtime/vdatetime_value.h"
29+
30+
namespace doris::vectorized {
31+
32+
template <typename T>
33+
void test_for_type() {
34+
PaddedPODArray<T> arr;
35+
for (int i = 0; i < 100; ++i) {
36+
arr.push_back(T {});
37+
}
38+
arr.resize(1000);
39+
for (int i = 0; i < 100; ++i) {
40+
EXPECT_EQ(T {}, arr[i]);
41+
}
42+
}
43+
44+
TEST(PodArrayTypeTest, test) {
45+
test_for_type<int8_t>();
46+
test_for_type<int16_t>();
47+
test_for_type<int32_t>();
48+
test_for_type<int64_t>();
49+
test_for_type<uint8_t>();
50+
test_for_type<uint16_t>();
51+
test_for_type<uint32_t>();
52+
test_for_type<uint64_t>();
53+
test_for_type<float>();
54+
test_for_type<double>();
55+
test_for_type<VecDateTimeValue>();
56+
test_for_type<DateV2Value<DateV2ValueType>>();
57+
test_for_type<DateV2Value<DateTimeV2ValueType>>();
58+
test_for_type<DecimalV2Value>();
59+
test_for_type<Decimal32>();
60+
test_for_type<Decimal64>();
61+
test_for_type<Decimal128V2>();
62+
test_for_type<Decimal128V3>();
63+
test_for_type<Decimal256>();
64+
}
65+
66+
} // namespace doris::vectorized

0 commit comments

Comments
 (0)