Skip to content

Commit

Permalink
[feature](decimal) support decimal256: link error
Browse files Browse the repository at this point in the history
  • Loading branch information
jacktengg committed Sep 26, 2023
1 parent 8679095 commit c3d1a1a
Show file tree
Hide file tree
Showing 23 changed files with 1,872 additions and 8 deletions.
1 change: 1 addition & 0 deletions be/src/common/consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ const std::string DYNAMIC_COLUMN_NAME = "__DORIS_DYNAMIC_COL__";
constexpr int MAX_DECIMAL32_PRECISION = 9;
constexpr int MAX_DECIMAL64_PRECISION = 18;
constexpr int MAX_DECIMAL128_PRECISION = 38;
constexpr int MAX_DECIMAL256_PRECISION = 76;
} // namespace BeConsts
} // namespace doris
6 changes: 6 additions & 0 deletions be/src/exec/schema_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ Status SchemaScanner::fill_dest_column_for_range(vectorized::Block* block, size_
reinterpret_cast<const char*>(&num), 0);
break;
}
// case TYPE_DECIMAL256: {
// const vectorized::Int256 num = (reinterpret_cast<PackedInt256*>(data))->value;
// reinterpret_cast<vectorized::ColumnDecimal256*>(col_ptr)->insert_data(
// reinterpret_cast<const char*>(&num), 0);
// break;
// }

case TYPE_DECIMAL32: {
const int32_t num = *reinterpret_cast<int32_t*>(data);
Expand Down
1 change: 1 addition & 0 deletions be/src/runtime/define_primitive_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ enum PrimitiveType : PrimitiveNative {
TYPE_VARIANT, /* 32 */
TYPE_LAMBDA_FUNCTION, /* 33 */
TYPE_AGG_STATE, /* 34 */
TYPE_DECIMAL256, /* 35 */
};

constexpr PrimitiveNative BEGIN_OF_PRIMITIVE_TYPE = INVALID_TYPE;
Expand Down
6 changes: 6 additions & 0 deletions be/src/runtime/primitive_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ constexpr bool is_enumeration_type(PrimitiveType type) {
case TYPE_DECIMAL32:
case TYPE_DECIMAL64:
case TYPE_DECIMAL128I:
case TYPE_DECIMAL256:
case TYPE_BOOLEAN:
case TYPE_ARRAY:
case TYPE_STRUCT:
Expand Down Expand Up @@ -205,6 +206,11 @@ struct PrimitiveTypeTraits<TYPE_DECIMAL128I> {
using ColumnType = vectorized::ColumnDecimal<vectorized::Decimal128I>;
};
template <>
struct PrimitiveTypeTraits<TYPE_DECIMAL256> {
using CppType = vectorized::Decimal256;
using ColumnType = vectorized::ColumnDecimal<vectorized::Decimal256>;
};
template <>
struct PrimitiveTypeTraits<TYPE_LARGEINT> {
using CppType = __int128_t;
using ColumnType = vectorized::ColumnInt128;
Expand Down
9 changes: 8 additions & 1 deletion be/src/runtime/type_limit.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "runtime/datetime_value.h"
#include "runtime/decimalv2_value.h"
#include "vec/common/string_ref.h"
#include "vec/core/wide_integer.h"

namespace doris {

Expand Down Expand Up @@ -61,7 +62,6 @@ struct type_limit<vectorized::Decimal128I> {
}
static vectorized::Decimal128I min() { return -max(); }
};

template <>
struct type_limit<vectorized::Decimal128> {
static vectorized::Decimal128 max() {
Expand All @@ -70,6 +70,13 @@ struct type_limit<vectorized::Decimal128> {
}
static vectorized::Decimal128 min() { return -max(); }
};
static Int256 MAX_DECIMAL256_INT({999999999999999999ll, 999999999999999999ll, 999999999999999999ll,
999999999999999999ll, 9999ll});
template <>
struct type_limit<vectorized::Decimal256> {
static vectorized::Decimal256 max() { return MAX_DECIMAL256_INT; }
static vectorized::Decimal256 min() { return -MAX_DECIMAL256_INT; }
};

template <>
struct type_limit<vectorized::VecDateTimeValue> {
Expand Down
4 changes: 2 additions & 2 deletions be/src/vec/columns/column_decimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ class ColumnDecimal final : public COWHelper<ColumnVectorHelper, ColumnDecimal<T
return StringRef(reinterpret_cast<const char*>(&data[n]), sizeof(data[n]));
}
void get(size_t n, Field& res) const override { res = (*this)[n]; }
bool get_bool(size_t n) const override { return bool(data[n]); }
Int64 get_int(size_t n) const override { return Int64(data[n] * scale); }
// bool get_bool(size_t n) const override { return bool(data[n]); }
// Int64 get_int(size_t n) const override { return Int64(data[n] * scale); }
UInt64 get64(size_t n) const override;
bool is_default_at(size_t n) const override { return data[n].value == 0; }

Expand Down
1 change: 1 addition & 0 deletions be/src/vec/columns/columns_number.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ using ColumnDecimal32 = ColumnDecimal<Decimal32>;
using ColumnDecimal64 = ColumnDecimal<Decimal64>;
using ColumnDecimal128 = ColumnDecimal<Decimal128>;
using ColumnDecimal128I = ColumnDecimal<Decimal128I>;
using ColumnDecimal256 = ColumnDecimal<Decimal256>;

template <typename T>
struct IsFixLenColumnType {
Expand Down
92 changes: 92 additions & 0 deletions be/src/vec/common/int_exp.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <limits>
#include <utility>

#include "vec/core/wide_integer.h"

namespace exp_details {

// compile-time exp(v, n) by linear recursion
Expand Down Expand Up @@ -78,4 +80,94 @@ inline constexpr __int128 exp10_i128(int x) {
return exp_details::get_exp<__int128, 10, 39>(x);
}

using wide::Int256;
inline Int256 exp10_i256(int x) {
if (x < 0) return 0;
if (x > 76) return std::numeric_limits<Int256>::max();

using Int256 = Int256;
static constexpr Int256 i10e18 {1000000000000000000ll};
static const Int256 values[] = {
static_cast<Int256>(1ll),
static_cast<Int256>(10ll),
static_cast<Int256>(100ll),
static_cast<Int256>(1000ll),
static_cast<Int256>(10000ll),
static_cast<Int256>(100000ll),
static_cast<Int256>(1000000ll),
static_cast<Int256>(10000000ll),
static_cast<Int256>(100000000ll),
static_cast<Int256>(1000000000ll),
static_cast<Int256>(10000000000ll),
static_cast<Int256>(100000000000ll),
static_cast<Int256>(1000000000000ll),
static_cast<Int256>(10000000000000ll),
static_cast<Int256>(100000000000000ll),
static_cast<Int256>(1000000000000000ll),
static_cast<Int256>(10000000000000000ll),
static_cast<Int256>(100000000000000000ll),
i10e18,
i10e18 * 10ll,
i10e18 * 100ll,
i10e18 * 1000ll,
i10e18 * 10000ll,
i10e18 * 100000ll,
i10e18 * 1000000ll,
i10e18 * 10000000ll,
i10e18 * 100000000ll,
i10e18 * 1000000000ll,
i10e18 * 10000000000ll,
i10e18 * 100000000000ll,
i10e18 * 1000000000000ll,
i10e18 * 10000000000000ll,
i10e18 * 100000000000000ll,
i10e18 * 1000000000000000ll,
i10e18 * 10000000000000000ll,
i10e18 * 100000000000000000ll,
i10e18 * 100000000000000000ll * 10ll,
i10e18 * 100000000000000000ll * 100ll,
i10e18 * 100000000000000000ll * 1000ll,
i10e18 * 100000000000000000ll * 10000ll,
i10e18 * 100000000000000000ll * 100000ll,
i10e18 * 100000000000000000ll * 1000000ll,
i10e18 * 100000000000000000ll * 10000000ll,
i10e18 * 100000000000000000ll * 100000000ll,
i10e18 * 100000000000000000ll * 1000000000ll,
i10e18 * 100000000000000000ll * 10000000000ll,
i10e18 * 100000000000000000ll * 100000000000ll,
i10e18 * 100000000000000000ll * 1000000000000ll,
i10e18 * 100000000000000000ll * 10000000000000ll,
i10e18 * 100000000000000000ll * 100000000000000ll,
i10e18 * 100000000000000000ll * 1000000000000000ll,
i10e18 * 100000000000000000ll * 10000000000000000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 10ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 100ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 1000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 10000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 100000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 1000000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 10000000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 100000000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 1000000000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 10000000000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 100000000000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 1000000000000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 10000000000000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 100000000000000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 1000000000000000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 10000000000000000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 100000000000000000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 100000000000000000ll * 10ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 100000000000000000ll * 100ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 100000000000000000ll * 1000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 100000000000000000ll * 10000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 100000000000000000ll * 100000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 100000000000000000ll * 1000000ll,
i10e18 * 100000000000000000ll * 100000000000000000ll * 100000000000000000ll *
10000000ll,
};
return values[x];
}

} // namespace common
14 changes: 13 additions & 1 deletion be/src/vec/core/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ class Field {
Bitmap = 27,
HyperLogLog = 28,
QuantileState = 29,
Decimal256 = 30,
};

static const int MIN_NON_POD = 16;
Expand Down Expand Up @@ -355,6 +356,8 @@ class Field {
return "Decimal128";
case Decimal128I:
return "Decimal128I";
case Decimal256:
return "Decimal256";
case FixedLengthObject:
return "FixedLengthObject";
case VariantMap:
Expand Down Expand Up @@ -562,7 +565,8 @@ class Field {
std::aligned_union_t<DBMS_MIN_FIELD_SIZE - sizeof(Types::Which), Null, UInt64, UInt128, Int64,
Int128, Float64, String, JsonbField, Array, Tuple, Map, VariantMap,
DecimalField<Decimal32>, DecimalField<Decimal64>, DecimalField<Decimal128>,
DecimalField<Decimal128I>, BitmapValue, HyperLogLog, QuantileState>
DecimalField<Decimal128I>, DecimalField<Decimal256>, BitmapValue,
HyperLogLog, QuantileState>
storage;

Types::Which which;
Expand Down Expand Up @@ -753,6 +757,10 @@ struct TypeId<DecimalField<Decimal128I>> {
static constexpr const TypeIndex value = TypeIndex::Decimal128I;
};
template <>
struct TypeId<DecimalField<Decimal256>> {
static constexpr const TypeIndex value = TypeIndex::Decimal256;
};
template <>
struct Field::TypeToEnum<Null> {
static constexpr Types::Which value = Types::Null;
};
Expand Down Expand Up @@ -813,6 +821,10 @@ struct Field::TypeToEnum<DecimalField<Decimal128I>> {
static constexpr Types::Which value = Types::Decimal128I;
};
template <>
struct Field::TypeToEnum<DecimalField<Decimal256>> {
static constexpr Types::Which value = Types::Decimal256;
};
template <>
struct Field::TypeToEnum<VariantMap> {
static constexpr Types::Which value = Types::VariantMap;
};
Expand Down
29 changes: 28 additions & 1 deletion be/src/vec/core/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include "common/consts.h"
#include "util/binary_cast.hpp"
#include "vec/common/int_exp.h"
#include "vec/core/wide_integer.h"

using wide::Int256;

namespace doris {

Expand Down Expand Up @@ -91,7 +94,8 @@ enum class TypeIndex {
VARIANT = 41,
QuantileState = 42,
Time = 43,
AggState
AggState = 44,
Decimal256
};

struct Consted {
Expand Down Expand Up @@ -299,6 +303,10 @@ template <>
inline constexpr Int128 decimal_scale_multiplier<Int128>(UInt32 scale) {
return common::exp10_i128(scale);
}
template <>
inline constexpr Int256 decimal_scale_multiplier<Int256>(UInt32 scale) {
return common::exp10_i256(scale);
}

/// Own FieldType for Decimal.
/// It is only a "storage" for decimal. To perform operations, you also have to provide a scale (number of digits after point).
Expand All @@ -313,6 +321,7 @@ struct Decimal {
#define DECLARE_NUMERIC_CTOR(TYPE) \
Decimal(const TYPE& value_) : value(value_) {}

DECLARE_NUMERIC_CTOR(Int256)
DECLARE_NUMERIC_CTOR(Int128)
DECLARE_NUMERIC_CTOR(Int32)
DECLARE_NUMERIC_CTOR(Int64)
Expand Down Expand Up @@ -524,6 +533,7 @@ struct Decimal128I : public Decimal<Int128> {
using Decimal32 = Decimal<Int32>;
using Decimal64 = Decimal<Int64>;
using Decimal128 = Decimal<Int128>;
using Decimal256 = Decimal<Int256>;

template <>
struct TypeName<Decimal32> {
Expand All @@ -542,6 +552,11 @@ struct TypeName<Decimal128I> {
static const char* get() { return "Decimal128I"; }
};

template <>
struct TypeName<Decimal256> {
static const char* get() { return "Decimal256"; }
};

template <>
struct TypeId<Decimal32> {
static constexpr const TypeIndex value = TypeIndex::Decimal32;
Expand All @@ -558,6 +573,10 @@ template <>
struct TypeId<Decimal128I> {
static constexpr const TypeIndex value = TypeIndex::Decimal128I;
};
template <>
struct TypeId<Decimal256> {
static constexpr const TypeIndex value = TypeIndex::Decimal256;
};

template <typename T>
constexpr bool IsDecimalNumber = false;
Expand All @@ -569,6 +588,8 @@ template <>
inline constexpr bool IsDecimalNumber<Decimal128> = true;
template <>
inline constexpr bool IsDecimalNumber<Decimal128I> = true;
template <>
inline constexpr bool IsDecimalNumber<Decimal256> = true;

template <typename T>
constexpr bool IsDecimal128 = false;
Expand Down Expand Up @@ -614,6 +635,10 @@ template <>
struct NativeType<Decimal128I> {
using Type = Int128;
};
template <>
struct NativeType<Decimal256> {
using Type = Int256;
};

inline const char* getTypeName(TypeIndex idx) {
switch (idx) {
Expand Down Expand Up @@ -669,6 +694,8 @@ inline const char* getTypeName(TypeIndex idx) {
return TypeName<Decimal128>::get();
case TypeIndex::Decimal128I:
return TypeName<Decimal128I>::get();
case TypeIndex::Decimal256:
return TypeName<Decimal256>::get();
case TypeIndex::UUID:
return "UUID";
case TypeIndex::Array:
Expand Down
Loading

0 comments on commit c3d1a1a

Please sign in to comment.