Skip to content

Commit

Permalink
[ut](datatype) Add BE UT for some basic data types and serializers
Browse files Browse the repository at this point in the history
DataTypeNumber, DataTypeDecimal, DataTypeString, DataTypeDateTimeV2
  • Loading branch information
jacktengg committed Mar 7, 2025
1 parent c361f61 commit 5ec7ffc
Show file tree
Hide file tree
Showing 28 changed files with 6,383 additions and 74 deletions.
4 changes: 2 additions & 2 deletions be/src/vec/core/decimal_comparison.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ class DecimalComparison {
using Type = std::conditional_t<sizeof(T) >= sizeof(U), T, U>;
auto type_ptr = decimal_result_type(*decimal0, *decimal1, false, false, false);
const DataTypeDecimal<Type>* result_type = check_decimal<Type>(*type_ptr);
shift.a = result_type->scale_factor_for(*decimal0, false).value;
shift.b = result_type->scale_factor_for(*decimal1, false).value;
shift.a = result_type->scale_factor_for(*decimal0).value;
shift.b = result_type->scale_factor_for(*decimal1).value;
} else if (decimal0) {
shift.b = decimal0->get_scale_multiplier().value;
} else if (decimal1) {
Expand Down
14 changes: 3 additions & 11 deletions be/src/vec/data_types/data_type_decimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class DataTypeDecimal final : public IDataType {
std::string to_string(const T& value) const;
Status from_string(ReadBuffer& rb, IColumn* column) const override;
DataTypeSerDeSPtr get_serde(int nesting_level = 1) const override {
return std::make_shared<DataTypeDecimalSerDe<T>>(scale, precision, nesting_level);
return std::make_shared<DataTypeDecimalSerDe<T>>(precision, scale, nesting_level);
};

/// Decimal specific
Expand All @@ -259,7 +259,7 @@ class DataTypeDecimal final : public IDataType {

/// @returns multiplier for U to become T with correct scale
template <typename U>
T scale_factor_for(const DataTypeDecimal<U>& x, bool) const {
T scale_factor_for(const DataTypeDecimal<U>& x) const {
if (get_scale() < x.get_scale()) {
throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
"Decimal result's scale is less then argument's one");
Expand All @@ -269,14 +269,6 @@ class DataTypeDecimal final : public IDataType {
return get_scale_multiplier(scale_delta);
}

template <typename U>
T scale_factor_for(const DataTypeNumber<U>&, bool is_multiply_or_divisor) const {
if (is_multiply_or_divisor) {
return 1;
}
return get_scale_multiplier();
}

static T get_scale_multiplier(UInt32 scale);

static T get_max_digits_number(UInt32 digit_count);
Expand Down Expand Up @@ -318,7 +310,7 @@ DataTypePtr decimal_result_type(const DataTypeDecimal<T>& tx, const DataTypeDeci
bool is_multiply, bool is_divide, bool is_plus_minus) {
using Type = std::conditional_t<sizeof(T) >= sizeof(U), T, U>;
if constexpr (IsDecimalV2<T> && IsDecimalV2<U>) {
return std::make_shared<DataTypeDecimal<Type>>((max_decimal_precision<T>(), 9));
return std::make_shared<DataTypeDecimal<Type>>(max_decimal_precision<T>(), 9);
} else {
UInt32 scale = std::max(tx.get_scale(), ty.get_scale());
auto precision = max_decimal_precision<Type>();
Expand Down
9 changes: 5 additions & 4 deletions be/src/vec/data_types/serde/data_type_decimal_serde.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class Arena;
template <typename T>
class DataTypeDecimalSerDe : public DataTypeSerDe {
static_assert(IsDecimalNumber<T>);
using ColumnType = ColumnDecimal<T>;
using FieldType = T;

public:
static constexpr PrimitiveType get_primitive_type() {
Expand All @@ -68,13 +70,12 @@ class DataTypeDecimalSerDe : public DataTypeSerDe {
}
throw doris::Exception(ErrorCode::INTERNAL_ERROR,
"get_primitive_type __builtin_unreachable");
__builtin_unreachable();
}

DataTypeDecimalSerDe(int scale_, int precision_, int nesting_level = 1)
DataTypeDecimalSerDe(int precision_, int scale_, int nesting_level = 1)
: DataTypeSerDe(nesting_level),
scale(scale_),
precision(precision_),
scale(scale_),
scale_multiplier(decimal_scale_multiplier<typename T::NativeType>(scale)) {}

Status serialize_one_cell_to_json(const IColumn& column, int64_t row_num, BufferWritable& bw,
Expand Down Expand Up @@ -128,8 +129,8 @@ class DataTypeDecimalSerDe : public DataTypeSerDe {
int64_t row_idx, bool col_const,
const FormatOptions& options) const;

int scale;
int precision;
int scale;
const typename T::NativeType scale_multiplier;
mutable char buf[T::max_string_length()];
};
Expand Down
2 changes: 2 additions & 0 deletions be/src/vec/data_types/serde/data_type_string_serde.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ inline void escape_string(const char* src, size_t& len, char escape_char) {

template <typename ColumnType>
class DataTypeStringSerDeBase : public DataTypeSerDe {
using ColumnStrType = ColumnType;

public:
DataTypeStringSerDeBase(int nesting_level = 1) : DataTypeSerDe(nesting_level) {};

Expand Down
8 changes: 4 additions & 4 deletions be/src/vec/sink/vmysql_result_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,12 @@ Status VMysqlResultWriter<is_binary_format>::_write_one_block(RuntimeState* stat
if (_output_vexpr_ctxs[col_idx]->root()->type().is_decimal_v2_type()) {
if (_output_vexpr_ctxs[col_idx]->root()->is_nullable()) {
auto nested_serde =
std::make_shared<DataTypeDecimalSerDe<vectorized::Decimal128V2>>(scale,
27);
std::make_shared<DataTypeDecimalSerDe<vectorized::Decimal128V2>>(27,
scale);
serde = std::make_shared<DataTypeNullableSerDe>(nested_serde);
} else {
serde = std::make_shared<DataTypeDecimalSerDe<vectorized::Decimal128V2>>(scale,
27);
serde = std::make_shared<DataTypeDecimalSerDe<vectorized::Decimal128V2>>(27,
scale);
}
} else {
serde = block.get_by_position(col_idx).type->get_serde();
Expand Down
132 changes: 132 additions & 0 deletions be/test/expected_result/vec/data_types/decimalv3_18_0_get_field.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
0
0
0
0
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
0
1
5
9
1000000000
5000000000
9000000000
1000000001
5000000005
9000000009
100000000000000000
100000000000000001
100000000000000005
100000000000000009
100000000000010000
100000000000050000
100000000000090000
100000000000010001
100000000000050005
100000000000090009
500000000000000000
500000000000000001
500000000000000005
500000000000000009
500000000000010000
500000000000050000
500000000000090000
500000000000010001
500000000000050005
500000000000090009
900000000000000000
900000000000000001
900000000000000005
900000000000000009
900000000000010000
900000000000050000
900000000000090000
900000000000010001
900000000000050005
900000000000090009
444444444444444444
499999999999999999
555555555555555555
999999999999999999
0
0
0
0
-1
-2
-3
-4
-5
-6
-7
-8
-9
-1
-2
-3
-4
-5
-6
-7
-8
-9
0
-1
-5
-9
-1000000000
-5000000000
-9000000000
-1000000001
-5000000005
-9000000009
-100000000000000000
-100000000000000001
-100000000000000005
-100000000000000009
-100000000000010000
-100000000000050000
-100000000000090000
-100000000000010001
-100000000000050005
-100000000000090009
-500000000000000000
-500000000000000001
-500000000000000005
-500000000000000009
-500000000000010000
-500000000000050000
-500000000000090000
-500000000000010001
-500000000000050005
-500000000000090009
-900000000000000000
-900000000000000001
-900000000000000005
-900000000000000009
-900000000000010000
-900000000000050000
-900000000000090000
-900000000000010001
-900000000000050005
-900000000000090009
-444444444444444444
-499999999999999999
-555555555555555555
-999999999999999999
Loading

0 comments on commit 5ec7ffc

Please sign in to comment.