iceberg-cpp
Loading...
Searching...
No Matches
truncate_util.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21
22#include <cstdint>
23#include <optional>
24#include <string>
25#include <utility>
26
27#include "iceberg/iceberg_export.h"
28#include "iceberg/result.h"
29#include "iceberg/type_fwd.h"
30
31namespace iceberg {
32
33class ICEBERG_EXPORT TruncateUtils {
34 public:
42 static std::string TruncateUTF8(std::string source, size_t L) {
43 size_t code_point_count = 0;
44 size_t safe_point = 0;
45
46 for (size_t i = 0; i < source.size(); ++i) {
47 // Start of a new UTF-8 code point
48 if ((source[i] & 0xC0) != 0x80) {
49 code_point_count++;
50 if (code_point_count > static_cast<size_t>(L)) {
51 safe_point = i;
52 break;
53 }
54 }
55 }
56
57 if (safe_point != 0) {
58 // Resize the string to the safe point
59 source.resize(safe_point);
60 }
61
62 return source;
63 }
64
77 static Result<std::optional<std::string>> TruncateUTF8Max(const std::string& source,
78 size_t L);
79
84 template <typename T>
85 requires std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t>
86 static inline T TruncateInteger(T v, int32_t W) {
87 return v - (((v % W) + W) % W);
88 }
89
94 static Decimal TruncateDecimal(const Decimal& decimal, int32_t width);
95
104 static Result<Literal> TruncateLiteral(const Literal& literal, int32_t width);
105
117 static Result<std::optional<Literal>> TruncateLiteralMax(const Literal& value,
118 int32_t width);
119
129 static Result<Literal> TruncateLowerBound(const PrimitiveType& type,
130 const Literal& value, int32_t width);
131
142 static Result<std::optional<Literal>> TruncateUpperBound(const PrimitiveType& type,
143 const Literal& value,
144 int32_t width);
145};
146
147} // namespace iceberg
Represents 128-bit fixed-point decimal numbers. The max decimal precision that can be safely represen...
Definition decimal.h:46
Literal is a literal value that is associated with a primitive type.
Definition literal.h:39
A data type that does not have child fields.
Definition type.h:78
Definition truncate_util.h:33
static T TruncateInteger(T v, int32_t W)
Truncate an integer v, either int32_t or int64_t, to v - (v % W).
Definition truncate_util.h:86
static std::string TruncateUTF8(std::string source, size_t L)
Truncate a UTF-8 string to a specified number of code points.
Definition truncate_util.h:42