yiguolei commented on code in PR #68664:
URL: https://github.com/apache/doris/pull/68664#discussion_r4145225026


##########
be/src/storage/storage_layout.h:
##########
@@ -0,0 +1,160 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#pragma once
+
+#include <bit>
+#include <cmath>
+#include <cstdint>
+#include <limits>
+#include <type_traits>
+
+#include "common/cast_set.h"
+#include "core/data_type/primitive_type.h"
+#include "core/data_type/storage_field_type.h"
+#include "core/decimal12.h"
+#include "core/uint24.h"
+#include "core/value/decimalv2_value.h"
+#include "core/value/vdatetime_value.h"
+#include "storage/field_type.h"
+#include "storage/types.h"
+
+namespace doris {
+
+// A FieldType's StorageValue is what a segment stores for one value of it. For
+// most FieldTypes it is the compute-layer value itself; where it is not -- the
+// V1 DATE, DATETIME and DECIMAL layouts and NaN canonicalisation for
+// FLOAT/DOUBLE -- StorageLayout<FT> says how a compute-layer value becomes the
+// StorageValue.
+//
+// Which PrimitiveType a FieldType holds is not repeated here:
+// core/data_type/storage_field_type.h owns that pairing, and it is constexpr,
+// so a template picks its layout with
+// StorageLayout<primitive_type_to_storage_field_type(PT)>.
+//
+// Declared only: a FieldType without a specialisation below has no storage
+// form, and using it is a compile error rather than a silent byte copy. Every
+// specialisation gives StorageValue, the C++ type of what is stored on disk,
+// PrimitiveValue, the compute layer's C++ type for the same value, and
+// to_storage(PrimitiveValue) -> StorageValue. FLOAT and DOUBLE also give
+// canonicalize_nan, the NaN rule to_storage applies, for code that holds a
+// StorageValue already.
+template <FieldType FT>
+struct StorageLayout;
+
+namespace storage_layout_detail {
+
+// A StorageValue is the PrimitiveValue's own bytes.
+template <FieldType FT>
+struct BitCast {
+    using StorageValue = typename CppTypeTraits<FT>::CppType;
+    using PrimitiveValue =
+            typename 
PrimitiveTypeTraits<storage_field_type_to_primitive_type(FT)>::CppType;
+    static_assert(sizeof(StorageValue) == sizeof(PrimitiveValue));
+
+    static StorageValue to_storage(const PrimitiveValue& value) {
+        return std::bit_cast<StorageValue>(value);
+    }
+};
+
+// Every NaN is stored as the quiet NaN, so equal values have equal stored 
bytes
+// and the bloom filter / dictionary / key encodings that hash or compare bytes
+// treat all NaNs as one value.
+template <FieldType FT>
+struct FloatingPoint {
+    using StorageValue = typename CppTypeTraits<FT>::CppType;
+    using PrimitiveValue = StorageValue;
+    static_assert(std::is_floating_point_v<StorageValue>);
+
+    static StorageValue canonicalize_nan(StorageValue value) {
+        return std::isnan(value) ? 
std::numeric_limits<StorageValue>::quiet_NaN() : value;
+    }
+    static StorageValue to_storage(PrimitiveValue value) { return 
canonicalize_nan(value); }
+};
+
+} // namespace storage_layout_detail
+
+#define DORIS_STORAGE_LAYOUT_BIT_CAST(FT) \
+    template <>                           \
+    struct StorageLayout<FieldType::FT> : 
storage_layout_detail::BitCast<FieldType::FT> {};
+
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_BOOL)
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_TINYINT)
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_SMALLINT)
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_INT)
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_BIGINT)
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_LARGEINT)
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_UNSIGNED_INT)
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_UNSIGNED_BIGINT)
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_DECIMAL32)
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_DECIMAL64)
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_DECIMAL128I)
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_DECIMAL256)
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_DATEV2)
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_DATETIMEV2)
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_TIMESTAMP_NS)
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_TIMESTAMPTZ)
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_IPV4)
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_IPV6)
+DORIS_STORAGE_LAYOUT_BIT_CAST(OLAP_FIELD_TYPE_UUID)
+#undef DORIS_STORAGE_LAYOUT_BIT_CAST
+
+template <>
+struct StorageLayout<FieldType::OLAP_FIELD_TYPE_FLOAT>
+        : 
storage_layout_detail::FloatingPoint<FieldType::OLAP_FIELD_TYPE_FLOAT> {};
+
+template <>
+struct StorageLayout<FieldType::OLAP_FIELD_TYPE_DOUBLE>
+        : 
storage_layout_detail::FloatingPoint<FieldType::OLAP_FIELD_TYPE_DOUBLE> {};
+
+// V1 DATE: three bytes of (year << 9) | (month << 5) | day.
+template <>
+struct StorageLayout<FieldType::OLAP_FIELD_TYPE_DATE> {
+    using StorageValue = typename 
CppTypeTraits<FieldType::OLAP_FIELD_TYPE_DATE>::CppType;
+    using PrimitiveValue = VecDateTimeValue;
+    static_assert(std::is_same_v<StorageValue, uint24_t>);
+
+    static StorageValue to_storage(const PrimitiveValue& value) {
+        return StorageValue(cast_set<uint32_t>(value.to_olap_date()));
+    }
+};
+
+// V1 DATETIME: the decimal digits YYYYMMDDhhmmss packed into one integer.
+template <>
+struct StorageLayout<FieldType::OLAP_FIELD_TYPE_DATETIME> {
+    using StorageValue = typename 
CppTypeTraits<FieldType::OLAP_FIELD_TYPE_DATETIME>::CppType;
+    using PrimitiveValue = VecDateTimeValue;
+    static_assert(std::is_same_v<StorageValue, int64_t>);
+
+    static StorageValue to_storage(const PrimitiveValue& value) {
+        return static_cast<StorageValue>(value.to_olap_datetime());
+    }
+};
+
+// V1 DECIMAL: the integer and the nine-digit fraction stored side by side.
+template <>
+struct StorageLayout<FieldType::OLAP_FIELD_TYPE_DECIMAL> {

Review Comment:
   template <>
   struct PrimitiveTypeTraits<TYPE_DECIMALV2> {
       using CppType = DecimalV2Value;
       /// Different with compute layer, the DecimalV1 was stored as 
decimal12_t(12 bytes).
       using StorageFieldType = decimal12_t;
       using DataType = DataTypeDecimalV2;
       using ColumnType = ColumnDecimal128V2;
   };
   
   跟之前这个定义的区别是啥



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to