be/src/util/variant/variant_scalar_encoding.h
Line | Count | Source |
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 | | #pragma once |
19 | | |
20 | | #include <array> |
21 | | #include <cstddef> |
22 | | #include <cstdint> |
23 | | |
24 | | #include "core/string_ref.h" |
25 | | |
26 | | namespace doris { |
27 | | |
28 | | // A validated, stack-only plan for one physical Variant scalar. The plan either owns a small |
29 | | // inline textual fallback or borrows a StringRef until write() returns; it never allocates. |
30 | | // Callers can therefore preflight a complete batch, reserve its final buffer once, and write |
31 | | // directly into that buffer without constructing a VariantBuilder per row. |
32 | | class VariantScalarEncodingPlan { |
33 | | public: |
34 | | static VariantScalarEncodingPlan null_value() noexcept; |
35 | | static VariantScalarEncodingPlan boolean(bool value) noexcept; |
36 | | // width == 0 selects the narrowest signed integer encoding. Otherwise width must be one of |
37 | | // 1, 2, 4, or 8 and value must fit that signed width. |
38 | | static VariantScalarEncodingPlan integer(int64_t value, uint8_t width = 0); |
39 | | static VariantScalarEncodingPlan float32(float value) noexcept; |
40 | | static VariantScalarEncodingPlan float64(double value) noexcept; |
41 | | // width == 0 selects the narrowest decimal width. Otherwise width must be 4, 8, or 16. |
42 | | static VariantScalarEncodingPlan decimal(__int128 unscaled, uint8_t scale, uint8_t width = 0); |
43 | | static VariantScalarEncodingPlan date(int32_t days_since_epoch) noexcept; |
44 | | static VariantScalarEncodingPlan timestamp_micros(int64_t value, bool utc_adjusted) noexcept; |
45 | | static VariantScalarEncodingPlan timestamp_nanos(int64_t value, bool utc_adjusted) noexcept; |
46 | | static VariantScalarEncodingPlan time_ntz_micros(int64_t value) noexcept; |
47 | | static VariantScalarEncodingPlan binary(StringRef value); |
48 | | static VariantScalarEncodingPlan string(StringRef value); |
49 | | static VariantScalarEncodingPlan uuid(const std::array<uint8_t, 16>& value) noexcept; |
50 | | // Values outside decimal38 are encoded as their canonical decimal string representation. |
51 | | static VariantScalarEncodingPlan largeint(__int128 value) noexcept; |
52 | | |
53 | 3.26M | size_t size() const noexcept { return _encoded_size; } |
54 | | bool used_string_fallback() const noexcept { return _used_string_fallback; } |
55 | | |
56 | | // Writes exactly size() bytes. Invalid destination/capacity fails before modifying memory. |
57 | | void write(char* destination, size_t capacity) const; |
58 | | |
59 | | private: |
60 | 1.08M | VariantScalarEncodingPlan() = default; |
61 | | |
62 | | enum class PayloadKind : uint8_t { |
63 | | HEADER_ONLY, |
64 | | UNSIGNED, |
65 | | SIGNED, |
66 | | BORROWED_BYTES, |
67 | | UUID, |
68 | | INLINE_STRING, |
69 | | }; |
70 | | |
71 | | uint64_t _unsigned_value = 0; |
72 | | __int128 _signed_value = 0; |
73 | | StringRef _borrowed; |
74 | | std::array<uint8_t, 16> _uuid {}; |
75 | | std::array<char, 40> _inline_string {}; |
76 | | size_t _encoded_size = 0; |
77 | | uint8_t _header = 0; |
78 | | uint8_t _payload_width = 0; |
79 | | uint8_t _scale = 0; |
80 | | uint8_t _inline_size = 0; |
81 | | PayloadKind _kind = PayloadKind::HEADER_ONLY; |
82 | | bool _has_scale = false; |
83 | | bool _has_length = false; |
84 | | bool _used_string_fallback = false; |
85 | | }; |
86 | | |
87 | | } // namespace doris |